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.

119 lines
4.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Product;
  3. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  4. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  5. use Lc\CaracoleBundle\Repository\SectionStoreTrait;
  6. use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
  7. use Lc\SovBundle\Repository\AbstractStore;
  8. class ProductFamilyStore extends AbstractStore
  9. {
  10. use SectionStoreTrait;
  11. protected ProductFamilyRepositoryQuery $query;
  12. protected PriceResolver $priceResolver;
  13. public function __construct(ProductFamilyRepositoryQuery $query, PriceResolver $priceResolver)
  14. {
  15. $this->query = $query;
  16. $this->priceResolver = $priceResolver;
  17. }
  18. public function isOneProductAvailableAddCart(array $products): bool
  19. {
  20. foreach ($products as $product) {
  21. if ($product->isAvailable(1, true)) {
  22. return true;
  23. }
  24. }
  25. return false;
  26. }
  27. public function getMultiplyingFactor($productFamily)
  28. {
  29. if ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  30. if ($productFamily->getBuyingPrice() > 0) {
  31. return number_format(
  32. $this->priceResolver->getPriceWithTax($productFamily) / $productFamily->getBuyingPrice(),
  33. 3
  34. );
  35. }
  36. } elseif ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  37. if ($productFamily->getBuyingPriceByRefUnit() > 0) {
  38. return number_format(
  39. $this->priceResolver->getPriceByRefUnitWithTax(
  40. $productFamily
  41. ) / $productFamily->getBuyingPriceByRefUnit(),
  42. 3
  43. );
  44. }
  45. }
  46. }
  47. public function getCheapestProduct(ProductFamilyInterface $productFamily)
  48. {
  49. $priceResolver = $this->priceResolver;
  50. return $this->getCheapestOrMostExpensiveProduct(
  51. $productFamily,
  52. function ($a, $b) use ($priceResolver) {
  53. return $priceResolver->getPriceWithTaxAndReduction(
  54. $a
  55. ) > $priceResolver->getPriceWithTaxAndReduction($b);
  56. },
  57. true
  58. );
  59. }
  60. public function getCheapestProductByRefUnit(ProductFamilyInterface $productFamily)
  61. {
  62. $priceResolver = $this->priceResolver;
  63. return $this->getCheapestOrMostExpensiveProduct(
  64. $productFamily,
  65. function ($a, $b) use ($priceResolver) {
  66. return $priceResolver->getPriceByRefUnitWithTaxAndReduction(
  67. $a
  68. ) > $priceResolver->getPriceByRefUnitWithTaxAndReduction($b);
  69. },
  70. false
  71. );
  72. }
  73. public function getMostExpensiveProductByRefUnit(ProductFamilyInterface $productFamily)
  74. {
  75. $priceResolver = $this->priceResolver;
  76. return $this->getCheapestOrMostExpensiveProduct(
  77. $productFamily,
  78. function ($a, $b) use ($priceResolver) {
  79. return $priceResolver->getPriceByRefUnitWithTaxAndReduction(
  80. $a
  81. ) < $priceResolver->getPriceByRefUnitWithTaxAndReduction($b);
  82. },
  83. false
  84. );
  85. }
  86. private function getCheapestOrMostExpensiveProduct(
  87. ProductFamilyInterface $productFamily,
  88. $comparisonFunction,
  89. $returnSelfIfNotActiveProducts
  90. ) {
  91. if ($productFamily->getActiveProducts()) {
  92. $products = $productFamily->getProductsOnline()->getValues();
  93. if (count($products) > 0) {
  94. usort($products, $comparisonFunction);
  95. return $products[0];
  96. }
  97. } else {
  98. return $productFamily->getOriginProduct();
  99. }
  100. if ($returnSelfIfNotActiveProducts) {
  101. return $productFamily;
  102. } else {
  103. return false;
  104. }
  105. }
  106. }