* * @author Anna Larch * @author Richard Steinmetz * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU AFFERO GENERAL PUBLIC LICENSE for more details. * * You should have received a copy of the GNU Affero General Public * License along with this library. If not, see . * */ namespace OCA\Gestion\Service\Devis\Pdf; use DateTime; use FPDF; use OCA\Gestion\Helpers\FileExportHelpers; use OCA\Gestion\Helpers\PriceHelpers; class DevisPdfHandler extends FPDF { private $multipleDevisData = []; private $devisData = []; private $logo = null; private $logoPath = "/var/www/html/data/admin/files/.gestion/"; public function Header() { if ($this->logo != "nothing") { $this->Image($this->logoPath . "logo.png", 4, 2, 36, 37); } else { $this->Cell(55, 30, ''); } } public function Footer() { $this->SetY(-40); $this->SetFont('Arial', '', 7); $this->MultiCell(0, 5, utf8_decode(html_entity_decode('Tout retard de paiement entraînera de plein droit une pénalité de retard de 3 fois le taux légal ( Loi 2008-776 du 4 août 2008) et une indemnité forfaitaire de 40 EUR pour frais de recouvrement sera appliquée.'))); $this->Ln(1); $this->MultiCell(0, 5, utf8_decode(html_entity_decode('Si les frais de recouvrement sont supérieurs à ce montant forfaitaire, une indemnisation complémentaire sera due sur présentation de justificatifs ( articles L.441-3 et L.441-6 du code de commerce ). '))); $this->SetY(-15); $this->SetFont('Arial', 'B', 8); $this->Cell(0, 10, utf8_decode(html_entity_decode($this->devisData['configuration']->legal_one)), 0, 0, 'C'); } public function SetDevisPdfData(array $devisData, $logo = null) { $this->devisData = $devisData; $this->logo = $logo; } public function SetMultipleDevisPdfData(array $multipleDevisData, $logo = null) { $this->multipleDevisData = $multipleDevisData; $this->logo = $logo; } private function DrawDevisCompanyAndClientInfo() { $this->SetY(40); $this->SetFont('Arial', '', 12); $this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->devisData['configuration']->entreprise), 0, 0); $this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->devisData['client_nom']), 0, 1, 'R'); $this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->devisData['configuration_adresse']), 0, 0); $this->Cell(0, 7, trim(FileExportHelpers::FormatTextForExport($this->devisData['client_real_adress'])), 0, 1, 'R'); $this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->devisData['configuration_adresse_city']), 0, 0); $this->Cell(0, 7, trim(FileExportHelpers::FormatTextForExport($this->devisData['client_adress_city'])), 0, 1, 'R'); $this->Cell(0, 7, FileExportHelpers::FormatTextForExport('Tél : ') . FileExportHelpers::FormatTextForExport($this->devisData['configuration']->telephone), 0, 0); $this->Cell(0, 7, 'Siret: ' . $this->devisData['siret'], 0, 1, 'R'); $this->Cell(0, 7, 'Mail : ' . $this->devisData['configuration']->mail, 0, 1); $this->Ln(3); } private function DrawDevisInfoTable() { $this->SetFont('Arial', 'B', 11); $this->Cell(30, 7, 'DATE', 1, 0, 'C'); $this->Cell(80, 7, 'CLIENT', 1, 0, 'C'); $this->Cell(40, 7, 'DEVIS', 1, 1, 'C'); $this->SetFont('Arial', '', 10); $this->Cell(30, 7, $this->devisData['devis_date'], 1, 0, 'C'); $this->Cell(80, 7, utf8_decode(html_entity_decode($this->devisData['client_nom'])), 1, 0, 'C'); $this->Cell(40, 7, $this->devisData['devis_full_number'], 1, 1, 'C'); $this->Ln(8); } private function DrawArticlesTable() { $this->SetLineWidth(0.1); $this->Rect(10, 105, 190, 100, "D"); // cadre titre des colonnes $this->Line(10, 115, 200, 115); // les traits verticaux colonnes $this->Line(35, 105, 35, 205); $this->Line(135, 105, 135, 205); $this->Line(155, 105, 155, 205); $this->Line(175, 105, 175, 205); } private function DrawArticlesTableHeader() { $this->SetFont('Arial', '', 10); $this->SetXY(10, 106); $this->Cell(20, 8, "Date", 0, 0, 'C'); $this->SetXY(35, 106); $this->Cell(100, 8, "Description", 0, 0, 'C'); $this->SetXY(135, 106); $this->Cell(20, 8, "Prix Uni. HT", 0, 0, 'C'); $this->SetXY(155, 106); $this->Cell(20, 8, 'TVA', 0, 0, 'C'); $this->SetXY(175, 106); $this->Cell(25, 8, "Prix Uni. TTC", 0, 0, 'C'); } public function DrawArticlesTableValueAndReturnTotalPrice() { $this->SetFont('Arial', '', 10); // Grouper les totaux par taux de TVA $totalsByTva = []; $products = $this->devisData["products"]; $yValue = 116; foreach ($products as $product) { // Récupérer le taux de TVA du produit (défaut à 20% si non défini) $tvaValue = isset($product['tva']) ? floatval($product['tva']) : 20.00; $valueHt = $product['produit_price'] * $product['quantite']; $valueTtc = PriceHelpers::calculPriceWithVatValue($valueHt, $tvaValue); $tvaAmount = $valueTtc - $valueHt; // Grouper par taux de TVA if (!isset($totalsByTva[$tvaValue])) { $totalsByTva[$tvaValue] = [ 'totalHT' => 0, 'totalTVA' => 0, 'totalTTC' => 0 ]; } $totalsByTva[$tvaValue]['totalHT'] += $valueHt; $totalsByTva[$tvaValue]['totalTVA'] += $tvaAmount; $totalsByTva[$tvaValue]['totalTTC'] += $valueTtc; // Affichage du produit $productDescription = $product["produit_description"]; $dateValue = ""; if ($product === end($products)) { $dateValue = $this->devisData['devis_date']; $productDescription .= " de " . FileExportHelpers::GetSexeLabel($this->devisData['defunt_sexe']) . ' ' . $this->devisData["defunt_nom"]; } $this->SetXY(10, $yValue); $this->Cell(20, 6, $dateValue, 0, 0); $this->SetXY(35, $yValue); $this->MultiAlignCell(100, 6, utf8_decode(html_entity_decode($productDescription)), 0, '0'); $this->SetXY(135, $yValue); $this->Cell(20, 6, number_format($valueHt, 2, '.', '') . chr(128), 0, 0, 'C'); $this->SetXY(155, $yValue); $this->Cell(20, 6, $tvaValue . '%', 0, 0, 'C'); $this->SetXY(175, $yValue); $this->Cell(25, 6, number_format($valueTtc, 2, '.', '') . chr(128), 0, 1, 'C'); $yValue += 12; } // Trier par taux de TVA ksort($totalsByTva); // ⭐ NOUVEAU: Ajouter les lignes de totaux par TVA DANS le tableau $this->SetFont('Arial', 'B', 10); // Ligne de séparation avant les totaux $this->Line(10, $yValue - 2, 200, $yValue - 2); $yValue += 2; foreach ($totalsByTva as $tva => $totals) { $this->SetXY(35, $yValue); $this->Cell(100, 6, 'Total TVA ' . number_format($tva, 0) . '%', 0, 0, 'L'); $this->SetXY(135, $yValue); $this->Cell(20, 6, number_format($totals['totalHT'], 2, '.', '') . chr(128), 0, 0, 'C'); $this->SetXY(155, $yValue); $this->Cell(20, 6, number_format($totals['totalTVA'], 2, '.', '') . chr(128), 0, 0, 'C'); $this->SetXY(175, $yValue); $this->Cell(25, 6, number_format($totals['totalTTC'], 2, '.', '') . chr(128), 0, 0, 'C'); $yValue += 8; } // Calculer les totaux généraux pour le cadre en bas à droite $totalGeneralHT = 0; $totalGeneralTVA = 0; $totalGeneralTTC = 0; foreach ($totalsByTva as $totals) { $totalGeneralHT += $totals['totalHT']; $totalGeneralTVA += $totals['totalTVA']; $totalGeneralTTC += $totals['totalTTC']; } // Retourner uniquement les totaux généraux return [ 'totalHT' => $totalGeneralHT, 'totalTVA' => $totalGeneralTVA, 'totalTTC' => $totalGeneralTTC ]; } private function DrawBankAndTotalPriceInfo($totalPriceArray) { $this->SetY(210); $this->SetFont('Arial', '', 9); $this->MultiCell(0, 5, utf8_decode(html_entity_decode("Paiement à votre convenance par chèque à l'ordre de " . $this->devisData['configuration']->entreprise))); $this->MultiCell(0, 5, utf8_decode(html_entity_decode("en indiquant le numéro de facture, ou par virement :"))); $this->Ln(1); //Table IBAN $this->SetFont('Arial', '', 11); $ibanWidth = 90; $ibanCursorY = $this->GetY(); $this->Cell($ibanWidth, 7, 'IBAN : FR76 1080 7004 4952 4211 5185 411', 1, 1, 'C'); $ibanCursorX = $this->GetX(); $this->Cell($ibanWidth, 7, 'Code SWIFT : AGRI FR PP 836', 1, 1, 'C'); //TABLE DES TOTAUX GÉNÉRAUX UNIQUEMENT (3 lignes) $ibanLastPositionX = $ibanCursorX + $ibanWidth + 20; $startOfArrayX = $ibanLastPositionX; $startOfArrayY = $ibanCursorY; // Afficher les 3 totaux généraux $this->SetFont('Arial', 'B', 11); $this->SetXY($startOfArrayX, $startOfArrayY); $this->Cell(40, 7, 'TOTAL HT', 1, 0, 'C'); $this->Cell(40, 7, number_format($totalPriceArray['totalHT'], 2, '.', '') . chr(128), 1, 1, 'C'); $startOfArrayY += 7; $this->SetXY($startOfArrayX, $startOfArrayY); $this->Cell(40, 7, 'TOTAL TVA', 1, 0, 'C'); $this->Cell(40, 7, number_format($totalPriceArray['totalTVA'], 2, '.', '') . chr(128), 1, 1, 'C'); $startOfArrayY += 7; $this->SetXY($startOfArrayX, $startOfArrayY); $this->Cell(40, 7, 'TOTAL TTC', 1, 0, 'C'); $this->Cell(40, 7, number_format($totalPriceArray['totalTTC'], 2, '.', '') . chr(128), 1, 1, 'C'); } public function SetMultipleDevisContent() { foreach ($this->multipleDevisData as $devisData) { $this->devisData = $devisData; $this->SetDevisContent(); } } public function SetDevisContent() { $this->AddPage(); $this->SetMargins(10, 0, 10); $this->DrawDevisCompanyAndClientInfo(); $this->DrawDevisInfoTable(); $this->DrawArticlesTable(); $this->DrawArticlesTableHeader(); $totalPriceValue = $this->DrawArticlesTableValueAndReturnTotalPrice(); $this->DrawBankAndTotalPriceInfo($totalPriceValue); } public function MultiAlignCell($w, $h, $text, $border = 0, $ln = 0, $align = 'L', $fill = false) { // Store reset values for (x,y) positions $x = $this->GetX() + $w; $y = $this->GetY(); // Make a call to FPDF's MultiCell $this->MultiCell($w, $h, $text, $border, $align, $fill); // Reset the line position to the right, like in Cell if ($ln == 0) { $this->SetXY($x, $y); } } public function NbLines($w, $txt) { // Compute the number of lines a MultiCell of width w will take if (!isset($this->CurrentFont)) { $this->Error('No font has been set'); } $cw = $this->CurrentFont['cw']; if ($w == 0) { $w = $this->w - $this->rMargin - $this->x; } $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize; $s = str_replace("\r", '', (string) $txt); $nb = strlen($s); if ($nb > 0 && $s[$nb - 1] == "\n") { $nb--; } $sep = -1; $i = 0; $j = 0; $l = 0; $nl = 1; while ($i < $nb) { $c = $s[$i]; if ($c == "\n") { $i++; $sep = -1; $j = $i; $l = 0; $nl++; continue; } if ($c == ' ') { $sep = $i; } $l += $cw[$c]; if ($l > $wmax) { if ($sep == -1) { if ($i == $j) { $i++; } } else { $i = $sep + 1; } $sep = -1; $j = $i; $l = 0; $nl++; } else { $i++; } } return $nl; } }