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