dolibarr  13.0.2
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  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
28 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
30 
31 
35 class Task extends CommonObject
36 {
40  public $element = 'project_task';
41 
45  public $table_element = 'projet_task';
46 
50  public $fk_element = 'fk_task';
51 
55  public $picto = 'projecttask';
56 
60  protected $childtables = array('projet_task_time');
61 
65  public $fk_task_parent = 0;
66 
70  public $label;
71 
75  public $description;
76 
77  public $duration_effective; // total of time spent on this task
78  public $planned_workload;
79  public $date_c;
80  public $date_start;
81  public $date_end;
82  public $progress;
83 
87  public $fk_statut;
88 
89  public $priority;
90 
94  public $fk_user_creat;
95 
99  public $fk_user_valid;
100 
101  public $rang;
102 
103  public $timespent_min_date;
104  public $timespent_max_date;
105  public $timespent_total_duration;
106  public $timespent_total_amount;
107  public $timespent_nblinesnull;
108  public $timespent_nblines;
109  // For detail of lines of timespent record, there is the property ->lines in common
110 
111  // Var used to call method addTimeSpent(). Bad practice.
112  public $timespent_id;
113  public $timespent_duration;
114  public $timespent_old_duration;
115  public $timespent_date;
116  public $timespent_datehour; // More accurate start date (same than timespent_date but includes hours, minutes and seconds)
117  public $timespent_withhour; // 1 = we entered also start hours for timesheet line
118  public $timespent_fk_user;
119  public $timespent_note;
120 
121  public $comments = array();
122 
123  public $oldcopy;
124 
125 
131  public function __construct($db)
132  {
133  $this->db = $db;
134  }
135 
136 
144  public function create($user, $notrigger = 0)
145  {
146  global $conf, $langs;
147 
148  //For the date
149  $now = dol_now();
150 
151  $error = 0;
152 
153  // Clean parameters
154  $this->label = trim($this->label);
155  $this->description = trim($this->description);
156 
157  // Check parameters
158  // Put here code to add control on parameters values
159 
160  // Insert request
161  $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet_task (";
162  $sql .= "entity";
163  $sql .= ", fk_projet";
164  $sql .= ", ref";
165  $sql .= ", fk_task_parent";
166  $sql .= ", label";
167  $sql .= ", description";
168  $sql .= ", datec";
169  $sql .= ", fk_user_creat";
170  $sql .= ", dateo";
171  $sql .= ", datee";
172  $sql .= ", planned_workload";
173  $sql .= ", progress";
174  $sql .= ") VALUES (";
175  $sql .= $conf->entity;
176  $sql .= ", ".$this->fk_project;
177  $sql .= ", ".(!empty($this->ref) ? "'".$this->db->escape($this->ref)."'" : 'null');
178  $sql .= ", ".$this->fk_task_parent;
179  $sql .= ", '".$this->db->escape($this->label)."'";
180  $sql .= ", '".$this->db->escape($this->description)."'";
181  $sql .= ", '".$this->db->idate($now)."'";
182  $sql .= ", ".$user->id;
183  $sql .= ", ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
184  $sql .= ", ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
185  $sql .= ", ".(($this->planned_workload != '' && $this->planned_workload >= 0) ? $this->planned_workload : 'null');
186  $sql .= ", ".(($this->progress != '' && $this->progress >= 0) ? $this->progress : 'null');
187  $sql .= ")";
188 
189  $this->db->begin();
190 
191  dol_syslog(get_class($this)."::create", LOG_DEBUG);
192  $resql = $this->db->query($sql);
193  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
194 
195  if (!$error)
196  {
197  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task");
198 
199  if (!$notrigger)
200  {
201  // Call trigger
202  $result = $this->call_trigger('TASK_CREATE', $user);
203  if ($result < 0) { $error++; }
204  // End call triggers
205  }
206  }
207 
208  // Update extrafield
209  if (!$error)
210  {
211  if (!$error)
212  {
213  $result = $this->insertExtraFields();
214  if ($result < 0)
215  {
216  $error++;
217  }
218  }
219  }
220 
221  // Commit or rollback
222  if ($error)
223  {
224  foreach ($this->errors as $errmsg)
225  {
226  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
227  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
228  }
229  $this->db->rollback();
230  return -1 * $error;
231  } else {
232  $this->db->commit();
233  return $this->id;
234  }
235  }
236 
237 
246  public function fetch($id, $ref = '', $loadparentdata = 0)
247  {
248  global $langs, $conf;
249 
250  $sql = "SELECT";
251  $sql .= " t.rowid,";
252  $sql .= " t.ref,";
253  $sql .= " t.fk_projet as fk_project,";
254  $sql .= " t.fk_task_parent,";
255  $sql .= " t.label,";
256  $sql .= " t.description,";
257  $sql .= " t.duration_effective,";
258  $sql .= " t.planned_workload,";
259  $sql .= " t.datec,";
260  $sql .= " t.dateo,";
261  $sql .= " t.datee,";
262  $sql .= " t.fk_user_creat,";
263  $sql .= " t.fk_user_valid,";
264  $sql .= " t.fk_statut,";
265  $sql .= " t.progress,";
266  $sql .= " t.priority,";
267  $sql .= " t.note_private,";
268  $sql .= " t.note_public,";
269  $sql .= " t.rang";
270  if (!empty($loadparentdata))
271  {
272  $sql .= ", t2.ref as task_parent_ref";
273  $sql .= ", t2.rang as task_parent_position";
274  }
275  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t";
276  if (!empty($loadparentdata)) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t2 ON t.fk_task_parent = t2.rowid";
277  $sql .= " WHERE ";
278  if (!empty($ref)) {
279  $sql .= "t.ref = '".$this->db->escape($ref)."'";
280  } else {
281  $sql .= "t.rowid = ".$id;
282  }
283 
284  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
285  $resql = $this->db->query($sql);
286  if ($resql)
287  {
288  $num_rows = $this->db->num_rows($resql);
289 
290  if ($num_rows)
291  {
292  $obj = $this->db->fetch_object($resql);
293 
294  $this->id = $obj->rowid;
295  $this->ref = $obj->ref;
296  $this->fk_project = $obj->fk_project;
297  $this->fk_task_parent = $obj->fk_task_parent;
298  $this->label = $obj->label;
299  $this->description = $obj->description;
300  $this->duration_effective = $obj->duration_effective;
301  $this->planned_workload = $obj->planned_workload;
302  $this->date_c = $this->db->jdate($obj->datec);
303  $this->date_start = $this->db->jdate($obj->dateo);
304  $this->date_end = $this->db->jdate($obj->datee);
305  $this->fk_user_creat = $obj->fk_user_creat;
306  $this->fk_user_valid = $obj->fk_user_valid;
307  $this->fk_statut = $obj->fk_statut;
308  $this->progress = $obj->progress;
309  $this->priority = $obj->priority;
310  $this->note_private = $obj->note_private;
311  $this->note_public = $obj->note_public;
312  $this->rang = $obj->rang;
313 
314  if (!empty($loadparentdata))
315  {
316  $this->task_parent_ref = $obj->task_parent_ref;
317  $this->task_parent_position = $obj->task_parent_position;
318  }
319 
320  // Retrieve all extrafield
321  $this->fetch_optionals();
322  }
323 
324  $this->db->free($resql);
325 
326  if ($num_rows)
327  {
328  return 1;
329  } else {
330  return 0;
331  }
332  } else {
333  $this->error = "Error ".$this->db->lasterror();
334  return -1;
335  }
336  }
337 
338 
346  public function update($user = null, $notrigger = 0)
347  {
348  global $conf, $langs;
349  $error = 0;
350 
351  // Clean parameters
352  if (isset($this->fk_project)) $this->fk_project = trim($this->fk_project);
353  if (isset($this->ref)) $this->ref = trim($this->ref);
354  if (isset($this->fk_task_parent)) $this->fk_task_parent = (int) $this->fk_task_parent;
355  if (isset($this->label)) $this->label = trim($this->label);
356  if (isset($this->description)) $this->description = trim($this->description);
357  if (isset($this->duration_effective)) $this->duration_effective = trim($this->duration_effective);
358  if (isset($this->planned_workload)) $this->planned_workload = trim($this->planned_workload);
359 
360  // Check parameters
361  // Put here code to add control on parameters values
362 
363  // Update request
364  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
365  $sql .= " fk_projet=".(isset($this->fk_project) ? $this->fk_project : "null").",";
366  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "'".$this->db->escape($this->id)."'").",";
367  $sql .= " fk_task_parent=".(isset($this->fk_task_parent) ? $this->fk_task_parent : "null").",";
368  $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
369  $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
370  $sql .= " duration_effective=".(isset($this->duration_effective) ? $this->duration_effective : "null").",";
371  $sql .= " planned_workload=".((isset($this->planned_workload) && $this->planned_workload != '') ? $this->planned_workload : "null").",";
372  $sql .= " dateo=".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null').",";
373  $sql .= " datee=".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null').",";
374  $sql .= " progress=".(($this->progress != '' && $this->progress >= 0) ? $this->progress : 'null').",";
375  $sql .= " rang=".((!empty($this->rang)) ? $this->rang : "0");
376  $sql .= " WHERE rowid=".$this->id;
377 
378  $this->db->begin();
379 
380  dol_syslog(get_class($this)."::update", LOG_DEBUG);
381  $resql = $this->db->query($sql);
382  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
383 
384  // Update extrafield
385  if (!$error) {
386  if (!$error)
387  {
388  $result = $this->insertExtraFields();
389  if ($result < 0)
390  {
391  $error++;
392  }
393  }
394  }
395 
396  if (!$error)
397  {
398  if (!$notrigger)
399  {
400  // Call trigger
401  $result = $this->call_trigger('TASK_MODIFY', $user);
402  if ($result < 0) { $error++; }
403  // End call triggers
404  }
405  }
406 
407  if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref))
408  {
409  // We remove directory
410  if ($conf->projet->dir_output)
411  {
412  $project = new Project($this->db);
413  $project->fetch($this->fk_project);
414 
415  $olddir = $conf->projet->dir_output.'/'.dol_sanitizeFileName($project->ref).'/'.dol_sanitizeFileName($this->oldcopy->ref);
416  $newdir = $conf->projet->dir_output.'/'.dol_sanitizeFileName($project->ref).'/'.dol_sanitizeFileName($this->ref);
417  if (file_exists($olddir))
418  {
419  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
420  $res = dol_move($olddir, $newdir);
421  if (!$res)
422  {
423  $langs->load("errors");
424  $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
425  $error++;
426  }
427  }
428  }
429  }
430 
431  // Commit or rollback
432  if ($error)
433  {
434  foreach ($this->errors as $errmsg)
435  {
436  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
437  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
438  }
439  $this->db->rollback();
440  return -1 * $error;
441  } else {
442  $this->db->commit();
443  return 1;
444  }
445  }
446 
447 
455  public function delete($user, $notrigger = 0)
456  {
457 
458  global $conf, $langs;
459  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
460 
461  $error = 0;
462 
463  $this->db->begin();
464 
465  if ($this->hasChildren() > 0)
466  {
467  dol_syslog(get_class($this)."::delete Can't delete record as it has some sub tasks", LOG_WARNING);
468  $this->error = 'ErrorRecordHasSubTasks';
469  $this->db->rollback();
470  return 0;
471  }
472 
473  $objectisused = $this->isObjectUsed($this->id);
474  if (!empty($objectisused))
475  {
476  dol_syslog(get_class($this)."::delete Can't delete record as it has some child", LOG_WARNING);
477  $this->error = 'ErrorRecordHasChildren';
478  $this->db->rollback();
479  return 0;
480  }
481 
482  if (!$error)
483  {
484  // Delete linked contacts
485  $res = $this->delete_linked_contact();
486  if ($res < 0)
487  {
488  $this->error = 'ErrorFailToDeleteLinkedContact';
489  //$error++;
490  $this->db->rollback();
491  return 0;
492  }
493  }
494 
495  if (!$error)
496  {
497  $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_time";
498  $sql .= " WHERE fk_task=".$this->id;
499 
500  $resql = $this->db->query($sql);
501  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
502  }
503 
504  if (!$error)
505  {
506  $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_extrafields";
507  $sql .= " WHERE fk_object=".$this->id;
508 
509  $resql = $this->db->query($sql);
510  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
511  }
512 
513  if (!$error)
514  {
515  $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task";
516  $sql .= " WHERE rowid=".$this->id;
517 
518  $resql = $this->db->query($sql);
519  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
520  }
521 
522  if (!$error)
523  {
524  if (!$notrigger)
525  {
526  // Call trigger
527  $result = $this->call_trigger('TASK_DELETE', $user);
528  if ($result < 0) { $error++; }
529  // End call triggers
530  }
531  }
532 
533  // Commit or rollback
534  if ($error)
535  {
536  foreach ($this->errors as $errmsg)
537  {
538  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
539  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
540  }
541  $this->db->rollback();
542  return -1 * $error;
543  } else {
544  //Delete associated link file
545  if ($conf->projet->dir_output)
546  {
547  $projectstatic = new Project($this->db);
548  $projectstatic->fetch($this->fk_project);
549 
550  $dir = $conf->projet->dir_output."/".dol_sanitizeFileName($projectstatic->ref).'/'.dol_sanitizeFileName($this->id);
551  dol_syslog(get_class($this)."::delete dir=".$dir, LOG_DEBUG);
552  if (file_exists($dir))
553  {
554  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
555  $res = @dol_delete_dir_recursive($dir);
556  if (!$res)
557  {
558  $this->error = 'ErrorFailToDeleteDir';
559  $this->db->rollback();
560  return 0;
561  }
562  }
563  }
564 
565  $this->db->commit();
566 
567  return 1;
568  }
569  }
570 
576  public function hasChildren()
577  {
578  $error = 0;
579  $ret = 0;
580 
581  $sql = "SELECT COUNT(*) as nb";
582  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task";
583  $sql .= " WHERE fk_task_parent=".$this->id;
584 
585  dol_syslog(get_class($this)."::hasChildren", LOG_DEBUG);
586  $resql = $this->db->query($sql);
587  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } else {
588  $obj = $this->db->fetch_object($resql);
589  if ($obj) $ret = $obj->nb;
590  $this->db->free($resql);
591  }
592 
593  if (!$error)
594  {
595  return $ret;
596  } else {
597  return -1;
598  }
599  }
600 
606  public function hasTimeSpent()
607  {
608  $error = 0;
609  $ret = 0;
610 
611  $sql = "SELECT COUNT(*) as nb";
612  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time";
613  $sql .= " WHERE fk_task=".$this->id;
614 
615  dol_syslog(get_class($this)."::hasTimeSpent", LOG_DEBUG);
616  $resql = $this->db->query($sql);
617  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); } else {
618  $obj = $this->db->fetch_object($resql);
619  if ($obj) $ret = $obj->nb;
620  $this->db->free($resql);
621  }
622 
623  if (!$error)
624  {
625  return $ret;
626  } else {
627  return -1;
628  }
629  }
630 
631 
644  public function getNomUrl($withpicto = 0, $option = '', $mode = 'task', $addlabel = 0, $sep = ' - ', $notooltip = 0, $save_lastsearch_value = -1)
645  {
646  global $conf, $langs, $user;
647 
648  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
649 
650  $result = '';
651  $label = img_picto('', $this->picto).' <u>'.$langs->trans("Task").'</u>';
652  if (!empty($this->ref))
653  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
654  if (!empty($this->label))
655  $label .= '<br><b>'.$langs->trans('LabelTask').':</b> '.$this->label;
656  if ($this->date_start || $this->date_end)
657  {
658  $label .= "<br>".get_date_range($this->date_start, $this->date_end, '', $langs, 0);
659  }
660 
661  $url = DOL_URL_ROOT.'/projet/tasks/'.$mode.'.php?id='.$this->id.($option == 'withproject' ? '&withproject=1' : '');
662  // Add param to save lastsearch_values or not
663  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
664  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
665  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
666 
667  $linkclose = '';
668  if (empty($notooltip))
669  {
670  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
671  {
672  $label = $langs->trans("ShowTask");
673  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
674  }
675  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
676  $linkclose .= ' class="classfortooltip nowraponall"';
677  } else {
678  $linkclose .= ' class="nowraponall"';
679  }
680 
681  $linkstart = '<a href="'.$url.'"';
682  $linkstart .= $linkclose.'>';
683  $linkend = '</a>';
684 
685  $picto = 'projecttask';
686 
687  $result .= $linkstart;
688  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), $picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
689  if ($withpicto != 2) $result .= $this->ref;
690  $result .= $linkend;
691  if ($withpicto != 2) $result .= (($addlabel && $this->label) ? $sep.dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
692 
693  return $result;
694  }
695 
703  public function initAsSpecimen()
704  {
705  $this->id = 0;
706 
707  $this->fk_project = '';
708  $this->ref = 'TK01';
709  $this->fk_task_parent = null;
710  $this->label = 'Specimen task TK01';
711  $this->duration_effective = '';
712  $this->fk_user_creat = null;
713  $this->progress = '25';
714  $this->fk_statut = null;
715  $this->note = 'This is a specimen task not';
716  }
717 
737  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())
738  {
739  global $conf, $hookmanager;
740 
741  $tasks = array();
742 
743  //print $usert.'-'.$userp.'-'.$projectid.'-'.$socid.'-'.$mode.'<br>';
744 
745  // List of tasks (does not care about permissions. Filtering will be done later)
746  $sql = "SELECT ";
747  if ($filteronprojuser > 0 || $filterontaskuser > 0) $sql .= " DISTINCT"; // We may get several time the same record if user has several roles on same project/task
748  $sql .= " p.rowid as projectid, p.ref, p.title as plabel, p.public, p.fk_statut as projectstatus, p.usage_bill_time,";
749  $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,";
750  $sql .= " t.dateo as date_start, t.datee as date_end, t.planned_workload, t.rang,";
751  $sql .= " t.description, ";
752  $sql .= " s.rowid as thirdparty_id, s.nom as thirdparty_name, s.email as thirdparty_email,";
753  $sql .= " p.fk_opp_status, p.opp_amount, p.opp_percent, p.budget_amount";
754  if (!empty($extrafields->attributes['projet']['label']))
755  {
756  foreach ($extrafields->attributes['projet']['label'] as $key => $val) $sql .= ($extrafields->attributes['projet']['type'][$key] != 'separate' ? ",efp.".$key.' as options_'.$key : '');
757  }
758  if (!empty($extrafields->attributes['projet_task']['label']))
759  {
760  foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) $sql .= ($extrafields->attributes['projet_task']['type'][$key] != 'separate' ? ",efpt.".$key.' as options_'.$key : '');
761  }
762  if ($includebilltime)
763  {
764  $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";
765  }
766 
767  $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
768  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
769  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_extrafields as efp ON (p.rowid = efp.fk_object)";
770 
771  if ($mode == 0)
772  {
773  if ($filteronprojuser > 0)
774  {
775  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
776  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
777  }
778  $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
779  if ($includebilltime)
780  {
781  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_time as tt ON tt.fk_task = t.rowid";
782  }
783  if ($filterontaskuser > 0)
784  {
785  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec2";
786  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc2";
787  }
788  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_extrafields as efpt ON (t.rowid = efpt.fk_object)";
789  $sql .= " WHERE p.entity IN (".getEntity('project').")";
790  $sql .= " AND t.fk_projet = p.rowid";
791  } elseif ($mode == 1)
792  {
793  if ($filteronprojuser > 0)
794  {
795  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
796  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
797  }
798  if ($filterontaskuser > 0)
799  {
800  $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
801  if ($includebilltime)
802  {
803  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_time as tt ON tt.fk_task = t.rowid";
804  }
805  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec2";
806  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc2";
807  } else {
808  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t on t.fk_projet = p.rowid";
809  if ($includebilltime)
810  {
811  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_time as tt ON tt.fk_task = t.rowid";
812  }
813  }
814  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_extrafields as efpt ON (t.rowid = efpt.fk_object)";
815  $sql .= " WHERE p.entity IN (".getEntity('project').")";
816  } else return 'BadValueForParameterMode';
817 
818  if ($filteronprojuser > 0)
819  {
820  $sql .= " AND p.rowid = ec.element_id";
821  $sql .= " AND ctc.rowid = ec.fk_c_type_contact";
822  $sql .= " AND ctc.element = 'project'";
823  $sql .= " AND ec.fk_socpeople = ".$filteronprojuser;
824  $sql .= " AND ec.statut = 4";
825  $sql .= " AND ctc.source = 'internal'";
826  }
827  if ($filterontaskuser > 0)
828  {
829  $sql .= " AND t.fk_projet = p.rowid";
830  $sql .= " AND p.rowid = ec2.element_id";
831  $sql .= " AND ctc2.rowid = ec2.fk_c_type_contact";
832  $sql .= " AND ctc2.element = 'project_task'";
833  $sql .= " AND ec2.fk_socpeople = ".$filterontaskuser;
834  $sql .= " AND ec2.statut = 4";
835  $sql .= " AND ctc2.source = 'internal'";
836  }
837  if ($socid) $sql .= " AND p.fk_soc = ".$socid;
838  if ($projectid) $sql .= " AND p.rowid in (".$projectid.")";
839  if ($filteronproj) $sql .= natural_search(array("p.ref", "p.title"), $filteronproj);
840  if ($filteronprojstatus && $filteronprojstatus != '-1') $sql .= " AND p.fk_statut IN (".$filteronprojstatus.")";
841  if ($morewherefilter) $sql .= $morewherefilter;
842  // Add where from extra fields
843  $extrafieldsobjectkey = 'projet_task';
844  $extrafieldsobjectprefix = 'efpt.';
845  include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
846  // Add where from hooks
847  $parameters = array();
848  $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
849  $sql .= $hookmanager->resPrint;
850  if ($includebilltime)
851  {
852  $sql .= " GROUP BY p.rowid, p.ref, p.title, p.public, p.fk_statut, p.usage_bill_time,";
853  $sql .= " t.datec, t.dateo, t.datee, t.tms,";
854  $sql .= " t.rowid, t.ref, t.label, t.description, t.fk_task_parent, t.duration_effective, t.progress, t.fk_statut,";
855  $sql .= " t.dateo, t.datee, t.planned_workload, t.rang,";
856  $sql .= " t.description, ";
857  $sql .= " s.rowid, s.nom, s.email,";
858  $sql .= " p.fk_opp_status, p.opp_amount, p.opp_percent, p.budget_amount";
859  if (!empty($extrafields->attributes['projet']['label']))
860  {
861  foreach ($extrafields->attributes['projet']['label'] as $key => $val) $sql .= ($extrafields->attributes['projet']['type'][$key] != 'separate' ? ",efp.".$key : '');
862  }
863  if (!empty($extrafields->attributes['projet_task']['label']))
864  {
865  foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) $sql .= ($extrafields->attributes['projet_task']['type'][$key] != 'separate' ? ",efpt.".$key : '');
866  }
867  }
868 
869 
870  $sql .= " ORDER BY p.ref, t.rang, t.dateo";
871 
872  //print $sql;exit;
873  dol_syslog(get_class($this)."::getTasksArray", LOG_DEBUG);
874  $resql = $this->db->query($sql);
875  if ($resql)
876  {
877  $num = $this->db->num_rows($resql);
878  $i = 0;
879  // Loop on each record found, so each couple (project id, task id)
880  while ($i < $num)
881  {
882  $error = 0;
883 
884  $obj = $this->db->fetch_object($resql);
885 
886  if ((!$obj->public) && (is_object($userp))) // If not public project and we ask a filter on project owned by a user
887  {
888  if (!$this->getUserRolesForProjectsOrTasks($userp, 0, $obj->projectid, 0))
889  {
890  $error++;
891  }
892  }
893  if (is_object($usert)) // If we ask a filter on a user affected to a task
894  {
895  if (!$this->getUserRolesForProjectsOrTasks(0, $usert, $obj->projectid, $obj->taskid))
896  {
897  $error++;
898  }
899  }
900 
901  if (!$error)
902  {
903  $tasks[$i] = new Task($this->db);
904  $tasks[$i]->id = $obj->taskid;
905  $tasks[$i]->ref = $obj->taskref;
906  $tasks[$i]->fk_project = $obj->projectid;
907  $tasks[$i]->projectref = $obj->ref;
908  $tasks[$i]->projectlabel = $obj->plabel;
909  $tasks[$i]->projectstatus = $obj->projectstatus;
910 
911  $tasks[$i]->fk_opp_status = $obj->fk_opp_status;
912  $tasks[$i]->opp_amount = $obj->opp_amount;
913  $tasks[$i]->opp_percent = $obj->opp_percent;
914  $tasks[$i]->budget_amount = $obj->budget_amount;
915  $tasks[$i]->usage_bill_time = $obj->usage_bill_time;
916 
917  $tasks[$i]->label = $obj->label;
918  $tasks[$i]->description = $obj->description;
919  $tasks[$i]->fk_parent = $obj->fk_task_parent; // deprecated
920  $tasks[$i]->fk_task_parent = $obj->fk_task_parent;
921  $tasks[$i]->duration = $obj->duration_effective;
922  $tasks[$i]->planned_workload = $obj->planned_workload;
923 
924  if ($includebilltime) {
925  $tasks[$i]->tobill = $obj->tobill;
926  $tasks[$i]->billed = $obj->billed;
927  }
928 
929  $tasks[$i]->progress = $obj->progress;
930  $tasks[$i]->fk_statut = $obj->status;
931  $tasks[$i]->public = $obj->public;
932  $tasks[$i]->date_start = $this->db->jdate($obj->date_start);
933  $tasks[$i]->date_end = $this->db->jdate($obj->date_end);
934  $tasks[$i]->rang = $obj->rang;
935 
936  $tasks[$i]->socid = $obj->thirdparty_id; // For backward compatibility
937  $tasks[$i]->thirdparty_id = $obj->thirdparty_id;
938  $tasks[$i]->thirdparty_name = $obj->thirdparty_name;
939  $tasks[$i]->thirdparty_email = $obj->thirdparty_email;
940 
941  if (!empty($extrafields->attributes['projet']['label']))
942  {
943  foreach ($extrafields->attributes['projet']['label'] as $key => $val)
944  {
945  if ($extrafields->attributes['projet']['type'][$key] != 'separate')
946  $tasks[$i]->{'options_'.$key} = $obj->{'options_'.$key};
947  }
948  }
949 
950  if (!empty($extrafields->attributes['projet_task']['label']))
951  {
952  foreach ($extrafields->attributes['projet_task']['label'] as $key => $val)
953  {
954  if ($extrafields->attributes['projet_task']['type'][$key] != 'separate')
955  $tasks[$i]->{'options_'.$key} = $obj->{'options_'.$key};
956  }
957  }
958  }
959 
960  $i++;
961  }
962  $this->db->free($resql);
963  } else {
964  dol_print_error($this->db);
965  }
966 
967  return $tasks;
968  }
969 
980  public function getUserRolesForProjectsOrTasks($userp, $usert, $projectid = '', $taskid = 0, $filteronprojstatus = -1)
981  {
982  $arrayroles = array();
983 
984  dol_syslog(get_class($this)."::getUserRolesForProjectsOrTasks userp=".is_object($userp)." usert=".is_object($usert)." projectid=".$projectid." taskid=".$taskid);
985 
986  // We want role of user for a projet or role of user for a task. Both are not possible.
987  if (empty($userp) && empty($usert))
988  {
989  $this->error = "CallWithWrongParameters";
990  return -1;
991  }
992  if (!empty($userp) && !empty($usert))
993  {
994  $this->error = "CallWithWrongParameters";
995  return -1;
996  }
997 
998  /* Liste des taches et role sur les projets ou taches */
999  $sql = "SELECT pt.rowid as pid, ec.element_id, ctc.code, ctc.source";
1000  if ($userp) $sql .= " FROM ".MAIN_DB_PREFIX."projet as pt";
1001  if ($usert && $filteronprojstatus > -1) $sql .= " FROM ".MAIN_DB_PREFIX."projet as p, ".MAIN_DB_PREFIX."projet_task as pt";
1002  if ($usert && $filteronprojstatus <= -1) $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as pt";
1003  $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
1004  $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
1005  $sql .= " WHERE pt.rowid = ec.element_id";
1006  if ($userp && $filteronprojstatus > -1) $sql .= " AND pt.fk_statut = ".$filteronprojstatus;
1007  if ($usert && $filteronprojstatus > -1) $sql .= " AND pt.fk_projet = p.rowid AND p.fk_statut = ".$filteronprojstatus;
1008  if ($userp) $sql .= " AND ctc.element = 'project'";
1009  if ($usert) $sql .= " AND ctc.element = 'project_task'";
1010  $sql .= " AND ctc.rowid = ec.fk_c_type_contact";
1011  if ($userp) $sql .= " AND ec.fk_socpeople = ".$userp->id;
1012  if ($usert) $sql .= " AND ec.fk_socpeople = ".$usert->id;
1013  $sql .= " AND ec.statut = 4";
1014  $sql .= " AND ctc.source = 'internal'";
1015  if ($projectid)
1016  {
1017  if ($userp) $sql .= " AND pt.rowid in (".$projectid.")";
1018  if ($usert) $sql .= " AND pt.fk_projet in (".$projectid.")";
1019  }
1020  if ($taskid)
1021  {
1022  if ($userp) $sql .= " ERROR SHOULD NOT HAPPENS";
1023  if ($usert) $sql .= " AND pt.rowid = ".$taskid;
1024  }
1025  //print $sql;
1026 
1027  dol_syslog(get_class($this)."::getUserRolesForProjectsOrTasks execute request", LOG_DEBUG);
1028  $resql = $this->db->query($sql);
1029  if ($resql)
1030  {
1031  $num = $this->db->num_rows($resql);
1032  $i = 0;
1033  while ($i < $num)
1034  {
1035  $obj = $this->db->fetch_object($resql);
1036  if (empty($arrayroles[$obj->pid])) $arrayroles[$obj->pid] = $obj->code;
1037  else $arrayroles[$obj->pid] .= ','.$obj->code;
1038  $i++;
1039  }
1040  $this->db->free($resql);
1041  } else {
1042  dol_print_error($this->db);
1043  }
1044 
1045  return $arrayroles;
1046  }
1047 
1048 
1055  public function getListContactId($source = 'internal')
1056  {
1057  $contactAlreadySelected = array();
1058  $tab = $this->liste_contact(-1, $source);
1059  //var_dump($tab);
1060  $num = count($tab);
1061  $i = 0;
1062  while ($i < $num)
1063  {
1064  if ($source == 'thirdparty') $contactAlreadySelected[$i] = $tab[$i]['socid'];
1065  else $contactAlreadySelected[$i] = $tab[$i]['id'];
1066  $i++;
1067  }
1068  return $contactAlreadySelected;
1069  }
1070 
1071 
1079  public function addTimeSpent($user, $notrigger = 0)
1080  {
1081  global $conf, $langs;
1082 
1083  dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1084 
1085  $ret = 0;
1086 
1087  // Check parameters
1088  if (!is_object($user))
1089  {
1090  dol_print_error('', "Method addTimeSpent was called with wrong parameter user");
1091  return -1;
1092  }
1093 
1094  // Clean parameters
1095  if (isset($this->timespent_note)) $this->timespent_note = trim($this->timespent_note);
1096  if (empty($this->timespent_datehour)) $this->timespent_datehour = $this->timespent_date;
1097 
1098  $this->db->begin();
1099 
1100  $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet_task_time (";
1101  $sql .= "fk_task";
1102  $sql .= ", task_date";
1103  $sql .= ", task_datehour";
1104  $sql .= ", task_date_withhour";
1105  $sql .= ", task_duration";
1106  $sql .= ", fk_user";
1107  $sql .= ", note";
1108  $sql .= ") VALUES (";
1109  $sql .= $this->id;
1110  $sql .= ", '".$this->db->idate($this->timespent_date)."'";
1111  $sql .= ", '".$this->db->idate($this->timespent_datehour)."'";
1112  $sql .= ", ".(empty($this->timespent_withhour) ? 0 : 1);
1113  $sql .= ", ".$this->timespent_duration;
1114  $sql .= ", ".$this->timespent_fk_user;
1115  $sql .= ", ".(isset($this->timespent_note) ? "'".$this->db->escape($this->timespent_note)."'" : "null");
1116  $sql .= ")";
1117 
1118  $resql = $this->db->query($sql);
1119  if ($resql)
1120  {
1121  $tasktime_id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task_time");
1122  $ret = $tasktime_id;
1123  $this->timespent_id = $ret;
1124 
1125  if (!$notrigger)
1126  {
1127  // Call trigger
1128  $result = $this->call_trigger('TASK_TIMESPENT_CREATE', $user);
1129  if ($result < 0) { $ret = -1; }
1130  // End call triggers
1131  }
1132  } else {
1133  $this->error = $this->db->lasterror();
1134  $ret = -1;
1135  }
1136 
1137  if ($ret > 0)
1138  {
1139  // Recalculate amount of time spent for task and update denormalized field
1140  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
1141  $sql .= " SET duration_effective = (SELECT SUM(task_duration) FROM ".MAIN_DB_PREFIX."projet_task_time as ptt where ptt.fk_task = ".$this->id.")";
1142  if (isset($this->progress)) $sql .= ", progress = ".$this->progress; // Do not overwrite value if not provided
1143  $sql .= " WHERE rowid = ".$this->id;
1144 
1145  dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1146  if (!$this->db->query($sql))
1147  {
1148  $this->error = $this->db->lasterror();
1149  $ret = -2;
1150  }
1151 
1152  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task_time";
1153  $sql .= " SET thm = (SELECT thm FROM ".MAIN_DB_PREFIX."user WHERE rowid = ".$this->timespent_fk_user.")"; // set average hour rate of user
1154  $sql .= " WHERE rowid = ".$tasktime_id;
1155 
1156  dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1157  if (!$this->db->query($sql))
1158  {
1159  $this->error = $this->db->lasterror();
1160  $ret = -2;
1161  }
1162  }
1163 
1164  if ($ret > 0)
1165  {
1166  $this->db->commit();
1167  } else {
1168  $this->db->rollback();
1169  }
1170  return $ret;
1171  }
1172 
1180  public function getSummaryOfTimeSpent($userobj = null, $morewherefilter = '')
1181  {
1182  global $langs;
1183 
1184  if (is_object($userobj)) $userid = $userobj->id;
1185  else $userid = $userobj; // old method
1186 
1187  $id = $this->id;
1188  if (empty($id) && empty($userid))
1189  {
1190  dol_syslog("getSummaryOfTimeSpent called on a not loaded task without user param defined", LOG_ERR);
1191  return -1;
1192  }
1193 
1194  $result = array();
1195 
1196  $sql = "SELECT";
1197  $sql .= " MIN(t.task_datehour) as min_date,";
1198  $sql .= " MAX(t.task_datehour) as max_date,";
1199  $sql .= " SUM(t.task_duration) as total_duration,";
1200  $sql .= " SUM(t.task_duration / 3600 * ".$this->db->ifsql("t.thm IS NULL", 0, "t.thm").") as total_amount,";
1201  $sql .= " COUNT(t.rowid) as nblines,";
1202  $sql .= " SUM(".$this->db->ifsql("t.thm IS NULL", 1, 0).") as nblinesnull";
1203  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
1204  $sql .= " WHERE 1 = 1";
1205  if ($morewherefilter) $sql .= $morewherefilter;
1206  if ($id > 0) $sql .= " AND t.fk_task = ".$id;
1207  if ($userid > 0) $sql .= " AND t.fk_user = ".$userid;
1208 
1209  dol_syslog(get_class($this)."::getSummaryOfTimeSpent", LOG_DEBUG);
1210  $resql = $this->db->query($sql);
1211  if ($resql)
1212  {
1213  $obj = $this->db->fetch_object($resql);
1214 
1215  $result['min_date'] = $obj->min_date; // deprecated. use the ->timespent_xxx instead
1216  $result['max_date'] = $obj->max_date; // deprecated. use the ->timespent_xxx instead
1217  $result['total_duration'] = $obj->total_duration; // deprecated. use the ->timespent_xxx instead
1218 
1219  $this->timespent_min_date = $this->db->jdate($obj->min_date);
1220  $this->timespent_max_date = $this->db->jdate($obj->max_date);
1221  $this->timespent_total_duration = $obj->total_duration;
1222  $this->timespent_total_amount = $obj->total_amount;
1223  $this->timespent_nblinesnull = ($obj->nblinesnull ? $obj->nblinesnull : 0);
1224  $this->timespent_nblines = ($obj->nblines ? $obj->nblines : 0);
1225 
1226  $this->db->free($resql);
1227  } else {
1228  dol_print_error($this->db);
1229  }
1230  return $result;
1231  }
1232 
1241  public function getSumOfAmount($fuser = '', $dates = '', $datee = '')
1242  {
1243  global $langs;
1244 
1245  if (empty($id)) $id = $this->id;
1246 
1247  $result = array();
1248 
1249  $sql = "SELECT";
1250  $sql .= " SUM(t.task_duration) as nbseconds,";
1251  $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";
1252  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
1253  $sql .= " WHERE t.fk_task = ".$id;
1254  if (is_object($fuser) && $fuser->id > 0)
1255  {
1256  $sql .= " AND fk_user = ".$fuser->id;
1257  }
1258  if ($dates > 0)
1259  {
1260  $datefieldname = "task_datehour";
1261  $sql .= " AND (".$datefieldname." >= '".$this->db->idate($dates)."' OR ".$datefieldname." IS NULL)";
1262  }
1263  if ($datee > 0)
1264  {
1265  $datefieldname = "task_datehour";
1266  $sql .= " AND (".$datefieldname." <= '".$this->db->idate($datee)."' OR ".$datefieldname." IS NULL)";
1267  }
1268  //print $sql;
1269 
1270  dol_syslog(get_class($this)."::getSumOfAmount", LOG_DEBUG);
1271  $resql = $this->db->query($sql);
1272  if ($resql)
1273  {
1274  $obj = $this->db->fetch_object($resql);
1275 
1276  $result['amount'] = $obj->amount;
1277  $result['nbseconds'] = $obj->nbseconds;
1278  $result['nblinesnull'] = $obj->nblinesnull;
1279 
1280  $this->db->free($resql);
1281  return $result;
1282  } else {
1283  dol_print_error($this->db);
1284  return $result;
1285  }
1286  }
1287 
1294  public function fetchTimeSpent($id)
1295  {
1296  global $langs;
1297 
1298  $sql = "SELECT";
1299  $sql .= " t.rowid,";
1300  $sql .= " t.fk_task,";
1301  $sql .= " t.task_date,";
1302  $sql .= " t.task_datehour,";
1303  $sql .= " t.task_date_withhour,";
1304  $sql .= " t.task_duration,";
1305  $sql .= " t.fk_user,";
1306  $sql .= " t.thm,";
1307  $sql .= " t.note";
1308  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
1309  $sql .= " WHERE t.rowid = ".$id;
1310 
1311  dol_syslog(get_class($this)."::fetchTimeSpent", LOG_DEBUG);
1312  $resql = $this->db->query($sql);
1313  if ($resql)
1314  {
1315  if ($this->db->num_rows($resql))
1316  {
1317  $obj = $this->db->fetch_object($resql);
1318 
1319  $this->timespent_id = $obj->rowid;
1320  $this->id = $obj->fk_task;
1321  $this->timespent_date = $this->db->jdate($obj->task_date);
1322  $this->timespent_datehour = $this->db->jdate($obj->task_datehour);
1323  $this->timespent_withhour = $obj->task_date_withhour;
1324  $this->timespent_duration = $obj->task_duration;
1325  $this->timespent_fk_user = $obj->fk_user;
1326  $this->timespent_thm = $obj->thm; // hourly rate
1327  $this->timespent_note = $obj->note;
1328  }
1329 
1330  $this->db->free($resql);
1331 
1332  return 1;
1333  } else {
1334  $this->error = "Error ".$this->db->lasterror();
1335  return -1;
1336  }
1337  }
1338 
1346  public function fetchAllTimeSpent(User $userobj, $morewherefilter = '')
1347  {
1348  global $langs;
1349 
1350  $arrayres = array();
1351 
1352  $sql = "SELECT";
1353  $sql .= " s.rowid as socid,";
1354  $sql .= " s.nom as thirdparty_name,";
1355  $sql .= " s.email as thirdparty_email,";
1356  $sql .= " ptt.rowid,";
1357  $sql .= " ptt.fk_task,";
1358  $sql .= " ptt.task_date,";
1359  $sql .= " ptt.task_datehour,";
1360  $sql .= " ptt.task_date_withhour,";
1361  $sql .= " ptt.task_duration,";
1362  $sql .= " ptt.fk_user,";
1363  $sql .= " ptt.note,";
1364  $sql .= " pt.rowid as task_id,";
1365  $sql .= " pt.ref as task_ref,";
1366  $sql .= " pt.label as task_label,";
1367  $sql .= " p.rowid as project_id,";
1368  $sql .= " p.ref as project_ref,";
1369  $sql .= " p.title as project_label,";
1370  $sql .= " p.public as public";
1371  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as ptt, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."projet as p";
1372  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
1373  $sql .= " WHERE ptt.fk_task = pt.rowid AND pt.fk_projet = p.rowid";
1374  $sql .= " AND ptt.fk_user = ".$userobj->id;
1375  $sql .= " AND pt.entity IN (".getEntity('project').")";
1376  if ($morewherefilter) $sql .= $morewherefilter;
1377 
1378  dol_syslog(get_class($this)."::fetchAllTimeSpent", LOG_DEBUG);
1379  $resql = $this->db->query($sql);
1380  if ($resql)
1381  {
1382  $num = $this->db->num_rows($resql);
1383 
1384  $i = 0;
1385  while ($i < $num)
1386  {
1387  $obj = $this->db->fetch_object($resql);
1388 
1389  $newobj = new stdClass();
1390 
1391  $newobj->socid = $obj->socid;
1392  $newobj->thirdparty_name = $obj->thirdparty_name;
1393  $newobj->thirdparty_email = $obj->thirdparty_email;
1394 
1395  $newobj->fk_project = $obj->project_id;
1396  $newobj->project_ref = $obj->project_ref;
1397  $newobj->project_label = $obj->project_label;
1398  $newobj->public = $obj->project_public;
1399 
1400  $newobj->fk_task = $obj->task_id;
1401  $newobj->task_ref = $obj->task_ref;
1402  $newobj->task_label = $obj->task_label;
1403 
1404  $newobj->timespent_id = $obj->rowid;
1405  $newobj->timespent_date = $this->db->jdate($obj->task_date);
1406  $newobj->timespent_datehour = $this->db->jdate($obj->task_datehour);
1407  $newobj->timespent_withhour = $obj->task_date_withhour;
1408  $newobj->timespent_duration = $obj->task_duration;
1409  $newobj->timespent_fk_user = $obj->fk_user;
1410  $newobj->timespent_note = $obj->note;
1411 
1412  $arrayres[] = $newobj;
1413 
1414  $i++;
1415  }
1416 
1417  $this->db->free($resql);
1418  } else {
1419  dol_print_error($this->db);
1420  $this->error = "Error ".$this->db->lasterror();
1421  return -1;
1422  }
1423 
1424  return $arrayres;
1425  }
1426 
1434  public function updateTimeSpent($user, $notrigger = 0)
1435  {
1436  global $conf, $langs;
1437 
1438  $ret = 0;
1439 
1440  // Check parameters
1441  if ($this->timespent_date == '')
1442  {
1443  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Date"));
1444  return -1;
1445  }
1446  if (!($this->timespent_fk_user > 0))
1447  {
1448  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("User"));
1449  return -1;
1450  }
1451 
1452  // Clean parameters
1453  if (empty($this->timespent_datehour)) $this->timespent_datehour = $this->timespent_date;
1454  if (isset($this->timespent_note)) $this->timespent_note = trim($this->timespent_note);
1455 
1456  $this->db->begin();
1457 
1458  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task_time SET";
1459  $sql .= " task_date = '".$this->db->idate($this->timespent_date)."',";
1460  $sql .= " task_datehour = '".$this->db->idate($this->timespent_datehour)."',";
1461  $sql .= " task_date_withhour = ".(empty($this->timespent_withhour) ? 0 : 1).",";
1462  $sql .= " task_duration = ".$this->timespent_duration.",";
1463  $sql .= " fk_user = ".$this->timespent_fk_user.",";
1464  $sql .= " note = ".(isset($this->timespent_note) ? "'".$this->db->escape($this->timespent_note)."'" : "null");
1465  $sql .= " WHERE rowid = ".$this->timespent_id;
1466 
1467  dol_syslog(get_class($this)."::updateTimeSpent", LOG_DEBUG);
1468  if ($this->db->query($sql))
1469  {
1470  if (!$notrigger)
1471  {
1472  // Call trigger
1473  $result = $this->call_trigger('TASK_TIMESPENT_MODIFY', $user);
1474  if ($result < 0)
1475  {
1476  $this->db->rollback();
1477  $ret = -1;
1478  } else $ret = 1;
1479  // End call triggers
1480  } else $ret = 1;
1481  } else {
1482  $this->error = $this->db->lasterror();
1483  $this->db->rollback();
1484  $ret = -1;
1485  }
1486 
1487  if ($ret == 1 && ($this->timespent_old_duration != $this->timespent_duration))
1488  {
1489  $newDuration = $this->timespent_duration - $this->timespent_old_duration;
1490 
1491  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
1492  $sql .= " SET duration_effective = (SELECT SUM(task_duration) FROM ".MAIN_DB_PREFIX."projet_task_time as ptt where ptt.fk_task = ".$this->db->escape($this->id).")";
1493  $sql .= " WHERE rowid = ".$this->id;
1494 
1495  dol_syslog(get_class($this)."::updateTimeSpent", LOG_DEBUG);
1496  if (!$this->db->query($sql))
1497  {
1498  $this->error = $this->db->lasterror();
1499  $this->db->rollback();
1500  $ret = -2;
1501  }
1502  }
1503 
1504  if ($ret >= 0) $this->db->commit();
1505  return $ret;
1506  }
1507 
1515  public function delTimeSpent($user, $notrigger = 0)
1516  {
1517  global $conf, $langs;
1518 
1519  $error = 0;
1520 
1521  $this->db->begin();
1522 
1523  $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_time";
1524  $sql .= " WHERE rowid = ".$this->timespent_id;
1525 
1526  dol_syslog(get_class($this)."::delTimeSpent", LOG_DEBUG);
1527  $resql = $this->db->query($sql);
1528  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
1529 
1530  if (!$error)
1531  {
1532  if (!$notrigger)
1533  {
1534  // Call trigger
1535  $result = $this->call_trigger('TASK_TIMESPENT_DELETE', $user);
1536  if ($result < 0) { $error++; }
1537  // End call triggers
1538  }
1539  }
1540 
1541  if (!$error)
1542  {
1543  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
1544  $sql .= " SET duration_effective = duration_effective - ".$this->db->escape($this->timespent_duration ? $this->timespent_duration : 0);
1545  $sql .= " WHERE rowid = ".$this->id;
1546 
1547  dol_syslog(get_class($this)."::delTimeSpent", LOG_DEBUG);
1548  if ($this->db->query($sql))
1549  {
1550  $result = 0;
1551  } else {
1552  $this->error = $this->db->lasterror();
1553  $result = -2;
1554  }
1555  }
1556 
1557  // Commit or rollback
1558  if ($error)
1559  {
1560  foreach ($this->errors as $errmsg)
1561  {
1562  dol_syslog(get_class($this)."::delTimeSpent ".$errmsg, LOG_ERR);
1563  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1564  }
1565  $this->db->rollback();
1566  return -1 * $error;
1567  } else {
1568  $this->db->commit();
1569  return 1;
1570  }
1571  }
1572 
1587  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)
1588  {
1589  global $langs, $conf;
1590 
1591  $error = 0;
1592 
1593  //Use 00:00 of today if time is use on task.
1594  $now = dol_mktime(0, 0, 0, dol_print_date(dol_now(), '%m'), dol_print_date(dol_now(), '%d'), dol_print_date(dol_now(), '%Y'));
1595 
1596  $datec = $now;
1597 
1598  $clone_task = new Task($this->db);
1599  $origin_task = new Task($this->db);
1600 
1601  $clone_task->context['createfromclone'] = 'createfromclone';
1602 
1603  $this->db->begin();
1604 
1605  // Load source object
1606  $clone_task->fetch($fromid);
1607  $clone_task->fetch_optionals();
1608  //var_dump($clone_task->array_options);exit;
1609 
1610  $origin_task->fetch($fromid);
1611 
1612  $defaultref = '';
1613  $obj = empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
1614  if (!empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.".php"))
1615  {
1616  require_once DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.'.php';
1617  $modTask = new $obj;
1618  $defaultref = $modTask->getNextValue(0, $clone_task);
1619  }
1620 
1621  $ori_project_id = $clone_task->fk_project;
1622 
1623  $clone_task->id = 0;
1624  $clone_task->ref = $defaultref;
1625  $clone_task->fk_project = $project_id;
1626  $clone_task->fk_task_parent = $parent_task_id;
1627  $clone_task->date_c = $datec;
1628  $clone_task->planned_workload = $origin_task->planned_workload;
1629  $clone_task->rang = $origin_task->rang;
1630 
1631  //Manage Task Date
1632  if ($clone_change_dt)
1633  {
1634  $projectstatic = new Project($this->db);
1635  $projectstatic->fetch($ori_project_id);
1636 
1637  //Origin project strat date
1638  $orign_project_dt_start = $projectstatic->date_start;
1639 
1640  //Calcultate new task start date with difference between origin proj start date and origin task start date
1641  if (!empty($clone_task->date_start))
1642  {
1643  $clone_task->date_start = $now + $clone_task->date_start - $orign_project_dt_start;
1644  }
1645 
1646  //Calcultate new task end date with difference between origin proj end date and origin task end date
1647  if (!empty($clone_task->date_end))
1648  {
1649  $clone_task->date_end = $now + $clone_task->date_end - $orign_project_dt_start;
1650  }
1651  }
1652 
1653  if (!$clone_prog)
1654  {
1655  $clone_task->progress = 0;
1656  }
1657 
1658  // Create clone
1659  $result = $clone_task->create($user);
1660 
1661  // Other options
1662  if ($result < 0)
1663  {
1664  $this->error = $clone_task->error;
1665  $error++;
1666  }
1667 
1668  // End
1669  if (!$error)
1670  {
1671  $clone_task_id = $clone_task->id;
1672  $clone_task_ref = $clone_task->ref;
1673 
1674  //Note Update
1675  if (!$clone_note)
1676  {
1677  $clone_task->note_private = '';
1678  $clone_task->note_public = '';
1679  } else {
1680  $this->db->begin();
1681  $res = $clone_task->update_note(dol_html_entity_decode($clone_task->note_public, ENT_QUOTES | ENT_HTML5), '_public');
1682  if ($res < 0)
1683  {
1684  $this->error .= $clone_task->error;
1685  $error++;
1686  $this->db->rollback();
1687  } else {
1688  $this->db->commit();
1689  }
1690 
1691  $this->db->begin();
1692  $res = $clone_task->update_note(dol_html_entity_decode($clone_task->note_private, ENT_QUOTES | ENT_HTML5), '_private');
1693  if ($res < 0)
1694  {
1695  $this->error .= $clone_task->error;
1696  $error++;
1697  $this->db->rollback();
1698  } else {
1699  $this->db->commit();
1700  }
1701  }
1702 
1703  //Duplicate file
1704  if ($clone_file)
1705  {
1706  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1707 
1708  //retrieve project origin ref to know folder to copy
1709  $projectstatic = new Project($this->db);
1710  $projectstatic->fetch($ori_project_id);
1711  $ori_project_ref = $projectstatic->ref;
1712 
1713  if ($ori_project_id != $project_id)
1714  {
1715  $projectstatic->fetch($project_id);
1716  $clone_project_ref = $projectstatic->ref;
1717  } else {
1718  $clone_project_ref = $ori_project_ref;
1719  }
1720 
1721  $clone_task_dir = $conf->projet->dir_output."/".dol_sanitizeFileName($clone_project_ref)."/".dol_sanitizeFileName($clone_task_ref);
1722  $ori_task_dir = $conf->projet->dir_output."/".dol_sanitizeFileName($ori_project_ref)."/".dol_sanitizeFileName($fromid);
1723 
1724  $filearray = dol_dir_list($ori_task_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', '', SORT_ASC, 1);
1725  foreach ($filearray as $key => $file)
1726  {
1727  if (!file_exists($clone_task_dir))
1728  {
1729  if (dol_mkdir($clone_task_dir) < 0)
1730  {
1731  $this->error .= $langs->trans('ErrorInternalErrorDetected').':dol_mkdir';
1732  $error++;
1733  }
1734  }
1735 
1736  $rescopy = dol_copy($ori_task_dir.'/'.$file['name'], $clone_task_dir.'/'.$file['name'], 0, 1);
1737  if (is_numeric($rescopy) && $rescopy < 0)
1738  {
1739  $this->error .= $langs->trans("ErrorFailToCopyFile", $ori_task_dir.'/'.$file['name'], $clone_task_dir.'/'.$file['name']);
1740  $error++;
1741  }
1742  }
1743  }
1744 
1745  // clone affectation
1746  if ($clone_affectation)
1747  {
1748  $origin_task = new Task($this->db);
1749  $origin_task->fetch($fromid);
1750 
1751  foreach (array('internal', 'external') as $source)
1752  {
1753  $tab = $origin_task->liste_contact(-1, $source);
1754  $num = count($tab);
1755  $i = 0;
1756  while ($i < $num)
1757  {
1758  $clone_task->add_contact($tab[$i]['id'], $tab[$i]['code'], $tab[$i]['source']);
1759  if ($clone_task->error == 'DB_ERROR_RECORD_ALREADY_EXISTS')
1760  {
1761  $langs->load("errors");
1762  $this->error .= $langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType");
1763  $error++;
1764  } else {
1765  if ($clone_task->error != '')
1766  {
1767  $this->error .= $clone_task->error;
1768  $error++;
1769  }
1770  }
1771  $i++;
1772  }
1773  }
1774  }
1775 
1776  if ($clone_time)
1777  {
1778  //TODO clone time of affectation
1779  }
1780  }
1781 
1782  unset($clone_task->context['createfromclone']);
1783 
1784  if (!$error)
1785  {
1786  $this->db->commit();
1787  return $clone_task_id;
1788  } else {
1789  $this->db->rollback();
1790  dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : ".$this->error, LOG_ERR);
1791  return -1;
1792  }
1793  }
1794 
1795 
1802  public function getLibStatut($mode = 0)
1803  {
1804  return $this->LibStatut($this->fk_statut, $mode);
1805  }
1806 
1807  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1815  public function LibStatut($status, $mode = 0)
1816  {
1817  // phpcs:enable
1818  global $langs;
1819 
1820  // list of Statut of the task
1821  $this->statuts[0] = 'Draft';
1822  $this->statuts[1] = 'ToDo';
1823  $this->statuts[2] = 'Running';
1824  $this->statuts[3] = 'Finish';
1825  $this->statuts[4] = 'Transfered';
1826  $this->statuts_short[0] = 'Draft';
1827  $this->statuts_short[1] = 'ToDo';
1828  $this->statuts_short[2] = 'Running';
1829  $this->statuts_short[3] = 'Completed';
1830  $this->statuts_short[4] = 'Transfered';
1831 
1832  if ($mode == 0)
1833  {
1834  return $langs->trans($this->statuts[$status]);
1835  } elseif ($mode == 1)
1836  {
1837  return $langs->trans($this->statuts_short[$status]);
1838  } elseif ($mode == 2)
1839  {
1840  if ($status == 0) return img_picto($langs->trans($this->statuts_short[$status]), 'statut0').' '.$langs->trans($this->statuts_short[$status]);
1841  elseif ($status == 1) return img_picto($langs->trans($this->statuts_short[$status]), 'statut1').' '.$langs->trans($this->statuts_short[$status]);
1842  elseif ($status == 2) return img_picto($langs->trans($this->statuts_short[$status]), 'statut3').' '.$langs->trans($this->statuts_short[$status]);
1843  elseif ($status == 3) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts_short[$status]);
1844  elseif ($status == 4) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts_short[$status]);
1845  elseif ($status == 5) return img_picto($langs->trans($this->statuts_short[$status]), 'statut5').' '.$langs->trans($this->statuts_short[$status]);
1846  } elseif ($mode == 3)
1847  {
1848  if ($status == 0) return img_picto($langs->trans($this->statuts_short[$status]), 'statut0');
1849  elseif ($status == 1) return img_picto($langs->trans($this->statuts_short[$status]), 'statut1');
1850  elseif ($status == 2) return img_picto($langs->trans($this->statuts_short[$status]), 'statut3');
1851  elseif ($status == 3) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
1852  elseif ($status == 4) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6');
1853  elseif ($status == 5) return img_picto($langs->trans($this->statuts_short[$status]), 'statut5');
1854  } elseif ($mode == 4)
1855  {
1856  if ($status == 0) return img_picto($langs->trans($this->statuts_short[$status]), 'statut0').' '.$langs->trans($this->statuts[$status]);
1857  elseif ($status == 1) return img_picto($langs->trans($this->statuts_short[$status]), 'statut1').' '.$langs->trans($this->statuts[$status]);
1858  elseif ($status == 2) return img_picto($langs->trans($this->statuts_short[$status]), 'statut3').' '.$langs->trans($this->statuts[$status]);
1859  elseif ($status == 3) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts[$status]);
1860  elseif ($status == 4) return img_picto($langs->trans($this->statuts_short[$status]), 'statut6').' '.$langs->trans($this->statuts[$status]);
1861  elseif ($status == 5) return img_picto($langs->trans($this->statuts_short[$status]), 'statut5').' '.$langs->trans($this->statuts[$status]);
1862  } elseif ($mode == 5)
1863  {
1864  /*if ($status==0) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut0');
1865  elseif ($status==1) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut1');
1866  elseif ($status==2) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut3');
1867  elseif ($status==3) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut6');
1868  elseif ($status==4) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut6');
1869  elseif ($status==5) return $langs->trans($this->statuts_short[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut5');
1870  */
1871  //else return $this->progress.' %';
1872  return '&nbsp;';
1873  } elseif ($mode == 6)
1874  {
1875  /*if ($status==0) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut0');
1876  elseif ($status==1) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut1');
1877  elseif ($status==2) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut3');
1878  elseif ($status==3) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut6');
1879  elseif ($status==4) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut6');
1880  elseif ($status==5) return $langs->trans($this->statuts[$status]).' '.img_picto($langs->trans($this->statuts_short[$status]),'statut5');
1881  */
1882  //else return $this->progress.' %';
1883  return '&nbsp;';
1884  }
1885  }
1886 
1897  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1898  {
1899  global $conf;
1900 
1901  $outputlangs->load("projects");
1902 
1903  if (!dol_strlen($modele)) {
1904  $modele = 'nodefault';
1905 
1906  if (!empty($this->model_pdf)) {
1907  $modele = $this->model_pdf;
1908  } elseif (!empty($this->modelpdf)) { // deprecated
1909  $modele = $this->modelpdf;
1910  } elseif (!empty($conf->global->PROJECT_TASK_ADDON_PDF)) {
1911  $modele = $conf->global->PROJECT_TASK_ADDON_PDF;
1912  }
1913  }
1914 
1915  $modelpath = "core/modules/project/task/doc/";
1916 
1917  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1918  }
1919 
1920 
1921  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1928  public function load_board($user)
1929  {
1930  // phpcs:enable
1931  global $conf, $langs;
1932 
1933  // For external user, no check is done on company because readability is managed by public status of project and assignement.
1934  //$socid=$user->socid;
1935 
1936  $projectstatic = new Project($this->db);
1937  $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, $socid);
1938 
1939  // List of tasks (does not care about permissions. Filtering will be done later)
1940  $sql = "SELECT p.rowid as projectid, p.fk_statut as projectstatus,";
1941  $sql .= " t.rowid as taskid, t.progress as progress, t.fk_statut as status,";
1942  $sql .= " t.dateo as date_start, t.datee as datee";
1943  $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
1944  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
1945  //if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
1946  $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
1947  $sql .= " WHERE p.entity IN (".getEntity('project', 0).')';
1948  $sql .= " AND p.fk_statut = 1";
1949  $sql .= " AND t.fk_projet = p.rowid";
1950  $sql .= " AND (t.progress IS NULL OR t.progress < 100)"; // tasks to do
1951  if (!$user->rights->projet->all->lire) $sql .= " AND p.rowid IN (".$projectsListId.")";
1952  // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
1953  //if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
1954  if ($socid) $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
1955  // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
1956  // if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id.") OR (s.rowid IS NULL))";
1957 
1958  //print $sql;
1959  $resql = $this->db->query($sql);
1960  if ($resql)
1961  {
1962  $task_static = new Task($this->db);
1963 
1964  $response = new WorkboardResponse();
1965  $response->warning_delay = $conf->projet->task->warning_delay / 60 / 60 / 24;
1966  $response->label = $langs->trans("OpenedTasks");
1967  if ($user->rights->projet->all->lire) $response->url = DOL_URL_ROOT.'/projet/tasks/list.php?mainmenu=project';
1968  else $response->url = DOL_URL_ROOT.'/projet/tasks/list.php?mode=mine&amp;mainmenu=project';
1969  $response->img = img_object('', "task");
1970 
1971  // This assignment in condition is not a bug. It allows walking the results.
1972  while ($obj = $this->db->fetch_object($resql))
1973  {
1974  $response->nbtodo++;
1975 
1976  $task_static->projectstatus = $obj->projectstatus;
1977  $task_static->progress = $obj->progress;
1978  $task_static->fk_statut = $obj->status;
1979  $task_static->date_end = $this->db->jdate($obj->datee);
1980 
1981  if ($task_static->hasDelay()) {
1982  $response->nbtodolate++;
1983  }
1984  }
1985 
1986  return $response;
1987  } else {
1988  $this->error = $this->db->error();
1989  return -1;
1990  }
1991  }
1992 
1993 
1994  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2000  public function load_state_board()
2001  {
2002  // phpcs:enable
2003  global $user;
2004 
2005  $mine = 0; $socid = $user->socid;
2006 
2007  $projectstatic = new Project($this->db);
2008  $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, $mine, 1, $socid);
2009 
2010  // List of tasks (does not care about permissions. Filtering will be done later)
2011  $sql = "SELECT count(p.rowid) as nb";
2012  $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2013  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2014  if (!$user->rights->societe->client->voir && !$socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2015  $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
2016  $sql .= " WHERE p.entity IN (".getEntity('project', 0).')';
2017  $sql .= " AND t.fk_projet = p.rowid"; // tasks to do
2018  if ($mine || !$user->rights->projet->all->lire) $sql .= " AND p.rowid IN (".$projectsListId.")";
2019  // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2020  //if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
2021  if ($socid) $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
2022  if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id.") OR (s.rowid IS NULL))";
2023 
2024  $resql = $this->db->query($sql);
2025  if ($resql)
2026  {
2027  // This assignment in condition is not a bug. It allows walking the results.
2028  while ($obj = $this->db->fetch_object($resql))
2029  {
2030  $this->nb["tasks"] = $obj->nb;
2031  }
2032  $this->db->free($resql);
2033  return 1;
2034  } else {
2035  dol_print_error($this->db);
2036  $this->error = $this->db->error();
2037  return -1;
2038  }
2039  }
2040 
2046  public function hasDelay()
2047  {
2048  global $conf;
2049 
2050  if (!($this->progress >= 0 && $this->progress < 100)) {
2051  return false;
2052  }
2053 
2054  $now = dol_now();
2055 
2056  $datetouse = ($this->date_end > 0) ? $this->date_end : ($this->datee > 0 ? $this->datee : 0);
2057 
2058  return ($datetouse > 0 && ($datetouse < ($now - $conf->projet->task->warning_delay)));
2059  }
2060 }
hasTimeSpent()
Return nb of time spent.
Definition: task.class.php:606
fetchTimeSpent($id)
Load one record of time spent.
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...
Definition: task.class.php:980
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
Copy a file to another file.
Definition: files.lib.php:663
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:346
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
isObjectUsed($id=0)
Function to check if an object is used by others.
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
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())
Return list of tasks for all projects or for one particular project Sort order is on project...
Definition: task.class.php:737
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:644
create($user, $notrigger=0)
Create into database.
Definition: task.class.php:144
addTimeSpent($user, $notrigger=0)
Add time spent.
$conf db
API class for accounts.
Definition: inc.php:54
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
Definition: files.lib.php:817
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:703
Class to manage projects.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
fetchAllTimeSpent(User $userobj, $morewherefilter= '')
Load all records of time spent.
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:576
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getSumOfAmount($fuser= '', $dates= '', $datee= '')
Calculate quantity and value of time consumed using the thm (hourly amount value of work for user ent...
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
Definition: files.lib.php:1286
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:131
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create an intervention document on disk using template defined into PROJECT_TASK_ADDON_PDF.
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...
print $_SERVER["PHP_SELF"]
Edit parameters.
getSummaryOfTimeSpent($userobj=null, $morewherefilter= '')
Calculate total of time spent for task.
Class to manage tasks.
Definition: task.class.php:35
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.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
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:246
load_state_board()
Charge indicateurs this-&gt;nb de tableau de bord.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
if(!empty($search_group)) natural_search(array("g.nom"g note
Definition: list.php:122
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)
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.