Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

335 lines
15KB

  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 getMarginOrderProductsWithReductionsPercent(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 getMarginOrderProductsPercent(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 getBrandTaxesOrderProductsWithReductionsPercent(OrderShopInterface $orderShop): float
  64. {
  65. if ($this->getTotalOrderProducts($orderShop)) {
  66. return $this->round($this->getMarginOrderProducts($orderShop) / $this->getTotalBuyingPriceOrderProducts($orderShop->getOrderProducts()) * 100);
  67. } else {
  68. return 0;
  69. }
  70. }
  71. public function getTotalOrderProductsWithTax(OrderShopInterface $orderShop): float
  72. {
  73. return $this->getTotalOrderProductsWithTaxByOrderProducts($orderShop->getOrderProducts());
  74. }
  75. public function getTotalBuyingPriceOrderProducts($orderProducts): float
  76. {
  77. $total = 0;
  78. foreach ($orderProducts as $orderProduct) {
  79. $total += $this->orderProductPriceUtils->getTotalBuyingPrice($orderProduct);
  80. }
  81. return $total;
  82. }
  83. public function getTotalBuyingPriceOrderProductsWithTax($orderProducts): float
  84. {
  85. $total = 0;
  86. foreach ($orderProducts as $orderProduct) {
  87. $total += $this->orderProductPriceUtils->getTotalBuyingPriceWithTax($orderProduct);
  88. }
  89. return $total;
  90. }
  91. public function getTotalOrderProductsWithTaxByOrderProducts($orderProducts): float
  92. {
  93. $total = 0;
  94. foreach ($orderProducts as $orderProduct) {
  95. $total += $this->orderProductPriceUtils->getTotalWithTaxAndReduction($orderProduct);
  96. }
  97. return $total;
  98. }
  99. public function getTotalOrderProductsTaxes(OrderShopInterface $orderShop): float
  100. {
  101. $total = 0;
  102. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  103. $total += $this->orderProductPriceUtils->getTotalTaxes($orderProduct) / $this->getReductionsCoef($orderShop);
  104. }
  105. return $total;
  106. }
  107. public function getOrderProductsTaxesAsArray(OrderShopInterface $orderShop): array
  108. {
  109. $orderProductsTaxes = [];
  110. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  111. $idTaxRate = $orderProduct->getTaxRate()->getId();
  112. if (!isset($orderProductsTaxes[$idTaxRate])) {
  113. $orderProductsTaxes[$idTaxRate] = [
  114. 'label' => $orderProduct->getTaxRate()->getValue() . '%',
  115. 'totalOrderProducts' => 0,
  116. 'totalTaxes' => 0,
  117. ];
  118. }
  119. $orderProductsTaxes[$idTaxRate]['totalOrderProducts'] += $this->orderProductPriceUtils->getTotalWithReduction($orderProduct) / $this->getReductionsCoef($orderShop);
  120. $orderProductsTaxes[$idTaxRate]['totalTaxes'] += $this->orderProductPriceUtils->getTotalTaxes($orderProduct) / $this->getReductionsCoef($orderShop);
  121. }
  122. return $orderProductsTaxes;
  123. }
  124. private function getReductionsCoef(OrderShopInterface $orderShop): float
  125. {
  126. return $this->getTotalOrderProducts($orderShop) / $this->getTotalOrderProductsWithReductions($orderShop);
  127. }
  128. private function getTaxRateAverage(OrderShopInterface $orderShop): float
  129. {
  130. return $this->getTotalOrderProductsWithTax($orderShop) / $this->getTotalOrderProducts($orderShop);
  131. }
  132. public function getTotalOrderProductsWithReductions(OrderShopInterface $orderShop)
  133. {
  134. $total = $this->getTotalOrderProducts($orderShop);
  135. $total -= $this->getTotalReductionCartsAmount($orderShop);
  136. $total -= $this->getTotalReductionCreditsAmount($orderShop);
  137. return $total;
  138. }
  139. public function getTotalOrderProductsWithReductionCarts(OrderShopInterface $orderShop)
  140. {
  141. $total = $this->getTotalOrderProducts($orderShop);
  142. $total -= $this->getTotalReductionCartsAmount($orderShop);
  143. return $total;
  144. }
  145. public function getTotalReductionCartsAmount(OrderShopInterface $orderShop)
  146. {
  147. $totalReductionAmount = 0;
  148. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  149. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop, $orderReductionCart);
  150. }
  151. return $totalReductionAmount;
  152. }
  153. public function getTotalReductionCreditsAmount(OrderShopInterface $orderShop)
  154. {
  155. $totalReductionAmount = 0;
  156. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  157. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop, $orderReductionCredit);
  158. }
  159. return $totalReductionAmount;
  160. }
  161. public function getTotalOrderProductsWithTaxAndReductions(OrderShopInterface $orderShop)
  162. {
  163. $total = $this->getTotalOrderProductsWithTax($orderShop);
  164. $total -= $this->getTotalReductionCartsAmountWithTax($orderShop);
  165. $total -= $this->getTotalReductionCreditsAmountWithTax($orderShop);
  166. return $total;
  167. }
  168. public function getTotalOrderProductsWithTaxAndReductionCarts(OrderShopInterface $orderShop)
  169. {
  170. $total = $this->getTotalOrderProductsWithTax($orderShop);
  171. $total -= $this->getTotalReductionCartsAmountWithTax($orderShop);
  172. return $total;
  173. }
  174. public function getTotalReductionCartsAmountWithTax(OrderShopInterface $orderShop)
  175. {
  176. $totalReductionAmount = 0;
  177. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  178. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithTax($orderShop, $orderReductionCart);
  179. }
  180. return $totalReductionAmount;
  181. }
  182. public function getTotalReductionCreditsAmountWithTax(OrderShopInterface $orderShop)
  183. {
  184. $totalReductionAmount = 0;
  185. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  186. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithTax($orderShop, $orderReductionCredit);
  187. }
  188. return $totalReductionAmount;
  189. }
  190. public function getOrderProductsReductionCartAmountWithoutTax(OrderShopInterface $order, $orderReductionCart)
  191. {
  192. $amount = 0;
  193. if ($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  194. if ($orderReductionCart->getUnit() == 'percent') {
  195. $amount = $this->amountReductionByPercentValue(
  196. $this->getTotalOrderProducts($order),
  197. $orderReductionCart->getValue()
  198. );
  199. } else if ($orderReductionCart->getUnit() == 'amount') {
  200. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  201. $amount = $orderReductionCart->getValue();
  202. } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  203. $amount = $this->round($orderReductionCart->getValue() / $this->getTaxRateAverage($order));
  204. }
  205. }
  206. }
  207. return $amount;
  208. }
  209. public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
  210. {
  211. $amount = 0;
  212. if ($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
  213. if ($orderReductionCart->getUnit() == 'percent') {
  214. $amount = $this->amountReductionByPercentValue(
  215. $this->getTotalOrderProductsWithTax($order),
  216. $orderReductionCart->getValue()
  217. );
  218. } elseif ($orderReductionCart->getUnit() == 'amount') {
  219. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  220. $amount = $this->round($orderReductionCart->getValue() * $this->getTaxRateAverage($order));
  221. } elseif ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  222. $amount = $orderReductionCart->getValue();
  223. }
  224. }
  225. }
  226. return $amount;
  227. }
  228. public function getOrderProductsReductionCreditAmountWithoutTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  229. {
  230. $amount = 0;
  231. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  232. $amount = $orderReductionCredit->getValue();
  233. } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  234. $amount = $this->round($orderReductionCredit->getValue() / $this->getTaxRateAverage($order));
  235. }
  236. return $amount;
  237. }
  238. public function getOrderProductsReductionCreditAmountWithTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
  239. {
  240. $amountWithTax = 0;
  241. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  242. $amountWithTax = $this->round($orderReductionCredit->getValue() * $this->getTaxRateAverage($order));
  243. } elseif ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  244. $amountWithTax = $orderReductionCredit->getValue();
  245. }
  246. return $amountWithTax;
  247. }
  248. public function getTotalReductions(OrderShopInterface $orderShop)
  249. {
  250. $total = 0 ;
  251. foreach($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  252. $total += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop, $orderReductionCart) ;
  253. }
  254. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  255. $total += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop, $orderReductionCredit) ;
  256. }
  257. return $total ;
  258. }
  259. public function getTotalReductionsWithTax(OrderShopInterface $orderShop)
  260. {
  261. $total = 0 ;
  262. foreach($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  263. $total += $this->getOrderProductsReductionCartAmountWithTax($orderShop, $orderReductionCart) ;
  264. }
  265. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  266. $total += $this->getOrderProductsReductionCreditAmountWithTax($orderShop, $orderReductionCredit) ;
  267. }
  268. return $total ;
  269. }
  270. }