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.

663 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. $orderProductAdd->setOrderShop($orderShop);
  213. if ($this->orderShopSolver->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
  214. if ($orderProductAdd->getQuantityOrder() > 0) {
  215. $updated = false;
  216. $this->orderProductBuilder->init($orderProductAdd);
  217. $this->orderProductBuilder->initReductionCatalog($orderShop->getSection(), $orderProductAdd);
  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)
  448. {
  449. $debug = '';
  450. foreach ($this->merchantResolver->getCurrent()->getSections() as $section) {
  451. // @TODO : faire la vérification isOpenSale depuis la méthode appelante
  452. if (!$this->openingResolver->isOpenSale($section)) {
  453. $countOrderProductUpdated = 0;
  454. foreach ($productFamily->getProducts() as $product) {
  455. $orderProducts = $this->orderProductStore->resetContext()->setSection(
  456. $section
  457. )->getInCartsByProduct($product);
  458. foreach ($orderProducts as $orderProduct) {
  459. $quantityOrder = $orderProduct->getQuantityOrder();
  460. $orderShop = $orderProduct->getOrderShop();
  461. $orderShop->removeOrderProduct($orderProduct);
  462. $this->entityManager->delete($orderProduct);
  463. $this->entityManager->flush();
  464. $this->entityManager->refresh($orderShop);
  465. $orderProductFactory = new OrderProductFactory();
  466. $addOrderProduct = $orderProductFactory->create($product, $quantityOrder);
  467. $this->addOrderProduct($orderShop, $addOrderProduct);
  468. $countOrderProductUpdated++;
  469. }
  470. }
  471. if ($countOrderProductUpdated) {
  472. $this->flashBagTranslator->add(
  473. 'success',
  474. 'orderProductUpdated',
  475. 'OrderShop',
  476. //array('%count%' => $countOrderProductUpdated)
  477. ['%count' => $debug]
  478. );
  479. $this->entityManager->flush();
  480. }
  481. return $countOrderProductUpdated;
  482. }
  483. }
  484. }
  485. public function setStatsInfo(OrderShopInterface $orderShop, $flush = true)
  486. {
  487. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  488. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  489. $orderShop->setStatTotalOrderProductsWithReductions(
  490. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  491. );
  492. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  493. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  494. );
  495. $orderShop->setStatMarginOrderProductsWithReductions(
  496. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  497. );
  498. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  499. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  500. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  501. );
  502. $this->entityManager->update($orderShop);
  503. if ($flush) {
  504. $this->entityManager->flush();
  505. }
  506. }
  507. public function setHasReach(int $reachStep, OrderShopInterface $orderShop)
  508. {
  509. if ($orderShop->getHasReach() === null || $orderShop->getHasReach() < $reachStep) {
  510. $orderShop->setHasReach($reachStep);
  511. $this->entityManager->persist($orderShop);
  512. $this->entityManager->flush($orderShop);
  513. }
  514. }
  515. public function initComplementaryOrderShop(OrderShopInterface $orderShop, OrderShopInterface $mainOrderShop): void
  516. {
  517. $orderShop->setMainOrderShop($mainOrderShop);
  518. $orderShop->setDeliveryPrice(0);
  519. if ($mainOrderShop->getDeliveryAddress()) {
  520. $this->initDeliveryAddress($orderShop, $mainOrderShop->getDeliveryAddress());
  521. }
  522. $orderShop->setInvoiceAddress($mainOrderShop->getInvoiceAddress());
  523. }
  524. // setDeliveryAddress
  525. public function initDeliveryAddress(OrderShopInterface $orderShop, AddressInterface $address = null): void
  526. {
  527. $orderShop->setDeliveryAddress($address);
  528. $orderShop->setDeliveryInfos($address ? $address->getDeliveryInfos() : null);
  529. }
  530. // resetOrderShopInfos
  531. public function reset(OrderShopInterface $orderShop)
  532. {
  533. $this->initDeliveryAddress($orderShop, null);
  534. $orderShop->setMainOrderShop(null);
  535. $orderShop->setDeliveryPrice(null);
  536. $orderShop->setInvoiceAddress(null);
  537. $orderShop->setDeclineComplementaryOrderShop(false);
  538. }
  539. public function getProductsSalesStatistic(SectionInterface $section, $entity, $nbWeek = 2)
  540. {
  541. $productsSalesStatistic = new ProductsSalesStatistic(
  542. $this->entityManager,
  543. $entity,
  544. $nbWeek,
  545. $this->productSolver
  546. );
  547. $productsSalesStatistic->init($section, $this->distributionBuilder);
  548. $productsSalesStatistic->populateProperties($this->orderShopStore->resetContext()->setSection($section));
  549. return $productsSalesStatistic->getAsArray();
  550. }
  551. }