23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
26require_once DOL_DOCUMENT_ROOT .
'/projet/class/task.class.php';
39 public static $FIELDS = array(
62 $this->project =
new Project($this->db);
63 $this->task =
new Task($this->db);
76 public function get(
$id)
78 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
79 throw new RestException(403);
82 $result = $this->project->fetch(
$id);
84 throw new RestException(404,
'Project with supplied id not found');
88 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
91 $this->project->fetchObjectLinked();
109 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
110 throw new RestException(403);
113 $result = $this->project->fetch(0, $ref);
115 throw new RestException(404,
'Project with supplied ref not found');
119 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
122 $this->project->fetchObjectLinked();
140 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
141 throw new RestException(403);
144 $result = $this->project->fetch(0,
'', $ref_ext);
146 throw new RestException(404,
'Project with supplied ref_ext not found');
150 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
153 $this->project->fetchObjectLinked();
171 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
172 throw new RestException(403);
175 $result = $this->project->fetch(0,
'',
'', $email_msgid);
177 throw new RestException(404,
'Project with supplied email_msgid not found');
181 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
184 $this->project->fetchObjectLinked();
206 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
208 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
209 throw new RestException(403);
215 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
219 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
220 $search_sale = DolibarrApiAccess::$user->id;
223 $sql =
"SELECT t.rowid";
224 $sql .=
" FROM " . MAIN_DB_PREFIX .
"projet as t";
225 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"societe AS s ON (s.rowid = t.fk_soc)";
226 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"projet_extrafields AS ef ON ef.fk_object = t.rowid";
228 $sql .=
", " . MAIN_DB_PREFIX .
"categorie_project as c";
230 $sql .=
' WHERE t.entity IN (' .
getEntity(
'project') .
')';
232 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
235 if ($search_sale && $search_sale !=
'-1') {
236 if ($search_sale == -2) {
237 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX .
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
238 } elseif ($search_sale > 0) {
239 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX .
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = " . ((int) $search_sale) .
")";
244 $sql .=
" AND c.fk_categorie = " . ((int) $category) .
" AND c.fk_project = t.rowid ";
251 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
256 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
258 $sql .= $this->db->order($sortfield, $sortorder);
263 $offset = $limit * $page;
265 $sql .= $this->db->plimit($limit + 1, $offset);
269 $result = $this->db->query($sql);
272 $num = $this->db->num_rows($result);
273 $min = min($num, ($limit <= 0 ? $num : $limit));
276 $obj = $this->db->fetch_object($result);
277 $project_static =
new Project($this->db);
278 if ($project_static->fetch($obj->rowid)) {
284 throw new RestException(503,
'Error when retrieve project list : ' . $this->db->lasterror());
288 if ($pagination_data) {
289 $totalsResult = $this->db->query($sqlTotals);
290 $total = $this->db->fetch_object($totalsResult)->total;
295 $obj_ret[
'data'] = $tmp;
296 $obj_ret[
'pagination'] = [
297 'total' => (int) $total,
299 'page_count' => ceil((
int) $total / $limit),
315 public function post($request_data =
null)
318 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
319 throw new RestException(403,
"Insufficiant rights");
322 $result = $this->
_validate($request_data);
324 foreach ($request_data as $field => $value) {
325 if ($field ===
'caller') {
327 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
331 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
342 if ($this->project->ref == -1 || $this->project->ref ===
'auto') {
350 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
351 foreach ($dirmodels as $reldir) {
352 $file =
dol_buildpath($reldir .
"core/modules/project/" . $modele .
'.php', 0);
353 if (file_exists($file)) {
355 $classname = $modele;
359 if ($filefound && !empty($classname)) {
360 $result =
dol_include_once($reldir .
"core/modules/project/" . $modele .
'.php');
361 if ($result !==
false && class_exists($classname)) {
362 $modProject =
new $classname();
363 '@phan-var-force ModeleNumRefProjects $modProject';
364 $defaultref = $modProject->getNextValue(
null, $this->project);
366 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/" . $modele .
'.php', LOG_ERR);
369 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
372 if (is_numeric($defaultref) && $defaultref <= 0) {
376 if (empty($defaultref)) {
380 $this->project->ref = $defaultref;
383 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
384 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
387 return $this->project->id;
408 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
410 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
411 throw new RestException(403);
414 $result = $this->project->fetch(
$id);
416 throw new RestException(404,
'project not found');
420 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
423 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
425 throw new RestException(500,
'Error : ' . $this->project->error);
447 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
448 throw new RestException(403);
451 $result = $this->project->fetch(
$id);
454 throw new RestException(404,
'Project not found');
458 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
460 foreach (array(
'internal',
'external') as $source) {
461 $contacts = $this->project->liste_contact(-1, $source);
463 foreach ($contacts as $contact) {
464 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
465 $result = $this->project->delete_contact($contact[
'rowid']);
467 throw new RestException(500,
'Error when deleted the contact');
488 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
489 throw new RestException(403);
492 $result = $this->project->fetch(
$id);
494 throw new RestException(404,
'Project not found');
498 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
500 $this->project->getLinesArray(DolibarrApiAccess::$user);
502 foreach ($this->project->lines as $line) {
503 if ($includetimespent == 1) {
504 $timespent = $line->getSummaryOfTimeSpent(0);
506 if ($includetimespent == 2) {
507 $timespent = $line->fetchTimeSpentOnTask();
528 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
529 throw new RestException(403);
532 $result = $this->project->fetch(
$id);
534 throw new RestException(404,
'Project not found');
538 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
541 require_once DOL_DOCUMENT_ROOT .
'/projet/class/task.class.php';
542 $taskstatic =
new Task($this->db);
543 $userp = DolibarrApiAccess::$user;
545 $userp =
new User($this->db);
546 $userp->fetch($userid);
548 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp,
null, (
string)
$id, 0);
550 foreach ($this->project->roles as $line) {
704 public function put(
$id, $request_data =
null)
706 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
707 throw new RestException(403);
710 $result = $this->project->fetch(
$id);
712 throw new RestException(404,
'Project not found');
716 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
718 foreach ($request_data as $field => $value) {
719 if ($field ==
'id') {
722 if ($field ===
'caller') {
724 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
727 if ($field ==
'array_options' && is_array($value)) {
728 foreach ($value as $index => $val) {
729 $this->project->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->project);
734 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
737 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
738 return $this->
get(
$id);
740 throw new RestException(500, $this->project->error);
753 public function delete(
$id)
755 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
756 throw new RestException(403);
758 $result = $this->project->fetch(
$id);
760 throw new RestException(404,
'Project not found');
764 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
767 if (!$this->project->delete(DolibarrApiAccess::$user)) {
768 throw new RestException(500,
'Error when delete project : ' . $this->project->error);
774 'message' =>
'Project deleted'
803 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
804 throw new RestException(403);
806 $result = $this->project->fetch(
$id);
808 throw new RestException(404,
'Project not found');
812 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
815 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
817 throw new RestException(304,
'Error nothing done. May be object is already validated');
820 throw new RestException(500,
'Error when validating Project: ' . $this->project->error);
826 'message' =>
'Project validated'
848 public function listTimespent($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
850 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
851 throw new RestException(403);
855 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
859 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
860 $search_sale = DolibarrApiAccess::$user->id;
863 $sql =
"SELECT et.rowid, et.element_duration, et.element_datehour, et.fk_user, et.note as time_note, et.thm,";
864 $sql .=
" u.login as user_login, u.firstname as user_firstname, u.lastname as user_lastname,";
865 $sql .=
" p.rowid as project_id, p.ref as project_ref, p.title as project_title,";
866 $sql .=
" t.rowid as task_id, t.ref as task_ref, t.label as task_label,";
867 $sql .=
" s.rowid as soc_id, s.nom as soc_name";
868 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as p";
869 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"projet_task as t ON (t.fk_projet = p.rowid)";
870 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"element_time as et ON (et.fk_element = t.rowid AND et.elementtype = 'task')";
871 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = p.fk_soc)";
872 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user AS u ON (u.rowid = et.fk_user)";
873 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
875 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
879 if ($search_sale && $search_sale !=
'-1') {
880 if ($search_sale == -2) {
881 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
882 } elseif ($search_sale > 0) {
883 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
888 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
896 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
901 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
903 $sql .= $this->db->order($sortfield, $sortorder);
908 $offset = $limit * $page;
910 $sql .= $this->db->plimit($limit + 1, $offset);
914 $result = $this->db->query($sql);
917 $num = $this->db->num_rows($result);
918 $min = min($num, ($limit <= 0 ? $num : $limit));
921 $obj = $this->db->fetch_object($result);
926 throw new RestException(503,
'Error when retrieve timestamp list : '.$this->db->lasterror());
931 if ($pagination_data) {
932 $totalsResult = $this->db->query($sqlTotals);
933 $total = $this->db->fetch_object($totalsResult)->total;
936 $obj_ret[
'data'] = $tmp;
937 $obj_ret[
'pagination'] = [
938 'total' => (int) $total,
940 'page_count' => ceil((
int) $total / $limit),
966 unset(
$object->barcode_type_code);
967 unset(
$object->barcode_type_label);
968 unset(
$object->barcode_type_coder);
969 unset(
$object->cond_reglement_id);
970 unset(
$object->cond_reglement);
971 unset(
$object->fk_delivery_address);
972 unset(
$object->shipping_method_id);
976 unset(
$object->label_incoterms);
977 unset(
$object->location_incoterms);
982 unset(
$object->mode_reglement_id);
994 unset(
$object->total_localtax1);
995 unset(
$object->total_localtax2);
1013 foreach (self::$FIELDS as $field) {
1014 if (!isset($data[$field])) {
1015 throw new RestException(400,
"$field field missing");
1017 $object[$field] = $data[$field];
1040 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1041 throw new RestException(403);
1044 $result = $this->project->fetch(
$id);
1046 throw new RestException(404,
'Project not found');
1050 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1053 $contacts = $this->project->liste_contact(-1,
'external', 0, $type);
1054 $socpeoples = $this->project->liste_contact(-1,
'internal', 0, $type);
1056 $contacts = array_merge($contacts, $socpeoples);
1080 public function addToContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0, $affect_to_tasks =
null)
1082 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1083 throw new RestException(403);
1086 $result = $this->project->fetch(
$id);
1088 throw new RestException(404,
'Project not found');
1092 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1096 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1098 throw new RestException(500,
'Error : ' . $this->project->error .
'result :' . $result);
1102 if ($affect_to_tasks !==
null) {
1103 $this->project->getLinesArray(DolibarrApiAccess::$user);
1105 foreach ($this->project->lines as $task) {
1108 if (empty($affect_to_tasks) || in_array($task->id, $affect_to_tasks)) {
1109 $task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1114 $result = $this->project->fetch(
$id);
1116 throw new RestException(404,
'Project not found');
1137 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1138 throw new RestException(403);
1141 $result = $this->project->fetch(
$id);
1143 throw new RestException(404,
'Project not found');
1147 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1150 foreach (array(
'internal',
'external') as $source) {
1151 $contacts = $this->project->liste_contact(-1, $source);
1153 foreach ($contacts as $contact) {
1154 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
1155 $result = $this->project->delete_contact($contact[
'rowid']);
1157 throw new RestException(500,
'Error when deleted the contact');
1179 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1180 throw new RestException(403);
1183 $result = $this->project->fetch(
$id);
1185 throw new RestException(404,
'Project not found');
1189 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1193 $this->project->getLinesArray(DolibarrApiAccess::$user);
1194 $allTimespent = array();
1195 foreach ($this->project->lines as $task) {
1196 $allTimespent[] = $task->getSummaryOfTimeSpent();
1204 return $allTimespent;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $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.
Class to manage projects.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an project.
post($request_data=null)
Create project object.
addToContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0, $affect_to_tasks=null)
Adds a contact to a project.
getByMsgId($email_msgid)
Get properties of a project object.
getLines($id, $includetimespent=0)
Get tasks of a project.
validate($id, $notrigger=0)
Validate a project.
_validate($data)
Validate fields before create or update object.
getByRef($ref)
Get properties of a project object.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List projects.
getTimespent($id)
Get timespent of a project (from all its tasks)
__construct()
Constructor.
deleteToContact($id, $contactid, $type)
Delete a contact type of given project.
put($id, $request_data=null)
Add a task to given project.
listTimespent($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='', $pagination_data=false)
Get all timespent.
deleteContact($id, $contactid, $type)
Delete a contact type of given project.
getByRefExt($ref_ext)
Get properties of a project object.
getRoles($id, $userid=0)
Get roles a user is assigned to a project with.
getContacts($id, $type='')
Get contacts of given project.
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.