21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
25require_once DOL_DOCUMENT_ROOT.
'/core/class/timespent.class.php';
38 public static $FIELDS = array(
56 $this->task =
new Task($this->db);
70 public function get(
$id, $includetimespent = 0)
72 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
73 throw new RestException(403);
76 $result = $this->task->fetch(
$id);
78 throw new RestException(404,
'Task not found');
82 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
85 if ($includetimespent == 1) {
86 $timespent = $this->task->getSummaryOfTimeSpent(0);
88 if ($includetimespent == 2) {
89 $timespent = $this->task->fetchTimeSpentOnTask();
112 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
116 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
117 throw new RestException(403);
123 $socids = DolibarrApiAccess::$user->socid ?: 0;
127 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
128 $search_sale = DolibarrApiAccess::$user->id;
131 $sql =
"SELECT t.rowid";
132 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet_task AS t";
133 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"projet_task_extrafields AS ef ON (ef.fk_object = t.rowid)";
134 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"projet AS p ON p.rowid = t.fk_projet";
135 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
137 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize((
string) $socids).
")";
140 if ($search_sale && $search_sale !=
'-1') {
141 if ($search_sale == -2) {
142 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc)";
143 } elseif ($search_sale > 0) {
144 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
152 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
156 $sql .= $this->db->order($sortfield, $sortorder);
161 $offset = $limit * $page;
163 $sql .= $this->db->plimit($limit + 1, $offset);
167 $result = $this->db->query($sql);
170 $num = $this->db->num_rows($result);
171 $min = min($num, ($limit <= 0 ? $num : $limit));
174 $obj = $this->db->fetch_object($result);
175 $task_static =
new Task($this->db);
176 if ($task_static->fetch($obj->rowid)) {
182 throw new RestException(503,
'Error when retrieve task list : '.$this->db->lasterror());
196 public function post($request_data =
null)
198 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
199 throw new RestException(403,
"Insuffisant rights");
202 $result = $this->
_validate($request_data);
204 foreach ($request_data as $field => $value) {
205 if ($field ===
'caller') {
207 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
211 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
220 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
221 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
224 return $this->task->id;
284 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
285 throw new RestException(403);
288 $result = $this->task->fetch(
$id);
290 throw new RestException(404,
'Task not found');
294 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
297 $usert = DolibarrApiAccess::$user;
299 $usert =
new User($this->db);
300 $usert->fetch($userid);
302 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert,
'0',
$id);
304 foreach ($this->task->roles as $line) {
455 public function put(
$id, $request_data =
null)
457 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
458 throw new RestException(403);
461 $result = $this->task->fetch(
$id);
463 throw new RestException(404,
'Task not found');
467 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
469 foreach ($request_data as $field => $value) {
470 if ($field ==
'id') {
473 if ($field ===
'caller') {
475 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
478 if ($field ==
'array_options' && is_array($value)) {
479 foreach ($value as $index => $val) {
480 $this->task->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->task);
485 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
488 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
489 return $this->
get(
$id);
491 throw new RestException(500, $this->task->error);
505 public function delete(
$id)
507 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
508 throw new RestException(403);
510 $result = $this->task->fetch(
$id);
512 throw new RestException(404,
'Task not found');
516 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
519 if ($this->task->delete(DolibarrApiAccess::$user) <= 0) {
520 throw new RestException(500,
'Error when delete task : '.$this->task->error);
526 'message' =>
'Task deleted'
545 dol_syslog(
"API Rest request::getTimeSpent", LOG_DEBUG);
546 if (! DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
547 throw new RestException(403);
550 $taskresult = $this->task->fetch(
$id);
552 throw new RestException(404,
'Task with id='.
$id.
' not found');
555 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
559 $timeresult = $timespent->fetch($timespent_id);
561 throw new RestException(404,
'Timespent with id='.$timespent_id.
' not found');
564 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
590 public function addTimeSpent(
$id, $date, $duration, $product_id =
null, $user_id = 0, $note =
'')
592 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
593 throw new RestException(403);
595 $result = $this->task->fetch(
$id);
597 throw new RestException(404,
'Task not found');
601 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
606 $uid = DolibarrApiAccess::$user->id;
610 $this->task->timespent_date = $newdate;
611 $this->task->timespent_datehour = $newdate;
612 $this->task->timespent_withhour = 1;
613 $this->task->timespent_duration = $duration;
614 $this->task->timespent_fk_product = $product_id;
615 $this->task->timespent_fk_user = $uid;
616 $this->task->timespent_note = $note;
618 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
620 throw new RestException(304,
'Error nothing done. May be object is already validated');
623 throw new RestException(500,
'Error when adding time: '.$this->task->error);
629 'message' =>
'Time spent added'
654 public function putTimeSpent(
$id, $timespent_id, $date, $duration, $product_id =
null, $user_id = 0, $note =
'')
656 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
657 throw new RestException(403);
662 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
666 $this->task->timespent_date = $newdate;
667 $this->task->timespent_datehour = $newdate;
668 $this->task->timespent_withhour = 1;
669 $this->task->timespent_duration = $duration;
670 $this->task->timespent_fk_product = $product_id;
671 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
672 $this->task->timespent_note = $note;
674 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
676 throw new RestException(304,
'Error nothing done.');
679 throw new RestException(500,
'Error when updating time spent: '.$this->task->error);
685 'message' =>
'Time spent updated'
704 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
705 throw new RestException(403);
710 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
713 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
714 throw new RestException(500,
'Error when deleting time spent: '.$this->task->error);
720 'message' =>
'Time spent deleted'
736 dol_syslog(
"API Rest request::timespentRecordChecks", LOG_DEBUG);
737 if ($this->task->fetch(
$id) <= 0) {
738 throw new RestException(404,
'Task not found');
740 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
741 throw new RestException(404,
'Timespent not found');
742 } elseif ($this->task->id !=
$id) {
743 throw new RestException(404,
'Timespent not found in selected task');
764 unset(
$object->barcode_type_code);
765 unset(
$object->barcode_type_label);
766 unset(
$object->barcode_type_coder);
767 unset(
$object->cond_reglement_id);
768 unset(
$object->cond_reglement);
769 unset(
$object->fk_delivery_address);
770 unset(
$object->shipping_method_id);
774 unset(
$object->label_incoterms);
775 unset(
$object->location_incoterms);
780 unset(
$object->mode_reglement_id);
792 unset(
$object->total_localtax1);
793 unset(
$object->total_localtax2);
818 $saving_fk_element =
$object->fk_element;
825 unset(
$object->barcode_type_code);
826 unset(
$object->barcode_type_label);
827 unset(
$object->barcode_type_coder);
828 unset(
$object->cond_reglement_id);
829 unset(
$object->cond_reglement);
830 unset(
$object->fk_delivery_address);
831 unset(
$object->shipping_method_id);
834 unset(
$object->label_incoterms);
835 unset(
$object->location_incoterms);
840 unset(
$object->mode_reglement_id);
848 unset(
$object->actiontypecode);
849 unset(
$object->array_languages);
853 unset(
$object->cond_reglement_supplier_id);
856 unset(
$object->contacts_ids_internal);
858 unset(
$object->date_validation);
859 unset(
$object->demand_reason_id);
860 unset(
$object->deposit_percent);
863 unset(
$object->fk_multicurrency);
869 unset(
$object->linkedObjectsIds);
871 unset(
$object->multicurrency_code);
872 unset(
$object->multicurrency_total_ht);
873 unset(
$object->multicurrency_total_localtax1);
874 unset(
$object->multicurrency_total_localtax2);
875 unset(
$object->multicurrency_total_ttc);
876 unset(
$object->multicurrency_total_tva);
877 unset(
$object->multicurrency_tx);
884 unset(
$object->retained_warranty_fk_cond_reglement);
886 unset(
$object->shipping_method);
892 unset(
$object->transport_mode_id);
895 unset(
$object->user_closing_id);
897 unset(
$object->user_creation_id);
898 unset(
$object->user_modification);
899 unset(
$object->user_modification_id);
901 unset(
$object->user_validation);
902 unset(
$object->user_validation_id);
907 unset(
$object->total_localtax1);
908 unset(
$object->total_localtax2);
916 if (!
$object->date_modification) {
920 $object->fk_element = $saving_fk_element;
936 if ($data ===
null) {
940 foreach (self::$FIELDS as $field) {
941 if (!isset($data[$field])) {
942 throw new RestException(400,
"$field field missing");
944 $object[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T of Object.
_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.
_cleanTimeSpentObjectDatas($object)
Clean sensitive object data @phpstan-template T of Object.
getRoles($id, $userid=0)
Get time spent of a task.
addTimeSpent($id, $date, $duration, $product_id=null, $user_id=0, $note='')
Add time spent to a task of a project.
__construct()
Constructor.
putTimeSpent($id, $timespent_id, $date, $duration, $product_id=null, $user_id=0, $note='')
Update time spent for a task of a project.
getTimeSpent($id, $timespent_id)
Get time spent of a task.
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
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...