19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
36 public static $FIELDS = array(
54 $this->task =
new Task($this->
db);
67 public function get($id)
69 if (!DolibarrApiAccess::$user->rights->projet->lire) {
70 throw new RestException(401);
73 $result = $this->project->fetch($id);
75 throw new RestException(404,
'Project not found');
79 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
82 $this->project->fetchObjectLinked();
102 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'')
106 if (!DolibarrApiAccess::$user->rights->projet->lire) {
107 throw new RestException(401);
113 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
117 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
118 $search_sale = DolibarrApiAccess::$user->id;
121 $sql =
"SELECT t.rowid";
122 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
123 $sql .=
", sc.fk_soc, sc.fk_user";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
126 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"projet_extrafields AS ef ON ef.fk_object = t.rowid";
128 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_project as c";
130 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
131 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
134 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
135 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
136 $sql .=
" AND t.fk_soc = sc.fk_soc";
139 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
141 if ($search_sale > 0) {
142 $sql .=
" AND t.rowid = sc.fk_soc";
145 if ($search_sale > 0) {
146 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
150 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
157 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
161 $sql .= $this->
db->order($sortfield, $sortorder);
166 $offset = $limit * $page;
168 $sql .= $this->
db->plimit($limit + 1, $offset);
172 $result = $this->
db->query($sql);
175 $num = $this->
db->num_rows($result);
176 $min = min($num, ($limit <= 0 ? $num : $limit));
179 $obj = $this->
db->fetch_object($result);
180 $project_static =
new Project($this->
db);
181 if ($project_static->fetch($obj->rowid)) {
187 throw new RestException(503,
'Error when retrieve project list : '.$this->
db->lasterror());
189 if (!count($obj_ret)) {
190 throw new RestException(404,
'No project found');
201 public function post($request_data =
null)
203 if (!DolibarrApiAccess::$user->rights->projet->creer) {
204 throw new RestException(401,
"Insuffisant rights");
207 $result = $this->
_validate($request_data);
209 foreach ($request_data as $field => $value) {
210 $this->project->$field = $value;
219 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
220 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
223 return $this->project->id;
236 public function getLines($id, $includetimespent = 0)
238 if (!DolibarrApiAccess::$user->rights->projet->lire) {
239 throw new RestException(401);
242 $result = $this->project->fetch($id);
244 throw new RestException(404,
'Project not found');
248 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
250 $this->project->getLinesArray(DolibarrApiAccess::$user);
252 foreach ($this->project->lines as $line) {
253 if ($includetimespent == 1) {
254 $timespent = $line->getSummaryOfTimeSpent(0);
256 if ($includetimespent == 2) {
257 $timespent = $line->fetchTimeSpentOnTask();
279 if (!DolibarrApiAccess::$user->rights->projet->lire) {
280 throw new RestException(401);
283 $result = $this->project->fetch($id);
285 throw new RestException(404,
'Project not found');
289 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
292 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
293 $taskstatic =
new Task($this->
db);
294 $userp = DolibarrApiAccess::$user;
296 $userp =
new User($this->
db);
297 $userp->fetch($userid);
299 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, 0, $id, 0);
301 foreach ($this->project->roles as $line) {
447 public function put($id, $request_data =
null)
449 if (!DolibarrApiAccess::$user->rights->projet->creer) {
450 throw new RestException(401);
453 $result = $this->project->fetch($id);
455 throw new RestException(404,
'Project not found');
459 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
461 foreach ($request_data as $field => $value) {
462 if ($field ==
'id') {
465 $this->project->$field = $value;
468 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
469 return $this->
get($id);
471 throw new RestException(500, $this->project->error);
482 public function delete($id)
484 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
485 throw new RestException(401);
487 $result = $this->project->fetch($id);
489 throw new RestException(404,
'Project not found');
493 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
496 if (!$this->project->delete(DolibarrApiAccess::$user)) {
497 throw new RestException(500,
'Error when delete project : '.$this->project->error);
503 'message' =>
'Project deleted'
528 if (!DolibarrApiAccess::$user->rights->projet->creer) {
529 throw new RestException(401);
531 $result = $this->project->fetch($id);
533 throw new RestException(404,
'Project not found');
537 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
540 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
542 throw new RestException(304,
'Error nothing done. May be object is already validated');
545 throw new RestException(500,
'Error when validating Project: '.$this->project->error);
551 'message' =>
'Project validated'
567 $object = parent::_cleanObjectDatas($object);
569 unset($object->datec);
570 unset($object->datem);
571 unset($object->barcode_type);
572 unset($object->barcode_type_code);
573 unset($object->barcode_type_label);
574 unset($object->barcode_type_coder);
575 unset($object->cond_reglement_id);
576 unset($object->cond_reglement);
577 unset($object->fk_delivery_address);
578 unset($object->shipping_method_id);
579 unset($object->fk_account);
580 unset($object->note);
581 unset($object->fk_incoterms);
582 unset($object->label_incoterms);
583 unset($object->location_incoterms);
584 unset($object->name);
585 unset($object->lastname);
586 unset($object->firstname);
587 unset($object->civility_id);
588 unset($object->mode_reglement_id);
589 unset($object->country);
590 unset($object->country_id);
591 unset($object->country_code);
593 unset($object->weekWorkLoad);
594 unset($object->weekWorkLoad);
598 unset($object->total_ht);
599 unset($object->total_tva);
600 unset($object->total_localtax1);
601 unset($object->total_localtax2);
602 unset($object->total_ttc);
604 unset($object->comments);
619 foreach (self::$FIELDS as $field) {
620 if (!isset($data[$field])) {
621 throw new RestException(400,
"$field field missing");
623 $object[$field] = $data[$field];