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.

232 lines
10KB

  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. $total -= $this->getTotalReductionCartsAmount($orderShop) ;
  74. $total -= $this->getTotalReductionCreditsAmount($orderShop) ;
  75. return $total ;
  76. }
  77. public function getTotalOrderProductsWithReductionCarts(OrderShopInterface $orderShop)
  78. {
  79. $total = $this->getTotalOrderProducts($orderShop) ;
  80. $total -= $this->getTotalReductionCartsAmount($orderShop) ;
  81. return $total ;
  82. }
  83. public function getTotalReductionCartsAmount(OrderShopInterface $orderShop)
  84. {
  85. $totalReductionAmount = 0 ;
  86. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  87. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop,$orderReductionCart);
  88. }
  89. return $totalReductionAmount ;
  90. }
  91. public function getTotalReductionCreditsAmount(OrderShopInterface $orderShop)
  92. {
  93. $totalReductionAmount = 0 ;
  94. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  95. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop,$orderReductionCredit);
  96. }
  97. return $totalReductionAmount ;
  98. }
  99. public function getTotalOrderProductsWithTaxAndReductions(OrderShopInterface $orderShop)
  100. {
  101. $total = $this->getTotalOrderProductsWithTax($orderShop) ;
  102. $total -= $this->getTotalReductionCartsAmountWithTax($orderShop) ;
  103. $total -= $this->getTotalReductionCreditsAmountWithTax($orderShop) ;
  104. return $total ;
  105. }
  106. public function getTotalOrderProductsWithTaxAndReductionCarts(OrderShopInterface $orderShop)
  107. {
  108. $total = $this->getTotalOrderProductsWithTax($orderShop) ;
  109. $total -= $this->getTotalReductionCartsAmountWithTax($orderShop) ;
  110. return $total ;
  111. }
  112. public function getTotalReductionCartsAmountWithTax(OrderShopInterface $orderShop)
  113. {
  114. $totalReductionAmount = 0 ;
  115. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  116. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithTax($orderShop,$orderReductionCart);
  117. }
  118. return $totalReductionAmount ;
  119. }
  120. public function getTotalReductionCreditsAmountWithTax(OrderShopInterface $orderShop)
  121. {
  122. $totalReductionAmount = 0 ;
  123. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  124. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithTax($orderShop,$orderReductionCredit);
  125. }
  126. return $totalReductionAmount ;
  127. }
  128. public function getOrderProductsReductionCartAmountWithoutTax(OrderShopInterface $order, $orderReductionCart)
  129. {
  130. $amount =0;
  131. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  132. if ($orderReductionCart->getUnit() == 'percent') {
  133. $amount = $this->amountReductionByPercentValue(
  134. $this->getTotalOrderProducts($order),
  135. $orderReductionCart->getValue()
  136. );
  137. } else if ($orderReductionCart->getUnit() == 'amount') {
  138. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  139. $amount = $orderReductionCart->getValue();
  140. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  141. $amount = $this->round($orderReductionCart->getValue() / $this->getTaxRateAverage($order));
  142. }
  143. }
  144. }
  145. return $amount ;
  146. }
  147. public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
  148. {
  149. $amount = 0;
  150. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  151. if ($orderReductionCart->getUnit() == 'percent') {
  152. $amount = $this->amountReductionByPercentValue(
  153. $this->getTotalOrderProductsWithTax($order),
  154. $orderReductionCart->getValue()
  155. );
  156. }
  157. elseif ($orderReductionCart->getUnit() == 'amount') {
  158. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  159. $amount = $this->round($orderReductionCart->getValue() * $this->getTaxRateAverage($order));
  160. }
  161. elseif ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  162. $amount = $orderReductionCart->getValue() ;
  163. }
  164. }
  165. }
  166. return $amount ;
  167. }
  168. public function getOrderProductsReductionCreditAmountWithoutTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  169. {
  170. $amount = 0;
  171. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  172. $amount = $orderReductionCredit->getValue();
  173. }
  174. else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  175. $amount = $this->round($orderReductionCredit->getValue() / $this->getTaxRateAverage($order));
  176. }
  177. return $amount;
  178. }
  179. public function getOrderProductsReductionCreditAmountWithTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  180. {
  181. $amountWithTax = 0;
  182. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  183. $amountWithTax = $this->round($orderReductionCredit->getValue() * $this->getTaxRateAverage($order));
  184. }
  185. elseif ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  186. $amountWithTax = $orderReductionCredit->getValue();
  187. }
  188. return $amountWithTax;
  189. }
  190. }