diff --git a/app/config/routing/admin_accounting_bank_accounts.yml b/app/config/routing/admin_accounting_bank_accounts.yml index 2c0defba6..9d8b2fba9 100644 --- a/app/config/routing/admin_accounting_bank_accounts.yml +++ b/app/config/routing/admin_accounting_bank_accounts.yml @@ -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 diff --git a/composer.json b/composer.json index e94f8b041..c91fbc642 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/htdocs/pages/administration/compta_banque.php b/htdocs/pages/administration/compta_banque.php index 1c91aeb7f..185bc7b3b 100644 --- a/htdocs/pages/administration/compta_banque.php +++ b/htdocs/pages/administration/compta_banque.php @@ -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(); diff --git a/htdocs/templates/administration/compta_journal.html b/htdocs/templates/administration/compta_journal.html index ba4adf820..b67d280e3 100644 --- a/htdocs/templates/administration/compta_journal.html +++ b/htdocs/templates/administration/compta_journal.html @@ -59,7 +59,7 @@

Journal

Exporter la période en CSV - + Télécharger les justificatifs groupés par mois diff --git a/sources/AppBundle/Controller/Admin/Accounting/BankAccounts/DownloadStatementAction.php b/sources/AppBundle/Controller/Admin/Accounting/BankAccounts/DownloadStatementAction.php new file mode 100644 index 000000000..b1daf1d7e --- /dev/null +++ b/sources/AppBundle/Controller/Admin/Accounting/BankAccounts/DownloadStatementAction.php @@ -0,0 +1,68 @@ +query->has('periodId') && $request->query->get('periodId') ? (int) $request->query->get('periodId') : null; + $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(), + ]); + } + } +} diff --git a/tests/behat/features/Admin/Tresorerie/Journal.feature b/tests/behat/features/Admin/Tresorerie/Journal.feature index 7e7bf4ccf..e87d82f42 100644 --- a/tests/behat/features/Admin/Tresorerie/Journal.feature +++ b/tests/behat/features/Admin/Tresorerie/Journal.feature @@ -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"#'