From 7abc53698a4915e44f6cfc0d992e208abf94623f Mon Sep 17 00:00:00 2001
From: Maxime Kohlhaas <mko@atm-consulting.fr>
Date: Fri, 19 Jul 2013 08:54:11 +0200
Subject: [PATCH] Add RefExt field on RemiseCheque

---
 .../cheque/class/remisecheque.class.php       | 41 +++++++++++++++-
 htdocs/compta/paiement/cheque/fiche.php       | 49 +++++++++++++++++++
 .../install/mysql/migration/3.4.0-3.5.0.sql   |  3 ++
 .../mysql/tables/llx_bordereau_cheque.sql     |  3 +-
 htdocs/langs/en_US/compta.lang                |  1 +
 htdocs/langs/fr_FR/compta.lang                |  1 +
 6 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/htdocs/compta/paiement/cheque/class/remisecheque.class.php b/htdocs/compta/paiement/cheque/class/remisecheque.class.php
index 2d5eaebf8e9..2f7db2cc226 100644
--- a/htdocs/compta/paiement/cheque/class/remisecheque.class.php
+++ b/htdocs/compta/paiement/cheque/class/remisecheque.class.php
@@ -37,6 +37,7 @@ class RemiseCheque extends CommonObject
 	var $id;
 	var $num;
 	var $intitule;
+	var $ref_ext;
 	//! Numero d'erreur Plage 1024-1279
 	var $errno;
 
@@ -63,7 +64,7 @@ class RemiseCheque extends CommonObject
 	{
 		global $conf;
 
-		$sql = "SELECT bc.rowid, bc.datec, bc.fk_user_author, bc.fk_bank_account, bc.amount, bc.number, bc.statut, bc.nbcheque";
+		$sql = "SELECT bc.rowid, bc.datec, bc.fk_user_author, bc.fk_bank_account, bc.amount, bc.number, bc.statut, bc.nbcheque, bc.ref_ext";
 		$sql.= ", bc.date_bordereau as date_bordereau";
 		$sql.= ", ba.label as account_label";
 		$sql.= " FROM ".MAIN_DB_PREFIX."bordereau_cheque as bc";
@@ -86,6 +87,7 @@ class RemiseCheque extends CommonObject
 				$this->author_id      = $obj->fk_user_author;
 				$this->nbcheque       = $obj->nbcheque;
 				$this->statut         = $obj->statut;
+				$this->ref_ext        = $obj->ref_ext;
 
 				if ($this->statut == 0)
 				{
@@ -139,6 +141,7 @@ class RemiseCheque extends CommonObject
 		$sql.= ", number";
 		$sql.= ", entity";
 		$sql.= ", nbcheque";
+		$sql.= ", ref_ext";
 		$sql.= ") VALUES (";
 		$sql.= $this->db->idate($now);
 		$sql.= ", ".$this->db->idate($now);
@@ -149,6 +152,7 @@ class RemiseCheque extends CommonObject
 		$sql.= ", 0";
 		$sql.= ", ".$conf->entity;
 		$sql.= ", 0";
+		$sql.= ", ''";
 		$sql.= ")";
 
 		dol_syslog("RemiseCheque::Create sql=".$sql, LOG_DEBUG);
@@ -720,6 +724,41 @@ class RemiseCheque extends CommonObject
             return -2;
         }
     }
