custom/plugins/EcomwiseOrderStatusComplete/src/Subscriber/OrderEventSubscriber.php line 157

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EcomwiseOrderStatusComplete\Subscriber;
  3. use Psr\Log\LoggerInterface;
  4. use Shopware\Core\Checkout\Cart\Exception\OrderTransactionNotFoundException;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Content\MailTemplate\Exception\SalesChannelNotFoundException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  14. use Shopware\Core\System\StateMachine\Transition;
  15. use Shopware\Core\Checkout\Order\OrderStates;
  16. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryStates;
  19. use Shopware\Core\Checkout\Cart\Exception\OrderDeliveryNotFoundException;
  20. use Shopware\Core\Framework\Context;
  21. /**
  22.  * Copyright (c) 2020. GOLLE IT.
  23.  *
  24.  * @author Fabian Golle <fabian@golle-it.de>
  25.  */
  26. class OrderEventSubscriber implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var EntityRepositoryInterface $orderRepository
  30.      */
  31.     private $orderRepository;
  32.     /**
  33.      * @var EntityRepositoryInterface $orderTransactionRepository
  34.      */
  35.     private $orderTransactionRepository;
  36.     /**
  37.      * @var SystemConfigService $systemConfigService
  38.      */
  39.     private $systemConfigService;
  40.     
  41.     /**
  42.      * @var LoggerInterface $logger
  43.      */
  44.     private $logger;
  45.      /**
  46.      * @var StateMachineRegistry $stateMachineRegistry
  47.      */
  48.     private $stateMachineRegistry;
  49.     /**
  50.      * @var EntityRepositoryInterface
  51.      */
  52.     private $deliveryRepository;
  53.     
  54.     /**
  55.      * OrderEventSubscriber constructor.
  56.      *
  57.      * @param EntityRepositoryInterface $orderRepository
  58.      * @param EntityRepositoryInterface $orderTransactionRepository
  59.      * @param LoggerInterface $logger
  60.      */
  61.     public function __construct(
  62.         EntityRepositoryInterface $orderRepository,
  63.         EntityRepositoryInterface $orderTransactionRepository,
  64.         SystemConfigService $systemConfigService,
  65.         LoggerInterface $logger,
  66.         StateMachineRegistry $stateMachineRegistry,
  67.         EntityRepositoryInterface $deliveryRepository
  68.     ) {
  69.         $this->orderRepository            $orderRepository;
  70.         $this->orderTransactionRepository $orderTransactionRepository;
  71.         $this->systemConfigService        $systemConfigService;
  72.         $this->logger                     $logger;
  73.         $this->stateMachineRegistry       $stateMachineRegistry;
  74.         $this->deliveryRepository         $deliveryRepository;
  75.     }
  76.     public static function getSubscribedEvents(): array
  77.     {
  78.         return[
  79.             'state_machine.order_transaction.state_changed' => 'onOrderTransactionStateChanged',
  80.             'state_machine.order_delivery.state_changed' => 'onOrderDeliveryStateChange'
  81.         ];
  82.     }
  83.     /**
  84.      * Gets invoked when the payment status is changed.
  85.      *
  86.      * @param StateMachineStateChangeEvent $event
  87.      *
  88.      * @throws InconsistentCriteriaIdsException
  89.      * @throws SalesChannelNotFoundException
  90.      * @throws OrderTransactionNotFoundException
  91.      */
  92.     public function onOrderTransactionStateChanged(StateMachineStateChangeEvent $event)
  93.     {
  94.         $config $this->systemConfigService->get('EcomwiseOrderStatusComplete.config');
  95.         $nextState $event->getNextState()->getTechnicalName();
  96.         $context $event->getContext();
  97.         if($config["enableOrderStatusComplete"] && $event->getTransitionSide() == 'state_leave' && $nextState == 'paid') {
  98.             $orderTransactionId $event->getTransition()->getEntityId();
  99.     
  100.             /** @var OrderTransactionEntity|null $orderTransaction */
  101.             $orderTransaction $this->orderTransactionRepository->search(
  102.                 new Criteria([$orderTransactionId]),
  103.                 $event->getContext()
  104.             )->first();
  105.     
  106.             if ($orderTransaction === null) {
  107.                 throw new OrderTransactionNotFoundException($orderTransactionId);
  108.             }
  109.     
  110.             $orderId $orderTransaction->getOrderId();
  111.     
  112.             /** @var OrderEntity|null $order */  
  113.             $criteria = new Criteria();
  114.             $criteria->addFilter(new EqualsFilter('order.id'$orderId))->addAssociation('deliveries');
  115.             $order $this->orderRepository->search($criteria,$context)->first();
  116.             $orderDeliveries $order->getDeliveries()->getElements();
  117.             $orderDeliveryState '';
  118.             foreach($orderDeliveries as $orderDelivery){
  119.                 $orderDeliveryState $orderDelivery->getStateMachineState()->getTechnicalName();
  120.             }
  121.             if ($order instanceof OrderEntity && $orderDeliveryState == OrderDeliveryStates::STATE_SHIPPED) {
  122.                 $order_status $order->getStateMachineState()->getTechnicalName();
  123.                 if($order_status == OrderStates::STATE_IN_PROGRESS) {
  124.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_COMPLETE'stateId');
  125.                     $this->stateMachineRegistry->transition($transition$context);
  126.                 } elseif($order_status == OrderStates::STATE_CANCELLED) {
  127.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_REOPEN'stateId');
  128.                     $this->stateMachineRegistry->transition($transition$context);
  129.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_PROCESS'stateId');
  130.                     $this->stateMachineRegistry->transition($transition$context);
  131.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_COMPLETE'stateId');
  132.                     $this->stateMachineRegistry->transition($transition$context);
  133.                 } elseif($order_status == 'open') {
  134.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_PROCESS'stateId');
  135.                     $this->stateMachineRegistry->transition($transition$context);
  136.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_COMPLETE'stateId');
  137.                     $this->stateMachineRegistry->transition($transition$context);
  138.                 }
  139.             } else {
  140.                 $this->logger->error(sprintf('Event %s did not receive a proper ordernumber. Unable to get Order-object. Aborting.'$event->getName()));
  141.             }
  142.         }
  143.     }
  144.     public function onOrderDeliveryStateChange(StateMachineStateChangeEvent $event): void
  145.     {
  146.         $config $this->systemConfigService->get('EcomwiseOrderStatusComplete.config');
  147.         $nextState $event->getNextState()->getTechnicalName();
  148.         $orderDeliveryId $event->getTransition()->getEntityId();
  149.         $context $event->getContext();
  150.         if($config["enableOrderStatusComplete"] && $event->getTransitionSide() == 'state_enter' && $nextState == OrderDeliveryStates::STATE_SHIPPED) {
  151.             /** @var OrderDeliveryEntity|null $orderDelivery */
  152.             $orderDelivery $this->deliveryRepository
  153.                 ->search(new Criteria([$orderDeliveryId]), $context)
  154.                 ->first();
  155.             if ($orderDelivery === null) {
  156.                 throw new OrderDeliveryNotFoundException($orderDeliveryId);
  157.             }
  158.             $orderId $orderDelivery->getOrderId();
  159.             $order $this->getOrder($orderId$context);
  160.             $orderTransactions $order->getTransactions()->getElements();
  161.             $orderTransactionState '';
  162.             foreach($orderTransactions as $orderTransaction){
  163.                 $orderTransactionState $orderTransaction->getStateMachineState()->getTechnicalName();
  164.             }
  165.             if ($orderTransactionState == 'paid') {
  166.                 $order_status $order->getStateMachineState()->getTechnicalName();
  167.                 if($order_status == OrderStates::STATE_IN_PROGRESS) {
  168.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_COMPLETE'stateId');
  169.                     $this->stateMachineRegistry->transition($transition$context);
  170.                 } elseif($order_status == OrderStates::STATE_CANCELLED) {
  171.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_REOPEN'stateId');
  172.                     $this->stateMachineRegistry->transition($transition$context);
  173.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_PROCESS'stateId');
  174.                     $this->stateMachineRegistry->transition($transition$context);
  175.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_COMPLETE'stateId');
  176.                     $this->stateMachineRegistry->transition($transition$context);
  177.                 } elseif($order_status == 'open') {
  178.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_PROCESS'stateId');
  179.                     $this->stateMachineRegistry->transition($transition$context);
  180.                     $transition = new Transition('order'$orderIdStateMachineTransitionActions::ACTION_COMPLETE'stateId');
  181.                     $this->stateMachineRegistry->transition($transition$context);
  182.                 }
  183.             } else {
  184.                 $this->logger->error(sprintf('Event %s did not receive a proper ordernumber. Unable to get Order-object. Aborting.'$event->getName()));
  185.             }
  186.         }
  187.     }
  188.     /**
  189.      * @throws OrderNotFoundException
  190.      */
  191.     private function getOrder(string $orderIdContext $context): OrderEntity
  192.     {
  193.         $orderCriteria $this->getOrderCriteria($orderId);
  194.         $order $this->orderRepository
  195.             ->search($orderCriteria$context)
  196.             ->first();
  197.         if (!$order instanceof OrderEntity) {
  198.             throw new OrderNotFoundException($orderId);
  199.         }
  200.         return $order;
  201.     }
  202.     private function getOrderCriteria(string $orderId): Criteria
  203.     {
  204.         $criteria = new Criteria([$orderId]);
  205.         $criteria->addAssociation('orderCustomer.salutation');
  206.         $criteria->addAssociation('stateMachineState');
  207.         $criteria->addAssociation('lineItems');
  208.         $criteria->addAssociation('transactions');
  209.         $criteria->addAssociation('deliveries.shippingMethod');
  210.         $criteria->addAssociation('deliveries.shippingOrderAddress.country');
  211.         $criteria->addAssociation('deliveries.shippingOrderAddress.countryState');
  212.         $criteria->addAssociation('deliveries.positions.orderLineItem');
  213.         $criteria->addAssociation('salesChannel');
  214.         return $criteria;
  215.     }
  216. }