src/EventSubscriber/Expediting/ShippingPlanCreationSubscriber.php line 28
<?php
namespace App\EventSubscriber\Expediting;
use App\Doctrine\Type\Scheduler\TaskStatus;
use App\Entity\Expediting\ShippingPlan;
use App\Entity\Sales\WorkOrder;
use App\Event\Expediting\ShippingPlanCreationEvent;
use App\Repository\Scheduler\TaskRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
readonly class ShippingPlanCreationSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
private TaskRepository $taskRepository,
) {}
public static function getSubscribedEvents(): array
{
return [
ShippingPlanCreationEvent::IDENTIFIER => ['process', 100],
];
}
public function process(ShippingPlanCreationEvent $event): void
{
$shippingPlan = $event->getShippingPlan();
// close work order planning task if any
$this->handleWorkOrderShipmentPlanningTask($shippingPlan);
$this->entityManager->flush();
}
private function handleWorkOrderShipmentPlanningTask(ShippingPlan $shippingPlan): void
{
$workOrder = $shippingPlan->getWorkOrder();
if (!$workOrder instanceof WorkOrder) {
return;
}
$shipmentPlanningTask = $this->taskRepository->getShipmentPlanningTask(workOrder: $workOrder, createIfNull: true);
$shipmentPlanningTask->setStatus(TaskStatus::COMPLETED);
$shipmentPlanningTask->setCompletedOn(new DateTime());
$this->entityManager->persist($shipmentPlanningTask);
}
}