dolibarr  16.0.1
task.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2008-2014 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
5  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
6  * Copyright (C) 2020 Juanjo Menent <jmenent@2byte.es>
7  * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
29 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
32 
33 
37 class Task extends CommonObjectLine
38 {
42  public $element = 'project_task';
43 
47  public $table_element = 'projet_task';
48 
52  public $fk_element = 'fk_task';
53 
57  public $picto = 'projecttask';
58 
62  protected $childtables = array(
63  'projet_task_time' => array('name' => 'Task', 'parent' => 'projet_task', 'parentkey' => 'fk_task')
64  );
65 
69  public $fk_task_parent = 0;
70 
74  public $label;
75 
79  public $description;
80 
81  public $duration_effective; // total of time spent on this task
82  public $planned_workload;
83  public $date_c;
84  public $date_start;
85  public $date_end;
86  public $progress;
87 
91  public $datee;
92 
96  public $fk_statut;
97 
98  public $priority;
99 
103  public $fk_user_creat;
104 
108  public $fk_user_valid;
109 
110  public $rang;
111 
112  public $timespent_min_date;
113  public $timespent_max_date;
114  public $timespent_total_duration;
115  public $timespent_total_amount;
116  public $timespent_nblinesnull;
117  public $timespent_nblines;
118  // For detail of lines of timespent record, there is the property ->lines in common
119 
120  // Var used to call method addTimeSpent(). Bad practice.
121  public $timespent_id;
122  public $timespent_duration;
123  public $timespent_old_duration;
124  public $timespent_date;
125  public $timespent_datehour; // More accurate start date (same than timespent_date but includes hours, minutes and seconds)
126  public $timespent_withhour; // 1 = we entered also start hours for timesheet line
127  public $timespent_fk_user;
128  public $timespent_thm;
129  public $timespent_note;
130 
131  public $comments = array();
132 
136  public $budget_amount;
137 
141  public $project_budget_amount;
142 
143  public $oldcopy;
144 
145 
151  public function __construct($db)
152  {
153  $this->db = $db;
154  }
155 
156 
164  public function create($user, $notrigger = 0)
165  {
166  global $conf, $langs;
167 
168  //For the date
169  $now = dol_now();
170 
171  $error = 0;
172 
173  // Clean parameters
174  $this->label = trim($this->label);
175  $this->description = trim($this->description);
176 
177  if (!empty($this->date_start) && !empty($this->date_end) && $this->date_start > $this->date_end) {
178  $this->errors[] = $langs->trans('StartDateCannotBeAfterEndDate');
179  return -1;
180  }
181 
182  // Check parameters
183  // Put here code to add control on parameters values
184 
185  // Insert request
186  $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet_task (";
187  $sql .= "entity";
188  $sql .= ", fk_projet";
189  $sql .= ", ref";
190  $sql .= ", fk_task_parent";
191  $sql .= ", label";
192  $sql .= ", description";
193  $sql .= ", datec";
194  $sql .= ", fk_user_creat";
195  $sql .= ", dateo";
196  $sql .= ", datee";
197  $sql .= ", planned_workload";
198  $sql .= ", progress";
199  $sql .= ", budget_amount";
200  $sql .= ") VALUES (";
201  $sql .= ((int) $conf->entity);
202  $sql .= ", ".((int) $this->fk_project);
203  $sql .= ", ".(!empty($this->ref) ? "'".$this->db->escape($this->ref)."'" : 'null');
204  $sql .= ", ".((int) $this->fk_task_parent);
205  $sql .= ", '".$this->db->escape($this->label)."'";
206  $sql .= ", '".$this->db->escape($this->description)."'";
207  $sql .= ", '".$this->db->idate($now)."'";
208  $sql .= ", ".((int) $user->id);
209  $sql .= ", ".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : 'null');
210  $sql .= ", ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : 'null');
211  $sql .= ", ".(($this->planned_workload != '' && $this->planned_workload >= 0) ? ((int) $this->planned_workload) : 'null');
212  $sql .= ", ".(($this->progress != '' && $this->progress >= 0) ? ((int) $this->progress) : 'null');
213  $sql .= ", ".(($this->budget_amount != '' && $this->budget_amount >= 0) ? ((int) $this->budget_amount) : 'null');
214  $sql .= ")";
215 
216  $this->db->begin();
217 
218  dol_syslog(get_class($this)."::create", LOG_DEBUG);
219  $resql = $this->db->query($sql);
220  if (!$resql) {
221  $error++; $this->errors[] = "Error ".$this->db->lasterror();
222  }
223 
224  if (!$error) {
225  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task");
226 
227  if (!$notrigger) {
228  // Call trigger
229  $result = $this->call_trigger('TASK_CREATE', $user);
230  if ($result < 0) {
231  $error++;
232  }
233  // End call triggers
234  }
235  }
236 
237  // Update extrafield
238  if (!$error) {
239  if (!$error) {
240  $result = $this->insertExtraFields();
241  if ($result < 0) {
242  $error++;
243  }
244  }
245  }
246 
247  // Commit or rollback
248  if ($error) {
249  foreach ($this->errors as $errmsg) {
250  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
251  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
252  }
253  $this->db->rollback();
254  return -1 * $error;
255  } else {
256  $this->db->commit();
257  return $this->id;
258  }
259  }
260 
261 
270  public function fetch($id, $ref = '', $loadparentdata = 0)
271  {
272  global $langs, $conf;
273 
274  $sql = "SELECT";
275  $sql .= " t.rowid,";
276  $sql .= " t.ref,";
277  $sql .= " t.fk_projet as fk_project,";
278  $sql .= " t.fk_task_parent,";
279  $sql .= " t.label,";
280  $sql .= " t.description,";
281  $sql .= " t.duration_effective,";
282  $sql .= " t.planned_workload,";
283  $sql .= " t.datec,";
284  $sql .= " t.dateo,";
285  $sql .= " t.datee,";
286  $sql .= " t.fk_user_creat,";
287  $sql .= " t.fk_user_valid,";
288  $sql .= " t.fk_statut,";
289  $sql .= " t.progress,";
290  $sql .= " t.budget_amount,";
291  $sql .= " t.priority,";
292  $sql .= " t.note_private,";
293  $sql .= " t.note_public,";
294  $sql .= " t.rang";
295  if (!empty($loadparentdata)) {
296  $sql .= ", t2.ref as task_parent_ref";
297  $sql .= ", t2.rang as task_parent_position";
298  }
299  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t";
300  if (!empty($loadparentdata)) {
301  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t2 ON t.fk_task_parent = t2.rowid";
302  }
303  $sql .= " WHERE ";
304  if (!empty($ref)) {
305  $sql .= "entity IN (".getEntity('project').")";
306  $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
307  } else {
308  $sql .= "t.rowid = ".((int) $id);
309  }
310 
311  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
312  $resql = $this->db->query($sql);
313  if ($resql) {
314  $num_rows = $this->db->num_rows($resql);
315 
316  if ($num_rows) {
317  $obj = $this->db->fetch_object($resql);
318 
319  $this->id = $obj->rowid;
320  $this->ref = $obj->ref;
321  $this->fk_project = $obj->fk_project;
322  $this->fk_task_parent = $obj->fk_task_parent;
323  $this->label = $obj->label;
324  $this->description = $obj->description;
325  $this->duration_effective = $obj->duration_effective;
326  $this->planned_workload = $obj->planned_workload;
327  $this->date_c = $this->db->jdate($obj->datec);
328  $this->date_start = $this->db->jdate($obj->dateo);
329  $this->date_end = $this->db->jdate($obj->datee);
330  $this->fk_user_creat = $obj->fk_user_creat;
331  $this->fk_user_valid = $obj->fk_user_valid;
332  $this->fk_statut = $obj->fk_statut;
333  $this->progress = $obj->progress;
334  $this->budget_amount = $obj->budget_amount;
335  $this->priority = $obj->priority;
336  $this->note_private = $obj->note_private;
337  $this->note_public = $obj->note_public;
338  $this->rang = $obj->rang;
339 
340  if (!empty($loadparentdata)) {
341  $this->task_parent_ref = $obj->task_parent_ref;
342  $this->task_parent_position = $obj->task_parent_position;
343  }
344 
345  // Retrieve all extrafield
346  $this->fetch_optionals();
347  }
348 
349  $this->db->free($resql);
350 
351  if ($num_rows) {
352  return 1;
353  } else {
354  return 0;
355  }
356  } else {
357  $this->error = "Error ".$this->db->lasterror();
358  return -1;
359  }
360  }
361 
362 
370  public function update($user = null, $notrigger = 0)
371  {
372  global $conf, $langs;
373  $error = 0;
374 
375  // Clean parameters
376  if (isset($this->fk_project)) {
377  $this->fk_project = trim($this->fk_project);
378  }
379  if (isset($this->ref)) {
380  $this->ref = trim($this->ref);
381  }
382  if (isset($this->fk_task_parent)) {
383  $this->fk_task_parent = (int) $this->fk_task_parent;
384  }
385  if (isset($this->label)) {
386  $this->label = trim($this->label);
387  }
388  if (isset($this->description)) {
389  $this->description = trim($this->description);
390  }
391  if (isset($this->duration_effective)) {
392  $this->duration_effective = trim($this->duration_effective);
393  }
394  if (isset($this->planned_workload)) {
395  $this->planned_workload = trim($this->planned_workload);
396  }
397  if (isset($this->budget_amount)) {
398  $this->budget_amount = trim($this->budget_amount);
399  }
400 
401  if (!empty($this->date_start) && !empty($this->date_end) && $this->date_start > $this->date_end) {
402  $this->errors[] = $langs->trans('StartDateCannotBeAfterEndDate');
403  return -1;
404  }
405 
406  // Check parameters
407  // Put here code to add control on parameters values
408 
409  // Update request
410  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
411  $sql .= " fk_projet=".(isset($this->fk_project) ? $this->fk_project : "null").",";
412  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "'".$this->db->escape($this->id)."'").",";
413  $sql .= " fk_task_parent=".(isset($this->fk_task_parent) ? $this->fk_task_parent : "null").",";
414  $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
415  $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
416  $sql .= " duration_effective=".(isset($this->duration_effective) ? $this->duration_effective : "null").",";
417  $sql .= " planned_workload=".((isset($this->planned_workload) && $this->planned_workload != '') ? $this->planned_workload : "null").",";
418  $sql .= " dateo=".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null').",";
419  $sql .= " datee=".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null').",";
420  $sql .= " progress=".(($this->progress != '' && $this->progress >= 0) ? $this->progress : 'null').",";
421  $sql .= " budget_amount=".(($this->budget_amount != '' && $this->budget_amount >= 0) ? $this->budget_amount : 'null').",";
422  $sql .= " rang=".((!empty($this->rang)) ? $this->rang : "0");
423  $sql .= " WHERE rowid=".((int) $this->id);
424 
425  $this->db->begin();
426 
427  dol_syslog(get_class($this)."::update", LOG_DEBUG);
428  $resql = $this->db->query($sql);
429  if (!$resql) {
430  $error++; $this->errors[] = "Error ".$this->db->lasterror();
431  }
432 
433  // Update extrafield
434  if (!$error) {
435  $result = $this->insertExtraFields();
436  if ($result < 0) {
437  $error++;
438  }
439  }
440 
441  if (!$error && !empty($conf->global->PROJECT_CLASSIFY_CLOSED_WHEN_ALL_TASKS_DONE)) {
442  // Close the parent project if it is open (validated) and its tasks are 100% completed
443  $project = new Project($this->db);
444  if ($project->fetch($this->fk_project) > 0) {
445  if ($project->statut == Project::STATUS_VALIDATED) {
446  $project->getLinesArray(null); // this method does not return <= 0 if fails
447  $projectCompleted = array_reduce(
448  $project->lines,
449  function ($allTasksCompleted, $task) {
450  return $allTasksCompleted && $task->progress >= 100;
451  },
452  1
453  );
454  if ($projectCompleted) {
455  if ($project->setClose($user) <= 0) {
456  $error++;
457  }
458  }
459  }
460  } else {
461  $error++;
462  }
463  if ($error) {
464  $this->errors[] = $project->error;
465  }
466  }
467 
468  if (!$error) {
469  if (!$notrigger) {
470  // Call trigger
471  $result = $this->call_trigger('TASK_MODIFY', $user);
472  if ($result < 0) {
473  $error++;
474  }
475  // End call triggers
476  }
477  }
478 
479  if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
480  // We remove directory
481  if ($conf->project->dir_output) {
482  $project = new Project($this->db);
483  $project->fetch($this->fk_project);
484 
485  $olddir = $conf->project->dir_output.'/'.dol_sanitizeFileName($project->ref).'/'.dol_sanitizeFileName($this->oldcopy->ref);
486  $newdir = $conf->project->dir_output.'/'.dol_sanitizeFileName($project->ref).'/'.dol_sanitizeFileName($this->ref);
487  if (file_exists($olddir)) {
488  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
489  $res = dol_move_dir($olddir, $newdir);
490  if (!$res) {
491  $langs->load("errors");
492  $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
493  $error++;
494  }
495  }
496  }
497  }
498 
499  // Commit or rollback
500  if ($error) {
501  foreach ($this->errors as $errmsg) {
502  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
503  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
504  }
505  $this->db->rollback();
506  return -1 * $error;
507  } else {
508  $this->db->commit();
509  return 1;
510  }
511  }
512 
513 
521  public function delete($user, $notrigger = 0)
522  {
523 
524  global $conf, $langs;
525  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
526 
527  $error = 0;
528 
529  $this->db->begin();
530 
531  if ($this->hasChildren() > 0) {
532  dol_syslog(get_class($this)."::delete Can't delete record as it has some sub tasks", LOG_WARNING);
533  $this->error = 'ErrorRecordHasSubTasks';
534  $this->db->rollback();
535  return 0;
536  }
537 
538  $objectisused = $this->isObjectUsed($this->id);
539  if (!empty($objectisused)) {
540  dol_syslog(get_class($this)."::delete Can't delete record as it has some child", LOG_WARNING);
541  $this->error = 'ErrorRecordHasChildren';
542  $this->db->rollback();
543  return 0;
544  }
545 
546  if (!$error) {
547  // Delete linked contacts
548  $res = $this->delete_linked_contact();
549  if ($res < 0) {
550  $this->error = 'ErrorFailToDeleteLinkedContact';
551  //$error++;
552  $this->db->rollback();
553  return 0;
554  }
555  }
556 
557  if (!$error) {
558  $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_time";
559  $sql .= " WHERE fk_task = ".((int) $this->id);
560 
561  $resql = $this->db->query($sql);
562  if (!$resql) {
563  $error++; $this->errors[] = "Error ".$this->db->lasterror();
564  }
565  }
566 
567  if (!$error) {
568  $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_extrafields";
569  $sql .= " WHERE fk_object = ".((int) $this->id);
570 
571  $resql = $this->db->query($sql);
572  if (!$resql) {
573  $error++; $this->errors[] = "Error ".$this->db->lasterror();
574  }
575  }
576 
577  if (!$error) {
578  $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task";
579  $sql .= " WHERE rowid=".((int) $this->id);
580 
581  $resql = $this->db->query($sql);
582  if (!$resql) {
583  $error++; $this->errors[] = "Error ".$this->db->lasterror();
584  }
585  }
586 
587  if (!$error) {
588  if (!$notrigger) {
589  // Call trigger
590  $result = $this->call_trigger('TASK_DELETE', $user);
591  if ($result < 0) {
592  $error++;
593  }
594  // End call triggers
595  }
596  }
597 
598  // Commit or rollback
599  if ($error) {
600  foreach ($this->errors as $errmsg) {
601  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
602  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
603  }
604  $this->db->rollback();
605  return -1 * $error;
606  } else {
607  //Delete associated link file
608  if ($conf->project->dir_output) {
609  $projectstatic = new Project($this->db);
610  $projectstatic->fetch($this->fk_project);
611 
612  $dir = $conf->project->dir_output."/".dol_sanitizeFileName($projectstatic->ref).'/'.dol_sanitizeFileName($this->id);
613  dol_syslog(get_class($this)."::delete dir=".$dir, LOG_DEBUG);
614  if (file_exists($dir)) {
615  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
616  $res = @dol_delete_dir_recursive($dir);
617  if (!$res) {
618  $this->error = 'ErrorFailToDeleteDir';
619  $this->db->rollback();
620  return 0;
621  }
622  }
623  }
624 
625  $this->db->commit();
626 
627  return 1;
628  }
629  }
630 
636  public function hasChildren()
637  {
638  $error = 0;
639  $ret = 0;
640 
641  $sql = "SELECT COUNT(*) as nb";
642  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task";
643  $sql .= " WHERE fk_task_parent = ".((int) $this->id);
644 
645  dol_syslog(get_class($this)."::hasChildren", LOG_DEBUG);
646  $resql = $this->db->query($sql);
647  if (!$resql) {
648  $error++; $this->errors[] = "Error ".$this->db->lasterror();
649  } else {
650  $obj = $this->db->fetch_object($resql);
651  if ($obj) {
652  $ret = $obj->nb;
653  }
654  $this->db->free($resql);
655  }
656 
657  if (!$error) {
658  return $ret;
659  } else {
660  return -1;
661  }
662  }
663 
669  public function hasTimeSpent()
670  {
671  $error = 0;
672  $ret = 0;
673 
674  $sql = "SELECT COUNT(*) as nb";
675  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time";
676  $sql .= " WHERE fk_task = ".((int) $this->id);
677 
678  dol_syslog(get_class($this)."::hasTimeSpent", LOG_DEBUG);
679  $resql = $this->db->query($sql);
680  if (!$resql) {
681  $error++; $this->errors[] = "Error ".$this->db->lasterror();
682  } else {
683  $obj = $this->db->fetch_object($resql);
684  if ($obj) {
685  $ret = $obj->nb;
686  }
687  $this->db->free($resql);
688  }
689 
690  if (!$error) {
691  return $ret;
692  } else {
693  return -1;
694  }
695  }
696 
697 
710  public function getNomUrl($withpicto = 0, $option = '', $mode = 'task', $addlabel = 0, $sep = ' - ', $notooltip = 0, $save_lastsearch_value = -1)
711  {
712  global $conf, $langs, $user;
713 
714  if (!empty($conf->dol_no_mouse_hover)) {
715  $notooltip = 1; // Force disable tooltips
716  }
717 
718  $result = '';
719  $label = img_picto('', $this->picto).' <u>'.$langs->trans("Task").'</u>';
720  if (!empty($this->ref)) {
721  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
722  }
723  if (!empty($this->label)) {
724  $label .= '<br><b>'.$langs->trans('LabelTask').':</b> '.$this->label;
725  }
726  if ($this->date_start || $this->date_end) {
727  $label .= "<br>".get_date_range($this->date_start, $this->date_end, '', $langs, 0);
728  }
729 
730  $url = DOL_URL_ROOT.'/projet/tasks/'.$mode.'.php?id='.$this->id.($option == 'withproject' ? '&withproject=1' : '');
731  // Add param to save lastsearch_values or not
732  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
733  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
734  $add_save_lastsearch_values = 1;
735  }
736  if ($add_save_lastsearch_values) {
737  $url .= '&save_lastsearch_values=1';
738  }
739 
740  $linkclose = '';
741  if (empty($notooltip)) {
742  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
743  $label = $langs->trans("ShowTask");
744  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
745  }
746  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
747  $linkclose .= ' class="classfortooltip nowraponall"';
748  } else {
749  $linkclose .= ' class="nowraponall"';
750  }
751 
752  $linkstart = '<a href="'.$url.'"';
753  $linkstart .= $linkclose.'>';
754  $linkend = '</a>';
755 
756  $picto = 'projecttask';
757 
758  $result .= $linkstart;
759  if ($withpicto) {
760  $result .= img_object(($notooltip ? '' : $label), $picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
761  }
762  if ($withpicto != 2) {
763  $result .= $this->ref;
764  }
765  $result .= $linkend;
766  if ($withpicto != 2) {
767  $result .= (($addlabel && $this->label) ? $sep.dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
768  }
769 
770  return $result;
771  }
772 
780  public function initAsSpecimen()
781  {
782  $this->id = 0;
783 
784  $this->fk_project = '';
785  $this->ref = 'TK01';
786  $this->fk_task_parent = null;
787  $this->label = 'Specimen task TK01';
788  $this->duration_effective = '';
789  $this->fk_user_creat = null;
790  $this->progress = '25';
791  $this->fk_statut = null;
792  $this->note = 'This is a specimen task not';
793  }
794 
815  public function getTasksArray($usert = null, $userp = null, $projectid = 0, $socid = 0, $mode = 0, $filteronproj = '', $filteronprojstatus = '-1', $morewherefilter = '', $filteronprojuser = 0, $filterontaskuser = 0, $extrafields = array(), $includebilltime = 0, $search_array_options = array(), $loadextras = 0)
816  {
817  global $conf, $hookmanager;
818 
819  $tasks = array();
820 
821  //print $usert.'-'.$userp.'-'.$projectid.'-'.$socid.'-'.$mode.'<br>';
822 
823  // List of tasks (does not care about permissions. Filtering will be done later)
824  $sql = "SELECT ";
825  if ($filteronprojuser > 0 || $filterontaskuser > 0) {
826  $sql .= " DISTINCT"; // We may get several time the same record if user has several roles on same project/task
827  }
828  $sql .= " p.rowid as projectid, p.ref, p.title as plabel, p.public, p.fk_statut as projectstatus, p.usage_bill_time,";
829  $sql .= " t.rowid as taskid, t.ref as taskref, t.label, t.description, t.fk_task_parent, t.duration_effective, t.progress, t.fk_statut as status,";
830  $sql .= " t.dateo as date_start, t.datee as date_end, t.planned_workload, t.rang,";
831  $sql .= " t.description, ";
832  $sql .= " t.budget_amount, ";
833  $sql .= " s.rowid as thirdparty_id, s.nom as thirdparty_name, s.email as thirdparty_email,";
834  $sql .= " p.fk_opp_status, p.opp_amount, p.opp_percent, p.budget_amount as project_budget_amount";
835  if (!empty($extrafields->attributes['projet']['label'])) {
836  foreach ($extrafields->attributes['projet']['label'] as $key => $val) {
837  $sql .= ($extrafields->attributes['projet']['type'][$key] != 'separate' ? ",efp.".$key." as options_".$key : '');
838  }
839  }
840  if (!empty($extrafields->attributes['projet_task']['label'])) {
841  foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) {
842  $sql .= ($extrafields->attributes['projet_task']['type'][$key] != 'separate' ? ",efpt.".$key." as options_".$key : '');
843  }
844  }
845  if ($includebilltime) {
846  $sql .= ", SUM(tt.task_duration * ".$this->db->ifsql("invoice_id IS NULL", "1", "0").") as tobill, SUM(tt.task_duration * ".$this->db->ifsql("invoice_id IS NULL", "0", "1").") as billed";
847  }
848 
849  $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
850  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
851  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_extrafields as efp ON (p.rowid = efp.fk_object)";
852 
853  if ($mode == 0) {
854  if ($filteronprojuser > 0) {
855  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
856  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
857  }
858  $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
859  if ($includebilltime) {
860  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_time as tt ON tt.fk_task = t.rowid";
861  }
862  if ($filterontaskuser > 0) {
863  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec2";
864  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc2";
865  }
866  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_extrafields as efpt ON (t.rowid = efpt.fk_object)";
867  $sql .= " WHERE p.entity IN (".getEntity('project').")";
868  $sql .= " AND t.fk_projet = p.rowid";
869  } elseif ($mode == 1) {
870  if ($filteronprojuser > 0) {
871  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
872  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
873  }
874  if ($filterontaskuser > 0) {
875  $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
876  if ($includebilltime) {
877  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_time as tt ON tt.fk_task = t.rowid";
878  }
879  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec2";
880  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc2";
881  } else {
882  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t on t.fk_projet = p.rowid";
883  if ($includebilltime) {
884  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_time as tt ON tt.fk_task = t.rowid";
885  }
886  }
887  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_extrafields as efpt ON (t.rowid = efpt.fk_object)";
888  $sql .= " WHERE p.entity IN (".getEntity('project').")";
889  } else {
890  return 'BadValueForParameterMode';
891  }
892 
893  if ($filteronprojuser > 0) {
894  $sql .= " AND p.rowid = ec.element_id";
895  $sql .= " AND ctc.rowid = ec.fk_c_type_contact";
896  $sql .= " AND ctc.element = 'project'";
897  $sql .= " AND ec.fk_socpeople = ".((int) $filteronprojuser);
898  $sql .= " AND ec.statut = 4";
899  $sql .= " AND ctc.source = 'internal'";
900  }
901  if ($filterontaskuser > 0) {
902  $sql .= " AND t.fk_projet = p.rowid";
903  $sql .= " AND p.rowid = ec2.element_id";
904  $sql .= " AND ctc2.rowid = ec2.fk_c_type_contact";
905  $sql .= " AND ctc2.element = 'project_task'";
906  $sql .= " AND ec2.fk_socpeople = ".((int) $filterontaskuser);
907  $sql .= " AND ec2.statut = 4";
908  $sql .= " AND ctc2.source = 'internal'";
909  }
910  if ($socid) {
911  $sql .= " AND p.fk_soc = ".((int) $socid);
912  }
913  if ($projectid) {
914  $sql .= " AND p.rowid IN (".$this->db->sanitize($projectid).")";
915  }
916  if ($filteronproj) {
917  $sql .= natural_search(array("p.ref", "p.title"), $filteronproj);
918  }
919  if ($filteronprojstatus && $filteronprojstatus != '-1') {
920  $sql .= " AND p.fk_statut IN (".$this->db->sanitize($filteronprojstatus).")";
921  }
922  if ($morewherefilter) {
923  $sql .= $morewherefilter;
924  }
925  // Add where from extra fields
926  $extrafieldsobjectkey = 'projet_task';
927  $extrafieldsobjectprefix = 'efpt.';
928  global $db; // needed for extrafields_list_search_sql.tpl
929  include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
930  // Add where from hooks
931  $parameters = array();
932  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
933  $sql .= $hookmanager->resPrint;
934  if ($includebilltime) {
935  $sql .= " GROUP BY p.rowid, p.ref, p.title, p.public, p.fk_statut, p.usage_bill_time,";
936  $sql .= " t.datec, t.dateo, t.datee, t.tms,";
937  $sql .= " t.rowid, t.ref, t.label, t.description, t.fk_task_parent, t.duration_effective, t.progress, t.fk_statut,";
938  $sql .= " t.dateo, t.datee, t.planned_workload, t.rang,";
939  $sql .= " t.description, ";
940  $sql .= " t.budget_amount, ";
941  $sql .= " s.rowid, s.nom, s.email,";
942  $sql .= " p.fk_opp_status, p.opp_amount, p.opp_percent, p.budget_amount";
943  if (!empty($extrafields->attributes['projet']['label'])) {
944  foreach ($extrafields->attributes['projet']['label'] as $key => $val) {
945  $sql .= ($extrafields->attributes['projet']['type'][$key] != 'separate' ? ",efp.".$key : '');
946  }
947  }
948  if (!empty($extrafields->attributes['projet_task']['label'])) {
949  foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) {
950  $sql .= ($extrafields->attributes['projet_task']['type'][$key] != 'separate' ? ",efpt.".$key : '');
951  }
952  }
953  }
954 
955 
956  $sql .= " ORDER BY p.ref, t.rang, t.dateo";
957 
958  //print $sql;exit;
959  dol_syslog(get_class($this)."::getTasksArray", LOG_DEBUG);
960  $resql = $this->db->query($sql);
961  if ($resql) {
962  $num = $this->db->num_rows($resql);
963  $i = 0;
964  // Loop on each record found, so each couple (project id, task id)
965  while ($i < $num) {
966  $error = 0;
967 
968  $obj = $this->db->fetch_object($resql);
969 
970  if ((!$obj->public) && (is_object($userp))) { // If not public project and we ask a filter on project owned by a user
971  if (!$this->getUserRolesForProjectsOrTasks($userp, 0, $obj->projectid, 0)) {
972  $error++;
973  }
974  }
975  if (is_object($usert)) { // If we ask a filter on a user affected to a task
976  if (!$this->getUserRolesForProjectsOrTasks(0, $usert, $obj->projectid, $obj->taskid)) {
977  $error++;
978  }
979  }
980 
981  if (!$error) {
982  $tasks[$i] = new Task($this->db);
983  $tasks[$i]->id = $obj->taskid;
984  $tasks[$i]->ref = $obj->taskref;
985  $tasks[$i]->fk_project = $obj->projectid;
986  $tasks[$i]->projectref = $obj->ref;
987  $tasks[$i]->projectlabel = $obj->plabel;
988  $tasks[$i]->projectstatus = $obj->projectstatus;
989 
990  $tasks[$i]->fk_opp_status = $obj->fk_opp_status;
991  $tasks[$i]->opp_amount = $obj->opp_amount;
992  $tasks[$i]->opp_percent = $obj->opp_percent;
993  $tasks[$i]->budget_amount = $obj->budget_amount;
994  $tasks[$i]->project_budget_amount = $obj->project_budget_amount;
995  $tasks[$i]->usage_bill_time = $obj->usage_bill_time;
996 
997  $tasks[$i]->label = $obj->label;
998  $tasks[$i]->description = $obj->description;
999  $tasks[$i]->fk_parent = $obj->fk_task_parent; // deprecated
1000  $tasks[$i]->fk_task_parent = $obj->fk_task_parent;
1001  $tasks[$i]->duration = $obj->duration_effective;
1002  $tasks[$i]->planned_workload = $obj->planned_workload;
1003 
1004  if ($includebilltime) {
1005  $tasks[$i]->tobill = $obj->tobill;
1006  $tasks[$i]->billed = $obj->billed;
1007  }
1008 
1009  $tasks[$i]->progress = $obj->progress;
1010  $tasks[$i]->fk_statut = $obj->status;
1011  $tasks[$i]->public = $obj->public;
1012  $tasks[$i]->date_start = $this->db->jdate($obj->date_start);
1013  $tasks[$i]->date_end = $this->db->jdate($obj->date_end);
1014  $tasks[$i]->rang = $obj->rang;
1015 
1016  $tasks[$i]->socid = $obj->thirdparty_id; // For backward compatibility
1017  $tasks[$i]->thirdparty_id = $obj->thirdparty_id;
1018  $tasks[$i]->thirdparty_name = $obj->thirdparty_name;
1019  $tasks[$i]->thirdparty_email = $obj->thirdparty_email;
1020 
1021  if (!empty($extrafields->attributes['projet']['label'])) {
1022  foreach ($extrafields->attributes['projet']['label'] as $key => $val) {
1023  if ($extrafields->attributes['projet']['type'][$key] != 'separate') {
1024  $tasks[$i]->{'options_'.$key} = $obj->{'options_'.$key};
1025  }
1026  }
1027  }
1028 
1029  if (!empty($extrafields->attributes['projet_task']['label'])) {
1030  foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) {
1031  if ($extrafields->attributes['projet_task']['type'][$key] != 'separate') {
1032  $tasks[$i]->{'options_'.$key} = $obj->{'options_'.$key};
1033  }
1034  }
1035  }
1036 
1037  if ($loadextras) {
1038  $tasks[$i]->fetch_optionals();
1039  }
1040  }
1041 
1042  $i++;
1043  }
1044  $this->db->free($resql);
1045  } else {
1046  dol_print_error($this->db);
1047  }
1048 
1049  return $tasks;
1050  }
1051 
1062  public function getUserRolesForProjectsOrTasks($userp, $usert, $projectid = '', $taskid = 0, $filteronprojstatus = -1)
1063  {
1064  $arrayroles = array();
1065 
1066  dol_syslog(get_class($this)."::getUserRolesForProjectsOrTasks userp=".is_object($userp)." usert=".is_object($usert)." projectid=".$projectid." taskid=".$taskid);
1067 
1068  // We want role of user for a projet or role of user for a task. Both are not possible.
1069  if (empty($userp) && empty($usert)) {
1070  $this->error = "CallWithWrongParameters";
1071  return -1;
1072  }
1073  if (!empty($userp) && !empty($usert)) {
1074  $this->error = "CallWithWrongParameters";
1075  return -1;
1076  }
1077 
1078  /* Liste des taches et role sur les projets ou taches */
1079  $sql = "SELECT pt.rowid as pid, ec.element_id, ctc.code, ctc.source";
1080  if ($userp) {
1081  $sql .= " FROM ".MAIN_DB_PREFIX."projet as pt";
1082  }
1083  if ($usert && $filteronprojstatus > -1) {
1084  $sql .= " FROM ".MAIN_DB_PREFIX."projet as p, ".MAIN_DB_PREFIX."projet_task as pt";
1085  }
1086  if ($usert && $filteronprojstatus <= -1) {
1087  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as pt";
1088  }
1089  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
1090  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
1091  $sql .= " WHERE pt.rowid = ec.element_id";
1092  if ($userp && $filteronprojstatus > -1) {
1093  $sql .= " AND pt.fk_statut = ".((int) $filteronprojstatus);
1094  }
1095  if ($usert && $filteronprojstatus > -1) {
1096  $sql .= " AND pt.fk_projet = p.rowid AND p.fk_statut = ".((int) $filteronprojstatus);
1097  }
1098  if ($userp) {
1099  $sql .= " AND ctc.element = 'project'";
1100  }
1101  if ($usert) {
1102  $sql .= " AND ctc.element = 'project_task'";
1103  }
1104  $sql .= " AND ctc.rowid = ec.fk_c_type_contact";
1105  if ($userp) {
1106  $sql .= " AND ec.fk_socpeople = ".((int) $userp->id);
1107  }
1108  if ($usert) {
1109  $sql .= " AND ec.fk_socpeople = ".((int) $usert->id);
1110  }
1111  $sql .= " AND ec.statut = 4";
1112  $sql .= " AND ctc.source = 'internal'";
1113  if ($projectid) {
1114  if ($userp) {
1115  $sql .= " AND pt.rowid IN (".$this->db->sanitize($projectid).")";
1116  }
1117  if ($usert) {
1118  $sql .= " AND pt.fk_projet IN (".$this->db->sanitize($projectid).")";
1119  }
1120  }
1121  if ($taskid) {
1122  if ($userp) {
1123  $sql .= " ERROR SHOULD NOT HAPPENS";
1124  }
1125  if ($usert) {
1126  $sql .= " AND pt.rowid = ".((int) $taskid);
1127  }
1128  }
1129  //print $sql;
1130 
1131  dol_syslog(get_class($this)."::getUserRolesForProjectsOrTasks execute request", LOG_DEBUG);
1132  $resql = $this->db->query($sql);
1133  if ($resql) {
1134  $num = $this->db->num_rows($resql);
1135  $i = 0;
1136  while ($i < $num) {
1137  $obj = $this->db->fetch_object($resql);
1138  if (empty($arrayroles[$obj->pid])) {
1139  $arrayroles[$obj->pid] = $obj->code;
1140  } else {
1141  $arrayroles[$obj->pid] .= ','.$obj->code;
1142  }
1143  $i++;
1144  }
1145  $this->db->free($resql);
1146  } else {
1147  dol_print_error($this->db);
1148  }
1149 
1150  return $arrayroles;
1151  }
1152 
1153 
1160  public function getListContactId($source = 'internal')
1161  {
1162  $contactAlreadySelected = array();
1163  $tab = $this->liste_contact(-1, $source);
1164  //var_dump($tab);
1165  $num = count($tab);
1166  $i = 0;
1167  while ($i < $num) {
1168  if ($source == 'thirdparty') {
1169  $contactAlreadySelected[$i] = $tab[$i]['socid'];
1170  } else {
1171  $contactAlreadySelected[$i] = $tab[$i]['id'];
1172  }
1173  $i++;
1174  }
1175  return $contactAlreadySelected;
1176  }
1177 
1178 
1186  public function addTimeSpent($user, $notrigger = 0)
1187  {
1188  global $conf, $langs;
1189 
1190  dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1191 
1192  $ret = 0;
1193  $now = dol_now();
1194 
1195  // Check parameters
1196  if (!is_object($user)) {
1197  dol_print_error('', "Method addTimeSpent was called with wrong parameter user");
1198  return -1;
1199  }
1200 
1201  // Clean parameters
1202  if (isset($this->timespent_note)) {
1203  $this->timespent_note = trim($this->timespent_note);
1204  }
1205  if (empty($this->timespent_datehour)) {
1206  $this->timespent_datehour = $this->timespent_date;
1207  }
1208 
1209  if (! empty($conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS)) {
1210  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
1211  $restrictBefore = dol_time_plus_duree(dol_now(), - $conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS, 'm');
1212 
1213  if ($this->timespent_date < $restrictBefore) {
1214  $this->error = $langs->trans('TimeRecordingRestrictedToNMonthsBack', $conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS);
1215  $this->errors[] = $this->error;
1216  return -1;
1217  }
1218  }
1219 
1220 
1221  $this->db->begin();
1222 
1223  $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet_task_time (";
1224  $sql .= "fk_task";
1225  $sql .= ", task_date";
1226  $sql .= ", task_datehour";
1227  $sql .= ", task_date_withhour";
1228  $sql .= ", task_duration";
1229  $sql .= ", fk_user";
1230  $sql .= ", note";
1231  $sql .= ", datec";
1232  $sql .= ") VALUES (";
1233  $sql .= ((int) $this->id);
1234  $sql .= ", '".$this->db->idate($this->timespent_date)."'";
1235  $sql .= ", '".$this->db->idate($this->timespent_datehour)."'";
1236  $sql .= ", ".(empty($this->timespent_withhour) ? 0 : 1);
1237  $sql .= ", ".((int) $this->timespent_duration);
1238  $sql .= ", ".((int) $this->timespent_fk_user);
1239  $sql .= ", ".(isset($this->timespent_note) ? "'".$this->db->escape($this->timespent_note)."'" : "null");
1240  $sql .= ", '".$this->db->idate($now)."'";
1241  $sql .= ")";
1242 
1243  $resql = $this->db->query($sql);
1244  if ($resql) {
1245  $tasktime_id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task_time");
1246  $ret = $tasktime_id;
1247  $this->timespent_id = $ret;
1248 
1249  if (!$notrigger) {
1250  // Call trigger
1251  $result = $this->call_trigger('TASK_TIMESPENT_CREATE', $user);
1252  if ($result < 0) {
1253  $ret = -1;
1254  }
1255  // End call triggers
1256  }
1257  } else {
1258  $this->error = $this->db->lasterror();
1259  $ret = -1;
1260  }
1261 
1262  if ($ret > 0) {
1263  // Recalculate amount of time spent for task and update denormalized field
1264  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
1265  $sql .= " SET duration_effective = (SELECT SUM(task_duration) FROM ".MAIN_DB_PREFIX."projet_task_time as ptt where ptt.fk_task = ".((int) $this->id).")";
1266  if (isset($this->progress)) {
1267  $sql .= ", progress = ".((float) $this->progress); // Do not overwrite value if not provided
1268  }
1269  $sql .= " WHERE rowid = ".((int) $this->id);
1270 
1271  dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1272  if (!$this->db->query($sql)) {
1273  $this->error = $this->db->lasterror();
1274  $ret = -2;
1275  }
1276 
1277  // Update hourly rate of this time spent entry
1278  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task_time";
1279  $sql .= " SET thm = (SELECT thm FROM ".MAIN_DB_PREFIX."user WHERE rowid = ".((int) $this->timespent_fk_user).")"; // set average hour rate of user
1280  $sql .= " WHERE rowid = ".((int) $tasktime_id);
1281 
1282  dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1283  if (!$this->db->query($sql)) {
1284  $this->error = $this->db->lasterror();
1285  $ret = -2;
1286  }
1287  }
1288 
1289  if ($ret > 0) {
1290  $this->db->commit();
1291  } else {
1292  $this->db->rollback();
1293  }
1294  return $ret;
1295  }
1296 
1303  public function fetchTimeSpentOnTask($morewherefilter = '')
1304  {
1305  global $langs;
1306 
1307  $arrayres = array();
1308 
1309  $sql = "SELECT";
1310  $sql .= " s.rowid as socid,";
1311  $sql .= " s.nom as thirdparty_name,";
1312  $sql .= " s.email as thirdparty_email,";
1313  $sql .= " ptt.rowid,";
1314  $sql .= " ptt.fk_task,";
1315  $sql .= " ptt.task_date,";
1316  $sql .= " ptt.task_datehour,";
1317  $sql .= " ptt.task_date_withhour,";
1318  $sql .= " ptt.task_duration,";
1319  $sql .= " ptt.fk_user,";
1320  $sql .= " ptt.note,";
1321  $sql .= " ptt.thm,";
1322  $sql .= " pt.rowid as task_id,";
1323  $sql .= " pt.ref as task_ref,";
1324  $sql .= " pt.label as task_label,";
1325  $sql .= " p.rowid as project_id,";
1326  $sql .= " p.ref as project_ref,";
1327  $sql .= " p.title as project_label,";
1328  $sql .= " p.public as public";
1329  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as ptt, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."projet as p";
1330  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
1331  $sql .= " WHERE ptt.fk_task = pt.rowid AND pt.fk_projet = p.rowid";
1332  $sql .= " AND pt.rowid = ".((int) $this->id);
1333  $sql .= " AND pt.entity IN (".getEntity('project').")";
1334  if ($morewherefilter) {
1335  $sql .= $morewherefilter;
1336  }
1337 
1338  dol_syslog(get_class($this)."::fetchAllTimeSpent", LOG_DEBUG);
1339  $resql = $this->db->query($sql);
1340  if ($resql) {
1341  $num = $this->db->num_rows($resql);
1342 
1343  $i = 0;
1344  while ($i < $num) {
1345  $obj = $this->db->fetch_object($resql);
1346 
1347  $newobj = new stdClass();
1348 
1349  $newobj->socid = $obj->socid;
1350  $newobj->thirdparty_name = $obj->thirdparty_name;
1351  $newobj->thirdparty_email = $obj->thirdparty_email;
1352 
1353  $newobj->fk_project = $obj->project_id;
1354  $newobj->project_ref = $obj->project_ref;
1355  $newobj->project_label = $obj->project_label;
1356  $newobj->public = $obj->project_public;
1357 
1358  $newobj->fk_task = $obj->task_id;
1359  $newobj->task_ref = $obj->task_ref;
1360  $newobj->task_label = $obj->task_label;
1361 
1362  $newobj->timespent_line_id = $obj->rowid;
1363  $newobj->timespent_line_date = $this->db->jdate($obj->task_date);
1364  $newobj->timespent_line_datehour = $this->db->jdate($obj->task_datehour);
1365  $newobj->timespent_line_withhour = $obj->task_date_withhour;
1366  $newobj->timespent_line_duration = $obj->task_duration;
1367  $newobj->timespent_line_fk_user = $obj->fk_user;
1368  $newobj->timespent_line_thm = $obj->thm; // hourly rate
1369  $newobj->timespent_line_note = $obj->note;
1370 
1371  $arrayres[] = $newobj;
1372 
1373  $i++;
1374  }
1375 
1376  $this->db->free($resql);
1377 
1378  $this->lines = $arrayres;
1379  return 1;
1380  } else {
1381  dol_print_error($this->db);
1382  $this->error = "Error ".$this->db->lasterror();
1383  return -1;
1384  }
1385  }
1386 
1387 
1395  public function getSummaryOfTimeSpent($userobj = null, $morewherefilter = '')
1396  {
1397  global $langs;
1398 
1399  if (is_object($userobj)) {
1400  $userid = $userobj->id;
1401  } else {
1402  $userid = $userobj; // old method
1403  }
1404 
1405  $id = $this->id;
1406  if (empty($id) && empty($userid)) {
1407  dol_syslog("getSummaryOfTimeSpent called on a not loaded task without user param defined", LOG_ERR);
1408  return -1;
1409  }
1410 
1411  $result = array();
1412 
1413  $sql = "SELECT";
1414  $sql .= " MIN(t.task_datehour) as min_date,";
1415  $sql .= " MAX(t.task_datehour) as max_date,";
1416  $sql .= " SUM(t.task_duration) as total_duration,";
1417  $sql .= " SUM(t.task_duration / 3600 * ".$this->db->ifsql("t.thm IS NULL", 0, "t.thm").") as total_amount,";
1418  $sql .= " COUNT(t.rowid) as nblines,";
1419  $sql .= " SUM(".$this->db->ifsql("t.thm IS NULL", 1, 0).") as nblinesnull";
1420  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
1421  $sql .= " WHERE 1 = 1";
1422  if ($morewherefilter) {
1423  $sql .= $morewherefilter;
1424  }
1425  if ($id > 0) {
1426  $sql .= " AND t.fk_task = ".((int) $id);
1427  }
1428  if ($userid > 0) {
1429  $sql .= " AND t.fk_user = ".((int) $userid);
1430  }
1431 
1432  dol_syslog(get_class($this)."::getSummaryOfTimeSpent", LOG_DEBUG);
1433  $resql = $this->db->query($sql);
1434  if ($resql) {
1435  $obj = $this->db->fetch_object($resql);
1436 
1437  $result['min_date'] = $obj->min_date; // deprecated. use the ->timespent_xxx instead
1438  $result['max_date'] = $obj->max_date; // deprecated. use the ->timespent_xxx instead
1439  $result['total_duration'] = $obj->total_duration; // deprecated. use the ->timespent_xxx instead
1440 
1441  $this->timespent_min_date = $this->db->jdate($obj->min_date);
1442  $this->timespent_max_date = $this->db->jdate($obj->max_date);
1443  $this->timespent_total_duration = $obj->total_duration;
1444  $this->timespent_total_amount = $obj->total_amount;
1445  $this->timespent_nblinesnull = ($obj->nblinesnull ? $obj->nblinesnull : 0);
1446  $this->timespent_nblines = ($obj->nblines ? $obj->nblines : 0);
1447 
1448  $this->db->free($resql);
1449  } else {
1450  dol_print_error($this->db);
1451  }
1452  return $result;
1453  }
1454 
1463  public function getSumOfAmount($fuser = '', $dates = '', $datee = '')
1464  {
1465  global $langs;
1466 
1467  if (empty($id)) {
1468  $id = $this->id;
1469  }
1470 
1471  $result = array();
1472 
1473  $sql = "SELECT";
1474  $sql .= " SUM(t.task_duration) as nbseconds,";
1475  $sql .= " SUM(t.task_duration / 3600 * ".$this->db->ifsql("t.thm IS NULL", 0, "t.thm").") as amount, SUM(".$this->db->ifsql("t.thm IS NULL", 1, 0).") as nblinesnull";
1476  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
1477  $sql .= " WHERE t.fk_task = ".((int) $id);
1478  if (is_object($fuser) && $fuser->id > 0) {
1479  $sql .= " AND fk_user = ".((int) $fuser->id);
1480  }
1481  if ($dates > 0) {
1482  $datefieldname = "task_datehour";
1483  $sql .= " AND (".$datefieldname." >= '".$this->db->idate($dates)."' OR ".$datefieldname." IS NULL)";
1484  }
1485  if ($datee > 0) {
1486  $datefieldname = "task_datehour";
1487  $sql .= " AND (".$datefieldname." <= '".$this->db->idate($datee)."' OR ".$datefieldname." IS NULL)";
1488  }
1489  //print $sql;
1490 
1491  dol_syslog(get_class($this)."::getSumOfAmount", LOG_DEBUG);
1492  $resql = $this->db->query($sql);
1493  if ($resql) {
1494  $obj = $this->db->fetch_object($resql);
1495 
1496  $result['amount'] = $obj->amount;
1497  $result['nbseconds'] = $obj->nbseconds;
1498  $result['nblinesnull'] = $obj->nblinesnull;
1499 
1500  $this->db->free($resql);
1501  return $result;
1502  } else {
1503  dol_print_error($this->db);
1504  return $result;
1505  }
1506  }
1507 
1514  public function fetchTimeSpent($id)
1515  {
1516  global $langs;
1517 
1518  $sql = "SELECT";
1519  $sql .= " t.rowid,";
1520  $sql .= " t.fk_task,";
1521  $sql .= " t.task_date,";
1522  $sql .= " t.task_datehour,";
1523  $sql .= " t.task_date_withhour,";
1524  $sql .= " t.task_duration,";
1525  $sql .= " t.fk_user,";
1526  $sql .= " t.thm,";
1527  $sql .= " t.note";
1528  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
1529  $sql .= " WHERE t.rowid = ".((int) $id);
1530 
1531  dol_syslog(get_class($this)."::fetchTimeSpent", LOG_DEBUG);
1532  $resql = $this->db->query($sql);
1533  if ($resql) {
1534  if ($this->db->num_rows($resql)) {
1535  $obj = $this->db->fetch_object($resql);
1536 
1537  $this->timespent_id = $obj->rowid;
1538  $this->id = $obj->fk_task;
1539  $this->timespent_date = $this->db->jdate($obj->task_date);
1540  $this->timespent_datehour = $this->db->jdate($obj->task_datehour);
1541  $this->timespent_withhour = $obj->task_date_withhour;
1542  $this->timespent_duration = $obj->task_duration;
1543  $this->timespent_fk_user = $obj->fk_user;
1544  $this->timespent_thm = $obj->thm; // hourly rate
1545  $this->timespent_note = $obj->note;
1546  }
1547 
1548  $this->db->free($resql);
1549 
1550  return 1;
1551  } else {
1552  $this->error = "Error ".$this->db->lasterror();
1553  return -1;
1554  }
1555  }
1556 
1564  public function fetchAllTimeSpent(User $userobj, $morewherefilter = '')
1565  {
1566  global $langs;
1567 
1568  $arrayres = array();
1569 
1570  $sql = "SELECT";
1571  $sql .= " s.rowid as socid,";
1572  $sql .= " s.nom as thirdparty_name,";
1573  $sql .= " s.email as thirdparty_email,";
1574  $sql .= " ptt.rowid,";
1575  $sql .= " ptt.fk_task,";
1576  $sql .= " ptt.task_date,";
1577  $sql .= " ptt.task_datehour,";
1578  $sql .= " ptt.task_date_withhour,";
1579  $sql .= " ptt.task_duration,";
1580  $sql .= " ptt.fk_user,";
1581  $sql .= " ptt.note,";
1582  $sql .= " ptt.thm,";
1583  $sql .= " pt.rowid as task_id,";
1584  $sql .= " pt.ref as task_ref,";
1585  $sql .= " pt.label as task_label,";
1586  $sql .= " p.rowid as project_id,";
1587  $sql .= " p.ref as project_ref,";
1588  $sql .= " p.title as project_label,";
1589  $sql .= " p.public as public";
1590  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as ptt, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."projet as p";
1591  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
1592  $sql .= " WHERE ptt.fk_task = pt.rowid AND pt.fk_projet = p.rowid";
1593  $sql .= " AND ptt.fk_user = ".((int) $userobj->id);
1594  $sql .= " AND pt.entity IN (".getEntity('project').")";
1595  if ($morewherefilter) {
1596  $sql .= $morewherefilter;
1597  }
1598 
1599  dol_syslog(get_class($this)."::fetchAllTimeSpent", LOG_DEBUG);
1600  $resql = $this->db->query($sql);
1601  if ($resql) {
1602  $num = $this->db->num_rows($resql);
1603 
1604  $i = 0;
1605  while ($i < $num) {
1606  $obj = $this->db->fetch_object($resql);
1607 
1608  $newobj = new stdClass();
1609 
1610  $newobj->socid = $obj->socid;
1611  $newobj->thirdparty_name = $obj->thirdparty_name;
1612  $newobj->thirdparty_email = $obj->thirdparty_email;
1613 
1614  $newobj->fk_project = $obj->project_id;
1615  $newobj->project_ref = $obj->project_ref;
1616  $newobj->project_label = $obj->project_label;
1617  $newobj->public = $obj->project_public;
1618 
1619  $newobj->fk_task = $obj->task_id;
1620  $newobj->task_ref = $obj->task_ref;
1621  $newobj->task_label = $obj->task_label;
1622 
1623  $newobj->timespent_id = $obj->rowid;
1624  $newobj->timespent_date = $this->db->jdate($obj->task_date);
1625  $newobj->timespent_datehour = $this->db->jdate($obj->task_datehour);
1626  $newobj->timespent_withhour = $obj->task_date_withhour;
1627  $newobj->timespent_duration = $obj->task_duration;
1628  $newobj->timespent_fk_user = $obj->fk_user;
1629  $newobj->timespent_thm = $obj->thm; // hourly rate
1630  $newobj->timespent_note = $obj->note;
1631 
1632  $arrayres[] = $newobj;
1633 
1634  $i++;
1635  }
1636 
1637  $this->db->free($resql);
1638  } else {
1639  dol_print_error($this->db);
1640  $this->error = "Error ".$this->db->lasterror();
1641  return -1;
1642  }
1643 
1644  return $arrayres;
1645  }
1646 
1654  public function updateTimeSpent($user, $notrigger = 0)
1655  {
1656  global $conf, $langs;
1657 
1658  $ret = 0;
1659 
1660  // Check parameters
1661  if ($this->timespent_date == '') {
1662  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Date"));
1663  return -1;
1664  }
1665  if (!($this->timespent_fk_user > 0)) {
1666  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("User"));
1667  return -1;
1668  }
1669 
1670  // Clean parameters
1671  if (empty($this->timespent_datehour)) {
1672  $this->timespent_datehour = $this->timespent_date;
1673  }
1674  if (isset($this->timespent_note)) {
1675  $this->timespent_note = trim($this->timespent_note);
1676  }
1677 
1678  if (! empty($conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS)) {
1679  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
1680  $restrictBefore = dol_time_plus_duree(dol_now(), - $conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS, 'm');
1681 
1682  if ($this->timespent_date < $restrictBefore) {
1683  $this->error = $langs->trans('TimeRecordingRestrictedToNMonthsBack', $conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS);
1684  $this->errors[] = $this->error;
1685  return -1;
1686  }
1687  }
1688 
1689  $this->db->begin();
1690 
1691  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task_time SET";
1692  $sql .= " task_date = '".$this->db->idate($this->timespent_date)."',";
1693  $sql .= " task_datehour = '".$this->db->idate($this->timespent_datehour)."',";
1694  $sql .= " task_date_withhour = ".(empty($this->timespent_withhour) ? 0 : 1).",";
1695  $sql .= " task_duration = ".((int) $this->timespent_duration).",";
1696  $sql .= " fk_user = ".((int) $this->timespent_fk_user).",";
1697  $sql .= " note = ".(isset($this->timespent_note) ? "'".$this->db->escape($this->timespent_note)."'" : "null");
1698  $sql .= " WHERE rowid = ".((int) $this->timespent_id);
1699 
1700  dol_syslog(get_class($this)."::updateTimeSpent", LOG_DEBUG);
1701  if ($this->db->query($sql)) {
1702  if (!$notrigger) {
1703  // Call trigger
1704  $result = $this->call_trigger('TASK_TIMESPENT_MODIFY', $user);
1705  if ($result < 0) {
1706  $this->db->rollback();
1707  $ret = -1;
1708  } else {
1709  $ret = 1;
1710  }
1711  // End call triggers
1712  } else {
1713  $ret = 1;
1714  }
1715  } else {
1716  $this->error = $this->db->lasterror();
1717  $this->db->rollback();
1718  $ret = -1;
1719  }
1720 
1721  if ($ret == 1 && ($this->timespent_old_duration != $this->timespent_duration)) {
1722  // Recalculate amount of time spent for task and update denormalized field
1723  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
1724  $sql .= " SET duration_effective = (SELECT SUM(task_duration) FROM ".MAIN_DB_PREFIX."projet_task_time as ptt where ptt.fk_task = ".((int) $this->id).")";
1725  if (isset($this->progress)) {
1726  $sql .= ", progress = ".((float) $this->progress); // Do not overwrite value if not provided
1727  }
1728  $sql .= " WHERE rowid = ".((int) $this->id);
1729 
1730  dol_syslog(get_class($this)."::updateTimeSpent", LOG_DEBUG);
1731  if (!$this->db->query($sql)) {
1732  $this->error = $this->db->lasterror();
1733  $this->db->rollback();
1734  $ret = -2;
1735  }
1736 
1737  // Update hourly rate of this time spent entry, but only if it was not set initialy
1738  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task_time";
1739  $sql .= " SET thm = (SELECT thm FROM ".MAIN_DB_PREFIX."user WHERE rowid = ".((int) $this->timespent_fk_user).")"; // set average hour rate of user
1740  $sql .= " WHERE (thm IS NULL OR thm = 0) AND rowid = ".((int) $this->timespent_id);
1741 
1742  dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1743  if (!$this->db->query($sql)) {
1744  $this->error = $this->db->lasterror();
1745  $ret = -2;
1746  }
1747  }
1748 
1749  if ($ret >= 0) {
1750  $this->db->commit();
1751  }
1752  return $ret;
1753  }
1754 
1762  public function delTimeSpent($user, $notrigger = 0)
1763  {
1764  global $conf, $langs;
1765 
1766  $error = 0;
1767 
1768  if (! empty($conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS)) {
1769  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
1770  $restrictBefore = dol_time_plus_duree(dol_now(), - $conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS, 'm');
1771 
1772  if ($this->timespent_date < $restrictBefore) {
1773  $this->error = $langs->trans('TimeRecordingRestrictedToNMonthsBack', $conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS);
1774  $this->errors[] = $this->error;
1775  return -1;
1776  }
1777  }
1778 
1779  $this->db->begin();
1780 
1781  if (!$notrigger) {
1782  // Call trigger
1783  $result = $this->call_trigger('TASK_TIMESPENT_DELETE', $user);
1784  if ($result < 0) {
1785  $error++;
1786  }
1787  // End call triggers
1788  }
1789 
1790  if (!$error) {
1791  $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_time";
1792  $sql .= " WHERE rowid = ".((int) $this->timespent_id);
1793 
1794  dol_syslog(get_class($this)."::delTimeSpent", LOG_DEBUG);
1795  $resql = $this->db->query($sql);
1796  if (!$resql) {
1797  $error++; $this->errors[] = "Error ".$this->db->lasterror();
1798  }
1799  }
1800 
1801  if (!$error) {
1802  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
1803  $sql .= " SET duration_effective = duration_effective - ".$this->db->escape($this->timespent_duration ? $this->timespent_duration : 0);
1804  $sql .= " WHERE rowid = ".((int) $this->id);
1805 
1806  dol_syslog(get_class($this)."::delTimeSpent", LOG_DEBUG);
1807  if ($this->db->query($sql)) {
1808  $result = 0;
1809  } else {
1810  $this->error = $this->db->lasterror();
1811  $result = -2;
1812  }
1813  }
1814 
1815  // Commit or rollback
1816  if ($error) {
1817  foreach ($this->errors as $errmsg) {
1818  dol_syslog(get_class($this)."::delTimeSpent ".$errmsg, LOG_ERR);
1819  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1820  }
1821  $this->db->rollback();
1822  return -1 * $error;
1823  } else {
1824  $this->db->commit();
1825  return 1;
1826  }
1827  }
1828 
1843  public function createFromClone(User $user, $fromid, $project_id, $parent_task_id, $clone_change_dt = false, $clone_affectation = false, $clone_time = false, $clone_file = false, $clone_note = false, $clone_prog = false)
1844  {
1845  global $langs, $conf;
1846 
1847  $error = 0;
1848 
1849  //Use 00:00 of today if time is use on task.
1850  $now = dol_mktime(0, 0, 0, dol_print_date(dol_now(), '%m'), dol_print_date(dol_now(), '%d'), dol_print_date(dol_now(), '%Y'));
1851 
1852  $datec = $now;
1853 
1854  $clone_task = new Task($this->db);
1855  $origin_task = new Task($this->db);
1856 
1857  $clone_task->context['createfromclone'] = 'createfromclone';
1858 
1859  $this->db->begin();
1860 
1861  // Load source object
1862  $clone_task->fetch($fromid);
1863  $clone_task->fetch_optionals();
1864  //var_dump($clone_task->array_options);exit;
1865 
1866  $origin_task->fetch($fromid);
1867 
1868  $defaultref = '';
1869  $obj = empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
1870  if (!empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.".php")) {
1871  require_once DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.'.php';
1872  $modTask = new $obj;
1873  $defaultref = $modTask->getNextValue(0, $clone_task);
1874  }
1875 
1876  $ori_project_id = $clone_task->fk_project;
1877 
1878  $clone_task->id = 0;
1879  $clone_task->ref = $defaultref;
1880  $clone_task->fk_project = $project_id;
1881  $clone_task->fk_task_parent = $parent_task_id;
1882  $clone_task->date_c = $datec;
1883  $clone_task->planned_workload = $origin_task->planned_workload;
1884  $clone_task->rang = $origin_task->rang;
1885 
1886  //Manage Task Date
1887  if ($clone_change_dt) {
1888  $projectstatic = new Project($this->db);
1889  $projectstatic->fetch($ori_project_id);
1890 
1891  //Origin project strat date
1892  $orign_project_dt_start = $projectstatic->date_start;
1893 
1894  //Calcultate new task start date with difference between origin proj start date and origin task start date
1895  if (!empty($clone_task->date_start)) {
1896  $clone_task->date_start = $now + $clone_task->date_start - $orign_project_dt_start;
1897  }
1898 
1899  //Calcultate new task end date with difference between origin proj end date and origin task end date
1900  if (!empty($clone_task->date_end)) {
1901  $clone_task->date_end = $now + $clone_task->date_end - $orign_project_dt_start;
1902  }
1903  }
1904 
1905  if (!$clone_prog) {
1906  $clone_task->progress = 0;
1907  }
1908 
1909  // Create clone
1910  $result = $clone_task->create($user);
1911 
1912  // Other options
1913  if ($result < 0) {
1914  $this->error = $clone_task->error;
1915  $error++;
1916  }
1917 
1918  // End
1919  if (!$error) {
1920  $clone_task_id = $clone_task->id;
1921  $clone_task_ref = $clone_task->ref;
1922 
1923  //Note Update
1924  if (!$clone_note) {
1925  $clone_task->note_private = '';
1926  $clone_task->note_public = '';
1927  } else {
1928  $this->db->begin();
1929  $res = $clone_task->update_note(dol_html_entity_decode($clone_task->note_public, ENT_QUOTES | ENT_HTML5), '_public');
1930  if ($res < 0) {
1931  $this->error .= $clone_task->error;
1932  $error++;
1933  $this->db->rollback();
1934  } else {
1935  $this->db->commit();
1936  }
1937 
1938  $this->db->begin();
1939  $res = $clone_task->update_note(dol_html_entity_decode($clone_task->note_private, ENT_QUOTES | ENT_HTML5), '_private');
1940  if ($res < 0) {
1941  $this->error .= $clone_task->error;
1942  $error++;
1943  $this->db->rollback();
1944  } else {
1945  $this->db->commit();
1946  }
1947  }
1948 
1949  //Duplicate file
1950  if ($clone_file) {
1951  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1952 
1953  //retrieve project origin ref to know folder to copy
1954  $projectstatic = new Project($this->db);
1955  $projectstatic->fetch($ori_project_id);
1956  $ori_project_ref = $projectstatic->ref;
1957 
1958  if ($ori_project_id != $project_id) {
1959  $projectstatic->fetch($project_id);
1960  $clone_project_ref = $projectstatic->ref;
1961  } else {
1962  $clone_project_ref = $ori_project_ref;
1963  }
1964 
1965  $clone_task_dir = $conf->project->dir_output."/".dol_sanitizeFileName($clone_project_ref)."/".dol_sanitizeFileName($clone_task_ref);
1966  $ori_task_dir = $conf->project->dir_output."/".dol_sanitizeFileName($ori_project_ref)."/".dol_sanitizeFileName($fromid);
1967 
1968  $filearray = dol_dir_list($ori_task_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', '', SORT_ASC, 1);
1969  foreach ($filearray as $key => $file) {
1970  if (!file_exists($clone_task_dir)) {
1971  if (dol_mkdir($clone_task_dir) < 0) {
1972  $this->error .= $langs->trans('ErrorInternalErrorDetected').':dol_mkdir';
1973  $error++;
1974  }
1975  }
1976 
1977  $rescopy = dol_copy($ori_task_dir.'/'.$file['name'], $clone_task_dir.'/'.$file['name'], 0, 1);
1978  if (is_numeric($rescopy) && $rescopy < 0) {
1979  $this->error .= $langs->trans("ErrorFailToCopyFile", $ori_task_dir.'/'.$file['name'], $clone_task_dir.'/'.$file['name']);
1980  $error++;
1981  }
1982  }
1983  }
1984 
1985  // clone affectation
1986  if ($clone_affectation) {
1987  $origin_task = new Task($this->db);
1988  $origin_task->fetch($fromid);
1989 
1990  foreach (array('internal', 'external') as $source) {
1991  $tab = $origin_task->liste_contact(-1, $source);
1992  $num = count($tab);
1993  $i = 0;
1994  while ($i < $num) {
1995  $clone_task->add_contact($tab[$i]['id'], $tab[$i]['code'], $tab[$i]['source']);
1996  if ($clone_task->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1997  $langs->load("errors");
1998  $this->error .= $langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType");
1999  $error++;
2000  } else {
2001  if ($clone_task->error != '') {
2002  $this->error .= $clone_task->error;
2003  $error++;
2004  }
2005  }
2006  $i++;
2007  }
2008  }
2009  }
2010 
2011  if ($clone_time) {
2012  //TODO clone time of affectation
2013  }
2014  }
2015 
2016  unset($clone_task->context['createfromclone']);
2017 
2018  if (!$error) {
2019  $this->db->commit();
2020  return $clone_task_id;
2021  } else {
2022  $this->db->rollback();
2023  dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : ".$this->error, LOG_ERR);
2024  return -1;
2025  }
2026  }
2027 
2028 
2035  public function getLibStatut($mode = 0)
2036  {
2037  return $this->LibStatut($this->fk_statut, $mode);
2038  }
2039 
2040  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2048  public function LibStatut($status, $mode = 0)
2049  {
2050  // phpcs:enable
2051  global $langs;
2052 
2053  // list of Statut of the task
2054  $this->statuts[0] = 'Draft';
2055  $this->statuts[1] = 'ToDo';
2056  $this->statuts[2] = 'Running';
2057  $this->statuts[3] = 'Finish';
2058  $this->statuts[4] = 'Transfered';
2059  $this->statuts_short[0] = 'Draft';
2060  $this->statuts_short[1] = 'ToDo';
2061  $this->statuts_short[2] = 'Running';
2062  $this->statuts_short[3] = 'Completed';
2063  $this->statuts_short[4] = 'Transfered';
2064 
2065  if ($mode == 0) {
2066  return $langs->trans($this->statuts[$status]);
2067  } elseif ($mode == 1) {
2068  return $langs->trans($this->statuts_short[$status]);
2069  } elseif ($mode == 2) {
2070  if ($status == 0) {
2071  return img_picto($langs->trans($this->statuts_short[$status]), 'statut0').' '.$langs->trans($this->statuts_short[$status]);
2072  } elseif ($status == 1) {
2073  return img_picto($langs->trans($this->statuts_short[$status]), 'statut1').' '.$langs->trans($this->statuts_short[$status]);
2074  } elseif ($status == 2) {
2075  return img_picto($langs->trans($this->statuts_short[$status]), 'statut3').' '.$langs->trans($this->statuts_short[$status]);
2076  } elseif ($status == 3) {
2077  return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts_short[$status]);
2078  } elseif ($status == 4) {
2079  return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts_short[$status]);
2080  } elseif ($status == 5) {
2081  return img_picto($langs->trans($this->statuts_short[$status]), 'statut5').' '.$langs->trans($this->statuts_short[$status]);
2082  }
2083  } elseif ($mode == 3) {
2084  if ($status == 0) {
2085  return img_picto($langs->trans($this->statuts_short[$status]), 'statut0');
2086  } elseif ($status == 1) {
2087  return img_picto($langs->trans($this->statuts_short[$status]), 'statut1');
2088  } elseif ($status == 2) {
2089  return img_picto($langs->trans($this->statuts_short[$status]), 'statut3');
2090  } elseif ($status == 3) {
2091  return img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
2092  } elseif ($status == 4) {
2093  return img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
2094  } elseif ($status == 5) {
2095  return img_picto($langs->trans($this->statuts_short[$status]), 'statut5');
2096  }
2097  } elseif ($mode == 4) {
2098  if ($status == 0) {
2099  return img_picto($langs->trans($this->statuts_short[$status]), 'statut0').' '.$langs->trans($this->statuts[$status]);
2100  } elseif ($status == 1) {
2101  return img_picto($langs->trans($this->statuts_short[$status]), 'statut1').' '.$langs->trans($this->statuts[$status]);
2102  } elseif ($status == 2) {
2103  return img_picto($langs->trans($this->statuts_short[$status]), 'statut3').' '.$langs->trans($this->statuts[$status]);
2104  } elseif ($status == 3) {
2105  return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts[$status]);
2106  } elseif ($status == 4) {
2107  return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts[$status]);
2108  } elseif ($status == 5) {
2109  return img_picto($langs->trans($this->statuts_short[$status]), 'statut5').' '.$langs->trans($this->statuts[$status]);
2110  }
2111  } elseif ($mode == 5) {
2112  /*if ($status==0) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut0');
2113  elseif ($status==1) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut1');
2114  elseif ($status==2) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut3');
2115  elseif ($status==3) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut6');
2116  elseif ($status==4) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut6');
2117  elseif ($status==5) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut5');
2118  */
2119  //else return $this->progress.' %';
2120  return '&nbsp;';
2121  } elseif ($mode == 6) {
2122  /*if ($status==0) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut0');
2123  elseif ($status==1) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut1');
2124  elseif ($status==2) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut3');
2125  elseif ($status==3) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut6');
2126  elseif ($status==4) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut6');
2127  elseif ($status==5) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut5');
2128  */
2129  //else return $this->progress.' %';
2130  return '&nbsp;';
2131  }
2132  }
2133 
2144  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
2145  {
2146  global $conf;
2147 
2148  $outputlangs->load("projects");
2149 
2150  if (!dol_strlen($modele)) {
2151  $modele = 'nodefault';
2152 
2153  if (!empty($this->model_pdf)) {
2154  $modele = $this->model_pdf;
2155  } elseif (!empty($this->modelpdf)) { // deprecated
2156  $modele = $this->modelpdf;
2157  } elseif (!empty($conf->global->PROJECT_TASK_ADDON_PDF)) {
2158  $modele = $conf->global->PROJECT_TASK_ADDON_PDF;
2159  }
2160  }
2161 
2162  $modelpath = "core/modules/project/task/doc/";
2163 
2164  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
2165  }
2166 
2167 
2168  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2175  public function load_board($user)
2176  {
2177  // phpcs:enable
2178  global $conf, $langs;
2179 
2180  // For external user, no check is done on company because readability is managed by public status of project and assignement.
2181  //$socid = $user->socid;
2182  $socid = 0;
2183 
2184  $projectstatic = new Project($this->db);
2185  $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, $socid);
2186 
2187  // List of tasks (does not care about permissions. Filtering will be done later)
2188  $sql = "SELECT p.rowid as projectid, p.fk_statut as projectstatus,";
2189  $sql .= " t.rowid as taskid, t.progress as progress, t.fk_statut as status,";
2190  $sql .= " t.dateo as date_start, t.datee as datee";
2191  $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2192  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2193  //if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2194  $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
2195  $sql .= " WHERE p.entity IN (".getEntity('project', 0).')';
2196  $sql .= " AND p.fk_statut = 1";
2197  $sql .= " AND t.fk_projet = p.rowid";
2198  $sql .= " AND (t.progress IS NULL OR t.progress < 100)"; // tasks to do
2199  if (empty($user->rights->projet->all->lire)) {
2200  $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2201  }
2202  // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2203  //if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
2204  // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2205  // if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id).") OR (s.rowid IS NULL))";
2206 
2207  //print $sql;
2208  $resql = $this->db->query($sql);
2209  if ($resql) {
2210  $task_static = new Task($this->db);
2211 
2212  $response = new WorkboardResponse();
2213  $response->warning_delay = $conf->project->task->warning_delay / 60 / 60 / 24;
2214  $response->label = $langs->trans("OpenedTasks");
2215  if ($user->rights->projet->all->lire) {
2216  $response->url = DOL_URL_ROOT.'/projet/tasks/list.php?mainmenu=project';
2217  } else {
2218  $response->url = DOL_URL_ROOT.'/projet/tasks/list.php?mode=mine&amp;mainmenu=project';
2219  }
2220  $response->img = img_object('', "task");
2221 
2222  // This assignment in condition is not a bug. It allows walking the results.
2223  while ($obj = $this->db->fetch_object($resql)) {
2224  $response->nbtodo++;
2225 
2226  $task_static->projectstatus = $obj->projectstatus;
2227  $task_static->progress = $obj->progress;
2228  $task_static->fk_statut = $obj->status;
2229  $task_static->date_end = $this->db->jdate($obj->datee);
2230 
2231  if ($task_static->hasDelay()) {
2232  $response->nbtodolate++;
2233  }
2234  }
2235 
2236  return $response;
2237  } else {
2238  $this->error = $this->db->error();
2239  return -1;
2240  }
2241  }
2242 
2243 
2244  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2250  public function load_state_board()
2251  {
2252  // phpcs:enable
2253  global $user;
2254 
2255  $mine = 0; $socid = $user->socid;
2256 
2257  $projectstatic = new Project($this->db);
2258  $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, $mine, 1, $socid);
2259 
2260  // List of tasks (does not care about permissions. Filtering will be done later)
2261  $sql = "SELECT count(p.rowid) as nb";
2262  $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2263  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2264  if (empty($user->rights->societe->client->voir) && !$socid) {
2265  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2266  }
2267  $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
2268  $sql .= " WHERE p.entity IN (".getEntity('project', 0).')';
2269  $sql .= " AND t.fk_projet = p.rowid"; // tasks to do
2270  if ($mine || empty($user->rights->projet->all->lire)) {
2271  $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2272  }
2273  // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2274  //if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
2275  if ($socid) {
2276  $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
2277  }
2278  if (empty($user->rights->societe->client->voir) && !$socid) {
2279  $sql .= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id).") OR (s.rowid IS NULL))";
2280  }
2281 
2282  $resql = $this->db->query($sql);
2283  if ($resql) {
2284  // This assignment in condition is not a bug. It allows walking the results.
2285  while ($obj = $this->db->fetch_object($resql)) {
2286  $this->nb["tasks"] = $obj->nb;
2287  }
2288  $this->db->free($resql);
2289  return 1;
2290  } else {
2291  dol_print_error($this->db);
2292  $this->error = $this->db->error();
2293  return -1;
2294  }
2295  }
2296 
2302  public function hasDelay()
2303  {
2304  global $conf;
2305 
2306  if (!($this->progress >= 0 && $this->progress < 100)) {
2307  return false;
2308  }
2309 
2310  $now = dol_now();
2311 
2312  $datetouse = ($this->date_end > 0) ? $this->date_end : ((isset($this->datee) && $this->datee > 0) ? $this->datee : 0);
2313 
2314  return ($datetouse > 0 && ($datetouse < ($now - $conf->project->task->warning_delay)));
2315  }
2316 }
hasTimeSpent()
Return nb of time spent.
Definition: task.class.php:669
fetchTimeSpent($id)
Load properties of timespent of a task from the time spent ID.
getUserRolesForProjectsOrTasks($userp, $usert, $projectid= '', $taskid=0, $filteronprojstatus=-1)
Return list of roles for a user for each projects or each tasks (or a particular project or a particu...
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
Copy a file to another file.
Definition: files.lib.php:702
getLibStatut($mode=0)
Return status label of object.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
update($user=null, $notrigger=0)
Update database.
Definition: task.class.php:370
dol_mkdir($dir, $dataroot= '', $newmask= '')
Creation of a directory (this can create recursive subdir)
$conf db
API class for accounts.
Definition: inc.php:41
updateTimeSpent($user, $notrigger=0)
Update time spent.
dol_html_entity_decode($a, $b, $c= 'UTF-8', $keepsomeentities=0)
Replace html_entity_decode functions to manage errors.
dol_now($mode= 'auto')
Return date for now.
delete_linked_contact($source= '', $code= '')
Delete all links between an object $this and all its contacts.
Class to manage Dolibarr users.
Definition: user.class.php:44
getNomUrl($withpicto=0, $option= '', $mode= 'task', $addlabel=0, $sep= '- ', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: task.class.php:710
isObjectUsed($id=0, $entity=0)
Function to check if an object is used by others.
create($user, $notrigger=0)
Create into database.
Definition: task.class.php:164
addTimeSpent($user, $notrigger=0)
Add time spent.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:121
const STATUS_VALIDATED
Open/Validated status.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
initAsSpecimen()
Initialise an instance with random values.
Definition: task.class.php:780
Class to manage projects.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
fetchAllTimeSpent(User $userobj, $morewherefilter= '')
Load all records of time spent.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
Definition: files.lib.php:1382
hasDelay()
Is the task delayed?
load_board($user)
Load indicators for dashboard (this-&gt;nbtodo and this-&gt;nbtodolate)
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
hasChildren()
Return nb of children.
Definition: task.class.php:636
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getTasksArray($usert=null, $userp=null, $projectid=0, $socid=0, $mode=0, $filteronproj= '', $filteronprojstatus= '-1', $morewherefilter= '', $filteronprojuser=0, $filterontaskuser=0, $extrafields=array(), $includebilltime=0, $search_array_options=array(), $loadextras=0)
Return list of tasks for all projects or for one particular project Sort order is on project...
Definition: task.class.php:815
getSumOfAmount($fuser= '', $dates= '', $datee= '')
Calculate quantity and value of time consumed using the thm (hourly amount value of work for user ent...
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
__construct($db)
Constructor.
Definition: task.class.php:151
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create an intervention document on disk using template defined into PROJECT_TASK_ADDON_PDF.
dol_move_dir($srcdir, $destdir, $overwriteifexists=1, $indexdatabase=1, $renamedircontent=1)
Move a directory into another name.
Definition: files.lib.php:978
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
getListContactId($source= 'internal')
Return list of id of contacts of task.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
if(isModEnabled('facture')&&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur')&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)&&$user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice')&&$user->rights->supplier_invoice->lire)) if(isModEnabled('don')&&!empty($user->rights->don->lire)) if(isModEnabled('tax')&&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture')&&isModEnabled('commande')&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
getSummaryOfTimeSpent($userobj=null, $morewherefilter= '')
Calculate total of time spent for task.
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
Class to manage tasks.
Definition: task.class.php:37
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
call_trigger($triggerName, $user)
Call trigger based on this instance.
LibStatut($status, $mode=0)
Return status label for an object.
$object ref
Definition: info.php:77
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
delTimeSpent($user, $notrigger=0)
Delete time spent.
fetch($id, $ref= '', $loadparentdata=0)
Load object in memory from database.
Definition: task.class.php:270
load_state_board()
Charge indicateurs this-&gt;nb de tableau de bord.
fetchTimeSpentOnTask($morewherefilter= '')
Fetch records of time spent of this task.
createFromClone(User $user, $fromid, $project_id, $parent_task_id, $clone_change_dt=false, $clone_affectation=false, $clone_time=false, $clone_file=false, $clone_note=false, $clone_prog=false)
Load an object from its id and create a new one in database.