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 ($search_sale && $search_sale !=
'-1') {
243 if ($search_sale == -2) {
244 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
245 } elseif ($search_sale > 0) {
246 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
251 $sql .=
" AND c.fk_categorie = " . ((int) $category) .
" AND c.fk_project = t.rowid ";
258 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
263 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
265 $sql .= $this->db->order($sortfield, $sortorder);
270 $offset = $limit * $page;
272 $sql .= $this->db->plimit($limit + 1, $offset);
276 $result = $this->db->query($sql);
279 $num = $this->db->num_rows($result);
280 $min = min($num, ($limit <= 0 ? $num : $limit));
283 $obj = $this->db->fetch_object($result);
284 $project_static =
new Project($this->db);
285 if ($project_static->fetch($obj->rowid)) {
291 throw new RestException(503,
'Error when retrieve project list : ' . $this->db->lasterror());
295 if ($pagination_data) {
296 $totalsResult = $this->db->query($sqlTotals);
297 $total = $this->db->fetch_object($totalsResult)->total;
302 $obj_ret[
'data'] = $tmp;
303 $obj_ret[
'pagination'] = [
304 'total' => (int) $total,
306 'page_count' => ceil((
int) $total / $limit),
323 public function post($request_data =
null)
326 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
327 throw new RestException(403,
"Insufficiant rights");
330 $result = $this->
_validate($request_data);
332 foreach ($request_data as $field => $value) {
333 if ($field ===
'caller') {
335 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
339 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
350 if ($this->project->ref == -1 || $this->project->ref ===
'auto') {
358 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
359 foreach ($dirmodels as $reldir) {
360 $file =
dol_buildpath($reldir .
"core/modules/project/" . $modele .
'.php', 0);
361 if (file_exists($file)) {
363 $classname = $modele;
367 if ($filefound && !empty($classname)) {
368 $result =
dol_include_once($reldir .
"core/modules/project/" . $modele .
'.php');
369 if ($result !==
false && class_exists($classname)) {
370 $modProject =
new $classname();
371 '@phan-var-force ModeleNumRefProjects $modProject';
372 $defaultref = $modProject->getNextValue(
null, $this->project);
374 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/" . $modele .
'.php', LOG_ERR);
377 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
380 if (is_numeric($defaultref) && $defaultref <= 0) {
384 if (empty($defaultref)) {
388 $this->project->ref = $defaultref;
391 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
392 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
395 return $this->project->id;
417 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
419 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
420 throw new RestException(403);
423 $result = $this->project->fetch(
$id);
425 throw new RestException(404,
'project not found');
429 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
432 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
434 throw new RestException(500,
'Error : ' . $this->project->error);
457 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
458 throw new RestException(403);
461 $result = $this->project->fetch(
$id);
464 throw new RestException(404,
'Project not found');
468 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
470 foreach (array(
'internal',
'external') as $source) {
471 $contacts = $this->project->liste_contact(-1, $source);
473 foreach ($contacts as $contact) {
474 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
475 $result = $this->project->delete_contact($contact[
'rowid']);
477 throw new RestException(500,
'Error when deleted the contact');
499 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
500 throw new RestException(403);
503 $result = $this->project->fetch(
$id);
505 throw new RestException(404,
'Project not found');
509 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
511 $this->project->getLinesArray(DolibarrApiAccess::$user);
513 foreach ($this->project->lines as $line) {
514 if ($includetimespent == 1) {
515 $timespent = $line->getSummaryOfTimeSpent(0);
517 if ($includetimespent == 2) {
518 $timespent = $line->fetchTimeSpentOnTask();
540 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
541 throw new RestException(403);
544 $result = $this->project->fetch(
$id);
546 throw new RestException(404,
'Project not found');
550 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
553 require_once DOL_DOCUMENT_ROOT .
'/projet/class/task.class.php';
554 $taskstatic =
new Task($this->db);
555 $userp = DolibarrApiAccess::$user;
557 $userp =
new User($this->db);
558 $userp->fetch($userid);
560 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp,
null, (
string)
$id, 0);
562 foreach ($this->project->roles as $line) {
717 public function put(
$id, $request_data =
null)
719 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
720 throw new RestException(403);
723 $result = $this->project->fetch(
$id);
725 throw new RestException(404,
'Project not found');
729 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
731 foreach ($request_data as $field => $value) {
732 if ($field ==
'id') {
735 if ($field ===
'caller') {
737 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
740 if ($field ==
'array_options' && is_array($value)) {
741 foreach ($value as $index => $val) {
747 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
750 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
751 return $this->
get(
$id);
753 throw new RestException(500, $this->project->error);
767 public function delete(
$id)
769 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
770 throw new RestException(403);
772 $result = $this->project->fetch(
$id);
774 throw new RestException(404,
'Project not found');
778 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
781 if (!$this->project->delete(DolibarrApiAccess::$user)) {
782 throw new RestException(500,
'Error when delete project : ' . $this->project->error);
788 'message' =>
'Project deleted'
818 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
819 throw new RestException(403);
821 $result = $this->project->fetch(
$id);
823 throw new RestException(404,
'Project not found');
827 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
830 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
832 throw new RestException(304,
'Error nothing done. May be object is already validated');
835 throw new RestException(500,
'Error when validating Project: ' . $this->project->error);
841 'message' =>
'Project validated'
864 public function listTimespent($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
866 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
867 throw new RestException(403);
871 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
875 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
876 $search_sale = DolibarrApiAccess::$user->id;
879 $sql =
"SELECT et.rowid, et.element_duration, et.element_datehour, et.fk_user, et.note as time_note, et.thm,";
880 $sql .=
" u.login as user_login, u.firstname as user_firstname, u.lastname as user_lastname,";
881 $sql .=
" p.rowid as project_id, p.ref as project_ref, p.title as project_title,";
882 $sql .=
" t.rowid as task_id, t.ref as task_ref, t.label as task_label,";
883 $sql .=
" s.rowid as soc_id, s.nom as soc_name";
884 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as p";
885 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"projet_task as t ON (t.fk_projet = p.rowid)";
886 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"element_time as et ON (et.fk_element = t.rowid AND et.elementtype = 'task')";
887 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = p.fk_soc)";
888 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user AS u ON (u.rowid = et.fk_user)";
889 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
891 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
895 if ($search_sale && $search_sale !=
'-1') {
896 if ($search_sale == -2) {
897 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
898 } elseif ($search_sale > 0) {
899 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
904 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
912 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
917 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
919 $sql .= $this->db->order($sortfield, $sortorder);
924 $offset = $limit * $page;
926 $sql .= $this->db->plimit($limit + 1, $offset);
930 $result = $this->db->query($sql);
933 $num = $this->db->num_rows($result);
934 $min = min($num, ($limit <= 0 ? $num : $limit));
937 $obj = $this->db->fetch_object($result);
942 throw new RestException(503,
'Error when retrieve timestamp list : '.$this->db->lasterror());
947 if ($pagination_data) {
948 $totalsResult = $this->db->query($sqlTotals);
949 $total = $this->db->fetch_object($totalsResult)->total;
952 $obj_ret[
'data'] = $tmp;
953 $obj_ret[
'pagination'] = [
954 'total' => (int) $total,
956 'page_count' => ceil((
int) $total / $limit),
982 unset(
$object->barcode_type_code);
983 unset(
$object->barcode_type_label);
984 unset(
$object->barcode_type_coder);
985 unset(
$object->cond_reglement_id);
986 unset(
$object->cond_reglement);
987 unset(
$object->fk_delivery_address);
988 unset(
$object->shipping_method_id);
992 unset(
$object->label_incoterms);
993 unset(
$object->location_incoterms);
998 unset(
$object->mode_reglement_id);
1010 unset(
$object->total_localtax1);
1011 unset(
$object->total_localtax2);
1029 foreach (self::$FIELDS as $field) {
1030 if (!isset($data[$field])) {
1031 throw new RestException(400,
"$field field missing");
1033 $object[$field] = $data[$field];
1057 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1058 throw new RestException(403);
1061 $result = $this->project->fetch(
$id);
1063 throw new RestException(404,
'Project not found');
1067 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1070 $contacts = $this->project->liste_contact(-1,
'external', 0, $type);
1071 $socpeoples = $this->project->liste_contact(-1,
'internal', 0, $type);
1073 $contacts = array_merge($contacts, $socpeoples);
1098 public function addToContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0, $affect_to_tasks =
null)
1100 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1101 throw new RestException(403);
1104 $result = $this->project->fetch(
$id);
1106 throw new RestException(404,
'Project not found');
1110 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1114 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1116 throw new RestException(500,
'Error : ' . $this->project->error .
'result :' . $result);
1120 if ($affect_to_tasks !==
null) {
1121 $this->project->getLinesArray(DolibarrApiAccess::$user);
1123 foreach ($this->project->lines as $task) {
1126 if (empty($affect_to_tasks) || in_array($task->id, $affect_to_tasks)) {
1127 $task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1132 $result = $this->project->fetch(
$id);
1134 throw new RestException(404,
'Project not found');
1156 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1157 throw new RestException(403);
1160 $result = $this->project->fetch(
$id);
1162 throw new RestException(404,
'Project not found');
1166 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1169 foreach (array(
'internal',
'external') as $source) {
1170 $contacts = $this->project->liste_contact(-1, $source);
1172 foreach ($contacts as $contact) {
1173 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
1174 $result = $this->project->delete_contact($contact[
'rowid']);
1176 throw new RestException(500,
'Error when deleted the contact');
1199 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
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);
1213 $this->project->getLinesArray(DolibarrApiAccess::$user);
1214 $allTimespent = array();
1215 foreach ($this->project->lines as $task) {
1216 $allTimespent[] = $task->getSummaryOfTimeSpent();
1224 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.
_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.
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.