210 lines
8.1 KiB
PHP
210 lines
8.1 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) {
|
|
// AVEC montants : toutes les colonnes
|
|
$verticalLines = [25, 45, 65, 80, 100, 125, 145, 165, 185];
|
|
} else {
|
|
// SANS montants : SUPPRIMER la colonne vide à droite
|
|
$verticalLines = [35, 70, 105, 120, 135, 160]; // Seulement 6 lignes au lieu de 7
|
|
}
|
|
|
|
foreach ($verticalLines as $x) {
|
|
$pdf->Line($x, 105, $x, $endY);
|
|
}
|
|
}
|
|
|
|
private function addTableHeaders($pdf, $sansMontant)
|
|
{
|
|
$pdf->SetFont('Arial', 'B', 7);
|
|
|
|
if (!$sansMontant) {
|
|
$headers = [
|
|
[5, 20, "N° Devis"],
|
|
[25, 20, "N° Dossier"],
|
|
[45, 20, "N° Commande"],
|
|
[65, 15, "Date"],
|
|
[80, 20, "Article"],
|
|
[100, 25, "Lieu du soin"],
|
|
[125, 20, "Défunt"],
|
|
[145, 20, "H.T."],
|
|
[165, 20, "TVA"],
|
|
[185, 20, "T.T.C"]
|
|
];
|
|
} else {
|
|
// SANS montants : SUPPRIMER la colonne vide, agrandir la dernière colonne
|
|
$headers = [
|
|
[5, 30, "N° Devis"],
|
|
[35, 35, "N° Dossier"],
|
|
[70, 35, "N° Commande"],
|
|
[105, 15, "Date"],
|
|
[120, 15, "Article"],
|
|
[135, 25, "Lieu du soin"],
|
|
[160, 45, "Défunt"] // Dernière colonne plus large
|
|
];
|
|
}
|
|
|
|
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);
|
|
|
|
// Fonction généraliste pour gérer automatiquement le débordement
|
|
$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);
|
|
}
|
|
};
|
|
|
|
if (!$sansMontant) {
|
|
$addSmartCell($pdf, 6, $yDevis, 18, $devis['devis_full_number']);
|
|
|
|
// N° Dossier - UTILISER case_number
|
|
$addSmartCell($pdf, 26, $yDevis, 18, $devis['case_number'] ?? '');
|
|
|
|
// N° Commande - UTILISER order_number
|
|
$addSmartCell($pdf, 46, $yDevis, 18, $devis['order_number'] ?? '');
|
|
|
|
$addSmartCell($pdf, 66, $yDevis, 13, mb_convert_encoding($formatterDate->format($dateSoin), 'ISO-8859-1', 'UTF-8'));
|
|
|
|
$articleText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['article'] ?? 'SOINS');
|
|
$addSmartCell($pdf, 81, $yDevis, 18, $articleText);
|
|
|
|
$lieuText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['lieu_nom'] ?? '');
|
|
$addSmartCell($pdf, 101, $yDevis, 23, $lieuText);
|
|
|
|
$defuntText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['defunt_nom'] ?? '');
|
|
$addSmartCell($pdf, 126, $yDevis, 18, $defuntText);
|
|
|
|
$addSmartCell($pdf, 146, $yDevis, 18, number_format($devis['montant_htc'], 2, '.', '') . chr(128), 'C');
|
|
$addSmartCell($pdf, 166, $yDevis, 18, number_format($devis['montant_tva'], 2, '.', '') . chr(128), 'C');
|
|
$addSmartCell($pdf, 186, $yDevis, 18, number_format($devis['montant_ttc'], 2, '.', '') . chr(128), 'C');
|
|
} else {
|
|
$addSmartCell($pdf, 6, $yDevis, 28, $devis['devis_full_number']);
|
|
|
|
// N° Dossier - UTILISER case_number
|
|
$addSmartCell($pdf, 36, $yDevis, 33, $devis['case_number'] ?? '');
|
|
|
|
// N° Commande - UTILISER order_number
|
|
$addSmartCell($pdf, 71, $yDevis, 33, $devis['order_number'] ?? '');
|
|
|
|
$addSmartCell($pdf, 106, $yDevis, 13, mb_convert_encoding($formatterDate->format($dateSoin), 'ISO-8859-1', 'UTF-8'));
|
|
|
|
$articleText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['article'] ?? 'SOINS');
|
|
$addSmartCell($pdf, 121, $yDevis, 13, $articleText);
|
|
|
|
$lieuText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['lieu_nom'] ?? '');
|
|
$addSmartCell($pdf, 136, $yDevis, 23, $lieuText);
|
|
|
|
$defuntText = \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['defunt_nom'] ?? '');
|
|
$addSmartCell($pdf, 161, $yDevis, 44, $defuntText);
|
|
}
|
|
}
|
|
|
|
private function addTableTotals($pdf, $totals)
|
|
{
|
|
$pdf->Line(5, 225, 205, 225);
|
|
$pdf->SetFont('Arial', 'B', 8);
|
|
|
|
$pdf->SetXY(5, 225);
|
|
$pdf->Cell(130, 8, 'TOTAL', 0, 0, 'C');
|
|
|
|
// POSITIONS CORRIGÉES
|
|
$pdf->SetXY(136, 225);
|
|
$pdf->Cell(18, 8, number_format($totals['ht'], 2, '.', '') . chr(128), 0, 0, 'C');
|
|
$pdf->SetXY(156, 225);
|
|
$pdf->Cell(18, 8, number_format($totals['tva'], 2, '.', '') . chr(128), 0, 0, 'C');
|
|
$pdf->SetXY(176, 225);
|
|
$pdf->Cell(14, 8, number_format($totals['ttc'], 2, '.', '') . chr(128), 0, 0, 'C');
|
|
|
|
// TOTAL TTC
|
|
$pdf->SetXY(155, 240);
|
|
$pdf->Cell(20, 8, 'TOTAL TTC', 0, 0, 'C');
|
|
$pdf->SetXY(176, 240);
|
|
$pdf->Cell(14, 8, number_format($totals['ttc'], 2, '.', '') . chr(128), 0, 0, 'C');
|
|
|
|
$lines = [
|
|
[155, 240, 155, 248],
|
|
[176, 240, 176, 248],
|
|
[190, 240, 190, 248],
|
|
[155, 240, 190, 240],
|
|
[155, 248, 190, 248]
|
|
];
|
|
|
|
foreach ($lines as $line) {
|
|
$pdf->Line($line[0], $line[1], $line[2], $line[3]);
|
|
}
|
|
}
|
|
}
|