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 line
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 pourquoi on doit faire un refresh ici
  278. $this->entityManager->refresh($orderShop1);
  279. $this->entityManager->refresh($orderShop2);
  280. foreach ($orderShop2->getOrderProducts() as $orderProduct) {
  281. $orderProductAlreadyInCart = $this->orderShopSolver->hasOrderProductAlreadyInCart(
  282. $orderShop1,
  283. $orderProduct
  284. );
  285. if ($orderProductAlreadyInCart) {
  286. if ($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) {
  287. $orderShop1->removeOrderProduct($orderProductAlreadyInCart);
  288. $this->addOrderProduct($orderShop1, $orderProduct);
  289. }
  290. } else {
  291. $this->addOrderProduct($orderShop1, $orderProduct);
  292. }
  293. if ($persist) {
  294. $this->entityManager->delete($orderProduct);
  295. }
  296. }
  297. if ($persist) {
  298. $this->entityManager->delete($orderShop2);
  299. $this->entityManager->update($orderShop1);
  300. $this->entityManager->flush();
  301. }
  302. return $orderShop1;
  303. }
  304. public function addPayment(OrderShopInterface $orderShop, string $meanPayment, float $amount): OrderShopInterface
  305. {
  306. $orderPaymentFactory = new OrderPaymentFactory();
  307. $orderPayment = $orderPaymentFactory->create($orderShop, $meanPayment, $amount);
  308. $orderShop->addOrderPayment($orderPayment);
  309. if ($meanPayment == OrderPaymentModel::MEAN_PAYMENT_CREDIT) {
  310. $this->creditHistoryBuilder->create(
  311. CreditHistoryModel::TYPE_DEBIT,
  312. $this->merchantResolver->getUserMerchant(),
  313. [
  314. 'orderPayment' => $orderPayment
  315. ]
  316. );
  317. }
  318. if ($this->orderShopResolver->isPaid($orderShop)) {
  319. $nextStatus = OrderStatusModel::ALIAS_PAID;
  320. } else {
  321. $nextStatus = OrderStatusModel::ALIAS_PARTIAL_PAYMENT;
  322. }
  323. if ($orderShop->getOrderStatus()->getAlias() != $nextStatus) {
  324. $this->changeOrderStatus($orderShop, $nextStatus);
  325. }
  326. $this->entityManager->create($orderPayment);
  327. $this->entityManager->update($orderShop);
  328. $this->entityManager->flush();
  329. return $orderShop;
  330. }
  331. public function initStatsInfo(OrderShopInterface $orderShop, $flush = true)
  332. {
  333. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  334. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  335. $orderShop->setStatTotalOrderProductsWithReductions(
  336. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  337. );
  338. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  339. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  340. );
  341. $orderShop->setStatMarginOrderProductsWithReductions(
  342. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  343. );
  344. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  345. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  346. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  347. );
  348. $this->entityManager->persist($orderShop);
  349. if ($flush) {
  350. $this->entityManager->flush();
  351. }
  352. }
  353. //initCycleNumber
  354. public function initDistribution(OrderShopInterface $orderShop): void
  355. {
  356. $distribution = $this->distributionBuilder->guessDistributionByDeliveryDate(
  357. $orderShop->getDeliveryDate(),
  358. $orderShop->getSection()
  359. );
  360. $orderShop->setDistribution($distribution);
  361. }
  362. public function createDocumentInvoice(OrderShopInterface $orderShop): DocumentInterface
  363. {
  364. $documentFactory = new DocumentFactory();
  365. $document = $documentFactory->create($orderShop->getSection()->getMerchant(), DocumentModel::TYPE_INVOICE);
  366. $this->documentBuilder->initFromOrderShop($document, $orderShop);
  367. return $document;
  368. }
  369. public function addReductionCart(
  370. OrderShopInterface $orderShop,
  371. ReductionCartInterface $reductionCart
  372. ): ?OrderReductionCartInterface {
  373. $orderReductionCartFactory = new OrderReductionCartFactory();
  374. $orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart);
  375. $orderShop->addOrderReductionCart($orderReductionCart);
  376. if ($this->orderShopResolver->isPositiveAmount($orderShop)
  377. && $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
  378. $this->entityManager->create($orderReductionCart);
  379. $this->entityManager->flush();
  380. return $orderReductionCart;
  381. } else {
  382. //TODO vérifier ce case ! Avec le null en valeur de retour
  383. $orderShop->removeOrderReductionCart($orderReductionCart);
  384. return null;
  385. }
  386. }
  387. // createOrderReductionCredit
  388. public function addReductionCredit(
  389. OrderShopInterface $orderShop,
  390. ReductionCreditInterface $reductionCredit
  391. ): ?OrderReductionCreditInterface {
  392. $orderReductionCreditFactory = new OrderReductionCreditFactory();
  393. $orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit);
  394. $orderShop->addOrderReductionCredit($orderReductionCredit);
  395. if ($this->orderShopResolver->isPositiveAmount($orderShop)
  396. && $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
  397. $this->entityManager->create($orderReductionCredit);
  398. $this->entityManager->flush();
  399. return $orderReductionCredit;
  400. } else {
  401. $orderShop->removeOrderReductionCredit($orderReductionCredit);
  402. return null;
  403. }
  404. }
  405. public function deductAvailabilityProduct(OrderShopInterface $orderShop): void
  406. {
  407. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  408. $this->applyDeductAvailabilityProduct($orderShop, $orderProduct->getProduct(), $orderProduct->getQuantityOrder());
  409. }
  410. }
  411. public function applyDeductAvailabilityProduct(
  412. OrderShopInterface $orderShop,
  413. ProductInterface $product,
  414. int $quantityOrder
  415. ): void {
  416. switch ($product->getProductFamily()->getBehaviorCountStock()) {
  417. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  418. //Disponibilité par unité de référence
  419. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($product);
  420. $newAvailability = $oldAvailability - ($quantityOrder * ($this->productSolver->getQuantityInherited(
  421. $product
  422. ) / $this->productSolver->getUnitInherited($product)->getCoefficient()));
  423. $productFamily = $product->getProductFamily();
  424. $productFamily->setAvailableQuantity($newAvailability);
  425. $productFamily->setUpdatedBy($orderShop->getUser());
  426. $this->entityManager->update($productFamily);
  427. break;
  428. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  429. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($product);
  430. $newAvailability = $oldAvailability - $quantityOrder;
  431. $productFamily = $product->getProductFamily();
  432. $productFamily->setAvailableQuantity($newAvailability);
  433. $productFamily->setUpdatedBy($orderShop->getUser());
  434. $this->entityManager->update($productFamily);
  435. break;
  436. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  437. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($product);
  438. $newAvailability = $oldAvailability - $quantityOrder;
  439. $product->setAvailableQuantity($newAvailability);
  440. $product->setUpdatedBy($orderShop->getUser());
  441. $this->entityManager->update($product);
  442. break;
  443. }
  444. $this->entityManager->flush();
  445. }
  446. public function updatePriceByProductFamily(ProductFamilyInterface $productFamily)
  447. {
  448. $debug = '';
  449. foreach ($this->merchantResolver->getCurrent()->getSections() as $section) {
  450. // @TODO : faire la vérification isOpenSale depuis la méthode appelante
  451. if (!$this->openingResolver->isOpenSale($section)) {
  452. $countOrderProductUpdated = 0;
  453. foreach ($productFamily->getProducts() as $product) {
  454. $orderProducts = $this->orderProductStore->resetContext()->setSection(
  455. $section
  456. )->getInCartsByProduct($product);
  457. foreach ($orderProducts as $orderProduct) {
  458. $quantityOrder = $orderProduct->getQuantityOrder();
  459. $orderShop = $orderProduct->getOrderShop();
  460. $orderShop->removeOrderProduct($orderProduct);
  461. $this->entityManager->delete($orderProduct);
  462. $this->entityManager->flush();
  463. $this->entityManager->refresh($orderShop);
  464. $orderProductFactory = new OrderProductFactory();
  465. $addOrderProduct = $orderProductFactory->create($product, $quantityOrder);
  466. $this->addOrderProduct($orderShop, $addOrderProduct);
  467. $countOrderProductUpdated++;
  468. }
  469. }
  470. if ($countOrderProductUpdated) {
  471. $this->flashBagTranslator->add(
  472. 'success',
  473. 'orderProductUpdated',
  474. 'OrderShop',
  475. //array('%count%' => $countOrderProductUpdated)
  476. ['%count' => $debug]
  477. );
  478. $this->entityManager->flush();
  479. }
  480. return $countOrderProductUpdated;
  481. }
  482. }
  483. }
  484. public function setStatsInfo(OrderShopInterface $orderShop, $flush = true)
  485. {
  486. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  487. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  488. $orderShop->setStatTotalOrderProductsWithReductions(
  489. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  490. );
  491. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  492. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  493. );
  494. $orderShop->setStatMarginOrderProductsWithReductions(
  495. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  496. );
  497. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  498. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  499. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  500. );
  501. $this->entityManager->update($orderShop);
  502. if ($flush) {
  503. $this->entityManager->flush();
  504. }
  505. }
  506. public function setHasReach(int $reachStep, OrderShopInterface $orderShop)
  507. {
  508. if ($orderShop->getHasReach() === null || $orderShop->getHasReach() < $reachStep) {
  509. $orderShop->setHasReach($reachStep);
  510. $this->entityManager->persist($orderShop);
  511. $this->entityManager->flush($orderShop);
  512. }
  513. }
  514. public function initComplementaryOrderShop(OrderShopInterface $orderShop, OrderShopInterface $mainOrderShop): void
  515. {
  516. $orderShop->setMainOrderShop($mainOrderShop);
  517. $orderShop->setDeliveryPrice(0);
  518. if ($mainOrderShop->getDeliveryAddress()) {
  519. $this->initDeliveryAddress($orderShop, $mainOrderShop->getDeliveryAddress());
  520. }
  521. $orderShop->setInvoiceAddress($mainOrderShop->getInvoiceAddress());
  522. }
  523. // setDeliveryAddress
  524. public function initDeliveryAddress(OrderShopInterface $orderShop, AddressInterface $address = null): void
  525. {
  526. $orderShop->setDeliveryAddress($address);
  527. $orderShop->setDeliveryInfos($address ? $address->getDeliveryInfos() : null);
  528. }
  529. // resetOrderShopInfos
  530. public function reset(OrderShopInterface $orderShop)
  531. {
  532. $this->initDeliveryAddress($orderShop, null);
  533. $orderShop->setMainOrderShop(null);
  534. $orderShop->setDeliveryPrice(null);
  535. $orderShop->setInvoiceAddress(null);
  536. $orderShop->setDeclineComplementaryOrderShop(false);
  537. }
  538. public function getProductsSalesStatistic(SectionInterface $section, $entity, $nbWeek = 2)
  539. {
  540. $productsSalesStatistic = new ProductsSalesStatistic(
  541. $this->entityManager,
  542. $entity,
  543. $nbWeek,
  544. $this->productSolver
  545. );
  546. $productsSalesStatistic->init($section, $this->distributionBuilder);
  547. $productsSalesStatistic->populateProperties($this->orderShopStore->resetContext()->setSection($section));
  548. return $productsSalesStatistic->getAsArray();
  549. }
  550. }