20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
36 public static $FIELDS = array(
59 $this->project =
new Project($this->db);
60 $this->task =
new Task($this->db);
73 public function get(
$id)
75 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
76 throw new RestException(403);
79 $result = $this->project->fetch(
$id);
81 throw new RestException(404,
'Project with supplied id not found');
85 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
88 $this->project->fetchObjectLinked();
106 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
107 throw new RestException(403);
110 $result = $this->project->fetch(0, $ref);
112 throw new RestException(404,
'Project with supplied ref not found');
116 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
119 $this->project->fetchObjectLinked();
137 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
138 throw new RestException(403);
141 $result = $this->project->fetch(0,
'', $ref_ext);
143 throw new RestException(404,
'Project with supplied ref_ext not found');
147 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
150 $this->project->fetchObjectLinked();
168 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
169 throw new RestException(403);
172 $result = $this->project->fetch(0,
'',
'', $email_msgid);
174 throw new RestException(404,
'Project with supplied email_msgid not found');
178 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
181 $this->project->fetchObjectLinked();
203 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
205 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
206 throw new RestException(403);
212 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
216 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
217 $search_sale = DolibarrApiAccess::$user->id;
220 $sql =
"SELECT t.rowid";
221 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
222 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"projet_extrafields AS ef ON ef.fk_object = t.rowid";
224 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_project as c";
226 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
228 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
231 if ($search_sale && $search_sale !=
'-1') {
232 if ($search_sale == -2) {
233 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
234 } elseif ($search_sale > 0) {
235 $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).
")";
240 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
247 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
252 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
254 $sql .= $this->db->order($sortfield, $sortorder);
259 $offset = $limit * $page;
261 $sql .= $this->db->plimit($limit + 1, $offset);
265 $result = $this->db->query($sql);
268 $num = $this->db->num_rows($result);
269 $min = min($num, ($limit <= 0 ? $num : $limit));
272 $obj = $this->db->fetch_object($result);
273 $project_static =
new Project($this->db);
274 if ($project_static->fetch($obj->rowid)) {
280 throw new RestException(503,
'Error when retrieve project list : '.$this->db->lasterror());
284 if ($pagination_data) {
285 $totalsResult = $this->db->query($sqlTotals);
286 $total = $this->db->fetch_object($totalsResult)->total;
291 $obj_ret[
'data'] = $tmp;
292 $obj_ret[
'pagination'] = [
293 'total' => (int) $total,
295 'page_count' => ceil((
int) $total / $limit),
311 public function post($request_data =
null)
314 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
315 throw new RestException(403,
"Insuffisant rights");
318 $result = $this->
_validate($request_data);
320 foreach ($request_data as $field => $value) {
321 if ($field ===
'caller') {
323 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
327 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
338 if ($this->project->ref == -1 || $this->project->ref ===
'auto') {
346 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
347 foreach ($dirmodels as $reldir) {
348 $file =
dol_buildpath($reldir.
"core/modules/project/".$modele.
'.php', 0);
349 if (file_exists($file)) {
351 $classname = $modele;
355 if ($filefound && !empty($classname)) {
356 $result =
dol_include_once($reldir .
"core/modules/project/" . $modele .
'.php');
357 if ($result !==
false && class_exists($classname)) {
358 $modProject =
new $classname();
359 '@phan-var-force ModeleNumRefProjects $modProject';
360 $defaultref = $modProject->getNextValue(
null, $this->project);
362 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/" . $modele .
'.php', LOG_ERR);
365 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
368 if (is_numeric($defaultref) && $defaultref <= 0) {
372 if (empty($defaultref)) {
376 $this->project->ref = $defaultref;
379 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
380 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
383 return $this->project->id;
400 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
401 throw new RestException(403);
404 $result = $this->project->fetch(
$id);
406 throw new RestException(404,
'Project not found');
410 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
412 $this->project->getLinesArray(DolibarrApiAccess::$user);
414 foreach ($this->project->lines as $line) {
415 if ($includetimespent == 1) {
416 $timespent = $line->getSummaryOfTimeSpent(0);
418 if ($includetimespent == 2) {
419 $timespent = $line->fetchTimeSpentOnTask();
442 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
443 throw new RestException(403);
446 $result = $this->project->fetch(
$id);
448 throw new RestException(404,
'Project not found');
452 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
455 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
456 $taskstatic =
new Task($this->db);
457 $userp = DolibarrApiAccess::$user;
459 $userp =
new User($this->db);
460 $userp->fetch($userid);
462 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp,
null,
$id, 0);
464 foreach ($this->project->roles as $line) {
618 public function put(
$id, $request_data =
null)
620 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
621 throw new RestException(403);
624 $result = $this->project->fetch(
$id);
626 throw new RestException(404,
'Project not found');
630 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
632 foreach ($request_data as $field => $value) {
633 if ($field ==
'id') {
636 if ($field ===
'caller') {
638 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
641 if ($field ==
'array_options' && is_array($value)) {
642 foreach ($value as $index => $val) {
643 $this->project->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->project);
648 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
651 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
652 return $this->
get(
$id);
654 throw new RestException(500, $this->project->error);
667 public function delete(
$id)
669 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
670 throw new RestException(403);
672 $result = $this->project->fetch(
$id);
674 throw new RestException(404,
'Project not found');
678 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
681 if (!$this->project->delete(DolibarrApiAccess::$user)) {
682 throw new RestException(500,
'Error when delete project : '.$this->project->error);
688 'message' =>
'Project deleted'
717 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
718 throw new RestException(403);
720 $result = $this->project->fetch(
$id);
722 throw new RestException(404,
'Project not found');
726 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
729 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
731 throw new RestException(304,
'Error nothing done. May be object is already validated');
734 throw new RestException(500,
'Error when validating Project: '.$this->project->error);
740 'message' =>
'Project validated'
761 unset(
$object->barcode_type_code);
762 unset(
$object->barcode_type_label);
763 unset(
$object->barcode_type_coder);
764 unset(
$object->cond_reglement_id);
765 unset(
$object->cond_reglement);
766 unset(
$object->fk_delivery_address);
767 unset(
$object->shipping_method_id);
771 unset(
$object->label_incoterms);
772 unset(
$object->location_incoterms);
777 unset(
$object->mode_reglement_id);
789 unset(
$object->total_localtax1);
790 unset(
$object->total_localtax2);
808 foreach (self::$FIELDS as $field) {
809 if (!isset($data[$field])) {
810 throw new RestException(400,
"$field field missing");
812 $object[$field] = $data[$field];
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.
Class to manage projects.
post($request_data=null)
Create project object.
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List projects.
__construct()
Constructor.
put($id, $request_data=null)
Add a task to 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.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...