Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

151 line
4.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Price;
  3. use Lc\CaracoleBundle\Doctrine\Extension\ProductPropertyInterface;
  4. use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
  5. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  6. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  7. trait PriceSolverTrait
  8. {
  9. public function applyTax($price, $taxRateValue, $round = true)
  10. {
  11. $price = $this->applyPercent($price, $taxRateValue);
  12. if($round) {
  13. return $this->round($price);
  14. }
  15. return $price;
  16. }
  17. public function excludeTax($price, $taxRateValue, $round = true)
  18. {
  19. $price = $this->applyPercentNegative($price, $taxRateValue);
  20. if($round) {
  21. return $this->round($price);
  22. }
  23. return $price;
  24. }
  25. public function applyReductionPercent($price, $percentage)
  26. {
  27. return $this->applyPercent($price, -$percentage);
  28. }
  29. public function applyReductionAmount($price, $amount)
  30. {
  31. return $price - $amount;
  32. }
  33. public function applyPercent($price, $percentage)
  34. {
  35. return $price * ($percentage / 100 + 1);
  36. }
  37. public function applyPercentNegative($price, $percentage)
  38. {
  39. return $price / ($percentage / 100 + 1);
  40. }
  41. public function round($price)
  42. {
  43. return round((($price * 100)) / 100, 2);
  44. }
  45. public function amountReductionByPercentValue($price, $percent)
  46. {
  47. return round((($price * $percent)) / 100, 2);
  48. }
  49. public function applyReductionCatalog(
  50. $entity,
  51. $price,
  52. $priceWithTax,
  53. $quantity = 1,
  54. $reductionCatalog = null,
  55. $withTax = true,
  56. $round = true
  57. ): ?float {
  58. if($entity instanceof ProductFamilyInterface) {
  59. $taxRate = $this->productFamilySolver->getTaxRateInherited($entity)->getValue();
  60. }else if ($entity instanceof ProductInterface) {
  61. $taxRate = $this->productFamilySolver->getTaxRateInherited($entity->getProductFamily())->getValue();
  62. }else{
  63. $taxRate = $entity->getTaxRate()->getValue();
  64. }
  65. if ($reductionCatalog) {
  66. $reductionCatalogValue = $reductionCatalog->getValue();
  67. $reductionCatalogUnit = $reductionCatalog->getUnit();
  68. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  69. } else {
  70. if ($entity instanceof ProductInterface) {
  71. $reductionCatalog = $this->productSolver->getReductionCatalogInherited($entity);
  72. }
  73. if ($entity instanceof ProductFamilyInterface) {
  74. $reductionCatalog = $this->productFamilySolver->getReductionCatalogInherited($entity);
  75. }
  76. if ($reductionCatalog) {
  77. $reductionCatalogValue = $reductionCatalog->getValue();
  78. $reductionCatalogUnit = $reductionCatalog->getUnit();
  79. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  80. }
  81. if ($entity instanceof OrderProductInterface) {
  82. $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog();
  83. if ($orderProductReductionCatalog) {
  84. $reductionCatalogValue = $orderProductReductionCatalog->getValue();
  85. $reductionCatalogUnit = $orderProductReductionCatalog->getUnit();
  86. $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate();
  87. }
  88. }
  89. }
  90. if (isset($reductionCatalogValue) && isset($reductionCatalogUnit)) {
  91. if ($reductionCatalogUnit == 'percent') {
  92. $priceWithTax = $this->applyReductionPercent(
  93. $priceWithTax,
  94. $reductionCatalogValue
  95. );
  96. } elseif ($reductionCatalogUnit == 'amount' && isset($reductionCatalogBehaviorTaxRate)) {
  97. if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  98. $priceWithTax = $this->applyTax(
  99. $this->applyReductionAmount(
  100. $price,
  101. $reductionCatalogValue * $quantity
  102. ),
  103. $taxRate
  104. );
  105. } elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') {
  106. $priceWithTax = $this->applyReductionAmount(
  107. $priceWithTax,
  108. $reductionCatalogValue * $quantity
  109. );
  110. }
  111. }
  112. }
  113. if ($withTax) {
  114. $priceReturn = $priceWithTax;
  115. } else {
  116. $priceReturn = $this->applyPercentNegative($priceWithTax, $taxRate);
  117. }
  118. if ($round) {
  119. return $this->round($priceReturn);
  120. } else {
  121. return $priceReturn;
  122. }
  123. }
  124. }