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