facturation/upgrade.php

382 lines
12 KiB
PHP

<?php
namespace Paheko;
use Paheko\Plugin\Facturation\Facture;
$db = DB::getInstance();
$old_version = $plugin->oldVersion();
error_log("upgrade:: à partir de la version = " . $old_version);
// 0.2.0 - Stock le contenu en json plutôt qu'en serialized
if (version_compare($old_version, '0.2.0', '<'))
{
$r = (array) DB::getInstance()->get('SELECT * FROM plugin_facturation_factures');
foreach ($r as $e) {
$e->contenu =json_encode(unserialize((string) $e->contenu));
$db->update('plugin_facturation_factures', $e, $db->where('id', (int)$e->id));
}
}
// 0.3.0 - Migration Facturation\Config vers la table plugins
if (version_compare($old_version, '0.3.0', '<'))
{
$conf = $db->getAssoc('SELECT cle, valeur FROM plugin_facturation_config ORDER BY cle;');
foreach($conf as $k=>$v)
{
$plugin->setConfigProperty($k, $v);
}
$plugin->save();
$db->exec('DROP TABLE `plugin_facturation_config`;');
}
// 0.4.0 -
if (version_compare($old_version, '0.4.0', '<'))
{
$db->exec(<<<EOT
CREATE TABLE IF NOT EXISTS plugin_facturation_paiement
-- Moyens de paiement
(
code TEXT NOT NULL PRIMARY KEY,
nom TEXT NOT NULL
);
INSERT OR IGNORE INTO plugin_facturation_paiement (code, nom) VALUES ('CB', 'Carte bleue');
INSERT OR IGNORE INTO plugin_facturation_paiement (code, nom) VALUES ('CH', 'Chèque');
INSERT OR IGNORE INTO plugin_facturation_paiement (code, nom) VALUES ('ES', 'Espèces');
INSERT OR IGNORE INTO plugin_facturation_paiement (code, nom) VALUES ('PR', 'Prélèvement');
INSERT OR IGNORE INTO plugin_facturation_paiement (code, nom) VALUES ('TI', 'TIP');
INSERT OR IGNORE INTO plugin_facturation_paiement (code, nom) VALUES ('VI', 'Virement');
INSERT OR IGNORE INTO plugin_facturation_paiement (code, nom) VALUES ('AU', 'Autre');
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 REAL DEFAULT 0
);
INSERT INTO plugin_facturation_factures_tmp SELECT * FROM plugin_facturation_factures;
DROP TABLE plugin_facturation_factures;
ALTER TABLE plugin_facturation_factures_tmp RENAME TO plugin_facturation_factures;
EOT
);
}
// 0.6.0 -
if (version_compare($old_version, '0.6.0', '<'))
{
defined('DEVIS') || define('DEVIS', 0);
defined('FACT') || define('FACT', 1);
defined('CERFA') || define('CERFA', 2);
defined('COTIS') || define('COTIS', 3);
$facture = new Facture;
$r = $db->first('SELECT id, total FROM plugin_facturation_factures;');
if ($r && strpos((string) $r->total, '.') !== false)
{
// 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 * FROM plugin_facturation_factures;
DROP TABLE plugin_facturation_factures;
ALTER TABLE plugin_facturation_factures_tmp RENAME TO plugin_facturation_factures;
EOT
);
$factures = $facture->listAll();
foreach($factures as $k=>$f)
{
$contenu = [];
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) ];
}
$f->contenu = $contenu;
$data = (array) $f;
$data['total'] = (int) ($data['total'] * 100);
unset($data['id']);
unset($data['date_emission']);
unset($data['date_echeance']);
$facture->edit($f->id, $data);
unset($contenu);
}
}
}
// 0.6.2 -
if (version_compare($old_version, '0.6.2', '<'))
{
defined('DEVIS') || define('DEVIS', 0);
defined('FACT') || define('FACT', 1);
defined('CERFA') || define('CERFA', 2);
defined('COTIS') || define('COTIS', 3);
$facture = new Facture;
$db->exec(<<<EOT
INSERT OR IGNORE INTO plugin_facturation_paiement
(code, nom) VALUES ('HA', 'HelloAsso');
INSERT OR IGNORE INTO plugin_facturation_paiement
(code, nom) VALUES ('AU', 'Autre');
CREATE TABLE IF NOT EXISTS
plugin_facturation_txt_cerfa
(
id PRIMARY KEY,
menu TEXT NOT NULL UNIQUE,
texte TEXT NOT NULL
);
INSERT OR IGNORE INTO plugin_facturation_txt_cerfa
("id","menu","texte") VALUES ('0','Aucun','');
INSERT OR IGNORE INTO plugin_facturation_txt_cerfa
("id","menu","texte")
VALUES ('1','HelloAsso','Don via HelloAsso');
INSERT OR IGNORE INTO plugin_facturation_txt_cerfa
("id","menu","texte")
VALUES ('2','Frais de déplacement',
'Renonciation aux remboursements de frais de déplacement');
INSERT OR IGNORE INTO plugin_facturation_txt_cerfa
("id","menu","texte")
VALUES ('3','Don en nature','Don en nature');
EOT
);
// Migration CERFA
$factures = $facture->listAll();
foreach($factures as $k=>$f)
{
if ($f->type_facture != CERFA)
{
continue;
}
$f->contenu = ['forme' => 1, 'nature' => 1, 'texte' => 0];
$data = (array) $f;
unset($data['id']);
unset($data['date_emission']);
unset($data['date_echeance']);
$facture->edit($f->id, $data);
}
}
// 0.7.1 - Ajout clé config TTC/HT
if (version_compare($old_version, '0.7.1', '<'))
{
$plugin->setConfigProperty('ttc', false);
$plugin->save();
}
// 0.8.1 - Signal menu item
if (version_compare($old_version, '0.8.1', '<'))
{
$plugin->unregisterSignal('menu.item');
}
// 0.8.5 Ajout champs SIREN/SIRET à la table clients
if (version_compare($old_version, '0.8.5', '<'))
{
$db->exec(<<<EOT
CREATE TABLE IF NOT EXISTS plugin_facturation_clients_tmp
(
id INTEGER PRIMARY KEY,
nom TEXT NOT NULL,
adresse TEXT NOT NULL,
code_postal TEXT NOT NULL,
ville TEXT NOT NULL,
siret TEXT,
date_creation TEXT NOT NULL DEFAULT CURRENT_DATE CHECK (date(date_creation) IS NOT NULL AND date(date_creation) = date_creation),
telephone TEXT,
email TEXT
);
EOT
);
// copier les clients dans la table temporaire en ajoutant un siret fictif
$sql = 'SELECT * FROM plugin_facturation_clients';
foreach ($db->iterate($sql) as $client)
{
$db->insert('plugin_facturation_clients_tmp', $client);
}
// remplacer l'ancienne table par la nouvelle
$db->exec(<<<EOT
DROP TABLE plugin_facturation_clients;
ALTER TABLE plugin_facturation_clients_tmp RENAME TO plugin_facturation_clients;
EOT
);
}
// 0.12 Ajout Mollie à la table moyens de paiement
if (version_compare($old_version, '0.12', '<'))
{
$db->exec(<<<EOT
INSERT OR IGNORE INTO plugin_facturation_paiement
(code, nom) VALUES ('MO', 'Mollie');
EOT
);
}
// version 0.15
// Ajout champs note et contact à la table clients
// Ajout divers champs à la table factures
if (version_compare($old_version, '0.15', '<'))
{
$db->exec(<<<EOT
CREATE TABLE IF NOT EXISTS plugin_facturation_clients_tmp
(
id INTEGER PRIMARY KEY,
nom TEXT NOT NULL,
adresse TEXT NOT NULL,
code_postal TEXT NOT NULL,
ville TEXT NOT NULL,
siret TEXT,
date_creation TEXT NOT NULL DEFAULT CURRENT_DATE CHECK (date(date_creation) IS NOT NULL AND date(date_creation) = date_creation),
telephone TEXT,
email TEXT,
nom_contact TEXT,
note TEXT
);
EOT
);
// copier les clients dans la table temporaire
$sql = 'SELECT * FROM plugin_facturation_clients';
foreach ($db->iterate($sql) as $client)
{
$db->insert('plugin_facturation_clients_tmp', $client);
}
// remplacer l'ancienne table par la nouvelle
$db->exec(<<<EOT
DROP TABLE plugin_facturation_clients;
ALTER TABLE plugin_facturation_clients_tmp RENAME TO plugin_facturation_clients;
EOT
);
$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,
nom_contact TEXT,
numero_commande TEXT,
reference_acheteur TEXT
);
EOT
);
// copier les factures dans la table temporaire
$sql = 'SELECT * FROM plugin_facturation_factures';
foreach ($db->iterate($sql) as $facture)
{
$db->insert('plugin_facturation_factures_tmp', $facture);
}
// remplacer l'ancienne table par la nouvelle
$db->exec(<<<EOT
DROP TABLE plugin_facturation_factures;
ALTER TABLE plugin_facturation_factures_tmp RENAME TO plugin_facturation_factures;
EOT
);
}
// 0.18 - Personnalisation du rendu des factures
if (version_compare($old_version, '0.18', '<'))
{
$defaults = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
foreach (['invoice_theme', 'invoice_accent_color', 'invoice_note', 'payment_terms', 'invoice_show_status'] as $key) {
if ($plugin->getConfig($key) === null && array_key_exists($key, $defaults)) {
$plugin->setConfigProperty($key, $defaults[$key]);
}
}
$plugin->save();
}
// 0.19 - Réparation des schémas partiellement migrés
if (version_compare($old_version, '0.19', '<'))
{
$tables = [
'plugin_facturation_clients' => [
'siret' => 'siret TEXT',
'telephone' => 'telephone TEXT',
'email' => 'email TEXT',
'nom_contact' => 'nom_contact TEXT',
'note' => 'note TEXT',
],
'plugin_facturation_factures' => [
'nom_contact' => 'nom_contact TEXT',
'numero_commande' => 'numero_commande TEXT',
'reference_acheteur' => 'reference_acheteur TEXT',
],
];
foreach ($tables as $table => $columns) {
$schema = $db->getTableSchema($table);
if (!$schema) {
continue;
}
foreach ($columns as $column => $definition) {
if (!isset($schema['columns'][$column])) {
$db->exec(sprintf('ALTER TABLE %s ADD COLUMN %s;', $db->quoteIdentifier($table), $definition));
}
}
}
}
// 0.25 - Options d'affichage du rendu facture
if (version_compare($old_version, '0.25', '<'))
{
$defaults = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
foreach (['invoice_show_address_borders', 'invoice_show_email', 'invoice_show_web'] as $key) {
if ($plugin->getConfig($key) === null && array_key_exists($key, $defaults)) {
$plugin->setConfigProperty($key, $defaults[$key]);
}
}
$plugin->save();
}