Files
myep-team/src/Controller/Admin/Document/CheckController.php
T

112 lines
4.3 KiB
PHP

<?php
namespace App\Controller\Admin\Document;
use App\Entity\Upload;
use App\Event\DocumentRejectedEvent;
use App\Form\DocumentCheckType;
use App\Model\AjaxModalResponseDto;
use App\Model\DocumentCheckDto;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Workflow\WorkflowInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class CheckController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
#[Target('disposition')]
private readonly WorkflowInterface $workflow,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly LoggerInterface $logger
) {
}
#[Route('/admin/document/check/{uuid}', name: 'app_admin_document_check')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
#[IsGranted('CHECK', subject: 'document')]
public function index(Upload $document, Request $request): JsonResponse
{
$response = new AjaxModalResponseDto();
$formData = new DocumentCheckDto($document);
$formAction = $this->generateUrl('app_admin_document_check', ['uuid' => $document->getUuid()]);
$form = $this->createForm(
DocumentCheckType::class,
$formData,
[
'action' => $formAction,
'ajax_submit' => true,
'document_type' => $document->getType(),
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
switch ($formData->getStatus()) {
case Upload::STATUS_CHECKED:
$this->confirmDocument($document, 'confirm_contract', Upload::STATUS_CHECKED);
break;
case Upload::STATUS_PAID:
$this->confirmDocument($document, 'confirm_invoice', Upload::STATUS_PAID);
break;
case Upload::STATUS_REJECTED:
$this->rejectDocument($document, $formData->getComment());
$this->eventDispatcher->dispatch(new DocumentRejectedEvent($formData), DocumentRejectedEvent::NAME);
}
$returnUrl = $this->generateUrl('app_admin_document_index');
$response->setCloseAndRedirect($returnUrl);
} else {
$response->setContent($this->renderView('admin/document/check.html.twig', [
'document' => $document,
'form' => $form,
]));
}
return $this->json($response);
}
private function rejectDocument(Upload $document, string $comment = null): void
{
$transition = Upload::TYPE_CONTRACT === $document->getType() ? 'reject_contract' : 'reject_invoice';
$this->workflow->apply($document->getDisposition(), $transition);
$document
->setStatus(Upload::STATUS_REJECTED)
->setComment($comment)
;
$this->entityManager->flush();
$this->addFlash('success', 'Das Dokument wurde abgelehnt');
$this->logger->info('Reject document', [
'document' => $document->getOriginalFilename(),
]);
}
private function confirmDocument(Upload $document, string $transition, string $status): void
{
if (true === $this->workflow->can($document->getDisposition(), $transition)) {
$document->setStatus($status);
$this->workflow->apply($document->getDisposition(), $transition);
$this->entityManager->flush();
$this->addFlash('success', 'Das Dokument wurde bestätigt');
$this->logger->info('Confirm document', [
'document' => $document->getOriginalFilename(),
]);
} else {
$blockers = $this->workflow->buildTransitionBlockerList($document->getDisposition(), $transition);
foreach ($blockers as $blocker) {
$this->addFlash('error', $blocker->getMessage());
}
}
}
}