custom/plugins/PickwareWms/src/PickingProcess/Subscriber/OrderShippingSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\PickwareWms\PickingProcess\Subscriber;
  11. use Pickware\DalBundle\EntityManager;
  12. use Pickware\HttpUtils\JsonApi\JsonApiError;
  13. use Pickware\PickwareErpStarter\OrderShipping\PreOrderShippingValidationEvent;
  14. use Pickware\PickwareWms\Delivery\Model\DeliveryCollection;
  15. use Pickware\PickwareWms\Delivery\Model\DeliveryDefinition;
  16. use Pickware\PickwareWms\Delivery\Model\DeliveryEntity;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class OrderShippingSubscriber implements EventSubscriberInterface
  19. {
  20.     private EntityManager $entityManager;
  21.     public function __construct(EntityManager $entityManager)
  22.     {
  23.         $this->entityManager $entityManager;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             PreOrderShippingValidationEvent::EVENT_NAME => 'preOrderShippingValidation',
  29.         ];
  30.     }
  31.     public function preOrderShippingValidation(PreOrderShippingValidationEvent $event): void
  32.     {
  33.         /** @var DeliveryCollection $deliveries */
  34.         $deliveries $this->entityManager->findBy(
  35.             DeliveryDefinition::class,
  36.             ['orderId' => $event->getOrderIds()],
  37.             $event->getContext(),
  38.             ['order'],
  39.         );
  40.         if (count($deliveries) > 0) {
  41.             $orderNumbers array_values($deliveries->map(
  42.                 fn (DeliveryEntity $delivery) => $delivery->getOrder()->getOrderNumber(),
  43.             ));
  44.             $event->addError(new JsonApiError([
  45.                 'code' => 'PICKWARE_WMS__PICKING_PROCESS__PICKING_PROCESSES_EXIST_FOR_ORDERS_TO_BE_SHIPPED',
  46.                 'title' => 'Picking processes exist for orders to be shipped',
  47.                 'detail' => sprintf(
  48.                     'A picking process exists for the following orders: %s',
  49.                     implode(', '$orderNumbers),
  50.                 ),
  51.                 'meta' => [
  52.                     'orderNumbers' => $orderNumbers,
  53.                 ],
  54.             ]));
  55.         }
  56.     }
  57. }