Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PriceSolverTrait.php 4.2KB

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