286 lines
12 KiB
PHP
Executable File
286 lines
12 KiB
PHP
Executable File
<?php
|
|
require_once 'Framework/Controleurrequete.php';
|
|
require_once 'Modele/Statistique.php';
|
|
|
|
class ControleurAjaxstatsmaladiefichetechniquecinqpdf extends Controleur
|
|
{
|
|
private $detailsp;
|
|
private $pdf;
|
|
private $currentPage = 1;
|
|
private $maxPages = 10; // Maximum de pages autorisées
|
|
|
|
public function __construct() {
|
|
$this->detailsp = new Statistique();
|
|
}
|
|
|
|
/**
|
|
* Vérifie les données avant création du PDF
|
|
*/
|
|
private function validerDonneesAvantPDF($idPolice, $debutStat, $finStat, $entete) {
|
|
// Vérification 1: Paramètres obligatoires
|
|
if (empty($debutStat) || empty($finStat) || empty($idPolice)) {
|
|
throw new Exception(_("Paramètres de filtrage incomplets"));
|
|
}
|
|
|
|
// Vérification 2: Dates valides et cohérentes
|
|
if ($debutStat > $finStat) {
|
|
throw new Exception(_("La date de début doit être antérieure à la date de fin"));
|
|
}
|
|
|
|
// Vérification 3: Période trop longue (max 2 ans)
|
|
$diff = (strtotime($finStat) - strtotime($debutStat)) / (60 * 60 * 24);
|
|
if ($diff > 730) {
|
|
throw new Exception(_("La période ne peut pas dépasser 2 ans"));
|
|
}
|
|
|
|
// Vérification 4: Données d'entête disponibles
|
|
if (empty($entete)) {
|
|
throw new Exception(_("Aucune donnée disponible pour cette police"));
|
|
}
|
|
|
|
// Vérification 5: Format des dates
|
|
if (!DateTime::createFromFormat('Y-m-d', $debutStat) ||
|
|
!DateTime::createFromFormat('Y-m-d', $finStat)) {
|
|
throw new Exception(_("Format de date invalide"));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Vérifie si on peut ajouter une nouvelle page
|
|
*/
|
|
private function peutAjouterPage() {
|
|
if ($this->currentPage >= $this->maxPages) {
|
|
throw new Exception(_("Le document a atteint le nombre maximum de pages (" . $this->maxPages . ")"));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Ajoute une page avec vérification
|
|
*/
|
|
private function ajouterPageAvecVerification($orientation = 'L') {
|
|
$this->peutAjouterPage();
|
|
$this->pdf->AddPage($orientation);
|
|
$this->currentPage++;
|
|
}
|
|
|
|
/**
|
|
* Vérifie les données des dépenses
|
|
*/
|
|
private function validerDonneesDepenses($p_factures) {
|
|
if (empty($p_factures)) {
|
|
throw new Exception(_("Aucune donnée de dépenses disponible pour la période sélectionnée"));
|
|
}
|
|
|
|
// Vérifier que nous avons au moins les données minimales requises
|
|
$libellesRequises = ['PRIME NETTE', 'CHARGEMENT', 'SINISTRES'];
|
|
$libellesTrouves = array_unique(array_column($p_factures, 'libelle'));
|
|
|
|
$manquantes = array_diff($libellesRequises, $libellesTrouves);
|
|
if (!empty($manquantes)) {
|
|
throw new Exception(_("Données incomplètes : libellés manquants - " . implode(', ', $manquantes)));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
try {
|
|
// Récupération et validation des paramètres
|
|
$idPolice = $this->requete->getParametreFormulaire("idPolice");
|
|
$idCollege = $this->requete->getParametreFormulaire("idCollege");
|
|
$chargeravantapres = $this->requete->getParametreFormulaire("chargeravantapres");
|
|
$debutStat = $this->requete->getParametreDate("debutStat");
|
|
$finStat = $this->requete->getParametreDate("finStat");
|
|
$Chargement = $this->requete->getParametreFormulaire("Chargement");
|
|
$depenses = $this->requete->getParametreFormulaire("depenses", "numerique");
|
|
|
|
// Normalisation des paramètres
|
|
$idCollege = empty($idCollege) ? null : $idCollege;
|
|
$idPolice = empty($idPolice) ? null : $idPolice;
|
|
|
|
// Vérification préliminaire des paramètres essentiels
|
|
if (empty($idPolice)) {
|
|
throw new Exception(_("L'identifiant de police est requis"));
|
|
}
|
|
|
|
// Récupération des données d'entête
|
|
$entete = $this->detailsp->detailrapportspperiodepdfentete($idPolice);
|
|
|
|
// VALIDATION 1: Vérification avant création PDF
|
|
$this->validerDonneesAvantPDF($idPolice, $debutStat, $finStat, $entete);
|
|
|
|
// Stockage en session
|
|
$_SESSION['debutStat'] = $debutStat;
|
|
$_SESSION['finStat'] = $finStat;
|
|
$_SESSION['depenses'] = $depenses;
|
|
|
|
// Préparation des paramètres pour la requête
|
|
$p_datedebut = $debutStat;
|
|
$p_datefin = $finStat;
|
|
$p_garant = $entete['codeGcAssureur'];
|
|
$p_limite = $depenses;
|
|
$p_id_client = null;
|
|
$p_id_college = $idCollege;
|
|
$p_id = $entete['idPolice'];
|
|
|
|
// Récupération des données de dépenses
|
|
$p_factures = $this->detailsp->evolutiondesdepenses(
|
|
$p_garant, $p_id_client, $p_id, $p_id_college,
|
|
$p_datedebut, $p_datefin, $Chargement, $chargeravantapres
|
|
);
|
|
|
|
// VALIDATION 2: Vérification des données de dépenses
|
|
$this->validerDonneesDepenses($p_factures);
|
|
|
|
// Initialisation du PDF avec gestion du pied de page
|
|
$this->pdf = new class extends FPDF {
|
|
function Header() {
|
|
// Vous pouvez ajouter un en-tête personnalisé ici si besoin
|
|
}
|
|
|
|
function Footer() {
|
|
// Positionnement à 1.5 cm du bas
|
|
$this->SetY(-15);
|
|
// Police Arial italique 8
|
|
$this->SetFont('Arial', 'I', 9);
|
|
$this->SetTextColor(100, 100, 100);
|
|
// Numéro de page centré - utilisation de {nb} qui est géré automatiquement par FPDF
|
|
$this->Cell(0, 10, convertirc(_('Page')) . ' ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
|
|
$this->SetTextColor(0);
|
|
}
|
|
};
|
|
|
|
// Utilisation simple d'AliasNbPages sans gestion manuelle
|
|
$this->pdf->AliasNbPages();
|
|
|
|
// VALIDATION 3: Ajout de la première page avec vérification
|
|
$this->ajouterPageAvecVerification('L');
|
|
|
|
// Configuration des marges
|
|
$this->pdf->SetMargins(10, 10, 10);
|
|
$logoPath = $_SESSION['dossierLogo'];
|
|
$this->pdf->Image($logoPath, 140, 3, 15);
|
|
|
|
// En-tête du document
|
|
$this->pdf->SetFont('Arial', 'B', 7);
|
|
$this->pdf->Cell(0, 5, convertirc(_('FICHE TECHNIQUE No 5')), 0, 1, 'L', false);
|
|
|
|
$this->pdf->SetY(10);
|
|
$this->pdf->SetFont('Arial', '', 7);
|
|
$this->pdf->Cell(0, 5, convertirc(_('Edition du') . ': ' . heureCouranteLang($_SESSION['lang'])), 0, 1, 'R', false);
|
|
|
|
$this->pdf->Ln(2);
|
|
$this->pdf->SetFont('Arial', 'B', 13);
|
|
$this->pdf->SetFillColor(182, 216, 242);
|
|
$this->pdf->Cell(0, 8, convertirc(_("EVOLUTION MENSUELLE DES DEPENSES")), 1, 0, 'C', true);
|
|
|
|
$this->pdf->Ln(8);
|
|
$this->pdf->SetFont('Arial', 'B', 11);
|
|
$this->pdf->SetTextColor(0);
|
|
$this->pdf->Ln(5);
|
|
$this->pdf->Cell(0, 6, convertirc(_('PERIODE COMPTABLE CONSIDEREE') . ': ' . dateLang($debutStat, $_SESSION['lang']) . ' ' . _('AU') . ' ' . dateLang($finStat, $_SESSION['lang'])), 0, 1, 'C', false);
|
|
|
|
$this->pdf->Ln(5);
|
|
$this->pdf->SetFont('Arial', 'BI', 11);
|
|
$this->pdf->SetTextColor(255, 0, 0);
|
|
$this->pdf->Cell(120, 8, convertirc(_($entete['garant'])), 0, 0, 'C', false);
|
|
|
|
$this->pdf->SetFont('Arial', 'BI', 11);
|
|
$this->pdf->SetTextColor(155, 38, 182);
|
|
$this->pdf->Cell(100, 8, convertirc(_($entete['souscripteur'])), 0, 1, 'C', false);
|
|
|
|
$this->pdf->SetFont('Arial', 'BUI', 11);
|
|
$this->pdf->SetTextColor(0, 0, 255);
|
|
$this->pdf->Cell(0, 8, convertirc(_('POLICE') . ':' . $entete['numeroPolice'] . ' / ' . $entete['libellePolice']), 0, 1, 'C', false);
|
|
$this->pdf->SetTextColor(0, 0, 0);
|
|
|
|
$this->pdf->Ln(5);
|
|
|
|
// Construction du tableau
|
|
$this->construireTableauDepenses($p_factures);
|
|
|
|
// Génération du fichier PDF
|
|
$fichier = "Temp/TMP_CUMUL_SP_FICHE5_" . uniqid() . ".pdf";
|
|
$this->pdf->Output($fichier, "F");
|
|
|
|
// Génération du lien de téléchargement
|
|
$t_html = '<div id="div_export_b" class="alert alert-info">';
|
|
$t_html .= '<a style="font-size:15pt;" href="' . $fichier . '" target="_blank">' . _("TELECHARGER FICHE 05") . '</a>';
|
|
$t_html .= '</div>';
|
|
|
|
echo $t_html;
|
|
|
|
} catch (Exception $e) {
|
|
// Gestion centralisée des erreurs
|
|
$erreur_html = '<div class="alert alert-danger">';
|
|
$erreur_html .= '<strong>' . _("Erreur") . ':</strong> ' . $e->getMessage();
|
|
$erreur_html .= '</div>';
|
|
|
|
echo $erreur_html;
|
|
error_log("Erreur génération PDF Fiche 05: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Construit le tableau des dépenses avec gestion des pages
|
|
*/
|
|
private function construireTableauDepenses($p_factures) {
|
|
$hl = 7;
|
|
$this->pdf->SetFont('Arial', 'B', 7);
|
|
$this->pdf->SetFillColor(182, 216, 242);
|
|
|
|
// En-tête du tableau
|
|
$enTetes = [
|
|
_('Libellés'), _("Janvier"), _("Février"), _("Mars"),
|
|
_("Avril"), _("Mai"), _("Juin"), _("Juillet"),
|
|
_("Août"), _("Septembre"), _("Octobre"), _("Novembre"),
|
|
_("Décembre"), _("Totaux")
|
|
];
|
|
|
|
$largeurs = [35, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19];
|
|
|
|
for ($i = 0; $i < count($enTetes); $i++) {
|
|
$this->pdf->Cell($largeurs[$i], $hl, convertirc($enTetes[$i]), 1, 0, 'C', true);
|
|
}
|
|
|
|
$this->pdf->Ln();
|
|
|
|
// Données du tableau
|
|
$hl = 6;
|
|
foreach ($p_factures as $index => $dep) {
|
|
// Vérification avant d'ajouter une nouvelle ligne (dépassement de page)
|
|
if ($this->pdf->GetY() + $hl > 270) {
|
|
$this->ajouterPageAvecVerification('L');
|
|
|
|
// Réafficher l'en-tête du tableau sur la nouvelle page
|
|
$this->pdf->SetFont('Arial', 'B', 7);
|
|
$this->pdf->SetFillColor(182, 216, 242);
|
|
for ($i = 0; $i < count($enTetes); $i++) {
|
|
$this->pdf->Cell($largeurs[$i], $hl, convertirc($enTetes[$i]), 1, 0, 'C', true);
|
|
}
|
|
$this->pdf->Ln();
|
|
}
|
|
|
|
$this->pdf->SetFont('Arial', 'B', 7);
|
|
$this->pdf->Cell(35, $hl, convertirc(_($dep['libelle'])), 1, 0, 'L', false);
|
|
$this->pdf->SetFont('Arial', '', 7);
|
|
|
|
// Déterminer le format d'affichage
|
|
$estRatio = in_array($dep['libelle'], ['RAPP.S/P', 'RAPP.S/P CUMULE']);
|
|
|
|
$colonnes = [_('JANVIER'), _('FEVRIER'), _('MARS'), _('AVRIL'), _('MAI'), _('JUIN'),
|
|
_('JUILLET'), _('AOUT'), _('SEPTEMBRE'), _('OCTOBRE'), _('NOVEMBRE'), _('DECEMBRE'), _('TOTAUX')];
|
|
|
|
foreach ($colonnes as $colonne) {
|
|
$valeur = $dep[$colonne] ?? 0;
|
|
$texte = $estRatio ? convertirc(_($valeur)) : convertirc(format_N($valeur));
|
|
$this->pdf->Cell(19, $hl, $texte, 1, 0, 'R', false);
|
|
}
|
|
$this->pdf->Ln();
|
|
}
|
|
}
|
|
} |