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.

128 lines
4.5KB

  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. return $product;
  96. // Retourner le produit en fonction de sa disponibilité
  97. // if ($this->orderShopSolver->isProductAvailable($orderShop->getSection(), $orderShop, $product, 1, true))
  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. }