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.

105 rindas
4.5KB

  1. <?php
  2. namespace Lc\ShopBundle\Price\Services;
  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 applyReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null, $withTax = true): ?float
  33. {
  34. if ($reductionCatalog) {
  35. $reductionCatalogValue = $reductionCatalog->getValue();
  36. $reductionCatalogUnit = $reductionCatalog->getUnit();
  37. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  38. } else {
  39. if ($entity instanceof ProductPropertyInterface) {
  40. $reductionCatalog = $entity->getReductionCatalogInherited();
  41. if ($reductionCatalog) {
  42. $reductionCatalogValue = $reductionCatalog->getValue();
  43. $reductionCatalogUnit = $reductionCatalog->getUnit();
  44. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  45. }
  46. }
  47. if ($entity instanceof OrderProductInterface) {
  48. $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog();
  49. if ($orderProductReductionCatalog) {
  50. $reductionCatalogValue = $orderProductReductionCatalog->getValue();
  51. $reductionCatalogUnit = $orderProductReductionCatalog->getUnit();
  52. $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate();
  53. }
  54. }
  55. }
  56. if (isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
  57. if ($reductionCatalogUnit == 'percent') {
  58. $priceWithTax = $this->applyReductionPercent(
  59. $priceWithTax,
  60. $reductionCatalogValue
  61. );
  62. } elseif ($reductionCatalogUnit == 'amount') {
  63. if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  64. $priceWithTax = $this->applyTax(
  65. $this->applyReductionAmount(
  66. $price,
  67. $reductionCatalogValue
  68. ),
  69. $entity->getTaxRateInherited()->getValue()
  70. );
  71. } elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') {
  72. $priceWithTax = $this->applyReductionAmount(
  73. $priceWithTax,
  74. $reductionCatalogValue
  75. );
  76. }
  77. }
  78. }
  79. if ($withTax) {
  80. $priceReturn = $priceWithTax;
  81. } else {
  82. $priceReturn = $this->applyPercentNegative($priceWithTax, $entity->getTaxRateInherited()->getValue());
  83. }
  84. return $this->round($priceReturn);
  85. }
  86. }