Feat: Implement workflow for disposition document uploads
This commit is contained in:
@@ -11,6 +11,7 @@ use App\Service\Upload\UploadHandler;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Target;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
@@ -24,7 +25,8 @@ class DetailController extends AbstractController
|
||||
public function __construct(
|
||||
private readonly UploadHandler $uploadHandler,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly WorkflowInterface $dispositionStateMachine,
|
||||
#[Target('disposition')]
|
||||
private readonly WorkflowInterface $workflow,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
@@ -41,10 +43,10 @@ class DetailController extends AbstractController
|
||||
$uploadSession = $this->uploadHandler->getUploadSession();
|
||||
|
||||
if (0 < $uploadSession->getCount()) {
|
||||
if (true === $this->dispositionStateMachine->can($disposition, 'upload_contract')) {
|
||||
if (true === $this->workflow->can($disposition, 'upload_contract')) {
|
||||
$this->uploadContract($disposition, $uploadSession);
|
||||
}
|
||||
if (true === $this->dispositionStateMachine->can($disposition, 'upload_invoice')) {
|
||||
if (true === $this->workflow->can($disposition, 'upload_invoice')) {
|
||||
$this->uploadInvoice($disposition, $uploadSession);
|
||||
}
|
||||
}
|
||||
@@ -74,7 +76,7 @@ class DetailController extends AbstractController
|
||||
$upload->setStatus(Upload::STATUS_NEW);
|
||||
|
||||
$disposition->addDocument($upload);
|
||||
$this->dispositionStateMachine->apply($disposition, 'upload_contract');
|
||||
$this->workflow->apply($disposition, 'upload_contract');
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
@@ -101,7 +103,7 @@ class DetailController extends AbstractController
|
||||
$upload->setStatus(Upload::STATUS_NEW);
|
||||
|
||||
$disposition->addDocument($upload);
|
||||
$this->dispositionStateMachine->apply($disposition, 'upload_invoice');
|
||||
$this->workflow->apply($disposition, 'upload_invoice');
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventListener;
|
||||
|
||||
use App\Entity\Disposition;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Workflow\Event\GuardEvent;
|
||||
|
||||
class DispositionWorkflowGuardSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public function __construct(private readonly TranslatorInterface $translator)
|
||||
{}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
'workflow.disposition.guard.upload_contract' => ['guardUploadContract'],
|
||||
'workflow.disposition.guard.upload_invoice' => ['guardUploadInvoice'],
|
||||
];
|
||||
}
|
||||
|
||||
public function guardUploadContract(GuardEvent $event): void
|
||||
{
|
||||
/** @var Disposition $disposition */
|
||||
$disposition = $event->getSubject();
|
||||
$assignment = $disposition->getAssignment();
|
||||
$assignmentPeriod = $assignment->getTotalPeriod();
|
||||
$today = new \DateTimeImmutable();
|
||||
|
||||
$dueDateFrom = $disposition->getCreatedAt()->modify('+ 7days');
|
||||
$dueDateTo = $assignmentPeriod->end->modify('-1 day');
|
||||
|
||||
if ($dueDateTo < $today) {
|
||||
$message = $this
|
||||
->translator
|
||||
->trans('message.upload_contract.too_late', [
|
||||
'%date_from%' => $dueDateFrom->format('d.m.Y'),
|
||||
'%date_to%' => $dueDateTo->format('d.m.Y'),
|
||||
])
|
||||
;
|
||||
$event->setBlocked(true, $message);
|
||||
}
|
||||
}
|
||||
|
||||
public function guardUploadInvoice(GuardEvent $event): void
|
||||
{
|
||||
/** @var Disposition $disposition */
|
||||
$disposition = $event->getSubject();
|
||||
$assignment = $disposition->getAssignment();
|
||||
$destination = $assignment->getDestination();
|
||||
$today = new \DateTimeImmutable();
|
||||
|
||||
$earliestDate = $destination->getDateFrom();
|
||||
|
||||
if ($earliestDate > $today) {
|
||||
$message = $this
|
||||
->translator
|
||||
->trans('message.upload_invoice.too_early', [
|
||||
'%date%' => $earliestDate->format('d.m.Y'),
|
||||
])
|
||||
;
|
||||
$event->setBlocked(true, $message);
|
||||
}
|
||||
|
||||
$dueDate = $destination->getDateTo()->modify('+14 days');
|
||||
|
||||
if ($dueDate < $today) {
|
||||
$message = $this
|
||||
->translator
|
||||
->trans('message.upload_invoice.too_late', [
|
||||
'%date%' => $dueDate->format('d.m.Y'),
|
||||
])
|
||||
;
|
||||
$event->setBlocked(true, $message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user