19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
36 public static $FIELDS = array(
54 $this->task =
new Task($this->db);
68 public function get($id, $includetimespent = 0)
70 if (!DolibarrApiAccess::$user->rights->projet->lire) {
71 throw new RestException(401);
74 $result = $this->task->fetch($id);
76 throw new RestException(404,
'Task not found');
80 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
83 if ($includetimespent == 1) {
84 $timespent = $this->task->getSummaryOfTimeSpent(0);
86 if ($includetimespent == 2) {
87 $timespent = $this->task->fetchTimeSpentOnTask();
107 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
111 if (!DolibarrApiAccess::$user->rights->projet->lire) {
112 throw new RestException(401);
118 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
122 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
123 $search_sale = DolibarrApiAccess::$user->id;
126 $sql =
"SELECT t.rowid";
127 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
128 $sql .=
", sc.fk_soc, sc.fk_user";
130 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet_task AS t LEFT JOIN ".MAIN_DB_PREFIX.
"projet_task_extrafields AS ef ON (ef.fk_object = t.rowid)";
132 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
133 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
136 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
137 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
138 $sql .=
" AND t.fk_soc = sc.fk_soc";
141 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
143 if ($search_sale > 0) {
144 $sql .=
" AND t.rowid = sc.fk_soc";
147 if ($search_sale > 0) {
148 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
155 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
159 $sql .= $this->db->order($sortfield, $sortorder);
164 $offset = $limit * $page;
166 $sql .= $this->db->plimit($limit + 1, $offset);
170 $result = $this->db->query($sql);
173 $num = $this->db->num_rows($result);
174 $min = min($num, ($limit <= 0 ? $num : $limit));
177 $obj = $this->db->fetch_object($result);
178 $task_static =
new Task($this->db);
179 if ($task_static->fetch($obj->rowid)) {
185 throw new RestException(503,
'Error when retrieve task list : '.$this->db->lasterror());
187 if (!count($obj_ret)) {
188 throw new RestException(404,
'No task found');
199 public function post($request_data =
null)
201 if (!DolibarrApiAccess::$user->rights->projet->creer) {
202 throw new RestException(401,
"Insuffisant rights");
205 $result = $this->
_validate($request_data);
207 foreach ($request_data as $field => $value) {
208 $this->task->$field = $value;
217 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
218 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
221 return $this->task->id;
280 if (!DolibarrApiAccess::$user->rights->projet->lire) {
281 throw new RestException(401);
284 $result = $this->task->fetch($id);
286 throw new RestException(404,
'Task not found');
290 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
293 $usert = DolibarrApiAccess::$user;
295 $usert =
new User($this->db);
296 $usert->fetch($userid);
298 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert, 0, $id);
300 foreach ($this->task->roles as $line) {
446 public function put($id, $request_data =
null)
448 if (!DolibarrApiAccess::$user->rights->projet->creer) {
449 throw new RestException(401);
452 $result = $this->task->fetch($id);
454 throw new RestException(404,
'Task not found');
458 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
460 foreach ($request_data as $field => $value) {
461 if ($field ==
'id') {
464 $this->task->$field = $value;
467 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
468 return $this->
get($id);
470 throw new RestException(500, $this->task->error);
481 public function delete($id)
483 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
484 throw new RestException(401);
486 $result = $this->task->fetch($id);
488 throw new RestException(404,
'Task not found');
492 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 if (!$this->task->delete(DolibarrApiAccess::$user)) {
496 throw new RestException(500,
'Error when delete task : '.$this->task->error);
502 'message' =>
'Task deleted'
524 public function addTimeSpent($id, $date, $duration, $user_id = 0, $note =
'')
526 if (!DolibarrApiAccess::$user->rights->projet->creer) {
527 throw new RestException(401);
529 $result = $this->task->fetch($id);
531 throw new RestException(404,
'Task not found');
535 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
540 $uid = DolibarrApiAccess::$user->id;
544 $this->task->timespent_date = $newdate;
545 $this->task->timespent_datehour = $newdate;
546 $this->task->timespent_withhour = 1;
547 $this->task->timespent_duration = $duration;
548 $this->task->timespent_fk_user = $uid;
549 $this->task->timespent_note = $note;
551 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
553 throw new RestException(304,
'Error nothing done. May be object is already validated');
556 throw new RestException(500,
'Error when adding time: '.$this->task->error);
562 'message' =>
'Time spent added'
583 public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note =
'')
585 if (!DolibarrApiAccess::$user->rights->projet->creer) {
586 throw new RestException(401);
591 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
595 $this->task->timespent_date = $newdate;
596 $this->task->timespent_datehour = $newdate;
597 $this->task->timespent_withhour = 1;
598 $this->task->timespent_duration = $duration;
599 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
600 $this->task->timespent_note = $note;
602 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
604 throw new RestException(304,
'Error nothing done.');
607 throw new RestException(500,
'Error when updating time spent: '.$this->task->error);
613 'message' =>
'Time spent updated'
630 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
631 throw new RestException(401);
636 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
639 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
640 throw new RestException(500,
'Error when deleting time spent: '.$this->task->error);
646 'message' =>
'Time spent deleted'
662 if ($this->task->fetch($id) <= 0) {
663 throw new RestException(404,
'Task not found');
665 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
666 throw new RestException(404,
'Timespent not found');
667 } elseif ($this->task->id != $id) {
668 throw new RestException(404,
'Timespent not found in selected task');
682 $object = parent::_cleanObjectDatas($object);
684 unset($object->barcode_type);
685 unset($object->barcode_type_code);
686 unset($object->barcode_type_label);
687 unset($object->barcode_type_coder);
688 unset($object->cond_reglement_id);
689 unset($object->cond_reglement);
690 unset($object->fk_delivery_address);
691 unset($object->shipping_method_id);
692 unset($object->fk_account);
693 unset($object->note);
694 unset($object->fk_incoterms);
695 unset($object->label_incoterms);
696 unset($object->location_incoterms);
697 unset($object->name);
698 unset($object->lastname);
699 unset($object->firstname);
700 unset($object->civility_id);
701 unset($object->mode_reglement_id);
702 unset($object->country);
703 unset($object->country_id);
704 unset($object->country_code);
706 unset($object->weekWorkLoad);
707 unset($object->weekWorkLoad);
711 unset($object->total_ht);
712 unset($object->total_tva);
713 unset($object->total_localtax1);
714 unset($object->total_localtax2);
715 unset($object->total_ttc);
717 unset($object->comments);
732 foreach (self::$FIELDS as $field) {
733 if (!isset($data[$field])) {
734 throw new RestException(400,
"$field field missing");
736 $object[$field] = $data[$field];
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_cleanObjectDatas($object)
Clean sensible object datas.
putTimeSpent($id, $timespent_id, $date, $duration, $user_id=0, $note='')
Update time spent for a task of a project.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create task object.
put($id, $request_data=null)
Add a task to given project.
getRoles($id, $userid=0)
Get time spent of a task.
__construct()
Constructor.
addTimeSpent($id, $date, $duration, $user_id=0, $note='')
Add time spent to a task of a project.
timespentRecordChecks($id, $timespent_id)
Validate task & timespent IDs for timespent API methods.
deleteTimeSpent($id, $timespent_id)
Delete time spent for a task of a project.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List tasks.
Class to manage Dolibarr users.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.