Liste dynamique des documents

This commit is contained in:
bohwaz 2021-12-17 13:47:23 +01:00
parent 440b2e3e66
commit fa35d7479d
3 changed files with 144 additions and 75 deletions

View File

@ -3,12 +3,22 @@
namespace Garradin\Plugin\Facturation; namespace Garradin\Plugin\Facturation;
use DateTime; use DateTime;
use Garradin\Config;
use Garradin\DB; use Garradin\DB;
use Garradin\DynamicList;
use Garradin\UserException; use Garradin\UserException;
use Garradin\Utils;
use Garradin\Services\Services_User; use Garradin\Services\Services_User;
class Facture class Facture
{ {
const TYPES_NAMES = [
DEVIS => 'Devis',
FACT => 'Facture',
CERFA => 'Reçu fiscal',
COTIS => 'Reçu de cotisation',
];
private $keys = [ private $keys = [
'type_facture', // 0 : devis, 1 : facture, 2 : reçu cerfa, 3 : reçu cotis 'type_facture', // 0 : devis, 1 : facture, 2 : reçu cerfa, 3 : reçu cotis
'numero', 'numero',
@ -235,6 +245,103 @@ class Facture
return $r; return $r;
} }
public function list(): DynamicList
{
$id_field = Config::getInstance()->champ_identite;
$columns = [
// Sélectionner cette colonne, mais ne pas la mettre dans la liste des colonnes
// (absence de label)
'id' => [
'select' => 'f.id',
],
'type_facture' => [
],
'receveur_membre' => [
],
'receveur_id' => [
],
// Créer une colonne virtuelle
'type' => [
'label' => 'Type',
'select' => null,
],
'numero' => [
'label' => 'Numéro',
'select' => 'f.numero',
],
'receveur' => [
'label' => 'Receveur',
'select' => sprintf('CASE WHEN receveur_membre THEN u.%s ELSE c.nom END', $id_field),
],
'receveur_adresse' => [
'label' => 'Son adresse',
'select' => 'CASE WHEN receveur_membre THEN u.adresse ELSE c.adresse END',
],
'receveur_ville' => [
'label' => 'Sa ville',
'select' => 'CASE WHEN receveur_membre THEN u.ville ELSE c.ville END',
],
'date_emission' => [
'label' => 'Émission',
],
'date_echeance' => [
'label' => 'Échéance',
],
'reglee' => [
'label' => 'Réglée',
],
'archivee' => [
'label' => 'Archivée',
],
'moyen_paiement' => [
'label' => 'Moyen de paiement',
'select' => 'mp.nom',
],
'contenu' => [
'label' => 'Contenu',
],
'total' => [
'label' => 'Total',
],
];
$tables = 'plugin_facturation_factures AS f
INNER JOIN plugin_facturation_paiement AS mp ON mp.code = f.moyen_paiement
LEFT JOIN membres AS u ON f.receveur_membre = 1 AND u.id = f.receveur_id
LEFT JOIN plugin_facturation_clients AS c ON f.receveur_membre = 0 AND c.id = f.receveur_id';
$list = new DynamicList($columns, $tables);
$list->orderBy('numero', true);
$currency = Config::getInstance()->monnaie;
$list->setModifier(function ($row) use ($currency) {
// Remplir la colonne virtuelle
$row->type = self::TYPES_NAMES[$row->type_facture] ?? null;
$row->reglee = $row->reglee ? 'Réglée' : 'Non';
$row->archivee = $row->archivee ? 'Archivée' : 'Non';
// Remplir le contenu
$content = json_decode((string)$row->contenu);
if ($row->type_facture == FACT && isset($content->intitule, $content->souscription)) {
$row->contenu = sprintf("Cotisation %s\nSouscrite le %s",
$content->intitule,
Utils::date_fr($content->souscription, 'd/m/Y')
);
}
else {
$row->contenu = implode("\n", array_map(function ($row) use ($currency) {
return sprintf('%s : %s %s', $row->designation, Utils::money_format($row->prix), $currency);
}, (array)$content));
}
});
$list->setPageSize(1000);
return $list;
}
public function edit($id, $data = []) public function edit($id, $data = [])
{ {
$db = DB::getInstance(); $db = DB::getInstance();
@ -348,5 +455,4 @@ class Facture
{ {
return DB::getInstance()->delete('plugin_facturation_factures', 'id = '. (int)$id); return DB::getInstance()->delete('plugin_facturation_factures', 'id = '. (int)$id);
} }
} }

View File

@ -3,71 +3,42 @@
{form_errors} {form_errors}
{if count($factures)} {if $list->count()}
<table class="list"> {include file="common/dynamic_list_head.tpl"}
<thead>
<td>Type</td>
<td>Numéro</td>
<td>Receveur</td>
<td>Son adresse</td>
<td>Sa ville</td>
<td>Emission</td>
<td>Echéance</td>
<td>Réglée</td>
<td>Archivée</td>
<td>Moyen paiement</td>
<td>Contenu</td>
<td>Total</td>
<td></td>
</thead>
<tbody>
{foreach from=$factures item=facture}
<tr>
<td><?php switch($facture->type_facture) {
case 0:
echo 'Devis';
break;
case 1:
echo 'Facture';
break;
case 2:
echo 'Reçu fiscal';
break;
case 3:
echo 'Reçu cotisation';
break;
}
?></td>
<td><a href="{plugin_url file="facture.php"}?id={$facture.id}">{$facture.numero}</a></td>
{if $facture.receveur_membre}
<td><a href="{$admin_url}membres/fiche.php?id={$facture.receveur.id}">{$facture.receveur->$identite}</a></td>
{else}
<td><a href="{plugin_url file="client.php"}?id={$facture.receveur.id}">{$facture.receveur.nom}</a></td>
{/if}
<td>{$facture.receveur.adresse}</td>
<td>{$facture.receveur.ville}</td>
<td>{$facture.date_emission|date:'d/m/Y'}</td>
<td>{$facture.date_echeance|date:'d/m/Y'}</td>
<td><?= $facture->reglee?'Réglée':'Non' ?></td>
<td><?= $facture->archivee?'Archivée':'Non' ?></td>
<td>{$facture.moyen_paiement}</td>
<td>
{if $facture.type_facture == 3}
<p>Cotisation {$facture.contenu.intitule}</p>
<p>Souscrite le {$facture.contenu.souscription|date_short}</p>
{else}
{foreach from=$facture.contenu item=contenu}
<p>{$contenu.designation} : {$contenu.prix|escape|money:false}&nbsp;{$config.monnaie}</p>
{/foreach}
{/if}
</td>
<td>{$facture.total|escape|money}&nbsp;{$config.monnaie}</td>
{* <td>{linkbutton shape="delete" href="%sfacture_supprimer.php?id=%d"|args:$plugin_url,$facture.id label="Supprimer"}</td> *}
</tr>
{/foreach}
</tbody>
{foreach from=$list->iterate() item="facture"}
<tr>
<td>{$facture.type}</td>
<th><a href="facture.php?id={$facture.id}">{$facture.numero}</a></th>
{if $facture.receveur_membre}
<td>{link href="!membres/fiche.php?id=%d"|args:$facture.receveur_id label=$facture.receveur}</td>
{else}
<td>{link href="client.php?id=%d"|args:$facture.receveur_id label=$facture.receveur}</td>
{/if}
<td>{$facture.receveur_adresse}</td>
<td>{$facture.receveur_ville}</td>
<td>{$facture.date_emission|date:'d/m/Y'}</td>
<td>{$facture.date_echeance|date:'d/m/Y'}</td>
<td>{$facture.reglee}</td>
<td>{$facture.archivee}</td>
<td>{$facture.moyen_paiement}</td>
<td>{$facture.contenu|escape|nl2br}</td>
<td>{$facture.total|escape|money_currency}</td>
<td class="actions">
{linkbutton shape="download" href="pdf.php?id=%d&d"|args:$facture.id label="Télécharger"}
{linkbutton shape="menu" href="facture.php?id=%d"|args:$facture.id label="Voir"}
</td>
</tr>
{/foreach}
</tbody>
</table> </table>
<p class="help">
Export de la liste&nbsp;:
{linkbutton href="?export=csv" label="Export CSV" shape="download"}
{linkbutton href="?export=ods" label="Export tableur" shape="download"}
</p>
{else} {else}
<p class="help">Aucun document, vous pouvez commencer par {link href="facture_ajouter.php" label="créer un nouveau document"}.</p> <p class="help">Aucun document, vous pouvez commencer par {link href="facture_ajouter.php" label="créer un nouveau document"}.</p>
{/if} {/if}

View File

@ -6,18 +6,10 @@ require_once __DIR__ . '/_inc.php';
$session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ); $session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ);
$membres = new Membres;
$tpl->assign('moyens_paiement', $facture->listMoyensPaiement()); $list = $facture->list();
$list->loadFromQueryString();
foreach($factures = $facture->listAll() as $k=>$f) $tpl->assign(compact('list'));
{
$factures[$k]->receveur = $f->receveur_membre? $membres->get($f->receveur_id) : $client->get($f->receveur_id);
$factures[$k]->moyen_paiement = $facture->getMoyenPaiement($f->moyen_paiement);
}
$tpl->assign('identite', $identite);
$tpl->assign('factures', $factures);
$tpl->assign('clients', $client->listAll());
$tpl->display(PLUGIN_ROOT . '/templates/index.tpl'); $tpl->display(PLUGIN_ROOT . '/templates/index.tpl');