dolibarr 21.0.3
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-2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2020 Juanjo Menent <jmenent@2byte.es>
7 * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
8 * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
9 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
10 * Copyright (C) 2024 Vincent de Grandpré <vincent@de-grandpre.quebec>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
32require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
34require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/class/timespent.class.php';
36
37
42{
46 public $element = 'project_task';
47
51 public $table_element = 'projet_task';
52
56 public $fk_element = 'fk_element';
57
61 public $picto = 'projecttask';
62
66 protected $childtables = array(
67 'element_time' => array('name' => 'Task', 'parent' => 'projet_task', 'parentkey' => 'fk_element', 'parenttypefield' => 'elementtype', 'parenttypevalue' => 'task')
68 );
69
73 public $fk_task_parent = 0;
74
78 public $label;
79
83 public $description;
84
88 public $duration_effective;
89
93 public $planned_workload;
94
99 public $date_c;
100
104 public $progress;
105
111 public $dateo;
112
117 public $date_start;
118
124 public $datee;
125
130 public $date_end;
131
137 public $fk_statut;
138
142 public $status;
143
147 public $priority;
148
152 public $fk_user_creat;
153
157 public $fk_user_valid;
158
162 public $rang;
163
167 public $timespent_min_date;
171 public $timespent_max_date;
175 public $timespent_total_duration;
179 public $timespent_total_amount;
183 public $timespent_nblinesnull;
187 public $timespent_nblines;
188 // For detail of lines of timespent record, there is the property ->lines in common
189
190 // Var used to call method addTimeSpent(). Bad practice.
194 public $timespent_id;
198 public $timespent_duration;
202 public $timespent_old_duration;
206 public $timespent_date;
210 public $timespent_datehour; // More accurate start date (same than timespent_date but includes hours, minutes and seconds)
211
215 public $timespent_withhour; // 0 or 1 = we have entered also start hours for timesheet line
220 public $timespent_fk_user;
224 public $timespent_thm;
228 public $timespent_note;
232 public $timespent_fk_product;
236 public $timespent_invoiceid;
240 public $timespent_invoicelineid;
241
242 public $comments = array();
243
244 // Properties calculated from sum of llx_element_time linked to task
248 public $tobill;
249
253 public $billed;
254
255 // Properties to store project information
259 public $projectref;
260
264 public $projectstatus;
265
269 public $projectlabel;
270
274 public $opp_amount;
275
279 public $opp_percent;
280
284 public $fk_opp_status;
285
289 public $usage_bill_time;
290
294 public $public;
295
299 public $array_options_project;
300
301 // Properties to store thirdparty of project information
302
308 public $socid;
309
313 public $thirdparty_id;
314
318 public $thirdparty_name;
319
323 public $thirdparty_email;
324
325 // store parent ref and position
329 public $task_parent_ref;
330
334 public $task_parent_position;
335
340 public $billable = 1;
341
345 public $budget_amount;
346
350 public $project_budget_amount;
351
355 const STATUS_DRAFT = 0;
356
361
365 const STATUS_CLOSED = 3;
366
371
376
377
383 public function __construct($db)
384 {
385 $this->db = $db;
386 }
387
388
396 public function create($user, $notrigger = 0)
397 {
398 global $conf, $langs;
399
400 //For the date
401 $now = dol_now();
402
403 $error = 0;
404
405 // Clean parameters
406 $this->label = trim($this->label);
407 $this->description = trim($this->description);
408 $this->note_public = trim($this->note_public);
409 $this->note_private = trim($this->note_private);
410
411 if (!empty($this->date_start) && !empty($this->date_end) && $this->date_start > $this->date_end) {
412 $this->errors[] = $langs->trans('StartDateCannotBeAfterEndDate');
413 return -1;
414 }
415
416 // Insert request
417 $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet_task (";
418 $sql .= "entity";
419 $sql .= ", fk_projet";
420 $sql .= ", ref";
421 $sql .= ", fk_task_parent";
422 $sql .= ", label";
423 $sql .= ", description";
424 $sql .= ", note_public";
425 $sql .= ", note_private";
426 $sql .= ", datec";
427 $sql .= ", fk_user_creat";
428 $sql .= ", dateo";
429 $sql .= ", datee";
430 $sql .= ", planned_workload";
431 $sql .= ", progress";
432 $sql .= ", budget_amount";
433 $sql .= ", priority";
434 $sql .= ", billable";
435 $sql .= ") VALUES (";
436 $sql .= (!empty($this->entity) ? (int) $this->entity : (int) $conf->entity);
437 $sql .= ", ".((int) $this->fk_project);
438 $sql .= ", ".(!empty($this->ref) ? "'".$this->db->escape($this->ref)."'" : 'null');
439 $sql .= ", ".((int) $this->fk_task_parent);
440 $sql .= ", '".$this->db->escape($this->label)."'";
441 $sql .= ", '".$this->db->escape($this->description)."'";
442 $sql .= ", '".$this->db->escape($this->note_public)."'";
443 $sql .= ", '".$this->db->escape($this->note_private)."'";
444 $sql .= ", '".$this->db->idate($now)."'";
445 $sql .= ", ".((int) $user->id);
446 $sql .= ", ".(isDolTms($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : 'null');
447 $sql .= ", ".(isDolTms($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : 'null');
448 $sql .= ", ".(($this->planned_workload != '' && $this->planned_workload >= 0) ? ((int) $this->planned_workload) : 'null');
449 $sql .= ", ".(($this->progress != '' && $this->progress >= 0) ? ((int) $this->progress) : 'null');
450 $sql .= ", ".(($this->budget_amount != '' && $this->budget_amount >= 0) ? ((int) $this->budget_amount) : 'null');
451 $sql .= ", ".(($this->priority != '' && $this->priority >= 0) ? (int) $this->priority : 'null');
452 $sql .= ", ".((int) $this->billable);
453 $sql .= ")";
454
455 $this->db->begin();
456
457 dol_syslog(get_class($this)."::create", LOG_DEBUG);
458 $resql = $this->db->query($sql);
459 if (!$resql) {
460 $error++;
461 $this->errors[] = "Error ".$this->db->lasterror();
462 }
463
464 if (!$error) {
465 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet_task");
466 // Update extrafield
467 $result = $this->insertExtraFields();
468 if ($result < 0) {
469 $error++;
470 }
471 }
472
473 if (!$error) {
474 if (!$notrigger) {
475 // Call trigger
476 $result = $this->call_trigger('TASK_CREATE', $user);
477 if ($result < 0) {
478 $error++;
479 }
480 // End call triggers
481 }
482 }
483
484 // Commit or rollback
485 if ($error) {
486 foreach ($this->errors as $errmsg) {
487 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
488 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
489 }
490 $this->db->rollback();
491 return -1 * $error;
492 } else {
493 $this->db->commit();
494 return $this->id;
495 }
496 }
497
498
507 public function fetch($id, $ref = '', $loadparentdata = 0)
508 {
509 $sql = "SELECT";
510 $sql .= " t.rowid,";
511 $sql .= " t.ref,";
512 $sql .= " t.entity,";
513 $sql .= " t.fk_projet as fk_project,";
514 $sql .= " t.fk_task_parent,";
515 $sql .= " t.label,";
516 $sql .= " t.description,";
517 $sql .= " t.duration_effective,";
518 $sql .= " t.planned_workload,";
519 $sql .= " t.datec,";
520 $sql .= " t.dateo as date_start,";
521 $sql .= " t.datee as date_end,";
522 $sql .= " t.fk_user_creat,";
523 $sql .= " t.fk_user_valid,";
524 $sql .= " t.fk_statut as status,";
525 $sql .= " t.progress,";
526 $sql .= " t.budget_amount,";
527 $sql .= " t.priority,";
528 $sql .= " t.note_private,";
529 $sql .= " t.note_public,";
530 $sql .= " t.rang,";
531 $sql .= " t.billable";
532 if (!empty($loadparentdata)) {
533 $sql .= ", t2.ref as task_parent_ref";
534 $sql .= ", t2.rang as task_parent_position";
535 }
536 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as t";
537 if (!empty($loadparentdata)) {
538 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t2 ON t.fk_task_parent = t2.rowid";
539 }
540 $sql .= " WHERE ";
541 if (!empty($ref)) {
542 $sql .= "entity IN (".getEntity('project').")";
543 $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
544 } else {
545 $sql .= "t.rowid = ".((int) $id);
546 }
547
548 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
549 $resql = $this->db->query($sql);
550 if ($resql) {
551 $num_rows = $this->db->num_rows($resql);
552
553 if ($num_rows) {
554 $obj = $this->db->fetch_object($resql);
555
556 $this->id = $obj->rowid;
557 $this->ref = $obj->ref;
558 $this->entity = $obj->entity;
559 $this->fk_project = $obj->fk_project;
560 $this->fk_task_parent = $obj->fk_task_parent;
561 $this->label = $obj->label;
562 $this->description = $obj->description;
563 $this->duration_effective = $obj->duration_effective;
564 $this->planned_workload = $obj->planned_workload;
565 $this->date_c = $this->db->jdate($obj->datec);
566 $this->date_start = $this->db->jdate($obj->date_start);
567 $this->date_end = $this->db->jdate($obj->date_end);
568 $this->fk_user_creat = $obj->fk_user_creat;
569 $this->fk_user_valid = $obj->fk_user_valid;
570 $this->fk_statut = $obj->status;
571 $this->status = $obj->status;
572 $this->progress = $obj->progress;
573 $this->budget_amount = $obj->budget_amount;
574 $this->priority = $obj->priority;
575 $this->note_private = $obj->note_private;
576 $this->note_public = $obj->note_public;
577 $this->rang = $obj->rang;
578
579 if (!empty($loadparentdata)) {
580 $this->task_parent_ref = $obj->task_parent_ref;
581 $this->task_parent_position = $obj->task_parent_position;
582 }
583 $this->billable = $obj->billable;
584
585 // Retrieve all extrafield
586 $this->fetch_optionals();
587 }
588
589 $this->db->free($resql);
590
591 if ($num_rows) {
592 return 1;
593 } else {
594 return 0;
595 }
596 } else {
597 $this->error = "Error ".$this->db->lasterror();
598 return -1;
599 }
600 }
601
602
610 public function update($user = null, $notrigger = 0)
611 {
612 global $conf, $langs;
613 $error = 0;
614
615 // Clean parameters
616 if (isset($this->fk_project)) {
617 $this->fk_project = (int) $this->fk_project;
618 }
619 if (isset($this->ref)) {
620 $this->ref = trim($this->ref);
621 }
622 if (isset($this->fk_task_parent)) {
623 $this->fk_task_parent = (int) $this->fk_task_parent;
624 }
625 if (isset($this->label)) {
626 $this->label = trim($this->label);
627 }
628 if (isset($this->description)) {
629 $this->description = trim($this->description);
630 }
631 if (isset($this->note_public)) {
632 $this->note_public = trim($this->note_public);
633 }
634 if (isset($this->note_private)) {
635 $this->note_private = trim($this->note_private);
636 }
637 if (isset($this->duration_effective)) {
638 $this->duration_effective = trim($this->duration_effective);
639 }
640 if (isset($this->planned_workload)) {
641 $this->planned_workload = trim($this->planned_workload);
642 }
643 if (isset($this->budget_amount)) {
644 $this->budget_amount = (float) $this->budget_amount;
645 }
646
647 if (!empty($this->date_start) && !empty($this->date_end) && $this->date_start > $this->date_end) {
648 $this->errors[] = $langs->trans('StartDateCannotBeAfterEndDate');
649 return -1;
650 }
651
652 // Check parameters
653 // Put here code to add control on parameters values
654
655 // Update request
656 $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
657 $sql .= " fk_projet=".(isset($this->fk_project) ? $this->fk_project : "null").",";
658 $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "'".$this->db->escape($this->id)."'").",";
659 $sql .= " fk_task_parent=".(isset($this->fk_task_parent) ? $this->fk_task_parent : "null").",";
660 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
661 $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
662 $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
663 $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
664 $sql .= " duration_effective=".(isset($this->duration_effective) ? $this->duration_effective : "null").",";
665 $sql .= " planned_workload=".((isset($this->planned_workload) && $this->planned_workload != '') ? $this->planned_workload : "null").",";
666 $sql .= " dateo=".(isDolTms($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : 'null').",";
667 $sql .= " datee=".(isDolTms($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : 'null').",";
668 $sql .= " progress=".(($this->progress != '' && $this->progress >= 0) ? $this->progress : 'null').",";
669 $sql .= " budget_amount=".(($this->budget_amount != '' && $this->budget_amount >= 0) ? $this->budget_amount : 'null').",";
670 $sql .= " rang=".((!empty($this->rang)) ? ((int) $this->rang) : "0").",";
671 $sql .= " priority=".((!empty($this->priority)) ? ((int) $this->priority) : "0").",";
672 $sql .= " billable=".((int) $this->billable);
673 $sql .= " WHERE rowid=".((int) $this->id);
674
675 $this->db->begin();
676
677 dol_syslog(get_class($this)."::update", LOG_DEBUG);
678 $resql = $this->db->query($sql);
679 if (!$resql) {
680 $error++;
681 $this->errors[] = "Error ".$this->db->lasterror();
682 }
683
684 // Update extrafield
685 if (!$error) {
686 $result = $this->insertExtraFields();
687 if ($result < 0) {
688 $error++;
689 }
690 }
691
692 if (!$error && getDolGlobalString('PROJECT_CLASSIFY_CLOSED_WHEN_ALL_TASKS_DONE')) {
693 // Close the parent project if it is open (validated) and its tasks are 100% completed
694 $project = new Project($this->db);
695 if ($project->fetch($this->fk_project) > 0) {
696 if ($project->statut == Project::STATUS_VALIDATED) {
697 $project->getLinesArray(null); // this method does not return <= 0 if fails
698 $projectCompleted = array_reduce(
699 $project->lines,
705 static function ($allTasksCompleted, $task) {
706 return $allTasksCompleted && $task->progress >= 100;
707 },
708 1
709 );
710 if ($projectCompleted) {
711 if ($project->setClose($user) <= 0) {
712 $error++;
713 }
714 }
715 }
716 } else {
717 $error++;
718 }
719 if ($error) {
720 $this->errors[] = $project->error;
721 }
722 }
723
724 if (!$error) {
725 if (!$notrigger) {
726 // Call trigger
727 $result = $this->call_trigger('TASK_MODIFY', $user);
728 if ($result < 0) {
729 $error++;
730 }
731 // End call triggers
732 }
733 }
734
735 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
736 // We remove directory
737 if ($conf->project->dir_output) {
738 $project = new Project($this->db);
739 $project->fetch($this->fk_project);
740
741 $olddir = $conf->project->dir_output.'/'.dol_sanitizeFileName($project->ref).'/'.dol_sanitizeFileName($this->oldcopy->ref);
742 $newdir = $conf->project->dir_output.'/'.dol_sanitizeFileName($project->ref).'/'.dol_sanitizeFileName($this->ref);
743 if (file_exists($olddir)) {
744 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
745 $res = dol_move_dir($olddir, $newdir);
746 if (!$res) {
747 $langs->load("errors");
748 $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
749 $error++;
750 }
751 }
752 }
753 }
754
755 // Commit or rollback
756 if ($error) {
757 foreach ($this->errors as $errmsg) {
758 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
759 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
760 }
761 $this->db->rollback();
762 return -1 * $error;
763 } else {
764 $this->db->commit();
765 return 1;
766 }
767 }
768
769
777 public function delete($user, $notrigger = 0)
778 {
779 global $conf;
780 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
781
782 $error = 0;
783
784 $this->db->begin();
785
786 if ($this->hasChildren() > 0) {
787 dol_syslog(get_class($this)."::delete Can't delete record as it has some sub tasks", LOG_WARNING);
788 $this->error = 'ErrorRecordHasSubTasks';
789 $this->db->rollback();
790 return 0;
791 }
792
793 $objectisused = $this->isObjectUsed($this->id);
794 if (!empty($objectisused)) {
795 dol_syslog(get_class($this)."::delete Can't delete record as it has some child", LOG_WARNING);
796 $this->error = 'ErrorRecordHasChildren';
797 $this->db->rollback();
798 return 0;
799 }
800
801 if (!$error) {
802 // Delete linked contacts
803 $res = $this->delete_linked_contact();
804 if ($res < 0) {
805 $this->error = 'ErrorFailToDeleteLinkedContact';
806 //$error++;
807 $this->db->rollback();
808 return 0;
809 }
810 }
811
812 if (!$error) {
813 $sql = "DELETE FROM ".MAIN_DB_PREFIX."element_time";
814 $sql .= " WHERE fk_element = ".((int) $this->id)." AND elementtype = 'task'";
815
816 $resql = $this->db->query($sql);
817 if (!$resql) {
818 $error++;
819 $this->errors[] = "Error ".$this->db->lasterror();
820 }
821 }
822
823 if (!$error) {
824 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task_extrafields";
825 $sql .= " WHERE fk_object = ".((int) $this->id);
826
827 $resql = $this->db->query($sql);
828 if (!$resql) {
829 $error++;
830 $this->errors[] = "Error ".$this->db->lasterror();
831 }
832 }
833
834 if (!$error) {
835 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task";
836 $sql .= " WHERE rowid=".((int) $this->id);
837
838 $resql = $this->db->query($sql);
839 if (!$resql) {
840 $error++;
841 $this->errors[] = "Error ".$this->db->lasterror();
842 }
843 }
844
845 if (!$error) {
846 if (!$notrigger) {
847 // Call trigger
848 $result = $this->call_trigger('TASK_DELETE', $user);
849 if ($result < 0) {
850 $error++;
851 }
852 // End call triggers
853 }
854 }
855
856 // Commit or rollback
857 if ($error) {
858 foreach ($this->errors as $errmsg) {
859 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
860 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
861 }
862 $this->db->rollback();
863 return -1 * $error;
864 } else {
865 //Delete associated link file
866 if ($conf->project->dir_output) {
867 $projectstatic = new Project($this->db);
868 $projectstatic->fetch($this->fk_project);
869
870 $dir = $conf->project->dir_output."/".dol_sanitizeFileName($projectstatic->ref).'/'.dol_sanitizeFileName((string) $this->id);
871 dol_syslog(get_class($this)."::delete dir=".$dir, LOG_DEBUG);
872 if (file_exists($dir)) {
873 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
874 $res = @dol_delete_dir_recursive($dir);
875 if (!$res) {
876 $this->error = 'ErrorFailToDeleteDir';
877 $this->db->rollback();
878 return 0;
879 }
880 }
881 }
882
883 $this->db->commit();
884
885 return 1;
886 }
887 }
888
894 public function hasChildren()
895 {
896 $error = 0;
897 $ret = 0;
898
899 $sql = "SELECT COUNT(*) as nb";
900 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task";
901 $sql .= " WHERE fk_task_parent = ".((int) $this->id);
902
903 dol_syslog(get_class($this)."::hasChildren", LOG_DEBUG);
904 $resql = $this->db->query($sql);
905 if (!$resql) {
906 $error++;
907 $this->errors[] = "Error ".$this->db->lasterror();
908 } else {
909 $obj = $this->db->fetch_object($resql);
910 if ($obj) {
911 $ret = $obj->nb;
912 }
913 $this->db->free($resql);
914 }
915
916 if (!$error) {
917 return $ret;
918 } else {
919 return -1;
920 }
921 }
922
928 public function hasTimeSpent()
929 {
930 $error = 0;
931 $ret = 0;
932
933 $sql = "SELECT COUNT(*) as nb";
934 $sql .= " FROM ".MAIN_DB_PREFIX."element_time";
935 $sql .= " WHERE fk_element = ".((int) $this->id);
936 $sql .= " AND elementtype = 'task'";
937
938 dol_syslog(get_class($this)."::hasTimeSpent", LOG_DEBUG);
939 $resql = $this->db->query($sql);
940 if (!$resql) {
941 $error++;
942 $this->errors[] = "Error ".$this->db->lasterror();
943 } else {
944 $obj = $this->db->fetch_object($resql);
945 if ($obj) {
946 $ret = $obj->nb;
947 }
948 $this->db->free($resql);
949 }
950
951 if (!$error) {
952 return $ret;
953 } else {
954 return -1;
955 }
956 }
957
958
965 public function getTooltipContentArray($params)
966 {
967 global $langs;
968
969 $langs->load('projects');
970
971 $datas = [];
972 $datas['picto'] = img_picto('', $this->picto).' <u>'.$langs->trans("Task").'</u>';
973 if (!empty($this->ref)) {
974 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
975 }
976 if (!empty($this->label)) {
977 $datas['label'] = '<br><b>'.$langs->trans('LabelTask').':</b> '.$this->label;
978 }
979 if ($this->date_start || $this->date_end) {
980 $datas['range'] = "<br>".get_date_range($this->date_start, $this->date_end, '', $langs, 0);
981 }
982
983 return $datas;
984 }
985
998 public function getNomUrl($withpicto = 0, $option = '', $mode = 'task', $addlabel = 0, $sep = ' - ', $notooltip = 0, $save_lastsearch_value = -1)
999 {
1000 global $action, $conf, $hookmanager, $langs;
1001
1002 if (!empty($conf->dol_no_mouse_hover)) {
1003 $notooltip = 1; // Force disable tooltips
1004 }
1005
1006 $result = '';
1007 $params = [
1008 'id' => $this->id,
1009 'objecttype' => $this->element,
1010 ];
1011 $classfortooltip = 'classfortooltip';
1012 $dataparams = '';
1013 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1014 $classfortooltip = 'classforajaxtooltip';
1015 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1016 $label = '';
1017 } else {
1018 $label = implode($this->getTooltipContentArray($params));
1019 }
1020
1021 $url = DOL_URL_ROOT.'/projet/tasks/'.$mode.'.php?id='.$this->id.($option == 'withproject' ? '&withproject=1' : '');
1022 // Add param to save lastsearch_values or not
1023 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1024 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1025 $add_save_lastsearch_values = 1;
1026 }
1027 if ($add_save_lastsearch_values) {
1028 $url .= '&save_lastsearch_values=1';
1029 }
1030
1031 $linkclose = '';
1032 if (empty($notooltip)) {
1033 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1034 $label = $langs->trans("ShowTask");
1035 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1036 }
1037 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1038 $linkclose .= $dataparams.' class="'.$classfortooltip.' nowraponall"';
1039 } else {
1040 $linkclose .= ' class="nowraponall"';
1041 }
1042
1043 $linkstart = '<a href="'.$url.'"';
1044 $linkstart .= $linkclose.'>';
1045 $linkend = '</a>';
1046
1047 $picto = 'projecttask';
1048
1049 $result .= $linkstart;
1050 if ($withpicto) {
1051 $result .= img_object(($notooltip ? '' : $label), $picto, 'class="paddingright"', 0, 0, $notooltip ? 0 : 1);
1052 }
1053 if ($withpicto != 2) {
1054 $result .= $this->ref;
1055 }
1056 $result .= $linkend;
1057 if ($withpicto != 2) {
1058 $result .= (($addlabel && $this->label) ? $sep.dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1059 }
1060
1061 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1062 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1063 if ($reshook > 0) {
1064 $result = $hookmanager->resPrint;
1065 } else {
1066 $result .= $hookmanager->resPrint;
1067 }
1068
1069 return $result;
1070 }
1071
1079 public function initAsSpecimen()
1080 {
1081 global $user;
1082
1083 $this->id = 0;
1084
1085 $this->fk_project = 0;
1086 $this->ref = 'TK01';
1087 $this->fk_task_parent = 0;
1088 $this->label = 'Specimen task TK01';
1089 $this->duration_effective = '';
1090 $this->fk_user_creat = $user->id;
1091 $this->progress = 25;
1092 $this->status = 0;
1093 $this->priority = 0;
1094 $this->note_private = 'This is a specimen private note';
1095 $this->note_public = 'This is a specimen public note';
1096 $this->billable = 1;
1097
1098 return 1;
1099 }
1100
1124 public function getTasksArray($usert = null, $userp = null, $projectid = 0, $socid = 0, $mode = 0, $filteronproj = '', $filteronprojstatus = '-1', $morewherefilter = '', $filteronprojuser = 0, $filterontaskuser = 0, $extrafields = null, $includebilltime = 0, $search_array_options = array(), $loadextras = 0, $loadRoleMode = 1, $sortfield = '', $sortorder = '')
1125 {
1126 global $hookmanager;
1127
1128 $tasks = array();
1129
1130 //print $usert.'-'.$userp.'-'.$projectid.'-'.$socid.'-'.$mode.'<br>';
1131
1132 // List of tasks (does not care about permissions. Filtering will be done later)
1133 $sql = "SELECT ";
1134 if ($filteronprojuser > 0 || $filterontaskuser > 0) {
1135 $sql .= " DISTINCT"; // We may get several time the same record if user has several roles on same project/task
1136 }
1137 $sql .= " p.rowid as projectid, p.ref, p.title as plabel, p.public, p.fk_statut as projectstatus, p.usage_bill_time,";
1138 $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,";
1139 $sql .= " t.dateo as date_start, t.datee as date_end, t.planned_workload, t.rang, t.priority,";
1140 $sql .= " t.budget_amount, t.billable,";
1141 $sql .= " t.note_public, t.note_private,";
1142 $sql .= " s.rowid as thirdparty_id, s.nom as thirdparty_name, s.email as thirdparty_email,";
1143 $sql .= " p.fk_opp_status, p.opp_amount, p.opp_percent, p.budget_amount as project_budget_amount";
1144 if ($loadextras) { // TODO Replace this with a fetch_optionnal() on the project after the fetch_object of line.
1145 if (!empty($extrafields->attributes['projet']['label'])) {
1146 foreach ($extrafields->attributes['projet']['label'] as $key => $val) {
1147 $sql .= ($extrafields->attributes['projet']['type'][$key] != 'separate' ? ",efp.".$key." as options_".$key : '');
1148 }
1149 }
1150 if (!empty($extrafields->attributes['projet_task']['label'])) {
1151 foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) {
1152 $sql .= ($extrafields->attributes['projet_task']['type'][$key] != 'separate' ? ",efpt.".$key." as options_".$key : '');
1153 }
1154 }
1155 }
1156 if ($includebilltime) {
1157 $sql .= ", SUM(tt.element_duration * ".$this->db->ifsql("invoice_id IS NULL", "1", "0").") as tobill, SUM(tt.element_duration * ".$this->db->ifsql("invoice_id IS NULL", "0", "1").") as billed";
1158 }
1159
1160 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
1161 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
1162 if ($loadextras) {
1163 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_extrafields as efp ON (p.rowid = efp.fk_object)";
1164 }
1165
1166 if ($mode == 0) {
1167 if ($filteronprojuser > 0) {
1168 $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
1169 $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
1170 }
1171 $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
1172 if ($loadextras) {
1173 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task_extrafields as efpt ON (t.rowid = efpt.fk_object)";
1174 }
1175 if ($includebilltime) {
1176 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_time as tt ON (tt.fk_element = t.rowid AND tt.elementtype='task')";
1177 }
1178 if ($filterontaskuser > 0) {
1179 $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec2";
1180 $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc2";
1181 }
1182 $sql .= " WHERE p.entity IN (".getEntity('project').")";
1183 $sql .= " AND t.fk_projet = p.rowid";
1184 } elseif ($mode == 1) {
1185 if ($filteronprojuser > 0) {
1186 $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
1187 $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
1188 }
1189 if ($filterontaskuser > 0) {
1190 $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
1191 if ($includebilltime) {
1192 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_time as tt ON (tt.fk_element = t.rowid AND tt.elementtype='task')";
1193 }
1194 $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec2";
1195 $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc2";
1196 } else {
1197 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t on t.fk_projet = p.rowid";
1198 if ($includebilltime) {
1199 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_time as tt ON (tt.fk_element = t.rowid AND tt.elementtype = 'task')";
1200 }
1201 }
1202 $sql .= " WHERE p.entity IN (".getEntity('project').")";
1203 } else {
1204 return 'BadValueForParameterMode';
1205 }
1206
1207 if ($filteronprojuser > 0) {
1208 $sql .= " AND p.rowid = ec.element_id";
1209 $sql .= " AND ctc.rowid = ec.fk_c_type_contact";
1210 $sql .= " AND ctc.element = 'project'";
1211 $sql .= " AND ec.fk_socpeople = ".((int) $filteronprojuser);
1212 $sql .= " AND ec.statut = 4";
1213 $sql .= " AND ctc.source = 'internal'";
1214 }
1215 if ($filterontaskuser > 0) {
1216 $sql .= " AND t.fk_projet = p.rowid";
1217 $sql .= " AND p.rowid = ec2.element_id";
1218 $sql .= " AND ctc2.rowid = ec2.fk_c_type_contact";
1219 $sql .= " AND ctc2.element = 'project_task'";
1220 $sql .= " AND ec2.fk_socpeople = ".((int) $filterontaskuser);
1221 $sql .= " AND ec2.statut = 4";
1222 $sql .= " AND ctc2.source = 'internal'";
1223 }
1224 if ($socid) {
1225 $sql .= " AND p.fk_soc = ".((int) $socid);
1226 }
1227 if ($projectid) {
1228 $sql .= " AND p.rowid IN (".$this->db->sanitize($projectid).")";
1229 }
1230 if ($filteronproj) {
1231 $sql .= natural_search(array("p.ref", "p.title"), $filteronproj);
1232 }
1233 if ($filteronprojstatus && (int) $filteronprojstatus != '-1') {
1234 $sql .= " AND p.fk_statut IN (".$this->db->sanitize($filteronprojstatus).")";
1235 }
1236 if ($morewherefilter) {
1237 $sql .= $morewherefilter;
1238 }
1239
1240 // Add where from extra fields
1241 $extrafieldsobjectkey = 'projet_task';
1242 $extrafieldsobjectprefix = 'efpt.';
1243 global $db, $conf; // needed for extrafields_list_search_sql.tpl
1244 include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
1245
1246 // Add where from hooks
1247 $parameters = array();
1248 $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
1249 $sql .= $hookmanager->resPrint;
1250 if ($includebilltime) {
1251 $sql .= " GROUP BY p.rowid, p.ref, p.title, p.public, p.fk_statut, p.usage_bill_time,";
1252 $sql .= " t.datec, t.dateo, t.datee, t.tms,";
1253 $sql .= " t.rowid, t.ref, t.label, t.description, t.fk_task_parent, t.duration_effective, t.progress, t.fk_statut,";
1254 $sql .= " t.dateo, t.datee, t.planned_workload, t.rang, t.priority,";
1255 $sql .= " t.budget_amount, t.billable,";
1256 $sql .= " t.note_public, t.note_private,";
1257 $sql .= " s.rowid, s.nom, s.email,";
1258 $sql .= " p.fk_opp_status, p.opp_amount, p.opp_percent, p.budget_amount";
1259 if ($loadextras) {
1260 if (!empty($extrafields->attributes['projet']['label'])) {
1261 foreach ($extrafields->attributes['projet']['label'] as $key => $val) {
1262 $sql .= ($extrafields->attributes['projet']['type'][$key] != 'separate' ? ",efp.".$key : '');
1263 }
1264 }
1265 if (!empty($extrafields->attributes['projet_task']['label'])) {
1266 foreach ($extrafields->attributes['projet_task']['label'] as $key => $val) {
1267 $sql .= ($extrafields->attributes['projet_task']['type'][$key] != 'separate' ? ",efpt.".$key : '');
1268 }
1269 }
1270 }
1271 }
1272
1273 if ($sortfield && $sortorder) {
1274 $sql .= $this->db->order($sortfield, $sortorder);
1275 } else {
1276 $sql .= " ORDER BY p.ref, t.rang, t.dateo";
1277 }
1278
1279 //print $sql;exit;
1280 dol_syslog(get_class($this)."::getTasksArray", LOG_DEBUG);
1281 $resql = $this->db->query($sql);
1282 if ($resql) {
1283 $num = $this->db->num_rows($resql);
1284 $i = 0;
1285 // Loop on each record found, so each couple (project id, task id)
1286 while ($i < $num) {
1287 $error = 0;
1288
1289 $obj = $this->db->fetch_object($resql);
1290
1291 if ($loadRoleMode) {
1292 if ((!$obj->public) && (is_object($userp))) { // If not public project and we ask a filter on project owned by a user
1293 if (!$this->getUserRolesForProjectsOrTasks($userp, null, $obj->projectid, 0)) {
1294 $error++;
1295 }
1296 }
1297 if (is_object($usert)) { // If we ask a filter on a user affected to a task
1298 if (!$this->getUserRolesForProjectsOrTasks(null, $usert, $obj->projectid, $obj->taskid)) {
1299 $error++;
1300 }
1301 }
1302 }
1303
1304 if (!$error) {
1305 $tasks[$i] = new Task($this->db);
1306 $tasks[$i]->id = $obj->taskid;
1307 $tasks[$i]->ref = $obj->taskref;
1308 $tasks[$i]->fk_project = $obj->projectid;
1309
1310 // Data from project
1311 $tasks[$i]->projectref = $obj->ref;
1312 $tasks[$i]->projectlabel = $obj->plabel;
1313 $tasks[$i]->projectstatus = $obj->projectstatus;
1314 $tasks[$i]->fk_opp_status = $obj->fk_opp_status;
1315 $tasks[$i]->opp_amount = $obj->opp_amount;
1316 $tasks[$i]->opp_percent = $obj->opp_percent;
1317 $tasks[$i]->budget_amount = $obj->budget_amount;
1318 $tasks[$i]->project_budget_amount = $obj->project_budget_amount;
1319 $tasks[$i]->usage_bill_time = $obj->usage_bill_time;
1320
1321 $tasks[$i]->label = $obj->label;
1322 $tasks[$i]->description = $obj->description;
1323
1324 $tasks[$i]->fk_task_parent = $obj->fk_task_parent;
1325 $tasks[$i]->note_public = $obj->note_public;
1326 $tasks[$i]->note_private = $obj->note_private;
1327 $tasks[$i]->duration_effective = $obj->duration_effective;
1328 $tasks[$i]->planned_workload = $obj->planned_workload;
1329
1330 if ($includebilltime) {
1331 // Data summed from element_time linked to task
1332 $tasks[$i]->tobill = $obj->tobill;
1333 $tasks[$i]->billed = $obj->billed;
1334 }
1335
1336 $tasks[$i]->progress = $obj->progress;
1337 $tasks[$i]->fk_statut = $obj->status;
1338 $tasks[$i]->status = $obj->status;
1339 $tasks[$i]->public = $obj->public;
1340 $tasks[$i]->date_start = $this->db->jdate($obj->date_start);
1341 $tasks[$i]->date_end = $this->db->jdate($obj->date_end);
1342 $tasks[$i]->rang = $obj->rang;
1343 $tasks[$i]->priority = $obj->priority;
1344
1345 $tasks[$i]->socid = $obj->thirdparty_id; // For backward compatibility
1346 $tasks[$i]->thirdparty_id = $obj->thirdparty_id;
1347 $tasks[$i]->thirdparty_name = $obj->thirdparty_name;
1348 $tasks[$i]->thirdparty_email = $obj->thirdparty_email;
1349
1350 $tasks[$i]->billable = $obj->billable;
1351
1352 if ($loadextras) {
1353 if (!empty($extrafields->attributes['projet']['label'])) {
1354 foreach ($extrafields->attributes['projet']['label'] as $key => $val) {
1355 if ($extrafields->attributes['projet']['type'][$key] != 'separate') {
1356 $tmpvar = 'options_'.$key;
1357 $tasks[$i]->array_options_project['options_'.$key] = $obj->$tmpvar;
1358 }
1359 }
1360 }
1361 }
1362
1363 if ($loadextras) {
1364 $tasks[$i]->fetch_optionals();
1365 }
1366 }
1367
1368 $i++;
1369 }
1370 $this->db->free($resql);
1371 } else {
1372 dol_print_error($this->db);
1373 }
1374
1375 return $tasks;
1376 }
1377
1388 public function getUserRolesForProjectsOrTasks($userp, $usert, $projectid = '', $taskid = 0, $filteronprojstatus = -1)
1389 {
1390 $arrayroles = array();
1391
1392 dol_syslog(get_class($this)."::getUserRolesForProjectsOrTasks userp=".json_encode(is_object($userp))." usert=".json_encode(is_object($usert))." projectid=".$projectid." taskid=".$taskid);
1393
1394 // We want role of user for a projet or role of user for a task. Both are not possible.
1395 if (empty($userp) && empty($usert)) {
1396 $this->error = "CallWithWrongParameters";
1397 return -1;
1398 }
1399 if (!empty($userp) && !empty($usert)) {
1400 $this->error = "CallWithWrongParameters";
1401 return -1;
1402 }
1403
1404 /* Liste des taches et role sur les projects ou taches */
1405 $sql = "SELECT ";
1406 if ($userp) {
1407 $sql .= " p.rowid as pid,";
1408 } else {
1409 $sql .= " pt.rowid as pid,";
1410 }
1411 $sql .= " ec.element_id, ctc.code, ctc.source";
1412 if ($userp) {
1413 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
1414 }
1415 if ($usert && $filteronprojstatus > -1) {
1416 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p, ".MAIN_DB_PREFIX."projet_task as pt";
1417 }
1418 if ($usert && $filteronprojstatus <= -1) {
1419 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task as pt";
1420 }
1421 $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
1422 $sql .= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
1423 if ($userp) {
1424 $sql .= " WHERE p.rowid = ec.element_id";
1425 } else {
1426 $sql .= " WHERE pt.rowid = ec.element_id";
1427 }
1428 if ($userp && $filteronprojstatus > -1) {
1429 $sql .= " AND p.fk_statut = ".((int) $filteronprojstatus);
1430 }
1431 if ($usert && $filteronprojstatus > -1) {
1432 $sql .= " AND pt.fk_projet = p.rowid AND p.fk_statut = ".((int) $filteronprojstatus);
1433 }
1434 if ($userp) {
1435 $sql .= " AND ctc.element = 'project'";
1436 }
1437 if ($usert) {
1438 $sql .= " AND ctc.element = 'project_task'";
1439 }
1440 $sql .= " AND ctc.rowid = ec.fk_c_type_contact";
1441 if ($userp) {
1442 $sql .= " AND ec.fk_socpeople = ".((int) $userp->id);
1443 }
1444 if ($usert) {
1445 $sql .= " AND ec.fk_socpeople = ".((int) $usert->id);
1446 }
1447 $sql .= " AND ec.statut = 4";
1448 $sql .= " AND ctc.source = 'internal'";
1449 if ($projectid) {
1450 if ($userp) {
1451 $sql .= " AND p.rowid IN (".$this->db->sanitize($projectid).")";
1452 }
1453 if ($usert) {
1454 $sql .= " AND pt.fk_projet IN (".$this->db->sanitize($projectid).")";
1455 }
1456 }
1457 if ($taskid) {
1458 if ($userp) {
1459 $sql .= " ERROR SHOULD NOT HAPPENS";
1460 }
1461 if ($usert) {
1462 $sql .= " AND pt.rowid = ".((int) $taskid);
1463 }
1464 }
1465 //print $sql;
1466
1467 dol_syslog(get_class($this)."::getUserRolesForProjectsOrTasks execute request", LOG_DEBUG);
1468 $resql = $this->db->query($sql);
1469 if ($resql) {
1470 $num = $this->db->num_rows($resql);
1471 $i = 0;
1472 while ($i < $num) {
1473 $obj = $this->db->fetch_object($resql);
1474 if (empty($arrayroles[$obj->pid])) {
1475 $arrayroles[$obj->pid] = $obj->code;
1476 } else {
1477 $arrayroles[$obj->pid] .= ','.$obj->code;
1478 }
1479 $i++;
1480 }
1481 $this->db->free($resql);
1482 } else {
1483 dol_print_error($this->db);
1484 }
1485
1486 return $arrayroles;
1487 }
1488
1489
1496 public function getListContactId($source = 'internal')
1497 {
1498 $contactAlreadySelected = array();
1499 $tab = $this->liste_contact(-1, $source);
1500 //var_dump($tab);
1501 $num = count($tab);
1502 $i = 0;
1503 while ($i < $num) {
1504 if ($source == 'thirdparty') {
1505 $contactAlreadySelected[$i] = $tab[$i]['socid'];
1506 } else {
1507 $contactAlreadySelected[$i] = $tab[$i]['id'];
1508 }
1509 $i++;
1510 }
1511 return $contactAlreadySelected;
1512 }
1513
1521 public function mergeContactTask($origin_id, $dest_id)
1522 {
1523 $error = 0;
1524 $origintask = new Task($this->db);
1525 $result = $origintask->fetch($origin_id);
1526 if ($result <= 0) {
1527 return false;
1528 }
1529
1530 //Get list of origin contacts
1531 $arraycontactorigin = array_merge($origintask->liste_contact(-1, 'internal'), $origintask->liste_contact(-1, 'external'));
1532 if (is_array($arraycontactorigin)) {
1533 foreach ($arraycontactorigin as $key => $contact) {
1534 $result = $this->add_contact($contact["id"], $contact["fk_c_type_contact"], $contact["source"]);
1535 if ($result < 0) {
1536 return false;
1537 }
1538 }
1539 }
1540 return true;
1541 }
1542
1550 public function mergeTimeSpentTask($origin_id, $dest_id)
1551 {
1552 $ret = true;
1553
1554 $this->db->begin();
1555
1556 $sql = "UPDATE ".MAIN_DB_PREFIX."element_time as et";
1557 $sql .= " SET et.fk_element = ".((int) $dest_id);
1558 $sql .= " WHERE et.elementtype = 'task'";
1559 $sql .= " AND et.fk_element = ".((int) $origin_id);
1560
1561 dol_syslog(get_class($this)."::mergeTimeSpentTask", LOG_DEBUG);
1562 if (!$this->db->query($sql)) {
1563 $this->error = $this->db->lasterror();
1564 $ret = false;
1565 }
1566
1567 if ($ret) {
1568 $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
1569 $sql .= " SET duration_effective = (SELECT SUM(element_duration) FROM ".MAIN_DB_PREFIX."element_time as ptt where ptt.elementtype = 'task' AND ptt.fk_element = ".((int) $dest_id).")";
1570 $sql .= " WHERE rowid = ".((int) $dest_id);
1571
1572 dol_syslog(get_class($this)."::mergeTimeSpentTask update project_task", LOG_DEBUG);
1573 if (!$this->db->query($sql)) {
1574 $this->error = $this->db->lasterror();
1575 $ret = false;
1576 }
1577 }
1578
1579 if ($ret) {
1580 $this->db->commit();
1581 } else {
1582 $this->db->rollback();
1583 }
1584 return $ret;
1585 }
1586
1594 public function addTimeSpent($user, $notrigger = 0)
1595 {
1596 global $langs;
1597
1598 dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1599
1600 $ret = 0;
1601 $now = dol_now();
1602
1603 // Check parameters
1604 if (!is_object($user)) {
1605 dol_print_error(null, "Method addTimeSpent was called with wrong parameter user");
1606 return -1;
1607 }
1608
1609 // Clean parameters
1610 if (isset($this->timespent_note)) {
1611 $this->timespent_note = trim($this->timespent_note);
1612 }
1613 if (empty($this->timespent_datehour) || ($this->timespent_date != $this->timespent_datehour)) {
1614 $this->timespent_datehour = $this->timespent_date;
1615 }
1616
1617 if (getDolGlobalInt('PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS')) {
1618 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
1619 $restrictBefore = dol_time_plus_duree(dol_now(), - getDolGlobalInt('PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS'), 'm');
1620
1621 if ($this->timespent_date < $restrictBefore) {
1622 $this->error = $langs->trans('TimeRecordingRestrictedToNMonthsBack', getDolGlobalString('PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS'));
1623 $this->errors[] = $this->error;
1624 return -1;
1625 }
1626 }
1627
1628 $this->db->begin();
1629
1630 $timespent = new TimeSpent($this->db);
1631 $timespent->fk_element = $this->id;
1632 $timespent->elementtype = 'task';
1633 $timespent->element_date = $this->timespent_date;
1634 $timespent->element_datehour = $this->timespent_datehour;
1635 $timespent->element_date_withhour = $this->timespent_withhour;
1636 $timespent->element_duration = $this->timespent_duration;
1637 $timespent->fk_user = $this->timespent_fk_user;
1638 $timespent->fk_product = $this->timespent_fk_product;
1639 $timespent->note = $this->timespent_note;
1640 $timespent->datec = $now;
1641
1642 $result = $timespent->create($user);
1643 if ($result > 0) {
1644 $ret = $result;
1645 $this->timespent_id = $result;
1646
1647 if (!$notrigger) {
1648 // Call trigger
1649 $result = $this->call_trigger('TASK_TIMESPENT_CREATE', $user);
1650 if ($result < 0) {
1651 $ret = -1;
1652 }
1653 // End call triggers
1654 }
1655 } else {
1656 $this->error = $this->db->lasterror();
1657 $ret = -1;
1658 }
1659
1660 if ($ret > 0) {
1661 // Recalculate amount of time spent for task and update denormalized field
1662 $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
1663 $sql .= " SET duration_effective = (SELECT SUM(element_duration) FROM ".MAIN_DB_PREFIX."element_time as ptt where ptt.elementtype = 'task' AND ptt.fk_element = ".((int) $this->id).")";
1664 if (isset($this->progress)) {
1665 $sql .= ", progress = ".((float) $this->progress); // Do not overwrite value if not provided
1666 }
1667 $sql .= " WHERE rowid = ".((int) $this->id);
1668
1669 dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1670 if (!$this->db->query($sql)) {
1671 $this->error = $this->db->lasterror();
1672 $ret = -2;
1673 }
1674
1675 // Update hourly rate of this time spent entry
1676 $resql_thm_user = $this->db->query("SELECT thm FROM " . MAIN_DB_PREFIX . "user WHERE rowid = " . ((int) $timespent->fk_user));
1677 if (!empty($resql_thm_user)) {
1678 $obj_thm_user = $this->db->fetch_object($resql_thm_user);
1679 $timespent->thm = $obj_thm_user->thm;
1680 }
1681 $res_update = $timespent->update($user);
1682
1683 dol_syslog(get_class($this)."::addTimeSpent", LOG_DEBUG);
1684 if ($res_update <= 0) {
1685 $this->error = $this->db->lasterror();
1686 $ret = -2;
1687 }
1688 }
1689
1690 if ($ret > 0) {
1691 $this->db->commit();
1692 } else {
1693 $this->db->rollback();
1694 }
1695 return $ret;
1696 }
1697
1704 public function fetchTimeSpentOnTask($morewherefilter = '')
1705 {
1706 $arrayres = array();
1707
1708 $sql = "SELECT";
1709 $sql .= " s.rowid as socid,";
1710 $sql .= " s.nom as thirdparty_name,";
1711 $sql .= " s.email as thirdparty_email,";
1712 $sql .= " ptt.rowid,";
1713 $sql .= " ptt.ref_ext,";
1714 $sql .= " ptt.fk_element as fk_task,";
1715 $sql .= " ptt.element_date as task_date,";
1716 $sql .= " ptt.element_datehour as task_datehour,";
1717 $sql .= " ptt.element_date_withhour as task_date_withhour,";
1718 $sql .= " ptt.element_duration as task_duration,";
1719 $sql .= " ptt.fk_user,";
1720 $sql .= " ptt.note,";
1721 $sql .= " ptt.thm,";
1722 $sql .= " pt.rowid as task_id,";
1723 $sql .= " pt.ref as task_ref,";
1724 $sql .= " pt.label as task_label,";
1725 $sql .= " p.rowid as project_id,";
1726 $sql .= " p.ref as project_ref,";
1727 $sql .= " p.title as project_label,";
1728 $sql .= " p.public as public";
1729 $sql .= " FROM ".MAIN_DB_PREFIX."element_time as ptt, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."projet as p";
1730 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
1731 $sql .= " WHERE ptt.fk_element = pt.rowid AND pt.fk_projet = p.rowid";
1732 $sql .= " AND ptt.elementtype = 'task'";
1733 $sql .= " AND pt.rowid = ".((int) $this->id);
1734 $sql .= " AND pt.entity IN (".getEntity('project').")";
1735 if ($morewherefilter) {
1736 $sql .= $morewherefilter;
1737 }
1738
1739 dol_syslog(get_class($this)."::fetchAllTimeSpent", LOG_DEBUG);
1740 $resql = $this->db->query($sql);
1741 if ($resql) {
1742 $num = $this->db->num_rows($resql);
1743
1744 $i = 0;
1745 while ($i < $num) {
1746 $obj = $this->db->fetch_object($resql);
1747
1748 $newobj = new stdClass();
1749
1750 $newobj->socid = $obj->socid;
1751 $newobj->thirdparty_name = $obj->thirdparty_name;
1752 $newobj->thirdparty_email = $obj->thirdparty_email;
1753
1754 $newobj->fk_project = $obj->project_id;
1755 $newobj->project_ref = $obj->project_ref;
1756 $newobj->project_label = $obj->project_label;
1757 $newobj->public = $obj->project_public;
1758
1759 $newobj->fk_task = $obj->task_id;
1760 $newobj->task_ref = $obj->task_ref;
1761 $newobj->task_label = $obj->task_label;
1762
1763 $newobj->timespent_line_id = $obj->rowid;
1764 $newobj->timespent_line_ref_ext = $obj->ref_ext;
1765 $newobj->timespent_line_date = $this->db->jdate($obj->task_date);
1766 $newobj->timespent_line_datehour = $this->db->jdate($obj->task_datehour);
1767 $newobj->timespent_line_withhour = $obj->task_date_withhour;
1768 $newobj->timespent_line_duration = $obj->task_duration;
1769 $newobj->timespent_line_fk_user = $obj->fk_user;
1770 $newobj->timespent_line_thm = $obj->thm; // hourly rate
1771 $newobj->timespent_line_note = $obj->note;
1772
1773 $arrayres[] = $newobj;
1774
1775 $i++;
1776 }
1777
1778 $this->db->free($resql);
1779
1780 $this->lines = $arrayres;
1781 return 1;
1782 } else {
1783 dol_print_error($this->db);
1784 $this->error = "Error ".$this->db->lasterror();
1785 return -1;
1786 }
1787 }
1788
1789
1797 public function getSummaryOfTimeSpent($userobj = null, $morewherefilter = '')
1798 {
1799 if (is_object($userobj)) {
1800 $userid = $userobj->id;
1801 } else {
1802 $userid = $userobj; // old method
1803 }
1804
1805 $id = $this->id;
1806 if (empty($id) && empty($userid)) {
1807 dol_syslog("getSummaryOfTimeSpent called on a not loaded task without user param defined", LOG_ERR);
1808 return -1;
1809 }
1810
1811 $result = array();
1812
1813 $sql = "SELECT";
1814 $sql .= " MIN(t.element_datehour) as min_date,";
1815 $sql .= " MAX(t.element_datehour) as max_date,";
1816 $sql .= " SUM(t.element_duration) as total_duration,";
1817 $sql .= " SUM(t.element_duration / 3600 * ".$this->db->ifsql("t.thm IS NULL", 0, "t.thm").") as total_amount,";
1818 $sql .= " COUNT(t.rowid) as nblines,";
1819 $sql .= " SUM(".$this->db->ifsql("t.thm IS NULL", 1, 0).") as nblinesnull";
1820 $sql .= " FROM ".MAIN_DB_PREFIX."element_time as t";
1821 $sql .= " WHERE t.elementtype='task'";
1822 if ($morewherefilter) {
1823 $sql .= $morewherefilter;
1824 }
1825 if ($id > 0) {
1826 $sql .= " AND t.fk_element = ".((int) $id);
1827 }
1828 if ($userid > 0) {
1829 $sql .= " AND t.fk_user = ".((int) $userid);
1830 }
1831
1832 dol_syslog(get_class($this)."::getSummaryOfTimeSpent", LOG_DEBUG);
1833 $resql = $this->db->query($sql);
1834 if ($resql) {
1835 $obj = $this->db->fetch_object($resql);
1836
1837 $result['min_date'] = $obj->min_date; // deprecated. use the ->timespent_xxx instead
1838 $result['max_date'] = $obj->max_date; // deprecated. use the ->timespent_xxx instead
1839 $result['total_duration'] = $obj->total_duration; // deprecated. use the ->timespent_xxx instead
1840
1841 $this->timespent_min_date = $this->db->jdate($obj->min_date);
1842 $this->timespent_max_date = $this->db->jdate($obj->max_date);
1843 $this->timespent_total_duration = $obj->total_duration;
1844 $this->timespent_total_amount = $obj->total_amount;
1845 $this->timespent_nblinesnull = ($obj->nblinesnull ? $obj->nblinesnull : 0);
1846 $this->timespent_nblines = ($obj->nblines ? $obj->nblines : 0);
1847
1848 $this->db->free($resql);
1849 } else {
1850 dol_print_error($this->db);
1851 }
1852 return $result;
1853 }
1854
1863 public function getSumOfAmount($fuser = '', $dates = '', $datee = '')
1864 {
1865 $id = $this->id;
1866
1867 $result = array();
1868
1869 $sql = "SELECT";
1870 $sql .= " SUM(t.element_duration) as nbseconds,";
1871 $sql .= " SUM(t.element_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";
1872 $sql .= " FROM ".MAIN_DB_PREFIX."element_time as t";
1873 $sql .= " WHERE t.elementtype='task' AND t.fk_element = ".((int) $id);
1874 if (is_object($fuser) && $fuser->id > 0) {
1875 $sql .= " AND fk_user = ".((int) $fuser->id);
1876 }
1877 if ($dates > 0) {
1878 $datefieldname = "element_datehour";
1879 $sql .= " AND (".$datefieldname." >= '".$this->db->idate($dates)."' OR ".$datefieldname." IS NULL)";
1880 }
1881 if ($datee > 0) {
1882 $datefieldname = "element_datehour";
1883 $sql .= " AND (".$datefieldname." <= '".$this->db->idate($datee)."' OR ".$datefieldname." IS NULL)";
1884 }
1885 //print $sql;
1886
1887 dol_syslog(get_class($this)."::getSumOfAmount", LOG_DEBUG);
1888 $resql = $this->db->query($sql);
1889 if ($resql) {
1890 $obj = $this->db->fetch_object($resql);
1891
1892 $result['amount'] = $obj->amount;
1893 $result['nbseconds'] = $obj->nbseconds;
1894 $result['nblinesnull'] = $obj->nblinesnull;
1895
1896 $this->db->free($resql);
1897 return $result;
1898 } else {
1899 dol_print_error($this->db);
1900 return $result;
1901 }
1902 }
1903
1910 public function fetchTimeSpent($id)
1911 {
1912 $timespent = new TimeSpent($this->db);
1913 $timespent->fetch($id);
1914
1915 dol_syslog(get_class($this)."::fetchTimeSpent", LOG_DEBUG);
1916
1917 if ($timespent->id > 0) {
1918 $this->timespent_id = $timespent->id;
1919 $this->id = $timespent->fk_element;
1920 $this->timespent_date = $timespent->element_date;
1921 $this->timespent_datehour = $timespent->element_datehour;
1922 $this->timespent_withhour = $timespent->element_date_withhour;
1923 $this->timespent_duration = $timespent->element_duration;
1924 $this->timespent_fk_user = $timespent->fk_user;
1925 $this->timespent_fk_product = $timespent->fk_product;
1926 $this->timespent_thm = $timespent->thm; // hourly rate
1927 $this->timespent_note = $timespent->note;
1928
1929 return 1;
1930 }
1931
1932 return 0;
1933 }
1934
1942 public function fetchAllTimeSpent(User $userobj, $morewherefilter = '')
1943 {
1944 $arrayres = array();
1945
1946 $sql = "SELECT";
1947 $sql .= " s.rowid as socid,";
1948 $sql .= " s.nom as thirdparty_name,";
1949 $sql .= " s.email as thirdparty_email,";
1950 $sql .= " ptt.rowid,";
1951 $sql .= " ptt.fk_element as fk_task,";
1952 $sql .= " ptt.element_date as task_date,";
1953 $sql .= " ptt.element_datehour as task_datehour,";
1954 $sql .= " ptt.element_date_withhour as task_date_withhour,";
1955 $sql .= " ptt.element_duration as task_duration,";
1956 $sql .= " ptt.fk_user,";
1957 $sql .= " ptt.note,";
1958 $sql .= " ptt.thm,";
1959 $sql .= " pt.rowid as task_id,";
1960 $sql .= " pt.ref as task_ref,";
1961 $sql .= " pt.label as task_label,";
1962 $sql .= " p.rowid as project_id,";
1963 $sql .= " p.ref as project_ref,";
1964 $sql .= " p.title as project_label,";
1965 $sql .= " p.public as public";
1966 $sql .= " FROM ".MAIN_DB_PREFIX."element_time as ptt, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."projet as p";
1967 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
1968 $sql .= " WHERE ptt.fk_element = pt.rowid AND pt.fk_projet = p.rowid";
1969 $sql .= " AND ptt.elementtype = 'task'";
1970 $sql .= " AND ptt.fk_user = ".((int) $userobj->id);
1971 $sql .= " AND pt.entity IN (".getEntity('project').")";
1972 if ($morewherefilter) {
1973 $sql .= $morewherefilter;
1974 }
1975
1976 dol_syslog(get_class($this)."::fetchAllTimeSpent", LOG_DEBUG);
1977 $resql = $this->db->query($sql);
1978 if ($resql) {
1979 $num = $this->db->num_rows($resql);
1980
1981 $i = 0;
1982 while ($i < $num) {
1983 $obj = $this->db->fetch_object($resql);
1984
1985 $newobj = new stdClass();
1986
1987 $newobj->socid = $obj->socid;
1988 $newobj->thirdparty_name = $obj->thirdparty_name;
1989 $newobj->thirdparty_email = $obj->thirdparty_email;
1990
1991 $newobj->fk_project = $obj->project_id;
1992 $newobj->project_ref = $obj->project_ref;
1993 $newobj->project_label = $obj->project_label;
1994 $newobj->public = $obj->project_public;
1995
1996 $newobj->fk_task = $obj->task_id;
1997 $newobj->task_ref = $obj->task_ref;
1998 $newobj->task_label = $obj->task_label;
1999
2000 $newobj->timespent_id = $obj->rowid;
2001 $newobj->timespent_date = $this->db->jdate($obj->task_date);
2002 $newobj->timespent_datehour = $this->db->jdate($obj->task_datehour);
2003 $newobj->timespent_withhour = $obj->task_date_withhour;
2004 $newobj->timespent_duration = $obj->task_duration;
2005 $newobj->timespent_fk_user = $obj->fk_user;
2006 $newobj->timespent_thm = $obj->thm; // hourly rate
2007 $newobj->timespent_note = $obj->note;
2008
2009 $arrayres[] = $newobj;
2010
2011 $i++;
2012 }
2013
2014 $this->db->free($resql);
2015 } else {
2016 dol_print_error($this->db);
2017 $this->error = "Error ".$this->db->lasterror();
2018 return -1;
2019 }
2020
2021 return $arrayres;
2022 }
2023
2031 public function updateTimeSpent($user, $notrigger = 0)
2032 {
2033 global $conf, $langs;
2034
2035 $ret = 0;
2036
2037 // Check parameters
2038 if ($this->timespent_date == '') {
2039 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Date"));
2040 return -1;
2041 }
2042
2043 // Clean parameters
2044 if (empty($this->timespent_datehour)) {
2045 $this->timespent_datehour = $this->timespent_date;
2046 }
2047 if (isset($this->timespent_note)) {
2048 $this->timespent_note = trim($this->timespent_note);
2049 }
2050
2051 if (getDolGlobalString('PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS')) {
2052 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
2053 $restrictBefore = dol_time_plus_duree(dol_now(), - $conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS, 'm');
2054
2055 if ($this->timespent_date < $restrictBefore) {
2056 $this->error = $langs->trans('TimeRecordingRestrictedToNMonthsBack', getDolGlobalString('PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS'));
2057 $this->errors[] = $this->error;
2058 return -1;
2059 }
2060 }
2061
2062 $this->db->begin();
2063
2064 $timespent = new TimeSpent($this->db);
2065 $timespent->fetch($this->timespent_id);
2066
2067 $timespent->element_date = $this->timespent_date;
2068 $timespent->element_datehour = $this->timespent_datehour;
2069 $timespent->element_date_withhour = $this->timespent_withhour;
2070 $timespent->element_duration = $this->timespent_duration;
2071 if ($this->timespent_fk_user > 0) {
2072 $timespent->fk_user = $this->timespent_fk_user;
2073 }
2074 $timespent->fk_product = $this->timespent_fk_product;
2075 $timespent->note = $this->timespent_note;
2076 $timespent->invoice_id = $this->timespent_invoiceid;
2077 $timespent->invoice_line_id = $this->timespent_invoicelineid;
2078
2079 dol_syslog(get_class($this)."::updateTimeSpent", LOG_DEBUG);
2080 if ($timespent->update($user) > 0) {
2081 if (!$notrigger) {
2082 // Call trigger
2083 $result = $this->call_trigger('TASK_TIMESPENT_MODIFY', $user);
2084 if ($result < 0) {
2085 $this->db->rollback();
2086 $ret = -1;
2087 } else {
2088 $ret = 1;
2089 }
2090 // End call triggers
2091 } else {
2092 $ret = 1;
2093 }
2094 } else {
2095 $this->error = $this->db->lasterror();
2096 $this->db->rollback();
2097 $ret = -1;
2098 }
2099
2100 if ($ret == 1 && (($this->timespent_old_duration != $this->timespent_duration) || getDolGlobalString('TIMESPENT_ALWAYS_UPDATE_THM'))) {
2101 if ($this->timespent_old_duration != $this->timespent_duration) {
2102 // Recalculate amount of time spent for task and update denormalized field
2103 $sql = "UPDATE " . MAIN_DB_PREFIX . "projet_task";
2104 $sql .= " SET duration_effective = (SELECT SUM(element_duration) FROM " . MAIN_DB_PREFIX . "element_time as ptt where ptt.elementtype = 'task' AND ptt.fk_element = " . ((int) $this->id) . ")";
2105 if (isset($this->progress)) {
2106 $sql .= ", progress = " . ((float) $this->progress); // Do not overwrite value if not provided
2107 }
2108 $sql .= " WHERE rowid = " . ((int) $this->id);
2109
2110 dol_syslog(get_class($this) . "::updateTimeSpent", LOG_DEBUG);
2111 if (!$this->db->query($sql)) {
2112 $this->error = $this->db->lasterror();
2113 $this->db->rollback();
2114 $ret = -2;
2115 }
2116 }
2117
2118 // Update hourly rate of this time spent entry, but only if it was not set initially
2119 $res_update = 1;
2120 if (empty($timespent->thm) || getDolGlobalString('TIMESPENT_ALWAYS_UPDATE_THM')) {
2121 $resql_thm_user = $this->db->query("SELECT thm FROM " . MAIN_DB_PREFIX . "user WHERE rowid = " . ((int) $timespent->fk_user));
2122 if (!empty($resql_thm_user)) {
2123 $obj_thm_user = $this->db->fetch_object($resql_thm_user);
2124 $timespent->thm = $obj_thm_user->thm;
2125 }
2126 $res_update = $timespent->update($user);
2127 }
2128
2129 dol_syslog(get_class($this)."::updateTimeSpent", LOG_DEBUG);
2130 if ($res_update <= 0) {
2131 $this->error = $this->db->lasterror();
2132 $ret = -2;
2133 }
2134 }
2135
2136 if ($ret >= 0) {
2137 $this->db->commit();
2138 }
2139 return $ret;
2140 }
2141
2149 public function delTimeSpent($user, $notrigger = 0)
2150 {
2151 global $conf, $langs;
2152
2153 $error = 0;
2154
2155 if (getDolGlobalString('PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS')) {
2156 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
2157 $restrictBefore = dol_time_plus_duree(dol_now(), - $conf->global->PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS, 'm');
2158
2159 if ($this->timespent_date < $restrictBefore) {
2160 $this->error = $langs->trans('TimeRecordingRestrictedToNMonthsBack', getDolGlobalString('PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS'));
2161 $this->errors[] = $this->error;
2162 return -1;
2163 }
2164 }
2165
2166 $this->db->begin();
2167
2168 if (!$notrigger) {
2169 // Call trigger
2170 $result = $this->call_trigger('TASK_TIMESPENT_DELETE', $user);
2171 if ($result < 0) {
2172 $error++;
2173 }
2174 // End call triggers
2175 }
2176
2177 if (!$error) {
2178 $timespent = new TimeSpent($this->db);
2179 $timespent->fetch($this->timespent_id);
2180
2181 $res_del = $timespent->delete($user);
2182
2183 if ($res_del < 0) {
2184 $error++;
2185 $this->errors[] = "Error ".$this->db->lasterror();
2186 }
2187 }
2188
2189 if (!$error) {
2190 $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task";
2191 $sql .= " SET duration_effective = duration_effective - ".$this->db->escape($this->timespent_duration ? $this->timespent_duration : 0);
2192 $sql .= " WHERE rowid = ".((int) $this->id);
2193
2194 dol_syslog(get_class($this)."::delTimeSpent", LOG_DEBUG);
2195 if ($this->db->query($sql)) {
2196 $result = 0;
2197 } else {
2198 $this->error = $this->db->lasterror();
2199 $result = -2;
2200 }
2201 }
2202
2203 // Commit or rollback
2204 if ($error) {
2205 foreach ($this->errors as $errmsg) {
2206 dol_syslog(get_class($this)."::delTimeSpent ".$errmsg, LOG_ERR);
2207 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2208 }
2209 $this->db->rollback();
2210 return -1 * $error;
2211 } else {
2212 $this->db->commit();
2213 return 1;
2214 }
2215 }
2216
2231 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)
2232 {
2233 global $langs, $conf;
2234
2235 $error = 0;
2236
2237 //Use 00:00 of today if time is use on task.
2238 $now = dol_mktime(0, 0, 0, (int) dol_print_date(dol_now(), '%m'), (int) dol_print_date(dol_now(), '%d'), (int) dol_print_date(dol_now(), '%Y'));
2239
2240 $datec = $now;
2241
2242 $clone_task = new Task($this->db);
2243 $origin_task = new Task($this->db);
2244
2245 $clone_task->context['createfromclone'] = 'createfromclone';
2246
2247 $this->db->begin();
2248
2249 // Load source object
2250 $clone_task->fetch($fromid);
2251 $clone_task->fetch_optionals();
2252 //var_dump($clone_task->array_options);exit;
2253
2254 $origin_task->fetch($fromid);
2255
2256 $defaultref = '';
2257 $obj = !getDolGlobalString('PROJECT_TASK_ADDON') ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
2258 if (getDolGlobalString('PROJECT_TASK_ADDON') && is_readable(DOL_DOCUMENT_ROOT."/core/modules/project/task/" . getDolGlobalString('PROJECT_TASK_ADDON').".php")) {
2259 require_once DOL_DOCUMENT_ROOT."/core/modules/project/task/" . getDolGlobalString('PROJECT_TASK_ADDON').'.php';
2260 $modTask = new $obj();
2261 '@phan-var-force ModeleNumRefTask $modTask';
2262 $defaultref = $modTask->getNextValue(0, $clone_task);
2263 }
2264
2265 $ori_project_id = $clone_task->fk_project;
2266
2267 $clone_task->id = 0;
2268 $clone_task->ref = $defaultref;
2269 $clone_task->fk_project = $project_id;
2270 $clone_task->fk_task_parent = $parent_task_id;
2271 $clone_task->date_c = $datec;
2272 $clone_task->planned_workload = $origin_task->planned_workload;
2273 $clone_task->rang = $origin_task->rang;
2274 $clone_task->priority = $origin_task->priority;
2275
2276 //Manage Task Date
2277 if ($clone_change_dt) {
2278 $projectstatic = new Project($this->db);
2279 $projectstatic->fetch($ori_project_id);
2280
2281 // Origin project start date
2282 $orign_project_dt_start = (!isset($projectstatic->date_start) || $projectstatic->date_start == '') ? $projectstatic->date_c : $projectstatic->date_start;
2283
2284 // Calculate new task start date with difference between origin proj start date and origin task start date
2285 if (!empty($clone_task->date_start)) {
2286 $clone_task->date_start = $now + $clone_task->date_start - $orign_project_dt_start;
2287 }
2288
2289 // Calculate new task end date with difference between origin proj start date and origin task end date
2290 if (!empty($clone_task->date_end)) {
2291 $clone_task->date_end = $now + $clone_task->date_end - $orign_project_dt_start;
2292 }
2293 }
2294
2295 if (!$clone_prog) {
2296 $clone_task->progress = 0;
2297 }
2298
2299 // Create clone
2300 $result = $clone_task->create($user);
2301
2302 // Other options
2303 if ($result < 0) {
2304 $this->error = $clone_task->error;
2305 $error++;
2306 }
2307 // End
2308 if ($error) {
2309 $clone_task_id = 0; // For static tool check
2310 } else {
2311 $clone_task_id = $clone_task->id;
2312 $clone_task_ref = $clone_task->ref;
2313
2314 //Note Update
2315 if (!$clone_note) {
2316 $clone_task->note_private = '';
2317 $clone_task->note_public = '';
2318 } else {
2319 $this->db->begin();
2320 $res = $clone_task->update_note(dol_html_entity_decode($clone_task->note_public, ENT_QUOTES | ENT_HTML5), '_public');
2321 if ($res < 0) {
2322 $this->error .= $clone_task->error;
2323 $error++;
2324 $this->db->rollback();
2325 } else {
2326 $this->db->commit();
2327 }
2328
2329 $this->db->begin();
2330 $res = $clone_task->update_note(dol_html_entity_decode($clone_task->note_private, ENT_QUOTES | ENT_HTML5), '_private');
2331 if ($res < 0) {
2332 $this->error .= $clone_task->error;
2333 $error++;
2334 $this->db->rollback();
2335 } else {
2336 $this->db->commit();
2337 }
2338 }
2339
2340 //Duplicate file
2341 if ($clone_file) {
2342 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2343
2344 //retrieve project origin ref to know folder to copy
2345 $projectstatic = new Project($this->db);
2346 $projectstatic->fetch($ori_project_id);
2347 $ori_project_ref = $projectstatic->ref;
2348
2349 if ($ori_project_id != $project_id) {
2350 $projectstatic->fetch($project_id);
2351 $clone_project_ref = $projectstatic->ref;
2352 } else {
2353 $clone_project_ref = $ori_project_ref;
2354 }
2355
2356 $clone_task_dir = $conf->project->dir_output."/".dol_sanitizeFileName($clone_project_ref)."/".dol_sanitizeFileName($clone_task_ref);
2357 $ori_task_dir = $conf->project->dir_output."/".dol_sanitizeFileName($ori_project_ref)."/".dol_sanitizeFileName((string) $fromid);
2358
2359 $filearray = dol_dir_list($ori_task_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', '', SORT_ASC, 1);
2360 foreach ($filearray as $file) {
2361 if (!file_exists($clone_task_dir)) {
2362 if (dol_mkdir($clone_task_dir) < 0) {
2363 $this->error .= $langs->trans('ErrorInternalErrorDetected').':dol_mkdir';
2364 $error++;
2365 }
2366 }
2367
2368 $rescopy = dol_copy($ori_task_dir.'/'.$file['name'], $clone_task_dir.'/'.$file['name'], '0', 1);
2369 if (is_numeric($rescopy) && $rescopy < 0) {
2370 $this->error .= $langs->trans("ErrorFailToCopyFile", $ori_task_dir.'/'.$file['name'], $clone_task_dir.'/'.$file['name']);
2371 $error++;
2372 }
2373 }
2374 }
2375
2376 // clone affectation
2377 if ($clone_affectation) {
2378 $origin_task = new Task($this->db);
2379 $origin_task->fetch($fromid);
2380
2381 foreach (array('internal', 'external') as $source) {
2382 $tab = $origin_task->liste_contact(-1, $source);
2383 $num = count($tab);
2384 $i = 0;
2385 while ($i < $num) {
2386 $clone_task->add_contact($tab[$i]['id'], $tab[$i]['code'], $tab[$i]['source']);
2387 if ($clone_task->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
2388 $langs->load("errors");
2389 $this->error .= $langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType");
2390 $error++;
2391 } else {
2392 if ($clone_task->error != '') {
2393 $this->error .= $clone_task->error;
2394 $error++;
2395 }
2396 }
2397 $i++;
2398 }
2399 }
2400 }
2401
2402 if ($clone_time) {
2403 //TODO clone time of affectation
2404 }
2405 }
2406
2407 unset($clone_task->context['createfromclone']);
2408
2409 if (!$error) {
2410 $this->db->commit();
2411 return $clone_task_id;
2412 } else {
2413 $this->db->rollback();
2414 dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : ".$this->error, LOG_ERR);
2415 return -1;
2416 }
2417 }
2418
2419
2426 public function getLibStatut($mode = 0)
2427 {
2428 return $this->LibStatut($this->status, $mode);
2429 }
2430
2431 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2439 public function LibStatut($status, $mode = 0)
2440 {
2441 // phpcs:enable
2442 global $langs;
2443
2444 // list of Statut of the task
2445 $this->labelStatus[0] = 'Draft';
2446 $this->labelStatus[1] = 'ToDo';
2447 $this->labelStatus[2] = 'Running';
2448 $this->labelStatus[3] = 'Finish';
2449 $this->labelStatus[4] = 'Transfered';
2450 $this->labelStatusShort[0] = 'Draft';
2451 $this->labelStatusShort[1] = 'ToDo';
2452 $this->labelStatusShort[2] = 'Running';
2453 $this->labelStatusShort[3] = 'Completed';
2454 $this->labelStatusShort[4] = 'Transfered';
2455
2456 if ($mode == 0) {
2457 return $langs->trans($this->labelStatus[$status]);
2458 } elseif ($mode == 1) {
2459 return $langs->trans($this->labelStatusShort[$status]);
2460 } elseif ($mode == 2) {
2461 if ($status == 0) {
2462 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut0').' '.$langs->trans($this->labelStatusShort[$status]);
2463 } elseif ($status == 1) {
2464 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut1').' '.$langs->trans($this->labelStatusShort[$status]);
2465 } elseif ($status == 2) {
2466 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut3').' '.$langs->trans($this->labelStatusShort[$status]);
2467 } elseif ($status == 3) {
2468 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut6').' '.$langs->trans($this->labelStatusShort[$status]);
2469 } elseif ($status == 4) {
2470 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut6').' '.$langs->trans($this->labelStatusShort[$status]);
2471 } elseif ($status == 5) {
2472 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut5').' '.$langs->trans($this->labelStatusShort[$status]);
2473 }
2474 } elseif ($mode == 3) {
2475 if ($status == 0) {
2476 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut0');
2477 } elseif ($status == 1) {
2478 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut1');
2479 } elseif ($status == 2) {
2480 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut3');
2481 } elseif ($status == 3) {
2482 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut6');
2483 } elseif ($status == 4) {
2484 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut6');
2485 } elseif ($status == 5) {
2486 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut5');
2487 }
2488 } elseif ($mode == 4) {
2489 if ($status == 0) {
2490 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut0').' '.$langs->trans($this->labelStatus[$status]);
2491 } elseif ($status == 1) {
2492 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut1').' '.$langs->trans($this->labelStatus[$status]);
2493 } elseif ($status == 2) {
2494 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut3').' '.$langs->trans($this->labelStatus[$status]);
2495 } elseif ($status == 3) {
2496 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut6').' '.$langs->trans($this->labelStatus[$status]);
2497 } elseif ($status == 4) {
2498 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut6').' '.$langs->trans($this->labelStatus[$status]);
2499 } elseif ($status == 5) {
2500 return img_picto($langs->trans($this->labelStatusShort[$status]), 'statut5').' '.$langs->trans($this->labelStatus[$status]);
2501 }
2502 } elseif ($mode == 5) {
2503 /*if ($status==0) return $langs->trans($this->labelStatusShort[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut0');
2504 elseif ($status==1) return $langs->trans($this->labelStatusShort[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut1');
2505 elseif ($status==2) return $langs->trans($this->labelStatusShort[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut3');
2506 elseif ($status==3) return $langs->trans($this->labelStatusShort[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut6');
2507 elseif ($status==4) return $langs->trans($this->labelStatusShort[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut6');
2508 elseif ($status==5) return $langs->trans($this->labelStatusShort[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut5');
2509 */
2510 //else return $this->progress.' %';
2511 return '&nbsp;';
2512 } elseif ($mode == 6) {
2513 /*if ($status==0) return $langs->trans($this->labelStatus[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut0');
2514 elseif ($status==1) return $langs->trans($this->labelStatus[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut1');
2515 elseif ($status==2) return $langs->trans($this->labelStatus[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut3');
2516 elseif ($status==3) return $langs->trans($this->labelStatus[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut6');
2517 elseif ($status==4) return $langs->trans($this->labelStatus[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut6');
2518 elseif ($status==5) return $langs->trans($this->labelStatus[$status]).' '.img_picto($langs->trans($this->labelStatusShort[$status]),'statut5');
2519 */
2520 //else return $this->progress.' %';
2521 return '&nbsp;';
2522 }
2523 return "";
2524 }
2525
2536 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
2537 {
2538 $outputlangs->load("projects");
2539
2540 if (!dol_strlen($modele)) {
2541 $modele = 'nodefault';
2542
2543 if (!empty($this->model_pdf)) {
2544 $modele = $this->model_pdf;
2545 } elseif (getDolGlobalString('PROJECT_TASK_ADDON_PDF')) {
2546 $modele = getDolGlobalString('PROJECT_TASK_ADDON_PDF');
2547 }
2548 }
2549
2550 $modelpath = "core/modules/project/task/doc/";
2551
2552 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
2553 }
2554
2555
2556 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2563 public function load_board($user)
2564 {
2565 // phpcs:enable
2566 global $conf, $langs;
2567
2568 // For external user, no check is done on company because readability is managed by public status of project and assignment.
2569 //$socid = $user->socid;
2570 $socid = 0;
2571
2572 $projectstatic = new Project($this->db);
2573 $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1, $socid);
2574
2575 // List of tasks (does not care about permissions. Filtering will be done later)
2576 $sql = "SELECT p.rowid as projectid, p.fk_statut as projectstatus,";
2577 $sql .= " t.rowid as taskid, t.progress as progress, t.fk_statut as status,";
2578 $sql .= " t.dateo as date_start, t.datee as date_end";
2579 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2580 //$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2581 //if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2582 $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
2583 $sql .= " WHERE p.entity IN (".getEntity('project', 0).')';
2584 $sql .= " AND p.fk_statut = 1";
2585 $sql .= " AND t.fk_projet = p.rowid";
2586 $sql .= " AND (t.progress IS NULL OR t.progress < 100)"; // tasks to do
2587 if (!$user->hasRight('projet', 'all', 'lire')) {
2588 $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2589 }
2590 // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2591 //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).")";
2592 // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2593 // 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))";
2594
2595 //print $sql;
2596 $resql = $this->db->query($sql);
2597 if ($resql) {
2598 $task_static = new Task($this->db);
2599
2600 $response = new WorkboardResponse();
2601 $response->warning_delay = $conf->project->task->warning_delay / 60 / 60 / 24;
2602 $response->label = $langs->trans("OpenedTasks");
2603 if ($user->hasRight("projet", "all", "lire")) {
2604 $response->url = DOL_URL_ROOT.'/projet/tasks/list.php?mainmenu=project';
2605 } else {
2606 $response->url = DOL_URL_ROOT.'/projet/tasks/list.php?mode=mine&amp;mainmenu=project';
2607 }
2608 $response->img = img_object('', "task");
2609
2610 // This assignment in condition is not a bug. It allows walking the results.
2611 while ($obj = $this->db->fetch_object($resql)) {
2612 $response->nbtodo++;
2613
2614 $task_static->projectstatus = $obj->projectstatus;
2615 $task_static->progress = $obj->progress;
2616 $task_static->fk_statut = $obj->status;
2617 $task_static->status = $obj->status;
2618 $task_static->date_start = $this->db->jdate($obj->date_start);
2619 $task_static->date_end = $this->db->jdate($obj->date_end);
2620
2621 if ($task_static->hasDelay()) {
2622 $response->nbtodolate++;
2623 }
2624 }
2625
2626 return $response;
2627 } else {
2628 $this->error = $this->db->error();
2629 return -1;
2630 }
2631 }
2632
2633
2639 public function loadStateBoard()
2640 {
2641 global $user;
2642
2643 $mine = 0;
2644 $socid = $user->socid;
2645
2646 $projectstatic = new Project($this->db);
2647 $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, $mine, 1, $socid);
2648
2649 // List of tasks (does not care about permissions. Filtering will be done later)
2650 $sql = "SELECT count(p.rowid) as nb";
2651 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2652 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2653 if (!$user->hasRight('societe', 'client', 'voir')) {
2654 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2655 }
2656 $sql .= ", ".MAIN_DB_PREFIX."projet_task as t";
2657 $sql .= " WHERE p.entity IN (".getEntity('project', 0).')';
2658 $sql .= " AND t.fk_projet = p.rowid"; // tasks to do
2659 if ($mine || !$user->hasRight('projet', 'all', 'lire')) {
2660 $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2661 }
2662 // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2663 //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).")";
2664 if ($socid) {
2665 $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
2666 }
2667 if (!$user->hasRight('societe', 'client', 'voir')) {
2668 $sql .= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id).") OR (s.rowid IS NULL))";
2669 }
2670
2671 $resql = $this->db->query($sql);
2672 if ($resql) {
2673 // This assignment in condition is not a bug. It allows walking the results.
2674 while ($obj = $this->db->fetch_object($resql)) {
2675 $this->nb["tasks"] = $obj->nb;
2676 }
2677 $this->db->free($resql);
2678 return 1;
2679 } else {
2680 dol_print_error($this->db);
2681 $this->error = $this->db->error();
2682 return -1;
2683 }
2684 }
2685
2691 public function hasDelay()
2692 {
2693 global $conf;
2694
2695 if (!($this->progress >= 0 && $this->progress < 100)) {
2696 return false;
2697 }
2698
2699 $now = dol_now();
2700
2701 $datetouse = ($this->date_end > 0) ? $this->date_end : ((isset($this->datee) && $this->datee > 0) ? $this->datee : 0);
2702
2703 return ($datetouse > 0 && ($datetouse < ($now - $conf->project->task->warning_delay)));
2704 }
2705
2713 public function getKanbanView($option = '', $arraydata = null)
2714 {
2715 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2716
2717 $return = '<div class="box-flex-item box-flex-grow-zero">';
2718 $return .= '<div class="info-box info-box-sm info-box-kanban">';
2719 $return .= '<span class="info-box-icon bg-infobox-action">';
2720 $return .= img_picto('', $this->picto);
2721 //$return .= '<i class="fa fa-dol-action"></i>'; // Can be image
2722 $return .= '</span>';
2723 $return .= '<div class="info-box-content">';
2724 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
2725 if ($selected >= 0) {
2726 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2727 }
2728 if (!empty($arraydata['projectlink'])) {
2729 //$tmpproject = $arraydata['project'];
2730 //$return .= '<br><span class="info-box-status ">'.$tmpproject->getNomProject().'</span>';
2731 $return .= '<br><span class="info-box-status ">'.$arraydata['projectlink'].'</span>';
2732 }
2733 //if (property_exists($this, 'budget_amount')) {
2734 //$return .= '<br><span class="info-box-label amount">'.$langs->trans("Budget").' : '.price($this->budget_amount, 0, $langs, 1, 0, 0, $conf->currency).'</span>';
2735 //}
2736 if (property_exists($this, 'duration_effective')) {
2737 $return .= '<br><div class="info-box-label progressinkanban paddingtop">'.getTaskProgressView($this, false, true).'</div>';
2738 }
2739 $return .= '</div>';
2740 $return .= '</div>';
2741 $return .= '</div>';
2742
2743 return $return;
2744 }
2745
2753 public function mergeTask($task_origin_id)
2754 {
2755 global $langs, $hookmanager, $user, $action;
2756
2757 $error = 0;
2758 $task_origin = new Task($this->db); // The thirdparty that we will delete
2759
2760 dol_syslog("mergeTask merge task id=".$task_origin_id." (will be deleted) into the task id=".$this->id);
2761
2762 $langs->load('error');
2763
2764 if (!$error && $task_origin->fetch($task_origin_id) < 1) {
2765 $this->error = $langs->trans('ErrorRecordNotFound');
2766 $error++;
2767 }
2768
2769 if (!$error) {
2770 $this->db->begin();
2771
2772 // Recopy some data
2773 $listofproperties = array(
2774 'label', 'description', 'duration_effective', 'planned_workload', 'datec', 'date_start',
2775 'date_end', 'fk_user_creat', 'fk_user_valid', 'fk_statut', 'progress', 'budget_amount',
2776 'priority', 'rang', 'fk_projet', 'fk_task_parent'
2777 );
2778 foreach ($listofproperties as $property) {
2779 if (empty($this->$property)) {
2780 $this->$property = $task_origin->$property;
2781 }
2782 }
2783
2784 // Concat some data
2785 $listofproperties = array(
2786 'note_public', 'note_private'
2787 );
2788 foreach ($listofproperties as $property) {
2789 $this->$property = dol_concatdesc($this->$property, $task_origin->$property);
2790 }
2791
2792 // Merge extrafields
2793 if (is_array($task_origin->array_options)) {
2794 foreach ($task_origin->array_options as $key => $val) {
2795 if (empty($this->array_options[$key])) {
2796 $this->array_options[$key] = $val;
2797 }
2798 }
2799 }
2800
2801 // Update
2802 $result = $this->update($user);
2803
2804 if ($result < 0) {
2805 $error++;
2806 }
2807
2808 // Merge time spent
2809 if (!$error) {
2810 $result = $this->mergeTimeSpentTask($task_origin_id, $this->id);
2811 if ($result != true) {
2812 $error++;
2813 }
2814 }
2815
2816 // Merge contacts
2817 if (!$error) {
2818 $result = $this->mergeContactTask($task_origin_id, $this->id);
2819 if ($result != true) {
2820 $error++;
2821 }
2822 }
2823
2824 // External modules should update their ones too
2825 if (!$error) {
2826 $parameters = array('task_origin' => $task_origin->id, 'task_dest' => $this->id);
2827 $reshook = $hookmanager->executeHooks('replaceThirdparty', $parameters, $this, $action);
2828
2829 if ($reshook < 0) {
2830 $this->error = $hookmanager->error;
2831 $this->errors = $hookmanager->errors;
2832 $error++;
2833 }
2834 }
2835
2836
2837 if (!$error) {
2838 $this->context = array('merge' => 1, 'mergefromid' => $task_origin->id, 'mergefromref' => $task_origin->ref);
2839
2840 // Call trigger
2841 $result = $this->call_trigger('TASK_MODIFY', $user);
2842 if ($result < 0) {
2843 $error++;
2844 }
2845 // End call triggers
2846 }
2847
2848 if (!$error) {
2849 // We finally remove the old task
2850 if ($task_origin->delete($user) < 1) {
2851 $this->error = $task_origin->error;
2852 $this->errors = $task_origin->errors;
2853 $error++;
2854 }
2855 }
2856
2857 if (!$error) {
2858 $this->db->commit();
2859 return 0;
2860 } else {
2861 $langs->load("errors");
2862 $this->error = $langs->trans('ErrorsTaskMerge');
2863 $this->db->rollback();
2864 return -1;
2865 }
2866 }
2867
2868 return -1;
2869 }
2870}
$object ref
Definition info.php:89
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...
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
liste_contact($statusoflink=-1, $source='external', $list=0, $code='', $status=-1, $arrayoftcids=array())
Get array of all contacts for an object.
isObjectUsed($id=0, $entity=0)
Function to check if an object is used by others (by children).
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 in llx_element_contact.
call_trigger($triggerName, $user)
Call trigger based on this instance.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
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.
create($user, $notrigger=0)
Create into database.
fetch($id, $ref='', $loadparentdata=0)
Load object in memory from database.
getLibStatut($mode=0)
Return status label of object.
mergeContactTask($origin_id, $dest_id)
Merge contact of tasks.
const STATUS_TRANSFERRED
Transferred status.
getSumOfAmount($fuser='', $dates='', $datee='')
Calculate quantity and value of time consumed using the thm (hourly amount value of work for user ent...
mergeTask($task_origin_id)
Merge a task with another one, deleting the given task.
__construct($db)
Constructor.
fetchTimeSpentOnTask($morewherefilter='')
Fetch records of time spent of this task.
const STATUS_VALIDATED
Validated status (To do).
hasTimeSpent()
Return nb of time spent.
getNomUrl($withpicto=0, $option='', $mode='task', $addlabel=0, $sep=' - ', $notooltip=0, $save_lastsearch_value=-1)
Return clickable name (with picto eventually)
getListContactId($source='internal')
Return list of id of contacts of task.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
delTimeSpent($user, $notrigger=0)
Delete time spent.
mergeTimeSpentTask($origin_id, $dest_id)
Merge time spent of tasks.
const STATUS_DRAFT
Draft status.
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.
getSummaryOfTimeSpent($userobj=null, $morewherefilter='')
Calculate total of time spent for task.
getTooltipContentArray($params)
getTooltipContentArray
const STATUS_CANCELED
status canceled
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 line with id $this->timespent_id.
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.
loadStateBoard()
Load indicators this->nb for state board.
initAsSpecimen()
Initialise an instance with random values.
getTasksArray($usert=null, $userp=null, $projectid=0, $socid=0, $mode=0, $filteronproj='', $filteronprojstatus='-1', $morewherefilter='', $filteronprojuser=0, $filterontaskuser=0, $extrafields=null, $includebilltime=0, $search_array_options=array(), $loadextras=0, $loadRoleMode=1, $sortfield='', $sortorder='')
Return list of tasks for all projects or for one particular project Sort order is on project,...
addTimeSpent($user, $notrigger=0)
Add time spent.
hasDelay()
Is the task delayed?
LibStatut($status, $mode=0)
Return status label for an object.
const STATUS_CLOSED
Finished status.
hasChildren()
Return nb of children.
Class for TimeSpent.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_copy($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
dol_move_dir($srcdir, $destdir, $overwriteifexists=1, $indexdatabase=1, $renamedircontent=1)
Move a directory into another name.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_dir_list($utf8_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:63
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 information (by default a local PHP server timestamp) Rep...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
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_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79