Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

ProductPriceSolver.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 getPriceWithReduction(ProductPropertyInterface $product)
  44. {
  45. return $this->applyReductionCatalog(
  46. $product,
  47. $this->getPrice($product),
  48. $this->getPriceWithTax($product),
  49. 1,
  50. null,
  51. false
  52. );
  53. }
  54. public function getPriceWithTax(ProductPropertyInterface $product)
  55. {
  56. return $this->applyTax(
  57. $this->getPrice($product),
  58. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  59. );
  60. }
  61. public function getPriceByRefUnit(ProductPropertyInterface $product)
  62. {
  63. $solver = $this->getSolver($product);
  64. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  65. return ($this->getPrice($product) * $solver->getUnitInherited($product)->getCoefficient()) / $solver->getQuantityInherited($product);
  66. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  67. return $solver->getPriceByRefUnitInherited($product);
  68. }
  69. }
  70. public function getPriceByRefUnitWithTax(ProductPropertyInterface $product)
  71. {
  72. return $this->applyTax(
  73. $this->getPriceByRefUnit($product),
  74. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  75. );
  76. }
  77. public function getPriceWithTaxAndReduction(ProductPropertyInterface $product)
  78. {
  79. //TODO voir différence entre prix ici et dans tableau décli
  80. return $this->applyReductionCatalog(
  81. $product,
  82. $this->getPrice($product),
  83. $this->getPriceWithTax($product)
  84. );
  85. }
  86. //Bridge pour applyReductionCatalog qui ne peut pas être appeler à cause du call
  87. public function getPriceWithTaxByReduction(
  88. ProductPropertyInterface $product,
  89. ReductionCatalogInterface $reductionCatalog
  90. )
  91. {
  92. return $this->applyReductionCatalog(
  93. $product,
  94. $this->getPrice($product),
  95. $this->getPriceWithTax($product),
  96. 1,
  97. $reductionCatalog
  98. );
  99. }
  100. public function getPriceByRefUnitWithTaxAndReduction(ProductPropertyInterface $product)
  101. {
  102. $priceWithTax = $this->getPriceWithTax($product);
  103. if ($priceWithTax) {
  104. return $this->round(
  105. ($this->getPriceByRefUnitWithTax($product) * $this->getPriceWithTaxAndReduction($product))
  106. / $priceWithTax
  107. );
  108. }
  109. return 0;
  110. }
  111. public function getBuyingPrice(ProductPropertyInterface $product)
  112. {
  113. $solver = $this->getSolver($product);
  114. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  115. return $solver->getBuyingPriceInherited($product);
  116. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  117. if ($solver->getQuantityInherited($product) > 0) {
  118. return $solver->getBuyingPriceByRefUnitInherited($product) * ($solver->getQuantityInherited(
  119. $product
  120. ) / $solver->getUnitInherited($product)->getCoefficient());
  121. } else {
  122. return 0;
  123. }
  124. }
  125. }
  126. public function getBuyingPriceWithTax(ProductPropertyInterface $product)
  127. {
  128. return $this->applyTax(
  129. $this->getBuyingPrice($product),
  130. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  131. );
  132. }
  133. public function getBuyingPriceByRefUnit(ProductPropertyInterface $product)
  134. {
  135. return $this->getSolver($product)->getBuyingPriceByRefUnitInherited($product);
  136. }
  137. public function getBuyingPriceByRefUnitWithTax(ProductPropertyInterface $product)
  138. {
  139. return $this->applyTax(
  140. $this->getBuyingPriceByRefUnit($product),
  141. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  142. );
  143. }
  144. public function getMultiplyingFactor(ProductPropertyInterface $product)
  145. {
  146. return $this->round($this->getPriceWithTax($product) / $this->getBuyingPrice($product));
  147. }
  148. }