You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductFamilyUtils.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Lc\ShopBundle\Price\Services\ProductPriceUtils;
  4. class ProductFamilyUtils
  5. {
  6. protected $productPriceUtils ;
  7. public function __construct(ProductPriceUtils $productPriceUtils)
  8. {
  9. $this->productPriceUtils = $productPriceUtils ;
  10. }
  11. public function getCheapestProduct($productFamily)
  12. {
  13. $productPriceUtils = $this->productPriceUtils ;
  14. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) {
  15. return $productPriceUtils->getPriceWithTaxAndReduction($a) > $productPriceUtils->getPriceWithTaxAndReduction($b) ;
  16. }, true);
  17. }
  18. public function getCheapestProductByRefUnit($productFamily)
  19. {
  20. $productPriceUtils = $this->productPriceUtils ;
  21. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) {
  22. return $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
  23. }, false);
  24. }
  25. public function getMostExpensiveProductByRefUnit($productFamily)
  26. {
  27. $productPriceUtils = $this->productPriceUtils ;
  28. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) {
  29. return $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
  30. }, false);
  31. }
  32. private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts)
  33. {
  34. $products = $productFamily->getProducts()->getValues() ;
  35. if (count($products) > 0) {
  36. usort($products, $comparisonFunction);
  37. return $products[0];
  38. }
  39. if ($returnSelfIfNotActiveProducts) {
  40. return $productFamily;
  41. }
  42. else {
  43. return false;
  44. }
  45. }
  46. }