From 081d0e7ec25a17f89d60cf95504833bb75fafa3b Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 9 May 2017 11:09:21 +0200
Subject: [PATCH] Debug module RH. Edition of user bank account is ok.

---
 htdocs/core/lib/usergroups.lib.php          |  2 +-
 htdocs/user/bank.php                        | 41 +++++++++++++--------
 htdocs/user/class/userbankaccount.class.php | 10 +++--
 3 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/htdocs/core/lib/usergroups.lib.php b/htdocs/core/lib/usergroups.lib.php
index 3b3cde1d66a..d983f799c0d 100644
--- a/htdocs/core/lib/usergroups.lib.php
+++ b/htdocs/core/lib/usergroups.lib.php
@@ -145,7 +145,7 @@ function user_prepare_head($object)
     {
 		// Bank
     	$head[$h][0] = DOL_URL_ROOT.'/user/bank.php?id='.$object->id;
-    	$head[$h][1] = $langs->trans("Bank");
+    	$head[$h][1] = $langs->trans("BankAccounts");
     	$head[$h][2] = 'bank';
     	$h++;
 	}
diff --git a/htdocs/user/bank.php b/htdocs/user/bank.php
index 9f36bc91e31..c86ca27bf34 100644
--- a/htdocs/user/bank.php
+++ b/htdocs/user/bank.php
@@ -38,7 +38,9 @@ $langs->load("banks");
 $langs->load("bills");
 
 $id = GETPOST('id','int');
-$action = GETPOST("action");
+$bankid = GETPOST('bankid','int');
+$action = GETPOST("action",'alpha');
+$cancel = GETPOST('cancel','alpha');
 
 // Security check
 $socid=0;
@@ -54,16 +56,17 @@ if ($id > 0 || ! empty($ref))
 	$object->getrights();
 }
 
+
 /*
  *	Actions
  */
 
-if ($action == 'update' && ! $_POST["cancel"])
+if ($action == 'update' && ! $cancel)
 {
 	// Modification
 	$account = new UserBankAccount($db);
 
-    $account->fetch($id);
+    $account->fetch($bankid);
 
     $account->userid          = $object->id;
 
@@ -82,54 +85,62 @@ if ($action == 'update' && ! $_POST["cancel"])
 	$account->owner_address   = $_POST["owner_address"];
 
 	$result = $account->update($user);
-	if (! $result)
+
+    if (! $result)
 	{
 		setEventMessages($account->error, $account->errors, 'errors');
-		$_GET["action"]='edit';     // Force chargement page edition
+		$action='edit';     // Force chargement page edition
 	}
 	else
 	{
-		$url=DOL_URL_ROOT.'/user/bank.php?id='.$object->id;
+		$url=DOL_URL_ROOT.'/user/bank.php?id='.$object->id.'&bankid='.$bankid;
         header('Location: '.$url);
         exit;
 	}
 }
 
+
 /*
  *	View
  */
 
 $form = new Form($db);
 
-llxHeader();
+llxHeader(null, $langs->trans("BankAccounts"));
 
 $head = user_prepare_head($object);
 
 $account = new UserBankAccount($db);
-if (! $id)
-    $account->fetch(0,$object->id);
+if (! $bankid)
+{
+    $account->fetch(0, '', $id);
+}
 else
-    $account->fetch($id);
+{
+    $account->fetch($bankid);
+}
 if (empty($account->userid)) $account->userid=$object->id;
 
 
-if ($id && $action == 'edit' && $user->rights->user->user->creer)
+if ($bankid && $action == 'edit' && $user->rights->user->user->creer)
 {
     print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="post">';
     print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
     print '<input type="hidden" name="action" value="update">';
     print '<input type="hidden" name="id" value="'.GETPOST("id",'int').'">';
+    print '<input type="hidden" name="bankid" value="'.$bankid.'">';
 }
-if ($id && $action == 'create' && $user->rights->user->user->creer)
+if ($bankid && $action == 'create' && $user->rights->user->user->creer)
 {
     print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="post">';
     print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
     print '<input type="hidden" name="action" value="add">';
+    print '<input type="hidden" name="bankid" value="'.$bankid.'">';
 }
 
 
 // View
-if ($id && $action != 'edit')
+if ($account->id && $action != 'edit')
 {
 	$title = $langs->trans("User");
 	dol_fiche_head($head, 'bank', $title, -1, 'user');
@@ -223,7 +234,7 @@ if ($id && $action != 'edit')
 
 	if ($user->rights->user->user->creer)
 	{
-		print '<a class="butAction" href="bank.php?id='.$object->id.'&amp;action=edit">'.$langs->trans("Edit").'</a>';
+		print '<a class="butAction" href="bank.php?id='.$object->id.'&bankid='.$account->id.'&action=edit">'.$langs->trans("Edit").'</a>';
 	}
 
 	print '</div>';
@@ -309,8 +320,6 @@ if ($id && $action == 'edit' && $user->rights->user->user->creer)
     print '</div>';
 }
 
-if ($id && $action == 'edit' && $user->rights->user->user->creer) print '</form>';
-
 if ($id && $action == 'edit' && $user->rights->user->user->creer) print '</form>';
     
 llxFooter();
diff --git a/htdocs/user/class/userbankaccount.class.php b/htdocs/user/class/userbankaccount.class.php
index 514e8d0b28c..a7aba35fc64 100644
--- a/htdocs/user/class/userbankaccount.class.php
+++ b/htdocs/user/class/userbankaccount.class.php
@@ -132,16 +132,20 @@ class UserBankAccount extends Account
      * 	Load record from database
      *
      *	@param	int		$id			Id of record
+     *	@param	string	$ref		Ref of record
+     *  @param  int     $userid     User id
      * 	@return	int					<0 if KO, >0 if OK
      */
-    function fetch($id)
+    function fetch($id, $ref='', $userid=0)
     {
-        if (empty($id)) return -1;
+        if (empty($id) && empty($ref) && empty($userid)) return -1;
 
         $sql = "SELECT rowid, fk_user, entity, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
         $sql.= " owner_address, label, datec, tms as datem";
         $sql.= " FROM ".MAIN_DB_PREFIX."user_rib";
-        $sql.= " WHERE rowid = ".$id;
+        if ($id) $sql.= " WHERE rowid = ".$id;
+        if ($ref) $sql.= " WHERE label = '".$this->db->escape($ref)."'";
+        if ($userid) $sql.= " WHERE fk_user = '".$userid."'";
 
         $resql = $this->db->query($sql);
         if ($resql)
-- 
GitLab