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.

ProductFamilyResolver.php 4.5KB

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