feat: download ZIP archive of invoices with status 'paid'
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Administrative\Document;
|
||||
|
||||
use App\Entity\Upload;
|
||||
use App\Repository\UploadRepository;
|
||||
use App\Service\Upload\UploadHandler;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\HeaderUtils;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
use ZipStream\Exception\FileNotFoundException;
|
||||
use ZipStream\Exception\FileNotReadableException;
|
||||
use ZipStream\ZipStream;
|
||||
|
||||
class BatchDownloadController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UploadRepository $uploadRepository,
|
||||
private readonly UploadHandler $uploadHandler,
|
||||
private readonly EntityManagerInterface $entityManager
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route(
|
||||
path: '/administrative/document/batch-download/{type}',
|
||||
name: 'app_administrative_document_batch_download',
|
||||
defaults: ['type' => Upload::TYPE_INVOICE]
|
||||
)]
|
||||
#[IsGranted('ROLE_ADMINISTRATIVE')]
|
||||
public function index(string $type, Request $request): Response
|
||||
{
|
||||
$status = $request->query->get('status', Upload::STATUS_PAID);
|
||||
|
||||
$uploads = $this
|
||||
->uploadRepository
|
||||
->getBatchDownloadList($type, $status)
|
||||
;
|
||||
|
||||
if (0 === count($uploads)) {
|
||||
$this->addFlash('error', 'Keine Dokumente vorhanden');
|
||||
|
||||
return $this->redirectToRoute('app_administrative_document_index');
|
||||
}
|
||||
|
||||
$zipFilename = sprintf('Honorarnoten_%s.zip', date('YmdHi'));
|
||||
|
||||
$response = new StreamedResponse(function () use ($uploads, $zipFilename) {
|
||||
$zip = new ZipStream(
|
||||
defaultEnableZeroHeader: true,
|
||||
outputName: $zipFilename,
|
||||
contentType: 'application/octet-stream'
|
||||
);
|
||||
|
||||
foreach ($uploads as $upload) {
|
||||
/** @var Upload $upload */
|
||||
$filename = $upload->getOriginalFilename();
|
||||
$filepath = $this->uploadHandler->getUploadFilepath($upload);
|
||||
|
||||
try {
|
||||
$zip->addFileFromPath(fileName: $filename, path: $filepath);
|
||||
$upload
|
||||
->setDownloaded()
|
||||
->setDownloadedBy($this->getUser()->getUserIdentifier())
|
||||
;
|
||||
} catch (FileNotFoundException $e) {
|
||||
} catch (FileNotReadableException $e) {
|
||||
}
|
||||
}
|
||||
$zip->finish();
|
||||
$this->entityManager->flush();
|
||||
});
|
||||
|
||||
$disposition = HeaderUtils::makeDisposition('attachment', $zipFilename, md5($zipFilename));
|
||||
$response->headers->set('Content-Disposition', $disposition);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Controller\Common;
|
||||
|
||||
use App\Entity\Upload;
|
||||
use App\Service\Upload\UploadHandler;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
|
||||
@@ -17,6 +18,7 @@ class DownloadController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UploadHandler $uploadHandler,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
@@ -31,6 +33,13 @@ class DownloadController extends AbstractController
|
||||
$inline = (bool) $request->get('inline');
|
||||
$disposition = $inline ? ResponseHeaderBag::DISPOSITION_INLINE : ResponseHeaderBag::DISPOSITION_ATTACHMENT;
|
||||
|
||||
// mark file downloaded
|
||||
$upload
|
||||
->setDownloaded()
|
||||
->setDownloadedBy($this->getUser()->getUserIdentifier())
|
||||
;
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->logger->info('Download file', [
|
||||
'file_id' => $upload->getId(),
|
||||
'file_filename' => $upload->getOriginalFilename(),
|
||||
|
||||
@@ -66,6 +66,12 @@ class Upload implements BlameableEntityInterface, TimestampableEntityInterface
|
||||
#[ORM\Column(type: Types::TEXT, nullable: true)]
|
||||
private ?string $comment = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)]
|
||||
private ?\DateTimeImmutable $downloadedAt = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?string $downloadedBy = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->uuid = Uuid::v4();
|
||||
@@ -242,4 +248,40 @@ class Upload implements BlameableEntityInterface, TimestampableEntityInterface
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDownloadedAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->downloadedAt;
|
||||
}
|
||||
|
||||
public function setDownloadedAt(?\DateTimeImmutable $downloadedAt): static
|
||||
{
|
||||
$this->downloadedAt = $downloadedAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isDownloaded(): bool
|
||||
{
|
||||
return null !== $this->downloadedAt;
|
||||
}
|
||||
|
||||
public function setDownloaded(): static
|
||||
{
|
||||
$this->downloadedAt = new \DateTimeImmutable();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDownloadedBy(): ?string
|
||||
{
|
||||
return $this->downloadedBy;
|
||||
}
|
||||
|
||||
public function setDownloadedBy(?string $downloadedBy): static
|
||||
{
|
||||
$this->downloadedBy = $downloadedBy;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Repository\Traits\QueryHelperTrait;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use setasign\Fpdi\PdfParser\CrossReference\AbstractReader;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Upload>
|
||||
@@ -79,6 +80,25 @@ class UploadRepository extends ServiceEntityRepository
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getBatchDownloadList(
|
||||
string $type = Upload::TYPE_INVOICE,
|
||||
string $status = Upload::STATUS_PAID
|
||||
): array {
|
||||
$qb = $this->createQueryBuilder('upload');
|
||||
|
||||
return $qb->where(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->eq('upload.type', ':type'),
|
||||
$qb->expr()->eq('upload.status', ':status'),
|
||||
$qb->expr()->isNull('upload.downloadedAt'),
|
||||
))
|
||||
->setParameter('type', $type)
|
||||
->setParameter('status', $status)
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
}
|
||||
|
||||
public function getUploadListQuery(DocumentFilterDto $filterDto): Query
|
||||
{
|
||||
$qb = $this->createQueryBuilder('upload');
|
||||
|
||||
Reference in New Issue
Block a user