OrderShopPriceUtils.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Lc\ShopBundle\Services\Price;
  3. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  4. use Lc\ShopBundle\Context\OrderShopInterface;
  5. use Lc\ShopBundle\Context\OrderShopPriceUtilsInterface;
  6. use Lc\ShopBundle\Model\ReductionCart;
  7. class OrderShopPriceUtils implements OrderShopPriceUtilsInterface
  8. {
  9. use PriceUtilsTrait ;
  10. protected $orderProductPriceUtils ;
  11. public function __construct(OrderProductPriceUtils $orderProductPriceUtils)
  12. {
  13. $this->orderProductPriceUtils = $orderProductPriceUtils ;
  14. }
  15. //Inclus les ReductionCatalog des OrderProducts
  16. public function getTotalOrderProducts(OrderShopInterface $orderShop):float
  17. {
  18. // A tester calculer ce montant en faisant TotalOrderWithTax - TotalOrderTaxes
  19. $total = 0;
  20. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  21. $total += $this->orderProductPriceUtils->getTotalWithReduction($orderProduct);
  22. }
  23. return $total;
  24. }
  25. public function getTotalOrderProductsWithTax(OrderShopInterface $orderShop):float
  26. {
  27. $total = 0;
  28. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  29. $total += $this->orderProductPriceUtils->getTotalWithTaxAndReduction($orderProduct);
  30. }
  31. return $total;
  32. }
  33. public function getTotalOrderProductsTaxes(OrderShopInterface $orderShop):float {
  34. }
  35. public function getOrderProductsTaxesAsArray(OrderShopInterface $orderShop):array
  36. {
  37. $orderProductsTaxes = [];
  38. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  39. $idTaxRate = $orderProduct->getTaxRate()->getId() ;
  40. if(!isset($orderProductsTaxes[$idTaxRate])) {
  41. $orderProductsTaxes[$idTaxRate] = [
  42. 'label' => $orderProduct->getTaxRate()->getValue() . '%',
  43. 'totalOrderProducts' => 0,
  44. 'totalTaxes' => 0,
  45. ];
  46. }
  47. $orderProductsTaxes[$idTaxRate]['totalOrderProducts'] += $this->orderProductPriceUtils->getTotalWithReduction($orderProduct) * $this->getReductionsCoef($orderShop);
  48. $orderProductsTaxes[$idTaxRate]['totalTaxes'] += $this->orderProductPriceUtils->getTotalTaxes($orderProduct) * $this->getReductionsCoef($orderShop) ;
  49. }
  50. return $orderProductsTaxes ;
  51. }
  52. private function getReductionsCoef(OrderShopInterface $orderShop) :float
  53. {
  54. return $this->getTotalOrderProducts($orderShop) / $this->getTotalOrderProductsWithReductions($orderShop);
  55. }
  56. private function getTaxRateAverage(OrderShopInterface $order):float
  57. {
  58. return $this->getTotalOrderProductsWithTax() / $this->getTotalOrderProducts();
  59. }
  60. public function getTotalOrderProductsWithReductions(OrderShopInterface $orderShop)
  61. {
  62. $total = $this->getTotalOrderProducts($orderShop) ;
  63. $totalReductionAmount = 0 ;
  64. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  65. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop,$orderReductionCart);
  66. }
  67. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  68. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop,$orderReductionCredit);
  69. }
  70. $total -= $totalReductionAmount ;
  71. return $total ;
  72. }
  73. public function getTotalOrderProductsWithTaxAndReductions(OrderShopInterface $orderShop)
  74. {
  75. $total = $this->getTotalOrderProductsWithTax($orderShop) ;
  76. $totalReductionAmount = 0 ;
  77. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  78. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithTax($orderShop,$orderReductionCart);
  79. }
  80. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  81. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithTax($orderShop,$orderReductionCredit);
  82. }
  83. $total -= $totalReductionAmount ;
  84. return $total ;
  85. }
  86. public function getOrderProductsReductionCartAmountWithoutTax(OrderShopInterface $order, $orderReductionCart)
  87. {
  88. $amount =0;
  89. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  90. if ($orderReductionCart->getUnit() == 'percent') {
  91. $amount = $this->amountReductionByPercentValue(
  92. $this->getTotalOrderProducts($order),
  93. $orderReductionCart->getValue()
  94. );
  95. } else if ($orderReductionCart->getUnit() == 'amount') {
  96. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  97. $price = $orderReductionCart->getValue();
  98. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  99. $price = $orderReductionCart->getValue() / $this->getTaxRateAverage();
  100. }
  101. }
  102. }
  103. return $amount ;
  104. }
  105. public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
  106. {
  107. $amount =0;
  108. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  109. if ($orderReductionCart->getUnit() == 'percent') {
  110. $amount = $this->amountReductionByPercentValue(
  111. $this->getTotalOrderProductsWithTax($order),
  112. $orderReductionCart->getValue()
  113. );
  114. } else if ($orderReductionCart->getUnit() == 'amount') {
  115. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  116. $amount = $orderReductionCart->getValue() * $this->getTaxRateAverage();
  117. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  118. $amount = $orderReductionCart->getValue() ;
  119. }
  120. }
  121. }
  122. return $amount ;
  123. }
  124. public function getOrderProductsReductionCreditAmountWithoutTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  125. {
  126. $amount =0;
  127. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  128. $amount = $orderReductionCredit->getValue();
  129. } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  130. $amount = $orderReductionCredit->getValue() / $this->getTaxRateAverage();
  131. }
  132. return $amount;
  133. }
  134. public function getOrderProductsReductionCreditAmountWithTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  135. {
  136. $amount =0;
  137. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  138. $amountWithTax = $orderReductionCredit->getValue() * $this->getTaxRateAverage();
  139. } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  140. $amountWithTax = $orderReductionCredit->getValue();
  141. }
  142. return $amountWithTax;
  143. }
  144. }