Skip to content

Commit

Permalink
Merge pull request #1410 from afup/fiscalisation_devis
Browse files Browse the repository at this point in the history
Fiscalisation : gestion de la TVA sur les PDFs de devis
  • Loading branch information
agallou authored Jan 2, 2024
2 parents 87e5f73 + 4785c5e commit 9cdbce3
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 17 deletions.
101 changes: 84 additions & 17 deletions sources/Afup/Comptabilite/Facture.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,11 @@ function genererDevis($reference, $chemin = null)
? \DateTimeImmutable::createFromFormat('Y-m-d', $coordonnees['date_devis'])
: new \DateTimeImmutable();

$isSubjectedToVat = Vat::isSubjectedToVat($dateDevis);

$bankAccountFactory = new BankAccountFactory($configuration);
// Construction du PDF
$pdf = new PDF_Facture($configuration, $bankAccountFactory->createApplyableAt($dateDevis));
$pdf = new PDF_Facture($configuration, $bankAccountFactory->createApplyableAt($dateDevis), $isSubjectedToVat);
$pdf->AddPage();

$pdf->Cell(130, 5);
Expand Down Expand Up @@ -387,12 +389,16 @@ function genererDevis($reference, $chemin = null)
$pdf->Ln(5);
$pdf->SetFillColor(200, 200, 200);
$pdf->Cell(30, 5, 'Type', 1, 0, 'L', 1);
$pdf->Cell(80, 5, 'Description', 1, 0, 'L', 1);
$pdf->Cell(20, 5, 'Quantite', 1, 0, 'L', 1);
$pdf->Cell(30, 5, 'Prix', 1, 0, 'L', 1);
$pdf->Cell(30, 5, 'Total', 1, 0, 'L', 1);
$pdf->Cell($isSubjectedToVat ? 60 : 80, 5, 'Description', 1, 0, 'L', 1);
$pdf->Cell(20, 5, 'Quantite', 1, 0, $isSubjectedToVat ? 'R' : 'L', 1);
if ($isSubjectedToVat) {
$pdf->Cell(20, 5, 'TVA', 1, 0, 'C', 1);
}
$pdf->Cell(30, 5, 'Prix' . ($isSubjectedToVat ? ' HT' : ''), 1, 0, $isSubjectedToVat ? 'R' : 'L', 1);
$pdf->Cell(30, 5, 'Total' . ($isSubjectedToVat ? ' TTC' : ''), 1, 0, $isSubjectedToVat ? 'R' : 'L', 1);

$total = 0;
$totalTtc = 0;
$totalHt = 0;
switch ($coordonnees['devise_facture']) {
case 'DOL':
$devise = ' $';
Expand All @@ -402,30 +408,90 @@ function genererDevis($reference, $chemin = null)
$devise = utf8_decode(' €');
break;
}

$yInitial = $pdf->getY();

$vatAmounts = [];

foreach ($details as $detail) {
if ($detail['quantite'] != 0) {
$montant = $detail['quantite'] * $detail['pu'];
$montantHt = $detail['quantite'] * $detail['pu'];
$montantTtc = $montantHt;

$pdf->Ln();
$pdf->SetFillColor(255, 255, 255);

$pdf->Cell(30, 5, $detail['ref'], 1);
$pdf->Cell(80, 5, utf8_decode($detail['designation']), 1);
$pdf->Cell(20, 5, utf8_decode($detail['quantite']), 1, 0, "C");
$pdf->Cell(30, 5, utf8_decode($detail['pu']) . $devise, 1, 0, "R");
$pdf->Cell(30, 5, utf8_decode($montant) . $devise, 1, 0, "R");
if ($isSubjectedToVat) {
$x = $pdf->GetX();
$y = $pdf->GetY();

$pdf->MultiCell(30, 5, $detail['ref'], 'T');
$x += 30;
$pdf->SetXY($x, $y);

$pdf->MultiCell(60, 5, utf8_decode($detail['designation']), 'T');

$x += 60;
$pdf->SetXY($x, $y);
$pdf->MultiCell(20, 5, utf8_decode($detail['quantite']), 'T', 0, "C");

$x += 20;
$pdf->SetXY($x, $y);
$pdf->MultiCell(20, 5, utf8_decode($detail['tva'] . '%'), 'T', 'C', "C");
$vatAmounts[$detail['tva']] = ($detail['tva'] / 100) * $montantTtc;
$montantTtc = $montantTtc * (1 + ($detail['tva'] / 100));

$x += 20;
$pdf->SetXY($x, $y);

$pdf->MultiCell(30, 5, utf8_decode($this->formatFactureValue($detail['pu'], $isSubjectedToVat)) . $devise, 'T', 0, "R");

$total += $montant;
$x += 30;
$pdf->SetXY($x, $y);
$pdf->MultiCell(30, 5, utf8_decode($this->formatFactureValue($montantTtc, $isSubjectedToVat)) . $devise, 'T', 0, "R");
} else {
$pdf->Cell(30, 5, $detail['ref'], 1);
$pdf->Cell(80, 5, utf8_decode($detail['designation']), 1);
$pdf->Cell(20, 5, utf8_decode($detail['quantite']), 1, 0, "C");
$pdf->Cell(30, 5, utf8_decode($detail['pu']) . $devise, 1, 0, "R");
$pdf->Cell(30, 5, utf8_decode($montantHt) . $devise, 1, 0, "R");
}

$totalHt += $montantHt;
$totalTtc += $montantTtc;
}
}
$pdf->Ln();

