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ů.

ProductPriceSolver.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }