Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

673 Zeilen
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\OrderShopChangeStatusEvent;
  8. use Lc\CaracoleBundle\Factory\Credit\CreditHistoryFactory;
  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\Section\SectionModel;
  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\OrderShopSolver;
  44. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  45. use Lc\CaracoleBundle\Solver\Product\ProductSolver;
  46. use Lc\CaracoleBundle\Statistic\Product\ProductsSalesStatistic;
  47. use Lc\SovBundle\Model\User\UserInterface;
  48. use Lc\SovBundle\Translation\FlashBagTranslator;
  49. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  50. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  51. use Symfony\Contracts\Cache\CacheInterface;
  52. use Symfony\Contracts\Cache\ItemInterface;
  53. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  54. class OrderShopBuilder
  55. {
  56. protected EntityManagerInterface $entityManager;
  57. protected OrderStatusStore $orderStatusStore;
  58. protected OrderProductStore $orderProductStore;
  59. protected OrderShopStore $orderShopStore;
  60. protected OrderShopSolver $orderShopSolver;
  61. protected ProductFamilyStore $productFamilyStore;
  62. protected PriceSolver $priceSolver;
  63. protected OrderProductBuilder $orderProductBuilder;
  64. protected DocumentBuilder $documentBuilder;
  65. protected EventDispatcherInterface $eventDispatcher;
  66. protected FlashBagInterface $flashBag;
  67. protected OpeningResolver $openingResolver;
  68. protected ProductSolver $productSolver;
  69. protected OrderShopResolver $orderShopResolver;
  70. protected OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver;
  71. protected DistributionBuilder $distributionBuilder;
  72. protected MerchantResolver $merchantResolver;
  73. protected CreditHistoryBuilder $creditHistoryBuilder;
  74. public function __construct(
  75. EntityManagerInterface $entityManager,
  76. OrderShopStore $orderShopStore,
  77. OrderShopSolver $orderShopSolver,
  78. OrderStatusStore $orderStatusStore,
  79. OrderProductStore $orderProductStore,
  80. ProductFamilyStore $productFamilyStore,
  81. OrderProductBuilder $orderProductBuilder,
  82. DocumentBuilder $documentBuilder,
  83. PriceSolver $priceSolver,
  84. EventDispatcherInterface $eventDispatcher,
  85. FlashBagInterface $flashBag,
  86. OpeningResolver $openingResolver,
  87. ProductSolver $productSolver,
  88. OrderShopResolver $orderShopResolver,
  89. OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver,
  90. DistributionBuilder $distributionBuilder,
  91. MerchantResolver $merchantResolver,
  92. CreditHistoryBuilder $creditHistoryBuilder
  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. }
  113. public function create(
  114. SectionInterface $section,
  115. UserInterface $user = null,
  116. VisitorInterface $visitor = null
  117. ): OrderShopInterface {
  118. $orderShopFactory = new OrderShopFactory();
  119. $orderShop = $orderShopFactory->create($section, $user, $visitor);
  120. $this->setOrderStatus($orderShop, OrderStatusModel::ALIAS_CART);
  121. $this->entityManager->create($orderShop);
  122. $this->entityManager->flush();
  123. return $orderShop;
  124. }
  125. protected array $cacheCartCurrentBySection = [];
  126. public function createIfNotExist(
  127. SectionInterface $section,
  128. UserInterface $user = null,
  129. VisitorInterface $visitor = null,
  130. bool $cache = false
  131. ): OrderShopInterface {
  132. $cart = null;
  133. // cache
  134. $cacheIdCartCurrent = 'cart_current_'.$section->getId();
  135. if($cache
  136. && isset($this->cacheCartCurrentBySection[$cacheIdCartCurrent])
  137. && $this->cacheCartCurrentBySection[$cacheIdCartCurrent]) {
  138. return $this->cacheCartCurrentBySection[$cacheIdCartCurrent];
  139. }
  140. $this->orderShopStore->setSection($section);
  141. $cartUser = $this->orderShopStore->getOneCartCurrent($user);
  142. $cartVisitor = $this->orderShopStore->getOneCartCurrent(null, $visitor);
  143. if ($cartUser && $cartVisitor && $cartUser->getId() != $cartVisitor->getId()) {
  144. $cart = $this->merge($cartUser, $cartVisitor);
  145. } else {
  146. if($cartUser) {
  147. $cart = $cartUser;
  148. }
  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) {
  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 est-ce un update ou un create ???
  254. $this->entityManager->persist($orderProductAdd);
  255. $this->entityManager->update($orderShop);
  256. }
  257. $return = true;
  258. }
  259. if ($persist) {
  260. $this->entityManager->flush();
  261. }
  262. // @TODO : dispatch event cart change
  263. //$this->eventCartChange($orderShop);
  264. }
  265. } else {
  266. // @TODO : retourner le message d'erreur et faire le addFlash dans le contrôleur
  267. /*$availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited();
  268. $textError = "Le produit <strong>" . $orderProductAdd->getTitleOrderShop(
  269. ) . "</strong> n'est pas disponible";
  270. if ($availableQuantity !== false && $availableQuantity > 0) {
  271. $unit = '';
  272. if ($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock(
  273. ) == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  274. $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnitReference()->getUnit();
  275. }
  276. $textError .= ' dans cette quantité ';
  277. $user = $this->security->getUser();
  278. if ($user && $user->hasRole('ROLE_USER')) {
  279. $textError .= '<br />' . $availableQuantity . $unit . ' disponible(s) dont ' . $this->getQuantityOrderByProduct(
  280. $orderShop,
  281. $orderProductAdd->getProduct()
  282. ) . $unit . ' déjà dans votre panier.';
  283. }
  284. }
  285. $this->utils->addFlash('error', $textError);*/
  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($orderShop1, $orderProduct);
  300. if ($orderProductAlreadyInCart) {
  301. if ($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) {
  302. $orderShop1->removeOrderProduct($orderProductAlreadyInCart);
  303. $this->addOrderProduct($orderShop1, $orderProduct);
  304. }
  305. } else {
  306. $this->addOrderProduct($orderShop1, $orderProduct);
  307. }
  308. if ($persist) {
  309. $this->entityManager->delete($orderProduct);
  310. }
  311. }
  312. if ($persist) {
  313. $this->entityManager->delete($orderShop2);
  314. $this->entityManager->update($orderShop1);
  315. $this->entityManager->flush();
  316. }
  317. return $orderShop1;
  318. }
  319. }
  320. public function addPayment(OrderShopInterface $orderShop, string $meanPayment, float $amount): OrderShopInterface
  321. {
  322. $orderPaymentFactory = new OrderPaymentFactory();
  323. $orderPayment = $orderPaymentFactory->create($orderShop, $meanPayment, $amount);
  324. $orderShop->addOrderPayment($orderPayment);
  325. if($meanPayment == OrderPaymentModel::MEAN_PAYMENT_CREDIT) {
  326. $this->creditHistoryBuilder->create(CreditHistoryModel::TYPE_DEBIT, $this->merchantResolver->getUserMerchant(), [
  327. 'orderPayment' => $orderPayment
  328. ]);
  329. }
  330. if ($this->orderShopResolver->isPaid($orderShop)) {
  331. $nextStatus = OrderStatusModel::ALIAS_PAID;
  332. } else {
  333. $nextStatus = OrderStatusModel::ALIAS_PARTIAL_PAYMENT;
  334. }
  335. if ($orderShop->getOrderStatus()->getAlias() != $nextStatus) {
  336. $this->changeOrderStatus($orderShop, $nextStatus);
  337. }
  338. $this->entityManager->create($orderPayment);
  339. $this->entityManager->update($orderShop);
  340. $this->entityManager->flush();
  341. return $orderShop;
  342. }
  343. public function initStatsInfo(OrderShopInterface $orderShop, $flush = true)
  344. {
  345. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  346. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  347. $orderShop->setStatTotalOrderProductsWithReductions(
  348. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  349. );
  350. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  351. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  352. );
  353. $orderShop->setStatMarginOrderProductsWithReductions(
  354. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  355. );
  356. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  357. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  358. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  359. );
  360. $this->entityManager->persist($orderShop);
  361. if ($flush) {
  362. $this->entityManager->flush();
  363. }
  364. }
  365. //initCycleNumber
  366. public function initDistribution(OrderShopInterface $orderShop): void
  367. {
  368. $distribution = $this->distributionBuilder->guessDistributionByDeliveryDate(
  369. $orderShop->getDeliveryDate(),
  370. $orderShop->getSection()
  371. );
  372. $orderShop->setDistribution($distribution);
  373. }
  374. public function createDocumentInvoice(OrderShopInterface $orderShop): DocumentInterface
  375. {
  376. $documentFactory = new DocumentFactory();
  377. $document = $documentFactory->create($orderShop->getSection()->getMerchant(), DocumentModel::TYPE_INVOICE);
  378. $this->documentBuilder->initFromOrderShop($document, $orderShop);
  379. return $document;
  380. }
  381. public function addReductionCart(
  382. OrderShopInterface $orderShop,
  383. ReductionCartInterface $reductionCart
  384. ): ?OrderReductionCartInterface {
  385. $orderReductionCartFactory = new OrderReductionCartFactory();
  386. $orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart);
  387. $orderShop->addOrderReductionCart($orderReductionCart);
  388. if ($this->orderShopResolver->isPositiveAmount($orderShop)
  389. && $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
  390. $this->entityManager->create($orderReductionCart);
  391. $this->entityManager->flush();
  392. return $orderReductionCart;
  393. } else {
  394. //TODO vérifier ce case ! Avec le null en valeur de retour
  395. $orderShop->removeOrderReductionCart($orderReductionCart);
  396. return null;
  397. }
  398. }
  399. // createOrderReductionCredit
  400. public function addReductionCredit(
  401. OrderShopInterface $orderShop,
  402. ReductionCreditInterface $reductionCredit
  403. ): ?OrderReductionCreditInterface {
  404. $orderReductionCreditFactory = new OrderReductionCreditFactory();
  405. $orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit);
  406. $orderShop->addOrderReductionCredit($orderReductionCredit);
  407. if ($this->orderShopResolver->isPositiveAmount($orderShop)
  408. && $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
  409. $this->entityManager->create($orderReductionCredit);
  410. $this->entityManager->flush();
  411. return $orderReductionCredit;
  412. } else {
  413. $orderShop->removeOrderReductionCredit($orderReductionCredit);
  414. return null;
  415. }
  416. }
  417. public function deductAvailabilityProduct(OrderShopInterface $orderShop): void
  418. {
  419. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  420. $this->applyDeductAvailabilityProduct($orderShop, $orderProduct);
  421. }
  422. }
  423. public function applyDeductAvailabilityProduct(
  424. OrderShopInterface $orderShop,
  425. OrderProductInterface $orderProduct
  426. ): void {
  427. switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
  428. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  429. //Disponibilité par unité de référence
  430. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($orderProduct->getProduct());
  431. $newAvailability = $oldAvailability - ($orderProduct->getQuantityOrder(
  432. ) * ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()));
  433. $productFamily = $orderProduct->getProduct()->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_FAMILY :
  439. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($orderProduct->getProduct());
  440. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  441. $productFamily = $orderProduct->getProduct()->getProductFamily();
  442. $productFamily->setAvailableQuantity($newAvailability);
  443. $productFamily->setUpdatedBy($orderShop->getUser());
  444. $this->entityManager->update($productFamily);
  445. break;
  446. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  447. $oldAvailability = $this->productSolver->getAvailableQuantityInherited($orderProduct->getProduct());
  448. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  449. $product = $orderProduct->getProduct();
  450. $product->setAvailableQuantity($newAvailability);
  451. $product->setUpdatedBy($orderShop->getUser());
  452. $this->entityManager->update($product);
  453. break;
  454. }
  455. $this->entityManager->flush();
  456. }
  457. public function updatePriceByProductFamily(ProductFamilyInterface $productFamily)
  458. {
  459. // @TODO : faire la vérification isOpenSale depuis la méthode appelante
  460. if (!$this->openingResolver->isOpenSale(
  461. $productFamily->getSection(),
  462. null,
  463. OpeningResolver::OPENING_CONTEXT_BACKEND
  464. )) {
  465. $countOrderProductUpdated = 0;
  466. foreach ($productFamily->getProducts() as $product) {
  467. $orderProducts = $this->orderProductStore->getInCartsByProduct($product);
  468. foreach ($orderProducts as $orderProduct) {
  469. $quantityOrder = $orderProduct->getQuantityOrder();
  470. $orderShop = $orderProduct->getOrderShop();
  471. $orderShop->removeOrderProduct($orderProduct);
  472. $this->entityManager->delete($orderProduct);
  473. $this->entityManager->flush();
  474. $this->entityManager->refresh($orderShop);
  475. $orderProductFactory = new OrderProductFactory();
  476. $addOrderProduct = $orderProductFactory->create($product, $quantityOrder);
  477. $this->addOrderProduct($orderShop, $addOrderProduct);
  478. $countOrderProductUpdated++;
  479. }
  480. }
  481. if ($countOrderProductUpdated) {
  482. // @TODO : faire le add flash dans le controller
  483. /*$this->utils->addFlash(
  484. 'success',
  485. 'success.OrderShop.orderProductUpdated',
  486. array(),
  487. array('%count%' => $countOrderProductUpdated)
  488. );*/
  489. $this->entityManager->flush();
  490. }
  491. return $countOrderProductUpdated;
  492. }
  493. }
  494. public function setStatsInfo(OrderShopInterface $orderShop, $flush = true)
  495. {
  496. $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
  497. $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
  498. $orderShop->setStatTotalOrderProductsWithReductions(
  499. $this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
  500. );
  501. $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
  502. $this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
  503. );
  504. $orderShop->setStatMarginOrderProductsWithReductions(
  505. $this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
  506. );
  507. $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
  508. $orderShop->setStatDeliveryPriceWithTaxAndReduction(
  509. $this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
  510. );
  511. $this->entityManager->update($orderShop);
  512. if ($flush) {
  513. $this->entityManager->flush();
  514. }
  515. }
  516. public function setHasReach(int $reachStep, OrderShopInterface $orderShop)
  517. {
  518. if ($orderShop->getHasReach() === null || $orderShop->getHasReach() < $reachStep) {
  519. $orderShop->setHasReach($reachStep);
  520. $this->entityManager->persist($orderShop);
  521. $this->entityManager->flush($orderShop);
  522. }
  523. }
  524. public function initComplementaryOrderShop(OrderShopInterface $orderShop, OrderShopInterface $mainOrderShop): void
  525. {
  526. $orderShop->setMainOrderShop($mainOrderShop);
  527. $orderShop->setDeliveryPrice(0);
  528. if ($mainOrderShop->getDeliveryAddress()) {
  529. $this->initDeliveryAddress($orderShop, $mainOrderShop->getDeliveryAddress());
  530. }
  531. $orderShop->setInvoiceAddress($mainOrderShop->getInvoiceAddress());
  532. }
  533. // setDeliveryAddress
  534. public function initDeliveryAddress(OrderShopInterface $orderShop, AddressInterface $address = null): void
  535. {
  536. $orderShop->setDeliveryAddress($address);
  537. $orderShop->setDeliveryInfos($address ? $address->getDeliveryInfos() : null);
  538. }
  539. // resetOrderShopInfos
  540. public function reset(OrderShopInterface $orderShop)
  541. {
  542. $this->initDeliveryAddress($orderShop, null);
  543. $orderShop->setMainOrderShop(null);
  544. $orderShop->setDeliveryPrice(null);
  545. $orderShop->setInvoiceAddress(null);
  546. $orderShop->setDeclineComplementaryOrderShop(false);
  547. }
  548. public function getProductsSalesStatistic(SectionInterface $section, $entity, $nbWeek = 2)
  549. {
  550. $productsSalesStatistic = new ProductsSalesStatistic(
  551. $this->entityManager,
  552. $entity,
  553. $nbWeek,
  554. $this->productSolver
  555. );
  556. $productsSalesStatistic->init($section, $this->distributionBuilder, $this->openingResolver);
  557. $productsSalesStatistic->populateProperties($this->orderShopStore);
  558. return $productsSalesStatistic->getAsArray();
  559. }
  560. }