custom/plugins/VioB2BWorkflow/src/Core/Subscriber/CartSubscriber.php line 107

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Vio\B2BWorkflow\Core\Subscriber;
  4. use NetInventors\NetiNextOrderFields\Core\Content\OrderFields\OrderFieldsEntity;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use VioB2BLogin\Core\Services\EmployeeService;
  14. use VioB2BLogin\Entity\Employee\EmployeeEntity;
  15. use VioB2BLogin\VioB2BLogin;
  16. use Vio\B2BWorkflow\Core\Checkout\Cart\Event\CheckoutCustomerOrderRequestPlacedEvent;
  17. use Vio\B2BWorkflow\Core\Checkout\Cart\Event\CheckoutEmployeeOrderRequestPlacedEvent;
  18. use Vio\B2BWorkflow\Core\ContextExtension;
  19. use Vio\B2BWorkflow\Services\StateService;
  20. class CartSubscriber implements EventSubscriberInterface
  21. {
  22.     private EmployeeService $employeeService;
  23.     private StateService $stateService;
  24.     private EventDispatcherInterface $eventDispatcher;
  25.     private EntityRepositoryInterface $employeeRepository;
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private $orderFieldRepository;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     private $orderFieldsOrderRepository;
  34.     /**
  35.      * @var EntityRepositoryInterface
  36.      */
  37.     private $orderFieldsOrderValueRepository;
  38.     /**
  39.      * @var RequestStack
  40.      */
  41.     private $requestStack;
  42.     public function __construct(
  43.         EmployeeService           $employeeService,
  44.         StateService              $stateService,
  45.         EventDispatcherInterface  $eventDispatcher,
  46.         EntityRepositoryInterface $employeeRepository,
  47.         EntityRepositoryInterface $orderFieldRepository,
  48.         EntityRepositoryInterface $orderFieldsOrderRepository,
  49.         EntityRepositoryInterface $orderFieldsOrderValueRepository,
  50.         RequestStack $requestStack
  51.     )
  52.     {
  53.         $this->employeeService $employeeService;
  54.         $this->stateService $stateService;
  55.         $this->eventDispatcher $eventDispatcher;
  56.         $this->employeeRepository $employeeRepository;
  57.         $this->orderFieldRepository            $orderFieldRepository;
  58.         $this->orderFieldsOrderRepository      $orderFieldsOrderRepository;
  59.         $this->orderFieldsOrderValueRepository $orderFieldsOrderValueRepository;
  60.         $this->requestStack                    $requestStack;
  61.     }
  62.     public static function getSubscribedEvents(): array
  63.     {
  64.         return [
  65.             CartConvertedEvent::class => 'onCartConverted',
  66.             CheckoutOrderPlacedEvent::class => ['onOrderPlaced'999]
  67.         ];
  68.     }
  69.     public function onCartConverted(CartConvertedEvent $event): void
  70.     {
  71.         $cart $event->getCart();
  72.         $convertedCart $event->getConvertedCart();
  73.         $salesChannelContext $event->getSalesChannelContext();
  74.         $employee $this->employeeService->getEmployeeByContext($salesChannelContext);
  75.         // check rights
  76.         if ($employee
  77.             && $cart->hasExtensionOfType(ContextExtension::KEYContextExtension::class)) {
  78.             $convertedCart['stateId'] = $this->stateService->getApprovalState($event->getContext())->getId();
  79.             foreach ($convertedCart['transactions'] as &$transaction) {
  80.                 $transaction['stateId'] = $this->stateService->getPresetState($event->getContext())->getId();
  81.             }
  82.             unset($transaction);
  83.             $extension = (new ContextExtension())->setActive(true);
  84.             $event->getContext()->addExtension(ContextExtension::KEY$extension);
  85.         }
  86.         $event->setConvertedCart($convertedCart);
  87.     }
  88.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  89.     {
  90.         $order $event->getOrder();
  91.         if ($order->getStateId() === $this->stateService->getApprovalState($event->getContext())->getId()) {
  92.             ///// added fix by Mindaugas
  93.             $request   $this->requestStack->getCurrentRequest();
  94.             $parameter $request->request->get('orderField') ?? [];
  95.             foreach ($parameter as $key => $value) {
  96.                 if ($value === '') {
  97.                     unset($parameter[$key]);
  98.                 }
  99.             }
  100.             if ([] === $parameter || !\is_array($parameter)) {
  101.                 return;
  102.             }
  103.             $orderId            $event->getOrder()->getId();
  104.             $criteria           = new Criteria(array_keys($parameter));
  105.             $orderFieldEntities $this->orderFieldRepository->search($criteria$event->getContext())->getElements();
  106.             $orderFields        = [];
  107.             $orderFieldsValues  = [];
  108.             /** @var OrderFieldsEntity $entity */
  109.             foreach ($orderFieldEntities as $id => $entity) {
  110.                 $orderFields[$id]['label']          = $entity->getTranslation('label');
  111.                 $orderFields[$id]['name']           = $entity->getName();
  112.                 $orderFields[$id]['type']           = $entity->getType();
  113.                 $orderFields[$id]['orderId']        = $orderId;
  114.                 $orderFields[$id]['orderVersionId'] = $event->getOrder()->getVersionId();
  115.                 $orderFields[$id]['orderFieldId']   = $entity->getId();
  116.                 $orderFields[$id]['versionId']      = $event->getOrder()->getVersionId();
  117.                 $orderFieldsValues[$id]['value'] = substr($parameter[$id], 0512);
  118.                 foreach ($entity->getExtension('netiOrderFieldsVal')->getElements() as $value) {
  119.                     if ($value->getValue() === $orderFieldsValues[$id]['value']) {
  120.                         $orderFieldsValues[$id]['valueLabel'] = $value->getTranslated()['label'];
  121.                         break;
  122.                     }
  123.                 }
  124.             }
  125.             if ($orderFields === []) {
  126.                 return;
  127.             }
  128.             $filterClosure = static function ($arr) {
  129.                 return isset($arr['value']) && '' !== $arr['value'];
  130.             };
  131.             $orderFieldsValues array_filter($orderFieldsValues$filterClosure);
  132.             $orderFields       array_intersect_key($orderFields$orderFieldsValues);
  133.             $context           $event->getContext();
  134.             $result            $this->orderFieldsOrderRepository->create(array_values($orderFields), $context);
  135.             /** @var EntityWrittenEvent $event */
  136.             $entityEvent $result->getEvents()->first();
  137.             $entityIds = [];
  138.             foreach ($entityEvent->getIds() as $entry) {
  139.                 $entityIds[] = $entry['id'];
  140.             }
  141.             $criteria             = new Criteria($entityIds);
  142.             $orderFieldAttributes $this->orderFieldsOrderRepository->search($criteria$context)->getElements();
  143.             foreach ($orderFieldAttributes as $attribute) {
  144.                 $orderFieldsValues[$attribute->getOrderFieldId()]['orderAttributeId'] = $attribute->getId();
  145.                 $orderFieldsValues[$attribute->getOrderFieldId()]['orderAttributeVersionId'] = $event->getOrder()->getVersionId();
  146.             }
  147.             $this->orderFieldsOrderValueRepository->create(array_values($orderFieldsValues), $context);
  148.             ///// end of fix
  149.             // when the order has an employee
  150.             $orderCustomFields $event->getOrder()->getCustomFields();
  151.             if (is_array($orderCustomFields)
  152.                 && array_key_exists(VioB2BLogin::EMPLOYEE_FIELD$orderCustomFields)) {
  153.                 // load Employee
  154.                 $employeeId $orderCustomFields[VioB2BLogin::EMPLOYEE_FIELD];
  155.                 $criteria = new Criteria([$employeeId]);
  156.                 $criteria->addAssociation('customer');
  157.                 $employee $this->employeeRepository->search($criteria$event->getContext())->first();
  158.                 if ($employee instanceof EmployeeEntity) {
  159.                     $this->eventDispatcher->dispatch(new CheckoutEmployeeOrderRequestPlacedEvent(
  160.                         $event->getContext(),
  161.                         $event->getOrder(),
  162.                         $employee,
  163.                         $event->getSalesChannelId()
  164.                     ));
  165.                     $this->eventDispatcher->dispatch(new CheckoutCustomerOrderRequestPlacedEvent(
  166.                         $event->getContext(),
  167.                         $event->getOrder(),
  168.                         $employee,
  169.                         $event->getSalesChannelId()
  170.                     ));
  171.                 }
  172.             }
  173.             $event->stopPropagation();
  174.         }
  175.     }
  176. }