THANASOFT-HFC/gestion/lib/Service/Devis/DevisPdfTableRenderer.php

217 lines
8.7 KiB
PHP

<?php
namespace OCA\Gestion\Service\Devis;
class DevisPdfTableRenderer
{
public function createDevisTable($pdf, $dataDevis, $config)
{
$pagination = $config['pagination'];
$itemsToProcess = $config['itemsToProcess'];
$numPage = $config['numPage'];
$nbPage = $config['nbPage'];
$totals = &$config['totals'];
$sansMontant = $config['sansMontant'] ?? false;
// Système de pagination comme les factures
$maxItemsPerPage = 22; // Nombre maximum d'éléments par page
$startIndex = $pagination['current_index'];
$itemsThisPage = min($itemsToProcess, count($dataDevis) - $startIndex);
$this->drawTableStructure($pdf, $numPage, $nbPage, $sansMontant);
$this->addTableHeaders($pdf, $sansMontant);
$this->populateTableData($pdf, $dataDevis, $startIndex, $itemsThisPage, $totals, $sansMontant);
// Totaux seulement sur la dernière page
if ($numPage == $nbPage && !$sansMontant) {
$this->addTableTotals($pdf, $totals);
}
}
private function drawTableStructure($pdf, $numPage, $nbPage, $sansMontant)
{
$pdf->SetLineWidth(0.2);
$pdf->Rect(5, 105, 200, 130, "D");
$pdf->Line(5, 115, 205, 115);
$endY = ($numPage == $nbPage && !$sansMontant) ? 225 : 235;
if (!$sansMontant) {
// Ajout de la colonne Thanatopracteur entre Défunt et H.T.
$verticalLines = [24, 41, 58, 77, 102, 127, 147, 167, 178, 189];
} else {
// Pour sans montant: ajout de Thanatopracteur à la fin
$verticalLines = [27, 47, 67, 85, 110, 135, 160];
}
foreach ($verticalLines as $x) {
$pdf->Line($x, 105, $x, $endY);
}
}
private function addTableHeaders($pdf, $sansMontant)
{
$pdf->SetFont('Arial', 'B', 7);
if (!$sansMontant) {
$headers = [
[5, 19, "N° Devis"],
[24, 17, "N° Dossier"],
[41, 17, "N° Commande"],
[58, 19, "Date"],
[77, 25, "Article"],
[102, 25, "Lieu du soin"],
[127, 20, "Thanato"],
[147, 20, "Défunt"],
[167, 11, "H.T."],
[178, 11, "TVA"],
[189, 16, "T.T.C"]
];
} else {
$headers = [
[5, 22, "N° Devis"],
[27, 20, "N° Dossier"],
[47, 20, "N° Commande"],
[67, 18, "Date"],
[85, 25, "Article"],
[110, 25, "Lieu du soin"],
[135, 25, "Thanato"],
[160, 45, "Défunt"]
];
}
foreach ($headers as $header) {
$pdf->SetXY($header[0] + 1, 106);
$pdf->Cell($header[1] - 2, 8, mb_convert_encoding($header[2], 'ISO-8859-1', 'UTF-8'), 0, 0, 'C');
}
}
private function populateTableData($pdf, $dataDevis, $startIndex, $itemsToProcess, &$totals, $sansMontant)
{
$formatterDate = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE);
$formatterDate->setPattern('dd-MMM');
$yDevis = 115;
for ($i = $startIndex; $i < $startIndex + $itemsToProcess && $i < count($dataDevis); $i++) {
$devis = $dataDevis[$i];
$dateSoin = new \DateTime($devis['devis_date']);
$this->addTableRow($pdf, $devis, $formatterDate, $dateSoin, $yDevis, $sansMontant);
// Calculer les totaux seulement si on affiche les montants
if (!$sansMontant) {
$totals['ht'] += $devis['montant_htc'];
$totals['tva'] += $devis['montant_tva'];
$totals['ttc'] += $devis['montant_ttc'];
}
$yDevis += 10;
}
}
private function addTableRow($pdf, $devis, $formatterDate, $dateSoin, $yDevis, $sansMontant)
{
$pdf->SetFont('Arial', '', 7);
$addSmartCell = function ($pdf, $x, $y, $width, $text, $align = 'L') use ($yDevis) {
$textWidth = $pdf->GetStringWidth($text);
$maxWidth = $width - 2;
if ($textWidth > $maxWidth && strlen($text) > 10) {
$pdf->SetXY($x, $y);
$pdf->MultiCell($width, 2.5, $text, 0, $align);
} else {
$pdf->SetXY($x, $y);
$pdf->Cell($width, 5, $text, 0, 0, $align);
}
};
// Préparer le nom complet du thanatopracteur
$thanatoNom = trim(($devis['thanato_prenom'] ?? '') . ' ' . ($devis['thanato_nom'] ?? ''));
$thanatoNom = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($thanatoNom);
if (!$sansMontant) {
$addSmartCell($pdf, 6, $yDevis, 18, $devis['devis_full_number']);
$addSmartCell($pdf, 25, $yDevis, 16, $devis['case_number'] ?? '');
$addSmartCell($pdf, 42, $yDevis, 16, $devis['order_number'] ?? '');
$addSmartCell($pdf, 59, $yDevis, 18, mb_convert_encoding($formatterDate->format($dateSoin), 'ISO-8859-1', 'UTF-8'));
$articleText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['article'] ?? 'SOINS');
$addSmartCell($pdf, 78, $yDevis, 24, $articleText);
$lieuText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['lieu_nom'] ?? '');
$addSmartCell($pdf, 103, $yDevis, 24, $lieuText);
$defuntText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['defunt_nom'] ?? '');
$addSmartCell($pdf, 128, $yDevis, 19, $defuntText);
// NOUVELLE COLONNE : Thanatopracteur
$addSmartCell($pdf, 148, $yDevis, 19, $thanatoNom);
$addSmartCell($pdf, 168, $yDevis, 10, number_format($devis['montant_htc'], 2, '.', '') . chr(128), 'R');
$addSmartCell($pdf, 179, $yDevis, 10, number_format($devis['montant_tva'], 2, '.', '') . chr(128), 'R');
$addSmartCell($pdf, 190, $yDevis, 15, number_format($devis['montant_ttc'], 2, '.', '') . chr(128), 'R');
} else {
$addSmartCell($pdf, 6, $yDevis, 21, $devis['devis_full_number']);
$addSmartCell($pdf, 28, $yDevis, 19, $devis['case_number'] ?? '');
$addSmartCell($pdf, 48, $yDevis, 19, $devis['order_number'] ?? '');
$addSmartCell($pdf, 68, $yDevis, 17, mb_convert_encoding($formatterDate->format($dateSoin), 'ISO-8859-1', 'UTF-8'));
$articleText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['article'] ?? 'SOINS');
$addSmartCell($pdf, 86, $yDevis, 24, $articleText);
$lieuText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['lieu_nom'] ?? '');
$addSmartCell($pdf, 111, $yDevis, 24, $lieuText);
$defuntText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['defunt_nom'] ?? '');
$addSmartCell($pdf, 136, $yDevis, 24, $defuntText);
// NOUVELLE COLONNE : Thanatopracteur
$addSmartCell($pdf, 161, $yDevis, 44, $thanatoNom);
}
}
private function addTableTotals($pdf, $totals)
{
$pdf->Line(5, 225, 205, 225);
$pdf->SetFont('Arial', 'B', 8);
// Alignement des totaux avec les colonnes HT, TVA, TTC avec espaces entre les valeurs
$pdf->SetXY(5, 225);
$pdf->Cell(162, 8, 'TOTAL', 0, 0, 'C');
// POSITIONS avec espaces entre les valeurs - plus d'espace entre HT et TVA
$pdf->SetXY(167, 225);
$pdf->Cell(9, 8, number_format($totals['ht'], 2, '.', '') . chr(128), 0, 0, 'R');
$pdf->SetXY(181, 225);
$pdf->Cell(9, 8, number_format($totals['tva'], 2, '.', '') . chr(128), 0, 0, 'R');
$pdf->SetXY(189, 225);
$pdf->Cell(16, 8, number_format($totals['ttc'], 2, '.', '') . chr(128), 0, 0, 'R');
// CADRE TOTAL TTC - Cadre encore plus agrandi
$pdf->SetXY(170, 241);
$pdf->Cell(19, 6, 'TOTAL TTC', 0, 0, 'C');
$pdf->SetXY(189, 241);
$pdf->Cell(16, 6, number_format($totals['ttc'], 2, '.', '') . chr(128), 0, 0, 'C');
// Cadre TOTAL TTC aligné avec la fin du tableau (205)
$lines = [
[170, 240, 170, 248], // Ligne verticale gauche (déplacée de 173 à 170 pour agrandir)
[189, 240, 189, 248], // Ligne de séparation (alignée avec colonne T.T.C)
[205, 240, 205, 248], // Ligne verticale droite (alignée avec fin du tableau)
[170, 240, 205, 240], // Ligne horizontale haute
[170, 248, 205, 248] // Ligne horizontale basse
];
foreach ($lines as $line) {
$pdf->Line($line[0], $line[1], $line[2], $line[3]);
}
}
}