您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

OrderShopBuilder.php 25KB

3 年前
3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. namespace Lc\CaracoleBundle\Builder\Order;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
  5. use Lc\CaracoleBundle\Event\Order\OrderShopChangeStatusEvent;
  6. use Lc\CaracoleBundle\Factory\File\DocumentFactory;
  7. use Lc\CaracoleBundle\Factory\Order\OrderPaymentFactory;
  8. use Lc\CaracoleBundle\Factory\Order\OrderProductFactory;
  9. use Lc\CaracoleBundle\Factory\Order\OrderProductReductionCatalogFactory;
  10. use Lc\CaracoleBundle\Factory\Order\OrderReductionCartFactory;
  11. use Lc\CaracoleBundle\Factory\Order\OrderReductionCreditFactory;
  12. use Lc\CaracoleBundle\Factory\Order\OrderShopFactory;
  13. use Lc\CaracoleBundle\Factory\Order\OrderStatusHistoryFactory;
  14. use Lc\CaracoleBundle\Model\Address\AddressInterface;
  15. use Lc\CaracoleBundle\Model\File\DocumentInterface;
  16. use Lc\CaracoleBundle\Model\File\DocumentModel;
  17. use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
  18. use Lc\CaracoleBundle\Model\Order\OrderReductionCartInterface;
  19. use Lc\CaracoleBundle\Model\Order\OrderReductionCreditInterface;
  20. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  21. use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
  22. use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
  23. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  24. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  25. use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
  26. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
  27. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  28. use Lc\CaracoleBundle\Model\Section\SectionModel;
  29. use Lc\CaracoleBundle\Model\User\VisitorInterface;
  30. use Lc\CaracoleBundle\Repository\Order\OrderProductStore;
  31. use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
  32. use Lc\CaracoleBundle\Repository\Order\OrderStatusStore;
  33. use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore;
  34. use Lc\CaracoleBundle\Resolver\OpeningResolver;
  35. use Lc\CaracoleBundle\Resolver\OrderShopResolver;
  36. use Lc\CaracoleBundle\Solver\Order\OrderProductReductionCatalogSolver;
  37. use Lc\CaracoleBundle\Solver\Order\OrderShopSolver;
  38. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  39. use Lc\CaracoleBundle\Solver\Product\ProductSolver;
  40. use Lc\CaracoleBundle\Statistic\Product\ProductsSalesStatistic;
  41. use Lc\SovBundle\Model\User\UserInterface;
  42. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  43. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  44. class OrderShopBuilder
  45. {
  46. protected EntityManagerInterface $entityManager;
  47. protected OrderStatusStore $orderStatusStore;
  48. protected OrderProductStore $orderProductStore;
  49. protected OrderShopStore $orderShopStore;
  50. protected OrderShopSolver $orderShopSolver;
  51. protected ProductFamilyStore $productFamilyStore;
  52. protected PriceSolver $priceSolver;
  53. protected OrderProductBuilder $orderProductBuilder;
  54. protected DocumentBuilder $documentBuilder;
  55. protected EventDispatcherInterface $eventDispatcher;
  56. protected FlashBagInterface $flashBag;
  57. protected OpeningResolver $openingResolver;
  58. protected ProductSolver $productSolver;
  59. protected OrderShopResolver $orderShopResolver;
  60. protected OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver;
  61. public function __construct(
  62. EntityManagerInterface $entityManager,
  63. OrderShopStore $orderShopStore,
  64. OrderShopSolver $orderShopSolver,
  65. OrderStatusStore $orderStatusStore,
  66. OrderProductStore $orderProductStore,
  67. ProductFamilyStore $productFamilyStore,
  68. OrderProductBuilder $orderProductBuilder,
  69. DocumentBuilder $documentBuilder,
  70. PriceSolver $priceSolver,
  71. EventDispatcherInterface $eventDispatcher,
  72. FlashBagInterface $flashBag,
  73. OpeningResolver $openingResolver,
  74. ProductSolver $productSolver,
  75. OrderShopResolver $orderShopResolver,
  76. OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver
  77. ) {
  78. $this->entityManager = $entityManager;
  79. $this->orderShopStore = $orderShopStore;
  80. $this->orderShopSolver = $orderShopSolver;
  81. $this->orderStatusStore = $orderStatusStore;
  82. $this->orderProductStore = $orderProductStore;
  83. $this->productFamilyStore = $productFamilyStore;
  84. $this->orderProductBuilder = $orderProductBuilder;
  85. $this->documentBuilder = $documentBuilder;
  86. $this->priceSolver = $priceSolver;
  87. $this->eventDispatcher = $eventDispatcher;
  88. $this->flashBag = $flashBag;
  89. $this->openingResolver = $openingResolver;
  90. $this->productSolver = $productSolver;
  91. $this->orderShopResolver = $orderShopResolver;
  92. $this->orderProductReductionCatalogSolver = $orderProductReductionCatalogSolver;
  93. }
  94. public function create(
  95. SectionInterface $section,
  96. UserInterface $user = null,
  97. VisitorInterface $visitor = null
  98. ): OrderShopInterface {
  99. $orderShopFactory = new OrderShopFactory();
  100. $orderShop = $orderShopFactory->create($section, $user, $visitor);
  101. $this->setOrderStatus($orderShop, OrderStatusModel::ALIAS_CART);
  102. $this->entityManager->create($orderShop);
  103. $this->entityManager->flush();
  104. return $orderShop;
  105. }
  106. public function createIfNotExist(
  107. SectionInterface $section,
  108. UserInterface $user = null,
  109. VisitorInterface $visitor = null
  110. ): OrderShopInterface {
  111. $cart = $this->orderShopStore
  112. ->setSection($section)
  113. ->getOneCartCurrent($user, $visitor);
  114. if (!$cart) {
  115. $cart = $this->create($section, $user, $visitor);
  116. }
  117. return $cart;
  118. }
  119. public function setOrderStatus(
  120. OrderShopInterface $orderShop,
  121. string $alias,
  122. bool $forceByAdmin = false
  123. ): OrderShopInterface {
  124. $orderStatus = $this->orderStatusStore->getOneByAlias($alias);
  125. if ($orderStatus) {
  126. if ($orderShop->getOrderStatus() === null
  127. || $orderShop->getOrderStatus()->getNextStatusAllowed()->contains($orderStatus)) {
  128. $this->applyChangeOrderStatus($orderShop, $orderStatus, $forceByAdmin);
  129. }
  130. } else {
  131. throw new \ErrorException('La statut demandé n\'existe pas.');
  132. }
  133. return $orderShop;
  134. }
  135. public function applyChangeOrderStatus(
  136. OrderShopInterface $orderShop,
  137. OrderStatusInterface $orderStatus,
  138. bool $forceByAdmin = false
  139. ): void {
  140. $this->eventDispatcher->dispatch(
  141. new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
  142. OrderShopChangeStatusEvent::PRE_CHANGE_STATUS
  143. );
  144. $orderShop->setOrderStatusProtected($orderStatus);
  145. $orderStatusHistoryFactory = new OrderStatusHistoryFactory();
  146. $orderStatusHistory = $orderStatusHistoryFactory->create($orderShop, $orderStatus);
  147. $orderShop->addOrderStatusHistory($orderStatusHistory);
  148. $this->eventDispatcher->dispatch(
  149. new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
  150. OrderShopChangeStatusEvent::POST_CHANGE_STATUS
  151. );
  152. }
  153. public function addOrderProduct(
  154. OrderShopInterface $orderShop,
  155. OrderProductInterface $orderProductAdd,
  156. bool $persist = true
  157. ): bool {
  158. $return = false;
  159. if ($this->orderShopSolver->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
  160. if ($orderProductAdd->getQuantityOrder() > 0) {
  161. $updated = false;
  162. $this->orderProductBuilder->init($orderProductAdd);
  163. $productFamily = $this->productFamilyStore->getOneBySlug(
  164. $orderProductAdd->getProduct()->getProductFamily()->getSlug()
  165. );
  166. if ($productFamily) {
  167. $reductionCatalog = $productFamily->getReductionCatalog();
  168. if ($reductionCatalog) {
  169. $orderProductReductionCatalogFactory = new OrderProductReductionCatalogFactory();
  170. $orderProductReductionCatalog = $orderProductReductionCatalogFactory->create(
  171. $reductionCatalog->getTitle(),
  172. $reductionCatalog->getValue(),
  173. $reductionCatalog->getUnit(),
  174. $reductionCatalog->getBehaviorTaxRate()
  175. );
  176. $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);
  177. }
  178. }
  179. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  180. if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
  181. && $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery()
  182. && (string)$this->priceSolver->getPrice($orderProduct)
  183. == (string)$this->priceSolver->getPrice($orderProductAdd)
  184. && $orderProduct->getOrderProductReductionCatalog()
  185. && $orderProductAdd->getOrderProductReductionCatalog()
  186. && $this->orderProductReductionCatalogSolver->compare(
  187. $orderProduct->getOrderProductReductionCatalog(),
  188. $orderProductAdd->getOrderProductReductionCatalog()
  189. )) {
  190. $orderProduct->setQuantityOrder(
  191. $orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
  192. );
  193. if ($persist) {
  194. $this->entityManager->persist($orderProduct);
  195. }
  196. $updated = true;
  197. $return = true;
  198. break;
  199. }
  200. }
  201. if (!$updated) {
  202. $orderShop->addOrderProduct($orderProductAdd);
  203. if ($persist) {
  204. if (isset($orderProductReductionCatalog)) {
  205. $this->entityManager->create($orderProductReductionCatalog);
  206. }
  207. //TODO est-ce un update ou un create ???
  208. $this->entityManager->persist($orderProductAdd);
  209. $this->entityManager->update($orderShop);
  210. }
  211. $return = true;
  212. }
  213. if ($persist) {
  214. $this->entityManager->flush();
  215. }
  216. // @TODO : dispatch event cart change
  217. //$this->eventCartChange($orderShop);
  218. }
  219. } else {
  220. // @TODO : retourner le message d'erreur et faire le addFlash dans le contrôleur
  221. /*$availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited();
  222. $textError = "Le produit <strong>" . $orderProductAdd->getTitleOrderShop(
  223. ) . "</strong> n'est pas disponible";
  224. if ($availableQuantity !== false && $availableQuantity > 0) {
  225. $unit = '';
  226. if ($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock(
  227. ) == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  228. $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnitReference()->getUnit();
  229. }
  230. $textError .= ' dans cette quantité ';
  231. $user = $this->security->getUser();
  232. if ($user && $user->hasRole('ROLE_USER')) {
  233. $textError .= '<br />' . $availableQuantity . $unit . ' disponible(s) dont ' . $this->getQuantityOrderByProduct(
  234. $orderShop,
  235. $orderProductAdd->getProduct()
  236. ) . $unit . ' déjà dans votre panier.';
  237. }
  238. }
  239. $this->utils->addFlash('error', $textError);*/
  240. }
  241. return $return;
  242. }
  243. public function merge(
  244. OrderShopInterface $orderShop1,
  245. OrderShopInterface $orderShop2,
  246. $persist = true
  247. ): OrderShopInterface {
  248. if ($orderShop1 && $orderShop2) {
  249. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  250. $orderProductAlreadyInCart = $orderShop1->hasOrderProductAlreadyInCart($orderProduct);
  251. if ($orderProductAlreadyInCart) {
  252. if ($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) {
  253. $orderShop1->removeOrderProduct($orderProductAlreadyInCart);
  254. $this->addOrderProduct($orderShop1, $orderProduct);
  255. }
  256. } else {
  257. $this->addOrderProduct($orderShop1, $orderProduct);
  258. }
  259. if ($persist) {
  260. $this->entityManager->delete($orderProduct);
  261. }
  262. }
  263. if ($persist) {
  264. $this->entityManager->delete($orderShop2);
  265. $this->entityManager->update($orderShop1);
  266. $this->entityManager->flush();
  267. }
  268. return $orderShop1;
  269. }
  270. }
  271. public function addPayment(OrderShopInterface $orderShop, string $meanPayment, float $amount): OrderShopInterface
  272. {
  273. $orderPaymentFactory = new OrderPaymentFactory();
  274. $orderPayment = $orderPaymentFactory->create($orderShop, $meanPayment, $amount);
  275. $orderShop->addOrderPayment($orderPayment);
  276. if ($this->orderShopResolver->isPaid($orderShop)) {
  277. $nextStatus = OrderStatusModel::ALIAS_PAID;
  278. } else {
  279. $nextStatus = OrderStatusModel::ALIAS_PARTIAL_PAYMENT;
  280. }
  281. if ($orderShop->getOrderStatus()->getAlias() != $nextStatus) {
  282. $this->changeOrderStatus($orderShop, $nextStatus);
  283. }
  284. $this->entityManager->create($orderPayment);
  285. $this->entityManager->update($orderShop);
  286. $this->entityManager->flush();
  287. return $orderShop;
  288. }
  289. public function initStatsInfo(OrderShopInterface $orderShop, $flush = true)
  290. {
  291. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  292. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  293. $orderShop->setStatTotalOrderProductsWithReductions(
  294. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  295. );
  296. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  297. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  298. );
  299. $orderShop->setStatMarginOrderProductsWithReductions(
  300. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  301. );
  302. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  303. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  304. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  305. );
  306. $this->entityManager->persist($orderShop);
  307. if ($flush) {
  308. $this->entityManager->flush();
  309. }
  310. }
  311. public function initCycleNumber(OrderShopInterface $orderShop): void
  312. {
  313. $cycleNumber = null;
  314. $deliveryDate = $orderShop->getDeliveryDate();
  315. switch ($orderShop->getSection()->getCycleType()) {
  316. case SectionModel::CYCLE_TYPE_DAY:
  317. $cycleNumber = $deliveryDate->format('z');
  318. break;
  319. case SectionModel::CYCLE_TYPE_WEEK:
  320. $cycleNumber = $deliveryDate->format('W');
  321. break;
  322. }
  323. $orderShop->setCycleNumber($cycleNumber);
  324. }
  325. public function createDocumentInvoice(OrderShopInterface $orderShop): DocumentInterface
  326. {
  327. $documentFactory = new DocumentFactory();
  328. $document = $documentFactory->create($orderShop->getSection(), DocumentModel::TYPE_INVOICE);
  329. $this->documentBuilder->initFromOrderShop($document, $orderShop);
  330. return $document;
  331. }
  332. public function addReductionCart(
  333. OrderShopInterface $orderShop,
  334. ReductionCartInterface $reductionCart
  335. ): ?OrderReductionCartInterface {
  336. $orderReductionCartFactory = new OrderReductionCartFactory();
  337. $orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart);
  338. $orderShop->addOrderReductionCart($orderReductionCart);
  339. if ($this->orderShopResolver->isPositiveAmount($orderShop)
  340. && $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
  341. $this->entityManager->create($orderReductionCart);
  342. $this->entityManager->flush();
  343. return $orderReductionCart;
  344. } else {
  345. //TODO vérifier ce case ! Avec le null en valeur de retour
  346. $orderShop->removeOrderReductionCart($orderReductionCart);
  347. return null;
  348. }
  349. }
  350. // createOrderReductionCredit
  351. public function addReductionCredit(
  352. OrderShopInterface $orderShop,
  353. ReductionCreditInterface $reductionCredit
  354. ): ?OrderReductionCreditInterface {
  355. $orderReductionCreditFactory = new OrderReductionCreditFactory();
  356. $orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit);
  357. $orderShop->addOrderReductionCredit($orderReductionCredit);
  358. if ($this->isOrderShopPositiveAmount($orderShop)
  359. && $this->isOrderShopPositiveAmountRemainingToBePaid($orderShop)) {
  360. $this->entityManager->create($orderReductionCredit);
  361. $this->entityManager->flush();
  362. return $orderReductionCredit;
  363. } else {
  364. $orderShop->removeOrderReductionCredit($orderReductionCredit);
  365. return null;
  366. }
  367. }
  368. public function deductAvailabilityProduct(OrderShopInterface $orderShop): void
  369. {
  370. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  371. $this->applyDeductAvailabilityProduct($orderShop, $orderProduct);
  372. }
  373. }
  374. public function applyDeductAvailabilityProduct(
  375. OrderShopInterface $orderShop,
  376. OrderProductInterface $orderProduct
  377. ): void {
  378. switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
  379. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  380. //Disponibilité par unité de référence
  381. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($orderProduct->getProduct());
  382. $newAvailability = $oldAvailability - ($orderProduct->getQuantityOrder(
  383. ) * ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()));
  384. $productFamily = $orderProduct->getProduct()->getProductFamily();
  385. $productFamily->setAvailableQuantity($newAvailability);
  386. $productFamily->setUpdatedBy($orderShop->getUser());
  387. $this->entityManager->update($productFamily);
  388. break;
  389. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  390. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($orderProduct->getProduct());
  391. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  392. $productFamily = $orderProduct->getProduct()->getProductFamily();
  393. $productFamily->setAvailableQuantity($newAvailability);
  394. $productFamily->setUpdatedBy($orderShop->getUser());
  395. $this->entityManager->update($productFamily);
  396. break;
  397. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  398. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($orderProduct->getProduct());
  399. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  400. $product = $orderProduct->getProduct();
  401. $product->setAvailableQuantity($newAvailability);
  402. $product->setUpdatedBy($orderShop->getUser());
  403. $this->entityManager->update($product);
  404. break;
  405. }
  406. $this->entityManager->flush();
  407. }
  408. public function updatePriceByProductFamily(ProductFamilyInterface $productFamily)
  409. {
  410. // @TODO : faire la vérification isOpenSale depuis la méthode appelante
  411. if (!$this->openingResolver->isOpenSale(
  412. $productFamily->getSection(),
  413. null,
  414. OpeningResolver::OPENING_CONTEXT_BACKEND
  415. )) {
  416. $countOrderProductUpdated = 0;
  417. foreach ($productFamily->getProducts() as $product) {
  418. $orderProducts = $this->orderProductStore->getInCartsByProduct($product);
  419. foreach ($orderProducts as $orderProduct) {
  420. $quantityOrder = $orderProduct->getQuantityOrder();
  421. $orderShop = $orderProduct->getOrderShop();
  422. $orderShop->removeOrderProduct($orderProduct);
  423. $this->entityManager->delete($orderProduct);
  424. $this->entityManager->flush();
  425. $this->entityManager->refresh($orderShop);
  426. $orderProductFactory = new OrderProductFactory();
  427. $addOrderProduct = $orderProductFactory->create($product, $quantityOrder);
  428. $this->addOrderProduct($orderShop, $addOrderProduct);
  429. $countOrderProductUpdated++;
  430. }
  431. }
  432. if ($countOrderProductUpdated) {
  433. // @TODO : faire le add flash dans le controller
  434. /*$this->utils->addFlash(
  435. 'success',
  436. 'success.OrderShop.orderProductUpdated',
  437. array(),
  438. array('%count%' => $countOrderProductUpdated)
  439. );*/
  440. $this->entityManager->flush();
  441. }
  442. return $countOrderProductUpdated;
  443. }
  444. }
  445. public function setStatsInfo(OrderShopInterface $orderShop, $flush = true)
  446. {
  447. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  448. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  449. $orderShop->setStatTotalOrderProductsWithReductions(
  450. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  451. );
  452. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  453. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  454. );
  455. $orderShop->setStatMarginOrderProductsWithReductions(
  456. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  457. );
  458. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  459. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  460. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  461. );
  462. $this->entityManager->update($orderShop);
  463. if ($flush) {
  464. $this->entityManager->flush();
  465. }
  466. }
  467. public function setHasReach(int $reachStep, OrderShopInterface $orderShop)
  468. {
  469. if ($orderShop->getHasReach() === null || $orderShop->getHasReach() < $reachStep) {
  470. $orderShop->setHasReach($reachStep);
  471. $this->entityManager->persist($orderShop);
  472. $this->entityManager->flush($orderShop);
  473. }
  474. }
  475. public function initComplementaryOrderShop(OrderShopInterface $orderShop, OrderShopInterface $mainOrderShop): void
  476. {
  477. $orderShop->setMainOrderShop($mainOrderShop);
  478. $orderShop->setDeliveryPrice(0);
  479. if ($mainOrderShop->getDeliveryAddress()) {
  480. $this->initDeliveryAddress($orderShop, $mainOrderShop->getDeliveryAddress());
  481. }
  482. $orderShop->setInvoiceAddress($mainOrderShop->getInvoiceAddress());
  483. }
  484. // setDeliveryAddress
  485. public function initDeliveryAddress(OrderShopInterface $orderShop, AddressInterface $address = null): void
  486. {
  487. $orderShop->setDeliveryAddress($address);
  488. $orderShop->setDeliveryInfos($address ? $address->getDeliveryInfos() : null);
  489. }
  490. // resetOrderShopInfos
  491. public function reset(OrderShopInterface $orderShop)
  492. {
  493. $this->initDeliveryAddress($orderShop, null);
  494. $orderShop->setMainOrderShop(null);
  495. $orderShop->setDeliveryPrice(null);
  496. $orderShop->setInvoiceAddress(null);
  497. $orderShop->setDeclineComplementaryOrderShop(false);
  498. }
  499. public function getProductsSalesStatistic(SectionInterface $section, $entity, $nbWeek = 2)
  500. {
  501. $productsSalesStatistic = new ProductsSalesStatistic(
  502. $this->entityManager,
  503. $entity,
  504. $nbWeek,
  505. $this->productSolver
  506. );
  507. $productsSalesStatistic->init($section, $this->orderShopSolver, $this->openingResolver);
  508. $productsSalesStatistic->populateProperties($this->orderShopStore);
  509. return $productsSalesStatistic->getAsArray();
  510. }
  511. }