entityManager = $entityManager; $this->orderShopStore = $orderShopStore; $this->orderStatusStore = $orderStatusStore; $this->orderProductStore = $orderProductStore; $this->productFamilyStore = $productFamilyStore; $this->orderProductBuilder = $orderProductBuilder; $this->documentBuilder = $documentBuilder; $this->priceResolver = $priceResolver; } public function create( SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null ) { $orderShopFactory = new OrderShopFactory(); $orderShop = $orderShopFactory->create($section, $user, $visitor); $this->changeOrderStatus($orderShop, OrderStatusModel::ALIAS_CART); return $orderShop; } public function createIfNotExist( SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null ) { $cart = $this->orderShopStore->getCartBy( [ 'section' => $section, 'user' => $user, 'visitor' => $visitor ] ); if (!$cart) { $cart = $this->create($section, $user, $visitor); } return $cart; } public function changeOrderStatus(OrderShopInterface $orderShop, string $alias) { $orderStatus = $this->orderStatusStore->getRepositoryQuery()->findOneByAlias($alias); if ($orderShop->getOrderStatus() === null || $orderShop->getOrderStatus()->getNextStatusAllowed()->contains($orderStatus)) { $this->eventDispatcher->dispatch( new OrderShopChangeStatusEvent($orderShop, $orderStatus), OrderShopChangeStatusEvent::PRE_CHANGE_STATUS ); $orderShop->setOrderStatusProtected($orderStatus); $orderStatusHistoryFactory = new OrderStatusHistoryFactory(); $orderStatusHistory = $orderStatusHistoryFactory->create($orderShop, $orderStatus); $orderShop->addOrderStatusHistory($orderStatusHistory); } return $orderShop; } public function addOrderProduct( OrderShopInterface $orderShop, OrderProductInterface $orderProductAdd, bool $persist = true ) { $return = false; if ($this->orderProductStore->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) { if ($orderProductAdd->getQuantityOrder() > 0) { $updated = false; $this->orderProductBuilder->init($orderProductAdd); $productFamily = $this->productFamilyStore->getOneBySlug( $orderProductAdd->getProduct()->getProductFamily()->getSlug() ); if ($productFamily) { $reductionCatalog = $productFamily->getReductionCatalog(); if ($reductionCatalog) { $orderProductReductionCatalogFactory = new OrderProductReductionCatalogFactory(); $orderProductReductionCatalog = $orderProductReductionCatalogFactory->create( $reductionCatalog->getTitle(), $reductionCatalog->getValue(), $reductionCatalog->getUnit(), $reductionCatalog->getBehaviorTaxRate() ); $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog); } } foreach ($orderShop->getOrderProducts() as $orderProduct) { if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId() && $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery() && (string)$this->priceResolver->getPrice($orderProduct) == (string)$this->priceResolver->getPrice($orderProductAdd) && $orderProduct->getOrderProductReductionCatalog()->compare( $orderProductAdd->getOrderProductReductionCatalog() )) { $orderProduct->setQuantityOrder( $orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder() ); if ($persist) { $this->entityManager->persist($orderProduct); } $updated = true; $return = true; break; } } if (!$updated) { $orderShop->addOrderProduct($orderProductAdd); if ($persist) { if (isset($orderProductReductionCatalog)) { $this->entityManager->persist($orderProductReductionCatalog); } $this->entityManager->persist($orderProductAdd); $this->entityManager->persist($orderShop); } $return = true; } if ($persist) { $this->entityManager->flush(); } // @TODO : dispatch event cart change //$this->eventCartChange($orderShop); } } else { // @TODO : retourner le message d'erreur et faire le addFlash dans le contrôleur /*$availableQuantity = $orderProductAdd->getProduct()->getAvailableQuantityInherited(); $textError = "Le produit " . $orderProductAdd->getTitleOrderShop( ) . " n'est pas disponible"; if ($availableQuantity !== false && $availableQuantity > 0) { $unit = ''; if ($orderProductAdd->getProduct()->getProductFamily()->getBehaviorCountStock( ) == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) { $unit = $orderProductAdd->getProduct()->getUnitInherited()->getUnitReference()->getUnit(); } $textError .= ' dans cette quantité '; $user = $this->security->getUser(); if ($user && $user->hasRole('ROLE_USER')) { $textError .= '
' . $availableQuantity . $unit . ' disponible(s) dont ' . $this->getQuantityOrderByProduct( $orderShop, $orderProductAdd->getProduct() ) . $unit . ' déjà dans votre panier.'; } } $this->utils->addFlash('error', $textError);*/ } return $return; } public function merge(OrderShopInterface $orderShop1, OrderShopInterface $orderShop2, $persist = true) { if ($orderShop1 && $orderShop2) { foreach ($orderShop2->getOrderProducts() as $orderProduct) { $orderProductAlreadyInCart = $orderShop1->hasOrderProductAlreadyInCart($orderProduct); if ($orderProductAlreadyInCart) { if ($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) { $orderShop1->removeOrderProduct($orderProductAlreadyInCart); $this->addOrderProduct($orderShop1, $orderProduct); } } else { $this->addOrderProduct($orderShop1, $orderProduct); } if ($persist) { $this->entityManager->remove($orderProduct); } } if ($persist) { $this->entityManager->remove($orderShop2); $this->entityManager->persist($orderShop1); $this->entityManager->flush(); } return $orderShop1; } } public function addPayment(OrderShopInterface $orderShop, $meanPayment, $amount) { $orderPaymentFactory = new OrderPaymentFactory(); $orderPayment = $orderPaymentFactory->create($orderShop, $meanPayment, $amount); $orderShop->addOrderPayment($orderPayment); if ($this->isOrderPaid($orderShop)) { $nextStatus = OrderStatusModel::ALIAS_PAID; } else { $nextStatus = OrderStatusModel::ALIAS_PARTIAL_PAYMENT; } if ($orderShop->getOrderStatus()->getAlias() != $nextStatus) { $orderShop = $this->changeOrderStatus($orderShop, $nextStatus); } $this->entityManager->persist($orderPayment); $this->entityManager->update($orderShop); $this->entityManager->flush(); return $orderShop; } public function createDocumentInvoice(OrderShopInterface $orderShop) { $documentFactory = new DocumentFactory(); $document = $documentFactory->create(DocumentModel::TYPE_INVOICE); $this->documentBuilder->initFromOrderShop($orderShop); return $document; } public function addReductionCart(OrderShopInterface $orderShop, ReductionCartInterface $reductionCart) { $orderReductionCartFactory = new OrderReductionCartFactory(); $orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart); $orderShop->addOrderReductionCart($orderReductionCart) ; if($this->orderShopStore->isPositiveAmount($orderShop) && $this->isPositiveAmountRemainingToBePaid($orderShop)) { $this->entityManager->persist($orderReductionCart); $this->entityManager->flush(); return $orderReductionCart ; } else { $orderShop->removeOrderReductionCart($orderReductionCart) ; return false; } } // createOrderReductionCredit public function addReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit) { $orderReductionCreditFactory = new OrderReductionCreditFactory(); $orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit); $orderShop->addOrderReductionCredit($orderReductionCredit) ; if($this->isOrderShopPositiveAmount($orderShop) && $this->isOrderShopPositiveAmountRemainingToBePaid($orderShop)) { $this->entityManager->persist($orderReductionCredit); $this->entityManager->flush(); return $orderReductionCredit; } else { $orderShop->removeOrderReductionCredit($orderReductionCredit) ; return false; } } public function deductAvailabilityProduct(OrderShopInterface $orderShop) { // @TODO : à refactorer en plaçant dans src tout ce qui est spécifique à PDL foreach ($orderShop->getOrderProducts() as $orderProduct) { //Si ce n'esrt pas une relivraison OU si c'est une relivraison + relivraison + ce n'est pas une erruer producteur if (!$orderProduct->isRedelivery() || ($orderProduct->isRedelivery() && $orderProduct->isRedeliverySupplierOrder() && !$orderProduct->isRedeliverySupplierMistake())) { switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) { case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE : //Disponibilité par unité de référence $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited(); $newAvailability = $oldAvailability - ($orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient())); $productFamily = $orderProduct->getProduct()->getProductFamily(); $productFamily->setAvailableQuantity($newAvailability); $productFamily->setUpdatedBy($orderShop->getUser()); $this->entityManager->persist($productFamily); break; case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY : $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited(); $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder(); $productFamily = $orderProduct->getProduct()->getProductFamily(); $productFamily->setAvailableQuantity($newAvailability); $productFamily->setUpdatedBy($orderShop->getUser()); $this->entityManager->persist($productFamily); break; case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT : $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited(); $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder(); $product = $orderProduct->getProduct(); $product->setAvailableQuantity($newAvailability); $product->setUpdatedBy($orderShop->getUser()); $this->entityManager->persist($product); break; } $this->entityManager->flush(); } } } }