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.

196 line
8.5KB

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