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

177 lines
7.1 KiB
PHP

<?php
namespace OCA\Gestion\Service\Devis;
class DevisPdfTableRenderer
{
public function createDevisTable($pdf, $dataDevis, $config)
{
// Extraction des paramètres du tableau de configuration
$pagination = $config['pagination'];
$itemsToProcess = $config['itemsToProcess'];
$numPage = $config['numPage'];
$nbPage = $config['nbPage'];
$totals = &$config['totals']; // Référence pour modification
$sansMontant = $config['sansMontant'] ?? false; // false par défaut = afficher les montants
$this->drawTableStructure($pdf, $numPage, $nbPage, $sansMontant);
$this->addTableHeaders($pdf, $sansMontant);
$this->populateTableData($pdf, $dataDevis, $pagination['current_index'], $itemsToProcess, $totals, $sansMontant);
// Afficher les totaux seulement sur la dernière page ET si les montants sont affichés
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);
// Si c'est la dernière page ET qu'on affiche les montants, ligne de total à 225
$endY = ($numPage == $nbPage && !$sansMontant) ? 225 : 235;
if (!$sansMontant) {
// AVEC montants : toutes les colonnes
$verticalLines = [25, 45, 70, 90, 125, 155, 175, 190];
} else {
// SANS montants : seulement jusqu'à Défunt
$verticalLines = [35, 70, 105, 125, 155];
}
foreach ($verticalLines as $x) {
$pdf->Line($x, 105, $x, $endY);
}
}
private function addTableHeaders($pdf, $sansMontant)
{
$pdf->SetFont('Arial', 'B', 8);
if (!$sansMontant) {
// AVEC montants
$headers = [
[5, 20, mb_convert_encoding(html_entity_decode("N° Devis"), 'ISO-8859-1', 'UTF-8')],
[25, 20, mb_convert_encoding(html_entity_decode("N° Dossier"), 'ISO-8859-1', 'UTF-8')],
[45, 25, mb_convert_encoding(html_entity_decode("N° Commande"), 'ISO-8859-1', 'UTF-8')],
[70, 20, "Date"],
[90, 35, "Lieu du soin"],
[125, 30, \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport("Défunt")],
[155, 20, "H.T."],
[175, 15, "TVA 20%"],
[190, 15, "T.T.C"]
];
} else {
// SANS montants - colonnes plus larges
$headers = [
[5, 30, mb_convert_encoding(html_entity_decode("N° Devis"), 'ISO-8859-1', 'UTF-8')],
[35, 35, mb_convert_encoding(html_entity_decode("N° Dossier"), 'ISO-8859-1', 'UTF-8')],
[70, 35, mb_convert_encoding(html_entity_decode("N° Commande"), 'ISO-8859-1', 'UTF-8')],
[105, 20, "Date"],
[125, 30, "Lieu du soin"],
[155, 50, \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport("Défunt")]
];
}
foreach ($headers as $header) {
$pdf->SetXY($header[0], 106);
$pdf->Cell($header[1], 8, $header[2], 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 += 5;
}
}
private function addTableRow($pdf, $devis, $formatterDate, $dateSoin, $yDevis, $sansMontant)
{
$pdf->SetFont('Arial', '', 7);
if (!$sansMontant) {
// AVEC montants
$rowData = [
[6, 18, $devis['devis_full_number']],
[26, 18, $devis['calendar_uuid']],
[46, 23, $devis['num_commande']],
[71, 18, mb_convert_encoding($formatterDate->format($dateSoin), 'ISO-8859-1', 'UTF-8')],
[91, 33, \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['lieu_nom'])],
[126, 28, \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['defunt_nom'])],
[156, 18, number_format($devis['montant_htc'], 2, '.', '') . chr(128), 'C'],
[176, 13, number_format($devis['montant_tva'], 2, '.', '') . chr(128), 'C'],
[191, 13, number_format($devis['montant_ttc'], 2, '.', '') . chr(128), 'C']
];
} else {
// SANS montants - colonnes plus larges
$rowData = [
[6, 28, $devis['devis_full_number']],
[36, 33, $devis['calendar_uuid']],
[71, 33, $devis['num_commande']],
[106, 18, mb_convert_encoding($formatterDate->format($dateSoin), 'ISO-8859-1', 'UTF-8')],
[126, 28, \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['lieu_nom'])],
[156, 48, \OCA\Gestion\Helpers\FileExportHelpers::FormatTextForExport($devis['defunt_nom'])]
];
}
foreach ($rowData as $data) {
$pdf->SetXY($data[0], $yDevis);
$align = isset($data[3]) ? $data[3] : '';
$pdf->Cell($data[1], 5, $data[2], 0, 0, $align);
}
}
private function addTableTotals($pdf, $totals)
{
// Cette méthode n'est appelée que si on affiche les montants (!$sansMontant)
$pdf->Line(5, 225, 205, 225);
$pdf->SetFont('Arial', 'B', 8);
$pdf->SetXY(5, 225);
$pdf->Cell(150, 8, 'TOTAL', 0, 0, 'C');
$pdf->SetXY(156, 225);
$pdf->Cell(18, 8, number_format($totals['ht'], 2, '.', '') . chr(128), 0, 0, 'C');
$pdf->SetXY(176, 225);
$pdf->Cell(13, 8, number_format($totals['tva'], 2, '.', '') . chr(128), 0, 0, 'C');
$pdf->SetXY(191, 225);
$pdf->Cell(13, 8, number_format($totals['ttc'], 2, '.', '') . chr(128), 0, 0, 'C');
$pdf->SetXY(170, 240);
$pdf->Cell(20, 8, 'TOTAL TTC', 0, 0, 'C');
$pdf->SetXY(190, 240);
$pdf->Cell(15, 8, number_format($totals['ttc'], 2, '.', '') . chr(128), 0, 0, 'C');
$lines = [
[170, 240, 170, 248],
[190, 240, 190, 248],
[205, 240, 205, 248],
[170, 240, 205, 240],
[170, 248, 205, 248]
];
foreach ($lines as $line) {
$pdf->Line($line[0], $line[1], $line[2], $line[3]);
}
}
}