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.

PriceSolverTrait.php 4.7KB

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