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.

OrderShopPriceResolver.php 14KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace Lc\CaracoleBundle\Resolver\Price;
  3. use Lc\CaracoleBundle\Model\Order\OrderReductionCreditInterface;
  4. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  5. use Lc\CaracoleBundle\Model\Reduction\ReductionCartModel;
  6. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditModel;
  7. class OrderShopPriceResolver
  8. {
  9. use PriceResolverTrait;
  10. protected OrderProductPriceResolver $orderProductPriceResolver;
  11. public function __construct(OrderProductPriceResolver $orderProductPriceResolver)
  12. {
  13. $this->orderProductPriceResolver = $orderProductPriceResolver;
  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->orderProductPriceResolver->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->orderProductPriceResolver->getTotalMargin($orderProduct);
  31. }
  32. return $total;
  33. }
  34. public function getMarginOrderProductsWithReductions(OrderShopInterface $orderShop, $cache = false): float
  35. {
  36. if ($cache && $orderShop->getStatMarginOrderProductsWithReductions() !== null) {
  37. return $orderShop->getStatMarginOrderProductsWithReductions();
  38. } else {
  39. $total = $this->getMarginOrderProducts($orderShop);
  40. $totalReductionAmount = 0;
  41. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  42. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax(
  43. $orderShop,
  44. $orderReductionCart
  45. );
  46. }
  47. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  48. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax(
  49. $orderShop,
  50. $orderReductionCredit
  51. );
  52. }
  53. $total -= $totalReductionAmount;
  54. return $total;
  55. }
  56. }
  57. public function getMarginOrderProductsWithReductionsPercent(OrderShopInterface $orderShop): float
  58. {
  59. if ($this->getTotalOrderProducts($orderShop)) {
  60. return $this->round(
  61. $this->getMarginOrderProductsWithReductions(
  62. $orderShop
  63. ) / $this->getTotalOrderProductsWithReductions($orderShop) * 100
  64. );
  65. } else {
  66. return 0;
  67. }
  68. }
  69. public function getMarginOrderProductsPercent(OrderShopInterface $orderShop): float
  70. {
  71. if ($this->getTotalOrderProducts($orderShop)) {
  72. return $this->round(
  73. $this->getMarginOrderProducts($orderShop) / $this->getTotalOrderProducts($orderShop) * 100
  74. );
  75. } else {
  76. return 0;
  77. }
  78. }
  79. public function getBrandTaxesOrderProductsWithReductionsPercent(OrderShopInterface $orderShop): float
  80. {
  81. if ($this->getTotalOrderProducts($orderShop)) {
  82. return $this->round(
  83. $this->getMarginOrderProducts($orderShop) / $this->getTotalBuyingPriceOrderProducts(
  84. $orderShop->getOrderProducts()
  85. ) * 100
  86. );
  87. } else {
  88. return 0;
  89. }
  90. }
  91. public function getTotalOrderProductsWithTax(OrderShopInterface $orderShop): float
  92. {
  93. return $this->getTotalOrderProductsWithTaxByOrderProducts($orderShop->getOrderProducts());
  94. }
  95. public function getTotalBuyingPriceOrderProducts($orderProducts): float
  96. {
  97. $total = 0;
  98. foreach ($orderProducts as $orderProduct) {
  99. $total += $this->orderProductPriceResolver->getTotalBuyingPrice($orderProduct);
  100. }
  101. return $total;
  102. }
  103. public function getTotalBuyingPriceOrderProductsWithTax($orderProducts): float
  104. {
  105. $total = 0;
  106. foreach ($orderProducts as $orderProduct) {
  107. $total += $this->orderProductPriceResolver->getTotalBuyingPriceWithTax($orderProduct);
  108. }
  109. return $total;
  110. }
  111. public function getTotalOrderProductsWithTaxByOrderProducts($orderProducts): float
  112. {
  113. $total = 0;
  114. foreach ($orderProducts as $orderProduct) {
  115. $total += $this->orderProductPriceResolver->getTotalWithTaxAndReduction($orderProduct);
  116. }
  117. return $total;
  118. }
  119. public function getTotalOrderProductsTaxes(OrderShopInterface $orderShop): float
  120. {
  121. $total = 0;
  122. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  123. $total += $this->orderProductPriceResolver->getTotalTaxes($orderProduct) / $this->getReductionsCoef(
  124. $orderShop
  125. );
  126. }
  127. return $total;
  128. }
  129. public function getOrderProductsTaxesAsArray(OrderShopInterface $orderShop): array
  130. {
  131. $orderProductsTaxes = [];
  132. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  133. $idTaxRate = $orderProduct->getTaxRate()->getId();
  134. if (!isset($orderProductsTaxes[$idTaxRate])) {
  135. $orderProductsTaxes[$idTaxRate] = [
  136. 'label' => $orderProduct->getTaxRate()->getValue() . '%',
  137. 'totalOrderProducts' => 0,
  138. 'totalTaxes' => 0,
  139. ];
  140. }
  141. $orderProductsTaxes[$idTaxRate]['totalOrderProducts'] += $this->orderProductPriceResolver->getTotalWithReduction(
  142. $orderProduct
  143. ) / $this->getReductionsCoef($orderShop);
  144. $orderProductsTaxes[$idTaxRate]['totalTaxes'] += $this->orderProductPriceResolver->getTotalTaxes(
  145. $orderProduct
  146. ) / $this->getReductionsCoef($orderShop);
  147. }
  148. return $orderProductsTaxes;
  149. }
  150. private function getReductionsCoef(OrderShopInterface $orderShop): float
  151. {
  152. return $this->getTotalOrderProducts($orderShop) / $this->getTotalOrderProductsWithReductions($orderShop);
  153. }
  154. private function getTaxRateAverage(OrderShopInterface $orderShop): float
  155. {
  156. return $this->getTotalOrderProductsWithTax($orderShop) / $this->getTotalOrderProducts($orderShop);
  157. }
  158. public function getTotalOrderProductsWithReductions(OrderShopInterface $orderShop, $cache = false)
  159. {
  160. if ($cache && $orderShop->getStatTotalOrderProductsWithReductions() !== null) {
  161. return $orderShop->getStatTotalOrderProductsWithReductions();
  162. } else {
  163. $total = $this->getTotalOrderProducts($orderShop);
  164. $total -= $this->getTotalReductionCartsAmount($orderShop);
  165. $total -= $this->getTotalReductionCreditsAmount($orderShop);
  166. return $total;
  167. }
  168. }
  169. public function getTotalOrderProductsWithReductionCarts(OrderShopInterface $orderShop)
  170. {
  171. $total = $this->getTotalOrderProducts($orderShop);
  172. $total -= $this->getTotalReductionCartsAmount($orderShop);
  173. return $total;
  174. }
  175. public function getTotalReductionCartsAmount(OrderShopInterface $orderShop)
  176. {
  177. $totalReductionAmount = 0;
  178. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  179. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax(
  180. $orderShop,
  181. $orderReductionCart
  182. );
  183. }
  184. return $totalReductionAmount;
  185. }
  186. public function getTotalReductionCreditsAmount(OrderShopInterface $orderShop)
  187. {
  188. $totalReductionAmount = 0;
  189. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  190. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax(
  191. $orderShop,
  192. $orderReductionCredit
  193. );
  194. }
  195. return $totalReductionAmount;
  196. }
  197. public function getTotalOrderProductsWithTaxAndReductions(OrderShopInterface $orderShop, $cache = false)
  198. {
  199. if ($cache && $orderShop->getStatTotalOrderProductsWithTaxAndReductions() !== null) {
  200. return $orderShop->getStatTotalOrderProductsWithTaxAndReductions();
  201. } else {
  202. $total = $this->getTotalOrderProductsWithTax($orderShop);
  203. $total -= $this->getTotalReductionCartsAmountWithTax($orderShop);
  204. $total -= $this->getTotalReductionCreditsAmountWithTax($orderShop);
  205. return $total;
  206. }
  207. }
  208. public function getTotalOrderProductsWithTaxAndReductionCarts(OrderShopInterface $orderShop)
  209. {
  210. $total = $this->getTotalOrderProductsWithTax($orderShop);
  211. $total -= $this->getTotalReductionCartsAmountWithTax($orderShop);
  212. return $total;
  213. }
  214. public function getTotalReductionCartsAmountWithTax(OrderShopInterface $orderShop)
  215. {
  216. $totalReductionAmount = 0;
  217. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  218. $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithTax($orderShop, $orderReductionCart);
  219. }
  220. return $totalReductionAmount;
  221. }
  222. public function getTotalReductionCreditsAmountWithTax(OrderShopInterface $orderShop)
  223. {
  224. $totalReductionAmount = 0;
  225. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  226. $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithTax(
  227. $orderShop,
  228. $orderReductionCredit
  229. );
  230. }
  231. return $totalReductionAmount;
  232. }
  233. public function getOrderProductsReductionCartAmountWithoutTax(OrderShopInterface $order, $orderReductionCart)
  234. {
  235. $amount = 0;
  236. if ($orderReductionCart->getAppliedTo() === ReductionCartModel::APPLIED_TO_ORDER_PRODUCTS) {
  237. if ($orderReductionCart->getUnit() == 'percent') {
  238. $amount = $this->amountReductionByPercentValue(
  239. $this->getTotalOrderProducts($order),
  240. $orderReductionCart->getValue()
  241. );
  242. } else {
  243. if ($orderReductionCart->getUnit() == 'amount') {
  244. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  245. $amount = $orderReductionCart->getValue();
  246. } else {
  247. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  248. $amount = $this->round($orderReductionCart->getValue() / $this->getTaxRateAverage($order));
  249. }
  250. }
  251. }
  252. }
  253. }
  254. return $amount;
  255. }
  256. public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
  257. {
  258. $amount = 0;
  259. if ($orderReductionCart->getAppliedTo() === ReductionCartModel::APPLIED_TO_ORDER_PRODUCTS) {
  260. if ($orderReductionCart->getUnit() == 'percent') {
  261. $amount = $this->amountReductionByPercentValue(
  262. $this->getTotalOrderProductsWithTax($order),
  263. $orderReductionCart->getValue()
  264. );
  265. } elseif ($orderReductionCart->getUnit() == 'amount') {
  266. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
  267. $amount = $this->round($orderReductionCart->getValue() * $this->getTaxRateAverage($order));
  268. } elseif ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
  269. $amount = $orderReductionCart->getValue();
  270. }
  271. }
  272. }
  273. return $amount;
  274. }
  275. public function getOrderProductsReductionCreditAmountWithoutTax(
  276. OrderShopInterface $order,
  277. OrderReductionCreditInterface $orderReductionCredit
  278. ) {
  279. $amount = 0;
  280. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  281. $amount = $orderReductionCredit->getValue();
  282. } else {
  283. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  284. $amount = $this->round($orderReductionCredit->getValue() / $this->getTaxRateAverage($order));
  285. }
  286. }
  287. return $amount;
  288. }
  289. public function getOrderProductsReductionCreditAmountWithTax(
  290. OrderShopInterface $order,
  291. OrderReductionCreditInterface $orderReductionCredit
  292. ) {
  293. $amountWithTax = 0;
  294. if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
  295. $amountWithTax = $this->round($orderReductionCredit->getValue() * $this->getTaxRateAverage($order));
  296. } elseif ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
  297. $amountWithTax = $orderReductionCredit->getValue();
  298. }
  299. return $amountWithTax;
  300. }
  301. public function getTotalReductions(OrderShopInterface $orderShop)
  302. {
  303. $total = 0;
  304. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  305. $total += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop, $orderReductionCart);
  306. }
  307. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  308. $total += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop, $orderReductionCredit);
  309. }
  310. return $total;
  311. }
  312. public function getTotalReductionsWithTax(OrderShopInterface $orderShop)
  313. {
  314. $total = 0;
  315. foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
  316. $total += $this->getOrderProductsReductionCartAmountWithTax($orderShop, $orderReductionCart);
  317. }
  318. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  319. $total += $this->getOrderProductsReductionCreditAmountWithTax($orderShop, $orderReductionCredit);
  320. }
  321. return $total;
  322. }
  323. }