No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

OrderShopPriceSolver.php 14KB

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