+	
+	/**
+     *      Set the external ref
+     *
+     *      @param	User		$user           Object user
+     *      @param  timestamp   $ref_rext       External ref
+     *      @return int                 		<0 if KO, >0 if OK
+     */
+    function set_ref_ext($user, $ref_ext)
+    {
+        if ($user->rights->banque->cheque)
+        {
+            $sql = "UPDATE ".MAIN_DB_PREFIX."bordereau_cheque";
+            $sql.= " SET ref_ext = '".$ref_ext."'";
+            $sql.= " WHERE rowid = ".$this->id;
+
+            dol_syslog("RemiseCheque::set_ref_ext sql=$sql",LOG_DEBUG);
+            $resql=$this->db->query($sql);
+            if ($resql)
+            {
+                $this->ref_ext = $ref_ext;
+                return 1;
+            }
+            else
+            {
+                $this->error=$this->db->error();
+                dol_syslog("RemiseCheque::set_ref_ext ".$this->error,LOG_ERR);
+                return -1;
+            }
+        }
+        else
+        {
+            return -2;
+        }
+    }
 
 
 	/**
diff --git a/htdocs/compta/paiement/cheque/fiche.php b/htdocs/compta/paiement/cheque/fiche.php
index 7cf5d15f3a2..ff20fe04fea 100644
--- a/htdocs/compta/paiement/cheque/fiche.php
+++ b/htdocs/compta/paiement/cheque/fiche.php
@@ -89,6 +89,29 @@ if ($action == 'setdate' && $user->rights->banque->cheque)
     }
 }
 
+/*
+ * Actions
+ */
+
+if ($action == 'setrefext' && $user->rights->banque->cheque)
+{
+    $result = $object->fetch(GETPOST('id','int'));
+    if ($result > 0)
+    {
+        $ref_ext = GETPOST('ref_ext');
+
+        $result=$object->set_ref_ext($user, $ref_ext);
+        if ($result < 0)
+        {
+            $mesg='<div class="error">'.$object->error.'</div>';
+        }
+    }
+    else
+    {
+        $mesg='<div class="error">'.$object->error.'</div>';
+    }
+}
+
 if ($action == 'create' && $_POST["accountid"] > 0 && $user->rights->banque->cheque)
 {
 	if (is_array($_POST['toRemise']))
@@ -491,6 +514,32 @@ else
 
 	print '</td>';
 	print '</tr>';
+	
+	// External ref
+	print '<tr><td>';
+	
+	print '<table class="nobordernopadding" width="100%"><tr><td>';
+    print $langs->trans('RefExt');
+    print '</td>';
+    if ($action != 'editrefext') print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editrefext&amp;id='.$object->id.'">'.img_edit($langs->trans('SetRefExt'),1).'</a></td>';
+    print '</tr></table>';
+    print '</td><td colspan="2">';
+    if ($action == 'editrefext')
+    {
+        print '<form name="setrefext" 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="setrefext">';
+        print '<input type="text" name="ref_ext" value="'.$object->ref_ext.'">';
+        print '<input type="submit" class="button" value="'.$langs->trans('Modify').'">';
+        print '</form>';
+    }
+    else
+    {
+        print $object->ref_ext;
+    }
+
+	print '</td>';
+	print '</tr>';
 
 	print '<tr><td>'.$langs->trans('Account').'</td><td colspan="2">';
 	print $accountstatic->getNomUrl(1);
diff --git a/htdocs/install/mysql/migration/3.4.0-3.5.0.sql b/htdocs/install/mysql/migration/3.4.0-3.5.0.sql
index 62811bdb70c..76137d75b7f 100755
--- a/htdocs/install/mysql/migration/3.4.0-3.5.0.sql
+++ b/htdocs/install/mysql/migration/3.4.0-3.5.0.sql
@@ -275,3 +275,6 @@ INSERT INTO llx_c_departements ( code_departement, fk_region, cheflieu, tncc, nc
 INSERT INTO llx_c_departements ( code_departement, fk_region, cheflieu, tncc, ncc, nom, active) VALUES('TN22', 1001, '', 0, '', 'Tozeur', 1);
 INSERT INTO llx_c_departements ( code_departement, fk_region, cheflieu, tncc, ncc, nom, active) VALUES('TN23', 1001, '', 0, '', 'Tunis', 1);
 INSERT INTO llx_c_departements ( code_departement, fk_region, cheflieu, tncc, ncc, nom, active) VALUES('TN24', 1001, '', 0, '', 'Zaghouan', 1);
+
+# Add ref_ext on bordereau_cheque
+ALTER TABLE `llx_bordereau_cheque` ADD `ref_ext` VARCHAR( 255 ) NOT NULL;
diff --git a/htdocs/install/mysql/tables/llx_bordereau_cheque.sql b/htdocs/install/mysql/tables/llx_bordereau_cheque.sql
index cc93cf188d6..60365665932 100644
--- a/htdocs/install/mysql/tables/llx_bordereau_cheque.sql
+++ b/htdocs/install/mysql/tables/llx_bordereau_cheque.sql
@@ -33,6 +33,7 @@ create table llx_bordereau_cheque
   fk_bank_account   integer,
   fk_user_author    integer,
   note              text,
-  statut            smallint NOT NULL DEFAULT 0
+  statut            smallint NOT NULL DEFAULT 0,
+  ref_ext	    varchar(255)
   
 )ENGINE=innodb;
diff --git a/htdocs/langs/en_US/compta.lang b/htdocs/langs/en_US/compta.lang
index cf95e809e4f..c2a27fe8b7d 100644
--- a/htdocs/langs/en_US/compta.lang
+++ b/htdocs/langs/en_US/compta.lang
@@ -154,3 +154,4 @@ InvoiceLinesToDispatch=Invoice lines to dispatch
 InvoiceDispatched=Dispatched invoices
 AccountancyDashboard=Accountancy summary
 ByProductsAndServices=By products and services
+RefExt=External ref
\ No newline at end of file
diff --git a/htdocs/langs/fr_FR/compta.lang b/htdocs/langs/fr_FR/compta.lang
index a86676b36b9..0b9cf8b7dda 100644
--- a/htdocs/langs/fr_FR/compta.lang
+++ b/htdocs/langs/fr_FR/compta.lang
@@ -165,3 +165,4 @@ InvoiceLinesToDispatch=Lignes de factures à ventiler
 InvoiceDispatched=Factures ventilées
 AccountancyDashboard=Synthèse compta/tréso
 ByProductsAndServices=Par produits et services
+RefExt=Référence externe
-- 
GitLab