implémentation impression reçus

FossilOrigin-Name: 81f54c69ca30c343f75b4190728e55d7679b6580724471ec69a6cca70f8f94ea
This commit is contained in:
engel 2023-01-31 18:35:27 +00:00
parent f5130172f0
commit 0e3c356d1a
7 changed files with 609 additions and 267 deletions

View file

@ -11,39 +11,34 @@ use Garradin\Plugin\RecusFiscaux\Personne;
// signature
$signature =
(null !== $plugin->getConfig('signature')) ?
Files::get($plugin->getConfig('signature'))->fullpath() :
"";
(null !== $plugin->getConfig('signature')) ?
\KD2\HTTP::getScheme() . '://' . \KD2\HTTP::getHost() . WWW_URI . "/" . $plugin->getConfig('signature') :
"";
// logo
$logo_file = Files::get(File::CONTEXT_CONFIG . '/logo.png');
$config = Config::getInstance();
$logo_asso =
(null !== $logo_file) ?
Files::get($logo_file->path)->fullpath() :
"";
(null !== $config->fileURL('logo')) ?
$config->fileURL('logo') :
"";
// articles du CGI
$articlesCGI = array();
foreach ($plugin->getConfig('articlesCGI') as $article)
{
foreach ($plugin->getConfig('articlesCGI') as $article) {
if ($article->valeur == 1) {
$articlesCGI[] = $article->titre;
}
}
$nbArticles = count($articlesCGI);
if ($nbArticles == 1)
{
if ($nbArticles == 1) {
$texteArticles = 'à larticle ' . $articlesCGI[0];
}
elseif ($nbArticles > 1)
{
} elseif ($nbArticles > 1) {
$texteArticles = 'aux articles ';
for ($i = 0; $i < $nbArticles; ++$i) {
$texteArticles .= $articlesCGI[$i];
if ($i < $nbArticles - 2) {
$texteArticles .= ", ";
}
else if ($i == $nbArticles - 2) {
} else if ($i == $nbArticles - 2) {
$texteArticles .= " et ";
}
}
@ -52,23 +47,8 @@ elseif ($nbArticles > 1)
// libellés pour les taux de réduction
$libelles_taux = Utils::getLignesReduction($plugin->getConfig('reduction'));
// numérotation des reçus
$configNum = $plugin->getConfig('numerotation');
error_log("config num = " . print_r($configNum, true));
$prefixeNum = "";
if (isset($configNum->prefixe) && $configNum->prefixe != "") {
$prefixeNum = $configNum->prefixe;
}
if (isset($configNum->annee) && $configNum->annee) {
if ($prefixeNum != "") { $prefixeNum .= "-"; }
$prefixeNum .= $_SESSION['annee_recu'];
}
if (isset($configNum->sequentiel) && $configNum->sequentiel) {
if (isset($configNum->valeur_init) && $configNum->valeur_init !="") {
$numero_sequentiel = $configNum->valeur_init;
} else {
$numero_sequentiel = 1;
}
}
// filtrer les versements sélectionnés
$lesLignes = f('selected');
@ -78,125 +58,178 @@ foreach ($lesLignes as $ligne) {
}
// cumuler les versements
if ($_GET['type'] == 'personne')
{
if ($_GET['type'] == 'personne') {
$totalPersonnes = cumulerVersementsPersonne($versementsSelectionnes);
}
elseif ($_GET['type'] == 'activite')
{
} elseif ($_GET['type'] == 'activite') {
$totalPersonnes = cumulerVersementsTarif($versementsSelectionnes);
}
// générer les reçus
$listeFichiersPDF = array();
$fmt = new \NumberFormatter('fr_FR', \NumberFormatter::SPELLOUT);
foreach ($totalPersonnes as $idPersonne => $personne)
{
$tpl = new UserTemplate();
$tpl->setSource(PLUGIN_ROOT . '/templates/recu.skel');
if ($_GET['format'] == 'pdf') {
genererRecusPDF($totalPersonnes,
$signature,
$logo_asso,
$texteArticles,
$plugin,
$configNum,
$libelles_taux
);
} else if ($_GET['format'] == 'print') {
generererRecusHTML($tpl,
$totalPersonnes,
$signature,
$logo_asso,
$texteArticles,
$plugin,
$configNum,
$libelles_taux
);
} else {
// Erreur : format inconnu ; ne devrait pas se produire
}
$tpl->assignArray(compact('signature', 'logo_asso', 'texteArticles'));
function genererRecusPDF($totalPersonnes,
$signature,
$logo_asso,
$texteArticles,
$plugin,
$configNum,
$libelles_taux
)
{
$listeFichiersPDF = array();
$fmt = new \NumberFormatter('fr_FR', \NumberFormatter::SPELLOUT);
$prefixeNum = getNumPrefixe($configNum);
$numero_sequentiel = getNumSequentiel($configNum);
foreach ($totalPersonnes as $idPersonne => $personne) {
$tpl = new UserTemplate();
$tpl->setSource(PLUGIN_ROOT . '/templates/recu.skel');
$tpl->assignArray(compact('signature', 'logo_asso', 'texteArticles'));
$tpl->assign('objet_asso', $plugin->getConfig('objet_asso'));
$tpl->assign('nom_responsable', $plugin->getConfig('nom_responsable'));
$tpl->assign('fonction_responsable', $plugin->getConfig('fonction_responsable'));
$tpl->assign('ville_asso', $plugin->getConfig('ville_asso'));
$tpl->assign('nom', $personne->nomPrenom);
$tpl->assign('adresse', $personne->adresse);
$tpl->assign('code_postal', $personne->codePostal);
$tpl->assign('ville', $personne->ville);
$tpl->assign('date', date("j/m/Y"));
// numéro de reçu
$tpl->assign('numero',
faireNumeroRecu($prefixeNum,
$configNum->membre,
$personne->numero,
$numero_sequentiel));
// adresse de courriel
if ($plugin->getConfig('imprimerCourriel')) {
$courriel = $personne->courriel;
} else {
$courriel = "";
}
$tpl->assign('courriel', $courriel);
// les versements
$tpl->registerSection(
'versements',
function () use ($personne, $libelles_taux, $fmt) {
foreach ($personne->versements as $taux => $versement) {
$ligne['montant'] = $versement->montant;
$ligne['euros'] = $fmt->format((int)($versement->montant / 100));
if ($versement->montant % 100 != 0) {
$ligne['cents'] = $fmt->format($versement->montant % 100);
} else {
$ligne['cents'] = "";
}
$ligne['libelle'] = $libelles_taux[$taux];
$ligne['dateMin'] = date("d/m/Y", $versement->dateMin);
$ligne['dateMax'] = date("d/m/Y", $versement->dateMax);
yield $ligne;
}
}
);
// mentions complémentaires
$complements = mentionsComplémentaires();
$tpl->registerSection(
'informations',
function () use ($complements) {
foreach ($complements as $elem) {
yield (array) $elem;
}
}
);
// fabriquer le fichier PDF
genererPDF($tpl->fetch(), $personne->nomPrenom, $listeFichiersPDF);
}
// faire une archive zip
$fichierZip = Utils::makeArchive(
$listeFichiersPDF,
$_SESSION['annee_recu'],
PLUGIN_ROOT . "/zip"
);
//supprimer les fichiers pdf (utile ?)
foreach ($listeFichiersPDF as $f) {
// Utils::safe_unlink($f);
}
} // genererRecusPDF
function generererRecusHTML($tpl,
$totalPersonnes,
$signature,
$logo_asso,
$texteArticles,
$plugin,
$configNum,
$libelles_taux
)
{
$tpl->register_function('afficher_numero_recu', function($params)
{
$prefixeNum = $params['prefixe'];
$membre = $params['membre'];
$numero_personne = $params['numero_personne'];
$numero_sequentiel = $params['numero_sequentiel'];
$numero = faireNumeroRecu($prefixeNum,
$membre,
$numero_personne,
$numero_sequentiel);
$out = sprintf('
<p class="important">Reçu %s</p>',
$numero
);
return $out;
});
$tpl->assign(compact(
'totalPersonnes',
'logo_asso',
'signature',
'libelles_taux',
'texteArticles'
));
$tpl->assign('prefixeNum', getNumPrefixe($configNum));
$tpl->assign('membre', $configNum->membre);
$tpl->assign('numero_sequentiel', getNumSequentiel($configNum));
$tpl->assign('nom_asso', Config::getInstance()->get('nom_asso'));
$tpl->assign('adresse_asso', Config::getInstance()->get('adresse_asso'));
$tpl->assign('objet_asso', $plugin->getConfig('objet_asso'));
$tpl->assign('courriel', $plugin->getConfig('imprimerCourriel'));
$tpl->assign('complements', mentionsComplémentaires());
$tpl->assign('ville_asso', $plugin->getConfig('ville_asso'));
$tpl->assign('date', date("j/m/Y"));
$tpl->assign('nom_responsable', $plugin->getConfig('nom_responsable'));
$tpl->assign('fonction_responsable', $plugin->getConfig('fonction_responsable'));
$tpl->assign('ville_asso', $plugin->getConfig('ville_asso'));
$tpl->assign('nom', $personne->nomPrenom);
$tpl->assign('adresse', $personne->adresse);
$tpl->assign('code_postal', $personne->codePostal);
$tpl->assign('ville', $personne->ville);
$tpl->assign('date', date("j/m/Y"));
// numéro de reçu
$chaineNum = $prefixeNum;
if (isset($configNum->membre) && $configNum->membre) {
if ($chaineNum != "") { $chaineNum .= "-"; }
$chaineNum .= $personne->numero;
}
if (isset($configNum->sequentiel) && $configNum->sequentiel) {
if ($chaineNum != "") { $chaineNum .= "-"; }
$chaineNum .= $numero_sequentiel;
++$numero_sequentiel;
}
$tpl->assign('numero', $chaineNum);
// adresse de courriel
if ($plugin->getConfig('imprimerCourriel')) {
$courriel = $personne->courriel;
}
else {
$courriel = "";
}
$tpl->assign('courriel', $courriel);
// les versements
$tpl->registerSection('versements',
function () use($personne, $libelles_taux, $fmt)
{
foreach ($personne->versements as $taux => $versement)
{
$ligne['montant'] = $versement->montant;
$ligne['euros'] = $fmt->format((int)($versement->montant/100));
if ($versement->montant % 100 != 0) {
$ligne['cents'] = $fmt->format($versement->montant % 100);
} else {
$ligne['cents'] = "";
}
$ligne['libelle'] = $libelles_taux[$taux];
$ligne['dateMin'] = date("d/m/Y", $versement->dateMin);
$ligne['dateMax'] = date("d/m/Y", $versement->dateMax);
yield $ligne;
}
});
// mentions complémentaires
$donnees = array(
'Nature du don : ' => "Numéraire",
'Mode de versement : ' => "chèque et/ou virement"
);
$infos = array();
foreach ($donnees as $titre => $libelle)
{
$elem = new \stdClass();
$elem->titre = $titre;
$elem->libelle = $libelle;
$infos[] = $elem;
}
$tpl->registerSection('informations',
function () use ($infos)
{
foreach ($infos as $elem)
{
yield (array) $elem;
}
});
// fabriquer le fichier PDF
$result = $tpl->fetch();
$nomPDF = \Garradin\Utils::filePDF($result);
// changer le nom du fichier
$nom = str_replace(' ', '_', $personne->nomPrenom);
$nom = str_replace("'", "", $nom);
$nomFichier = sprintf('%s/recu_%s_%s.pdf',
dirname($nomPDF),
$_SESSION['annee_recu'],
$nom);
rename($nomPDF, $nomFichier);
// ajouter le nom du fichier à la liste pour mettre dans une archive
$listeFichiersPDF[] = $nomFichier;
}
// faire une archive zip
$fichierZip = Utils::makeArchive(
$listeFichiersPDF,
$_SESSION['annee_recu'],
PLUGIN_ROOT . "/zip"
);
//supprimer les fichiers pdf (utile ?)
foreach ($listeFichiersPDF as $f)
{
unlink($f);
}
$tpl->assign('plugin_css', ['previs_recu.css', 'imprimer_recu.css']);
$tpl->display(PLUGIN_ROOT . '/templates/recu_html.tpl');
} // generererRecusHTML
/**
* Cumuler les versements de chaque personne
@ -210,13 +243,10 @@ function cumulerVersementsPersonne($versements)
$dateMin = PHP_INT_MAX;
$dateMax = -1;
$totalVersements = 0;
foreach ($versements as $ligne)
{
if ($ligne->idUser != $idPersonneCourant)
{
foreach ($versements as $ligne) {
if ($ligne->idUser != $idPersonneCourant) {
// changement de personne
if ($idPersonneCourant != -1)
{
if ($idPersonneCourant != -1) {
$totalPersonnes[$idPersonneCourant]->ajouterVersement(
$_SESSION['taux_reduction'],
$totalVersements,
@ -229,15 +259,18 @@ function cumulerVersementsPersonne($versements)
$idPersonneCourant = $ligne->idUser;
$totalVersements = $ligne->versement;
// créer les infos de la personne, sauf si elle est déjà présente
if (!array_key_exists($idPersonneCourant, $totalPersonnes))
{
$totalPersonnes["$idPersonneCourant"] = $_SESSION['membresDonateurs'][$ligne->idUser]->clone();
if (!array_key_exists($idPersonneCourant, $totalPersonnes)) {
$totalPersonnes[$idPersonneCourant] = $_SESSION['membresDonateurs'][$ligne->idUser]->clone();
}
} else {
// même personne : cumuler versements et mettre à jour les dates
$totalVersements += $ligne->versement;
if (strtotime($ligne->date) < $dateMin) { $dateMin = strtotime($ligne->date); }
if (strtotime($ligne->date) > $dateMax) { $dateMax = strtotime($ligne->date); }
if (strtotime($ligne->date) < $dateMin) {
$dateMin = strtotime($ligne->date);
}
if (strtotime($ligne->date) > $dateMax) {
$dateMax = strtotime($ligne->date);
}
}
}
// et le dernier
@ -248,7 +281,7 @@ function cumulerVersementsPersonne($versements)
$dateMax
);
return $totalPersonnes;
}
} // cumulerVersementsPersonne
/**
* Cumuler les versements de chaque personne par tarif
@ -264,20 +297,17 @@ function cumulerVersementsTarif($versements)
$dateMin = PHP_INT_MAX;
$dateMax = -1;
$totalVersements = 0;
foreach ($versements as $ligne)
{
foreach ($versements as $ligne) {
if (
$ligne->idTarif != $idTarifCourant ||
$ligne->idUser != $idPersonneCourant ||
$ligne->idCompte != $idCompteCourant
)
{
if ($idTarifCourant != -1)
{
) {
if ($idTarifCourant != -1) {
// changement de tarif, de personne ou de compte
$tarifCompte = ($idTarifCourant == 0) ?
$idCompteCourant :
$idTarifCourant . "_" . $idCompteCourant;
$idCompteCourant :
$idTarifCourant . "_" . $idCompteCourant;
$totalPersonnes[$idPersonneCourant]->ajouterVersement(
$_SESSION['tauxSelectionnes'][$tarifCompte],
$totalVersements,
@ -292,21 +322,24 @@ function cumulerVersementsTarif($versements)
$idCompteCourant = $ligne->idCompte;
$totalVersements = $ligne->versement;
// créer les infos de la personne, sauf si elle est déjà présente
if (!array_key_exists($idPersonneCourant, $totalPersonnes))
{
$totalPersonnes["$idPersonneCourant"] = $_SESSION['membresDonateurs'][$ligne->idUser]->clone();
if (!array_key_exists($idPersonneCourant, $totalPersonnes)) {
$totalPersonnes[$idPersonneCourant] = $_SESSION['membresDonateurs'][$ligne->idUser]->clone();
}
} else {
// même personne : cumuler versements et mettre à jour les dates
$totalVersements += $ligne->versement;
if (strtotime($ligne->date) < $dateMin) { $dateMin = strtotime($ligne->date); }
if (strtotime($ligne->date) > $dateMax) { $dateMax = strtotime($ligne->date); }
if (strtotime($ligne->date) < $dateMin) {
$dateMin = strtotime($ligne->date);
}
if (strtotime($ligne->date) > $dateMax) {
$dateMax = strtotime($ligne->date);
}
}
}
// et le dernier
$tarifCompte = ($idTarifCourant == 0) ?
$idCompteCourant :
$idTarifCourant . "_" . $idCompteCourant;
$idCompteCourant :
$idTarifCourant . "_" . $idCompteCourant;
$totalPersonnes[$idPersonneCourant]->ajouterVersement(
$_SESSION['tauxSelectionnes'][$tarifCompte],
$totalVersements,
@ -314,4 +347,94 @@ function cumulerVersementsTarif($versements)
$dateMax
);
return $totalPersonnes;
} // cumulerVersementsTarif
/**
* génère un fichier PDF à partir d'un document html
* ajoute son nom à la liste de fichiers
*/
function genererPDF($docHTML, $nomPersonne, &$listeFichiersPDF)
{
// fabriquer le fichier PDF
$nomPDF = \Garradin\Utils::filePDF($docHTML);
// changer le nom du fichier
$nom = str_replace(' ', '_', $nomPersonne);
$nom = str_replace("'", "", $nom);
$nomFichier = sprintf(
'%s/recu_%s_%s.pdf',
dirname($nomPDF),
$_SESSION['annee_recu'],
$nom
);
rename($nomPDF, $nomFichier);
// ajouter le nom du fichier à la liste pour mettre dans une archive
$listeFichiersPDF[] = $nomFichier;
} // genererPDF
function faireNumeroRecu($prefixeNum, $membre, $numero, &$numero_sequentiel)
{
if (isset($membre) && $membre) {
if ($prefixeNum != "") {
$prefixeNum .= "-";
}
$prefixeNum .= $numero;
}
if ($numero_sequentiel) {
if ($prefixeNum != "") {
$prefixeNum .= "-";
}
$prefixeNum .= $numero_sequentiel;
++$numero_sequentiel;
}
return $prefixeNum;
}
/**
* renvoyer le préfixe du numéro de reçu
*/
function getNumPrefixe($configNum)
{
$prefixeNum = "";
if (isset($configNum->prefixe) && $configNum->prefixe != "") {
$prefixeNum = $configNum->prefixe;
}
if (isset($configNum->annee) && $configNum->annee) {
if ($prefixeNum != "") {
$prefixeNum .= "-";
}
$prefixeNum .= $_SESSION['annee_recu'];
}
return $prefixeNum;
}
/**
* renvoyer le premier numéro de la numérotation séquentielle
* renvoie false si pas de numérotation séquentielle
*/
function getNumSequentiel($configNum)
{
if (isset($configNum->sequentiel) && $configNum->sequentiel) {
if (isset($configNum->valeur_init) && $configNum->valeur_init != "") {
$numero_sequentiel = $configNum->valeur_init;
} else {
$numero_sequentiel = 1;
}
}
return isset($numero_sequentiel) ? $numero_sequentiel : false;
}
function mentionsComplémentaires()
{
$donnees = array(
'Nature du don : ' => "Numéraire",
'Mode de versement : ' => "chèque et/ou virement"
);
$complements = array();
foreach ($donnees as $titre => $libelle) {
$elem = new \stdClass();
$elem->titre = $titre;
$elem->libelle = $libelle;
$complements[] = $elem;
}
return $complements;
}