選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

165 行
5.8KB

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