PriceUtilsTrait.php 4.7KB

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