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

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