19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
22 require_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";
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(0, $usert, 0, $id);
300 foreach ($this->task->roles as $line) {
445 public function put($id, $request_data =
null)
447 if (!DolibarrApiAccess::$user->rights->projet->creer) {
448 throw new RestException(401);
451 $result = $this->task->fetch($id);
453 throw new RestException(404,
'Task not found');
457 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 foreach ($request_data as $field => $value) {
460 if ($field ==
'id') {
463 $this->task->$field = $value;
466 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
467 return $this->
get($id);
469 throw new RestException(500, $this->task->error);
480 public function delete($id)
482 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
483 throw new RestException(401);
485 $result = $this->task->fetch($id);
487 throw new RestException(404,
'Task not found');
491 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
494 if (!$this->task->delete(DolibarrApiAccess::$user)) {
495 throw new RestException(500,
'Error when delete task : '.$this->task->error);
501 'message' =>
'Task deleted'
523 public function addTimeSpent($id, $date, $duration, $user_id = 0, $note =
'')
525 if (!DolibarrApiAccess::$user->rights->projet->creer) {
526 throw new RestException(401);
528 $result = $this->task->fetch($id);
530 throw new RestException(404,
'Task not found');
534 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
539 $uid = DolibarrApiAccess::$user->id;
543 $this->task->timespent_date = $newdate;
544 $this->task->timespent_datehour = $newdate;
545 $this->task->timespent_withhour = 1;
546 $this->task->timespent_duration = $duration;
547 $this->task->timespent_fk_user = $uid;
548 $this->task->timespent_note = $note;
550 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
552 throw new RestException(304,
'Error nothing done. May be object is already validated');
555 throw new RestException(500,
'Error when adding time: '.$this->task->error);
561 'message' =>
'Time spent added'
582 public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note =
'')
584 if (!DolibarrApiAccess::$user->rights->projet->creer) {
585 throw new RestException(401);
590 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
594 $this->task->timespent_date = $newdate;
595 $this->task->timespent_datehour = $newdate;
596 $this->task->timespent_withhour = 1;
597 $this->task->timespent_duration = $duration;
598 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
599 $this->task->timespent_note = $note;
601 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
603 throw new RestException(304,
'Error nothing done.');
606 throw new RestException(500,
'Error when updating time spent: '.$this->task->error);
612 'message' =>
'Time spent updated'
629 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
630 throw new RestException(401);
635 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
638 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
639 throw new RestException(500,
'Error when deleting time spent: '.$this->task->error);
645 'message' =>
'Time spent deleted'
661 if ($this->task->fetch($id) <= 0) {
662 throw new RestException(404,
'Task not found');
664 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
665 throw new RestException(404,
'Timespent not found');
666 } elseif ($this->task->id != $id) {
667 throw new RestException(404,
'Timespent not found in selected task');
681 $object = parent::_cleanObjectDatas($object);
683 unset($object->barcode_type);
684 unset($object->barcode_type_code);
685 unset($object->barcode_type_label);
686 unset($object->barcode_type_coder);
687 unset($object->cond_reglement_id);
688 unset($object->cond_reglement);
689 unset($object->fk_delivery_address);
690 unset($object->shipping_method_id);
691 unset($object->fk_account);
692 unset($object->note);
693 unset($object->fk_incoterms);
694 unset($object->label_incoterms);
695 unset($object->location_incoterms);
696 unset($object->name);
697 unset($object->lastname);
698 unset($object->firstname);
699 unset($object->civility_id);
700 unset($object->mode_reglement_id);
701 unset($object->country);
702 unset($object->country_id);
703 unset($object->country_code);
705 unset($object->weekWorkLoad);
706 unset($object->weekWorkLoad);
710 unset($object->total_ht);
711 unset($object->total_tva);
712 unset($object->total_localtax1);
713 unset($object->total_localtax2);
714 unset($object->total_ttc);
716 unset($object->comments);
731 foreach (self::$FIELDS as $field) {
732 if (!isset($data[$field])) {
733 throw new RestException(400,
"$field field missing");
735 $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)
Update task general fields (won't touch time spent of task)
getRoles($id, $userid=0)
Get roles a user is assigned to a task with.
__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, &$error='')
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.