From ede7c789d78128d7a1cfb8a10058c81c4dc317d9 Mon Sep 17 00:00:00 2001 From: Noizette Date: Fri, 5 Nov 2021 23:47:36 +0100 Subject: [PATCH] Utiliser des entiers au lieu de float tel Garradin --- data/schema.sql | 2 +- garradin_plugin.ini | 2 +- lib/Facture.php | 33 +++++++---- templates/_js.tpl | 2 + templates/facture_ajouter.tpl | 24 ++++---- templates/facture_modifier.tpl | 24 ++++---- templates/index.tpl | 2 +- upgrade.php | 51 +++++++++++++++- www/admin/_inc.php | 35 +++++++++++ www/admin/_upgrade_trick.php | 13 ----- www/admin/facture_ajouter.php | 104 +++++++++++++++------------------ www/admin/facture_modifier.php | 35 ++++------- www/admin/index.php | 1 - 13 files changed, 189 insertions(+), 139 deletions(-) delete mode 100644 www/admin/_upgrade_trick.php diff --git a/data/schema.sql b/data/schema.sql index 43b7d84..cf3fa58 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS plugin_facturation_factures ( archivee INTEGER DEFAULT 0, -- bool moyen_paiement TEXT NOT NULL, contenu TEXT NOT NULL, - total REAL DEFAULT 0 + total INTEGER DEFAULT 0 -- FOREIGN KEY(moyen_paiement) REFERENCES compta_moyens_paiement(code) ); diff --git a/garradin_plugin.ini b/garradin_plugin.ini index 2221e6f..b4cd152 100644 --- a/garradin_plugin.ini +++ b/garradin_plugin.ini @@ -2,7 +2,7 @@ nom="Facturation" description="Permet d'éditer des factures, devis et reçus à ses membres ainsi qu'à une base de clients supplémentaire." auteur="zou" url="https://gitlab.com/ramoloss/garradin-plugin-facturation/" -version="0.5.0" +version="0.6.0" menu=1 config=1 min_version="1.1.0" \ No newline at end of file diff --git a/lib/Facture.php b/lib/Facture.php index 1745828..619b34c 100644 --- a/lib/Facture.php +++ b/lib/Facture.php @@ -51,7 +51,8 @@ class Facture } - // Wsh je sais que data c invariable, laissez-moi tranquille jsp comment faire sinon + // 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) public function _checkFields(&$datas) { foreach($datas as $k=>$data) @@ -125,20 +126,28 @@ class Facture throw new UserException("Le contenu du document est vide ($data)."); } $total = 0; - $vide = 1; - foreach($datas[$k] as $g=>$r) + foreach($datas[$k] as $g => $r) { - if ($r['designation'] !== '' && is_numeric($r['prix'])) - { - $vide = 0; - } - else + 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']; } - if($fac && $vide) + + if($fac && !$total) { throw new UserException("Toutes les désignations/prix sont vides."); } @@ -164,7 +173,7 @@ class Facture if ($fac && $total != $datas[$k]) { throw new UserException("Les totaux sont différents ($total != $datas[$k]."); - } + } break; } } @@ -239,7 +248,7 @@ class Facture return $db->update('plugin_facturation_factures', $data, $db->where('id', (int)$id)); } - public function listUserDoc($base = 0, $id) + public function listUserDoc($base, $id) { $client = new Client; @@ -271,7 +280,7 @@ class Facture return empty($r)?false:$r; } - public function hasDocs($base = 0, $id) + public function hasDocs($base, $id) { $client = new Client; diff --git a/templates/_js.tpl b/templates/_js.tpl index 6c2da5c..53b96d5 100644 --- a/templates/_js.tpl +++ b/templates/_js.tpl @@ -15,6 +15,8 @@ function plus(){ var newdiv = document.createElement('tr'); newdiv.innerHTML = document.getElementById('Line1').innerHTML; + newdiv.getElementsByTagName('textarea')[0].setAttribute('name', 'designation[]'); + newdiv.getElementsByTagName('input')[0].setAttribute('name', 'prix[]'); newdiv.querySelector('.fact_rm_line button').onclick = function(){ this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); updateSum(); diff --git a/templates/facture_ajouter.tpl b/templates/facture_ajouter.tpl index 9b014e9..a8b1112 100644 --- a/templates/facture_ajouter.tpl +++ b/templates/facture_ajouter.tpl @@ -107,31 +107,27 @@ - {if count($designations) > 0} + {if count($designations) > 0} - - + + {money_fac name="prix_tpl[]"} {button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"} - {foreach from=$designations item=designation key=key} - + {foreach from=$designations item=designation key=key} - + {money_fac value=$prix[$key] user=$from_user} {button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"} - {/foreach} - - - {else} - - - + {else} + + + {money_fac name="prix_tpl[]"} {button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"} - {/if} + {/if} diff --git a/templates/facture_modifier.tpl b/templates/facture_modifier.tpl index 7f6baeb..b148a29 100644 --- a/templates/facture_modifier.tpl +++ b/templates/facture_modifier.tpl @@ -100,31 +100,27 @@ - {if count($designations) > 0} + {if count($designations) > 0} - - + + {money_fac name="prix_tpl[]"} {button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"} - {foreach from=$designations item=designation key=key} - + {foreach from=$designations item=designation key=key} - + {money_fac value=$prix[$key] user=$from_user} {button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"} - {/foreach} - - - {else} - - - + {else} + + + {money_fac name="prix_tpl[]"} {button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"} - {/if} + {/if} diff --git a/templates/index.tpl b/templates/index.tpl index 71010bc..44c7812 100644 --- a/templates/index.tpl +++ b/templates/index.tpl @@ -51,7 +51,7 @@ {$facture.moyen_paiement} {foreach from=$facture.contenu item=contenu} -

