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.

62 lines
2.3KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Product;
  3. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  4. use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
  5. use Lc\SovBundle\Repository\AbstractStore;
  6. class ProductStore extends AbstractStore
  7. {
  8. protected ProductRepositoryQuery $query;
  9. protected PriceResolver $priceResolver;
  10. public function __construct(ProductRepositoryQuery $query, PriceResolver $priceResolver)
  11. {
  12. $this->query = $query;
  13. $this->priceResolver = $priceResolver;
  14. }
  15. public function getCheapestProduct($productFamily)
  16. {
  17. $priceResolver = $this->priceResolver;
  18. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) {
  19. return $priceResolver->getPriceWithTaxAndReduction($a) > $priceResolver->getPriceWithTaxAndReduction($b);
  20. }, true);
  21. }
  22. public function getCheapestProductByRefUnit(ProductFamilyInterface $productFamily)
  23. {
  24. $priceResolver = $this->priceResolver;
  25. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) {
  26. return $priceResolver->getPriceByRefUnitWithTaxAndReduction($a) > $priceResolver->getPriceByRefUnitWithTaxAndReduction($b);
  27. }, false);
  28. }
  29. public function getMostExpensiveProductByRefUnit(ProductFamilyInterface $productFamily)
  30. {
  31. $priceResolver = $this->priceResolver;
  32. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) {
  33. return $priceResolver->getPriceByRefUnitWithTaxAndReduction($a) < $priceResolver->getPriceByRefUnitWithTaxAndReduction($b);
  34. }, false);
  35. }
  36. private function getCheapestOrMostExpensiveProduct(ProductFamilyInterface $productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts)
  37. {
  38. if ($productFamily->getActiveProducts()) {
  39. $products = $productFamily->getProductsOnline()->getValues();
  40. if (count($products) > 0) {
  41. usort($products, $comparisonFunction);
  42. return $products[0];
  43. }
  44. } else {
  45. return $productFamily->getOriginProduct();
  46. }
  47. if ($returnSelfIfNotActiveProducts) {
  48. return $productFamily;
  49. } else {
  50. return false;
  51. }
  52. }
  53. }