Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

57 lines
2.1KB

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