|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
-
- namespace Lc\CaracoleBundle\Builder\Order;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
- use Lc\CaracoleBundle\Event\Order\OrderShopChangeStatusEvent;
- use Lc\CaracoleBundle\Factory\File\DocumentFactory;
- use Lc\CaracoleBundle\Factory\Order\OrderPaymentFactory;
- use Lc\CaracoleBundle\Factory\Order\OrderProductReductionCatalogFactory;
- use Lc\CaracoleBundle\Factory\Order\OrderReductionCartFactory;
- use Lc\CaracoleBundle\Factory\Order\OrderReductionCreditFactory;
- use Lc\CaracoleBundle\Factory\Order\OrderShopFactory;
- use Lc\CaracoleBundle\Factory\Order\OrderStatusHistoryFactory;
- use Lc\CaracoleBundle\Model\File\DocumentModel;
- use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
- use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Model\User\VisitorInterface;
- use Lc\CaracoleBundle\Repository\Order\OrderProductStore;
- use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
- use Lc\CaracoleBundle\Repository\Order\OrderStatusStore;
- use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore;
- use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
- use Lc\SovBundle\Model\User\UserInterface;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
-
- class OrderShopBuilder
- {
- protected EntityManagerInterface $entityManager;
- protected OrderStatusStore $orderStatusStore;
- protected OrderProductStore $orderProductStore;
- protected ProductFamilyStore $productFamilyStore;
- protected PriceResolver $priceResolver;
- protected OrderProductBuilder $orderProductBuilder;
- protected DocumentBuilder $documentBuilder;
- protected EventDispatcherInterface $eventDispatcher;
- protected FlashBagInterface $flashBag;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- OrderShopStore $orderShopStore,
- OrderStatusStore $orderStatusStore,
- OrderProductStore $orderProductStore,
- ProductFamilyStore $productFamilyStore,
- OrderProductBuilder $orderProductBuilder,
- DocumentBuilder $documentBuilder,
- PriceResolver $priceResolver,
- EventDispatcherInterface $eventDispatcher,
- FlashBagInterface $flashBag
- ) {
- $this->entityManager = $entityManager;
- $this->orderShopStore = $orderShopStore;
- $this->orderStatusStore = $orderStatusStore;
- $this->orderProductStore = $orderProductStore;
- $this->productFamilyStore = $productFamilyStore;
- $this->orderProductBuilder = $orderProductBuilder;
- $this->documentBuilder = $documentBuilder;
- $this->priceResolver = $priceResolver;
- $this->eventDispatcher = $eventDispatcher;
- $this->flashBag = $flashBag;
- }
-
- 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, bool $forceByAdmin = false)
- {
- $orderStatus = $this->orderStatusStore->getRepositoryQuery()->findOneByAlias($alias);
-
- if ($orderStatus) {
- if ($orderShop->getOrderStatus() === null
- || $orderShop->getOrderStatus()->getNextStatusAllowed()->contains($orderStatus)) {
-
- $this->applyChangeOrderStatus($orderShop, $orderStatus, $forceByAdmin);
- }
- } else {
- throw new \ErrorException('La statut demandé n\'existe pas.');
- }
-
- return $orderShop;
- }
-
- public function applyChangeOrderStatus(
- OrderShopInterface $orderShop,
- OrderStatusInterface $orderStatus,
- bool $forceByAdmin = false
- ) {
- $this->eventDispatcher->dispatch(
- new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
- OrderShopChangeStatusEvent::PRE_CHANGE_STATUS
- );
-
- $orderShop->setOrderStatusProtected($orderStatus);
-
- $orderStatusHistoryFactory = new OrderStatusHistoryFactory();
- $orderStatusHistory = $orderStatusHistoryFactory->create($orderShop, $orderStatus);
-
- $orderShop->addOrderStatusHistory($orderStatusHistory);
-
- $this->eventDispatcher->dispatch(
- new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
- OrderShopChangeStatusEvent::POST_CHANGE_STATUS
- );
- }
-
- 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 <strong>" . $orderProductAdd->getTitleOrderShop(
- ) . "</strong> 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 .= '<br />' . $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();
- }
- }
- }
-
- }
|