Skip to content

Commit 5ff960f

Browse files
committed
Refonte - Trésorerie > Comptes banque > Export
1 parent dae9f4a commit 5ff960f

File tree

9 files changed

+206
-5
lines changed

9 files changed

+206
-5
lines changed

app/config/routing/admin_accounting.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,7 @@ admin_accounting_search:
125125
admin_accounting_membership_fee:
126126
resource: "admin_accounting_membership_fee.yml"
127127
prefix: /membership-fee
128+
129+
admin_accounting_bank_accounts:
130+
resource: "admin_accounting_bank_accounts.yml"
131+
prefix: /bank-accounts
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
admin_accounting_bankaccounts_export:
2+
path: /export
3+
defaults:
4+
_controller: AppBundle\Controller\Admin\Accounting\BankAccounts\ExportStatementAction

app/config/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ services:
170170
Afup\Site\Association\Cotisations:
171171
factory: ['@Afup\Site\Association\CotisationsFactory', 'create']
172172

173+
Afup\Site\Comptabilite\Comptabilite:
174+
autowire: true
175+
173176
Afup\Site\Comptabilite\Facture:
174177
autowire: true
175178

htdocs/templates/administration/compta_banque.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ <h2>Journal de banque</h2>
3232
</div>
3333

3434
<div class="ui menu">
35-
<a href="index.php?id_periode={$smarty.get.id_periode|default:''}&amp;page=compta_banque&amp;compte={$compte_id}&amp;action=exporter" class="item">
35+
<a href="/admin/accounting/bank-accounts/export?periodId={$smarty.get.id_periode|default:''}&amp;accountId={$compte_id}" class="item">
3636
<i class="icon file excel"></i>
3737
Export XLSX
3838
</a>

sources/AppBundle/Accounting/Form/InvoicingPeriodType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
2020
$periods = [];
2121
/** @var InvoicingPeriod $period */
2222
foreach ($this->invoicingPeriodRepository->getAll() as $period) {
23-
$periods["{$period->getStartdate()->format('d/m/Y')} - {$period->getEndDate()->format('d/m/Y')}"] = $period->getId();
23+
$periods["{$period->getStartDate()->format('d/m/Y')} - {$period->getEndDate()->format('d/m/Y')}"] = $period->getId();
2424
}
2525

