Files
myep-team/src/Controller/Administrative/Disposition/DocumentUploadController.php
T
2026-09-09 11:10:57 +02:00

136 lines
4.9 KiB
PHP

<?php
namespace App\Controller\Administrative\Disposition;
use App\Entity\Disposition;
use App\Entity\Upload;
use App\Entity\User;
use App\Event\DocumentConfirmedEvent;
use App\Event\DocumentUploadedEvent;
use App\Htmx\HxRedirectResponse;
use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface;
use Flagception\Manager\FeatureManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class DocumentUploadController extends AbstractController
{
public function __construct(
private readonly UploadHandler $uploadHandler,
private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly LoggerInterface $logger,
private readonly FeatureManagerInterface $featureManager,
) {
}
#[Route(
path: '/administrative/disposition/document-upload/{uuid}/{type}',
name: 'app_administrative_disposition_document_upload'
)]
#[IsGranted('ADMIN_DOCUMENT_UPLOAD', subject: 'disposition')]
public function index(Disposition $disposition, string $type, Request $request): Response
{
if (false === in_array($type, [Upload::TYPE_CONTRACT, Upload::TYPE_INVOICE], true)) {
throw $this->createNotFoundException('Unsupported document type');
}
$teamer = $disposition->getTeamer();
$modalTitle = Upload::TYPE_CONTRACT === $type
? sprintf('Honorarvertrag für %s hochladen', $teamer)
: sprintf('Honorarnote für %s hochladen', $teamer);
$form = $this->createFormBuilder()->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->processUpload($disposition, $type);
$this->addFlash(
'success',
Upload::TYPE_CONTRACT === $type
? 'Der Honorarvertrag wurde hochgeladen'
: 'Die Honorarnote wurde hochgeladen'
);
return new HxRedirectResponse($this->generateUrl('app_administrative_assignment_detail', [
'uuid' => $disposition->getAssignment()->getUuid(),
'r' => $request->query->get('r'),
]));
}
return $this->render('administrative/disposition/modal_document_upload.html.twig', [
'disposition' => $disposition,
'modalTitle' => $modalTitle,
'endpointUpload' => Upload::TYPE_CONTRACT === $type
? '_uploader_upload_contract'
: '_uploader_upload_invoice',
'form' => $form->createView(),
]);
}
private function processUpload(Disposition $disposition, string $type): void
{
if (null !== $existingUpload = $disposition->getDocumentByType($type)) {
$this->entityManager->remove($existingUpload);
}
/** @var User $user */
$user = $this->getUser();
$teamer = $disposition->getTeamer();
$uploadSession = $this->uploadHandler->getUploadSession();
$upload = Upload::fromUploadDto(
$uploadSession->getUploadsByType($type)->first(),
$teamer->getUser(),
$type
);
if (Upload::TYPE_CONTRACT === $type) {
$upload
->setStatus(Upload::STATUS_CHECKED)
->setApprovedAt(new \DateTimeImmutable())
->setApprovedBy($user->getInitials())
;
$disposition->setStatus(Disposition::STATUS_CONFIRMED);
} else {
$upload->setStatus(Upload::STATUS_NEW);
$disposition->setStatus(Disposition::STATUS_CHECKING_INVOICE);
}
$disposition->addDocument($upload);
$this->entityManager->flush();
$this->uploadHandler->moveUploadSessionFilesFromOrphanage($type, $uploadSession);
$this->uploadHandler->removeUploadSessionUploadsByType($type);
if (true === $this->featureManager->isActive('sanitize_uploads')) {
$this->uploadHandler->ensurePdf($upload);
}
$this->eventDispatcher->dispatch(new DocumentUploadedEvent($upload), DocumentUploadedEvent::NAME);
if (Upload::TYPE_CONTRACT === $type) {
$this->eventDispatcher->dispatch(new DocumentConfirmedEvent($upload), DocumentConfirmedEvent::NAME);
}
$this->logger->info('Administrative document upload for teamer', [
'teamer_id' => $teamer->getId(),
'teamer_name' => $teamer->getFullName(),
'disposition_id' => $disposition->getId(),
'document_id' => $upload->getId(),
'upload_type' => $type,
]);
}
}