diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php
index 4a212792be0fdc8286c3052283fe867d94adfd6f..4f27a70a0a50a7edca5c0f8bb64469cc4e63186c 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])."<br>\n";
+              print $langs->trans('MigrationProcessPaymentUpdate', $row[$i][0])."<br>\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 '<tr><td colspan="4">';
+
+	print '<br>';
+	print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\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)."<br>\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'].'<br>';
+				// 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.'<br>';
+					$resql=$db->query($sql);
+					if ($resql)
+					{
+						$num = $db->num_rows($resql);
+						//print 'Nb of invoice found for this amount and company :'.$num.'<br>';
+						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'])."<br>\n";
+						}
+					}
+					else
+					{
+						print 'ERROR';
+					}
+				}
+			} 
+		}
+		
+		if ($res > 0)
+		{
+			print $langs->trans('MigrationSuccessfullUpdate')."<br>";
+		}
+		else
+		{
+			print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
+		}
+	}
+	else
+	{
+		print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
+	}
+
+	print '</td></tr>';
+}
+
+
 /*
  * 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 138d21f56611b13d4e83803b39ab705a86c1203c..a5e94bf344f64921b5050584e3cc108118eb88e9 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 1269c0d77ee75439ed3be18650e69a5948a0b6e8..f68b4782ff7d502b9d70e8151f71def8cda8ab5f 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 a5d38b931ad66b864fb91dea95da0be99df70687..e7cc5bdcae925c9d61df3eb3dc4541ec4cf08e0e 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;