Utiliser des entiers au lieu de float tel Garradin

This commit is contained in:
Noizette 2021-11-05 23:47:36 +01:00
parent 11974482f2
commit ede7c789d7
13 changed files with 189 additions and 139 deletions

View File

@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS plugin_facturation_factures (
archivee INTEGER DEFAULT 0, -- bool archivee INTEGER DEFAULT 0, -- bool
moyen_paiement TEXT NOT NULL, moyen_paiement TEXT NOT NULL,
contenu TEXT NOT NULL, contenu TEXT NOT NULL,
total REAL DEFAULT 0 total INTEGER DEFAULT 0
-- FOREIGN KEY(moyen_paiement) REFERENCES compta_moyens_paiement(code) -- FOREIGN KEY(moyen_paiement) REFERENCES compta_moyens_paiement(code)
); );

View File

@ -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." description="Permet d'éditer des factures, devis et reçus à ses membres ainsi qu'à une base de clients supplémentaire."
auteur="zou" auteur="zou"
url="https://gitlab.com/ramoloss/garradin-plugin-facturation/" url="https://gitlab.com/ramoloss/garradin-plugin-facturation/"
version="0.5.0" version="0.6.0"
menu=1 menu=1
config=1 config=1
min_version="1.1.0" min_version="1.1.0"

View File

