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.

679 lines
28KB

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