Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

164 lines
7.1KB

  1. <?php
  2. namespace Lc\ShopBundle\Price\Services;
  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. $total -= $totalReductionAmount ;
  68. return $total ;
  69. }
  70. public function getTotalWithTaxAndReductions(OrderShopInterface $orderShop)
  71. {
  72. }
  73. public function getOrderProductsReductionCartAmountWithoutTax(OrderShopInterface $order, $orderReductionCart)
  74. {
  75. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  76. if ($orderReductionCart->getUnit() == 'percent') {
  77. $price = $this->amountReductionByPercentValue(
  78. $this->getTotalOrderProducts($order),
  79. $orderReductionCart->getValue()
  80. );
  81. } else if ($orderReductionCart->getUnit() == 'amount') {
  82. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  83. $price = $orderReductionCart->getValue();
  84. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  85. $price = $orderReductionCart->getValue() / $this->getTaxRateAverage();
  86. }
  87. }
  88. }
  89. }
  90. public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
  91. {
  92. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  93. if ($orderReductionCart->getUnit() == 'percent') {
  94. $price = $this->amountReductionByPercentValue(
  95. $this->getTotalOrderProductsWithTax($order),
  96. $orderReductionCart->getValue()
  97. );
  98. } else if ($orderReductionCart->getUnit() == 'amount') {
  99. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  100. $price = $orderReductionCart->getValue() * $this->getTaxRateAverage();
  101. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  102. $price = $orderReductionCart->getValue() ;
  103. }
  104. }
  105. }
  106. }
  107. public function getOrderProductsReductionCreditAmountWithoutTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  108. {
  109. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  110. $amount = $orderReductionCredit->getValue();
  111. } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  112. $amount = $orderReductionCredit->getValue() / $this->getTaxRateAverage();
  113. }
  114. return $amount;
  115. }
  116. public function getOrderProductsReductionCreditAmountWithTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  117. {
  118. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  119. $amountWithTax = $orderReductionCredit->getValue() * $this->getTaxRateAverage();
  120. } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  121. $amountWithTax = $orderReductionCredit->getValue();
  122. }
  123. return $amountWithTax;
  124. }
  125. }