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.

277 lines
12KB

  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 getTotalBuyingPriceOrderProductsWithTax($orderProducts):float
  40. {
  41. $total = 0;
  42. foreach ($orderProducts as $orderProduct) {
  43. $total += $this->orderProductPriceUtils->getTotalBuyingPriceWithTax($orderProduct);
  44. }
  45. return $total;
  46. }
  47. public function getTotalOrderProductsWithTaxByOrderProducts($orderProducts):float
  48. {
  49. $total = 0;
  50. foreach ($orderProducts as $orderProduct) {
  51. $total += $this->orderProductPriceUtils->getTotalWithTaxAndReduction($orderProduct);
  52. }
  53. return $total;
  54. }
  55. public function getTotalOrderProductsTaxes(OrderShopInterface $orderShop):float
  56. {
  57. $total = 0 ;
  58. foreach($orderShop->getOrderProducts() as $orderProduct) {
  59. $total += $this->orderProductPriceUtils->getTotalTaxes($orderProduct) / $this->getReductionsCoef($orderShop) ;
  60. }
  61. return $total ;
  62. }
  63. public function getOrderProductsTaxesAsArray(OrderShopInterface $orderShop):array
  64. {
  65. $orderProductsTaxes = [];
  66. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  67. $idTaxRate = $orderProduct->getTaxRate()->getId() ;
  68. if(!isset($orderProductsTaxes[$idTaxRate])) {
  69. $orderProductsTaxes[$idTaxRate] = [
  70. 'label' => $orderProduct->getTaxRate()->getValue() . '%',
  71. 'totalOrderProducts' => 0,
  72. 'totalTaxes' => 0,
  73. ];
  74. }
  75. $orderProductsTaxes[$idTaxRate]['totalOrderProducts'] += $this->orderProductPriceUtils->getTotalWithReduction($orderProduct) / $this->getReductionsCoef($orderShop);
  76. $orderProductsTaxes[$idTaxRate]['totalTaxes'] += $this->orderProductPriceUtils->getTotalTaxes($orderProduct) / $this->getReductionsCoef($orderShop) ;
  77. }
  78. return $orderProductsTaxes ;
  79. }
  80. private function getReductionsCoef(OrderShopInterface $orderShop) :float
  81. {
  82. return $this->getTotalOrderProducts($orderShop) / $this->getTotalOrderProductsWithReductions($orderShop);
  83. }
  84. private function getTaxRateAverage(OrderShopInterface $orderShop):float
  85. {
  86. return $this->getTotalOrderProductsWithTax($orderShop) / $this->getTotalOrderProducts($orderShop);
  87. }
  88. public function getTotalOrderProductsWithReductions(OrderShopInterface $orderShop)
  89. {
  90. $total = $this->getTotalOrderProducts($orderShop) ;
  91. $total -= $this->getTotalReductionCartsAmount($orderShop) ;
  92. $total -= $this->getTotalReductionCreditsAmount($orderShop) ;
  93. return $total ;
  94. }
  95. public function getTotalOrderProductsWithReductionCarts(OrderShopInterface $orderShop)
  96. {
  97. $total = $this->getTotalOrderProducts($orderShop) ;
  98. $total -= $this->getTotalReductionCartsAmount($orderShop) ;
  99. return $total ;
  100. }
  101. public function getTotalReductionCartsAmount(OrderShopInterface $orderShop)
  102. {
  103. $totalReductionAmount = 0 ;
  104. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  105. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop,$orderReductionCart);
  106. }
  107. return $totalReductionAmount ;
  108. }
  109. public function getTotalReductionCreditsAmount(OrderShopInterface $orderShop)
  110. {
  111. $totalReductionAmount = 0 ;
  112. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  113. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop,$orderReductionCredit);
  114. }
  115. return $totalReductionAmount ;
  116. }
  117. public function getTotalOrderProductsWithTaxAndReductions(OrderShopInterface $orderShop)
  118. {
  119. $total = $this->getTotalOrderProductsWithTax($orderShop) ;
  120. $total -= $this->getTotalReductionCartsAmountWithTax($orderShop) ;
  121. $total -= $this->getTotalReductionCreditsAmountWithTax($orderShop) ;
  122. return $total ;
  123. }
  124. public function getMarginOrderProductsWithReductions(OrderShopInterface $orderShop):float
  125. {
  126. $total = $this->getMarginOrderProducts($orderShop) ;
  127. $totalReductionAmount = 0 ;
  128. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  129. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop,$orderReductionCart);
  130. }
  131. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  132. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop,$orderReductionCredit);
  133. }
  134. $total -= $totalReductionAmount ;
  135. return $total ;
  136. }
  137. public function getTotalOrderProductsWithTaxAndReductionCarts(OrderShopInterface $orderShop)
  138. {
  139. $total = $this->getTotalOrderProductsWithTax($orderShop) ;
  140. $total -= $this->getTotalReductionCartsAmountWithTax($orderShop) ;
  141. return $total ;
  142. }
  143. public function getTotalReductionCartsAmountWithTax(OrderShopInterface $orderShop)
  144. {
  145. $totalReductionAmount = 0 ;
  146. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  147. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithTax($orderShop,$orderReductionCart);
  148. }
  149. return $totalReductionAmount ;
  150. }
  151. public function getTotalReductionCreditsAmountWithTax(OrderShopInterface $orderShop)
  152. {
  153. $totalReductionAmount = 0 ;
  154. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  155. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithTax($orderShop,$orderReductionCredit);
  156. }
  157. return $totalReductionAmount ;
  158. }
  159. public function getOrderProductsReductionCartAmountWithoutTax(OrderShopInterface $order, $orderReductionCart)
  160. {
  161. $amount =0;
  162. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  163. if ($orderReductionCart->getUnit() == 'percent') {
  164. $amount = $this->amountReductionByPercentValue(
  165. $this->getTotalOrderProducts($order),
  166. $orderReductionCart->getValue()
  167. );
  168. } else if ($orderReductionCart->getUnit() == 'amount') {
  169. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  170. $amount = $orderReductionCart->getValue();
  171. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  172. $amount = $this->round($orderReductionCart->getValue() / $this->getTaxRateAverage($order));
  173. }
  174. }
  175. }
  176. return $amount ;
  177. }
  178. public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
  179. {
  180. $amount = 0;
  181. if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  182. if ($orderReductionCart->getUnit() == 'percent') {
  183. $amount = $this->amountReductionByPercentValue(
  184. $this->getTotalOrderProductsWithTax($order),
  185. $orderReductionCart->getValue()
  186. );
  187. }
  188. elseif ($orderReductionCart->getUnit() == 'amount') {
  189. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  190. $amount = $this->round($orderReductionCart->getValue() * $this->getTaxRateAverage($order));
  191. }
  192. elseif ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  193. $amount = $orderReductionCart->getValue() ;
  194. }
  195. }
  196. }
  197. return $amount ;
  198. }
  199. public function getOrderProductsReductionCreditAmountWithoutTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  200. {
  201. $amount = 0;
  202. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  203. $amount = $orderReductionCredit->getValue();
  204. }
  205. else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  206. $amount = $this->round($orderReductionCredit->getValue() / $this->getTaxRateAverage($order));
  207. }
  208. return $amount;
  209. }
  210. public function getOrderProductsReductionCreditAmountWithTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  211. {
  212. $amountWithTax = 0;
  213. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  214. $amountWithTax = $this->round($orderReductionCredit->getValue() * $this->getTaxRateAverage($order));
  215. }
  216. elseif ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  217. $amountWithTax = $orderReductionCredit->getValue();
  218. }
  219. return $amountWithTax;
  220. }
  221. }