23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
26require_once DOL_DOCUMENT_ROOT .
'/projet/class/task.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
41 public static $FIELDS = array(
64 $this->project =
new Project($this->db);
65 $this->task =
new Task($this->db);
79 public function get(
$id)
81 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
82 throw new RestException(403);
85 $result = $this->project->fetch(
$id);
87 throw new RestException(404,
'Project with supplied id not found');
91 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
94 $this->project->fetchObjectLinked();
113 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
114 throw new RestException(403);
117 $result = $this->project->fetch(0, $ref);
119 throw new RestException(404,
'Project with supplied ref not found');
123 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
126 $this->project->fetchObjectLinked();
145 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
146 throw new RestException(403);
149 $result = $this->project->fetch(0,
'', $ref_ext);
151 throw new RestException(404,
'Project with supplied ref_ext not found');
155 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
158 $this->project->fetchObjectLinked();
177 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
178 throw new RestException(403);
181 $result = $this->project->fetch(0,
'',
'', $email_msgid);
183 throw new RestException(404,
'Project with supplied email_msgid not found');
187 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
190 $this->project->fetchObjectLinked();
213 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
215 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
216 throw new RestException(403);
222 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
226 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
227 $search_sale = DolibarrApiAccess::$user->id;
230 $sql =
"SELECT t.rowid";
231 $sql .=
" FROM " . MAIN_DB_PREFIX .
"projet as t";
232 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"societe AS s ON (s.rowid = t.fk_soc)";
233 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"projet_extrafields AS ef ON ef.fk_object = t.rowid";
235 $sql .=
", " . MAIN_DB_PREFIX .
"categorie_project as c";
237 $sql .=
' WHERE t.entity IN (' .
getEntity(
'project') .
')';
239 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
242 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'all',
'lire')) {
243 $projectsListId = $this->project->getProjectsAuthorizedForUser(DolibarrApiAccess::$user, 0, 1, 0);
244 $sql .=
" AND t.rowid IN (" . $this->db->sanitize($projectsListId) .
")";
247 if ($search_sale && $search_sale !=
'-1') {
248 if ($search_sale == -2) {
249 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
250 } elseif ($search_sale > 0) {
251 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
256 $sql .=
" AND c.fk_categorie = " . ((int) $category) .
" AND c.fk_project = t.rowid ";
263 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
268 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
270 $sql .= $this->db->order($sortfield, $sortorder);
275 $offset = $limit * $page;
277 $sql .= $this->db->plimit($limit + 1, $offset);
281 $result = $this->db->query($sql);
284 $num = $this->db->num_rows($result);
285 $min = min($num, ($limit <= 0 ? $num : $limit));
288 $obj = $this->db->fetch_object($result);
289 $project_static =
new Project($this->db);
290 if ($project_static->fetch($obj->rowid)) {
296 throw new RestException(503,
'Error when retrieve project list : ' . $this->db->lasterror());
300 if ($pagination_data) {
301 $totalsResult = $this->db->query($sqlTotals);
302 $total = $this->db->fetch_object($totalsResult)->total;
307 $obj_ret[
'data'] = $tmp;
308 $obj_ret[
'pagination'] = [
309 'total' => (int) $total,
311 'page_count' => ceil((
int) $total / $limit),
328 public function post($request_data =
null)
331 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
332 throw new RestException(403,
"Insufficiant rights");
335 $result = $this->
_validate($request_data);
337 foreach ($request_data as $field => $value) {
338 if ($field ===
'caller') {
340 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
344 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
355 if ($this->project->ref == -1 || $this->project->ref ===
'auto') {
363 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
364 foreach ($dirmodels as $reldir) {
365 $file =
dol_buildpath($reldir .
"core/modules/project/" . $modele .
'.php', 0);
366 if (file_exists($file)) {
368 $classname = $modele;
372 if ($filefound && !empty($classname)) {
373 $result =
dol_include_once($reldir .
"core/modules/project/" . $modele .
'.php');
374 if ($result !==
false && class_exists($classname)) {
375 $modProject =
new $classname();
376 '@phan-var-force ModeleNumRefProjects $modProject';
377 $defaultref = $modProject->getNextValue(
null, $this->project);
379 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/" . $modele .
'.php', LOG_ERR);
382 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
385 if (is_numeric($defaultref) && $defaultref <= 0) {
389 if (empty($defaultref)) {
393 $this->project->ref = $defaultref;
396 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
397 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
400 return $this->project->id;
422 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
424 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
425 throw new RestException(403);
428 $result = $this->project->fetch(
$id);
430 throw new RestException(404,
'project not found');
434 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
437 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
439 throw new RestException(500,
'Error : ' . $this->project->errorsToString());
462 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
463 throw new RestException(403);
466 $result = $this->project->fetch(
$id);
469 throw new RestException(404,
'Project not found');
473 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
475 foreach (array(
'internal',
'external') as $source) {
476 $contacts = $this->project->liste_contact(-1, $source);
478 foreach ($contacts as $contact) {
479 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
480 $result = $this->project->delete_contact($contact[
'rowid']);
482 throw new RestException(500,
'Error when deleted the contact');
504 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
505 throw new RestException(403);
508 $result = $this->project->fetch(
$id);
510 throw new RestException(404,
'Project not found');
514 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
516 $this->project->getLinesArray(DolibarrApiAccess::$user);
518 foreach ($this->project->lines as $line) {
519 if ($includetimespent == 1) {
520 $timespent = $line->getSummaryOfTimeSpent(0);
522 if ($includetimespent == 2) {
523 $timespent = $line->fetchTimeSpentOnTask();
545 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
546 throw new RestException(403);
549 $result = $this->project->fetch(
$id);
551 throw new RestException(404,
'Project not found');
555 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
558 require_once DOL_DOCUMENT_ROOT .
'/projet/class/task.class.php';
559 $taskstatic =
new Task($this->db);
560 $userp = DolibarrApiAccess::$user;
562 $userp =
new User($this->db);
563 $userp->fetch($userid);
565 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp,
null, (
string)
$id, 0);
567 foreach ($this->project->roles as $line) {
722 public function put(
$id, $request_data =
null)
724 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
725 throw new RestException(403);
728 $result = $this->project->fetch(
$id);
730 throw new RestException(404,
'Project not found');
734 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
736 foreach ($request_data as $field => $value) {
737 if ($field ==
'id') {
740 if ($field ===
'caller') {
742 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
745 if ($field ==
'array_options' && is_array($value)) {
746 foreach ($value as $index => $val) {
752 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
755 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
756 return $this->
get(
$id);
758 throw new RestException(500, $this->project->errorsToString());
772 public function delete(
$id)
774 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
775 throw new RestException(403);
777 $result = $this->project->fetch(
$id);
779 throw new RestException(404,
'Project not found');
783 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
786 if (!$this->project->delete(DolibarrApiAccess::$user)) {
787 throw new RestException(500,
'Error when delete project : ' . $this->project->errorsToString());
793 'message' =>
'Project deleted'
823 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
824 throw new RestException(403);
826 $result = $this->project->fetch(
$id);
828 throw new RestException(404,
'Project not found');
832 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
835 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
837 throw new RestException(304,
'Error nothing done. May be object is already validated');
840 throw new RestException(500,
'Error when validating Project: ' . $this->project->errorsToString());
846 'message' =>
'Project validated'
869 public function listTimespent($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
871 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
872 throw new RestException(403);
876 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
880 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
881 $search_sale = DolibarrApiAccess::$user->id;
885 $canreadsalary = ((
isModEnabled(
'salaries') && DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) || !
isModEnabled(
'salaries'));
887 $sql =
"SELECT et.rowid, et.element_duration, et.element_datehour, et.fk_user, et.note as time_note,";
888 if ($canreadsalary) {
891 $sql .=
" u.login as user_login, u.firstname as user_firstname, u.lastname as user_lastname,";
892 $sql .=
" p.rowid as project_id, p.ref as project_ref, p.title as project_title,";
893 $sql .=
" t.rowid as task_id, t.ref as task_ref, t.label as task_label,";
894 $sql .=
" s.rowid as soc_id, s.nom as soc_name";
895 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as p";
896 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"projet_task as t ON (t.fk_projet = p.rowid)";
897 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"element_time as et ON (et.fk_element = t.rowid AND et.elementtype = 'task')";
898 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = p.fk_soc)";
899 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user AS u ON (u.rowid = et.fk_user)";
900 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
902 $sql .=
" AND p.fk_soc IN (".$this->db->sanitize($socids).
")";
905 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'all',
'lire')) {
906 $projectsListId = $this->project->getProjectsAuthorizedForUser(DolibarrApiAccess::$user, 0, 1, 0);
907 $sql .=
" AND p.rowid IN (".$this->db->sanitize($projectsListId).
")";
911 if ($search_sale && $search_sale !=
'-1') {
912 if ($search_sale == -2) {
913 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
914 } elseif ($search_sale > 0) {
915 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
920 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
927 $forbiddenfilterfields = array(
933 'national_registration_number',
943 if (!$canreadsalary) {
944 $forbiddenfilterfields[] =
'salary';
945 $forbiddenfilterfields[] =
'salaryextra';
946 $forbiddenfilterfields[] =
'thm';
947 $forbiddenfilterfields[] =
'tjm';
952 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
957 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
959 $sql .= $this->db->order($sortfield, $sortorder);
964 $offset = $limit * $page;
966 $sql .= $this->db->plimit($limit + 1, $offset);
970 $result = $this->db->query($sql);
973 $num = $this->db->num_rows($result);
974 $min = min($num, ($limit <= 0 ? $num : $limit));
977 $obj = $this->db->fetch_object($result);
982 throw new RestException(503,
'Error when retrieve timestamp list : '.$this->db->lasterror());
987 if ($pagination_data) {
988 $totalsResult = $this->db->query($sqlTotals);
989 $total = $this->db->fetch_object($totalsResult)->total;
992 $obj_ret[
'data'] = $tmp;
993 $obj_ret[
'pagination'] = [
994 'total' => (int) $total,
996 'page_count' => ceil((
int) $total / $limit),
1022 unset(
$object->barcode_type_code);
1023 unset(
$object->barcode_type_label);
1024 unset(
$object->barcode_type_coder);
1025 unset(
$object->cond_reglement_id);
1026 unset(
$object->cond_reglement);
1027 unset(
$object->fk_delivery_address);
1028 unset(
$object->shipping_method_id);
1032 unset(
$object->label_incoterms);
1033 unset(
$object->location_incoterms);
1038 unset(
$object->mode_reglement_id);
1049 unset(
$object->total_localtax1);
1050 unset(
$object->total_localtax2);
1068 foreach (self::$FIELDS as $field) {
1069 if (!isset($data[$field])) {
1070 throw new RestException(400,
"$field field missing");
1072 $object[$field] = $data[$field];
1096 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1097 throw new RestException(403);
1100 $result = $this->project->fetch(
$id);
1102 throw new RestException(404,
'Project not found');
1106 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1109 $contacts = $this->project->liste_contact(-1,
'external', 0, $type);
1110 $socpeoples = $this->project->liste_contact(-1,
'internal', 0, $type);
1112 $contacts = array_merge($contacts, $socpeoples);
1137 public function addToContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0, $affect_to_tasks =
null)
1139 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1140 throw new RestException(403);
1143 $result = $this->project->fetch(
$id);
1145 throw new RestException(404,
'Project not found');
1149 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1153 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1155 throw new RestException(500,
'Error : ' . $this->project->error .
'result :' . $result);
1159 if ($affect_to_tasks !==
null) {
1160 $this->project->getLinesArray(DolibarrApiAccess::$user);
1161 $taskContactType = ($type_contact ==
'PROJECTLEADER' ?
'TASKEXECUTIVE' :
'TASKCONTRIBUTOR');
1163 foreach ($this->project->lines as $task) {
1166 if (empty($affect_to_tasks) || in_array($task->id, $affect_to_tasks)) {
1167 $result = $task->add_contact($fk_socpeople, $taskContactType, $source, $notrigger);
1169 throw new RestException(500,
'Error adding contact to task '.$task->id.
': '.$task->errorsToString());
1175 $result = $this->project->fetch(
$id);
1177 throw new RestException(404,
'Project not found');
1199 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1200 throw new RestException(403);
1203 $result = $this->project->fetch(
$id);
1205 throw new RestException(404,
'Project not found');
1209 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1212 foreach (array(
'internal',
'external') as $source) {
1213 $contacts = $this->project->liste_contact(-1, $source);
1215 foreach ($contacts as $contact) {
1216 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
1217 $result = $this->project->delete_contact($contact[
'rowid']);
1219 throw new RestException(500,
'Error when deleted the contact');
1242 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1243 throw new RestException(403);
1246 $result = $this->project->fetch(
$id);
1248 throw new RestException(404,
'Project not found');
1252 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1256 $this->project->getLinesArray(DolibarrApiAccess::$user);
1257 $allTimespent = array();
1258 foreach ($this->project->lines as $task) {
1259 $allTimespent[] = $task->getSummaryOfTimeSpent();
1267 return $allTimespent;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
_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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
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.
isModEnabled($module)
Is Dolibarr module enabled.
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.