23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
26require_once DOL_DOCUMENT_ROOT .
'/projet/class/task.class.php';
40 public static $FIELDS = array(
63 $this->project =
new Project($this->db);
64 $this->task =
new Task($this->db);
78 public function get(
$id)
80 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
81 throw new RestException(403);
84 $result = $this->project->fetch(
$id);
86 throw new RestException(404,
'Project with supplied id not found');
90 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
93 $this->project->fetchObjectLinked();
112 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
113 throw new RestException(403);
116 $result = $this->project->fetch(0, $ref);
118 throw new RestException(404,
'Project with supplied ref not found');
122 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
125 $this->project->fetchObjectLinked();
144 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
145 throw new RestException(403);
148 $result = $this->project->fetch(0,
'', $ref_ext);
150 throw new RestException(404,
'Project with supplied ref_ext not found');
154 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
157 $this->project->fetchObjectLinked();
176 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
177 throw new RestException(403);
180 $result = $this->project->fetch(0,
'',
'', $email_msgid);
182 throw new RestException(404,
'Project with supplied email_msgid not found');
186 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
189 $this->project->fetchObjectLinked();
212 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
214 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
215 throw new RestException(403);
221 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
225 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
226 $search_sale = DolibarrApiAccess::$user->id;
229 $sql =
"SELECT t.rowid";
230 $sql .=
" FROM " . MAIN_DB_PREFIX .
"projet as t";
231 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"societe AS s ON (s.rowid = t.fk_soc)";
232 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"projet_extrafields AS ef ON ef.fk_object = t.rowid";
234 $sql .=
", " . MAIN_DB_PREFIX .
"categorie_project as c";
236 $sql .=
' WHERE t.entity IN (' .
getEntity(
'project') .
')';
238 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
241 if ($search_sale && $search_sale !=
'-1') {
242 if ($search_sale == -2) {
243 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX .
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
244 } elseif ($search_sale > 0) {
245 $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) .
")";
250 $sql .=
" AND c.fk_categorie = " . ((int) $category) .
" AND c.fk_project = t.rowid ";
255 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
257 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
262 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
264 $sql .= $this->db->order($sortfield, $sortorder);
269 $offset = $limit * $page;
271 $sql .= $this->db->plimit($limit + 1, $offset);
275 $result = $this->db->query($sql);
278 $num = $this->db->num_rows($result);
279 $min = min($num, ($limit <= 0 ? $num : $limit));
282 $obj = $this->db->fetch_object($result);
283 $project_static =
new Project($this->db);
284 if ($project_static->fetch($obj->rowid)) {
290 throw new RestException(503,
'Error when retrieve project list : ' . $this->db->lasterror());
294 if ($pagination_data) {
295 $totalsResult = $this->db->query($sqlTotals);
296 $total = $this->db->fetch_object($totalsResult)->total;
301 $obj_ret[
'data'] = $tmp;
302 $obj_ret[
'pagination'] = [
303 'total' => (int) $total,
305 'page_count' => ceil((
int) $total / $limit),
322 public function post($request_data =
null)
325 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
326 throw new RestException(403,
"Insufficiant rights");
329 $result = $this->
_validate($request_data);
331 foreach ($request_data as $field => $value) {
332 if ($field ===
'caller') {
334 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
338 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
349 if ($this->project->ref == -1 || $this->project->ref ===
'auto') {
357 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
358 foreach ($dirmodels as $reldir) {
359 $file =
dol_buildpath($reldir .
"core/modules/project/" . $modele .
'.php', 0);
360 if (file_exists($file)) {
362 $classname = $modele;
366 if ($filefound && !empty($classname)) {
367 $result =
dol_include_once($reldir .
"core/modules/project/" . $modele .
'.php');
368 if ($result !==
false && class_exists($classname)) {
369 $modProject =
new $classname();
370 '@phan-var-force ModeleNumRefProjects $modProject';
371 $defaultref = $modProject->getNextValue(
null, $this->project);
373 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/" . $modele .
'.php', LOG_ERR);
376 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
379 if (is_numeric($defaultref) && $defaultref <= 0) {
383 if (empty($defaultref)) {
387 $this->project->ref = $defaultref;
390 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
391 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
394 return $this->project->id;
416 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
418 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
419 throw new RestException(403);
422 $result = $this->project->fetch(
$id);
424 throw new RestException(404,
'project not found');
428 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
431 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
433 throw new RestException(500,
'Error : ' . $this->project->error);
456 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
457 throw new RestException(403);
460 $result = $this->project->fetch(
$id);
463 throw new RestException(404,
'Project not found');
467 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
469 foreach (array(
'internal',
'external') as $source) {
470 $contacts = $this->project->liste_contact(-1, $source);
472 foreach ($contacts as $contact) {
473 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
474 $result = $this->project->delete_contact($contact[
'rowid']);
476 throw new RestException(500,
'Error when deleted the contact');
498 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
499 throw new RestException(403);
502 $result = $this->project->fetch(
$id);
504 throw new RestException(404,
'Project not found');
508 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
510 $this->project->getLinesArray(DolibarrApiAccess::$user);
512 foreach ($this->project->lines as $line) {
513 if ($includetimespent == 1) {
514 $timespent = $line->getSummaryOfTimeSpent(0);
516 if ($includetimespent == 2) {
517 $timespent = $line->fetchTimeSpentOnTask();
539 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
540 throw new RestException(403);
543 $result = $this->project->fetch(
$id);
545 throw new RestException(404,
'Project not found');
549 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
552 require_once DOL_DOCUMENT_ROOT .
'/projet/class/task.class.php';
553 $taskstatic =
new Task($this->db);
554 $userp = DolibarrApiAccess::$user;
556 $userp =
new User($this->db);
557 $userp->fetch($userid);
559 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp,
null, (
string)
$id, 0);
561 foreach ($this->project->roles as $line) {
716 public function put(
$id, $request_data =
null)
718 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
719 throw new RestException(403);
722 $result = $this->project->fetch(
$id);
724 throw new RestException(404,
'Project not found');
728 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
730 foreach ($request_data as $field => $value) {
731 if ($field ==
'id') {
734 if ($field ===
'caller') {
736 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
739 if ($field ==
'array_options' && is_array($value)) {
740 foreach ($value as $index => $val) {
746 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
749 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
750 return $this->
get(
$id);
752 throw new RestException(500, $this->project->error);
766 public function delete(
$id)
768 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
769 throw new RestException(403);
771 $result = $this->project->fetch(
$id);
773 throw new RestException(404,
'Project not found');
777 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
780 if (!$this->project->delete(DolibarrApiAccess::$user)) {
781 throw new RestException(500,
'Error when delete project : ' . $this->project->error);
787 'message' =>
'Project deleted'
817 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
818 throw new RestException(403);
820 $result = $this->project->fetch(
$id);
822 throw new RestException(404,
'Project not found');
826 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
829 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
831 throw new RestException(304,
'Error nothing done. May be object is already validated');
834 throw new RestException(500,
'Error when validating Project: ' . $this->project->error);
840 'message' =>
'Project validated'
863 public function listTimespent($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
865 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
866 throw new RestException(403);
870 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
874 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
875 $search_sale = DolibarrApiAccess::$user->id;
878 $sql =
"SELECT et.rowid, et.element_duration, et.element_datehour, et.fk_user, et.note as time_note, et.thm,";
879 $sql .=
" u.login as user_login, u.firstname as user_firstname, u.lastname as user_lastname,";
880 $sql .=
" p.rowid as project_id, p.ref as project_ref, p.title as project_title,";
881 $sql .=
" t.rowid as task_id, t.ref as task_ref, t.label as task_label,";
882 $sql .=
" s.rowid as soc_id, s.nom as soc_name";
883 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as p";
884 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"projet_task as t ON (t.fk_projet = p.rowid)";
885 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"element_time as et ON (et.fk_element = t.rowid AND et.elementtype = 'task')";
886 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = p.fk_soc)";
887 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user AS u ON (u.rowid = et.fk_user)";
888 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
890 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
894 if ($search_sale && $search_sale !=
'-1') {
895 if ($search_sale == -2) {
896 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
897 } elseif ($search_sale > 0) {
898 $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).
")";
903 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
909 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
911 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
916 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
918 $sql .= $this->db->order($sortfield, $sortorder);
923 $offset = $limit * $page;
925 $sql .= $this->db->plimit($limit + 1, $offset);
929 $result = $this->db->query($sql);
932 $num = $this->db->num_rows($result);
933 $min = min($num, ($limit <= 0 ? $num : $limit));
936 $obj = $this->db->fetch_object($result);
941 throw new RestException(503,
'Error when retrieve timestamp list : '.$this->db->lasterror());
946 if ($pagination_data) {
947 $totalsResult = $this->db->query($sqlTotals);
948 $total = $this->db->fetch_object($totalsResult)->total;
951 $obj_ret[
'data'] = $tmp;
952 $obj_ret[
'pagination'] = [
953 'total' => (int) $total,
955 'page_count' => ceil((
int) $total / $limit),
981 unset(
$object->barcode_type_code);
982 unset(
$object->barcode_type_label);
983 unset(
$object->barcode_type_coder);
984 unset(
$object->cond_reglement_id);
985 unset(
$object->cond_reglement);
986 unset(
$object->fk_delivery_address);
987 unset(
$object->shipping_method_id);
991 unset(
$object->label_incoterms);
992 unset(
$object->location_incoterms);
997 unset(
$object->mode_reglement_id);
1009 unset(
$object->total_localtax1);
1010 unset(
$object->total_localtax2);
1028 foreach (self::$FIELDS as $field) {
1029 if (!isset($data[$field])) {
1030 throw new RestException(400,
"$field field missing");
1032 $object[$field] = $data[$field];
1056 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1057 throw new RestException(403);
1060 $result = $this->project->fetch(
$id);
1062 throw new RestException(404,
'Project not found');
1066 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1069 $contacts = $this->project->liste_contact(-1,
'external', 0, $type);
1070 $socpeoples = $this->project->liste_contact(-1,
'internal', 0, $type);
1072 $contacts = array_merge($contacts, $socpeoples);
1097 public function addToContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0, $affect_to_tasks =
null)
1099 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1100 throw new RestException(403);
1103 $result = $this->project->fetch(
$id);
1105 throw new RestException(404,
'Project not found');
1109 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1113 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1115 throw new RestException(500,
'Error : ' . $this->project->error .
'result :' . $result);
1119 if ($affect_to_tasks !==
null) {
1120 $this->project->getLinesArray(DolibarrApiAccess::$user);
1122 foreach ($this->project->lines as $task) {
1125 if (empty($affect_to_tasks) || in_array($task->id, $affect_to_tasks)) {
1126 $task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1131 $result = $this->project->fetch(
$id);
1133 throw new RestException(404,
'Project not found');
1155 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1156 throw new RestException(403);
1159 $result = $this->project->fetch(
$id);
1161 throw new RestException(404,
'Project not found');
1165 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1168 foreach (array(
'internal',
'external') as $source) {
1169 $contacts = $this->project->liste_contact(-1, $source);
1171 foreach ($contacts as $contact) {
1172 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
1173 $result = $this->project->delete_contact($contact[
'rowid']);
1175 throw new RestException(500,
'Error when deleted the contact');
1198 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1199 throw new RestException(403);
1202 $result = $this->project->fetch(
$id);
1204 throw new RestException(404,
'Project not found');
1208 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1212 $this->project->getLinesArray(DolibarrApiAccess::$user);
1213 $allTimespent = array();
1214 foreach ($this->project->lines as $task) {
1215 $allTimespent[] = $task->getSummaryOfTimeSpent();
1223 return $allTimespent;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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.
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.