From 2f424d2edba916230d460de62933e415524e87d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Doursenaud?= <rdoursenaud@gpcsolutions.fr>
Date: Sat, 19 Nov 2016 19:38:06 +0100
Subject: [PATCH] FIX #5325 contract service triggers

Pass ContratLigne objects to the triggers when updating contract
lines
---
 htdocs/contrat/class/contrat.class.php | 175 ++++++++++++++++---------
 1 file changed, 112 insertions(+), 63 deletions(-)

diff --git a/htdocs/contrat/class/contrat.class.php b/htdocs/contrat/class/contrat.class.php
index 550d5241431..75100aae9e6 100644
--- a/htdocs/contrat/class/contrat.class.php
+++ b/htdocs/contrat/class/contrat.class.php
@@ -141,6 +141,12 @@ class Contrat extends CommonObject
 	 */
 	var $lines=array();
 
+	/**
+	 * Maps ContratLigne IDs to $this->lines indexes
+	 * @var int[]
+	 */
+	protected $lines_id_index_mapper=array();
+
 
 	/**
 	 *	Constructor
@@ -221,38 +227,7 @@ class Contrat extends CommonObject
 	 */
 	function active_line($user, $line_id, $date, $date_end='', $comment='')
 	{
-		global $langs,$conf;
-
-		$error=0;
-
-		$this->db->begin();
-
-		$sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = 4,";
-		$sql.= " date_ouverture = ".(dol_strlen($date)!=0?"'".$this->db->idate($date)."'":"null").",";
-		$sql.= " date_fin_validite = ".(dol_strlen($date_end)!=0?"'".$this->db->idate($date_end)."'":"null").",";
-		$sql.= " fk_user_ouverture = ".$user->id.",";
-		$sql.= " date_cloture = null,";
-		$sql.= " commentaire = '".$this->db->escape($comment)."'";
-		$sql.= " WHERE rowid = ".$line_id . " AND (statut = 0 OR statut = 3 OR statut = 5)";
-
-		dol_syslog(get_class($this)."::active_line", LOG_DEBUG);
-		$resql = $this->db->query($sql);
-		if ($resql)
-		{
-            // Call trigger
-            $result=$this->call_trigger('CONTRACT_SERVICE_ACTIVATE',$user);
-            if ($result < 0) { $error++; $this->db->rollback(); return -1; }
-            // End call triggers
-
-			$this->db->commit();
-			return 1;
-		}
-		else
-		{
-			$this->error=$this->db->lasterror();
-			$this->db->rollback();
-			return -1;
-		}
+		$this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date, $date_end, $comment);
 	}
 
 
@@ -267,37 +242,7 @@ class Contrat extends CommonObject
 	 */
 	function close_line($user, $line_id, $date_end, $comment='')
 	{
-		global $langs,$conf;
-
-		$error=0;
-
-		// statut actif : 4
-
-		$this->db->begin();
-
-		$sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET statut = 5,";
-		$sql.= " date_cloture = '".$this->db->idate($date_end)."',";
-		$sql.= " fk_user_cloture = ".$user->id.",";
-		$sql.= " commentaire = '".$this->db->escape($comment)."'";
-		$sql.= " WHERE rowid = ".$line_id . " AND statut = 4";
-
-		$resql = $this->db->query($sql);
-		if ($resql)
-		{
-            // Call trigger
-            $result=$this->call_trigger('CONTRACT_SERVICE_CLOSE',$user);
-            if ($result < 0) { $error++; $this->db->rollback(); return -1; }
-            // End call triggers
-
-			$this->db->commit();
-			return 1;
-		}
-		else
-		{
-			$this->error=$this->db->lasterror();
-			$this->db->rollback();
-			return -1;
-		}
+		$this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
 	}
 
 
@@ -748,6 +693,7 @@ class Contrat extends CommonObject
 				$line->fetch_optionals($line->id,$extralabelsline);
 
 				$this->lines[]			= $line;
+				$this->lines_id_index_mapper[$line->id] = key($this->lines);
 
 				//dol_syslog("1 ".$line->desc);
 				//dol_syslog("2 ".$line->product_desc);
