Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

285 linhas
13KB

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