src/EventSubscriber/StatusSubscriber.php line 87
<?phpnamespace App\EventSubscriber;use App\Entity\Logger\CronLogger;use App\Entity\Order;use App\Entity\Shipment;use App\Event\OrderStatusChangedEvent;use App\Event\ReceivingOrderStatusChangedEvent;use App\Event\ReturnOrderStatusChangedEvent;use App\Event\ShipmentStatusChangedEvent;use App\Service\InventoryService;use App\Service\Logger\CronLoggerService;use App\Service\OrderService;use App\Service\ReturnOrderService;use App\Service\ShipmentLabelService;use App\Service\ShipmentService;use App\Service\ScanOrderService;use Doctrine\ORM\EntityManagerInterface;use Exception;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use App\EventSubscriber\Traits\OrderStatusHandlerTrait;class StatusSubscriber implements EventSubscriberInterface{use OrderStatusHandlerTrait;/*** @param EntityManagerInterface $em* @param OrderService $orderService* @param ShipmentService $shipmentService* @param ShipmentLabelService $shipmentLabelService* @param InventoryService $inventoryService* @param ReturnOrderService $returnOrderService* @param ScanOrderService $orderScanningService* @param CronLoggerService $cronLoggerService*/public function __construct(private readonly EntityManagerInterface $em,private readonly OrderService $orderService,private readonly ShipmentService $shipmentService,private readonly ShipmentLabelService $shipmentLabelService,private readonly InventoryService $inventoryService,private readonly ReturnOrderService $returnOrderService,private readonly ScanOrderService $orderScanningService,private readonly CronLoggerService $cronLoggerService){}/*** @return array*/public static function getSubscribedEvents(): array{return [ReceivingOrderStatusChangedEvent::class => ['receivingOrderStatusChangedAction'],OrderStatusChangedEvent::class => ['orderStatusChangedAction'],ShipmentStatusChangedEvent::class => ['shipmentStatusChangedAction'],ReturnOrderStatusChangedEvent::class => ['returnOrderStatusChangedAction'],];}/*** @throws Exception*/public function receivingOrderStatusChangedAction(ReceivingOrderStatusChangedEvent $event){$receivingOrder = $event->getReceivingOrder();if ($receivingOrder->isArrivedStatus()) {foreach ($receivingOrder->getReceivingOrderItems() as $orderItem) {$this->inventoryService->refillProduct($orderItem->getProduct(),$orderItem->getQuantity());$this->orderService->activateNotEnoughReservationQuantityOrders($orderItem->getProduct());}}}/*** @noinspection PhpUnused* @throws Exception*/public function orderStatusChangedAction(OrderStatusChangedEvent $event): void{$order = $event->getOrder();if ($order->isPendingStatus()) {$this->inventoryService->orderProductsReservation($order);return;}if ($order->isProcessingStatus()) {$this->inventoryService->orderProductsReservation($order);$this->moveOrderToPickingStatus($order);return;}if ($order->isPickingStatus()) {$this->handleChangedOrderStatusToPicking($order);return;}if ($order->isCompletedStatus()) {$this->handleChangedOrderStatusToCompleted($order);return;}if ($order->isDeliveredStatus()) {$this->shipmentService->fillOrderDeliveredAt($order);foreach ($order->getShipments() as $shipment) {$this->shipmentService->changeStatus($shipment, Shipment::STATUS_DELIVERED);}return;}if ($order->isReturnedStatus()) {$this->handleChangedOrderStatusToReturned($order);return;}if ($order->isUnsuccessfulStatus()|| $order->isCanceledStatus()|| $order->isDuplicateStatus()|| $order->isInvalidDeliveryDataStatus()) {try {$this->inventoryService->cancelOrderProductsReservation($order);} catch (\Exception $exception) {$this->cronLoggerService->addLog(CronLogger::COMMAND_INVENTORY_UPDATED,"Order #". $order->getId(). ": ". $exception->getMessage(). ", stack trace: ". $exception->getTraceAsString(). ", \ndebug trace: ". json_encode(debug_backtrace()),CronLogger::STATUS_FAILED,$order->getShop(),);}}if ($order->isInvalidDeliveryDataStatus()&& $order->getPreviousStatus()&& $order->getPreviousStatus() === Order::STATUS_PICKING) {$this->rollBackAlreadyPickedOrder($order);}}/*** @throws Exception*/public function shipmentStatusChangedAction(ShipmentStatusChangedEvent $event){$shipment = $event->getShipment();$order = $shipment->getOrder();if ($shipment->isOnItsWayStatus()) {$this->shipmentService->createShipmentProducts($shipment);return;}if ($shipment->isAcceptedStatus()) {$this->shipmentService->fillApiAcceptedAt($order);return;}if ($shipment->isDeliveredStatus() && $order->isAllShipmentDelivered()) {$this->orderService->changeStatus($order, Order::STATUS_DELIVERED);return;}if ($shipment->isReturnedStatus() && $order->isAllShipmentReturned()) {$this->orderService->changeStatus($order, Order::STATUS_RETURNED);return;}if ($shipment->isUnsuccessfulStatus()) {$this->orderService->changeStatus($order, Order::STATUS_UNSUCCESSFUL);}}/*** @throws Exception*/public function returnOrderStatusChangedAction(ReturnOrderStatusChangedEvent $event){$returnOrder = $event->getReturnOrder();if ($returnOrder->isConfirmedStatus()) {foreach ($returnOrder->getReturnOrderProducts() as $returnOrderProduct) {$this->inventoryService->refillProduct($returnOrderProduct->getProduct(),$returnOrderProduct->getQuantity());$this->orderService->activateNotEnoughReservationQuantityOrders($returnOrderProduct->getProduct());}}}}