Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

154 lines
5.3KB

  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($product
  36. ) / $solver->getUnitInherited($product)->getCoefficient());
  37. } else {
  38. return 0;
  39. }
  40. }
  41. }
  42. public function getPriceWithTax(ProductPropertyInterface $product)
  43. {
  44. return $this->applyTax(
  45. $this->getPrice($product),
  46. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  47. );
  48. }
  49. public function getPriceByRefUnit(ProductPropertyInterface $product)
  50. {
  51. $solver = $this->getSolver($product);
  52. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  53. return ($this->getPrice($product) * $solver->getUnitInherited($product)->getCoefficient(
  54. )) / $solver->getQuantityInherited($product);
  55. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  56. return $solver->getPriceByRefUnitInherited($product);
  57. }
  58. }
  59. public function getPriceByRefUnitWithTax(ProductPropertyInterface $product)
  60. {
  61. return $this->applyTax(
  62. $this->getPriceByRefUnit($product),
  63. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  64. );
  65. }
  66. public function getPriceWithTaxAndReduction(ProductPropertyInterface $product)
  67. {
  68. return $this->applyReductionCatalog(
  69. $product,
  70. $this->getPrice($product),
  71. $this->getPriceWithTax($product)
  72. );
  73. }
  74. //Bridge pour applyReductionCatalog qui ne peut pas être appeler à cause du call
  75. public function getPriceWithTaxByReduction(ProductPropertyInterface $product, ReductionCatalogInterface $reductionCatalog)
  76. {
  77. return $this->applyReductionCatalog(
  78. $product,
  79. $this->getPrice($product),
  80. $this->getPriceWithTax($product),
  81. 1,
  82. $reductionCatalog
  83. );
  84. }
  85. public function getPriceByRefUnitWithTaxAndReduction(ProductPropertyInterface $product)
  86. {
  87. return ($this->getPriceByRefUnitWithTax($product) * $this->getPriceWithTaxAndReduction($product))
  88. / $this->getPriceWithTax($product);
  89. }
  90. public function getBuyingPrice(ProductPropertyInterface $product)
  91. {
  92. $solver = $this->getSolver($product);
  93. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  94. return $solver->getBuyingPriceInherited($product);
  95. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  96. if ($solver->getQuantityInherited($product) > 0) {
  97. return $solver->getBuyingPriceByRefUnitInherited($product) * ($solver->getQuantityInherited($product
  98. ) / $solver->getUnitInherited($product)->getCoefficient());
  99. } else {
  100. return 0;
  101. }
  102. }
  103. }
  104. public function getBuyingPriceWithTax(ProductPropertyInterface $product)
  105. {
  106. return $this->applyTax(
  107. $this->getBuyingPrice($product),
  108. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  109. );
  110. }
  111. public function getBuyingPriceByRefUnit(ProductPropertyInterface $product)
  112. {
  113. return $this->getSolver($product)->getBuyingPriceByRefUnitInherited($product);
  114. }
  115. public function getBuyingPriceByRefUnitWithTax(ProductPropertyInterface $product)
  116. {
  117. return $this->applyTax(
  118. $this->getBuyingPriceByRefUnit($product),
  119. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  120. );
  121. }
  122. public function getMultiplyingFactor(ProductPropertyInterface $product)
  123. {
  124. return $this->round($this->getPriceWithTax($product) / $this->getBuyingPrice($product));
  125. }
  126. }