Skip to content

Commit 86ada9b

Browse files
committed
Refonte - Trésorerie > Comptes banques > Télécharger les justificatifs #2046
1 parent 6160e41 commit 86ada9b

5 files changed

Lines changed: 73 additions & 2 deletions

File tree

app/config/routing/admin_accounting_bank_accounts.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ admin_accounting_bank_accounts_list:
22
path: /list
33
defaults:
44
_controller: AppBundle\Controller\Admin\Accounting\BankAccounts\ListStatementAction
5+
6+
7+
admin_accounting_bank_accounts_download:
8+
path: /download-statements
9+
defaults:
10+
_controller: AppBundle\Controller\Admin\Accounting\BankAccounts\DownloadStatementAction

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"ext-libxml": "*",
1212
"ext-openssl": "*",
1313
"ext-pdo": "*",
14+
"ext-zip": "*",
1415
"algolia/algoliasearch-client-php": "^3.4",
1516
"beberlei/assert": "^2.9",
1617
"captioning/captioning": "^2.6",

htdocs/templates/administration/compta_journal.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ <h2>Journal</h2>
5959
<i class="icon file"></i>
6060
Exporter la période en CSV
6161
</a>
62-
<a href="index.php?page=compta_banque&amp;action=download_attachments&amp;id_periode={$smarty.get.id_periode|default:''}" class="item">
62+
<a href="/admin/accounting/bank-accounts/download-statements?periodId={$smarty.get.id_periode|default:''}" class="item">
6363
<i class="icon file zip"></i>
6464
Télécharger les justificatifs groupés par mois
6565
</a>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Controller\Admin\Accounting\BankAccounts;
6+
7+
use AppBundle\Accounting\Model\Repository\InvoicingPeriodRepository;
8+
use DateInterval;
9+
use DatePeriod;
10+
use RuntimeException;
11+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
15+
use ZipArchive;
16+
17+
class DownloadStatementAction extends AbstractController
18+
{
19+
public function __construct(private readonly InvoicingPeriodRepository $invoicingPeriodRepository) {}
20+
21+
public function __invoke(Request $request): Response
22+
{
23+
$periodId = $request->query->has('periodId') && $request->query->get('periodId') ? (int) $request->query->get('periodId') : null;
24+
$period = $this->invoicingPeriodRepository->getCurrentPeriod($periodId);
25+
try {
26+
$year = $period->getStartDate()->format('Y');
27+
28+
// Create the zip
29+
$zipFilename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'afup_justificatifs-' . $year . '.zip';
30+
$zip = new ZipArchive();
31+
$state = $zip->open($zipFilename, ZipArchive::CREATE);
32+
if ($state !== true) {
33+
throw new RuntimeException("Impossible to open the Zip archive.");
34+
} else {
35+
$datePeriod = new DatePeriod($period->getStartDate(), new DateInterval('P1M'), $period->getEndDate());
36+
/** @var \DateTime $month */
37+
foreach ($datePeriod as $month) {
38+
$searchDir = $month->format('Ym');
39+
$zipDir = $month->format('Ym');
40+
$options = [
41+
'add_path' => 'afup_justificatifs-' . $year . '/' . $zipDir . '/',
42+
'remove_all_path' => true,
43+
];
44+
$zip->addGlob(AFUP_CHEMIN_RACINE . '/uploads/' . $searchDir . '/*.*', 0, $options);
45+
}
46+
$zip->close();
47+
48+
$response = new BinaryFileResponse($zipFilename, Response::HTTP_OK, [
49+
'Content-Type' => 'application/zip',
50+
'Content-Transfer-Encoding' => 'Binary',
51+
'Content-Disposition' => 'attachment; filename="' . basename($zipFilename) . '"',
52+
'Cache-Control' => 'max-age=0',
53+
], false);
54+
$response->deleteFileAfterSend(true);
55+
56+
return $response;
57+
}
58+
} catch (\Exception $exception) {
59+
return new Response(null, Response::HTTP_BAD_REQUEST, [
60+
'X-Info' => $exception->getMessage(),
61+
]);
62+
}
63+
}
64+
}

tests/behat/features/Admin/Tresorerie/Journal.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ Feature: Administration - Trésorerie - Journal
8989
Given I am logged in as admin and on the Administration
9090
When I follow "Journal"
9191
And I follow "Télécharger les justificatifs groupés par mois"
92-
Then the response header "Content-disposition" should match '#filename="afup_justificatifs-(.*).zip"#'
92+
Then the response header "Content-Disposition" should match '#filename="afup_justificatifs-(.*).zip"#'

0 commit comments

Comments
 (0)