WIP: Implement teamer document upload handling

This commit is contained in:
Björn Fromme
2023-10-12 19:50:08 +02:00
parent bfd18b21ce
commit 6df725edc4
25 changed files with 611 additions and 165 deletions
@@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Workflow\WorkflowInterface;
class DetailController extends AbstractController
{
@@ -23,6 +24,7 @@ class DetailController extends AbstractController
public function __construct(
private readonly UploadHandler $uploadHandler,
private readonly EntityManagerInterface $entityManager,
private readonly WorkflowInterface $dispositionStateMachine,
private readonly LoggerInterface $logger
) {
}
@@ -31,16 +33,22 @@ class DetailController extends AbstractController
#[IsGranted('VIEW', subject: 'disposition')]
public function index(Disposition $disposition, Request $request): Response
{
// Handle upload independently from form submission to avoid issues with failing validation
$uploadSession = $this->uploadHandler->getUploadSession();
if (true === $request->isMethod('POST') && 0 < $uploadSession->getCount()) {
$this->updateContract($disposition, $uploadSession);
}
// Create dummy form without fields to generate POST requests
$form = $this->createFormBuilder()->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$uploadSession = $this->uploadHandler->getUploadSession();
if (0 < $uploadSession->getCount()) {
if (true === $this->dispositionStateMachine->can($disposition, 'upload_contract')) {
$this->uploadContract($disposition, $uploadSession);
}
if (true === $this->dispositionStateMachine->can($disposition, 'upload_invoice')) {
$this->uploadInvoice($disposition, $uploadSession);
}
}
return $this->redirectToRoute('app_teamer_disposition_detail', [
'uuid' => $disposition->getUuid(),
]);
@@ -48,28 +56,61 @@ class DetailController extends AbstractController
return $this->render('teamer/disposition/detail.html.twig', [
'disposition' => $disposition,
'returnUrl' => $this->getReturnUrl($request, 'app_teamer_index'),
'form' => $form,
'returnUrl' => $this->getReturnUrl($request, 'app_teamer_index'),
]);
}
private function updateContract(Disposition $disposition, UploadSessionDto $uploadSession): void
private function uploadContract(Disposition $disposition, UploadSessionDto $uploadSession): void
{
if (null !== $existingUpload = $disposition->getContractPdf()) {
if (null !== $existingUpload = $disposition->getDocumentByType(Upload::TYPE_CONTRACT)) {
$this->entityManager->remove($existingUpload);
}
/** @var User $user */
$user = $this->getUser();
$upload = Upload::fromUploadDto($uploadSession->getUploads()->first(), $user, Upload::TYPE_CONTRACT);
$disposition->setContractPdf($upload);
$upload->setStatus(Upload::STATUS_NEW);
$disposition->addDocument($upload);
$this->dispositionStateMachine->apply($disposition, 'upload_contract');
$this->entityManager->flush();
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_CONTRACT, $uploadSession);
$this->uploadHandler->destroyUploadSession();
$this->addFlash('success', 'Der Honorarvertrag wurde hochgeladen');
$this->logger->info('Update contract', [
$this->logger->info('Upload contract', [
'user' => $user->getUserIdentifier(),
]);
}
private function uploadInvoice(Disposition $disposition, UploadSessionDto $uploadSession): void
{
if (null !== $existingUpload = $disposition->getDocumentByType(Upload::TYPE_INVOICE)) {
$this->entityManager->remove($existingUpload);
}
/** @var User $user */
$user = $this->getUser();
$upload = Upload::fromUploadDto($uploadSession->getUploads()->first(), $user, Upload::TYPE_INVOICE);
$upload->setStatus(Upload::STATUS_NEW);
$disposition->addDocument($upload);
$this->dispositionStateMachine->apply($disposition, 'upload_invoice');
$this->entityManager->flush();
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_INVOICE, $uploadSession);
$this->uploadHandler->destroyUploadSession();
$this->addFlash('success', 'Die Honornote wurde hochgeladen');
$this->logger->info('Upload invoice', [
'user' => $user->getUserIdentifier(),
]);
}