Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

OrderShopPriceUtils.php 15KB

před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. }