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.

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