You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 satır
8.4KB

  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. return 0 ;
  36. }
  37. public function getOrderProductsTaxesAsArray(OrderShopInterface $orderShop):array
  38. {
  39. $orderProductsTaxes = [];
  40. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  41. $idTaxRate = $orderProduct->getTaxRate()->getId() ;
  42. if(!isset($orderProductsTaxes[$idTaxRate])) {
  43. $orderProductsTaxes[$idTaxRate] = [
  44. 'label' => $orderProduct->getTaxRate()->getValue() . '%',
  45. 'totalOrderProducts' => 0,
  46. 'totalTaxes' => 0,
  47. ];
  48. }
  49. $orderProductsTaxes[$idTaxRate]['totalOrderProducts'] += $this->orderProductPriceUtils->getTotalWithReduction($orderProduct) / $this->getReductionsCoef($orderShop);
  50. $orderProductsTaxes[$idTaxRate]['totalTaxes'] += $this->orderProductPriceUtils->getTotalTaxes($orderProduct) / $this->getReductionsCoef($orderShop) ;
  51. }
  52. return $orderProductsTaxes ;
  53. }
  54. private function getReductionsCoef(OrderShopInterface $orderShop) :float
  55. {
  56. return $this->getTotalOrderProducts($orderShop) / $this->getTotalOrderProductsWithReductions($orderShop);
  57. }
  58. private function getTaxRateAverage(OrderShopInterface $orderShop):float
  59. {
  60. return $this->getTotalOrderProductsWithTax($orderShop) / $this->getTotalOrderProducts($orderShop);
  61. }
  62. public function getTotalOrderProductsWithReductions(OrderShopInterface $orderShop)
  63. {
  64. $total = $this->getTotalOrderProducts($orderShop) ;
  65. $totalReductionAmount = 0 ;
  66. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  67. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop,$orderReductionCart);
  68. }
  69. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  70. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop,$orderReductionCredit);
  71. }
  72. $total -= $totalReductionAmount ;
  73. return $total ;
  74. }
  75. public function getTotalOrderProductsWithTaxAndReductions(OrderShopInterface $orderShop)
  76. {
  77. $total = $this->getTotalOrderProductsWithTax($orderShop) ;
  78. $totalReductionAmount = 0 ;
  79. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  80. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithTax($orderShop,$orderReductionCart);
  81. }
  82. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  83. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithTax($orderShop,$orderReductionCredit);
  84. }
  85. $total -= $totalReductionAmount ;
  86. return $total ;
  87. }
  88. public function getOrderProductsReductionCartAmountWithoutTax(OrderShopInterface $order, $orderReductionCart)
  89. {
  90. $amount =0;
  91. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  92. if ($orderReductionCart->getUnit() == 'percent') {
  93. $amount = $this->amountReductionByPercentValue(
  94. $this->getTotalOrderProducts($order),
  95. $orderReductionCart->getValue()
  96. );
  97. } else if ($orderReductionCart->getUnit() == 'amount') {
  98. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  99. $amount = $orderReductionCart->getValue();
  100. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  101. $amount = $this->round($orderReductionCart->getValue() / $this->getTaxRateAverage($order));
  102. }
  103. }
  104. }
  105. return $amount ;
  106. }
  107. public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
  108. {
  109. $amount = 0;
  110. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  111. if ($orderReductionCart->getUnit() == 'percent') {
  112. $amount = $this->amountReductionByPercentValue(
  113. $this->getTotalOrderProductsWithTax($order),
  114. $orderReductionCart->getValue()
  115. );
  116. }
  117. elseif ($orderReductionCart->getUnit() == 'amount') {
  118. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  119. $amount = $this->round($orderReductionCart->getValue() * $this->getTaxRateAverage($order));
  120. }
  121. elseif ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  122. $amount = $orderReductionCart->getValue() ;
  123. }
  124. }
  125. }
  126. return $amount ;
  127. }
  128. public function getOrderProductsReductionCreditAmountWithoutTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  129. {
  130. $amount = 0;
  131. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  132. $amount = $orderReductionCredit->getValue();
  133. }
  134. else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  135. $amount = $this->round($orderReductionCredit->getValue() / $this->getTaxRateAverage($order));
  136. }
  137. return $amount;
  138. }
  139. public function getOrderProductsReductionCreditAmountWithTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  140. {
  141. $amountWithTax = 0;
  142. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  143. $amountWithTax = $this->round($orderReductionCredit->getValue() * $this->getTaxRateAverage($order));
  144. }
  145. elseif ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  146. $amountWithTax = $orderReductionCredit->getValue();
  147. }
  148. return $amountWithTax;
  149. }
  150. }