239 lines
8.9 KiB
PHP
239 lines
8.9 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;
|
|
|
|
$startIndex = $pagination['current_index'];
|
|
$itemsThisPage = min($itemsToProcess, count($dataDevis) - $startIndex);
|
|
|
|
$tableEndY = $this->drawTableStructure($pdf, $numPage, $nbPage, $sansMontant, $itemsThisPage);
|
|
$this->addTableHeaders($pdf, $sansMontant);
|
|
$this->populateTableData($pdf, $dataDevis, $startIndex, $itemsThisPage, $totals, $sansMontant);
|
|
|
|
if ($numPage == $nbPage && !$sansMontant) {
|
|
$finalY = $this->addTableTotals($pdf, $totals, $tableEndY);
|
|
} else {
|
|
$finalY = $tableEndY;
|
|
}
|
|
|
|
return $finalY;
|
|
}
|
|
|
|
private function drawTableStructure($pdf, $numPage, $nbPage, $sansMontant, $itemsOnPage)
|
|
{
|
|
$pdf->SetLineWidth(0.2);
|
|
|
|
$headerHeight = 10;
|
|
$rowHeight = 10;
|
|
|
|
$tableHeight = $headerHeight + ($itemsOnPage * $rowHeight);
|
|
$endY = 105 + $tableHeight;
|
|
|
|
$isLastPage = ($numPage == $nbPage);
|
|
$hasAmounts = !$sansMontant;
|
|
|
|
if ($isLastPage && $hasAmounts) {
|
|
// Dessiner 3 côtés seulement (haut, gauche, droite)
|
|
$pdf->Line(5, 105, 205, 105); // Haut
|
|
$pdf->Line(5, 105, 5, $endY); // Gauche
|
|
$pdf->Line(205, 105, 205, $endY); // Droite
|
|
// Pas de ligne du bas - elle sera tracée dans addTableTotals
|
|
} else {
|
|
// Dessiner rectangle complet
|
|
$pdf->Rect(5, 105, 200, $tableHeight, "D");
|
|
}
|
|
|
|
$pdf->Line(5, 115, 205, 115);
|
|
|
|
if (!$sansMontant) {
|
|
$verticalLines = [24, 41, 58, 77, 102, 127, 147, 167, 178, 189];
|
|
} else {
|
|
$verticalLines = [27, 47, 67, 85, 110, 135, 160];
|
|
}
|
|
|
|
foreach ($verticalLines as $x) {
|
|
$pdf->Line($x, 105, $x, $endY);
|
|
}
|
|
|
|
return $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);
|
|
|
|
// COLONNE Thanatopracteur (après Lieu du soin)
|
|
$addSmartCell($pdf, 128, $yDevis, 19, $thanatoNom);
|
|
|
|
// COLONNE Défunt (après Thanato)
|
|
$defuntText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['defunt_nom'] ?? '');
|
|
$addSmartCell($pdf, 148, $yDevis, 19, $defuntText);
|
|
|
|
$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);
|
|
|
|
// COLONNE Thanatopracteur (après Lieu du soin)
|
|
$addSmartCell($pdf, 136, $yDevis, 24, $thanatoNom);
|
|
|
|
// COLONNE Défunt (après Thanato)
|
|
$defuntText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['defunt_nom'] ?? '');
|
|
$addSmartCell($pdf, 161, $yDevis, 44, $defuntText);
|
|
}
|
|
}
|
|
|
|
private function addTableTotals($pdf, $totals, $tableEndY)
|
|
{
|
|
$totalEndY = $tableEndY + 8;
|
|
|
|
$pdf->SetLineWidth(0.2);
|
|
|
|
// Juste les lignes horizontales haut et bas
|
|
$pdf->Line(5, $tableEndY, 205, $tableEndY);
|
|
$pdf->Line(5, $totalEndY, 205, $totalEndY);
|
|
|
|
$pdf->Line(5, $tableEndY, 5, $totalEndY);
|
|
$pdf->Line(205, $tableEndY, 205, $totalEndY);
|
|
|
|
$pdf->SetFont('Arial', 'B', 8);
|
|
|
|
$pdf->SetXY(5, $tableEndY);
|
|
$pdf->Cell(162, 8, 'TOTAL', 0, 0, 'C');
|
|
|
|
$pdf->SetXY(167, $tableEndY);
|
|
$pdf->Cell(9, 8, number_format($totals['ht'], 2, '.', '') . chr(128), 0, 0, 'R');
|
|
|
|
$pdf->SetXY(181, $tableEndY);
|
|
$pdf->Cell(9, 8, number_format($totals['tva'], 2, '.', '') . chr(128), 0, 0, 'R');
|
|
|
|
$pdf->SetXY(189, $tableEndY);
|
|
$pdf->Cell(16, 8, number_format($totals['ttc'], 2, '.', '') . chr(128), 0, 0, 'R');
|
|
|
|
$cadreY = $totalEndY + 3;
|
|
|
|
$pdf->SetXY(170, $cadreY + 1);
|
|
$pdf->Cell(19, 6, 'TOTAL TTC', 0, 0, 'C');
|
|
|
|
$pdf->SetXY(189, $cadreY + 1);
|
|
$pdf->Cell(16, 6, number_format($totals['ttc'], 2, '.', '') . chr(128), 0, 0, 'C');
|
|
|
|
$pdf->Rect(170, $cadreY, 35, 8, 'D');
|
|
$pdf->Line(189, $cadreY, 189, $cadreY + 8);
|
|
|
|
return $cadreY + 8;
|
|
}
|
|
}
|