Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

134 linhas
4.6KB

  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($entity instanceof ProductFamilyInterface) {
  47. $taxRate = $this->productFamilySolver->getTaxRateInherited($entity)->getValue();
  48. }else if ($entity instanceof ProductInterface) {
  49. $taxRate = $this->productFamilySolver->getTaxRateInherited($entity->getProductFamily())->getValue();
  50. }else{
  51. $taxRate = $entity->getTaxRate()->getValue();
  52. }
  53. if ($reductionCatalog) {
  54. $reductionCatalogValue = $reductionCatalog->getValue();
  55. $reductionCatalogUnit = $reductionCatalog->getUnit();
  56. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  57. } else {
  58. if ($entity instanceof ProductInterface) {
  59. $reductionCatalog = $this->productSolver->getReductionCatalogInherited($entity);
  60. }
  61. if ($entity instanceof ProductFamilyInterface) {
  62. $reductionCatalog = $this->productFamilySolver->getReductionCatalogInherited($entity);
  63. }
  64. if ($reductionCatalog) {
  65. $reductionCatalogValue = $reductionCatalog->getValue();
  66. $reductionCatalogUnit = $reductionCatalog->getUnit();
  67. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  68. }
  69. if ($entity instanceof OrderProductInterface) {
  70. $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog();
  71. if ($orderProductReductionCatalog) {
  72. $reductionCatalogValue = $orderProductReductionCatalog->getValue();
  73. $reductionCatalogUnit = $orderProductReductionCatalog->getUnit();
  74. $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate();
  75. }
  76. }
  77. }
  78. if (isset($reductionCatalogValue) && isset($reductionCatalogUnit)) {
  79. if ($reductionCatalogUnit == 'percent') {
  80. $priceWithTax = $this->applyReductionPercent(
  81. $priceWithTax,
  82. $reductionCatalogValue
  83. );
  84. } elseif ($reductionCatalogUnit == 'amount' && isset($reductionCatalogBehaviorTaxRate)) {
  85. if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  86. $priceWithTax = $this->applyTax(
  87. $this->applyReductionAmount(
  88. $price,
  89. $reductionCatalogValue * $quantity
  90. ),
  91. $taxRate
  92. );
  93. } elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') {
  94. $priceWithTax = $this->applyReductionAmount(
  95. $priceWithTax,
  96. $reductionCatalogValue * $quantity
  97. );
  98. }
  99. }
  100. }
  101. if ($withTax) {
  102. $priceReturn = $priceWithTax;
  103. } else {
  104. $priceReturn = $this->applyPercentNegative($priceWithTax, $taxRate);
  105. }
  106. if ($round) {
  107. return $this->round($priceReturn);
  108. } else {
  109. return $priceReturn;
  110. }
  111. }
  112. }