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
1 changed files with 38 additions and 22 deletions

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', '<'))
@ -79,6 +85,9 @@ 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', '<'))
{
$r = $db->first('SELECT id, total FROM plugin_facturation_factures;');
if (strpos($r->total,'.'))
{ {
// SQL -> total integer // SQL -> total integer
$db->exec(<<<EOT $db->exec(<<<EOT
@ -99,27 +108,34 @@ if (version_compare($infos->version, '0.6.0', '<'))
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($factures = $facture->listAll() as $k=>$f)
{ {
foreach($f->contenu as $line => $content) 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'], $contenu[] = ['designation' => $content['designation'],
'prix' => (int) ($content['prix'] * 100) ]; 'prix' => (int) ($content['prix'] * 100) ];
} }
$f->contenu = $contenu; $f->contenu = $contenu;
$data = (array) $f; $data = (array) $f;
$data['total'] = (int) ($data['total'] * 100);
unset($data['id']); unset($data['id']);
unset($data['date_emission']); unset($data['date_emission']);
unset($data['date_echeance']); unset($data['date_echeance']);
var_dump($data);
$facture->edit($f->id, $data); $facture->edit($f->id, $data);
unset($contenu); unset($contenu);
} }
} }
}