Files
crm_ecosplay/src/Service/Pdf/RapportFinancierPdf.php

356 lines
12 KiB
PHP
Raw Normal View History

feat: comptabilite + prestataires + rapport financier + stats dynamiques Comptabilite (Super Admin) : - ComptabiliteController avec 7 exports CSV/JSON compatibles SAGE (journal ventes, grand livre, FEC, balance agee, reglements, commissions Stripe 1.5%+0.25E, couts services) - Export PDF via ComptaPdf (FPDF) avec bloc legal pre-rempli, tableau pagine, champ signature DocuSeal - Signature electronique DocuSeal + callback + envoi email signe avec template dedie (compta_export_signed.html.twig) - Rapport financier public (RapportFinancierPdf) : recettes par service, depenses (Stripe, infra, prestataires), bilan excedent/deficit - Codes comptables clients EC-XXXX (plus de 411xxx) Prestataires (Super Admin) : - Entite Prestataire (raisonSociale, siret, email, phone, adresse) - Entite FacturePrestataire (numFacture, montantHt, montantTtc, year, month, isPaid, PDF via Vich) - CRUD complet avec recherche SIRET via proxy API data.gouv.fr - Commande cron app:reminder:factures-prestataire (5 du mois) - Factures prestataires integrees dans export couts services - Sidebar Super Admin : entree Prestataires + Comptabilite Stats (/admin/stats) : - Cout prestataire dynamique depuis FacturePrestataire - Fusion Infra + Prestataire en "Cout de fonctionnement" - Commission Stripe corrigee (1.5% + 0.25E par transaction) Divers : - DocuSealService::sendComptaForSignature() + getApi() - Customer::generateCodeComptable() format EC-XXXX-XXXXX - Protection double prefixe EC- a la creation client - Bouton regenerer PDF cache quand advert state=accepted - Modals sans script inline (data-modal-open/close dans app.js) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 23:39:31 +02:00
<?php
namespace App\Service\Pdf;
use setasign\Fpdi\Fpdi;
use Symfony\Component\HttpKernel\KernelInterface;
if (!\defined('EURO')) {
\define('EURO', \chr(128));
}
class RapportFinancierPdf extends Fpdi
{
private string $periodFrom;
private string $periodTo;
/** @var array<string, float> */
private array $recettes = [];
/** @var array<string, float> */
private array $depenses = [];
private float $totalRecettes = 0.0;
private float $totalDepenses = 0.0;
public function __construct(
private readonly KernelInterface $kernel,
string $periodFrom,
string $periodTo,
) {
parent::__construct();
$this->periodFrom = $periodFrom;
$this->periodTo = $periodTo;
$this->SetTitle($this->enc('Rapport financier - '.$periodFrom.' au '.$periodTo));
$this->SetAuthor($this->enc('Association E-Cosplay'));
$this->SetCreator('CRM E-Cosplay');
}
/**
* @param array<string, float> $recettes Libelle => montant
* @param array<string, float> $depenses Libelle => montant
*/
public function setData(array $recettes, array $depenses): void
{
$this->recettes = $recettes;
$this->depenses = $depenses;
$this->totalRecettes = array_sum($recettes);
$this->totalDepenses = array_sum($depenses);
}
public function generate(): void
{
$this->AliasNbPages();
$this->AddPage();
$this->writeContextBlock();
$this->writeRecettes();
$this->writeDepenses();
$this->writeBilan();
$this->writeSignatureBlock();
}
// ---------------------------------------------------------------
// Header / Footer
// ---------------------------------------------------------------
public function Header(): void
{
$logo = $this->kernel->getProjectDir().'/public/logo.jpg';
if (file_exists($logo)) {
$this->Image($logo, 10, 8, 45);
}
$this->SetFont('Arial', 'B', 18);
$this->SetXY(60, 10);
$this->Cell(0, 8, $this->enc('RAPPORT FINANCIER'), 0, 1, 'L');
$this->SetFont('Arial', '', 11);
$this->SetXY(60, 19);
$this->Cell(0, 5, $this->enc('Periode : du '.$this->periodFrom.' au '.$this->periodTo), 0, 1, 'L');
$formatter = new \IntlDateFormatter(
'fr_FR',
\IntlDateFormatter::FULL,
\IntlDateFormatter::NONE,
'Europe/Paris',
\IntlDateFormatter::GREGORIAN
);
$this->SetFont('Arial', '', 9);
$this->SetTextColor(120, 120, 120);
$this->SetXY(60, 25);
$this->Cell(0, 5, $this->enc('Edite le '.$formatter->format(new \DateTime())), 0, 1, 'L');
$this->SetTextColor(0, 0, 0);
// Mention document public
$this->SetFont('Arial', 'I', 8);
$this->SetTextColor(100, 100, 100);
$this->SetXY(10, 35);
$this->Cell(0, 4, $this->enc('Document public - Association loi 1901 - Les montants sont presentes de maniere synthetique.'), 0, 1, 'C');
$this->SetTextColor(0, 0, 0);
$this->Ln(5);
}
public function Footer(): void
{
$this->SetY(-22);
$this->SetDrawColor(253, 140, 4);
$this->Line(15, $this->GetY(), 195, $this->GetY());
$this->Ln(3);
$this->SetFont('Arial', '', 8);
$this->SetTextColor(0, 0, 0);
$this->Cell(190, 4, $this->enc('42 rue de Saint-Quentin - 02800 BEAUTOR - Tel: 06 79 34 88 02 - contact@e-cosplay.fr'), 0, 1, 'C');
$this->Cell(190, 4, $this->enc('Association E-Cosplay - N SIRET 943 121 517 00011 - CODE APE 9329Z - RNA W022006988'), 0, 1, 'C');
$this->SetFont('Arial', 'I', 7);
$this->SetTextColor(150, 150, 150);
$this->Cell(190, 4, $this->enc('Page ').$this->PageNo().' / {nb}', 0, 0, 'C');
}
// ---------------------------------------------------------------
// Bloc legal
// ---------------------------------------------------------------
private function writeContextBlock(): void
{
$this->SetY(45);
$this->SetDrawColor(200, 200, 200);
$this->Cell(0, 0.5, '', 'T', 1, 'L');
$this->Ln(2);
$this->SetFont('Arial', 'B', 11);
$this->Cell(0, 6, $this->enc('INFORMATIONS LEGALES'), 0, 1, 'C');
$this->Ln(1);
$labelW = 55;
$this->SetFont('Arial', '', 9);
$this->Cell($labelW, 5, $this->enc('Association :'), 0, 0, 'L');
$this->SetFont('Arial', 'B', 9);
$this->Cell(0, 5, $this->enc('E-Cosplay - Association loi 1901 - RNA W022006988'), 0, 1, 'L');
$this->SetFont('Arial', '', 9);
$this->Cell($labelW, 5, $this->enc('Siege social :'), 0, 0, 'L');
$this->Cell(0, 5, $this->enc('42 rue de Saint-Quentin, 02800 Beautor'), 0, 1, 'L');
$this->Cell($labelW, 5, $this->enc('SIRET :'), 0, 0, 'L');
$this->Cell(0, 5, $this->enc('943 121 517 00011'), 0, 1, 'L');
$this->Cell($labelW, 5, $this->enc('Activite :'), 0, 0, 'L');
$this->Cell(0, 5, $this->enc('Services numeriques (hebergement, noms de domaine, messagerie)'), 0, 1, 'L');
$this->Ln(2);
$this->Cell(0, 0.5, '', 'T', 1, 'L');
$this->Ln(5);
}
// ---------------------------------------------------------------
// Recettes (montant entrant)
// ---------------------------------------------------------------
private function writeRecettes(): void
{
$this->SetFont('Arial', 'B', 12);
$this->SetTextColor(22, 163, 74);
$this->Cell(0, 7, $this->enc('RECETTES (MONTANTS ENTRANTS)'), 0, 1, 'L');
$this->SetTextColor(0, 0, 0);
$this->Ln(2);
// En-tete
$this->SetFont('Arial', 'B', 9);
$this->SetFillColor(35, 35, 35);
$this->SetTextColor(255, 255, 255);
$this->Cell(120, 7, $this->enc(' Service'), 1, 0, 'L', true);
$this->Cell(50, 7, $this->enc('Montant HT'), 1, 1, 'R', true);
$this->SetTextColor(0, 0, 0);
// Lignes
$this->SetFont('Arial', '', 10);
$fill = false;
foreach ($this->recettes as $label => $montant) {
$this->SetFillColor(245, 245, 240);
$this->Cell(120, 7, $this->enc(' '.$label), 'B', 0, 'L', $fill);
$this->Cell(50, 7, number_format($montant, 2, ',', ' ').' '.EURO, 'B', 1, 'R', $fill);
$fill = !$fill;
}
// Total
$this->SetFont('Arial', 'B', 11);
$this->SetFillColor(22, 163, 74);
$this->SetTextColor(255, 255, 255);
$this->Cell(120, 8, $this->enc(' TOTAL RECETTES'), 0, 0, 'L', true);
$this->Cell(50, 8, number_format($this->totalRecettes, 2, ',', ' ').' '.EURO, 0, 1, 'R', true);
$this->SetTextColor(0, 0, 0);
$this->Ln(8);
}
// ---------------------------------------------------------------
// Depenses (montant sortant)
// ---------------------------------------------------------------
private function writeDepenses(): void
{
$this->SetFont('Arial', 'B', 12);
$this->SetTextColor(220, 38, 38);
$this->Cell(0, 7, $this->enc('DEPENSES (MONTANTS SORTANTS)'), 0, 1, 'L');
$this->SetTextColor(0, 0, 0);
$this->Ln(2);
// En-tete
$this->SetFont('Arial', 'B', 9);
$this->SetFillColor(35, 35, 35);
$this->SetTextColor(255, 255, 255);
$this->Cell(120, 7, $this->enc(' Poste de depense'), 1, 0, 'L', true);
$this->Cell(50, 7, $this->enc('Montant'), 1, 1, 'R', true);
$this->SetTextColor(0, 0, 0);
// Lignes
$this->SetFont('Arial', '', 10);
$fill = false;
foreach ($this->depenses as $label => $montant) {
$this->SetFillColor(245, 245, 240);
$this->Cell(120, 7, $this->enc(' '.$label), 'B', 0, 'L', $fill);
$this->Cell(50, 7, number_format($montant, 2, ',', ' ').' '.EURO, 'B', 1, 'R', $fill);
$fill = !$fill;
}
// Total
$this->SetFont('Arial', 'B', 11);
$this->SetFillColor(220, 38, 38);
$this->SetTextColor(255, 255, 255);
$this->Cell(120, 8, $this->enc(' TOTAL DEPENSES'), 0, 0, 'L', true);
$this->Cell(50, 8, number_format($this->totalDepenses, 2, ',', ' ').' '.EURO, 0, 1, 'R', true);
$this->SetTextColor(0, 0, 0);
$this->Ln(8);
}
// ---------------------------------------------------------------
// Bilan
// ---------------------------------------------------------------
private function writeBilan(): void
{
$marge = $this->totalRecettes - $this->totalDepenses;
$isPositif = $marge >= 0;
$this->SetDrawColor(200, 200, 200);
$this->Cell(0, 0.5, '', 'T', 1, 'L');
$this->Ln(3);
$this->SetFont('Arial', 'B', 13);
$this->Cell(0, 7, $this->enc('BILAN'), 0, 1, 'C');
$this->Ln(2);
// Tableau bilan
$this->SetFont('Arial', '', 11);
$col1 = 90;
$col2 = 80;
$this->SetX(15);
$this->Cell($col1, 8, $this->enc('Total des recettes :'), 0, 0, 'L');
$this->SetFont('Arial', 'B', 11);
$this->SetTextColor(22, 163, 74);
$this->Cell($col2, 8, '+ '.number_format($this->totalRecettes, 2, ',', ' ').' '.EURO, 0, 1, 'R');
$this->SetTextColor(0, 0, 0);
$this->SetFont('Arial', '', 11);
$this->SetX(15);
$this->Cell($col1, 8, $this->enc('Total des depenses :'), 0, 0, 'L');
$this->SetFont('Arial', 'B', 11);
$this->SetTextColor(220, 38, 38);
$this->Cell($col2, 8, '- '.number_format($this->totalDepenses, 2, ',', ' ').' '.EURO, 0, 1, 'R');
$this->SetTextColor(0, 0, 0);
$this->Ln(1);
$this->SetDrawColor(0, 0, 0);
$this->Line(15, $this->GetY(), 185, $this->GetY());
$this->Ln(2);
// Resultat
$this->SetFont('Arial', 'B', 14);
$this->SetX(15);
$this->Cell($col1, 10, $this->enc('Resultat net :'), 0, 0, 'L');
$this->SetTextColor($isPositif ? 22 : 220, $isPositif ? 163 : 38, $isPositif ? 74 : 38);
$sign = $isPositif ? '+ ' : '- ';
$this->Cell($col2, 10, $sign.number_format(abs($marge), 2, ',', ' ').' '.EURO, 0, 1, 'R');
$this->SetTextColor(0, 0, 0);
// Statut
$this->Ln(3);
$this->SetFont('Arial', 'B', 12);
if ($isPositif) {
$this->SetFillColor(22, 163, 74);
$this->SetTextColor(255, 255, 255);
$label = $marge > $this->totalRecettes * 0.3 ? 'EXCEDENT' : 'EQUILIBRE';
} else {
$this->SetFillColor(220, 38, 38);
$this->SetTextColor(255, 255, 255);
$label = 'DEFICIT';
}
$this->Cell(0, 10, $this->enc($label), 0, 1, 'C', true);
$this->SetTextColor(0, 0, 0);
$this->Ln(5);
}
// ---------------------------------------------------------------
// Signature
// ---------------------------------------------------------------
private function writeSignatureBlock(): void
{
if ($this->GetY() + 40 > $this->GetPageHeight() - 25) {
$this->AddPage();
}
$this->SetDrawColor(200, 200, 200);
$this->Cell(0, 0.5, '', 'T', 1, 'L');
$this->Ln(3);
$formatter = new \IntlDateFormatter(
'fr_FR',
\IntlDateFormatter::LONG,
\IntlDateFormatter::NONE,
'Europe/Paris',
\IntlDateFormatter::GREGORIAN
);
$this->SetFont('Arial', '', 9);
$this->Cell(0, 5, $this->enc('Fait a Beautor, le '.$formatter->format(new \DateTime())), 0, 1, 'L');
$this->Ln(2);
$this->SetFont('Arial', 'B', 9);
$this->Cell(0, 5, $this->enc('Signature du responsable :'), 0, 1, 'L');
$this->Ln(1);
$this->SetAutoPageBreak(false);
$this->SetFont('Arial', '', 10);
$this->SetTextColor(0, 0, 0);
$this->Cell(60, 20, '{{Sign;type=signature;role=First Party}}', 0, 1, 'L');
}
// ---------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------
private function enc(string $text): string
{
return mb_convert_encoding($text, 'Windows-1252', 'UTF-8');
}
}