src/EventSubscriber/Expediting/ExpeditablesReadinessWatcher.php line 63
<?php
namespace App\EventSubscriber\Expediting;
use App\Doctrine\Type\Expediting\ExpeditableStatus;
use App\Doctrine\Type\Sales\WorkOrderStatus;
use App\Entity\Sales\WorkOrder;
use App\Event\Expediting\FreightDatasheetImportEvent;
use App\Event\Expediting\FreightDocumentUploadEvent;
use App\Event\Sales\WorkOrderApprovalEvent;
use App\Event\Sales\WorkOrderCancelationEvent;
use App\Event\Sales\WorkOrderReopeningEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
readonly class ExpeditablesReadinessWatcher implements EventSubscriberInterface
{
use ExpeditablesStatusUpdaterTrait;
use FreightDocumentUploadCheckerTrait;
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher,
) {}
public static function getSubscribedEvents(): array
{
return [
FreightDatasheetImportEvent::IDENTIFIER => ['handleDatasheetImport', -10],
FreightDocumentUploadEvent::IDENTIFIER => ['handleDocumentUpload', -10],
WorkOrderApprovalEvent::IDENTIFIER => ['handleApproval', -10],
WorkOrderReopeningEvent::IDENTIFIER => ['handleReopening', -10],
WorkOrderCancelationEvent::IDENTIFIER => ['handleCancelation', -200],
];
}
public function handleDatasheetImport(FreightDatasheetImportEvent $event): void
{
/** @var WorkOrder $workOrder */
$workOrder = $this->entityManager->getRepository(WorkOrder::class)->findOneBy([
'reference' => $event->getDatasheet()->workOrderReference,
]);
$this->process($workOrder);
}
public function handleDocumentUpload(FreightDocumentUploadEvent $event): void
{
$this->process($event->getFreight()->getWorkOrder());
}
public function handleApproval(WorkOrderApprovalEvent $event): void
{
$this->process($event->getWorkOrder());
}
public function handleReopening(WorkOrderReopeningEvent $event): void
{
$this->process($event->getWorkOrder());
}
public function handleCancelation(WorkOrderCancelationEvent $event): void
{
$this->updateExpeditables(packages: $event->getWorkOrder()->getPackages(), status: ExpeditableStatus::CANCELED);
}
private function process(WorkOrder $workOrder): void
{
$hasAuthorization = $workOrder->getStatus() === WorkOrderStatus::APPROVED;
$hasDocuments = true;
foreach ($workOrder->getFreights() as $freight) {
if (!$this->freightHasExpeditingDocuments($freight)) {
$hasDocuments = false;
break;
}
}
if ($hasAuthorization && $hasDocuments) {
$this->updateExpeditables(packages: $workOrder->getPackages(), status: ExpeditableStatus::READY_FOR_PICKUP);
} elseif ($hasAuthorization) {
$this->updateExpeditables(packages: $workOrder->getPackages(), status: ExpeditableStatus::AWAITING_DOCUMENTS);
} elseif ($hasDocuments) {
$this->updateExpeditables(packages: $workOrder->getPackages(), status: ExpeditableStatus::AWAITING_AUTHORIZATION);
}
}
}