OrderShopPriceUtils.php 12KB

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