Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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