if ($isSubjectedToVat) {
$columns = [0, 30, 90, 110, 130, 160, 190];

foreach ($columns as $column) {
$pdf->Line($pdf->GetX() + $column, $yInitial, $pdf->GetX() + $column, $pdf->GetY());
}

$pdf->SetFillColor(225, 225, 225);
$pdf->Cell(160, 5, 'TOTAL HT', 1, 0, 'R', 1);
$pdf->Cell(30, 5, $this->formatFactureValue($totalHt, $isSubjectedToVat) . $devise, 1, 0, 'R', 1);
$pdf->Ln(5);

foreach ($vatAmounts as $vat => $amount) {
$pdf->SetFillColor(255, 255, 255);
$pdf->Cell(160, 5, 'Total TVA ' . $vat . '%', 1, 0, 'R', 1);
$pdf->Cell(30, 5, $this->formatFactureValue($amount, $isSubjectedToVat) . $devise, 1, 0, 'R', 1);
$pdf->Ln(5);
}
} else {
$pdf->ln();
}

$pdf->SetFillColor(225, 225, 225);
$pdf->Cell(160, 5, 'TOTAL', 1, 0, 'L', 1);
$pdf->Cell(30, 5, $total . $devise, 1, 0, 'R', 1);
$pdf->Cell(160, 5, 'TOTAL' . ($isSubjectedToVat ? ' TTC' : ''), 1, 0, $isSubjectedToVat ? 'R' : 'L', 1);
$pdf->Cell(30, 5, $this->formatFactureValue($totalTtc, $isSubjectedToVat) . $devise, 1, 0, 'R', 1);

$pdf->Ln(15);
$pdf->Cell(10, 5, 'TVA non applicable - art. 293B du CGI');
if (!$isSubjectedToVat) {
$pdf->Cell(10, 5, 'TVA non applicable - art. 293B du CGI');
}
$pdf->Ln(10);
$pdf->Cell(10, 5, 'Observations : ');
$pdf->Ln(5);
Expand All @@ -434,6 +500,7 @@ function genererDevis($reference, $chemin = null)

if (is_null($chemin)) {
$pdf->Output('Devis - ' . $coordonnees['societe'] . ' - ' . $coordonnees['date_devis'] . '.pdf', 'D');
exit(0);
} else {
$pdf->Output($chemin, 'F');
}
Expand Down
45 changes: 45 additions & 0 deletions tests/behat/features/Admin/Tresorerie/DevisFactures.feature
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,48 @@ Feature: Administration - Trésorerie - Devis/Facture
Then The page "1" of the PDF should contain "Total TVA 20.00% 200,00 €"
Then The page "1" of the PDF should contain "TOTAL TTC 1 200,00 €"
Then The page "1" of the PDF should not contain "TVA non applicable - art. 293B du CGI"

@reloadDbWithTestData
@vat
Scenario: Test du PDF de facture avant 2024
Given I am logged in as admin and on the Administration
When I go to "/pages/administration/index.php?page=compta_devis&id_periode=14"
Then the ".content h2" element should contain "Liste devis"
When I follow the button of tooltip "Télécharger le devis Krampouz"
Then the response header "Content-disposition" should equal 'attachment; filename="Devis - Krampouz - 2023-06-10.pdf"'
Given I parse the pdf downloaded content
Then The page "1" of the PDF should contain "Le 10/06/2023"
Then The page "1" of the PDF should contain "Krampouz"
Then The page "1" of the PDF should contain "3, rue du port"
Then The page "1" of the PDF should contain "Devis n° 2023-01"
Then The page "1" of the PDF should contain "Repère(s) : Forum PHP 2023"
Then The page "1" of the PDF should contain "Comme convenu, nous vous prions de trouver votre devis"
Then The page "1" of the PDF should contain "Type Description Quantite Prix Total"
Then The page "1" of the PDF should contain "forum_php_2023 Forum PHP 2023 - Sponsoring Bronze 1.00 1000.00 € 1000 €"
Then The page "1" of the PDF should contain "TOTAL 1000 €"
Then The page "1" of the PDF should contain "TVA non applicable - art. 293B du CGI"
Then the checksum of the response content should be "9ad7aeae75768031bb7098a80a403c7b"


@reloadDbWithTestData
@vat
Scenario: Test du PDF de facture après 2024
Given I am logged in as admin and on the Administration
When I go to "/pages/administration/index.php?page=compta_devis"
Then the ".content h2" element should contain "Liste devis"
When I follow the button of tooltip "Télécharger le devis Krampouz"
Then the response header "Content-disposition" should equal 'attachment; filename="Devis - Krampouz - 2024-01-03.pdf"'
Given I parse the pdf downloaded content
Then The page "1" of the PDF should contain "Le 03/01/2024"
Then The page "1" of the PDF should contain "Krampouz"
Then The page "1" of the PDF should contain "3, rue du port"
Then The page "1" of the PDF should contain "Devis n° 2024-02"
Then The page "1" of the PDF should contain "Repère(s) : Forum PHP 2024"
Then The page "1" of the PDF should contain "Comme convenu, nous vous prions de trouver votre devis"
Then The page "1" of the PDF should contain "Type Description Quantite TVA Prix HT Total TTC"
Then The page "1" of the PDF should contain "forum_php_2024 Forum PHP 2024 - Sponsoring"
Then The page "1" of the PDF should contain "Bronze 1.00 20.00% 1 000,00 € 1 200,00 €"
Then The page "1" of the PDF should contain "TOTAL HT 1 000,00 €"
Then The page "1" of the PDF should contain "Total TVA 20.00% 200,00 €"
Then The page "1" of the PDF should contain "TOTAL TTC 1 200,00 €"
Then The page "1" of the PDF should not contain "TVA non applicable - art. 293B du CGI"

0 comments on commit 9cdbce3

Please sign in to comment.