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.

162 lines
5.6KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Price;
  3. use Lc\CaracoleBundle\Doctrine\Extension\ProductPropertyInterface;
  4. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  5. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  6. use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
  7. use Lc\CaracoleBundle\Solver\Product\ProductFamilySolver;
  8. use Lc\CaracoleBundle\Solver\Product\ProductSolver;
  9. class ProductPriceSolver
  10. {
  11. use PriceSolverTrait;
  12. protected ProductSolver $productSolver;
  13. protected ProductFamilySolver $productFamilySolver;
  14. public function __construct(ProductSolver $productSolver, ProductFamilySolver $productFamilySolver)
  15. {
  16. $this->productSolver = $productSolver;
  17. $this->productFamilySolver = $productFamilySolver;
  18. }
  19. public function getSolver(ProductPropertyInterface $product)
  20. {
  21. if ($product instanceof ProductFamilyInterface) {
  22. return $this->productFamilySolver;
  23. }
  24. if ($product instanceof ProductInterface) {
  25. return $this->productSolver;
  26. }
  27. }
  28. public function getPrice(ProductPropertyInterface $product)
  29. {
  30. $solver = $this->getSolver($product);
  31. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  32. return $solver->getPriceInherited($product);
  33. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  34. if ($solver->getQuantityInherited($product) > 0) {
  35. return $solver->getPriceByRefUnitInherited($product) * ($solver->getQuantityInherited(
  36. $product
  37. ) / $solver->getUnitInherited($product)->getCoefficient());
  38. } else {
  39. return 0;
  40. }
  41. }
  42. }
  43. public function getPriceWithTax(ProductPropertyInterface $product)
  44. {
  45. return $this->applyTax(
  46. $this->getPrice($product),
  47. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  48. );
  49. }
  50. public function getPriceByRefUnit(ProductPropertyInterface $product)
  51. {
  52. $solver = $this->getSolver($product);
  53. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  54. return ($this->getPrice($product) * $solver->getUnitInherited($product)->getCoefficient(
  55. )) / $solver->getQuantityInherited($product);
  56. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  57. return $solver->getPriceByRefUnitInherited($product);
  58. }
  59. }
  60. public function getPriceByRefUnitWithTax(ProductPropertyInterface $product)
  61. {
  62. return $this->applyTax(
  63. $this->getPriceByRefUnit($product),
  64. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  65. );
  66. }
  67. public function getPriceWithTaxAndReduction(ProductPropertyInterface $product)
  68. {
  69. return $this->applyReductionCatalog(
  70. $product,
  71. $this->getPrice($product),
  72. $this->getPriceWithTax($product)
  73. );
  74. }
  75. //Bridge pour applyReductionCatalog qui ne peut pas être appeler à cause du call
  76. public function getPriceWithTaxByReduction(
  77. ProductPropertyInterface $product,
  78. ReductionCatalogInterface $reductionCatalog
  79. ) {
  80. return $this->applyReductionCatalog(
  81. $product,
  82. $this->getPrice($product),
  83. $this->getPriceWithTax($product),
  84. 1,
  85. $reductionCatalog
  86. );
  87. }
  88. public function getPriceByRefUnitWithTaxAndReduction(ProductPropertyInterface $product)
  89. {
  90. $priceWithTax = $this->getPriceWithTax($product);
  91. if ($priceWithTax) {
  92. return $this->round(
  93. ($this->getPriceByRefUnitWithTax($product) * $this->getPriceWithTaxAndReduction($product))
  94. / $priceWithTax
  95. );
  96. }
  97. return 0;
  98. }
  99. public function getBuyingPrice(ProductPropertyInterface $product)
  100. {
  101. $solver = $this->getSolver($product);
  102. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  103. return $solver->getBuyingPriceInherited($product);
  104. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  105. if ($solver->getQuantityInherited($product) > 0) {
  106. return $solver->getBuyingPriceByRefUnitInherited($product) * ($solver->getQuantityInherited(
  107. $product
  108. ) / $solver->getUnitInherited($product)->getCoefficient());
  109. } else {
  110. return 0;
  111. }
  112. }
  113. }
  114. public function getBuyingPriceWithTax(ProductPropertyInterface $product)
  115. {
  116. return $this->applyTax(
  117. $this->getBuyingPrice($product),
  118. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  119. );
  120. }
  121. public function getBuyingPriceByRefUnit(ProductPropertyInterface $product)
  122. {
  123. return $this->getSolver($product)->getBuyingPriceByRefUnitInherited($product);
  124. }
  125. public function getBuyingPriceByRefUnitWithTax(ProductPropertyInterface $product)
  126. {
  127. return $this->applyTax(
  128. $this->getBuyingPriceByRefUnit($product),
  129. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  130. );
  131. }
  132. public function getMultiplyingFactor(ProductPropertyInterface $product)
  133. {
  134. return $this->round($this->getPriceWithTax($product) / $this->getBuyingPrice($product));
  135. }
  136. }