Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

649 Zeilen
26KB

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