You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

661 lines
27KB

  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\OpeningResolver;
  41. use Lc\CaracoleBundle\Resolver\OrderShopResolver;
  42. use Lc\CaracoleBundle\Solver\Order\OrderProductReductionCatalogSolver;
  43. use Lc\CaracoleBundle\Solver\Order\OrderProductSolver;
  44. use Lc\CaracoleBundle\Solver\Order\OrderShopSolver;
  45. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  46. use Lc\CaracoleBundle\Solver\Product\ProductSolver;
  47. use Lc\CaracoleBundle\Statistic\Product\ProductsSalesStatistic;
  48. use Lc\SovBundle\Model\User\UserInterface;
  49. use Lc\SovBundle\Translation\FlashBagTranslator;
  50. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  51. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  52. class OrderShopBuilder
  53. {
  54. protected EntityManagerInterface $entityManager;
  55. protected OrderStatusStore $orderStatusStore;
  56. protected OrderProductStore $orderProductStore;
  57. protected OrderShopStore $orderShopStore;
  58. protected OrderShopSolver $orderShopSolver;
  59. protected ProductFamilyStore $productFamilyStore;
  60. protected PriceSolver $priceSolver;
  61. protected OrderProductBuilder $orderProductBuilder;
  62. protected DocumentBuilder $documentBuilder;
  63. protected EventDispatcherInterface $eventDispatcher;
  64. protected FlashBagInterface $flashBag;
  65. protected OpeningResolver $openingResolver;
  66. protected ProductSolver $productSolver;
  67. protected OrderShopResolver $orderShopResolver;
  68. protected OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver;
  69. protected DistributionBuilder $distributionBuilder;
  70. protected MerchantResolver $merchantResolver;
  71. protected CreditHistoryBuilder $creditHistoryBuilder;
  72. protected FlashBagTranslator $flashBagTranslator;
  73. protected OrderShopFactory $orderShopFactory;
  74. protected OrderProductSolver $orderProductSolver;
  75. public function __construct(
  76. EntityManagerInterface $entityManager,
  77. OrderShopStore $orderShopStore,
  78. OrderShopSolver $orderShopSolver,
  79. OrderStatusStore $orderStatusStore,
  80. OrderProductStore $orderProductStore,
  81. ProductFamilyStore $productFamilyStore,
  82. OrderProductBuilder $orderProductBuilder,
  83. DocumentBuilder $documentBuilder,
  84. PriceSolver $priceSolver,
  85. EventDispatcherInterface $eventDispatcher,
  86. FlashBagInterface $flashBag,
  87. OpeningResolver $openingResolver,
  88. ProductSolver $productSolver,
  89. OrderShopResolver $orderShopResolver,
  90. OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver,
  91. DistributionBuilder $distributionBuilder,
  92. MerchantResolver $merchantResolver,
  93. CreditHistoryBuilder $creditHistoryBuilder,
  94. FlashBagTranslator $flashBagTranslator,
  95. OrderShopFactory $orderShopFactory,
  96. OrderProductSolver $orderProductSolver
  97. ) {
  98. $this->entityManager = $entityManager;
  99. $this->orderShopStore = $orderShopStore;
  100. $this->orderShopSolver = $orderShopSolver;
  101. $this->orderStatusStore = $orderStatusStore;
  102. $this->orderProductStore = $orderProductStore;
  103. $this->productFamilyStore = $productFamilyStore;
  104. $this->orderProductBuilder = $orderProductBuilder;
  105. $this->documentBuilder = $documentBuilder;
  106. $this->priceSolver = $priceSolver;
  107. $this->eventDispatcher = $eventDispatcher;
  108. $this->flashBag = $flashBag;
  109. $this->openingResolver = $openingResolver;
  110. $this->productSolver = $productSolver;
  111. $this->orderShopResolver = $orderShopResolver;
  112. $this->orderProductReductionCatalogSolver = $orderProductReductionCatalogSolver;
  113. $this->distributionBuilder = $distributionBuilder;
  114. $this->merchantResolver = $merchantResolver;
  115. $this->creditHistoryBuilder = $creditHistoryBuilder;
  116. $this->flashBagTranslator = $flashBagTranslator;
  117. $this->orderShopFactory = $orderShopFactory;
  118. $this->orderProductSolver = $orderProductSolver;
  119. }
  120. public function create(
  121. SectionInterface $section,
  122. UserInterface $user = null,
  123. VisitorInterface $visitor = null
  124. ): OrderShopInterface {
  125. $orderShop = $this->orderShopFactory->create($section, $user, $visitor);
  126. $this->setOrderStatus($orderShop, OrderStatusModel::ALIAS_CART);
  127. $this->entityManager->create($orderShop);
  128. $this->entityManager->flush();
  129. return $orderShop;
  130. }
  131. protected array $cacheCartCurrentBySection = [];
  132. public function createIfNotExist(
  133. SectionInterface $section,
  134. UserInterface $user = null,
  135. VisitorInterface $visitor = null,
  136. bool $cache = false
  137. ): OrderShopInterface {
  138. $cart = null;
  139. // cache
  140. $cacheIdCartCurrent = 'cart_current_' . $section->getId();
  141. if ($cache
  142. && isset($this->cacheCartCurrentBySection[$cacheIdCartCurrent])
  143. && $this->cacheCartCurrentBySection[$cacheIdCartCurrent]) {
  144. return $this->cacheCartCurrentBySection[$cacheIdCartCurrent];
  145. }
  146. $this->orderShopStore->setSection($section);
  147. $cartUser = $this->orderShopStore->getOneCartCurrent($user);
  148. $cartVisitor = $this->orderShopStore->getOneCartCurrent(null, $visitor);
  149. if ($cartUser && $cartVisitor && $cartUser->getId() != $cartVisitor->getId()) {
  150. $cart = $this->merge($cartUser, $cartVisitor);
  151. } else {
  152. if ($cartUser) {
  153. $cart = $cartUser;
  154. } elseif ($cartVisitor) {
  155. if ($user && $cartVisitor && !$cartVisitor->getUser()) {
  156. $cartVisitor->setUser($user);
  157. $this->entityManager->update($cartVisitor);
  158. $this->entityManager->flush();
  159. }
  160. $cart = $cartVisitor;
  161. }
  162. }
  163. if (!$cart) {
  164. $cart = $this->create($section, $user, $visitor);
  165. }
  166. // @TODO : obligé de faire ça sinon le panier ne se met pas à jour quand on ajoute des produits. Pourquoi ?
  167. $this->entityManager->refresh($cart);
  168. // cache
  169. $this->cacheCartCurrentBySection[$cacheIdCartCurrent] = $cart;
  170. return $cart;
  171. }
  172. public function setOrderStatus(
  173. OrderShopInterface $orderShop,
  174. string $alias,
  175. bool $forceByAdmin = false
  176. ): OrderShopInterface {
  177. $orderStatus = $this->orderStatusStore->getOneByAlias($alias);
  178. if ($orderStatus) {
  179. if ($orderShop->getOrderStatus() === null
  180. || $orderShop->getOrderStatus()->getNextStatusAllowed()->contains($orderStatus)) {
  181. $this->applyChangeOrderStatus($orderShop, $orderStatus, $forceByAdmin);
  182. }
  183. } else {
  184. throw new \ErrorException('La statut demandé n\'existe pas.');
  185. }
  186. return $orderShop;
  187. }
  188. public function applyChangeOrderStatus(
  189. OrderShopInterface $orderShop,
  190. OrderStatusInterface $orderStatus,
  191. bool $forceByAdmin = false
  192. ): void {
  193. $this->eventDispatcher->dispatch(
  194. new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
  195. OrderShopChangeStatusEvent::PRE_CHANGE_STATUS
  196. );
  197. $orderShop->setOrderStatusProtected($orderStatus);
  198. $orderStatusHistoryFactory = new OrderStatusHistoryFactory();
  199. $orderStatusHistory = $orderStatusHistoryFactory->create($orderShop, $orderStatus);
  200. $orderShop->addOrderStatusHistory($orderStatusHistory);
  201. $this->eventDispatcher->dispatch(
  202. new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
  203. OrderShopChangeStatusEvent::POST_CHANGE_STATUS
  204. );
  205. }
  206. public function addOrderProduct(
  207. OrderShopInterface $orderShop,
  208. OrderProductInterface $orderProductAdd,
  209. bool $persist = true
  210. ): bool {
  211. $return = false;
  212. if ($this->orderShopSolver->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
  213. if ($orderProductAdd->getQuantityOrder() > 0) {
  214. $updated = false;
  215. $this->orderProductBuilder->init($orderProductAdd);
  216. $this->orderProductBuilder->initReductionCatalog($orderShop->getSection(), $orderProductAdd);
  217. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  218. if ($this->orderProductSolver->compare($orderProduct, $orderProductAdd)) {
  219. $orderProduct->setQuantityOrder(
  220. $orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
  221. );
  222. if ($persist) {
  223. $this->entityManager->update($orderProduct);
  224. }
  225. $updated = true;
  226. $return = true;
  227. break;
  228. }
  229. }
  230. if (!$updated) {
  231. $orderShop->addOrderProduct($orderProductAdd);
  232. if ($persist) {
  233. if (isset($orderProductReductionCatalog)) {
  234. $this->entityManager->create($orderProductReductionCatalog);
  235. }
  236. // @TODO : update ou create ?
  237. $this->entityManager->persist($orderProductAdd);
  238. $this->entityManager->update($orderShop);
  239. }
  240. $return = true;
  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. $return = false;
  269. }
  270. return $return;
  271. }
  272. public function merge(
  273. OrderShopInterface $orderShop1,
  274. OrderShopInterface $orderShop2,
  275. $persist = true
  276. ): OrderShopInterface {
  277. //TODO essayer de comprendre prk on doit faire un refresh ici ???
  278. $this->entityManager->refresh($orderShop1);
  279. $this->entityManager->refresh($orderShop2);
  280. if ($orderShop1 && $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. }
  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)
  449. {
  450. $debug = '';
  451. foreach ($this->merchantResolver->getCurrent()->getSections() as $section) {
  452. // @TODO : faire la vérification isOpenSale depuis la méthode appelante
  453. if (!$this->openingResolver->isOpenSale($section)) {
  454. $countOrderProductUpdated = 0;
  455. foreach ($productFamily->getProducts() as $product) {
  456. $orderProducts = $this->orderProductStore->resetContext()->setSection(
  457. $section
  458. )->getInCartsByProduct($product);
  459. foreach ($orderProducts as $orderProduct) {
  460. $quantityOrder = $orderProduct->getQuantityOrder();
  461. $orderShop = $orderProduct->getOrderShop();
  462. $orderShop->removeOrderProduct($orderProduct);
  463. $this->entityManager->delete($orderProduct);
  464. $this->entityManager->flush();
  465. $this->entityManager->refresh($orderShop);
  466. $orderProductFactory = new OrderProductFactory();
  467. $addOrderProduct = $orderProductFactory->create($product, $quantityOrder);
  468. $this->addOrderProduct($orderShop, $addOrderProduct);
  469. $countOrderProductUpdated++;
  470. }
  471. }
  472. if ($countOrderProductUpdated) {
  473. $this->flashBagTranslator->add(
  474. 'success',
  475. 'orderProductUpdated',
  476. 'OrderShop',
  477. //array('%count%' => $countOrderProductUpdated)
  478. ['%count' => $debug]
  479. );
  480. $this->entityManager->flush();
  481. }
  482. return $countOrderProductUpdated;
  483. }
  484. }
  485. }
  486. public function setStatsInfo(OrderShopInterface $orderShop, $flush = true)
  487. {
  488. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  489. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  490. $orderShop->setStatTotalOrderProductsWithReductions(
  491. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  492. );
  493. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  494. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  495. );
  496. $orderShop->setStatMarginOrderProductsWithReductions(
  497. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  498. );
  499. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  500. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  501. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  502. );
  503. $this->entityManager->update($orderShop);
  504. if ($flush) {
  505. $this->entityManager->flush();
  506. }
  507. }
  508. public function setHasReach(int $reachStep, OrderShopInterface $orderShop)
  509. {
  510. if ($orderShop->getHasReach() === null || $orderShop->getHasReach() < $reachStep) {
  511. $orderShop->setHasReach($reachStep);
  512. $this->entityManager->persist($orderShop);
  513. $this->entityManager->flush($orderShop);
  514. }
  515. }
  516. public function initComplementaryOrderShop(OrderShopInterface $orderShop, OrderShopInterface $mainOrderShop): void
  517. {
  518. $orderShop->setMainOrderShop($mainOrderShop);
  519. $orderShop->setDeliveryPrice(0);
  520. if ($mainOrderShop->getDeliveryAddress()) {
  521. $this->initDeliveryAddress($orderShop, $mainOrderShop->getDeliveryAddress());
  522. }
  523. $orderShop->setInvoiceAddress($mainOrderShop->getInvoiceAddress());
  524. }
  525. // setDeliveryAddress
  526. public function initDeliveryAddress(OrderShopInterface $orderShop, AddressInterface $address = null): void
  527. {
  528. $orderShop->setDeliveryAddress($address);
  529. $orderShop->setDeliveryInfos($address ? $address->getDeliveryInfos() : null);
  530. }
  531. // resetOrderShopInfos
  532. public function reset(OrderShopInterface $orderShop)
  533. {
  534. $this->initDeliveryAddress($orderShop, null);
  535. $orderShop->setMainOrderShop(null);
  536. $orderShop->setDeliveryPrice(null);
  537. $orderShop->setInvoiceAddress(null);
  538. $orderShop->setDeclineComplementaryOrderShop(false);
  539. }
  540. public function getProductsSalesStatistic(SectionInterface $section, $entity, $nbWeek = 2)
  541. {
  542. $productsSalesStatistic = new ProductsSalesStatistic(
  543. $this->entityManager,
  544. $entity,
  545. $nbWeek,
  546. $this->productSolver
  547. );
  548. $productsSalesStatistic->init($section, $this->distributionBuilder);
  549. $productsSalesStatistic->populateProperties($this->orderShopStore->resetContext()->setSection($section));
  550. return $productsSalesStatistic->getAsArray();
  551. }
  552. }