@@ -852,6 +798,7 @@ class Contrat extends CommonObject
 
 
 				$this->lines[]        = $line;
+				$this->lines_id_index_mapper[$line->id] = key($this->lines);
 
 				$total_ttc+=$objp->total_ttc;
                 $total_vat+=$objp->total_tva;
@@ -2947,4 +2894,106 @@ class ContratLigne extends CommonObjectLine
 			return -1;
 		}
 	}
+
+	/**
+	 *  Activate a contract line
+	 *
+	 * @param    User $user Objet User who activate contract
+	 * @param  int $date Date d'ouverture
+	 * @param  int|string $date_end Date fin prevue
+	 * @param    string $comment A comment typed by user
+	 * @return int                    <0 if KO, >0 if OK
+	 */
+	function active_line($user, $date, $date_end = '', $comment = '')
+	{
+		global $langs, $conf;
+
+		// Update object
+		$this->date_ouverture = $date;
+		$this->date_fin_validite = $date_end;
+		$this->fk_user_ouverture = $user->id;
+		$this->date_cloture = null;
+		$this->commentaire = $comment;
+
+		$error = 0;
+
+		$this->db->begin();
+
+		$sql = "UPDATE " . MAIN_DB_PREFIX . "contratdet SET statut = 4,";
+		$sql .= " date_ouverture = " . (dol_strlen($date) != 0 ? "'" . $this->db->idate($date) . "'" : "null") . ",";
+		$sql .= " date_fin_validite = " . (dol_strlen($date_end) != 0 ? "'" . $this->db->idate($date_end) . "'" : "null") . ",";
+		$sql .= " fk_user_ouverture = " . $user->id . ",";
+		$sql .= " date_cloture = null,";
+		$sql .= " commentaire = '" . $this->db->escape($comment) . "'";
+		$sql .= " WHERE rowid = " . $this->id . " AND (statut = 0 OR statut = 3 OR statut = 5)";
+
+		dol_syslog(get_class($this) . "::active_line", LOG_DEBUG);
+		$resql = $this->db->query($sql);
+		if ($resql) {
+			// Call trigger
+			$result = $this->call_trigger('CONTRACT_SERVICE_ACTIVATE', $user);
+			if ($result < 0) {
+				$error++;
+				$this->db->rollback();
+				return -1;
+			}
+			// End call triggers
+
+			$this->db->commit();
+			return 1;
+		} else {
+			$this->error = $this->db->lasterror();
+			$this->db->rollback();
+			return -1;
+		}
+	}
+
+	/**
+	 *  Close a contract line
+	 *
+	 * @param    User $user Objet User who close contract
+	 * @param  int $date_end Date end
+	 * @param    string $comment A comment typed by user
+	 * @return int                    <0 if KO, >0 if OK
+	 */
+	function close_line($user, $date_end, $comment = '')
+	{
+		global $langs, $conf;
+
+		// Update object
+		$this->date_cloture = $date_end;
+		$this->fk_user_cloture = $user->id;
+		$this->commentaire = $comment;
+
+		$error = 0;
+
+		// statut actif : 4
+
+		$this->db->begin();
+
+		$sql = "UPDATE " . MAIN_DB_PREFIX . "contratdet SET statut = 5,";
+		$sql .= " date_cloture = '" . $this->db->idate($date_end) . "',";
+		$sql .= " fk_user_cloture = " . $user->id . ",";
+		$sql .= " commentaire = '" . $this->db->escape($comment) . "'";
+		$sql .= " WHERE rowid = " . $this->id . " AND statut = 4";
+
+		$resql = $this->db->query($sql);
+		if ($resql) {
+			// Call trigger
+			$result = $this->call_trigger('CONTRACT_SERVICE_CLOSE', $user);
+			if ($result < 0) {
+				$error++;
+				$this->db->rollback();
+				return -1;
+			}
+			// End call triggers
+
+			$this->db->commit();
+			return 1;
+		} else {
+			$this->error = $this->db->lasterror();
+			$this->db->rollback();
+			return -1;
+		}
+	}
 }
-- 
GitLab