From 4215ec3ca9655d8dd6597e19ce473f51ca857cb6 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sun, 28 Aug 2016 20:12:25 +0200
Subject: [PATCH] FIX usage of sepa generation setup.

---
 htdocs/admin/prelevement.php                  |  3 +-
 .../class/bonprelevement.class.php            | 52 ++++++++++++++-----
 scripts/withdrawals/build_withdrawal_file.php |  1 +
 3 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/htdocs/admin/prelevement.php b/htdocs/admin/prelevement.php
index 1a021996163..0e82681c686 100644
--- a/htdocs/admin/prelevement.php
+++ b/htdocs/admin/prelevement.php
@@ -48,11 +48,11 @@ if ($action == "set")
 
     $id=GETPOST('PRELEVEMENT_ID_BANKACCOUNT','int');
     $account = new Account($db);
-
     if($account->fetch($id)>0)
     {
         $res = dolibarr_set_const($db, "PRELEVEMENT_ID_BANKACCOUNT", $id,'chaine',0,'',$conf->entity);
         if (! $res > 0) $error++;
+        /*
         $res = dolibarr_set_const($db, "PRELEVEMENT_CODE_BANQUE", $account->code_banque,'chaine',0,'',$conf->entity);
         if (! $res > 0) $error++;
         $res = dolibarr_set_const($db, "PRELEVEMENT_CODE_GUICHET", $account->code_guichet,'chaine',0,'',$conf->entity);
@@ -67,6 +67,7 @@ if ($action == "set")
         if (! $res > 0) $error++;
         $res = dolibarr_set_const($db, "PRELEVEMENT_RAISON_SOCIALE", $account->proprio,'chaine',0,'',$conf->entity);
         if (! $res > 0) $error++;
+        */
     }
     else $error++;
 
diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php
index 800cd8492c9..a49b5f5f7c6 100644
--- a/htdocs/compta/prelevement/class/bonprelevement.class.php
+++ b/htdocs/compta/prelevement/class/bonprelevement.class.php
@@ -27,6 +27,7 @@
 
 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
+require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
 
@@ -1015,15 +1016,21 @@ class BonPrelevement extends CommonObject
                     $this->date_echeance = $datetimeprev;
                     $this->reference_remise = $ref;
 
-                    $this->raison_sociale              = $conf->global->PRELEVEMENT_RAISON_SOCIALE;
-
-                    $this->emetteur_code_banque 		  = $conf->global->PRELEVEMENT_CODE_BANQUE;
-                    $this->emetteur_code_guichet       = $conf->global->PRELEVEMENT_CODE_GUICHET;
-                    $this->emetteur_numero_compte      = $conf->global->PRELEVEMENT_NUMERO_COMPTE;
-                    $this->emetteur_number_key		  = $conf->global->PRELEVEMENT_NUMBER_KEY;
-                    $this->emetteur_iban               = $conf->global->PRELEVEMENT_IBAN;
-                    $this->emetteur_bic                = $conf->global->PRELEVEMENT_BIC;
-                    $this->emetteur_ics                = $conf->global->PRELEVEMENT_ICS;		// Ex: PRELEVEMENT_ICS = "FR78ZZZ123456";
+                    $id=$conf->global->PRELEVEMENT_ID_BANKACCOUNT;
+                    $account = new Account($this->db);
+                    if ($account->fetch($id)>0)
+                    {
+                        $this->emetteur_code_banque 	   = $account->code_banque;
+                        $this->emetteur_code_guichet       = $account->code_guichet;
+                        $this->emetteur_numero_compte      = $account->number;
+                        $this->emetteur_number_key		   = $account->cle_rib;
+                        $this->emetteur_iban               = $account->iban;
+                        $this->emetteur_bic                = $account->bic;
+
+                        $this->emetteur_ics                = $conf->global->PRELEVEMENT_ICS;		// Ex: PRELEVEMENT_ICS = "FR78ZZZ123456";
+    
+                        $this->raison_sociale              = $account->proprio;
+                    }
 
                     $this->factures = $factures_prev_id;
 
@@ -1680,6 +1687,23 @@ class BonPrelevement extends CommonObject
 		$dateTime_ETAD = dol_print_date($ladate, '%Y-%m-%d');
 		$dateTime_YMDHMS = dol_print_date($ladate, '%Y-%m-%dT%H:%M:%S');
 
+		// Get data of bank account
+		$id=$configuration->global->PRELEVEMENT_ID_BANKACCOUNT;
+		$account = new Account($this->db);
+		if ($account->fetch($id)>0)
+		{
+		    $this->emetteur_code_banque 	   = $account->code_banque;
+		    $this->emetteur_code_guichet       = $account->code_guichet;
+		    $this->emetteur_numero_compte      = $account->number;
+		    $this->emetteur_number_key		   = $account->cle_rib;
+		    $this->emetteur_iban               = $account->iban;
+		    $this->emetteur_bic                = $account->bic;
+		
+		    $this->emetteur_ics                = $conf->global->PRELEVEMENT_ICS;		// Ex: PRELEVEMENT_ICS = "FR78ZZZ123456";
+		
+		    $this->raison_sociale              = $account->proprio;
+		}
+		
 		// Récupération info demandeur
 		$sql = "SELECT rowid, ref";
 		$sql.= " FROM";
