您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

163 行
5.7KB

  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. //TODO voir différence entre prix ici et dans tableau décli
  70. return $this->applyReductionCatalog(
  71. $product,
  72. $this->getPrice($product),
  73. $this->getPriceWithTax($product)
  74. );
  75. }
  76. //Bridge pour applyReductionCatalog qui ne peut pas être appeler à cause du call
  77. public function getPriceWithTaxByReduction(
  78. ProductPropertyInterface $product,
  79. ReductionCatalogInterface $reductionCatalog
  80. ) {
  81. return $this->applyReductionCatalog(
  82. $product,
  83. $this->getPrice($product),
  84. $this->getPriceWithTax($product),
  85. 1,
  86. $reductionCatalog
  87. );
  88. }
  89. public function getPriceByRefUnitWithTaxAndReduction(ProductPropertyInterface $product)
  90. {
  91. $priceWithTax = $this->getPriceWithTax($product);
  92. if ($priceWithTax) {
  93. return $this->round(
  94. ($this->getPriceByRefUnitWithTax($product) * $this->getPriceWithTaxAndReduction($product))
  95. / $priceWithTax
  96. );
  97. }
  98. return 0;
  99. }
  100. public function getBuyingPrice(ProductPropertyInterface $product)
  101. {
  102. $solver = $this->getSolver($product);
  103. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  104. return $solver->getBuyingPriceInherited($product);
  105. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  106. if ($solver->getQuantityInherited($product) > 0) {
  107. return $solver->getBuyingPriceByRefUnitInherited($product) * ($solver->getQuantityInherited(
  108. $product
  109. ) / $solver->getUnitInherited($product)->getCoefficient());
  110. } else {
  111. return 0;
  112. }
  113. }
  114. }
  115. public function getBuyingPriceWithTax(ProductPropertyInterface $product)
  116. {
  117. return $this->applyTax(
  118. $this->getBuyingPrice($product),
  119. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  120. );
  121. }
  122. public function getBuyingPriceByRefUnit(ProductPropertyInterface $product)
  123. {
  124. return $this->getSolver($product)->getBuyingPriceByRefUnitInherited($product);
  125. }
  126. public function getBuyingPriceByRefUnitWithTax(ProductPropertyInterface $product)
  127. {
  128. return $this->applyTax(
  129. $this->getBuyingPriceByRefUnit($product),
  130. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  131. );
  132. }
  133. public function getMultiplyingFactor(ProductPropertyInterface $product)
  134. {
  135. return $this->round($this->getPriceWithTax($product) / $this->getBuyingPrice($product));
  136. }
  137. }