Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

141 lines
4.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\Solver\Product\ProductFamilySolver;
  7. use Lc\CaracoleBundle\Solver\Product\ProductSolver;
  8. class ProductPriceSolver
  9. {
  10. use PriceSolverTrait;
  11. protected ProductSolver $productSolver;
  12. protected ProductFamilySolver $productFamilySolver;
  13. public function __construct(ProductSolver $productSolver, ProductFamilySolver $productFamilySolver)
  14. {
  15. $this->productSolver = $productSolver;
  16. $this->productFamilySolver = $productFamilySolver;
  17. }
  18. public function getSolver(ProductPropertyInterface $product)
  19. {
  20. if($product instanceof ProductFamilyInterface) {
  21. return $this->productFamilySolver;
  22. }
  23. if($product instanceof ProductInterface) {
  24. return $this->productSolver;
  25. }
  26. }
  27. public function getPrice(ProductPropertyInterface $product)
  28. {
  29. $solver = $this->getSolver($product);
  30. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  31. return $solver->getPriceInherited($product);
  32. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  33. if ($solver->getQuantityInherited($product) > 0) {
  34. return $solver->getPriceByRefUnitInherited($product) * ($solver->getQuantityInherited($product
  35. ) / $solver->getUnitInherited($product)->getCoefficient());
  36. } else {
  37. return 0;
  38. }
  39. }
  40. }
  41. public function getPriceWithTax(ProductPropertyInterface $product)
  42. {
  43. return $this->applyTax(
  44. $this->getPrice($product),
  45. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  46. );
  47. }
  48. public function getPriceByRefUnit(ProductPropertyInterface $product)
  49. {
  50. $solver = $this->getSolver($product);
  51. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  52. return ($this->getPrice($product) * $solver->getUnitInherited($product)->getCoefficient(
  53. )) / $solver->getQuantityInherited($product);
  54. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  55. return $solver->getPriceByRefUnitInherited($product);
  56. }
  57. }
  58. public function getPriceByRefUnitWithTax(ProductPropertyInterface $product)
  59. {
  60. return $this->applyTax(
  61. $this->getPriceByRefUnit($product),
  62. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  63. );
  64. }
  65. public function getPriceWithTaxAndReduction(ProductPropertyInterface $product)
  66. {
  67. return $this->applyReductionCatalog(
  68. $product,
  69. $this->getPrice($product),
  70. $this->getPriceWithTax($product)
  71. );
  72. }
  73. public function getPriceByRefUnitWithTaxAndReduction(ProductPropertyInterface $product)
  74. {
  75. return ($this->getPriceByRefUnitWithTax($product) * $this->getPriceWithTaxAndReduction($product))
  76. / $this->getPriceWithTax($product);
  77. }
  78. public function getBuyingPrice(ProductPropertyInterface $product)
  79. {
  80. $solver = $this->getSolver($product);
  81. if ($solver->getBehaviorPriceInherited($product) == 'by-piece') {
  82. return $solver->getBuyingPriceInherited($product);
  83. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  84. if ($solver->getQuantityInherited($product) > 0) {
  85. return $solver->getBuyingPriceByRefUnitInherited($product) * ($solver->getQuantityInherited($product
  86. ) / $solver->getUnitInherited($product)->getCoefficient());
  87. } else {
  88. return 0;
  89. }
  90. }
  91. }
  92. public function getBuyingPriceWithTax(ProductPropertyInterface $product)
  93. {
  94. return $this->applyTax(
  95. $this->getBuyingPrice($product),
  96. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  97. );
  98. }
  99. public function getBuyingPriceByRefUnit(ProductPropertyInterface $product)
  100. {
  101. return $this->getSolver($product)->getBuyingPriceByRefUnitInherited($product);
  102. }
  103. public function getBuyingPriceByRefUnitWithTax(ProductPropertyInterface $product)
  104. {
  105. return $this->applyTax(
  106. $this->getBuyingPriceByRefUnit($product),
  107. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  108. );
  109. }
  110. public function getMultiplyingFactor(ProductPropertyInterface $product)
  111. {
  112. return $this->round($this->getPriceWithTax($product) / $this->getBuyingPrice($product));
  113. }
  114. }