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 if ($field ==
'array_options' && is_array($value)) {
465 foreach ($value as $index => $val) {
466 $this->task->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->task);
470 $this->task->$field = $value;
473 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
474 return $this->
get($id);
476 throw new RestException(500, $this->task->error);
487 public function delete($id)
489 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
490 throw new RestException(401);
492 $result = $this->task->fetch($id);
494 throw new RestException(404,
'Task not found');
498 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
501 if (!$this->task->delete(DolibarrApiAccess::$user)) {
502 throw new RestException(500,
'Error when delete task : '.$this->task->error);
508 'message' =>
'Task deleted'
530 public function addTimeSpent($id, $date, $duration, $user_id = 0, $note =
'')
532 if (!DolibarrApiAccess::$user->rights->projet->creer) {
533 throw new RestException(401);
535 $result = $this->task->fetch($id);
537 throw new RestException(404,
'Task not found');
541 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
546 $uid = DolibarrApiAccess::$user->id;
550 $this->task->timespent_date = $newdate;
551 $this->task->timespent_datehour = $newdate;
552 $this->task->timespent_withhour = 1;
553 $this->task->timespent_duration = $duration;
554 $this->task->timespent_fk_user = $uid;
555 $this->task->timespent_note = $note;
557 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
559 throw new RestException(304,
'Error nothing done. May be object is already validated');
562 throw new RestException(500,
'Error when adding time: '.$this->task->error);
568 'message' =>
'Time spent added'
589 public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note =
'')
591 if (!DolibarrApiAccess::$user->rights->projet->creer) {
592 throw new RestException(401);
597 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
601 $this->task->timespent_date = $newdate;
602 $this->task->timespent_datehour = $newdate;
603 $this->task->timespent_withhour = 1;
604 $this->task->timespent_duration = $duration;
605 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
606 $this->task->timespent_note = $note;
608 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
610 throw new RestException(304,
'Error nothing done.');
613 throw new RestException(500,
'Error when updating time spent: '.$this->task->error);
619 'message' =>
'Time spent updated'
636 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
637 throw new RestException(401);
642 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
645 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
646 throw new RestException(500,
'Error when deleting time spent: '.$this->task->error);
652 'message' =>
'Time spent deleted'
668 if ($this->task->fetch($id) <= 0) {
669 throw new RestException(404,
'Task not found');
671 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
672 throw new RestException(404,
'Timespent not found');
673 } elseif ($this->task->id != $id) {
674 throw new RestException(404,
'Timespent not found in selected task');
688 $object = parent::_cleanObjectDatas($object);
690 unset($object->barcode_type);
691 unset($object->barcode_type_code);
692 unset($object->barcode_type_label);
693 unset($object->barcode_type_coder);
694 unset($object->cond_reglement_id);
695 unset($object->cond_reglement);
696 unset($object->fk_delivery_address);
697 unset($object->shipping_method_id);
698 unset($object->fk_account);
699 unset($object->note);
700 unset($object->fk_incoterms);
701 unset($object->label_incoterms);
702 unset($object->location_incoterms);
703 unset($object->name);
704 unset($object->lastname);
705 unset($object->firstname);
706 unset($object->civility_id);
707 unset($object->mode_reglement_id);
708 unset($object->country);
709 unset($object->country_id);
710 unset($object->country_code);
712 unset($object->weekWorkLoad);
713 unset($object->weekWorkLoad);
717 unset($object->total_ht);
718 unset($object->total_tva);
719 unset($object->total_localtax1);
720 unset($object->total_localtax2);
721 unset($object->total_ttc);
723 unset($object->comments);
738 foreach (self::$FIELDS as $field) {
739 if (!isset($data[$field])) {
740 throw new RestException(400,
"$field field missing");
742 $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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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.