From a8dd73d007d95f9e34a23b93a8ca40b11ba9bff5 Mon Sep 17 00:00:00 2001 From: mschamp <34092708+mschamp@users.noreply.github.com> Date: Fri, 28 Jan 2022 22:59:32 +0100 Subject: [PATCH 1/2] FIX #19294 implement detailed timespent in task of project API --- htdocs/projet/class/api_projects.class.php | 7 +- htdocs/projet/class/api_tasks.class.php | 7 +- htdocs/projet/class/task.class.php | 91 ++++++++++++++++++++++ 3 files changed, 97 insertions(+), 8 deletions(-) diff --git a/htdocs/projet/class/api_projects.class.php b/htdocs/projet/class/api_projects.class.php index e83921135a2..880a087d10c 100644 --- a/htdocs/projet/class/api_projects.class.php +++ b/htdocs/projet/class/api_projects.class.php @@ -228,7 +228,7 @@ class Projects extends DolibarrApi * See also API /tasks * * @param int $id Id of project - * @param int $includetimespent 0=Return only list of tasks. 1=Include a summary of time spent, 2=Include details of time spent lines (2 is no implemented yet) + * @param int $includetimespent 0=Return only list of tasks. 1=Include a summary of time spent, 2=Include details of time spent lines * @return int * * @url GET {id}/tasks @@ -253,9 +253,8 @@ class Projects extends DolibarrApi if ($includetimespent == 1) { $timespent = $line->getSummaryOfTimeSpent(0); } - if ($includetimespent == 1) { - // TODO - // Add class for timespent records and loop and fill $line->lines with records of timespent + if ($includetimespent == 2) { + $timespent = $line->fetchTimeSpentOnTask(); } array_push($result, $this->_cleanObjectDatas($line)); } diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php index 49a5d9d418b..21b5e49f44e 100644 --- a/htdocs/projet/class/api_tasks.class.php +++ b/htdocs/projet/class/api_tasks.class.php @@ -60,7 +60,7 @@ class Tasks extends DolibarrApi * Return an array with task informations * * @param int $id ID of task - * @param int $includetimespent 0=Return only task. 1=Include a summary of time spent, 2=Include details of time spent lines (2 is no implemented yet) + * @param int $includetimespent 0=Return only task. 1=Include a summary of time spent, 2=Include details of time spent lines * @return array|mixed data without useless information * * @throws RestException @@ -83,9 +83,8 @@ class Tasks extends DolibarrApi if ($includetimespent == 1) { $timespent = $this->task->getSummaryOfTimeSpent(0); } - if ($includetimespent == 1) { - // TODO - // Add class for timespent records and loop and fill $line->lines with records of timespent + if ($includetimespent == 2) { + $timespent = $this->task->fetchTimeSpentOnTask(); } return $this->_cleanObjectDatas($this->task); diff --git a/htdocs/projet/class/task.class.php b/htdocs/projet/class/task.class.php index 87a66279714..0254be49ef6 100644 --- a/htdocs/projet/class/task.class.php +++ b/htdocs/projet/class/task.class.php @@ -1280,6 +1280,97 @@ class Task extends CommonObject } return $ret; } + + /** + * Fetch records of time spent of this task + * + * @param string $morewherefilter Add more filter into where SQL request (must start with ' AND ...') + * @return int <0 if KO, array of time spent if OK + */ + public function fetchTimeSpentOnTask($morewherefilter = '') + { + global $langs; + + $arrayres = array(); + + $sql = "SELECT"; + $sql .= " s.rowid as socid,"; + $sql .= " s.nom as thirdparty_name,"; + $sql .= " s.email as thirdparty_email,"; + $sql .= " ptt.rowid,"; + $sql .= " ptt.fk_task,"; + $sql .= " ptt.task_date,"; + $sql .= " ptt.task_datehour,"; + $sql .= " ptt.task_date_withhour,"; + $sql .= " ptt.task_duration,"; + $sql .= " ptt.fk_user,"; + $sql .= " ptt.note,"; + $sql .= " ptt.thm,"; + $sql .= " pt.rowid as task_id,"; + $sql .= " pt.ref as task_ref,"; + $sql .= " pt.label as task_label,"; + $sql .= " p.rowid as project_id,"; + $sql .= " p.ref as project_ref,"; + $sql .= " p.title as project_label,"; + $sql .= " p.public as public"; + $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as ptt, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."projet as p"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid"; + $sql .= " WHERE ptt.fk_task = pt.rowid AND pt.fk_projet = p.rowid"; + $sql .= " AND pt.rowid = ".((int) $this->id); + $sql .= " AND pt.entity IN (".getEntity('project').")"; + if ($morewherefilter) { + $sql .= $morewherefilter; + } + + dol_syslog(get_class($this)."::fetchAllTimeSpent", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $num = $this->db->num_rows($resql); + + $i = 0; + while ($i < $num) { + $obj = $this->db->fetch_object($resql); + + $newobj = new stdClass(); + + $newobj->socid = $obj->socid; + $newobj->thirdparty_name = $obj->thirdparty_name; + $newobj->thirdparty_email = $obj->thirdparty_email; + + $newobj->fk_project = $obj->project_id; + $newobj->project_ref = $obj->project_ref; + $newobj->project_label = $obj->project_label; + $newobj->public = $obj->project_public; + + $newobj->fk_task = $obj->task_id; + $newobj->task_ref = $obj->task_ref; + $newobj->task_label = $obj->task_label; + + $newobj->timespent_line_id = $obj->rowid; + $newobj->timespent_line_date = $this->db->jdate($obj->task_date); + $newobj->timespent_line_datehour = $this->db->jdate($obj->task_datehour); + $newobj->timespent_line_withhour = $obj->task_date_withhour; + $newobj->timespent_line_duration = $obj->task_duration; + $newobj->timespent_line_fk_user = $obj->fk_user; + $newobj->timespent_line_thm = $obj->thm; // hourly rate + $newobj->timespent_line_note = $obj->note; + + $arrayres[] = $newobj; + + $i++; + } + + $this->db->free($resql); + + $this->lines = $arrayres; + return 1; + } else { + dol_print_error($this->db); + $this->error = "Error ".$this->db->lasterror(); + return -1; + } + } + /** * Calculate total of time spent for task From a8344d9059bfd76e74a15bdb53b5aa68c602e5b0 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Fri, 28 Jan 2022 22:03:08 +0000 Subject: [PATCH 2/2] Fixing style errors. --- htdocs/projet/class/api_projects.class.php | 2 +- htdocs/projet/class/api_tasks.class.php | 2 +- htdocs/projet/class/task.class.php | 164 ++++++++++----------- 3 files changed, 84 insertions(+), 84 deletions(-) diff --git a/htdocs/projet/class/api_projects.class.php b/htdocs/projet/class/api_projects.class.php index 880a087d10c..59576b961d4 100644 --- a/htdocs/projet/class/api_projects.class.php +++ b/htdocs/projet/class/api_projects.class.php @@ -254,7 +254,7 @@ class Projects extends DolibarrApi $timespent = $line->getSummaryOfTimeSpent(0); } if ($includetimespent == 2) { - $timespent = $line->fetchTimeSpentOnTask(); + $timespent = $line->fetchTimeSpentOnTask(); } array_push($result, $this->_cleanObjectDatas($line)); } diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php index 21b5e49f44e..cafb60f2865 100644 --- a/htdocs/projet/class/api_tasks.class.php +++ b/htdocs/projet/class/api_tasks.class.php @@ -84,7 +84,7 @@ class Tasks extends DolibarrApi $timespent = $this->task->getSummaryOfTimeSpent(0); } if ($includetimespent == 2) { - $timespent = $this->task->fetchTimeSpentOnTask(); + $timespent = $this->task->fetchTimeSpentOnTask(); } return $this->_cleanObjectDatas($this->task); diff --git a/htdocs/projet/class/task.class.php b/htdocs/projet/class/task.class.php index 0254be49ef6..0656fd0bf96 100644 --- a/htdocs/projet/class/task.class.php +++ b/htdocs/projet/class/task.class.php @@ -1280,7 +1280,7 @@ class Task extends CommonObject } return $ret; } - + /** * Fetch records of time spent of this task * @@ -1289,88 +1289,88 @@ class Task extends CommonObject */ public function fetchTimeSpentOnTask($morewherefilter = '') { - global $langs; - - $arrayres = array(); - - $sql = "SELECT"; - $sql .= " s.rowid as socid,"; - $sql .= " s.nom as thirdparty_name,"; - $sql .= " s.email as thirdparty_email,"; - $sql .= " ptt.rowid,"; - $sql .= " ptt.fk_task,"; - $sql .= " ptt.task_date,"; - $sql .= " ptt.task_datehour,"; - $sql .= " ptt.task_date_withhour,"; - $sql .= " ptt.task_duration,"; - $sql .= " ptt.fk_user,"; - $sql .= " ptt.note,"; - $sql .= " ptt.thm,"; - $sql .= " pt.rowid as task_id,"; - $sql .= " pt.ref as task_ref,"; - $sql .= " pt.label as task_label,"; - $sql .= " p.rowid as project_id,"; - $sql .= " p.ref as project_ref,"; - $sql .= " p.title as project_label,"; - $sql .= " p.public as public"; - $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as ptt, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."projet as p"; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid"; - $sql .= " WHERE ptt.fk_task = pt.rowid AND pt.fk_projet = p.rowid"; - $sql .= " AND pt.rowid = ".((int) $this->id); - $sql .= " AND pt.entity IN (".getEntity('project').")"; - if ($morewherefilter) { - $sql .= $morewherefilter; - } - - dol_syslog(get_class($this)."::fetchAllTimeSpent", LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) { - $num = $this->db->num_rows($resql); - - $i = 0; - while ($i < $num) { - $obj = $this->db->fetch_object($resql); - - $newobj = new stdClass(); - - $newobj->socid = $obj->socid; - $newobj->thirdparty_name = $obj->thirdparty_name; - $newobj->thirdparty_email = $obj->thirdparty_email; - - $newobj->fk_project = $obj->project_id; - $newobj->project_ref = $obj->project_ref; - $newobj->project_label = $obj->project_label; - $newobj->public = $obj->project_public; - - $newobj->fk_task = $obj->task_id; - $newobj->task_ref = $obj->task_ref; - $newobj->task_label = $obj->task_label; - - $newobj->timespent_line_id = $obj->rowid; - $newobj->timespent_line_date = $this->db->jdate($obj->task_date); - $newobj->timespent_line_datehour = $this->db->jdate($obj->task_datehour); - $newobj->timespent_line_withhour = $obj->task_date_withhour; - $newobj->timespent_line_duration = $obj->task_duration; - $newobj->timespent_line_fk_user = $obj->fk_user; - $newobj->timespent_line_thm = $obj->thm; // hourly rate - $newobj->timespent_line_note = $obj->note; - - $arrayres[] = $newobj; - - $i++; - } - - $this->db->free($resql); - - $this->lines = $arrayres; - return 1; - } else { - dol_print_error($this->db); - $this->error = "Error ".$this->db->lasterror(); - return -1; - } + global $langs; + + $arrayres = array(); + + $sql = "SELECT"; + $sql .= " s.rowid as socid,"; + $sql .= " s.nom as thirdparty_name,"; + $sql .= " s.email as thirdparty_email,"; + $sql .= " ptt.rowid,"; + $sql .= " ptt.fk_task,"; + $sql .= " ptt.task_date,"; + $sql .= " ptt.task_datehour,"; + $sql .= " ptt.task_date_withhour,"; + $sql .= " ptt.task_duration,"; + $sql .= " ptt.fk_user,"; + $sql .= " ptt.note,"; + $sql .= " ptt.thm,"; + $sql .= " pt.rowid as task_id,"; + $sql .= " pt.ref as task_ref,"; + $sql .= " pt.label as task_label,"; + $sql .= " p.rowid as project_id,"; + $sql .= " p.ref as project_ref,"; + $sql .= " p.title as project_label,"; + $sql .= " p.public as public"; + $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as ptt, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."projet as p"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid"; + $sql .= " WHERE ptt.fk_task = pt.rowid AND pt.fk_projet = p.rowid"; + $sql .= " AND pt.rowid = ".((int) $this->id); + $sql .= " AND pt.entity IN (".getEntity('project').")"; + if ($morewherefilter) { + $sql .= $morewherefilter; + } + + dol_syslog(get_class($this)."::fetchAllTimeSpent", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $num = $this->db->num_rows($resql); + + $i = 0; + while ($i < $num) { + $obj = $this->db->fetch_object($resql); + + $newobj = new stdClass(); + + $newobj->socid = $obj->socid; + $newobj->thirdparty_name = $obj->thirdparty_name; + $newobj->thirdparty_email = $obj->thirdparty_email; + + $newobj->fk_project = $obj->project_id; + $newobj->project_ref = $obj->project_ref; + $newobj->project_label = $obj->project_label; + $newobj->public = $obj->project_public; + + $newobj->fk_task = $obj->task_id; + $newobj->task_ref = $obj->task_ref; + $newobj->task_label = $obj->task_label; + + $newobj->timespent_line_id = $obj->rowid; + $newobj->timespent_line_date = $this->db->jdate($obj->task_date); + $newobj->timespent_line_datehour = $this->db->jdate($obj->task_datehour); + $newobj->timespent_line_withhour = $obj->task_date_withhour; + $newobj->timespent_line_duration = $obj->task_duration; + $newobj->timespent_line_fk_user = $obj->fk_user; + $newobj->timespent_line_thm = $obj->thm; // hourly rate + $newobj->timespent_line_note = $obj->note; + + $arrayres[] = $newobj; + + $i++; + } + + $this->db->free($resql); + + $this->lines = $arrayres; + return 1; + } else { + dol_print_error($this->db); + $this->error = "Error ".$this->db->lasterror(); + return -1; + } } - + /** * Calculate total of time spent for task