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.

OrderShopPriceUtils.php 12KB

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