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.

638 line
26KB

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