Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

142 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. dump($product);
  83. return $solver->getBuyingPriceInherited($product);
  84. } elseif ($solver->getBehaviorPriceInherited($product) == 'by-reference-unit') {
  85. if ($solver->getQuantityInherited($product) > 0) {
  86. return $solver->getBuyingPriceByRefUnitInherited($product) * ($solver->getQuantityInherited($product
  87. ) / $solver->getUnitInherited($product)->getCoefficient());
  88. } else {
  89. return 0;
  90. }
  91. }
  92. }
  93. public function getBuyingPriceWithTax(ProductPropertyInterface $product)
  94. {
  95. return $this->applyTax(
  96. $this->getBuyingPrice($product),
  97. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  98. );
  99. }
  100. public function getBuyingPriceByRefUnit(ProductPropertyInterface $product)
  101. {
  102. return $this->getSolver($product)->getBuyingPriceByRefUnitInherited($product);
  103. }
  104. public function getBuyingPriceByRefUnitWithTax(ProductPropertyInterface $product)
  105. {
  106. return $this->applyTax(
  107. $this->getBuyingPriceByRefUnit($product),
  108. $this->productFamilySolver->getTaxRateInherited($product)->getValue()
  109. );
  110. }
  111. public function getMultiplyingFactor(ProductPropertyInterface $product)
  112. {
  113. return $this->round($this->getPriceWithTax($product) / $this->getBuyingPrice($product));
  114. }
  115. }