facturation/admin/pdf.php

509 lines
11 KiB
PHP
Raw Normal View History

2019-11-02 17:53:27 +01:00
<?php
2023-07-21 23:28:54 +02:00
namespace Paheko;
2019-11-02 17:53:27 +01:00
require_once __DIR__ . '/_inc.php';
2021-04-11 14:25:59 +02:00
$session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ);
2023-08-01 22:56:38 +02:00
$users = new Users\Users;
2019-11-02 17:53:27 +01:00
2023-08-01 22:56:38 +02:00
f(['id' => 'required|numeric']);
2019-11-02 17:53:27 +01:00
$id = (int) qg('id');
2023-08-01 22:56:38 +02:00
$id_field = Users\DynamicFields::getNameFieldsSQL();
2019-11-02 17:53:27 +01:00
2021-11-10 23:33:00 +01:00
// Vérification que le document existe
if (!$f = $facture->get($id))
2019-11-02 17:53:27 +01:00
{
throw new UserException("Ce document n'existe pas.");
}
2020-12-29 18:07:26 +01:00
$moyen_paiement = $facture->getMoyenPaiement($f->moyen_paiement);
2019-11-02 17:53:27 +01:00
2021-11-10 23:33:00 +01:00
// Récupération infos membre
2019-11-02 17:53:27 +01:00
try
{
if ($f->receveur_membre)
{
2023-08-01 22:56:38 +02:00
$c = $users->get($f->receveur_id);
$c->$id_field = $c->$identite;
2019-11-02 17:53:27 +01:00
foreach(['ville','code_postal','adresse'] as $v)
{
if($c->$v == '')
{
$c->$v = '[A RENSEIGNER DANS LA FICHE MEMBRE]';
}
}
}
else
{
$c = $client->get($f->receveur_id);
2023-08-01 22:56:38 +02:00
$c->$id_field = $c->nom;
2019-11-02 17:53:27 +01:00
}
}
catch(UserException $e)
{
$form->addError($e);
2019-11-02 17:53:27 +01:00
}
2021-11-10 23:33:00 +01:00
// Formatage dates
2020-12-29 18:07:26 +01:00
$emission = $f->date_emission->format('d/m/Y');
if (isset($f->date_echeance))
{
$echeance = $f->date_echeance->format('d/m/Y');
}
2019-11-02 17:53:27 +01:00
2021-11-10 23:33:00 +01:00
// -- Création du PDF
// Génération factures, devis et cotisation
2021-11-10 23:27:07 +01:00
if ($f->type_facture != CERFA)
{
2021-11-10 23:27:07 +01:00
switch ($f->type_facture)
{
case FACT:
$doc = 'Facture n°'. $f->numero;
break;
case DEVIS:
$doc = 'Devis n°'. $f->numero;
break;
case COTIS:
$doc = 'Reçu de cotisation n°'. $f->numero;
break;
}
$asso =
// 'Émis par :<br><br>'.
'<b>'.$config->get('nom_asso')."</b><br>".
2023-08-01 22:56:38 +02:00
str_replace("\n", '<br>', $config->get('org_address'))."<br>".
(($t = $plugin->getConfig('rna_asso'))?"RNA : $t<br>":'').
(($t = $plugin->getConfig('siret_asso'))?"SIRET : $t<br>":'').
(($t = $config->get('email_asso'))?"Email : $t<br>":'').
(($t = $config->get('site_asso'))?"Site web : $t<br>":'');
$receveur =
'Adressé à :<br><br>'.
'<b>'.$c->identite.'</b><br>'.
$c->adresse."<br>".
$c->code_postal.' '.$c->ville."<br>".
(($t = $c->email)?"Email : $t<br>":'').
(($t = $c->telephone)?"Tel : $t<br>":'');
2021-11-06 21:23:49 +01:00
$total = Utils::money_format($f->total, ',', ' ');
2019-11-02 17:53:27 +01:00
2021-11-10 23:27:07 +01:00
// Devis et facture
if ($f->type_facture != COTIS)
{
$echeance = ($f->type_facture?'Échéance de paiement':'Échéance du devis')." : ".$echeance;
$reglee = !$f->reglee?'Cette facture est en attente de règlement.':'Cette facture a été reglée.';
$footer = str_replace("\n", '<br>', $plugin->getConfig('footer'));
2022-01-10 06:40:40 +01:00
$ttc = $plugin->getConfig('ttc') ? 'TTC':'HT';
2019-11-02 17:53:27 +01:00
2021-11-10 23:33:00 +01:00
// Génération du contenu de la facture
2021-11-10 23:27:07 +01:00
ob_start();
echo <<<EOF
<div class="h2">
<span>Contenu</span> - $doc
</div>
<hr>
<table class="contenu">
<thead>
<tr>
<th>
Désignations
</th>
<th>
Prix
</th>
</tr>
</thead>
<tbody>
EOF;
$i = 1;
foreach($f->contenu as $k=>$v)
{
echo '<tr><td>';
echo str_replace("\n", '<br>', $v['designation']);
echo '</td><td>';
echo Utils::money_format($v['prix'], ',', ' ') .' €';
echo '</td></tr>';
$i++;
}
echo <<<EOF
</tbody>
<tfoot>
<tr>
2022-01-10 18:16:32 +01:00
<td><b>Total</b><br>Net à payer</td>
<td><b>$total </b><br>({$ttc})</td>
2021-11-10 23:27:07 +01:00
</tr>
</tfoot>
</table>
<footer>
<div class="h2"><span>Détails</span></div>
<hr>
$echeance <br>
$reglee
Moyen de paiement : $moyen_paiement
<p>
$footer
</p>
</footer>
EOF;
$content = ob_get_clean();
}
else // Reçu de cotisation
{
$lieu = $plugin->getConfig('ville_asso');
$intitule = $f->contenu['intitule'];
$souscription = date('d/m/Y', strtotime($f->contenu['souscription']));
if($f->contenu['expiration'] == '1970-01-01')
{
$expiration = "jour même, s'agissant d'une cotisation ponctuelle.";
}
else {
$expiration = date('d/m/Y', strtotime($f->contenu['expiration']));
}
2021-11-10 23:33:00 +01:00
// Génération du contenu du reçu de cotisation
2021-11-10 23:27:07 +01:00
$content = <<<EOF
<div class="h2">
<span>Reçu de votre cotisation</span> - $doc
</div>
<hr>
<div class="contenuTexte">
<p>À $lieu, le $emission,</p>
<p>Bonjour,</p>
<p>Nous accusons réception de votre cotisation « $intitule » reçue le $emission et nous vous en remercions.</p>
<p>Nous reconnaissons que vous avez acquitté la somme de {$total} .<br>111
Votre adhésion sera donc effective à compter du $souscription jusquau $expiration.</p>
<br>
<p>Nous vous prions de recevoir, chère adhérente, cher adhérent, nos meilleures salutations,</p>
<br>
<p>-représentant·e de l'asso-</p>
<br>
<p><i>Nous vous rappelons que la cotisation nest pas soumise à la TVA et quelle ne donne pas lieu à la délivrance dune facture. Elle nouvre pas droit au bénéfice des dispositions des articles 200, 238 bis et 885-0 V bis A du code général des impôts.</i></p>
</div>
EOF;
}
2021-11-10 23:33:00 +01:00
//-- Layout du document
2021-11-10 23:27:07 +01:00
ob_start();
2021-11-10 23:33:00 +01:00
echo <<<EOF
2021-11-10 23:27:07 +01:00
<!DOCTYPE html>
<html>
2021-11-10 23:33:00 +01:00
<head>
2023-04-14 19:48:07 +02:00
<title>{$doc}_{$emission}</title>
2023-07-22 21:31:15 +02:00
<meta charset="UTF-8">
2021-11-10 23:33:00 +01:00
<style>
@page {
size: A4 portrait;
margin: 0;
}
body {
padding: 4mm;
font-family: Helvetica, Arial, sans;
font-size: 10pt;
background: white;
}
2021-11-10 23:27:07 +01:00
2021-11-10 23:33:00 +01:00
.titre {
text-align: center;
font-size: 8pt;
margin-bottom: 6mm;
margin-top: 0mm;
}
.h2 {
margin: 20px 20px 0px 20px;
}
.h2 span {
font-weight: bold;
font-size: 14pt;
}
hr {
margin: 5px 0px 15px 0px;
border: none;
border-top: 1px solid;
}
.adressage {
font-size: 11pt;
margin: auto;
}
.adressage td {
width: 40%;
vertical-align: top;
}
2021-11-10 23:27:07 +01:00
2021-11-10 23:33:00 +01:00
.contenuTexte {
padding: 0 6mm;
}
2021-11-06 21:23:49 +01:00
2021-11-10 23:33:00 +01:00
.contenu {
width: 100%;
}
2019-11-02 17:53:27 +01:00
2021-11-10 23:33:00 +01:00
.contenu td, .contenu th {
vertical-align: top;
padding: 8px 10px;
margin : 0px;
border-collapse: collapse;
}
2019-11-02 17:53:27 +01:00
2021-11-10 23:33:00 +01:00
.contenu tr th, .contenu tr td {
text-align: right;
width: 16%;
}
.contenu tr td:nth-child(1), .contenu tr th:nth-child(1) {
text-align: left;
width: 84%;
}
2019-11-02 17:53:27 +01:00
2021-11-10 23:33:00 +01:00
.contenu thead tr, .contenu tfoot tr {
background-color: #CCE;
border: 10px solid black;
}
.contenu tbody tr:nth-child(even) {
background-color: #DDF !important;
}
.contenu tfoot {
display: table-row-group;
}
footer {
bottom: 0;
margin: 14mm 0;
width: inherit;
font-size: 9pt;
}
</style>
</head>
<body>
<p class="titre">
$doc - Émis le $emission
</p>
<table class="adressage">
<tr>
<td>$asso</td>
<td>$receveur</td>
</tr>
</table>
<br>
$content
</body>
2021-11-10 23:27:07 +01:00
</html>
2019-11-02 17:53:27 +01:00
EOF;
$html = ob_get_clean();
2021-11-10 23:33:00 +01:00
} // Génération du CERFA
2021-11-06 21:23:49 +01:00
elseif ($f->type_facture == CERFA)
{
2021-11-24 23:44:46 +01:00
$doc = 'Reçu de don n°'. $f->numero;
$url = WWW_URL;
2022-01-07 01:24:32 +01:00
$libelles = $facture->listTextesCerfa(false);
2021-11-24 23:44:46 +01:00
$t['numero'] = $f->numero;
$t['nom_asso'] = $config->get('nom_asso');
$t['n_rue_asso'] = $plugin->getConfig('numero_rue_asso');
$t['rue_asso'] = $plugin->getConfig('rue_asso');
$t['cp_asso'] = $plugin->getConfig('cp_asso');
$t['ville_asso'] = $plugin->getConfig('ville_asso');
$t['objet0'] = $plugin->getConfig('objet_0');
$t['objet1'] = $plugin->getConfig('objet_1');
$t['objet2'] = $plugin->getConfig('objet_2');
$t['nom'] = $c->identite;
$t['adresse'] = $c->adresse;
$t['cp'] = $c->code_postal;
$t['ville'] = $c->ville;
$t['total'] = '***'.Utils::money_format($f->total).'***';
$t['total_lettre'] = numfmt_create('fr_FR', \NumberFormatter::SPELLOUT)->format($f->total/100). ' euros';
$t['d'] = ($f->date_emission->format('d'));
$t['m'] = ($f->date_emission->format('m'));
$t['Y'] = ($f->date_emission->format('Y'));
2021-11-24 23:44:46 +01:00
2022-01-07 01:24:32 +01:00
$t['forme'] = $f->contenu['forme'];
$t['nature'] = $f->contenu['nature'];
$t['texte'] = $libelles[$f->contenu['texte']];
2021-11-24 23:44:46 +01:00
$t['art200'] = $t['art238'] = $t['art885'] = '';
if($plugin->getConfig('droit_art200')){
2021-11-24 23:44:46 +01:00
$t['art200'] = 'X';
}
if($plugin->getConfig('droit_art238bis')){
2021-11-24 23:44:46 +01:00
$t['art238'] = 'X';
}
if($plugin->getConfig('droit_art885-0VbisA')){
2021-11-24 23:44:46 +01:00
$t['art885'] = 'X';
}
2022-01-07 01:24:32 +01:00
// forme du don
switch ($t['forme']){
case '1':
$t['frm'] = 'left: 15mm;';
break;
case '2':
$t['frm'] = 'left: 57.3mm;';
break;
case '3':
$t['frm'] = 'left: 115.2mm;';
break;
case '4':
$t['frm'] = 'left: 175.2mm;';
}
// nature du don
switch ($t['nature']){
case '1':
$t['nat'] = 'left: 15mm;';
break;
case '2':
$t['nat'] = 'left: 57.3mm;';
break;
case '3':
$t['nat'] = 'left: 115.2mm;';
}
// moyen de paiement
switch ($f->moyen_paiement){
case 'ES':
2022-01-07 01:24:32 +01:00
$t['pos'] = 'left: 15mm;';
break;
case 'CH':
2022-01-07 01:24:32 +01:00
$t['pos'] = 'left: 57.3mm;';
break;
default:
2022-01-07 01:24:32 +01:00
$t['pos'] = 'left: 115.2mm;';
}
$t['d2'] = ($f->date_echeance->format('d'));
$t['m2'] = ($f->date_echeance->format('m'));
$t['Y2'] = ($f->date_echeance->format('Y'));
2021-11-24 23:44:46 +01:00
ob_start();
echo <<<EOF
<!DOCTYPE html>
<html>
<head>
2023-04-14 19:48:07 +02:00
<title>{$doc}_{$emission}</title>
2023-07-22 21:31:15 +02:00
<meta charset="UTF-8">
2021-11-24 23:44:46 +01:00
<style>
@page {
size: A4 portrait;
margin: 0;
padding: 0;
}
body {
font-family: Helvetica, Arial, sans;
font-size: 10pt;
background: white;
padding: 0;
margin: 0;
}
.page div {
position: absolute;
}
.page {
padding: 0;
margin: 0;
break-after: always;
width: 210mm;
height: 297mm;
background-size: cover;
background-position: -5mm -4.8mm;
}
#p1 {
2023-04-14 19:48:07 +02:00
background-image: url('{$url}p/facturation/cerfa-1.png');
2021-11-24 23:44:46 +01:00
}
#p2 {
2023-04-14 19:48:07 +02:00
background-image: url('{$url}p/facturation/cerfa-2.png');
2021-11-24 23:44:46 +01:00
position: relative;
}
</style>
</head>
<body>
<div class="page" id="p1">
2023-04-14 19:48:07 +02:00
<div style="top: 10mm; left: 170mm;">{$t['numero']}</div>
2021-11-24 23:44:46 +01:00
2023-04-14 19:48:07 +02:00
<div style="top: 35mm; left: 20mm;">{$t['nom_asso']}</div>
<div style="top: 46mm; left: 20mm;">{$t['n_rue_asso']}</div>
<div style="top: 46mm; left: 40mm;">{$t['rue_asso']}</div>
<div style="top: 51.5mm; left: 37mm;">{$t['cp_asso']}</div>
<div style="top: 51.5mm; left: 75mm;">{$t['ville_asso']}</div>
2021-11-24 23:44:46 +01:00
2023-04-14 19:48:07 +02:00
<div style="top: 62mm; left: 18mm;">{$t['objet0']}</div>
<div style="top: 66.5mm; left: 18mm;">{$t['objet1']}</div>
<div style="top: 70.8mm; left: 18mm;">{$t['objet2']}</div>
2021-11-24 23:44:46 +01:00
<div style="top: 128.5mm; left: 15mm;">X</div>
</div>
<div class="page" id="p2">
2023-04-14 19:48:07 +02:00
<div style="top: 18mm; left: 18mm;">{$t['nom']}</div>
<div style="top: 32mm; left: 18mm;">{$t['adresse']}</div>
<div style="top: 37mm; left: 40mm;">{$t['cp']}</div>
<div style="top: 37mm; left: 80mm;">{$t['ville']}</div>
<div style="top: 63mm; left: 87mm;">{$t['total']}</div>
<div style="top: 73mm; left: 58mm;">{$t['total_lettre']}</div>
<div style="top: 82mm; left: 69mm;">{$t['d']}</div>
<div style="top: 82mm; left: 82mm;">{$t['m']}</div>
<div style="top: 82mm; left: 99mm;">{$t['Y']}</div>
<div style="top: 96mm; left: 53mm;">{$t['art200']}</div>
<div style="top: 96mm; left: 103mm;">{$t['art238']}</div>
<div style="top: 96mm; left: 153.0mm;">{$t['art885']}</div>
<div style="top: 113mm; {$t['frm']}">X</div>
<div style="top: 136mm; {$t['nat']}">X</div>
<div style="top: 142mm; left: 25mm;">{$t['texte']}</div>
<div style="top: 158.2mm; {$t['pos']}">X</div>
<div style="top: 239mm; left: 139mm;">{$t['d2']}</div>
<div style="top: 239mm; left: 148mm;">{$t['m2']}</div>
<div style="top: 239mm; left: 156mm;">{$t['Y2']}</div>
<div style="top: 243mm; left: 137mm;"><img src="{$url}plugin/facturation/sign.png" style="width: 50mm;"/></div>
2021-11-24 23:44:46 +01:00
</div>
</body>
</html>
EOF;
$html = ob_get_clean();
} // End if cerfa
2019-11-02 17:53:27 +01:00
2019-11-02 17:53:27 +01:00
if(qg('d') !== null)
{
2021-11-10 23:27:07 +01:00
$filename = 'Print';
if (preg_match('!<title>(.*)</title>!U', $html, $match)) {
$filename = trim($match[1]);
}
header('Content-type: application/pdf');
header(sprintf('Content-Disposition: attachment; filename="%s.pdf"', Utils::safeFileName($filename)));
Utils::streamPDF($html);
2019-11-02 17:53:27 +01:00
}
else
{
2021-11-10 23:27:07 +01:00
echo $html;
2019-11-02 17:53:27 +01:00
}