{$contenu.designation} : {$contenu.prix|escape|money} {$config.monnaie}

+

{$contenu.designation} : {$contenu.prix|escape|money:false} {$config.monnaie}

{/foreach} {$facture.total|escape|money} {$config.monnaie} diff --git a/upgrade.php b/upgrade.php index 9eaba65..c3a03a5 100644 --- a/upgrade.php +++ b/upgrade.php @@ -32,7 +32,7 @@ if (version_compare($infos->version, '0.3.0', '<')) $db->exec('DROP TABLE `plugin_facturation_config`;'); } -// 0.3.0 - Migration Facturation\Config vers la table plugins +// 0.4.0 - Migration Facturation\Config vers la table plugins if (version_compare($infos->version, '0.4.0', '<')) { $db->exec(<<version, '0.4.0', '<')) moyen_paiement TEXT NOT NULL, contenu TEXT NOT NULL, total REAL DEFAULT 0 - - -- FOREIGN KEY(moyen_paiement) REFERENCES plugin_facturation_paiement(code) ); INSERT INTO plugin_facturation_factures_tmp SELECT * FROM plugin_facturation_factures; @@ -78,3 +76,50 @@ EOT ); } + +// 0.6.0 - Migration Facturation\Config vers la table plugins +if (version_compare($infos->version, '0.6.0', '<')) +{ + // SQL -> total integer + $db->exec(<<listAll() as $k=>$f) + { + foreach($f->contenu as $line => $content) + { + $contenu[] = ['designation' => $content['designation'], + 'prix' => (int) ($content['prix'] * 100) ]; + } + $f->contenu = $contenu; + $data = (array) $f; + unset($data['id']); + unset($data['date_emission']); + unset($data['date_echeance']); + var_dump($data); + $facture->edit($f->id, $data); + unset($contenu); + } +} \ No newline at end of file diff --git a/www/admin/_inc.php b/www/admin/_inc.php index 7721b8d..5031b36 100644 --- a/www/admin/_inc.php +++ b/www/admin/_inc.php @@ -17,3 +17,38 @@ $facture = new Facture; $tpl->assign('f_obj', $facture); $identite = (string) Config::getInstance()->get('champ_identite'); + +$tpl->register_function('money_fac', function (array $params) + { + static $params_list = ['value', 'name', 'user']; + + // Extract params and keep attributes separated + $attributes = array_diff_key($params, array_flip($params_list)); + $params = array_intersect_key($params, array_flip($params_list)); + extract($params, \EXTR_SKIP); + + $current_value = null; + + if (isset($value)) { + $current_value = $value; + } + + if (!isset($user)) { + $user = false; + } + + if (!isset($name)) + { + $name = 'prix[]'; + } + + if (null !== $current_value && !$user) { + $current_value = Utils::money_format($current_value, ',', ''); + } + + $current_value = htmlspecialchars($current_value, ENT_QUOTES, 'UTF-8'); + + $currency = Config::getInstance()->get('monnaie'); + return sprintf('%s', $name, $current_value, $currency); + } +); diff --git a/www/admin/_upgrade_trick.php b/www/admin/_upgrade_trick.php deleted file mode 100644 index c6aac1b..0000000 --- a/www/admin/_upgrade_trick.php +++ /dev/null @@ -1,13 +0,0 @@ -needUpgrade()) -{ - $plugin->upgrade(); -} - -unset($plugin); \ No newline at end of file diff --git a/www/admin/facture_ajouter.php b/www/admin/facture_ajouter.php index 7debf4d..67d0b70 100644 --- a/www/admin/facture_ajouter.php +++ b/www/admin/facture_ajouter.php @@ -43,60 +43,52 @@ if (f('add')) if ( count(f('designation')) !== count(f('prix')) ) { throw new UserException('Nombre de désignations et de prix reçus différent.'); - } - - $truc = [ - 'numero' =>f('numero_facture'), - 'date_emission' => f('date_emission'), - 'date_echeance' => f('date_echeance'), - 'reglee' => f('reglee') == 1?1:0, - 'archivee' => f('archivee') == 1?1:0, - 'moyen_paiement' => f('moyen_paiement'), - 'toto' => 0 - ]; + } + + $truc = [ + 'numero' =>f('numero_facture'), + 'date_emission' => f('date_emission'), + 'date_echeance' => f('date_echeance'), + 'reglee' => f('reglee') == 1?1:0, + 'archivee' => f('archivee') == 1?1:0, + 'moyen_paiement' => f('moyen_paiement'), + 'toto' => 0 + ]; - if (f('type') == DEVIS) + if (in_array(f('type'), [DEVIS, FACT, CERFA])) + { + $truc['type_facture'] = f('type'); + } + + foreach(f('designation') as $k=>$value) + { + $truc['contenu'][$k]['designation'] = $value; + $truc['contenu'][$k]['prix'] = Utils::moneyToInteger(f('prix')[$k]); + $truc['toto'] += Utils::moneyToInteger(f('prix')[$k]); + } + $truc['total'] = $truc['toto']; + unset($truc['toto']); + + if (f('base_receveur') == 'client') + { + $truc['receveur_membre'] = 0; + $truc['receveur_id'] = f('client'); + } + elseif (f('base_receveur') == 'membre') + { + $truc['receveur_membre'] = 1; + $truc['receveur_id'] = f('membre'); + } + + $id = $facture->add($truc); + + Utils::redirect(PLUGIN_URL . 'facture.php?id='.(int)$id); + + } + catch(UserException $e) { - $truc['type_facture'] = DEVIS; + $form->addError($e->getMessage()); } - elseif (f('type') == FACT) - { - $truc['type_facture'] = FACT; - } - elseif (f('type') == CERFA) - { - $truc['type_facture'] = CERFA; - } - - foreach(f('designation') as $k=>$value) - { - $truc['contenu'][$k]['designation'] = $value; - $truc['contenu'][$k]['prix'] = f('prix')[$k]; - $truc['toto'] += f('prix')[$k]; - } - $truc['total'] = $truc['toto']; - unset($truc['toto']); - - if (f('base_receveur') == 'client') - { - $truc['receveur_membre'] = 0; - $truc['receveur_id'] = f('client'); - } - elseif (f('base_receveur') == 'membre') - { - $truc['receveur_membre'] = 1; - $truc['receveur_id'] = f('membre'); - } - - $id = $facture->add($truc); - - Utils::redirect(PLUGIN_URL . 'facture.php?id='.(int)$id); - - } - catch(UserException $e) - { - $form->addError($e->getMessage()); - } } } @@ -197,21 +189,23 @@ $tpl->assign('membre_id', f('membre') ?: -1); $designations = []; $prix = []; +$from_user = false; if (($d = f('designation')) && ($p = f('prix')) && implode($d)) { foreach($d as $k=>$v) { - if ($v == '' && $p[$k] == 0) + if (empty($v) && empty($p[$k])) { continue; } $designations[] = $v; $prix[] = $p[$k]; } + $from_user = true; } else { $designations = ['Exemple']; - $prix = [1.5]; + $prix = [250]; } $tpl->assign(compact('liste', 'radio', 'step')); @@ -220,9 +214,7 @@ $date = new \DateTime; $date->setTimestamp(time()); $tpl->assign('date', $date->format('d/m/Y')); -$tpl->assign('designations', $designations); -$tpl->assign('prix', $prix); -$tpl->assign('identite', $identite); +$tpl->assign(compact('designations', 'prix', 'from_user', 'identite')); $tpl->assign('membres', $db->getAssoc('SELECT id, '.$identite.' FROM membres WHERE id_category != -2 NOT IN (SELECT id FROM users_categories WHERE hidden = 1);')); $tpl->assign('clients', $db->getAssoc('SELECT id, nom FROM plugin_facturation_clients;')); diff --git a/www/admin/facture_modifier.php b/www/admin/facture_modifier.php index 2fdfe0b..e61527d 100644 --- a/www/admin/facture_modifier.php +++ b/www/admin/facture_modifier.php @@ -65,28 +65,17 @@ if(f('save')) 'toto' => 0 ]; - if (f('type') == DEVIS) + if (in_array(f('type'), [DEVIS, FACT, CERFA])) { - $truc['type_facture'] = DEVIS; - } - elseif (f('type') == FACT) - { - $truc['type_facture'] = FACT; - } - elseif (f('type') == CERFA) - { - $truc['type_facture'] = CERFA; - } - elseif (f('type') == COTIS) - { - $truc['type_facture'] = COTIS; + $truc['type_facture'] = f('type'); } foreach(f('designation') as $k=>$value) { $truc['contenu'][$k]['designation'] = $value; - $truc['contenu'][$k]['prix'] = f('prix')[$k]; - $truc['toto'] += f('prix')[$k]; + $truc['contenu'][$k]['prix'] = Utils::moneyToInteger(f('prix')[$k]); + $truc['toto'] += Utils::moneyToInteger(f('prix')[$k]); + } $truc['total'] = $truc['toto']; unset($truc['toto']); @@ -229,39 +218,39 @@ $tpl->assign(compact('liste', 'radio', 'step')); // C'est un peu l'équivalent de form_field, mais j'avais écrit ça avant // et oulala, c'est un peu complexe, faudrait réfléchir keskivomieux +$from_user = false; if ($f->type_facture != COTIS) { - - if (($d = f('designation')) && ($p = f('prix'))) { foreach($d as $k=>$v) { - if ($v == '' && $p[$k] == 0) + if (empty($v) && empty($p[$k])) { continue; } $designations[] = $v; $prix[] = $p[$k]; } + $from_user = true; } else { foreach($f->contenu as $k=>$v) { - if ($v['designation'] == '' && $v['prix'] == 0) + if (empty($v['designation']) && empty($v['prix'])) { continue; } $designations[] = $v['designation']; $prix[] = $v['prix']; } + $from_user = false; } } -$tpl->assign('designations', $designations??[]); -$tpl->assign('prix', $prix??[]); -$tpl->assign('identite', $identite); + +$tpl->assign(compact('designations', 'prix', 'from_user', 'identite')); $tpl->assign('membres', $db->getAssoc('SELECT id, '.$identite.' FROM membres WHERE id_category != -2 NOT IN (SELECT id FROM users_categories WHERE hidden = 1);')); $tpl->assign('clients', $db->getAssoc('SELECT id, nom FROM plugin_facturation_clients;')); diff --git a/www/admin/index.php b/www/admin/index.php index 0bbbeff..0793e32 100644 --- a/www/admin/index.php +++ b/www/admin/index.php @@ -2,7 +2,6 @@ namespace Garradin; -require_once __DIR__ . '/_upgrade_trick.php'; require_once __DIR__ . '/_inc.php'; $session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ);