選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

181 行
8.0KB

  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. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  89. if ($orderReductionCart->getUnit() == 'percent') {
  90. $price = $this->amountReductionByPercentValue(
  91. $this->getTotalOrderProducts($order),
  92. $orderReductionCart->getValue()
  93. );
  94. } else if ($orderReductionCart->getUnit() == 'amount') {
  95. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  96. $price = $orderReductionCart->getValue();
  97. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  98. $price = $orderReductionCart->getValue() / $this->getTaxRateAverage();
  99. }
  100. }
  101. }
  102. }
  103. public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
  104. {
  105. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  106. if ($orderReductionCart->getUnit() == 'percent') {
  107. $price = $this->amountReductionByPercentValue(
  108. $this->getTotalOrderProductsWithTax($order),
  109. $orderReductionCart->getValue()
  110. );
  111. } else if ($orderReductionCart->getUnit() == 'amount') {
  112. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  113. $price = $orderReductionCart->getValue() * $this->getTaxRateAverage();
  114. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  115. $price = $orderReductionCart->getValue() ;
  116. }
  117. }
  118. }
  119. }
  120. public function getOrderProductsReductionCreditAmountWithoutTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  121. {
  122. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  123. $amount = $orderReductionCredit->getValue();
  124. } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  125. $amount = $orderReductionCredit->getValue() / $this->getTaxRateAverage();
  126. }
  127. return $amount;
  128. }
  129. public function getOrderProductsReductionCreditAmountWithTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  130. {
  131. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  132. $amountWithTax = $orderReductionCredit->getValue() * $this->getTaxRateAverage();
  133. } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  134. $amountWithTax = $orderReductionCredit->getValue();
  135. }
  136. return $amountWithTax;
  137. }
  138. }