Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/config/routing/admin_accounting_bank_accounts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ admin_accounting_bank_accounts_list:
path: /list
defaults:
_controller: AppBundle\Controller\Admin\Accounting\BankAccounts\ListStatementAction


admin_accounting_bank_accounts_download:
path: /download-statements
defaults:
_controller: AppBundle\Controller\Admin\Accounting\BankAccounts\DownloadStatementAction
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ext-libxml": "*",
"ext-openssl": "*",
"ext-pdo": "*",
"ext-zip": "*",
"algolia/algoliasearch-client-php": "^3.4",
"beberlei/assert": "^2.9",
"captioning/captioning": "^2.6",
Expand Down
7 changes: 3 additions & 4 deletions htdocs/pages/administration/compta_banque.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,12 @@
throw new RuntimeException("Impossible to open the Zip archive.");
} else {
for ($month = 1; $month <= 12; $month++) {
$searchDir = sprintf('%d%02d', $year, $month);
$zipDir = sprintf('%d%02d', $year, $month);
$directory = sprintf('%d%02d', $year, $month);
$options = [
'add_path' => 'afup_justificatifs-' . $year . '/' . $zipDir . '/',
'add_path' => 'afup_justificatifs-' . $year . '/' . $directory . '/',
'remove_all_path' => true,
];
$zip->addGlob(AFUP_CHEMIN_RACINE . '/uploads/' . $searchDir . '/*.*', 0, $options);
$zip->addGlob(AFUP_CHEMIN_RACINE . '/uploads/' . $directory . '/*.*', 0, $options);
}
$zip->close();

Expand Down
2 changes: 1 addition & 1 deletion htdocs/templates/administration/compta_journal.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h2>Journal</h2>
<i class="icon file"></i>
Exporter la période en CSV
</a>
<a href="index.php?page=compta_banque&amp;action=download_attachments&amp;id_periode={$smarty.get.id_periode|default:''}" class="item">
<a href="/admin/accounting/bank-accounts/download-statements?periodId={$smarty.get.id_periode|default:''}" class="item">
<i class="icon file zip"></i>
Télécharger les justificatifs groupés par mois
</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace AppBundle\Controller\Admin\Accounting\BankAccounts;

use AppBundle\Accounting\Model\Repository\InvoicingPeriodRepository;
use DateInterval;
use DatePeriod;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use ZipArchive;

class DownloadStatementAction extends AbstractController
{
public function __construct(
private readonly InvoicingPeriodRepository $invoicingPeriodRepository,
#[Autowire('%kernel.project_dir%/../htdocs/uploads/')] private readonly string $uploadDir,
) {}

public function __invoke(Request $request): Response
{
$periodId = $request->query->has('periodId') && $request->query->get('periodId') ? (int) $request->query->get('periodId') : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$periodId = $request->query->has('periodId') && $request->query->get('periodId') ? (int) $request->query->get('periodId') : null;
$periodId = $request->query->getInt('periodId', null);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ce n'est pas possible, getInt() doit forcément returner un int. Je peux au mieux utiliser getInt() et utiliser 0 en valeur par défaut mais ça va impliquer de modifier la fonction getCurrentPeriod pour gérer null et 0.

$period = $this->invoicingPeriodRepository->getCurrentPeriod($periodId);
try {
$year = $period->getStartDate()->format('Y');

// Create the zip
$zipFilename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'afup_justificatifs-' . $year . '.zip';
$zip = new ZipArchive();
$state = $zip->open($zipFilename, ZipArchive::CREATE);
if ($state !== true) {
throw new RuntimeException("Impossible to open the Zip archive.");
} else {
$datePeriod = new DatePeriod($period->getStartDate(), new DateInterval('P1M'), $period->getEndDate());
/** @var \DateTime $month */
foreach ($datePeriod as $month) {
$searchDir = $month->format('Ym');
$zipDir = $month->format('Ym');
$options = [
'add_path' => 'afup_justificatifs-' . $year . '/' . $zipDir . '/',
'remove_all_path' => true,
];
$zip->addGlob($this->uploadDir . $searchDir . '/*.*', 0, $options);
}
$zip->close();

$response = new BinaryFileResponse($zipFilename, Response::HTTP_OK, [
'Content-Type' => 'application/zip',
'Content-Transfer-Encoding' => 'Binary',
'Content-Disposition' => 'attachment; filename="' . basename($zipFilename) . '"',
'Cache-Control' => 'max-age=0',
], false);
$response->deleteFileAfterSend(true);

return $response;
}
} catch (\Exception $exception) {
return new Response(null, Response::HTTP_BAD_REQUEST, [
'X-Info' => $exception->getMessage(),
]);
}
}
}
2 changes: 1 addition & 1 deletion tests/behat/features/Admin/Tresorerie/Journal.feature
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ Feature: Administration - Trésorerie - Journal
Given I am logged in as admin and on the Administration
When I follow "Journal"
And I follow "Télécharger les justificatifs groupés par mois"
Then the response header "Content-disposition" should match '#filename="afup_justificatifs-(.*).zip"#'
Then the response header "Content-Disposition" should match '#filename="afup_justificatifs-(.*).zip"#'