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.

650 líneas
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. ): ?array {
  203. $orderProductAddedArray = [];
  204. $orderProductAdd->setOrderShop($orderShop);
  205. if ($this->orderShopSolver->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
  206. if ($orderProductAdd->getQuantityOrder() > 0) {
  207. $orderProductAddedArray[] = $orderProductAdd;
  208. $updated = false;
  209. $this->orderProductBuilder->init($orderProductAdd);
  210. if(is_null($orderProductAdd->getOrderProductReductionCatalog())) {
  211. // on hydrate $productFamily avec réduction
  212. $productFamily = $this->productFamilyStore->setSection($orderShop->getSection())->getOneBySlug(
  213. $orderProductAdd->getProduct()->getProductFamily()->getSlug()
  214. );
  215. if($productFamily) {
  216. $this->orderProductBuilder->initReductionCatalog($orderShop->getSection(), $orderProductAdd, $productFamily);
  217. }
  218. }
  219. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  220. if ($this->orderProductSolver->compare($orderProduct, $orderProductAdd)) {
  221. $orderProduct->setQuantityOrder(
  222. $orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
  223. );
  224. if ($persist) {
  225. $this->entityManager->update($orderProduct);
  226. }
  227. $updated = 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. }
  242. if ($persist) {
  243. $this->entityManager->flush();
  244. $this->eventDispatcher->dispatch(new CartChangeEvent($orderShop), CartChangeEvent::POST_UPDATE);
  245. }
  246. }
  247. } else {
  248. // @TODO : retourner le message d'erreur et faire le addFlash dans le contrôleur
  249. /*$availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited();
  250. $textError = "Le produit <strong>" . $orderProductAdd->getTitleOrderShop(
  251. ) . "</strong> n'est pas disponible";
  252. if ($availableQuantity !== false && $availableQuantity > 0) {
  253. $unit = '';
  254. if ($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock(
  255. ) == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  256. $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnitReference()->getUnit();
  257. }
  258. $textError .= ' dans cette quantité ';
  259. $user = $this->security->getUser();
  260. if ($user && $user->hasRole('ROLE_USER')) {
  261. $textError .= '<br />' . $availableQuantity . $unit . ' disponible(s) dont ' . $this->getQuantityOrderByProduct(
  262. $orderShop,
  263. $orderProductAdd->getProduct()
  264. ) . $unit . ' déjà dans votre panier.';
  265. }
  266. }
  267. $this->utils->addFlash('error', $textError);*/
  268. }
  269. if(count($orderProductAddedArray) == 0) {
  270. return null;
  271. }
  272. return $orderProductAddedArray;
  273. }
  274. public function merge(
  275. OrderShopInterface $orderShop1,
  276. OrderShopInterface $orderShop2,
  277. $persist = true
  278. ): OrderShopInterface {
  279. // TODO essayer de comprendre pourquoi on doit faire un refresh ici
  280. $this->entityManager->refresh($orderShop1);
  281. $this->entityManager->refresh($orderShop2);
  282. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  283. $orderProductAlreadyInCart = $this->orderShopSolver->hasOrderProductAlreadyInCart(
  284. $orderShop1,
  285. $orderProduct
  286. );
  287. if ($orderProductAlreadyInCart) {
  288. if ($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) {
  289. $orderShop1->removeOrderProduct($orderProductAlreadyInCart);
  290. $this->addOrderProduct($orderShop1, $orderProduct);
  291. }
  292. } else {
  293. $this->addOrderProduct($orderShop1, $orderProduct);
  294. }
  295. if ($persist) {
  296. $this->entityManager->delete($orderProduct);
  297. }
  298. }
  299. if ($persist) {
  300. $this->entityManager->delete($orderShop2);
  301. $this->entityManager->update($orderShop1);
  302. $this->entityManager->flush();
  303. }
  304. return $orderShop1;
  305. }
  306. public function addPayment(OrderShopInterface $orderShop, string $meanPayment, float $amount): OrderShopInterface
  307. {
  308. $orderPaymentFactory = new OrderPaymentFactory();
  309. $orderPayment = $orderPaymentFactory->create($orderShop, $meanPayment, $amount);
  310. $orderShop->addOrderPayment($orderPayment);
  311. if ($meanPayment == OrderPaymentModel::MEAN_PAYMENT_CREDIT) {
  312. $this->creditHistoryBuilder->create(
  313. CreditHistoryModel::TYPE_DEBIT,
  314. $this->merchantResolver->getUserMerchant(),
  315. [
  316. 'orderPayment' => $orderPayment
  317. ]
  318. );
  319. }
  320. if ($this->orderShopResolver->isPaid($orderShop)) {
  321. $nextStatus = OrderStatusModel::ALIAS_PAID;
  322. } else {
  323. $nextStatus = OrderStatusModel::ALIAS_PARTIAL_PAYMENT;
  324. }
  325. if ($orderShop->getOrderStatus()->getAlias() != $nextStatus) {
  326. $this->changeOrderStatus($orderShop, $nextStatus);
  327. }
  328. $this->entityManager->create($orderPayment);
  329. $this->entityManager->update($orderShop);
  330. $this->entityManager->flush();
  331. return $orderShop;
  332. }
  333. public function initStatsInfo(OrderShopInterface $orderShop, $flush = true)
  334. {
  335. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  336. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  337. $orderShop->setStatTotalOrderProductsWithReductions(
  338. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  339. );
  340. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  341. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  342. );
  343. $orderShop->setStatMarginOrderProductsWithReductions(
  344. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  345. );
  346. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  347. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  348. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  349. );
  350. $this->entityManager->persist($orderShop);
  351. if ($flush) {
  352. $this->entityManager->flush();
  353. }
  354. }
  355. //initCycleNumber
  356. public function initDistribution(OrderShopInterface $orderShop): void
  357. {
  358. $distribution = $this->distributionBuilder->guessDistributionByDeliveryDate(
  359. $orderShop->getDeliveryDate(),
  360. $orderShop->getSection()
  361. );
  362. $orderShop->setDistribution($distribution);
  363. }
  364. public function createDocumentInvoice(OrderShopInterface $orderShop): DocumentInterface
  365. {
  366. $documentFactory = new DocumentFactory();
  367. $document = $documentFactory->create($orderShop->getSection()->getMerchant(), DocumentModel::TYPE_INVOICE);
  368. $this->documentBuilder->initFromOrderShop($document, $orderShop);
  369. return $document;
  370. }
  371. public function addReductionCart(
  372. OrderShopInterface $orderShop,
  373. ReductionCartInterface $reductionCart
  374. ): ?OrderReductionCartInterface {
  375. $orderReductionCartFactory = new OrderReductionCartFactory();
  376. $orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart);
  377. $orderShop->addOrderReductionCart($orderReductionCart);
  378. if ($this->orderShopResolver->isPositiveAmount($orderShop)
  379. && $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
  380. $this->entityManager->create($orderReductionCart);
  381. $this->entityManager->flush();
  382. return $orderReductionCart;
  383. } else {
  384. //TODO vérifier ce case ! Avec le null en valeur de retour
  385. $orderShop->removeOrderReductionCart($orderReductionCart);
  386. return null;
  387. }
  388. }
  389. // createOrderReductionCredit
  390. public function addReductionCredit(
  391. OrderShopInterface $orderShop,
  392. ReductionCreditInterface $reductionCredit
  393. ): ?OrderReductionCreditInterface {
  394. $orderReductionCreditFactory = new OrderReductionCreditFactory();
  395. $orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit);
  396. $orderShop->addOrderReductionCredit($orderReductionCredit);
  397. if ($this->orderShopResolver->isPositiveAmount($orderShop)
  398. && $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
  399. $this->entityManager->create($orderReductionCredit);
  400. $this->entityManager->flush();
  401. return $orderReductionCredit;
  402. } else {
  403. $orderShop->removeOrderReductionCredit($orderReductionCredit);
  404. return null;
  405. }
  406. }
  407. public function deductAvailabilityProduct(OrderShopInterface $orderShop): void
  408. {
  409. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  410. $this->applyDeductAvailabilityProduct($orderShop, $orderProduct->getProduct(), $orderProduct->getQuantityOrder());
  411. }
  412. }
  413. public function applyDeductAvailabilityProduct(
  414. OrderShopInterface $orderShop,
  415. ProductInterface $product,
  416. int $quantityOrder
  417. ): void {
  418. switch ($product->getProductFamily()->getBehaviorCountStock()) {
  419. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  420. //Disponibilité par unité de référence
  421. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($product);
  422. $newAvailability = $oldAvailability - ($quantityOrder * ($this->productSolver->getQuantityInherited(
  423. $product
  424. ) / $this->productSolver->getUnitInherited($product)->getCoefficient()));
  425. $productFamily = $product->getProductFamily();
  426. $productFamily->setAvailableQuantity($newAvailability);
  427. $productFamily->setUpdatedBy($orderShop->getUser());
  428. $this->entityManager->update($productFamily);
  429. break;
  430. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  431. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($product);
  432. $newAvailability = $oldAvailability - $quantityOrder;
  433. $productFamily = $product->getProductFamily();
  434. $productFamily->setAvailableQuantity($newAvailability);
  435. $productFamily->setUpdatedBy($orderShop->getUser());
  436. $this->entityManager->update($productFamily);
  437. break;
  438. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  439. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($product);
  440. $newAvailability = $oldAvailability - $quantityOrder;
  441. $product->setAvailableQuantity($newAvailability);
  442. $product->setUpdatedBy($orderShop->getUser());
  443. $this->entityManager->update($product);
  444. break;
  445. }
  446. $this->entityManager->flush();
  447. }
  448. public function updatePriceByProductFamily(ProductFamilyInterface $productFamily, SectionInterface $section)
  449. {
  450. $countOrderProductUpdated = 0;
  451. foreach ($productFamily->getProducts() as $product) {
  452. $orderProducts = $this->orderProductStore->resetContext()->setSection(
  453. $section
  454. )->getInCartsByProduct($product);
  455. foreach ($orderProducts as $orderProduct) {
  456. $quantityOrder = $orderProduct->getQuantityOrder();
  457. $orderShop = $orderProduct->getOrderShop();
  458. $orderShop->removeOrderProduct($orderProduct);
  459. $this->entityManager->delete($orderProduct);
  460. $this->entityManager->flush();
  461. $this->entityManager->refresh($orderShop);
  462. $orderProductFactory = new OrderProductFactory();
  463. $addOrderProduct = $orderProductFactory->create($product, $quantityOrder);
  464. $this->addOrderProduct($orderShop, $addOrderProduct);
  465. $countOrderProductUpdated++;
  466. }
  467. }
  468. if ($countOrderProductUpdated) {
  469. $this->entityManager->flush();
  470. }
  471. return $countOrderProductUpdated;
  472. }
  473. public function setStatsInfo(OrderShopInterface $orderShop, $flush = true)
  474. {
  475. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  476. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  477. $orderShop->setStatTotalOrderProductsWithReductions(
  478. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  479. );
  480. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  481. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  482. );
  483. $orderShop->setStatMarginOrderProductsWithReductions(
  484. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  485. );
  486. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  487. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  488. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  489. );
  490. $this->entityManager->update($orderShop);
  491. if ($flush) {
  492. $this->entityManager->flush();
  493. }
  494. }
  495. public function setHasReach(int $reachStep, OrderShopInterface $orderShop)
  496. {
  497. if ($orderShop->getHasReach() === null || $orderShop->getHasReach() < $reachStep) {
  498. $orderShop->setHasReach($reachStep);
  499. $this->entityManager->persist($orderShop);
  500. $this->entityManager->flush($orderShop);
  501. }
  502. }
  503. public function initComplementaryOrderShop(OrderShopInterface $orderShop, OrderShopInterface $mainOrderShop): void
  504. {
  505. $orderShop->setMainOrderShop($mainOrderShop);
  506. $orderShop->setDeliveryPrice(0);
  507. if ($mainOrderShop->getDeliveryAddress()) {
  508. $this->initDeliveryAddress($orderShop, $mainOrderShop->getDeliveryAddress());
  509. }
  510. $orderShop->setInvoiceAddress($mainOrderShop->getInvoiceAddress());
  511. }
  512. // setDeliveryAddress
  513. public function initDeliveryAddress(OrderShopInterface $orderShop, AddressInterface $address = null): void
  514. {
  515. $orderShop->setDeliveryAddress($address);
  516. $orderShop->setDeliveryInfos($address ? $address->getDeliveryInfos() : null);
  517. }
  518. // resetOrderShopInfos
  519. public function reset(OrderShopInterface $orderShop)
  520. {
  521. $this->initDeliveryAddress($orderShop, null);
  522. $orderShop->setMainOrderShop(null);
  523. $orderShop->setDeliveryPrice(null);
  524. $orderShop->setInvoiceAddress(null);
  525. $orderShop->setDeclineComplementaryOrderShop(false);
  526. }
  527. public function getProductsSalesStatistic(SectionInterface $section, $entity, $nbWeek = 2)
  528. {
  529. $productsSalesStatistic = new ProductsSalesStatistic(
  530. $this->entityManager,
  531. $entity,
  532. $nbWeek,
  533. $this->productSolver
  534. );
  535. $productsSalesStatistic->init($section, $this->distributionBuilder);
  536. $productsSalesStatistic->populateProperties($this->orderShopStore->resetContext()->setSection($section));
  537. return $productsSalesStatistic->getAsArray();
  538. }
  539. }