Evite que la migration se fasse plusieurs fois et fuck up les sommes

This commit is contained in:
Noizette 2021-11-06 02:47:19 +01:00
parent d0543cd744
commit 5a4fede36d

View File

@ -1,11 +1,17 @@
<?php <?php
namespace Garradin; namespace Garradin;
use Garradin\Plugin\Facturation\Facture;
define('DEVIS', 0);
define('FACT', 1);
define('CERFA', 2);
define('COTIS', 3);
$db = DB::getInstance(); $db = DB::getInstance();
$facture = new Facture;
$infos = $plugin->getInfos(); $infos = $plugin->getInfos();
// Pl il y avait cette ligne ?
// $db->import(dirname(__FILE__) . "/data/schema.sql");
// 0.2.0 - Stock le contenu en json plutôt qu'en serialized // 0.2.0 - Stock le contenu en json plutôt qu'en serialized
if (version_compare($infos->version, '0.2.0', '<')) if (version_compare($infos->version, '0.2.0', '<'))
@ -80,9 +86,12 @@ EOT
// 0.6.0 - Migration Facturation\Config vers la table plugins // 0.6.0 - Migration Facturation\Config vers la table plugins
if (version_compare($infos->version, '0.6.0', '<')) if (version_compare($infos->version, '0.6.0', '<'))
{ {
// SQL -> total integer $r = $db->first('SELECT id, total FROM plugin_facturation_factures;');
$db->exec(<<<EOT if (strpos($r->total,'.'))
{
// SQL -> total integer
$db->exec(<<<EOT
CREATE TABLE IF NOT EXISTS plugin_facturation_factures_tmp CREATE TABLE IF NOT EXISTS plugin_facturation_factures_tmp
( (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
@ -98,28 +107,35 @@ if (version_compare($infos->version, '0.6.0', '<'))
contenu TEXT NOT NULL, contenu TEXT NOT NULL,
total INTEGER DEFAULT 0 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; INSERT INTO plugin_facturation_factures_tmp SELECT * FROM plugin_facturation_factures;
DROP TABLE plugin_facturation_factures; DROP TABLE plugin_facturation_factures;
ALTER TABLE plugin_facturation_factures_tmp RENAME TO plugin_facturation_factures; ALTER TABLE plugin_facturation_factures_tmp RENAME TO plugin_facturation_factures;
EOT EOT
); );
foreach($factures = $facture->listAll() as $k=>$f)
{
foreach($f->contenu as $line => $content)
{
// Petit bug qui peut arriver avec des contenus mal enregistrés en db
if (is_int($content))
{
continue;
}
$contenu[] = ['designation' => $content['designation'],
'prix' => (int) ($content['prix'] * 100) ];
}
foreach($factures = $facture->listAll() as $k=>$f) $f->contenu = $contenu;
{ $data = (array) $f;
foreach($f->contenu as $line => $content) $data['total'] = (int) ($data['total'] * 100);
{ unset($data['id']);
$contenu[] = ['designation' => $content['designation'], unset($data['date_emission']);
'prix' => (int) ($content['prix'] * 100) ]; unset($data['date_echeance']);
} $facture->edit($f->id, $data);
$f->contenu = $contenu; unset($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);
} }
} }