diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php
index 4a212792be0..4f27a70a0a5 100644
--- a/htdocs/install/upgrade2.php
+++ b/htdocs/install/upgrade2.php
@@ -151,6 +151,7 @@ if (isset($_POST['action']) && $_POST['action'] == 'upgrade')
// dans la 1ere colonne, la description de l'action a faire
// dans la 4eme colonne, le texte 'OK' si fait ou 'AlreadyDone' si rien n'est fait ou 'Error'
+ // Script pour V2 -> V2.1
migrate_paiements($db,$langs,$conf);
migrate_contracts_det($db,$langs,$conf);
@@ -174,11 +175,13 @@ if (isset($_POST['action']) && $_POST['action'] == 'upgrade')
migrate_paiementfourn_facturefourn($db,$langs,$conf);
migrate_delete_old_files($db,$langs,$conf);
+
+ // Script pour V2.1 -> V2.2
+ migrate_paiements_orphelins($db,$langs,$conf);
// On commit dans tous les cas.
// La procédure etant conçue pour pouvoir passer plusieurs fois quelquesoit la situation.
- $db->commit();
-
+ $db->commit(); // FIXME
$db->close();
}
@@ -199,7 +202,7 @@ pFooter($error,$setuplang);
/**
- * Mise a jour des paiements (lien n-n paiements factures)
+ * Reporte liens vers une facture de paiements sur table de jointure (lien n-n paiements factures)
*/
function migrate_paiements($db,$langs,$conf)
{
@@ -249,7 +252,7 @@ function migrate_paiements($db,$langs,$conf)
$res += $db->query($sql);
- print $langs->trans('MigrationProcessPaymentUpdate', $row[$i])."
\n";
+ print $langs->trans('MigrationProcessPaymentUpdate', $row[$i][0])."
\n";
}
}
@@ -273,6 +276,117 @@ function migrate_paiements($db,$langs,$conf)
}
+/**
+ * Corrige paiement orphelins (liens paumes suite a bugs)
+ * Pour verifier s'il reste des orphelins:
+ * select * from llx_paiement as p left join llx_paiement_facture as pf on pf.fk_paiement=p.rowid WHERE pf.rowid IS NULL AND (p.fk_facture = 0 OR p.fk_facture IS NULL)
+ */
+function migrate_paiements_orphelins($db,$langs,$conf)
+{
+ print '
';
+
+ print ' ';
+ print ''.$langs->trans('MigrationPaymentsUpdate')." \n";
+
+ // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
+ $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
+ $sql.= " bu2.url_id as socid";
+ $sql.= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
+ $sql.= " left join llx_paiement_facture as pf on pf.fk_paiement=p.rowid";
+ $sql.= " left join llx_bank_url as bu2 on (bu.fk_bank=bu2.fk_bank AND bu2.type='company')";
+ $sql.= " WHERE pf.rowid IS NULL AND (p.rowid=bu.url_id AND bu.type='payment') AND bu.fk_bank = b.rowid";
+ $sql.= " AND b.rappro = 1";
+ $sql.= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
+ $resql = $db->query($sql);
+
+ if ($resql)
+ {
+ $i = $j = 0;
+ $row = array();
+ $num = $db->num_rows($resql);
+
+ while ($i < $num)
+ {
+ $obj = $db->fetch_object($resql);
+ if ($obj->pamount == $obj->bamount) // Pour etre sur d'avoir bon cas
+ {
+ $row[$j]['paymentid'] = $obj->rowid ; // paymentid
+ $row[$j]['pamount'] = $obj->pamount;
+ $row[$j]['fk_bank'] = $obj->fk_bank;
+ $row[$j]['bamount'] = $obj->bamount;
+ $row[$j]['socid'] = $obj->socid;
+ $row[$j]['datec'] = $obj->datec;
+ $j++;
+ }
+ $i++;
+ }
+ }
+ else {
+ dolibarr_print_error($db);
+ }
+
+ if ($num)
+ {
+ print $langs->trans('MigrationPaymentsNumberToUpdate', $num)." \n";
+ if ($db->begin())
+ {
+ $res = 0;
+ for ($i = 0 ; $i < sizeof($row) ; $i++)
+ {
+ //print '* '.$row[$i]['datec'].' paymentid='.$row[$i]['paymentid'].' '.$row[$i]['pamount'].' fk_bank='.$row[$i]['fk_bank'].' '.$row[$i]['bamount'].' socid='.$row[$i]['socid'].' ';
+ // On cherche facture du meme montant pour meme societe.
+ // Si y en a plusieurs, on prend la premiere sans lien vers un paiement
+ if ($row[$i]['socid'] > 0)
+ {
+ $sql=" SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
+ $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
+ $sql.=" WHERE f.fk_statut in (2,3) AND fk_soc = ".$row[$i]['socid']." AND total_ttc = ".$row[$i]['pamount'];
+ $sql.=" AND pf.fk_facture IS NULL";
+ $sql.=" ORDER BY f.fk_statut";
+ print $sql.' ';
+ $resql=$db->query($sql);
+ if ($resql)
+ {
+ $num = $db->num_rows($resql);
+ //print 'Nb of invoice found for this amount and company :'.$num.' ';
+ if ($num >= 1)
+ {
+ $obj=$db->fetch_object($resql);
+ $facid=$obj->rowid;
+
+ $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
+ $sql .= " VALUES (".$facid.",".$row[$i]['paymentid'].",".$row[$i]['pamount'].")";
+ $res += $db->query($sql);
+
+ print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])." \n";
+ }
+ }
+ else
+ {
+ print 'ERROR';
+ }
+ }
+ }
+ }
+
+ if ($res > 0)
+ {
+ print $langs->trans('MigrationSuccessfullUpdate')." ";
+ }
+ else
+ {
+ print $langs->trans('MigrationPaymentsNothingToUpdate')." \n";
+ }
+ }
+ else
+ {
+ print $langs->trans('MigrationPaymentsNothingToUpdate')." \n";
+ }
+
+ print ' |
';
+}
+
+
/*
* Mise a jour des contrats (gestion du contrat + detail de contrat)
*/
diff --git a/mysql/migration/2.0.0-2.1.0.sql b/mysql/migration/2.0.0-2.1.0.sql
index 138d21f5661..a5e94bf344f 100644
--- a/mysql/migration/2.0.0-2.1.0.sql
+++ b/mysql/migration/2.0.0-2.1.0.sql
@@ -227,7 +227,7 @@ alter table llx_paiementfourn add statut smallint(6) NOT NULL DEFAULT 0;
alter table llx_bank_url add column type enum("company","payment","member","subscription","donation","sc","payment_sc");
update llx_bank_url set type=null where type='';
-alter table llx_bank_url modify type enum("company","payment","member","subscription","donation","sc","payment_sc");
+alter table llx_bank_url modify type enum("company","payment","member","subscription","donation","sc","payment_sc") NOT NULL;
update llx_bank_url set type = 'payment_supplier' where label = '(paiement)' and type='payment' and url like '%/fourn/%';
diff --git a/mysql/migration/2.1.0-2.2.0.sql b/mysql/migration/2.1.0-2.2.0.sql
index 1269c0d77ee..f68b4782ff7 100644
--- a/mysql/migration/2.1.0-2.2.0.sql
+++ b/mysql/migration/2.1.0-2.2.0.sql
@@ -8,6 +8,14 @@
-- sans AUCUNE erreur ni warning
--
+-- Sequence de requete pour nettoyage et correction champ type table llx_bank_url
+update llx_bank_url set type='company' where (type is null or type = '') and url like '%compta/fiche.php?socid=%';
+alter table llx_bank_url modify `type` enum("","?","company","payment","payment_supplier","member","subscription","donation","sc","payment_sc");
+update llx_bank_url set type='?' where (type is null or type = '') and url like '%compta/facture.php?facid=%';
+update llx_bank_url set type='payment_supplier' where (type='' or type is null) and url like '%fourn/paiement/fiche.php?id=%';
+alter table llx_bank_url modify `type` enum("?","company","payment","payment_supplier","member","subscription","donation","sc","payment_sc") NOT NULL;
+
+
update llx_actioncomm set fk_action = 9 where fk_action = 10;
ALTER TABLE llx_cotisation ADD UNIQUE INDEX uk_cotisation (fk_adherent,dateadh);
diff --git a/mysql/tables/llx_bank_url.sql b/mysql/tables/llx_bank_url.sql
index a5d38b931ad..e7cc5bdcae9 100644
--- a/mysql/tables/llx_bank_url.sql
+++ b/mysql/tables/llx_bank_url.sql
@@ -28,5 +28,5 @@ create table llx_bank_url
url_id integer,
url varchar(255),
label varchar(255),
- type enum("company","payment","member","subscription","donation","sc","payment_sc")
+ type enum("?","company","payment","payment_supplier","member","subscription","donation","sc","payment_sc") NOT NULL;
)type=innodb;