recusfiscaux/www/admin/generer_recus.php

318 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace Garradin;
use Garradin\Files\Files;
use Garradin\Entities\Files\File;
use Garradin\UserTemplate\UserTemplate;
use Garradin\Plugin\RecusFiscaux\Utils;
use Garradin\Plugin\RecusFiscaux\Personne;
// signature
$signature =
(null !== $plugin->getConfig('signature')) ?
Files::get($plugin->getConfig('signature'))->fullpath() :
"";
// logo
$logo_file = Files::get(File::CONTEXT_CONFIG . '/logo.png');
$logo_asso =
(null !== $logo_file) ?
Files::get($logo_file->path)->fullpath() :
"";
// articles du CGI
$articlesCGI = array();
foreach ($plugin->getConfig('articlesCGI') as $article)
{
if ($article->valeur == 1) {
$articlesCGI[] = $article->titre;
}
}
$nbArticles = count($articlesCGI);
if ($nbArticles == 1)
{
$texteArticles = 'à larticle ' . $articlesCGI[0];
}
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) {
$texteArticles .= " et ";
}
}
}
// libellés pour les taux de réduction
$libelles_taux = Utils::getLignesReduction($plugin->getConfig('reduction'));
$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');
$versementsSelectionnes = array();
foreach ($lesLignes as $ligne) {
$versementsSelectionnes[] = $_SESSION['lesVersements'][$ligne];
}
// cumuler les versements
if ($_GET['type'] == 'personne')
{
$totalPersonnes = cumulerVersementsPersonne($versementsSelectionnes);
}
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');
$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
$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);
}
/**
* Cumuler les versements de chaque personne
* @param tableau des versements triés par idUser, date
* @return tableau des versements cumulés : id => Personne
*/
function cumulerVersementsPersonne($versements)
{
$totalPersonnes = array();
$idPersonneCourant = -1;
$dateMin = PHP_INT_MAX;
$dateMax = -1;
$totalVersements = 0;
foreach ($versements as $ligne)
{
if ($ligne->idUser != $idPersonneCourant)
{
// changement de personne
if ($idPersonneCourant != -1)
{
$totalPersonnes[$idPersonneCourant]->ajouterVersement(
$_SESSION['taux_reduction'],
$totalVersements,
$dateMin,
$dateMax
);
}
$dateMin = strtotime($ligne->date);
$dateMax = strtotime($ligne->date);
$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();
}
} 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); }
}
}
// et le dernier
$totalPersonnes[$idPersonneCourant]->ajouterVersement(
$_SESSION['taux_reduction'],
$totalVersements,
$dateMin,
$dateMax
);
return $totalPersonnes;
}
/**
* Cumuler les versements de chaque personne par tarif
* @param tableau des versements triés par idTarif, idUser, date
* @return tableau des versements cumulés : id => Personne
*/
function cumulerVersementsTarif($versements)
{
$totalPersonnes = array();
$idTarifCourant = -1;
$idPersonneCourant = -1;
$idCompteCourant = -1;
$dateMin = PHP_INT_MAX;
$dateMax = -1;
$totalVersements = 0;
foreach ($versements as $ligne)
{
if (
$ligne->idTarif != $idTarifCourant ||
$ligne->idUser != $idPersonneCourant ||
$ligne->idCompte != $idCompteCourant
)
{
if ($idTarifCourant != -1)
{
// changement de tarif, de personne ou de compte
$tarifCompte = ($idTarifCourant == 0) ?
$idCompteCourant :
$idTarifCourant . "_" . $idCompteCourant;
$totalPersonnes[$idPersonneCourant]->ajouterVersement(
$_SESSION['tauxSelectionnes'][$tarifCompte],
$totalVersements,
$dateMin,
$dateMax
);
}
$dateMin = strtotime($ligne->date);
$dateMax = strtotime($ligne->date);
$idTarifCourant = $ligne->idTarif;
$idPersonneCourant = $ligne->idUser;
$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();
}
} 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); }
}
}
// et le dernier
$tarifCompte = ($idTarifCourant == 0) ?
$idCompteCourant :
$idTarifCourant . "_" . $idCompteCourant;
$totalPersonnes[$idPersonneCourant]->ajouterVersement(
$_SESSION['tauxSelectionnes'][$tarifCompte],
$totalVersements,
$dateMin,
$dateMax
);
return $totalPersonnes;
}