custom/plugins/SwagB2bPlatform/components/Cart/BridgePlatform/OrderClearanceCartInterceptor.php line 102

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.     /**
  25.      * @var LineItemListService
  26.      */
  27.     private $lineItemListService;
  28.     /**
  29.      * @var OrderContextService
  30.      */
  31.     private $orderContextService;
  32.     /**
  33.      * @var AuthenticationService
  34.      */
  35.     private $authenticationService;
  36.     /**
  37.      * @var OrderClearanceRepository
  38.      */
  39.     private $orderClearanceRepository;
  40.     /**
  41.      * @var EventDispatcherInterface
  42.      */
  43.     private $eventDispatcher;
  44.     /**
  45.      * @var CartService
  46.      */
  47.     private $cartService;
  48.     /**
  49.      * @var ShopCartService
  50.      */
  51.     private $shopCartService;
  52.     /**
  53.      * @var OrderContextRepository
  54.      */
  55.     private $orderContextRepository;
  56.     /**
  57.      * @var TotalPriceCalculationService
  58.      */
  59.     private $totalPriceCalculationService;
  60.     public function __construct(
  61.         AuthenticationService $authenticationService,
  62.         LineItemListService $lineItemListService,
  63.         OrderContextService $orderContextService,
  64.         OrderClearanceRepository $orderClearanceRepository,
  65.         EventDispatcherInterface $eventDispatcher,
  66.         CartService $cartService,
  67.         ShopCartService $shopCartService,
  68.         OrderContextRepository $orderContextRepository,
  69.         TotalPriceCalculationService $totalPriceCalculationService
  70.     ) {
  71.         $this->authenticationService $authenticationService;
  72.         $this->lineItemListService $lineItemListService;
  73.         $this->orderContextService $orderContextService;
  74.         $this->orderClearanceRepository $orderClearanceRepository;
  75.         $this->eventDispatcher $eventDispatcher;
  76.         $this->cartService $cartService;
  77.         $this->shopCartService $shopCartService;
  78.         $this->orderContextRepository $orderContextRepository;
  79.         $this->totalPriceCalculationService $totalPriceCalculationService;
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public static function getSubscribedEvents(): array
  85.     {
  86.         return [CartInterceptEvent::class => 'moveToClearance'];
  87.     }
  88.     public function moveToClearance(CartInterceptEvent $event): void
  89.     {
  90.         $identity $this->authenticationService
  91.             ->getIdentity();
  92.         $context $event->getContext();
  93.         $orderContext $this->createOrderContext($identity$event->getCart(), $context);
  94.         $this->updateOrderContextWithAdditionalData($orderContext$context);
  95.         $this->orderClearanceRepository
  96.             ->sendToOrderClearance($orderContext->id$identity->getOwnershipContext());
  97.         $this->sendOrderClearanceInquiryToDebtor($orderContext$identity$event->getContext());
  98.         $this->shopCartService->clear($event->getContext());
  99.         throw new B2bControllerRedirectException('finish''b2bcart');
  100.     }
  101.     /**
  102.      * @internal
  103.      */
  104.     protected function updateOrderContextWithAdditionalData(
  105.         OrderContext $orderContext,
  106.         SalesChannelContext $context
  107.     ): void {
  108.         $cart $this->cartService->getCart($context->getToken(), $context);
  109.         /** @var AdditionalDataExtension $additionalData */
  110.         $additionalData $cart->getExtension(AdditionalDataExtension::NAME);
  111.         if (!$additionalData) {
  112.             return;
  113.         }
  114.         $orderReference $additionalData->getOrderReferenceNumber();
  115.         $deliveryDate $additionalData->getRequestedDeliveryDate();
  116.         if (!$orderReference && !$deliveryDate) {
  117.             return;
  118.         }
  119.         $orderContext->orderReference $orderReference;
  120.         $orderContext->requestedDeliveryDate $deliveryDate;
  121.         $this->orderContextRepository->updateContext($orderContext);
  122.     }
  123.     /**
  124.      * @internal
  125.      */
  126.     protected function createOrderContext(Identity $identityCart $cartSalesChannelContext $salesChannelContext): OrderContext
  127.     {
  128.         $ownershipContext $identity->getOwnershipContext();
  129.         $list $this->lineItemListService
  130.             ->createListThroughCheckoutSource(
  131.                 $ownershipContext,
  132.                 new LineItemCheckoutSource($cart)
  133.             );
  134.         return $this->orderContextService
  135.             ->createContextThroughCheckoutSource(
  136.                 $ownershipContext,
  137.                 $list,
  138.                 $this->createOrderCheckoutSourceFromCart($cart$salesChannelContext)
  139.             );
  140.     }
  141.     /**
  142.      * @internal
  143.      */
  144.     protected function createOrderCheckoutSourceFromCart(Cart $cartSalesChannelContext $salesChannelContext): OrderCheckoutSource
  145.     {
  146.         // ToDo set comment and device type
  147.         return new OrderCheckoutSource(
  148.             IdValue::create($salesChannelContext->getCustomer()->getActiveBillingAddress()->getId()),
  149.             IdValue::create($salesChannelContext->getCustomer()->getActiveShippingAddress()->getId()),
  150.             IdValue::create($salesChannelContext->getShippingMethod()->getId()),
  151.             '',
  152.             '',
  153.             $this->totalPriceCalculationService->getTotalAmount($cart->getShippingCosts(), $salesChannelContext),
  154.             $this->totalPriceCalculationService->getTotalAmountNet($cart->getShippingCosts(), $salesChannelContext),
  155.             IdValue::create($salesChannelContext->getPaymentMethod()->getId())
  156.         );
  157.     }
  158.     /**
  159.      * @internal
  160.      */
  161.     protected function sendOrderClearanceInquiryToDebtor(OrderContext $orderContextIdentity $identitySalesChannelContext $salesChannelContext): void
  162.     {
  163.         if (!$identity instanceof ContactIdentity) {
  164.             return;
  165.         }
  166.         $contact $identity->getEntity();
  167.         $debtor $contact->debtor;
  168.         // @todo There is no guarantee that $contact is actually a ContactEntity here!!
  169.         $this->eventDispatcher->dispatch(new OrderClearanceInquiryMailEvent(
  170.             $contact,
  171.             $debtor,
  172.             $salesChannelContext,
  173.             $orderContext
  174.         ));
  175.     }
  176. }