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.

116 lines
4.0KB

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