Feat: Implement disposition document handling for admins

This commit is contained in:
Björn Fromme
2023-10-13 17:20:13 +02:00
parent 789249cba7
commit 194cd4e59d
12 changed files with 282 additions and 39 deletions
@@ -0,0 +1,107 @@
<?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')]
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);
$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): void
{
$transition = Upload::TYPE_CONTRACT === $document->getType() ? 'reject_contract' : 'reject_invoice';
$this->workflow->apply($document->getDisposition(), $transition);
$document->setStatus(Upload::STATUS_REJECTED);
$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());
}
}
}
}
@@ -1,31 +0,0 @@
<?php
namespace App\Controller\Admin\Document;
use App\Entity\Upload;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class DeleteController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger
)
{
}
#[Route('/admin/document/delete/{uuid}', name: 'app_admin_document_delete')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Upload $document): Response
{
$this->entityManager->remove($document);
$this->entityManager->flush();
return $this->render('');
}
}
+1 -2
View File
@@ -22,10 +22,9 @@ class Upload implements BlameableEntityInterface, TimestampableEntityInterface
public const TYPE_DOCUMENT = 'document';
public const STATUS_NEW = 'new';
public const CHECKING = 'checking';
public const STATUS_CHECKED = 'checked';
public const STATUS_PAID = 'paid';
public const STATUS_ERROR = 'error';
public const STATUS_REJECTED = 'rejected';
#[ORM\Id]
#[ORM\GeneratedValue]
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace App\Event;
use App\Entity\Upload;
use App\Model\DocumentCheckDto;
use Symfony\Contracts\EventDispatcher\Event;
class DocumentRejectedEvent extends Event
{
public const NAME = 'document.rejected';
private Upload $document;
private ?string $comment;
public function __construct(DocumentCheckDto $documentCheckDto)
{
$this->document = $documentCheckDto->getUpload();
$this->comment = $documentCheckDto->getComment();
}
public function getDocument(): Upload
{
return $this->document;
}
public function getComment(): ?string
{
return $this->comment;
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace App\Form;
use App\Entity\Upload;
use App\Model\DocumentCheckDto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DocumentCheckType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('status', ChoiceType::class, [
'label' => 'neuer Status',
'choices' => $options['status_choices'][$options['document_type']],
])
->add('comment', TextareaType::class, [
'label' => 'Kommentar/Begründung',
'required' => false,
'attr' => [
'rows' => 3,
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'data_class' => DocumentCheckDto::class,
'status_choices' => [
Upload::TYPE_CONTRACT => [
'bestätigt' => Upload::STATUS_CHECKED,
'abgelehnt' => Upload::STATUS_REJECTED,
],
Upload::TYPE_INVOICE => [
'bezahlt' => Upload::STATUS_PAID,
'abgelehnt' => Upload::STATUS_REJECTED,
],
],
])
->setRequired(['document_type'])
->setAllowedTypes('document_type', 'string')
->setAllowedValues('document_type', [Upload::TYPE_INVOICE, Upload::TYPE_CONTRACT])
;
}
}
+7
View File
@@ -49,6 +49,13 @@ class TeamerMenuBuilder extends AbstractMenuBuilder
[
'route' => 'app_teamer_bookmark_index',
'title' => 'Merkliste',
'children' => [
[
'route' => 'app_teamer_assignment',
'title' => 'Einsatzdetails',
'routeParameters' => $this->getDefaultRouteParameters('uuid'),
]
],
],
[
'route' => 'app_teamer_application_index',
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace App\Model;
use App\Entity\Upload;
class DocumentCheckDto
{
private Upload $upload;
private ?string $status = null;
private ?string $comment = null;
public function __construct(Upload $upload)
{
$this->upload = $upload;
}
public function getUpload(): Upload
{
return $this->upload;
}
public function getStatus(): string
{
return $this->status;
}
public function setStatus(string $status): static
{
$this->status = $status;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): static
{
$this->comment = $comment;
return $this;
}
}
+1 -1
View File
@@ -103,7 +103,7 @@ class AppRuntime implements RuntimeExtensionInterface
$class = 'upload-status-badge ';
$class .= match($status) {
Upload::STATUS_NEW => 'upload-status-badge--new',
Upload::STATUS_ERROR => 'upload-status-badge--error',
Upload::STATUS_REJECTED => 'upload-status-badge--error',
Upload::STATUS_PAID => 'upload-status-badge--paid',
Upload::STATUS_CHECKED => 'upload-status-badge--checked',
default => 'upload-status-badge--default',