From 9321272838e91f8d1450b082ba050ee37b05e94f Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 4 Jul 2015 04:34:14 +0200
Subject: [PATCH] Uniformize code

---
 htdocs/compta/paiement/cheque/card.php        | 27 +++++++------------
 .../cheque/class/remisecheque.class.php       | 27 ++++++++++---------
 .../compta/paiement/class/paiement.class.php  | 13 +++++----
 3 files changed, 32 insertions(+), 35 deletions(-)

diff --git a/htdocs/compta/paiement/cheque/card.php b/htdocs/compta/paiement/cheque/card.php
index c07b275aaef..bafa20aa84f 100644
--- a/htdocs/compta/paiement/cheque/card.php
+++ b/htdocs/compta/paiement/cheque/card.php
@@ -229,7 +229,7 @@ if ($action == 'confirm_reject_check' && $confirm == 'yes' && $user->rights->ban
 {
 	$reject_date = dol_mktime(0, 0, 0, GETPOST('rejectdate_month'), GETPOST('rejectdate_day'), GETPOST('rejectdate_year'));
 	$rejected_check = GETPOST('bankid');
-	
+
 	$object->fetch($id);
 	$paiement_id = $object->reject_check($rejected_check, $reject_date);
 	if ($paiement_id > 0)
@@ -353,7 +353,7 @@ else
 		print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans("ValidateCheckReceipt"), $langs->trans("ConfirmValidateCheckReceipt"), 'confirm_valide','','',1);
 
 	}
-	
+
 	/*
 	 * Confirm check rejection
 	 */
@@ -381,7 +381,7 @@ if ($action == 'new')
 	print '<input type="hidden" name="action" value="new">';
 
 	dol_fiche_head();
-	
+
 	print '<table class="border" width="100%">';
 	//print '<tr><td width="30%">'.$langs->trans('Date').'</td><td width="70%">'.dol_print_date($now,'day').'</td></tr>';
 	// Filter
@@ -392,9 +392,9 @@ if ($action == 'new')
     $form->select_comptes($filteraccountid,'accountid',0,'courant <> 2',1);
     print '</td></tr>';
 	print '</table>';
-	
+
 	dol_fiche_end();
-	
+
     print '<div class="center">';
 	print '<input type="submit" class="button" name="filter" value="'.dol_escape_htmltag($langs->trans("ToFilter")).'">';
     if ($filterdate || $filteraccountid > 0)
@@ -655,8 +655,7 @@ else
 		print_liste_field_titre($langs->trans("Amount"),$_SERVER["PHP_SELF"],"b.amount", "",$param,'align="right"',$sortfield,$sortorder);
 		print_liste_field_titre($langs->trans("LineRecord"),$_SERVER["PHP_SELF"],"b.rowid", "",$param,'align="center"',$sortfield,$sortorder);
 		print_liste_field_titre($langs->trans("Payment"),$_SERVER["PHP_SELF"],"p.rowid", "",$param,'align="center"',$sortfield,$sortorder);
-		print_liste_field_titre('');
-
+		print_liste_field_titre('',$_SERVER["PHP_SELF"],"",'','','',$sortfield,$sortorder,'maxwidthsearch ');
 		print "</tr>\n";
 		$i=1;
 		$var=false;
@@ -699,17 +698,11 @@ else
 			print '</td>';
 			if ($object->statut == 0)
 			{
-				print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=remove&amp;lineid='.$objp->rowid.'">'.img_delete().'</a></td>';
+				print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=remove&amp;lineid='.$objp->rowid.'">'.img_delete().'</a>';
+				print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=reject_check&amp;lineid='.$objp->rowid.'">'.img_picto($langs->trans("RejectCheck"),'disable').'</a></td>';
 			}
-			else
-			{
-				if($objp->statut == 0)
-				{
-					print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=reject_check&amp;lineid='.$objp->rowid.'">'.img_picto($langs->trans("RejectCheck"),'disable').'</a></td>';
-				}
-				else if($objp->statut == 2) {
-					print '<td align="right">'.img_picto($langs->trans('CheckRejected'),'statut8').'</a></td>';
-				}
+			else if($objp->statut == 2) {
+				print '<td align="right">'.img_picto($langs->trans('CheckRejected'),'statut8').'</a></td>';
 			}
 			print '</tr>';
 			$var=!$var;
diff --git a/htdocs/compta/paiement/cheque/class/remisecheque.class.php b/htdocs/compta/paiement/cheque/class/remisecheque.class.php
index 82047342d35..20923222741 100644
--- a/htdocs/compta/paiement/cheque/class/remisecheque.class.php
+++ b/htdocs/compta/paiement/cheque/class/remisecheque.class.php
@@ -659,7 +659,7 @@ class RemiseCheque extends CommonObject
 		}
 		return 0;
 	}
