154 lines
5.6 KiB
PHP
154 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Teamer\Disposition;
|
|
|
|
use App\Controller\Traits\ReturnUrlTrait;
|
|
use App\Entity\Disposition;
|
|
use App\Entity\Upload;
|
|
use App\Entity\User;
|
|
use App\Event\DocumentUploadedEvent;
|
|
use App\Form\TeamerDispositionType;
|
|
use App\Model\UploadSessionDto;
|
|
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\DependencyInjection\Attribute\Target;
|
|
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\Component\Workflow\WorkflowInterface;
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
|
|
|
class DetailController extends AbstractController
|
|
{
|
|
use ReturnUrlTrait;
|
|
|
|
public function __construct(
|
|
private readonly UploadHandler $uploadHandler,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
#[Target('disposition')]
|
|
private readonly WorkflowInterface $workflow,
|
|
private readonly EventDispatcherInterface $eventDispatcher,
|
|
private readonly LoggerInterface $logger,
|
|
private readonly FeatureManagerInterface $featureManager,
|
|
) {
|
|
}
|
|
|
|
#[Route('/teamer/disposition/detail/{uuid}', name: 'app_teamer_disposition_detail')]
|
|
#[IsGranted('ROLE_TEAMER')]
|
|
#[IsGranted('VIEW', subject: 'disposition')]
|
|
public function index(Disposition $disposition, Request $request): Response
|
|
{
|
|
$isUploadContract = $this->workflow->can($disposition, 'upload_contract');
|
|
$isUploadInvoice = $this->workflow->can($disposition, 'upload_invoice');
|
|
|
|
// Create dummy form without fields to generate POST requests
|
|
$form = $this->createForm(TeamerDispositionType::class, $disposition, [
|
|
'enable_pickup' => $isUploadContract,
|
|
'enable_invoice_confirmation' => $isUploadInvoice,
|
|
]);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$uploadSession = $this->uploadHandler->getUploadSession();
|
|
|
|
if (0 < $uploadSession->getCount()) {
|
|
if (true === $isUploadContract) {
|
|
$this->uploadContract($disposition, $uploadSession);
|
|
}
|
|
if (true === $isUploadInvoice) {
|
|
$this->uploadInvoice($disposition, $uploadSession);
|
|
}
|
|
}
|
|
|
|
return $this->redirectToRoute('app_teamer_disposition_detail', [
|
|
'uuid' => $disposition->getUuid(),
|
|
'r' => $request->query->get('r'),
|
|
]);
|
|
}
|
|
|
|
return $this->render('teamer/disposition/detail.html.twig', [
|
|
'disposition' => $disposition,
|
|
'form' => $form->createView(),
|
|
'returnUrl' => $this->getReturnUrl($request, 'app_teamer_index'),
|
|
]);
|
|
}
|
|
|
|
private function uploadContract(Disposition $disposition, UploadSessionDto $uploadSession): void
|
|
{
|
|
if (null !== $existingUpload = $disposition->getDocumentByType(Upload::TYPE_CONTRACT)) {
|
|
$this->entityManager->remove($existingUpload);
|
|
}
|
|
|
|
/** @var User $user */
|
|
$user = $this->getUser();
|
|
|
|
$upload = Upload::fromUploadDto(
|
|
$uploadSession->getUploadsByType(Upload::TYPE_CONTRACT)->first(),
|
|
$user,
|
|
Upload::TYPE_CONTRACT
|
|
);
|
|
$upload->setStatus(Upload::STATUS_NEW);
|
|
|
|
$disposition->addDocument($upload);
|
|
$this->workflow->apply($disposition, 'upload_contract');
|
|
|
|
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_CONTRACT, $uploadSession);
|
|
$this->uploadHandler->removeUploadSessionUploadsByType(Upload::TYPE_CONTRACT);
|
|
|
|
if (true === $this->featureManager->isActive('sanitize_uploads')) {
|
|
$this->uploadHandler->ensurePdf($upload);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Der Honorarvertrag wurde hochgeladen');
|
|
|
|
$this->logger->info('Upload contract', [
|
|
'user' => $user->getUserIdentifier(),
|
|
]);
|
|
|
|
$this->eventDispatcher->dispatch(new DocumentUploadedEvent($upload), DocumentUploadedEvent::NAME);
|
|
}
|
|
|
|
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->getUploadsByType(Upload::TYPE_INVOICE)->first(),
|
|
$user,
|
|
Upload::TYPE_INVOICE
|
|
);
|
|
$upload->setStatus(Upload::STATUS_NEW);
|
|
|
|
$disposition->addDocument($upload);
|
|
$this->workflow->apply($disposition, 'upload_invoice');
|
|
|
|
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_INVOICE, $uploadSession);
|
|
$this->uploadHandler->removeUploadSessionUploadsByType(Upload::TYPE_INVOICE);
|
|
|
|
if (true === $this->featureManager->isActive('sanitize_uploads')) {
|
|
$this->uploadHandler->ensurePdf($upload);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Die Honornote wurde hochgeladen');
|
|
|
|
$this->logger->info('Upload invoice', [
|
|
'user' => $user->getUserIdentifier(),
|
|
]);
|
|
|
|
$this->eventDispatcher->dispatch(new DocumentUploadedEvent($upload), DocumentUploadedEvent::NAME);
|
|
}
|
|
}
|