From fdfb7f1a8aa0fae3ceb75b73b08a5c163cf9914d Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 8 Feb 2014 03:24:48 +0100
Subject: [PATCH] Fix: Be sure there is no duplicate default rib.

---
 ChangeLog                                     |  3 +-
 .../class/bonprelevement.class.php            | 30 +++++++++----------
 htdocs/compta/prelevement/create.php          |  2 +-
 htdocs/langs/fr_FR/withdrawals.lang           |  2 +-
 htdocs/societe/rib.php                        |  8 ++++-
 5 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index afde59ec7f1..8d28705d569 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -19,7 +19,8 @@ Fix: Page load not ending when large number of thirdparies. We
      added option MAIN_DISABLE_AJAX_COMBOX to disable javascript
      combo feature that is root cause of problem.
 Fix: [ bug #1231 ] PDF always generated in interventions
-     
+Fix: Be sure there is no duplicate default rib.
+
 ***** ChangeLog for 3.5 compared to 3.4.* *****
 For users:
 - New: Add hidden option BANK_DISABLE_DIRECT_INPUT.
diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php
index a4606d669c2..018bbdb9d40 100644
--- a/htdocs/compta/prelevement/class/bonprelevement.class.php
+++ b/htdocs/compta/prelevement/class/bonprelevement.class.php
@@ -683,16 +683,16 @@ class BonPrelevement extends CommonObject
         $sql = "SELECT count(f.rowid)";
         $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
         $sql.= ", ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
-        if ($banque == 1 || $agence == 1) $sql.=", ".MAIN_DB_PREFIX."societe_rib as sr";
+        if ($banque || $agence) $sql.=", ".MAIN_DB_PREFIX."societe_rib as sr";
         $sql.= " WHERE f.fk_statut = 1";
         $sql.= " AND f.entity = ".$conf->entity;
         $sql.= " AND f.rowid = pfd.fk_facture";
         $sql.= " AND f.paye = 0";
         $sql.= " AND pfd.traite = 0";
         $sql.= " AND f.total_ttc > 0";
-        if ($banque == 1 || $agence == 1) $sql.= " AND f.fk_soc = sr.rowid";
-        if ($banque == 1) $sql.= " AND sr.code_banque = '".$conf->global->PRELEVEMENT_CODE_BANQUE."'";
-        if ($agence == 1) $sql.= " AND sr.code_guichet = '".$conf->global->PRELEVEMENT_CODE_GUICHET."'";
+        if ($banque || $agence) $sql.= " AND f.fk_soc = sr.rowid";
+        if ($banque) $sql.= " AND sr.code_banque = '".$conf->global->PRELEVEMENT_CODE_BANQUE."'";
+        if ($agence) $sql.= " AND sr.code_guichet = '".$conf->global->PRELEVEMENT_CODE_GUICHET."'";
 
         $resql = $this->db->query($sql);
 
@@ -716,8 +716,8 @@ class BonPrelevement extends CommonObject
     /**
      *	Create a withdraw
      *
-     *	@param 	int		$banque		code of bank
-     *	@param	int		$agence		code of bank office (guichet)
+     *	@param 	int		$banque		code of bank (to withdraw a specific bankof a specific customer. By default '')
+     *	@param	int		$agence		code of bank office (guichet) (to withdraw a specific bankof a specific customer. By default '')
      *	@param	string	$mode		real=do action, simu=test only
      *	@return	int					<0 if KO, nbre of invoice withdrawed if OK
      */
@@ -755,17 +755,17 @@ class BonPrelevement extends CommonObject
             $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
             $sql.= ", ".MAIN_DB_PREFIX."societe as s";
             $sql.= ", ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
-            if ($banque == 1 || $agence ==1) $sql.= ", ".MAIN_DB_PREFIX."societe_rib as sr";
+            if ($banque || $agence) $sql.= ", ".MAIN_DB_PREFIX."societe_rib as sr";
             $sql.= " WHERE f.rowid = pfd.fk_facture";
             $sql.= " AND f.entity = ".$conf->entity;
             $sql.= " AND s.rowid = f.fk_soc";
-            if ($banque == 1 || $agence ==1) $sql.= " AND s.rowid = sr.fk_soc";
+            if ($banque || $agence) $sql.= " AND s.rowid = sr.fk_soc";
             $sql.= " AND f.fk_statut = 1";
             $sql.= " AND f.paye = 0";
             $sql.= " AND pfd.traite = 0";
             $sql.= " AND f.total_ttc > 0";
-            if ($banque == 1) $sql.= " AND sr.code_banque = '".$conf->global->PRELEVEMENT_CODE_BANQUE."'";
-            if ($agence == 1) $sql.= " AND sr.code_guichet = '".$conf->global->PRELEVEMENT_CODE_GUICHET."'";
+            if ($banque) $sql.= " AND sr.code_banque = '".$conf->global->PRELEVEMENT_CODE_BANQUE."'";
+            if ($agence) $sql.= " AND sr.code_guichet = '".$conf->global->PRELEVEMENT_CODE_GUICHET."'";
 
             dol_syslog(get_class($this)."::Create sql=".$sql, LOG_DEBUG);
             $resql = $this->db->query($sql);
@@ -777,7 +777,7 @@ class BonPrelevement extends CommonObject
                 while ($i < $num)
                 {
                     $row = $this->db->fetch_row($resql);
-                    $factures[$i] = $row;
+                    $factures[$i] = $row;	// All fields
                     $i++;
                 }
                 $this->db->free($resql);
@@ -802,10 +802,10 @@ class BonPrelevement extends CommonObject
 
             if (count($factures) > 0)
             {
-                foreach ($factures as $fac)
+                foreach ($factures as $key => $fac)
                 {
                     $fact = new Facture($this->db);
-                    if ($fact->fetch($fac[0]) >= 0)
+                    if ($fact->fetch($fac[0]) >= 0)		// Field 0 of $fac is rowid of invoice
                     {
                         if ($soc->fetch($fact->socid) >= 0)
                         {
@@ -820,8 +820,8 @@ class BonPrelevement extends CommonObject
                             }
                             else
 							{
-                                dol_syslog("Error on third party bank number RIB/IBAN ".$fact->socid." ".$soc->nom, LOG_ERR);
-                                $facture_errors[$fac[0]]="Error on third party bank number RIB/IBAN ".$fact->socid." ".$soc->nom;
+								dol_syslog("Error on default bank number RIB/IBAN for thirdparty reported by verif() ".$fact->socid." ".$soc->nom, LOG_ERR);
+                                $facture_errors[$fac[0]]="Error on default bank number RIB/IBAN for thirdparty reported by function verif() ".$fact->socid." ".$soc->nom;
                             }
                         }
                         else
diff --git a/htdocs/compta/prelevement/create.php b/htdocs/compta/prelevement/create.php
index 115902db228..81bb6794736 100644
--- a/htdocs/compta/prelevement/create.php
+++ b/htdocs/compta/prelevement/create.php
@@ -58,9 +58,9 @@ if ($action == 'modify')
         dolibarr_set_const($db, GETPOST("nom$i"), GETPOST("value$i"),'chaine',0,'',$conf->entity);
     }
 }
-
 if ($action == 'create')
 {
+	// $conf->global->PRELEVEMENT_CODE_BANQUE and $conf->global->PRELEVEMENT_CODE_GUICHET should be empty
     $bprev = new BonPrelevement($db);
     $result=$bprev->create($conf->global->PRELEVEMENT_CODE_BANQUE, $conf->global->PRELEVEMENT_CODE_GUICHET);
     if ($result < 0)
diff --git a/htdocs/langs/fr_FR/withdrawals.lang b/htdocs/langs/fr_FR/withdrawals.lang
index 538dafdd397..5cfef386e88 100644
--- a/htdocs/langs/fr_FR/withdrawals.lang
+++ b/htdocs/langs/fr_FR/withdrawals.lang
@@ -32,7 +32,7 @@ LastWithdrawalReceipt=Les %s derniers bons de prélèvements
 MakeWithdrawRequest=Faire une demande de prélèvement
 ThirdPartyBankCode=Code banque du tiers
 ThirdPartyDeskCode=Code guichet du tiers
-NoInvoiceCouldBeWithdrawed=Aucune facture percevable, prélevée avec succès. Vérifiez que les factures sont sur des sociétés dont le RIB est correctement renseigné.
+NoInvoiceCouldBeWithdrawed=Aucune facture percevable, prélevée avec succès. Vérifiez que les factures sont sur des sociétés dont le compte bancaire par défaut est correctement renseigné.
 ClassCredited=Classer crédité
 ClassCreditedConfirm=Êtes-vous sûr de vouloir classer ce bon de prélèvement comme crédité sur votre compte bancaire ?
 TransData=Date de transmission
diff --git a/htdocs/societe/rib.php b/htdocs/societe/rib.php
index 53a148cffc8..0d99b1f8a35 100644
--- a/htdocs/societe/rib.php
+++ b/htdocs/societe/rib.php
@@ -60,7 +60,7 @@ if ($action == 'update' && ! $_POST["cancel"])
 
     $account->fetch($id);
 
-	$account->socid           = $soc->id;
+    $account->socid           = $soc->id;
 
 	$account->bank            = $_POST["bank"];
 	$account->label           = $_POST["label"];
@@ -84,6 +84,12 @@ if ($action == 'update' && ! $_POST["cancel"])
 	}
 	else
 	{
+		// If this account is the default bank account, we disable others
+		if ($account->default_rib)
+		{
+			$account->setAsDefault($id);	// This will make sure there is only one default rib
+		}
+
 		$url=DOL_URL_ROOT.'/societe/rib.php?socid='.$soc->id;
         header('Location: '.$url);
         exit;
-- 
GitLab