@ -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) public function _checkFields(&$datas)
{ {
foreach($datas as $k=>$data) foreach($datas as $k=>$data)
@ -125,20 +126,28 @@ class Facture
throw new UserException("Le contenu du document est vide ($data)."); throw new UserException("Le contenu du document est vide ($data).");
} }
$total = 0; $total = 0;
$vide = 1; foreach($datas[$k] as $g => $r)
foreach($datas[$k] as $g=>$r)
{ {
if ($r['designation'] !== '' && is_numeric($r['prix'])) if (empty($r['designation']) && empty($r['prix']))
{
$vide = 0;
}
else
{ {
unset($datas[$k][$g]); 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']; $total += $r['prix'];
} }
if($fac && $vide)
if($fac && !$total)
{ {
throw new UserException("Toutes les désignations/prix sont vides."); throw new UserException("Toutes les désignations/prix sont vides.");
} }
@ -239,7 +248,7 @@ class Facture
return $db->update('plugin_facturation_factures', $data, $db->where('id', (int)$id)); 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; $client = new Client;
@ -271,7 +280,7 @@ class Facture
return empty($r)?false:$r; return empty($r)?false:$r;
} }
public function hasDocs($base = 0, $id) public function hasDocs($base, $id)
{ {
$client = new Client; $client = new Client;

View File

@ -15,6 +15,8 @@
function plus(){ function plus(){
var newdiv = document.createElement('tr'); var newdiv = document.createElement('tr');
newdiv.innerHTML = document.getElementById('Line1').innerHTML; 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(){ newdiv.querySelector('.fact_rm_line button').onclick = function(){
this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
updateSum(); updateSum();

View File

@ -109,26 +109,22 @@
<tbody id="Lines"> <tbody id="Lines">
{if count($designations) > 0} {if count($designations) > 0}
<tr id="Line1" class="hidden"> <tr id="Line1" class="hidden">
<td><textarea name="designation[]" style="width:98%;"></textarea></td> <td><textarea name="designation_tpl[]" style="width:98%;"></textarea></td>
<td><input type="number" step="0.01" value="0" style="width: 60%" onchange="updateSum();" name="prix[]"><span style="position: relative;right: 50px;">€</span></td> {money_fac name="prix_tpl[]"}
<td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td> <td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td>
</tr> </tr>
{foreach from=$designations item=designation key=key} {foreach from=$designations item=designation key=key}
<tr> <tr>
<td><textarea name="designation[]" style="width:98%;">{$designation}</textarea></td> <td><textarea name="designation[]" style="width:98%;">{$designation}</textarea></td>
<td><input type="number" step="0.01" value="{$prix[$key]}" style="width: 60%" onchange="updateSum();" name="prix[]"><span style="position: relative;right: 50px;">€</span></td> {money_fac value=$prix[$key] user=$from_user}
<td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td> <td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td>
</tr> </tr>
{/foreach} {/foreach}
{else} {else}
<tr id="Line1"> <tr id="Line1" class="hidden">
<td><textarea name="designation[]" style="width:98%;"></textarea></td> <td><textarea name="designation_tpl[]" style="width:98%;"></textarea></td>
<td><input type="number" step="0.01" value="0" style="width: 60%" onchange="updateSum();" name="prix[]"><span style="position: relative;right: 50px;">€</span></td> {money_fac name="prix_tpl[]"}
<td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td> <td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td>
</tr> </tr>
{/if} {/if}

View File

@ -102,26 +102,22 @@
<tbody id="Lines"> <tbody id="Lines">
{if count($designations) > 0} {if count($designations) > 0}
<tr id="Line1" class="hidden"> <tr id="Line1" class="hidden">
<td><textarea name="designation[]" style="width:98%;"></textarea></td> <td><textarea name="designation_tpl[]" style="width:98%;"></textarea></td>
<td><input type="number" step="0.01" value="0" style="width: 60%" onchange="updateSum();" name="prix[]"><span style="position: relative;right: 50px;">€</span></td> {money_fac name="prix_tpl[]"}
<td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td> <td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td>
</tr> </tr>
{foreach from=$designations item=designation key=key} {foreach from=$designations item=designation key=key}
<tr> <tr>
<td><textarea name="designation[]" style="width:98%;">{$designation}</textarea></td> <td><textarea name="designation[]" style="width:98%;">{$designation}</textarea></td>
<td><input type="number" step="0.01" value="{$prix[$key]}" style="width: 60%" onchange="updateSum();" name="prix[]"><span style="position: relative;right: 50px;">€</span></td> {money_fac value=$prix[$key] user=$from_user}
<td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td> <td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td>
</tr> </tr>
{/foreach} {/foreach}
{else} {else}
<tr id="Line1"> <tr id="Line1" class="hidden">
<td><textarea name="designation[]" style="width:98%;"></textarea></td> <td><textarea name="designation_tpl[]" style="width:98%;"></textarea></td>
<td><input type="number" step="0.01" value="0" style="width: 60%" onchange="updateSum();" name="prix[]"><span style="position: relative;right: 50px;">€</span></td> {money_fac name="prix_tpl[]"}
<td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td> <td class="fact_rm_line">{button label="Enlever" title="Enlever la ligne" shape="minus" min="2" name="remove_line"}</td>
</tr> </tr>
{/if} {/if}

View File

@ -51,7 +51,7 @@
<td>{$facture.moyen_paiement}</td> <td>{$facture.moyen_paiement}</td>
<td> <td>
{foreach from=$facture.contenu item=contenu} {foreach from=$facture.contenu item=contenu}
<p>{$contenu.designation} : {$contenu.prix|escape|money}&nbsp;{$config.monnaie}</p> <p>{$contenu.designation} : {$contenu.prix|escape|money:false}&nbsp;{$config.monnaie}</p>
{/foreach} {/foreach}
</td> </td>
<td>{$facture.total|escape|money}&nbsp;{$config.monnaie}</td> <td>{$facture.total|escape|money}&nbsp;{$config.monnaie}</td>

View File

@ -32,7 +32,7 @@ if (version_compare($infos->version, '0.3.0', '<'))
$db->exec('DROP TABLE `plugin_facturation_config`;'); $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', '<')) if (version_compare($infos->version, '0.4.0', '<'))
{ {
$db->exec(<<<EOT $db->exec(<<<EOT
@ -66,8 +66,6 @@ if (version_compare($infos->version, '0.4.0', '<'))
moyen_paiement TEXT NOT NULL, moyen_paiement TEXT NOT NULL,
contenu TEXT NOT NULL, contenu TEXT NOT NULL,
total REAL DEFAULT 0 total REAL DEFAULT 0
-- FOREIGN KEY(moyen_paiement) REFERENCES plugin_facturation_paiement(code)
); );
INSERT INTO plugin_facturation_factures_tmp SELECT * FROM plugin_facturation_factures; 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(<<<EOT
CREATE TABLE IF NOT EXISTS plugin_facturation_factures_tmp
(
id INTEGER PRIMARY KEY,
type_facture INTEGER NOT NULL DEFAULT 0,
numero TEXT NOT NULL UNIQUE,
receveur_membre INTEGER NOT NULL, -- bool
receveur_id INTEGER NOT NULL,
date_emission TEXT NOT NULL, -- CHECK (date(date_emission) IS NOT NULL AND date(date_emission) = date_emission),
date_echeance TEXT NOT NULL, -- CHECK (date(date_echeance) IS NOT NULL AND date(date_echeance) = date_echeance),
reglee INTEGER DEFAULT 0, -- bool
archivee INTEGER DEFAULT 0, -- bool
moyen_paiement TEXT NOT NULL,
contenu TEXT NOT NULL,
total INTEGER DEFAULT 0
);
INSERT INTO plugin_facturation_factures_tmp SELECT id, type_facture, numero, receveur_membre, receveur_id, date_emission, date_echeance, reglee, archivee, moyen_paiement, contenu, CAST(total * 100 as INT) as total FROM plugin_facturation_factures;
DROP TABLE plugin_facturation_factures;
ALTER TABLE plugin_facturation_factures_tmp RENAME TO plugin_facturation_factures;
EOT
);
foreach($factures = $facture->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);
}
}

View File

@ -17,3 +17,38 @@ $facture = new Facture;
$tpl->assign('f_obj', $facture); $tpl->assign('f_obj', $facture);
$identite = (string) Config::getInstance()->get('champ_identite'); $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('<td><nobr><input type="text" pattern="[0-9]*([.,][0-9]{1,2})?" inputmode="decimal" size="8" class="money" style="width: 60%%" onchange="updateSum();" name="%s" value="%s" /><b>%s</b></nobr></td>', $name, $current_value, $currency);
}
);

View File

@ -1,13 +0,0 @@
<?php
namespace Garradin;
// Trick to migrate from v0.3 to v0.4, before upgrading Garradin to 1.0
// Remove in future versions?
$plugin = new Plugin('facturation');
if ($plugin->needUpgrade())
{
$plugin->upgrade();
}
unset($plugin);

View File

@ -55,24 +55,16 @@ if (f('add'))
'toto' => 0 'toto' => 0
]; ];
if (f('type') == DEVIS) if (in_array(f('type'), [DEVIS, FACT, CERFA]))
{ {
$truc['type_facture'] = DEVIS; $truc['type_facture'] = f('type');
}
elseif (f('type') == FACT)
{
$truc['type_facture'] = FACT;
}
elseif (f('type') == CERFA)
{
$truc['type_facture'] = CERFA;
} }
foreach(f('designation') as $k=>$value) foreach(f('designation') as $k=>$value)
{ {
$truc['contenu'][$k]['designation'] = $value; $truc['contenu'][$k]['designation'] = $value;
$truc['contenu'][$k]['prix'] = f('prix')[$k]; $truc['contenu'][$k]['prix'] = Utils::moneyToInteger(f('prix')[$k]);
$truc['toto'] += f('prix')[$k]; $truc['toto'] += Utils::moneyToInteger(f('prix')[$k]);
} }
$truc['total'] = $truc['toto']; $truc['total'] = $truc['toto'];
unset($truc['toto']); unset($truc['toto']);
@ -197,21 +189,23 @@ $tpl->assign('membre_id', f('membre') ?: -1);
$designations = []; $designations = [];
$prix = []; $prix = [];
$from_user = false;
if (($d = f('designation')) && ($p = f('prix')) && implode($d)) if (($d = f('designation')) && ($p = f('prix')) && implode($d))
{ {
foreach($d as $k=>$v) foreach($d as $k=>$v)
{ {
if ($v == '' && $p[$k] == 0) if (empty($v) && empty($p[$k]))
{ {
continue; continue;
} }
$designations[] = $v; $designations[] = $v;
$prix[] = $p[$k]; $prix[] = $p[$k];
} }
$from_user = true;
} }
else { else {
$designations = ['Exemple']; $designations = ['Exemple'];
$prix = [1.5]; $prix = [250];
} }
$tpl->assign(compact('liste', 'radio', 'step')); $tpl->assign(compact('liste', 'radio', 'step'));
@ -220,9 +214,7 @@ $date = new \DateTime;
$date->setTimestamp(time()); $date->setTimestamp(time());
$tpl->assign('date', $date->format('d/m/Y')); $tpl->assign('date', $date->format('d/m/Y'));
$tpl->assign('designations', $designations); $tpl->assign(compact('designations', 'prix', 'from_user', 'identite'));
$tpl->assign('prix', $prix);
$tpl->assign('identite', $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('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;')); $tpl->assign('clients', $db->getAssoc('SELECT id, nom FROM plugin_facturation_clients;'));

View File

@ -65,28 +65,17 @@ if(f('save'))
'toto' => 0 'toto' => 0
]; ];
if (f('type') == DEVIS) if (in_array(f('type'), [DEVIS, FACT, CERFA]))
{ {
$truc['type_facture'] = DEVIS; $truc['type_facture'] = f('type');
}
elseif (f('type') == FACT)
{
$truc['type_facture'] = FACT;
}
elseif (f('type') == CERFA)
{
$truc['type_facture'] = CERFA;
}
elseif (f('type') == COTIS)
{
$truc['type_facture'] = COTIS;
} }
foreach(f('designation') as $k=>$value) foreach(f('designation') as $k=>$value)
{ {
$truc['contenu'][$k]['designation'] = $value; $truc['contenu'][$k]['designation'] = $value;
$truc['contenu'][$k]['prix'] = f('prix')[$k]; $truc['contenu'][$k]['prix'] = Utils::moneyToInteger(f('prix')[$k]);
$truc['toto'] += f('prix')[$k]; $truc['toto'] += Utils::moneyToInteger(f('prix')[$k]);
} }
$truc['total'] = $truc['toto']; $truc['total'] = $truc['toto'];
unset($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 // 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 // et oulala, c'est un peu complexe, faudrait réfléchir keskivomieux
$from_user = false;
if ($f->type_facture != COTIS) if ($f->type_facture != COTIS)
{ {
if (($d = f('designation')) && ($p = f('prix'))) if (($d = f('designation')) && ($p = f('prix')))
{ {
foreach($d as $k=>$v) foreach($d as $k=>$v)
{ {
if ($v == '' && $p[$k] == 0) if (empty($v) && empty($p[$k]))
{ {
continue; continue;
} }
$designations[] = $v; $designations[] = $v;
$prix[] = $p[$k]; $prix[] = $p[$k];
} }
$from_user = true;
} }
else else
{ {
foreach($f->contenu as $k=>$v) foreach($f->contenu as $k=>$v)
{ {
if ($v['designation'] == '' && $v['prix'] == 0) if (empty($v['designation']) && empty($v['prix']))
{ {
continue; continue;
} }
$designations[] = $v['designation']; $designations[] = $v['designation'];
$prix[] = $v['prix']; $prix[] = $v['prix'];
} }
$from_user = false;
} }
} }
$tpl->assign('designations', $designations??[]);
$tpl->assign('prix', $prix??[]); $tpl->assign(compact('designations', 'prix', 'from_user', 'identite'));
$tpl->assign('identite', $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('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;')); $tpl->assign('clients', $db->getAssoc('SELECT id, nom FROM plugin_facturation_clients;'));

View File

@ -2,7 +2,6 @@
namespace Garradin; namespace Garradin;
require_once __DIR__ . '/_upgrade_trick.php';
require_once __DIR__ . '/_inc.php'; require_once __DIR__ . '/_inc.php';
$session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ); $session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ);