Upgrade pour Garradin 1.0

This commit is contained in:
Noizette 2020-12-29 17:07:26 +00:00
parent 9c52dc8b4e
commit 3001646e37
29 changed files with 860 additions and 828 deletions

View file

@ -2,8 +2,10 @@
namespace Garradin\Plugin\Facturation;
use DateTime;
use Garradin\DB;
use Garradin\UserException;
use Garradin\Services\Services_User;
class Facture
{
@ -21,12 +23,28 @@ class Facture
'total'
];
public $type = [
0 => 'devis',
1 => 'facture',
2 => 'cerfa',
3 => 'cotis',
];
public $types = [
DEVIS => [
'id' => DEVIS,
'accounts' => [],
'label' => 'Devis',
'help' => ''],
FACT => [
'id' => FACT,
'accounts' => [],
'label' => 'Facture',
'help' => ''],
CERFA => [
'id' => CERFA,
'accounts' => [],
'label' => 'Reçu fiscal',
'help' => 'Reçu fiscal pour un don (membre ou client)'],
COTIS => [
'id' => COTIS,
'accounts' => [],
'label' => 'Reçu de cotisation',
'help' => 'Reçu pour une cotisation payée par un·e membre'],
];
public function __construct()
{
@ -51,11 +69,10 @@ class Facture
throw new UserException("La valeur de $k est vide");
}
switch($k)
{
case 'type_facture':
if (!array_key_exists($datas[$k], $this->type)) {
if (!array_key_exists($datas[$k], $this->types)) {
throw new UserException("$k est de type non-attendue ($data).");
}
if ($datas[$k] < 2) {
@ -87,24 +104,19 @@ class Facture
}
break;
case 'date_emission':
if (!strtotime($datas[$k])) {
throw new UserException("La date d'émission est non-attendue ($data).");
}
$datas[$k] = \DateTime::createFromFormat('!d/m/Y', $data)->format('Y-m-d');
break;
case 'date_echeance':
if (!strtotime($datas[$k])) {
throw new UserException("La date d'émission est non-attendue ($data).");
}
if (isset($datas['date_emission']) && (strtotime($datas[$k]) < strtotime($datas['date_emission']))) {
$datas[$k] = \DateTime::createFromFormat('!d/m/Y', $data)->format('Y-m-d');
if (DateTime::createFromFormat('!Y-m-d', $datas[$k])->format('U') < DateTime::createFromFormat('!Y-m-d', $datas['date_emission'])->format('U'))
{
throw new UserException("La date d'échéance est antérieure à la date d'émission ($data).");
}
break;
case 'moyen_paiement':
$cats = new \Garradin\Compta\Categories;
if (!array_key_exists($datas[$k], $cats->listMoyensPaiement())) {
if (!array_key_exists($datas[$k], $this->listMoyensPaiement())) {
throw new UserException("Le moyen de paiement ne correspond pas à la liste interne ($data).");
}
unset($cats);
break;
case 'contenu':
if ($fac)
@ -176,9 +188,7 @@ class Facture
{
$db = DB::getInstance();
$r = $db->first('SELECT *, strftime(\'%s\', date_emission) AS date_emission,
strftime(\'%s\', date_echeance) AS date_echeance
FROM plugin_facturation_factures WHERE id = ? LIMIT 1;', (int)$id);
$r = $db->first('SELECT * FROM plugin_facturation_factures WHERE id = ? LIMIT 1;', (int)$id);
if(!$r)
{
@ -190,6 +200,12 @@ class Facture
$r->contenu = json_decode($r->contenu, true);
}
$r->date_emission = \DateTime::createFromFormat('!Y-m-d', $r->date_emission);
if ($r->date_echeance)
{
$r->date_echeance= \DateTime::createFromFormat('!Y-m-d', $r->date_echeance);
}
return $r;
}
@ -276,26 +292,47 @@ class Facture
// ** Pour type reçu **
public $recu_fields = ['id', 'intitule', 'montant', 'date', 'expiration'];
public $recu_fields = ['id', 'label', 'amount', 'date', 'expiry', 'paid', 'paid_amount'];
public function getCotis($membre_id = 1)
public function getCotis(int $user_id, int $su_id = null)
{
// C un peu overkill nn?
// Copié/modifié de Membres\Cotisations::listSubscriptionsForMember($id)
$db = DB::getInstance();
return $db->get('SELECT cm.id, c.intitule, strftime(\'%s\', c.debut) AS debut, strftime(\'%s\', c.fin) AS fin, c.montant, strftime(\'%s\', cm.date) AS date,
CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\') >= date()
WHEN c.fin IS NOT NULL THEN (cm.id IS NOT NULL AND cm.date <= c.fin AND cm.date >= c.debut)
WHEN cm.id IS NOT NULL THEN 1 ELSE 0 END AS a_jour,
strftime(\'%s\', CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\')
WHEN c.fin IS NOT NULL THEN c.fin ELSE 1 END ) AS expiration,
(julianday(date()) - julianday(CASE WHEN c.duree IS NOT NULL THEN date(cm.date, \'+\'||c.duree||\' days\')
WHEN c.fin IS NOT NULL THEN c.fin END)) AS nb_jours
FROM cotisations_membres AS cm
INNER JOIN cotisations AS c ON c.id = cm.id_cotisation
WHERE cm.id_membre = ?
AND ((c.fin IS NOT NULL AND cm.date <= c.fin AND cm.date >= c.debut) OR c.fin IS NULL)
GROUP BY cm.id_cotisation
ORDER BY cm.date DESC;', (int)$membre_id);
$where = 'WHERE su.id_user = ?';
if (null !== $su_id)
{
$where .= ' AND su.id = '.$su_id;
}
$sql = 'SELECT su.id, s.label, su.date, MAX(su.expiry_date) as expiry, sf.label as fee, sf.amount as amount, su.paid, SUM(tl.debit) as paid_amount
FROM services_users su
INNER JOIN services s ON s.id = su.id_service
LEFT JOIN services_fees sf ON sf.id = su.id_fee
LEFT JOIN acc_transactions_users tu ON tu.id_service_user = su.id
LEFT JOIN acc_transactions_lines tl ON tl.id_transaction = tu.id_transaction
'.$where.'
GROUP BY su.id
ORDER BY su.date;';
return DB::getInstance()->get($sql, $user_id);
}
public function listMoyensPaiement($assoc = false)
{
$db = DB::getInstance();
$query = 'SELECT code, nom FROM plugin_facturation_paiement ORDER BY nom COLLATE NOCASE;';
if ($assoc) {
return $db->getAssoc($query);
}
else {
return $db->getGrouped($query);
}
}
public function getMoyenPaiement($code)
{
$db = DB::getInstance();
return $db->firstColumn('SELECT nom FROM plugin_facturation_paiement WHERE code = ?;', $code);
}
}