vendor/store.shopware.com/swagb2bplatform/components/Cart/BridgePlatform/OrderClearanceCartInterceptor.php line 75

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Cart\BridgePlatform;
  3. use Shopware\B2B\Common\Controller\B2bControllerRedirectException;
  4. use Shopware\B2B\Common\IdValue;
  5. use Shopware\B2B\Contact\Framework\ContactIdentity;
  6. use Shopware\B2B\LineItemList\BridgePlatform\LineItemCheckoutSource;
  7. use Shopware\B2B\LineItemList\Framework\LineItemListService;
  8. use Shopware\B2B\Order\BridgePlatform\AdditionalDataExtension;
  9. use Shopware\B2B\Order\Framework\OrderCheckoutSource;
  10. use Shopware\B2B\Order\Framework\OrderContext;
  11. use Shopware\B2B\Order\Framework\OrderContextRepository;
  12. use Shopware\B2B\Order\Framework\OrderContextService;
  13. use Shopware\B2B\OrderClearance\BridgePlatform\OrderClearanceInquiryMailEvent;
  14. use Shopware\B2B\OrderClearance\Framework\OrderClearanceRepository;
  15. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  16. use Shopware\B2B\StoreFrontAuthentication\Framework\Identity;
  17. use Shopware\Core\Checkout\Cart\Cart;
  18. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  19. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. class OrderClearanceCartInterceptor implements EventSubscriberInterface
  23. {
  24.     private LineItemListService $lineItemListService;
  25.     private OrderContextService $orderContextService;
  26.     private AuthenticationService $authenticationService;
  27.     private OrderClearanceRepository $orderClearanceRepository;
  28.     private EventDispatcherInterface $eventDispatcher;
  29.     private CartService $cartService;
  30.     private ShopCartService $shopCartService;
  31.     private OrderContextRepository $orderContextRepository;
  32.     private TotalPriceCalculationService $totalPriceCalculationService;
  33.     public function __construct(
  34.         AuthenticationService $authenticationService,
  35.         LineItemListService $lineItemListService,
  36.         OrderContextService $orderContextService,
  37.         OrderClearanceRepository $orderClearanceRepository,
  38.         EventDispatcherInterface $eventDispatcher,
  39.         CartService $cartService,
  40.         ShopCartService $shopCartService,
  41.         OrderContextRepository $orderContextRepository,
  42.         TotalPriceCalculationService $totalPriceCalculationService
  43.     ) {
  44.         $this->authenticationService $authenticationService;
  45.         $this->lineItemListService $lineItemListService;
  46.         $this->orderContextService $orderContextService;
  47.         $this->orderClearanceRepository $orderClearanceRepository;
  48.         $this->eventDispatcher $eventDispatcher;
  49.         $this->cartService $cartService;
  50.         $this->shopCartService $shopCartService;
  51.         $this->orderContextRepository $orderContextRepository;
  52.         $this->totalPriceCalculationService $totalPriceCalculationService;
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         return [CartInterceptEvent::class => 'moveToClearance'];
  60.     }
  61.     public function moveToClearance(CartInterceptEvent $event): void
  62.     {
  63.         $identity $this->authenticationService
  64.             ->getIdentity();
  65.         $context $event->getContext();
  66.         $orderContext $this->createOrderContext($identity$event->getCart(), $context);
  67.         $this->updateOrderContextWithAdditionalData($orderContext$context);
  68.         $this->orderClearanceRepository
  69.             ->sendToOrderClearance($orderContext->id$identity->getOwnershipContext());
  70.         $this->sendOrderClearanceInquiryToDebtor($orderContext$identity$event->getContext());
  71.         $this->shopCartService->clear($event->getContext());
  72.         throw new B2bControllerRedirectException('finish''b2bcart');
  73.     }
  74.     /**
  75.      * @internal
  76.      */
  77.     protected function updateOrderContextWithAdditionalData(
  78.         OrderContext $orderContext,
  79.         SalesChannelContext $context
  80.     ): void {
  81.         $cart $this->cartService->getCart($context->getToken(), $context);
  82.         /** @var AdditionalDataExtension $additionalData */
  83.         $additionalData $cart->getExtension(AdditionalDataExtension::NAME);
  84.         if (!$additionalData) {
  85.             return;
  86.         }
  87.         $orderReference $additionalData->getOrderReferenceNumber();
  88.         $deliveryDate $additionalData->getRequestedDeliveryDate();
  89.         if (!$orderReference && !$deliveryDate) {
  90.             return;
  91.         }
  92.         $orderContext->orderReference $orderReference;
  93.         $orderContext->requestedDeliveryDate $deliveryDate;
  94.         $this->orderContextRepository->updateContext($orderContext);
  95.     }
  96.     /**
  97.      * @internal
  98.      */
  99.     protected function createOrderContext(Identity $identityCart $cartSalesChannelContext $salesChannelContext): OrderContext
  100.     {
  101.         $ownershipContext $identity->getOwnershipContext();
  102.         $list $this->lineItemListService
  103.             ->createListThroughCheckoutSource(
  104.                 $ownershipContext,
  105.                 new LineItemCheckoutSource($cart)
  106.             );
  107.         return $this->orderContextService
  108.             ->createContextThroughCheckoutSource(
  109.                 $ownershipContext,
  110.                 $list,
  111.                 $this->createOrderCheckoutSourceFromCart($cart$salesChannelContext)
  112.             );
  113.     }
  114.     /**
  115.      * @internal
  116.      */
  117.     protected function createOrderCheckoutSourceFromCart(Cart $cartSalesChannelContext $salesChannelContext): OrderCheckoutSource
  118.     {
  119.         // ToDo set comment and device type
  120.         return new OrderCheckoutSource(
  121.             IdValue::create($salesChannelContext->getCustomer()->getActiveBillingAddress()->getId()),
  122.             IdValue::create($salesChannelContext->getCustomer()->getActiveShippingAddress()->getId()),
  123.             IdValue::create($salesChannelContext->getShippingMethod()->getId()),
  124.             '',
  125.             '',
  126.             $this->totalPriceCalculationService->getTotalAmount($cart->getShippingCosts(), $salesChannelContext),
  127.             $this->totalPriceCalculationService->getTotalAmountNet($cart->getShippingCosts(), $salesChannelContext),
  128.             IdValue::create($salesChannelContext->getPaymentMethod()->getId())
  129.         );
  130.     }
  131.     /**
  132.      * @internal
  133.      */
  134.     protected function sendOrderClearanceInquiryToDebtor(OrderContext $orderContextIdentity $identitySalesChannelContext $salesChannelContext): void
  135.     {
  136.         if (!$identity instanceof ContactIdentity) {
  137.             return;
  138.         }
  139.         $contact $identity->getEntity();
  140.         $debtor $contact->debtor;
  141.         // @todo There is no guarantee that $contact is actually a ContactEntity here!!
  142.         $this->eventDispatcher->dispatch(new OrderClearanceInquiryMailEvent(
  143.             $contact,
  144.             $debtor,
  145.             $salesChannelContext,
  146.             $orderContext
  147.         ));
  148.     }
  149. }