Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

OrderShopBuilder.php 27KB

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