|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- use Lc\ShopBundle\Services\Price\ProductPriceUtils;
-
- class ProductFamilyUtils
- {
- protected $productPriceUtils ;
-
- public function __construct(ProductPriceUtils $productPriceUtils)
- {
- $this->productPriceUtils = $productPriceUtils ;
- }
-
- public function getCheapestProduct($productFamily)
- {
- $productPriceUtils = $this->productPriceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) {
- return $productPriceUtils->getPriceWithTaxAndReduction($a) > $productPriceUtils->getPriceWithTaxAndReduction($b) ;
- }, true);
- }
-
- public function getCheapestProductByRefUnit($productFamily)
- {
- $productPriceUtils = $this->productPriceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) {
- return $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
- }, false);
- }
-
- public function getMostExpensiveProductByRefUnit($productFamily)
- {
- $productPriceUtils = $this->productPriceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) {
- return $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
- }, false);
- }
-
- private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts)
- {
- $products = $productFamily->getProducts()->getValues() ;
- if (count($products) > 0) {
- usort($products, $comparisonFunction);
- return $products[0];
- }
- if ($returnSelfIfNotActiveProducts) {
- return $productFamily;
- }
- else {
- return false;
- }
- }
-
- }
|