2626
$builder->add('periodId', ChoiceType::class, [

sources/AppBundle/Accounting/Model/InvoicingPeriod.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public function setId(?int $id): self
3131
return $this;
3232
}
3333

34-
public function getStartdate(): ?DateTime
34+
public function getStartDate(): ?DateTime
3535
{
3636
return $this->startDate;
3737
}
3838

39-
public function setStartdate(?DateTime $startDate): self
39+
public function setStartDate(?DateTime $startDate): self
4040
{
4141
$this->startDate = $startDate;
4242
$this->propertyChanged('startdate', $this->startDate, $startDate);
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Accounting;
6+
7+
use AppBundle\Accounting\Model\InvoicingPeriod;
8+
use DatePeriod;
9+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
10+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
11+
use PhpOffice\PhpSpreadsheet\Style\Border;
12+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
13+
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
14+
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
15+
16+
class SpreadsheetGenerator
17+
{
18+
public function generate(array $statements, array $subTotal, InvoicingPeriod $period): Spreadsheet
19+
{
20+
$formater = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE);
21+
$formater->setPattern('MMMM yyyy');
22+
23+
$monthsOfYear = new DatePeriod($period->getStartDate(), new \DateInterval('P1M'), $period->getEndDate());
24+
$compteurLigne = [];
25+
$workbook = new Spreadsheet();
26+
/** @var \DateTime $month */
27+
foreach ($monthsOfYear as $month) {
28+
$currentMonth = (int) $month->format('n');
29+
$compteurLigne[$currentMonth] = 4;
30+
$sheet = $workbook->createSheet($currentMonth);
31+
$sheet->setTitle('Mois de ' . $formater->format($month));
32+
$sheet->setCellValue('A1', 'Mois de ' . $formater->format($month));
33+
$sheet->setCellValue('A3', 'Date');
34+
$sheet->setCellValue('B3', 'Opération');
35+
$sheet->setCellValue('C3', 'Description');
36+
$sheet->setCellValue('D3', 'Événement');
37+
$sheet->setCellValue('E3', 'Catégorie');
38+
$sheet->setCellValue('F3', 'Dépense');
39+
$sheet->setCellValue('G3', 'Recette');
40+
$sheet->setCellValue('H3', 'Commentaire');
41+
$sheet->setCellValue('I3', 'Justificatif');
42+
$sheet->setCellValue('J3', 'Nom du justificatif');
43+
$sheet->setCellValue('K3', 'Nom du compte');
44+
}
45+
46+
foreach ($statements as $ecriture) {
47+
$sheet = $workbook->getSheet($ecriture['mois']);
48+
$sheet->setCellValue('A' . $compteurLigne[$ecriture['mois']], date('d/m/Y', strtotime((string) $ecriture['date_regl'])));
49+
$sheet->setCellValue('B' . $compteurLigne[$ecriture['mois']], $ecriture['reglement']);
50+
$sheet->setCellValue('C' . $compteurLigne[$ecriture['mois']], $ecriture['description']);
51+
$sheet->setCellValue('D' . $compteurLigne[$ecriture['mois']], $ecriture['evenement']);
52+
$sheet->setCellValue('E' . $compteurLigne[$ecriture['mois']], $ecriture['categorie']);
53+
if ($ecriture['idoperation'] == 1) {
54+
$sheet->setCellValue('F' . $compteurLigne[$ecriture['mois']], $ecriture['montant']);
55+
} else {
56+
$sheet->setCellValue('G' . $compteurLigne[$ecriture['mois']], $ecriture['montant']);
57+
}
58+
$sheet->setCellValue('H' . $compteurLigne[$ecriture['mois']], $ecriture['comment']);
59+
$sheet->setCellValue('I' . $compteurLigne[$ecriture['mois']], $ecriture['attachment_required'] ? 'Oui' : 'Non');
60+
$sheet->setCellValue('J' . $compteurLigne[$ecriture['mois']], $ecriture['attachment_filename']);
61+
$sheet->setCellValue('K' . $compteurLigne[$ecriture['mois']], $ecriture['compta_compte_nom_compte']);
62+
$compteurLigne[$ecriture['mois']]++;
63+
}
64+
for ($i = 1; $i < 13; $i++) {
65+
$sheet = $workbook->getSheet($i);
66+
67+
$sheet->getStyle('A1')->applyFromArray([
68+
'font' => [
69+
'size' => 12,
70+
'bold' => true,
71+
'name' => 'Ubuntu',
72+
],
73+
]);
74+
$sheet->getStyle('A3:K3')->applyFromArray([
75+
'font' => [
76+
'size' => 10,
77+
'bold' => true,
78+
'name' => 'Ubuntu',
79+
],
80+
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
81+
'borders' => [
82+
'allborders' => [
83+
'style' => Border::BORDER_THIN,
84+
'color' => ['rgb' => 'FF666666'],
85+
],
86+
],
87+
]);
88+
$sheet->getStyle('A4:K' . ($compteurLigne[$i] + 1))->applyFromArray([
89+
'font' => [
90+
'size' => 10,
91+
'name' => 'Ubuntu',
92+
],
93+
'borders' => [
94+
'allborders' => [
95+
'style' => Border::BORDER_THIN,
96+
'color' => ['rgb' => 'FF666666'],
97+
],
98+
],
99+
]);
100+
$sheet->getStyle('J3:I200')->applyFromArray(['alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER]]);
101+
102+
$sheet->setCellValue('E' . $compteurLigne[$i], 'TOTAL');
103+
$sheet->setCellValue('F' . $compteurLigne[$i], $subTotal[$i]['debit']);
104+
$sheet->setCellValue('G' . $compteurLigne[$i], $subTotal[$i]['credit']);
105+
$sheet->setCellValue('E' . ($compteurLigne[$i] + 1), 'SOLDE');
106+
$sheet->setCellValue('F' . ($compteurLigne[$i] + 1), $subTotal[$i]['dif']);
107+
$sheet->mergeCells('F' . ($compteurLigne[$i] + 1) . ':G' . ($compteurLigne[$i] + 1));
108+
109+
$sheet->getStyle('A' . $compteurLigne[$i] . ':J' . ($compteurLigne[$i] + 1))->applyFromArray([
110+
'font' => [
111+
'size' => 10,
112+
'bold' => true,
113+
'name' => 'Ubuntu',
114+
],
115+
]);
116+
$sheet->getStyle('F' . ($compteurLigne[$i] + 1))->getAlignment()->applyFromArray(['horizontal' => Alignment::HORIZONTAL_CENTER]);
117+
118+
$sheet->getStyle('F4:G200')->applyFromArray(['numberformat' => ['code' => NumberFormat::FORMAT_NUMBER_00]]);
119+
120+
$sheet->getColumnDimension('A')->setWidth(8);
121+
$sheet->getColumnDimension('C')->setWidth(36);
122+
$sheet->getColumnDimension('D')->setWidth(12);
123+
$sheet->getColumnDimension('E')->setWidth(12);
124+
$sheet->getPageSetup()->setPaperSize(PageSetup::PAPERSIZE_A4);
125+
$sheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
126+
$sheet->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 3);
127+
$sheet->getHeaderFooter()->setOddFooter('&CPage &P de &N');
128+
129+
$objDrawing = new Drawing();
130+
$objDrawing->setName('Logo_AFUP');
131+
$objDrawing->setDescription('Logo_AFUP');
132+
$objDrawing->setPath(__DIR__ . '/../../../htdocs/templates/administration/images/logo_afup.png');
133+
$objDrawing->setCoordinates('H1');
134+
$objDrawing->setHeight(35);
135+
$objDrawing->setWidth(70);
136+
$objDrawing->setWorksheet($sheet);
137+
}
138+
139+
return $workbook;
140+
}
141+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Controller\Admin\Accounting\BankAccounts;
6+
7+
use Afup\Site\Comptabilite\Comptabilite;
8+
use AppBundle\Accounting\Model\Repository\InvoicingPeriodRepository;
9+
use AppBundle\Accounting\SpreadsheetGenerator;
10+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
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+
16+
class ExportStatementAction extends AbstractController
17+
{
18+
public function __construct(
19+
private readonly InvoicingPeriodRepository $invoicingPeriodRepository,
20+
private readonly Comptabilite $comptabilite,
21+
private readonly SpreadsheetGenerator $spreadsheetGenerator,
22+
) {}
23+
24+
public function __invoke(Request $request): Response
25+
{
26+
$periodId = $request->query->has('periodId') && $request->query->get('periodId') ? (int) $request->query->get('periodId') : null;
27+
$accountId = $request->query->getInt('accountId', 1);
28+
$period = $this->invoicingPeriodRepository->getCurrentPeriod($periodId);
29+
30+
$start = $period->getStartDate()->format('Y-m-d');
31+
$end = $period->getEndDate()->format('Y-m-d');
32+
$statements = $this->comptabilite->obtenirJournalBanque($accountId, $start, $end);
33+
$subTotal = $this->comptabilite->obtenirSousTotalJournalBanque($start, $end, $accountId);
34+
35+
36+
$spreadsheet = $this->spreadsheetGenerator->generate($statements, $subTotal, $period);
37+
$tempfile = tempnam(sys_get_temp_dir(), 'bank_acconts_export');
38+
$writer = new Xlsx($spreadsheet);
39+
$writer->save($tempfile);
40+
41+
$response = new BinaryFileResponse($tempfile, Response::HTTP_OK, [
42+
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
43+
'Content-Disposition' => 'attachment; filename="compta_afup_' . $period->getStartDate()->format('Y') . '.xlsx"',
44+
'Cache-Control' => 'max-age=0',
45+
], false);
46+
$response->deleteFileAfterSend(true);
47+
return $response;
48+
}
49+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ Feature: Administration - Trésorerie - Compte banques
1616
Given I am logged in as admin and on the Administration
1717
When I follow "Compte banques"
1818
And I follow "Export XLSX"
19-
Then the response header "Content-disposition" should match '#filename="compta_afup_(.*).xlsx"#'
19+
Then the response header "Content-Disposition" should match '#filename="compta_afup_(.*).xlsx"#'

0 commit comments

Comments
 (0)