facturation/lib/Facture.php

552 lines
14 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\Plugin\Facturation;
2019-11-02 17:53:27 +01:00
2020-12-29 18:07:26 +01:00
use DateTime;
2023-07-21 23:28:54 +02:00
use Paheko\Config;
use Paheko\DB;
use Paheko\DynamicList;
use Paheko\UserException;
use Paheko\Utils;
use Paheko\Services\Services_User;
2019-11-02 17:53:27 +01:00
class Facture
{
2021-12-17 13:47:23 +01:00
const TYPES_NAMES = [
DEVIS => 'Devis',
FACT => 'Facture',
CERFA => 'Reçu fiscal',
COTIS => 'Reçu de cotisation',
];
2019-11-02 17:53:27 +01:00
private $keys = [
'type_facture', // 0 : devis, 1 : facture, 2 : reçu cerfa, 3 : reçu cotis
2019-11-02 17:53:27 +01:00
'numero',
'receveur_membre',
'receveur_id',
'date_emission', // Reçus : date du don
'date_echeance', // Reçus : date d'édition du reçu
2019-11-02 17:53:27 +01:00
'reglee',
'archivee',
'moyen_paiement',
'contenu',
'total'
];
2020-12-29 18:07:26 +01:00
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'],
];
2019-11-02 17:53:27 +01:00
public function __construct()
{
}
// Fix : est dépendant de l'ordre des données dans l'array
// et implique que toutes les données soient présentes (pas possible de faire un update partiel)
2019-11-02 17:53:27 +01:00
public function _checkFields(&$datas)
{
foreach($datas as $k=>$data)
{
if (!in_array($k, $this->keys))
{
throw new UserException("Clé inattendue : $k.");
}
2019-11-03 17:51:31 +01:00
2023-04-16 00:04:47 +02:00
if(!is_array($data) && null !== $data){
2019-11-02 17:53:27 +01:00
$datas[$k] = trim($data);
}
if ($datas[$k] === '' && $k != 'numero')
2019-11-02 17:53:27 +01:00
{
throw new UserException("La valeur de $k est vide");
}
2022-01-07 01:24:32 +01:00
2019-11-02 17:53:27 +01:00
switch($k)
{
case 'type_facture':
2020-12-29 18:07:26 +01:00
if (!array_key_exists($datas[$k], $this->types)) {
throw new UserException("$k est de type non-attendue ($data).");
}
if ($datas[$k] < 2) {
$fac = true;
$cerfa = false;
$recu = false;
2020-10-24 07:04:06 +02:00
}
elseif ($datas[$k] == 2) {
$fac = false;
$cerfa = true;
2020-10-24 07:04:06 +02:00
$recu = false;
}
elseif ($datas[$k] == 3) {
$fac = false;
$cerfa = false;
$recu = true;
}
break;
case 'receveur_membre':
case 'reglee':
case 'archivee':
2020-10-24 07:04:06 +02:00
if ($datas[$k] != 1 && $datas[$k] != 0) {
throw new UserException("$k est de valeur non-attendue ($data).");
2019-11-02 17:53:27 +01:00
}
break;
case 'receveur_id':
if (!is_numeric($datas[$k]) || $datas[$k] < 0) {
throw new UserException("L'id du receveur est non-attendu ($data).");
2019-11-02 17:53:27 +01:00
}
break;
case 'date_emission':
2020-12-29 18:07:26 +01:00
$datas[$k] = \DateTime::createFromFormat('!d/m/Y', $data)->format('Y-m-d');
2019-11-02 17:53:27 +01:00
break;
case 'date_echeance':
2020-12-29 18:07:26 +01:00
$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).");
2019-11-02 17:53:27 +01:00
}
break;
case 'moyen_paiement':
2020-12-29 18:07:26 +01:00
if (!array_key_exists($datas[$k], $this->listMoyensPaiement())) {
throw new UserException("Le moyen de paiement ne correspond pas à la liste interne ($data).");
2019-11-02 17:53:27 +01:00
}
break;
case 'contenu':
if ($fac)
2019-11-02 17:53:27 +01:00
{
if (!is_array($datas[$k]) || empty($datas[$k])) {
throw new UserException("Le contenu du document est vide ($data).");
}
$total = 0;
foreach($datas[$k] as $g => $r)
2019-11-02 17:53:27 +01:00
{
if (empty($r['designation']) && empty($r['prix']))
{
unset($datas[$k][$g]);
unset($datas[$k]['prix']);
continue;
}
elseif (empty($r['prix']))
{
$datas[$k]['prix'] = 0;
}
if (!is_int($r['prix']))
{
throw new UserException('Un (ou plus) des prix n\'est pas un entier.');
}
$total += $r['prix'];
2019-11-02 17:53:27 +01:00
}
if($fac && !$total)
2019-11-02 17:53:27 +01:00
{
throw new UserException("Toutes les désignations/prix sont vides.");
2019-11-02 17:53:27 +01:00
}
}
elseif ($cerfa)
{
}
elseif ($recu)
2019-11-02 17:53:27 +01:00
{
// $fields = ['id', 'intitule', 'date', 'expiration'];
// foreach ($datas[$k]as $)
2019-11-02 17:53:27 +01:00
}
$datas[$k] = json_encode($datas[$k]);
2019-11-02 17:53:27 +01:00
break;
case 'total':
if ($cerfa && $datas[$k] < 1) {
throw new UserException('Le total ne peut être inférieur à 1€ pour les reçus (bug encore non résolu).');
}
if ($fac && !isset($datas['contenu'])) {
throw new UserException("Pas de contenu fourni pour vérifier le total.");
2019-11-02 17:53:27 +01:00
}
if ($fac && $total != $datas[$k])
2019-11-02 17:53:27 +01:00
{
throw new UserException("Les totaux sont différents ($total != $datas[$k].");
}
2019-11-02 17:53:27 +01:00
break;
}
}
}
public function add($data, ?string $number_pattern = null)
2019-11-02 17:53:27 +01:00
{
$db = DB::getInstance();
$this->_checkFields($data);
$generate_number = false;
2019-11-02 17:53:27 +01:00
if (empty($data['numero']) && $number_pattern) {
$generate_number = true;
$data['numero'] = sha1(random_bytes(10));
}
if ($db->test('plugin_facturation_factures', 'numero = ? COLLATE NOCASE', $data['numero']))
2019-11-02 17:53:27 +01:00
{
throw new UserException('Le numéro de document doit être unique, or il existe déjà un document avec le numéro ' . $data['numero']);
2019-11-02 17:53:27 +01:00
}
2019-11-02 17:53:27 +01:00
$db->insert('plugin_facturation_factures', $data);
$id = $db->lastInsertRowId();
if ($generate_number) {
$numero = $this->getNewNumber($number_pattern, $data['type_facture'], \DateTime::createFromFormat('!Y-m-d', $data['date_emission']), $id);
$db->update('plugin_facturation_factures', compact('numero'), 'id = ' . (int) $id);
}
return $id;
}
/**
* Renvoie un nouveau numéro de facture selon un motif défini et le type de document
*/
public function getNewNumber(string $pattern, int $type, \DateTimeInterface $date, int $id)
{
if ($type == DEVIS) {
$type = 'DEVIS';
$t = 'D';
}
elseif ($type == FACT) {
$type = 'FACT';
$t = 'F';
}
elseif ($type == CERFA) {
$type = 'CERFA';
$t = 'RF';
}
else {
$type = 'COTIS';
$t = 'RC';
}
$year = $date->format('Y');
$y = $date->format('y');
// Garantir l'unicité du numéro
$db = DB::getInstance();
$sql = sprintf('SELECT numero FROM plugin_facturation_factures');
$numeros = array_column($db->get($sql), 'numero');
//sélectionner les numéros qui correspondent au pattern
$selpattern = preg_replace('/%(\d+)?\{(ynumber|id)\}/', '', $pattern);
$data = compact('type', 't', 'year', 'y');
$prefixe = preg_replace_callback('/%(\d+)?\{([a-z]+)\}/', function ($match) use ($data) {
$v = (string) $data[$match[2]];
$type = ctype_digit($v) ? 'd' : 's';
return sprintf('%' . $match[1] . $type, $v);
}, $selpattern);
$modele = '/^' . $prefixe . '\d+$/';
$numeros_filtres = array_filter($numeros, function($elem) use ($modele) {
return preg_match($modele, $elem);
}, 0);
// extraire le numéro d'ordre
$rangs = array_map(function($elem) use($prefixe) {
return (int) substr($elem, strlen($prefixe));
}, array_values($numeros_filtres));
sort($rangs);
if (empty($rangs)) {
$ynumber = 1;
} else {
$ynumber = end($rangs) + 1;
}
// fabriquer le numéro selon le pattern
$data = compact('type', 't', 'year', 'y', 'ynumber', 'id');
return preg_replace_callback('/%(\d+)?\{([a-z]+)\}/', function ($match) use ($data) {
2023-04-14 19:48:07 +02:00
$v = (string) $data[$match[2]];
$type = ctype_digit($v) ? 'd' : 's';
return sprintf('%' . $match[1] . $type, $v);
}, $pattern);
2019-11-02 17:53:27 +01:00
}
public function get($id)
2019-11-03 17:51:31 +01:00
{
$db = DB::getInstance();
2019-11-02 17:53:27 +01:00
2020-12-29 18:07:26 +01:00
$r = $db->first('SELECT * FROM plugin_facturation_factures WHERE id = ? LIMIT 1;', (int)$id);
2019-11-02 17:53:27 +01:00
if(!$r)
{
throw new UserException("Pas de document retournée avec cet id.");
2019-11-02 17:53:27 +01:00
}
2019-11-03 17:51:31 +01:00
if ($r->contenu)
{
$r->contenu = json_decode($r->contenu, true);
}
2019-11-02 17:53:27 +01:00
2020-12-29 18:07:26 +01:00
$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);
}
2019-11-02 17:53:27 +01:00
return $r;
}
public function listAll()
{
$r = (array)DB::getInstance()->get('SELECT *, strftime(\'%s\', date_emission) AS date_emission,
strftime(\'%s\', date_echeance) AS date_echeance
FROM plugin_facturation_factures');
2019-11-03 17:51:31 +01:00
2019-11-02 17:53:27 +01:00
foreach ($r as $e)
{
if($e->contenu)
{
$e->contenu = json_decode((string)$e->contenu, true);
}
2019-11-02 17:53:27 +01:00
}
return $r;
}
2021-12-17 13:47:23 +01:00
public function list(): DynamicList
{
2023-08-01 22:56:38 +02:00
$id_field = \Paheko\Users\DynamicFields::getNameFieldsSQL('u');
2021-12-17 13:47:23 +01:00
$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',
2023-08-01 22:56:38 +02:00
'select' => sprintf('CASE WHEN receveur_membre THEN %s ELSE c.nom END', $id_field),
2021-12-17 13:47:23 +01:00
],
'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',
'order' => 'date_emission %s, id %1$s',
2021-12-17 13:47:23 +01:00
],
'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
2023-08-01 22:56:38 +02:00
LEFT JOIN users AS u ON f.receveur_membre = 1 AND u.id = f.receveur_id
2021-12-17 13:47:23 +01:00
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('date_emission', true);
2021-12-17 13:47:23 +01:00
$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 == COTIS && isset($content->intitule, $content->souscription)) {
2021-12-17 13:47:23 +01:00
$row->contenu = sprintf("Cotisation %s\nSouscrite le %s",
$content->intitule,
Utils::date_fr($content->souscription, 'd/m/Y')
);
}
2022-01-07 01:24:32 +01:00
elseif ($row->type_facture != CERFA) {
2021-12-17 13:47:23 +01:00
$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));
}
2022-01-07 01:24:32 +01:00
else
{
$row->contenu = '';
}
2021-12-17 13:47:23 +01:00
});
$list->setPageSize(1000);
return $list;
}
2019-11-02 17:53:27 +01:00
public function edit($id, $data = [])
{
$db = DB::getInstance();
$this->_checkFields($data);
if(isset($data['numero']) && $db->test('plugin_facturation_factures', 'numero = ? COLLATE NOCASE AND id != ?', $data['numero'], (int)$id))
{
throw new UserException('Un document avec ce numéro existe déjà, hors le numéro doit être unique.');
2019-11-02 17:53:27 +01:00
}
return $db->update('plugin_facturation_factures', $data, $db->where('id', (int)$id));
}
public function listUserDoc($base, $id)
2019-11-02 17:53:27 +01:00
{
$client = new Client;
if ($base == 0) // Si c'est un client
2019-11-02 17:53:27 +01:00
{
if(!$client->get($id))
{
throw new UserException("Ce client n'existe pas.");
}
}
else // Si c'est un membre de l'asso
2019-11-02 17:53:27 +01:00
{
2023-08-01 22:56:38 +02:00
throw new UserException("Woopsie, g pô encore implémenté l'usage des users de l'asso comme clients");
2019-11-02 17:53:27 +01:00
}
$r = (array)DB::getInstance()->get('SELECT *, strftime(\'%s\', date_emission) AS date_emission,
2019-11-03 17:51:31 +01:00
strftime(\'%s\', date_echeance) AS date_echeance
FROM plugin_facturation_factures
WHERE receveur_membre = ? AND receveur_id = ?', (int)$base, (int)$id);
2019-11-02 17:53:27 +01:00
foreach ($r as $e)
{
if ($e->contenu)
{
$e->contenu = json_decode((string)$e->contenu, true);
}
2019-11-02 17:53:27 +01:00
}
2019-11-03 17:51:31 +01:00
2019-11-02 17:53:27 +01:00
return empty($r)?false:$r;
}
public function hasDocs($base, $id)
2019-11-02 17:53:27 +01:00
{
$client = new Client;
if ($base == 0) // Si c'est un client
2019-11-02 17:53:27 +01:00
{
if(!$client->get($id))
{
throw new UserException("Ce client n'existe pas.");
}
}
else // Si c'est un membre de l'asso
2019-11-02 17:53:27 +01:00
{
2023-08-01 22:56:38 +02:00
throw new UserException("Woopsie, g pô encore implémenté l'usage des users de l'asso comme clients");
2019-11-02 17:53:27 +01:00
}
return DB::getInstance()->test('plugin_facturation_factures', 'receveur_membre = ? AND receveur_id = ?', $base, $id);
2019-11-02 17:53:27 +01:00
}
// ** Pour type reçu **
2020-12-29 18:07:26 +01:00
public $recu_fields = ['id', 'label', 'amount', 'date', 'expiry', 'paid', 'paid_amount'];
2020-12-29 18:07:26 +01:00
public function getCotis(int $user_id, int $su_id = null)
{
2020-12-29 18:07:26 +01:00
$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);
}
2020-12-29 18:07:26 +01:00
public function listMoyensPaiement($assoc = false)
{
$db = DB::getInstance();
2020-12-29 18:07:26 +01:00
$query = 'SELECT code, nom FROM plugin_facturation_paiement ORDER BY nom COLLATE NOCASE;';
2020-12-29 18:07:26 +01:00
if ($assoc) {
return $db->getAssoc($query);
}
else {
return $db->getGrouped($query);
}
2020-12-29 18:07:26 +01:00
}
2022-01-07 01:24:32 +01:00
/* modif DD -- lecture et retour des textes de CERFA -- */
public function listTextesCerfa($menu = true)
{
$db = DB::getInstance();
$sel = ($menu) ? 'id, menu' : 'id, texte';
$query = 'SELECT '.$sel.' FROM "plugin_facturation_txt_cerfa" WHERE 1 ORDER BY id ;';
2022-01-07 01:24:32 +01:00
return $db->getAssoc($query);
2022-01-07 01:24:32 +01:00
}
public function getMoyenPaiement($code)
{
$db = DB::getInstance();
return $db->firstColumn('SELECT nom FROM plugin_facturation_paiement WHERE code = ?;', $code);
}
public function delete($id)
{
return DB::getInstance()->delete('plugin_facturation_factures', 'id = '. (int)$id);
}
2019-11-02 17:53:27 +01:00
}