-	
+
 	/**
 	 *	Check rejection management
 	 *	Reopen linked invoices and saves a new negative payment
@@ -671,34 +671,35 @@ class RemiseCheque extends CommonObject
 	function reject_check($bank_id, $rejection_date)
 	{
 		global $db, $user;
-		
+
 		$payment = new Paiement($db);
-		$payment->fetch(0,$bank_id);
-		
+		$payment->fetch(0,0,$bank_id);
+
 		$bankaccount = $payment->fk_account;
-		
+
 		// Get invoice list to reopen them
 		$sql = 'SELECT pf.fk_facture, pf.amount';
 		$sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf';
 		$sql.= ' WHERE pf.fk_paiement = '.$payment->id;
-		
+
 		$resql=$db->query($sql);
 		if ($resql)
 		{
 			$rejectedPayment = new Paiement($db);
 			$rejectedPayment->amounts = array();
 			$rejectedPayment->datepaye = $rejection_date;
-			$rejectedPayment->paiementid = 7; // type of payment: check
+			$rejectedPayment->paiementid = dol_getIdFromCode($this->db, 'CHQ', 'c_paiement');
 			$rejectedPayment->num_paiement = $payment->numero;
-			
-			while($obj = $db->fetch_object($resql)) {
+
+			while($obj = $db->fetch_object($resql))
+			{
 				$invoice = new Facture($db);
 				$invoice->fetch($obj->fk_facture);
 				$invoice->set_unpaid($user);
-				
+
 				$rejectedPayment->amounts[$obj->fk_facture] = price2num($obj->amount) * -1;
 			}
-			
+
 			if ($rejectedPayment->create($user) > 0)
 			{
 				$result=$rejectedPayment->addPaymentToBank($user,'payment','(CheckRejected)',$bankaccount,'','');
@@ -723,7 +724,7 @@ class RemiseCheque extends CommonObject
 			return -1;
 		}
 	}
-	
+
 	/**
 	 *	Charge les proprietes ref_previous et ref_next
 	 *
@@ -813,7 +814,7 @@ class RemiseCheque extends CommonObject
 			$sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
 			$sql.= " SET number = '".$number."'" ;
 			$sql.= " WHERE rowid = ".$this->id;
-			
+
 			dol_syslog("RemiseCheque::set_number", LOG_DEBUG);
 			$resql=$this->db->query($sql);
 			if ($resql)
diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php
index dc4be17aa07..e45d0936bcc 100644
--- a/htdocs/compta/paiement/class/paiement.class.php
+++ b/htdocs/compta/paiement/class/paiement.class.php
@@ -79,10 +79,11 @@ class Paiement extends CommonObject
 	 *    Load payment from database
 	 *
 	 *    @param	int		$id			Id of payment to get
+	 *    @param	string	$ref		Ref of payment to get (currently ref = id but this may change in future)
 	 *    @param	int		$fk_bank	Id of bank line associated to payment
 	 *    @return   int		<0 if KO, 0 if not found, >0 if OK
 	 */
-	function fetch($id, $fk_bank='')
+	function fetch($id, $ref='', $fk_bank='')
 	{
 		$sql = 'SELECT p.rowid, p.datep as dp, p.amount, p.statut, p.fk_bank,';
 		$sql.= ' c.code as type_code, c.libelle as type_libelle,';
@@ -91,10 +92,12 @@ class Paiement extends CommonObject
 		$sql.= ' FROM '.MAIN_DB_PREFIX.'c_paiement as c, '.MAIN_DB_PREFIX.'paiement as p';
 		$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid ';
 		$sql.= ' WHERE p.fk_paiement = c.id';
-		if ($fk_bank)
-			$sql.= ' AND p.fk_bank = '.$fk_bank;
-		else
+		if ($id > 0)
 			$sql.= ' AND p.rowid = '.$id;
+		else if ($ref)
+			$sql.= ' AND p.rowid = '.$ref;
+		else if ($fk_bank)
+			$sql.= ' AND p.fk_bank = '.$fk_bank;
 
 		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
 		$result = $this->db->query($sql);
@@ -670,7 +673,7 @@ class Paiement extends CommonObject
 			return -1;
 		}
 	}
-	
+
 	/**
 	 *    Reject payment
 	 *
-- 
GitLab