Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

ProductFamilyUtils.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. }