Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

100 Zeilen
4.4KB

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