From e51a002c10355b17d693d4cda9b5434e88f38e48 Mon Sep 17 00:00:00 2001
From: Regis Houssin <regis@dolibarr.fr>
Date: Tue, 2 Mar 2010 07:28:59 +0000
Subject: [PATCH] Fix: management of time spent in task card Fix: Update time
 spent in seconds

---
 htdocs/projet/tasks/task.class.php | 19 +++++++++----------
 htdocs/projet/tasks/time.php       |  8 +++++---
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/htdocs/projet/tasks/task.class.php b/htdocs/projet/tasks/task.class.php
index bf2db78307b..b4136b54e54 100644
--- a/htdocs/projet/tasks/task.class.php
+++ b/htdocs/projet/tasks/task.class.php
@@ -59,6 +59,7 @@ class Task extends CommonObject
 
 	var $timespent_id;
 	var $timespent_duration;
+	var $timespent_old_duration;
 	var $timespent_date;
 	var $timespent_fk_user;
 	var $timespent_note;
@@ -597,8 +598,6 @@ class Task extends CommonObject
 		$ret = 0;
 
 		// Clean parameters
-		$this->timespent_duration = intval($this->timespent_duration)+(($this->timespent_duration-intval($this->timespent_duration))*(1+2/3));
-		$this->timespent_duration = price2num($this->timespent_duration);
 		if (isset($this->timespent_note)) $this->timespent_note = trim($this->timespent_note);
 
 		$sql = "INSERT INTO ".MAIN_DB_PREFIX."projet_task_time (";
@@ -713,8 +712,6 @@ class Task extends CommonObject
 		$ret = 0;
 
 		// Clean parameters
-		$this->timespent_duration = intval($this->timespent_duration)+(($this->timespent_duration-intval($this->timespent_duration))*(1+2/3));
-		$this->timespent_duration = price2num($this->timespent_duration);
 		if (isset($this->timespent_note)) $this->timespent_note = trim($this->timespent_note);
 
 		$sql = "UPDATE ".MAIN_DB_PREFIX."projet_task_time SET";
@@ -744,14 +741,16 @@ class Task extends CommonObject
 			dol_syslog(get_class($this)."::updateTimeSpent error -1 ".$this->error,LOG_ERR);
 			$ret = -1;
 		}
-/*
-		if ($ret >= 0)
+
+		if ($ret == 1 && ($this->timespent_old_duration != $this->timespent_duration))
 		{
+			$newDuration = $this->timespent_duration - $this->timespent_old_duration;
+
 			$sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
-			$sql.= " SET duration_effective = duration_effective + '".price2num($this->timespent_duration)."'";
+			$sql.= " SET duration_effective = duration_effective + '".$newDuration."'";
 			$sql.= " WHERE rowid = ".$this->id;
 
-			dol_syslog(get_class($this)."::addTimeSpent sql=".$sql, LOG_DEBUG);
+			dol_syslog(get_class($this)."::updateTimeSpent sql=".$sql, LOG_DEBUG);
 			if (! $this->db->query($sql) )
 			{
 				$this->error=$this->db->lasterror();
@@ -759,7 +758,7 @@ class Task extends CommonObject
 				$ret = -2;
 			}
 		}
-*/
+
 		return $ret;
 	}
     
@@ -800,7 +799,7 @@ class Task extends CommonObject
 		if (! $error)
 		{
 			$sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
-			$sql.= " SET duration_effective = duration_effective - '".price2num($this->timespent_duration)."'";
+			$sql.= " SET duration_effective = duration_effective - '".$this->timespent_duration."'";
 			$sql.= " WHERE rowid = ".$this->id;
 
 			dol_syslog(get_class($this)."::delTimeSpent sql=".$sql, LOG_DEBUG);
diff --git a/htdocs/projet/tasks/time.php b/htdocs/projet/tasks/time.php
index 899b2c74ae2..5ff5dc86b82 100644
--- a/htdocs/projet/tasks/time.php
+++ b/htdocs/projet/tasks/time.php
@@ -79,7 +79,7 @@ if ($_POST["action"] == 'updateline' && ! $_POST["cancel"] && $user->rights->pro
 {
 	$error=0;
 
-	if (empty($_POST["timespent_duration_linehour"]) && empty($_POST["timespent_duration_linemin"]))
+	if (empty($_POST["new_durationhour"]) && empty($_POST["new_durationmin"]))
 	{
 		$mesg='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Duration")).'</div>';
 		$error++;
@@ -88,11 +88,13 @@ if ($_POST["action"] == 'updateline' && ! $_POST["cancel"] && $user->rights->pro
 	if (! $error)
 	{
 		$task = new Task($db);
+		$task->fetch($_POST["id"]);
 
 		$task->timespent_id = $_POST["lineid"];
 		$task->timespent_note = $_POST["timespent_note_line"];
-		$task->timespent_duration = $_POST["timespent_duration_linehour"]*60*60;	// We store duration in seconds
-		$task->timespent_duration+= $_POST["timespent_duration_linemin"]*60;		// We store duration in seconds
+		$task->timespent_old_duration = $_POST["old_duration"];
+		$task->timespent_duration = $_POST["new_durationhour"]*60*60;	// We store duration in seconds
+		$task->timespent_duration+= $_POST["new_durationmin"]*60;		// We store duration in seconds
 		$task->timespent_date = dol_mktime(12,0,0,$_POST["timelinemonth"],$_POST["timelineday"],$_POST["timelineyear"]);
 		$task->timespent_fk_user = $_POST["userid_line"];
 
-- 
GitLab