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();
108 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
112 if (!DolibarrApiAccess::$user->rights->projet->lire) {
113 throw new RestException(401);
119 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
123 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
124 $search_sale = DolibarrApiAccess::$user->id;
127 $sql =
"SELECT t.rowid";
128 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
129 $sql .=
", sc.fk_soc, sc.fk_user";
131 $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)";
133 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
134 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
137 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
138 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
139 $sql .=
" AND t.fk_soc = sc.fk_soc";
142 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
144 if ($search_sale > 0) {
145 $sql .=
" AND t.rowid = sc.fk_soc";
148 if ($search_sale > 0) {
149 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
156 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
160 $sql .= $this->db->order($sortfield, $sortorder);
165 $offset = $limit * $page;
167 $sql .= $this->db->plimit($limit + 1, $offset);
171 $result = $this->db->query($sql);
174 $num = $this->db->num_rows($result);
175 $min = min($num, ($limit <= 0 ? $num : $limit));
178 $obj = $this->db->fetch_object($result);
179 $task_static =
new Task($this->db);
180 if ($task_static->fetch($obj->rowid)) {
186 throw new RestException(503,
'Error when retrieve task list : '.$this->db->lasterror());
198 public function post($request_data =
null)
200 if (!DolibarrApiAccess::$user->rights->projet->creer) {
201 throw new RestException(401,
"Insuffisant rights");
204 $result = $this->
_validate($request_data);
206 foreach ($request_data as $field => $value) {
207 if ($field ===
'caller') {
209 $this->task->context[
'caller'] = $request_data[
'caller'];
213 $this->task->$field = $value;
222 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
223 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
226 return $this->task->id;
285 if (!DolibarrApiAccess::$user->rights->projet->lire) {
286 throw new RestException(401);
289 $result = $this->task->fetch($id);
291 throw new RestException(404,
'Task not found');
295 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
298 $usert = DolibarrApiAccess::$user;
300 $usert =
new User($this->db);
301 $usert->fetch($userid);
303 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert, 0, $id);
305 foreach ($this->task->roles as $line) {
451 public function put($id, $request_data =
null)
453 if (!DolibarrApiAccess::$user->rights->projet->creer) {
454 throw new RestException(401);
457 $result = $this->task->fetch($id);
459 throw new RestException(404,
'Task not found');
463 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
465 foreach ($request_data as $field => $value) {
466 if ($field ==
'id') {
469 if ($field ===
'caller') {
471 $this->task->context[
'caller'] = $request_data[
'caller'];
475 $this->task->$field = $value;
478 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
479 return $this->
get($id);
481 throw new RestException(500, $this->task->error);
492 public function delete($id)
494 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
495 throw new RestException(401);
497 $result = $this->task->fetch($id);
499 throw new RestException(404,
'Task not found');
503 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
506 if (!$this->task->delete(DolibarrApiAccess::$user)) {
507 throw new RestException(500,
'Error when delete task : '.$this->task->error);
513 'message' =>
'Task deleted'
535 public function addTimeSpent($id, $date, $duration, $user_id = 0, $note =
'')
537 if (!DolibarrApiAccess::$user->rights->projet->creer) {
538 throw new RestException(401);
540 $result = $this->task->fetch($id);
542 throw new RestException(404,
'Task not found');
546 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
551 $uid = DolibarrApiAccess::$user->id;
555 $this->task->timespent_date = $newdate;
556 $this->task->timespent_datehour = $newdate;
557 $this->task->timespent_withhour = 1;
558 $this->task->timespent_duration = $duration;
559 $this->task->timespent_fk_user = $uid;
560 $this->task->timespent_note = $note;
562 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
564 throw new RestException(304,
'Error nothing done. May be object is already validated');
567 throw new RestException(500,
'Error when adding time: '.$this->task->error);
573 'message' =>
'Time spent added'
594 public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note =
'')
596 if (!DolibarrApiAccess::$user->rights->projet->creer) {
597 throw new RestException(401);
602 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
606 $this->task->timespent_date = $newdate;
607 $this->task->timespent_datehour = $newdate;
608 $this->task->timespent_withhour = 1;
609 $this->task->timespent_duration = $duration;
610 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
611 $this->task->timespent_note = $note;
613 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
615 throw new RestException(304,
'Error nothing done.');
618 throw new RestException(500,
'Error when updating time spent: '.$this->task->error);
624 'message' =>
'Time spent updated'
641 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
642 throw new RestException(401);
647 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
650 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
651 throw new RestException(500,
'Error when deleting time spent: '.$this->task->error);
657 'message' =>
'Time spent deleted'
673 if ($this->task->fetch($id) <= 0) {
674 throw new RestException(404,
'Task not found');
676 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
677 throw new RestException(404,
'Timespent not found');
678 } elseif ($this->task->id != $id) {
679 throw new RestException(404,
'Timespent not found in selected task');
693 $object = parent::_cleanObjectDatas($object);
695 unset($object->barcode_type);
696 unset($object->barcode_type_code);
697 unset($object->barcode_type_label);
698 unset($object->barcode_type_coder);
699 unset($object->cond_reglement_id);
700 unset($object->cond_reglement);
701 unset($object->fk_delivery_address);
702 unset($object->shipping_method_id);
703 unset($object->fk_account);
704 unset($object->note);
705 unset($object->fk_incoterms);
706 unset($object->label_incoterms);
707 unset($object->location_incoterms);
708 unset($object->name);
709 unset($object->lastname);
710 unset($object->firstname);
711 unset($object->civility_id);
712 unset($object->mode_reglement_id);
713 unset($object->country);
714 unset($object->country_id);
715 unset($object->country_code);
717 unset($object->weekWorkLoad);
718 unset($object->weekWorkLoad);
722 unset($object->total_ht);
723 unset($object->total_tva);
724 unset($object->total_localtax1);
725 unset($object->total_localtax2);
726 unset($object->total_ttc);
728 unset($object->comments);
743 foreach (self::$FIELDS as $field) {
744 if (!isset($data[$field])) {
745 throw new RestException(400,
"$field field missing");
747 $object[$field] = $data[$field];
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List tasks.
deleteTimeSpent($id, $timespent_id)
Delete time spent for a task of a project.
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.