diff --git a/htdocs/projet/class/api_projects.class.php b/htdocs/projet/class/api_projects.class.php index e83921135a2..59576b961d4 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..cafb60f2865 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..0656fd0bf96 100644 --- a/htdocs/projet/class/task.class.php +++ b/htdocs/projet/class/task.class.php @@ -1281,6 +1281,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 *