Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PriceSolverTrait.php 3.9KB

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