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.

54 lines
2.0KB

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