@@ -1714,7 +1738,7 @@ class BonPrelevement extends CommonObject
 			$XML_SEPA_INFO .= '			</PmtTpInf>'.$CrLf;
 			$XML_SEPA_INFO .= '			<ReqdColltnDt>'.$dateTime_ETAD.'</ReqdColltnDt>'.$CrLf;
 			$XML_SEPA_INFO .= '			<Cdtr>'.$CrLf;
-			$XML_SEPA_INFO .= '				<Nm>'.strtoupper(dol_string_unaccent($configuration->global->PRELEVEMENT_RAISON_SOCIALE)).'</Nm>'.$CrLf;
+			$XML_SEPA_INFO .= '				<Nm>'.strtoupper(dol_string_unaccent($this->raison_sociale)).'</Nm>'.$CrLf;
 			$XML_SEPA_INFO .= '				<PstlAdr>'.$CrLf;
 			$XML_SEPA_INFO .= '					<Ctry>'.$country[1].'</Ctry>'.$CrLf;
 			$XML_SEPA_INFO .= '					<AdrLine>'.strtoupper(dol_string_unaccent($configuration->global->MAIN_INFO_SOCIETE_ADDRESS)).'</AdrLine>'.$CrLf;
@@ -1723,16 +1747,16 @@ class BonPrelevement extends CommonObject
 			$XML_SEPA_INFO .= '			</Cdtr>'.$CrLf;
 			$XML_SEPA_INFO .= '			<CdtrAcct>'.$CrLf;
 			$XML_SEPA_INFO .= '				<Id>'.$CrLf;
-			$XML_SEPA_INFO .= '					<IBAN>'.preg_replace('/\s/', '', $configuration->global->PRELEVEMENT_IBAN).'</IBAN>'.$CrLf;
+			$XML_SEPA_INFO .= '					<IBAN>'.preg_replace('/\s/', '', $this->emetteur_iban).'</IBAN>'.$CrLf;
 			$XML_SEPA_INFO .= '				</Id>'.$CrLf;
 			$XML_SEPA_INFO .= '			</CdtrAcct>'.$CrLf;
 			$XML_SEPA_INFO .= '			<CdtrAgt>'.$CrLf;
 			$XML_SEPA_INFO .= '				<FinInstnId>'.$CrLf;
-			$XML_SEPA_INFO .= '					<BIC>'.$configuration->global->PRELEVEMENT_BIC.'</BIC>'.$CrLf;
+			$XML_SEPA_INFO .= '					<BIC>'.$this->emetteur_bic.'</BIC>'.$CrLf;
 			$XML_SEPA_INFO .= '				</FinInstnId>'.$CrLf;
 			$XML_SEPA_INFO .= '			</CdtrAgt>'.$CrLf;
 /*			$XML_SEPA_INFO .= '			<UltmtCdtr>'.$CrLf;
-			$XML_SEPA_INFO .= '				<Nm>'.$configuration->global->PRELEVEMENT_RAISON_SOCIALE.'</Nm>'.$CrLf;
+			$XML_SEPA_INFO .= '				<Nm>'.$this->raison_sociale.'</Nm>'.$CrLf;
 			$XML_SEPA_INFO .= '				<PstlAdr>'.$CrLf;
 			$XML_SEPA_INFO .= '					<Ctry>'.$country[1].'</Ctry>'.$CrLf;
 			$XML_SEPA_INFO .= '					<AdrLine>'.$conf->global->MAIN_INFO_SOCIETE_ADDRESS.'</AdrLine>'.$CrLf;
@@ -1744,7 +1768,7 @@ class BonPrelevement extends CommonObject
 			$XML_SEPA_INFO .= '				<Id>'.$CrLf;
 			$XML_SEPA_INFO .= '					<PrvtId>'.$CrLf;
 			$XML_SEPA_INFO .= '						<Othr>'.$CrLf;
-			$XML_SEPA_INFO .= '							<Id>'.$configuration->global->PRELEVEMENT_ICS.'</Id>'.$CrLf;
+			$XML_SEPA_INFO .= '							<Id>'.$this->emetteur_ics.'</Id>'.$CrLf;
 			$XML_SEPA_INFO .= '							<SchmeNm>'.$CrLf;
 			$XML_SEPA_INFO .= '								<Prtry>SEPA</Prtry>'.$CrLf;
 			$XML_SEPA_INFO .= '							</SchmeNm>'.$CrLf;
diff --git a/scripts/withdrawals/build_withdrawal_file.php b/scripts/withdrawals/build_withdrawal_file.php
index 1a8c673c5f9..d381c32676a 100755
--- a/scripts/withdrawals/build_withdrawal_file.php
+++ b/scripts/withdrawals/build_withdrawal_file.php
@@ -70,6 +70,7 @@ if (! isset($argv[1])) {	// Check parameters
 
 
 $withdrawreceipt=new BonPrelevement($db);
+// $conf->global->PRELEVEMENT_CODE_BANQUE and $conf->global->PRELEVEMENT_CODE_GUICHET should be empty
 $result=$withdrawreceipt->create($conf->global->PRELEVEMENT_CODE_BANQUE,$conf->global->PRELEVEMENT_CODE_GUICHET,$argv[1]);
 
 
-- 
GitLab