You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 satır
4.2KB

  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)
  10. {
  11. return $this->round($this->applyPercent($price, $taxRateValue));
  12. }
  13. public function applyReductionPercent($price, $percentage)
  14. {
  15. return $this->applyPercent($price, -$percentage);
  16. }
  17. public function applyReductionAmount($price, $amount)
  18. {
  19. return $price - $amount;
  20. }
  21. public function applyPercent($price, $percentage)
  22. {
  23. return $price * ($percentage / 100 + 1);
  24. }
  25. public function applyPercentNegative($price, $percentage)
  26. {
  27. return $price / ($percentage / 100 + 1);
  28. }
  29. public function round($price)
  30. {
  31. return round((($price * 100)) / 100, 2);
  32. }
  33. public function amountReductionByPercentValue($price, $percent)
  34. {
  35. return round((($price * $percent)) / 100, 2);
  36. }
  37. public function applyReductionCatalog(
  38. $entity,
  39. $price,
  40. $priceWithTax,
  41. $quantity = 1,
  42. $reductionCatalog = null,
  43. $withTax = true,
  44. $round = true
  45. ): ?float {
  46. if ($reductionCatalog) {
  47. $reductionCatalogValue = $reductionCatalog->getValue();
  48. $reductionCatalogUnit = $reductionCatalog->getUnit();
  49. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  50. } else {
  51. if ($entity instanceof ProductInterface) {
  52. $reductionCatalog = $this->productSolver->getReductionCatalogInherited($entity);
  53. }
  54. if ($entity instanceof ProductFamilyInterface) {
  55. $reductionCatalog = $this->productFamilySolver->getReductionCatalogInherited($entity);
  56. }
  57. if ($reductionCatalog) {
  58. $reductionCatalogValue = $reductionCatalog->getValue();
  59. $reductionCatalogUnit = $reductionCatalog->getUnit();
  60. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  61. }
  62. if ($entity instanceof OrderProductInterface) {
  63. $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog();
  64. if ($orderProductReductionCatalog) {
  65. $reductionCatalogValue = $orderProductReductionCatalog->getValue();
  66. $reductionCatalogUnit = $orderProductReductionCatalog->getUnit();
  67. $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate();
  68. }
  69. }
  70. }
  71. if (isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
  72. if ($reductionCatalogUnit == 'percent') {
  73. $priceWithTax = $this->applyReductionPercent(
  74. $priceWithTax,
  75. $reductionCatalogValue
  76. );
  77. } elseif ($reductionCatalogUnit == 'amount') {
  78. if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  79. $priceWithTax = $this->applyTax(
  80. $this->applyReductionAmount(
  81. $price,
  82. $reductionCatalogValue * $quantity
  83. ),
  84. $entity->getTaxRateInherited()->getValue()
  85. );
  86. } elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') {
  87. $priceWithTax = $this->applyReductionAmount(
  88. $priceWithTax,
  89. $reductionCatalogValue * $quantity
  90. );
  91. }
  92. }
  93. }
  94. if ($withTax) {
  95. $priceReturn = $priceWithTax;
  96. } else {
  97. $priceReturn = $this->applyPercentNegative($priceWithTax, $entity->getTaxRateInherited()->getValue());
  98. }
  99. if ($round) {
  100. return $this->round($priceReturn);
  101. } else {
  102. return $priceReturn;
  103. }
  104. }
  105. }