203 Zeilen
8.9KB

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