dolibarr 21.0.0-beta
api_projects.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
23require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
24
31class Projects extends DolibarrApi
32{
36 public static $FIELDS = array(
37 'ref',
38 'title'
39 );
40
44 public $project;
45
49 public $task;
50
51
55 public function __construct()
56 {
57 global $db, $conf;
58 $this->db = $db;
59 $this->project = new Project($this->db);
60 $this->task = new Task($this->db);
61 }
62
73 public function get($id)
74 {
75 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
76 throw new RestException(403);
77 }
78
79 $result = $this->project->fetch($id);
80 if (!$result) {
81 throw new RestException(404, 'Project with supplied id not found');
82 }
83
84 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
85 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
86 }
87
88 $this->project->fetchObjectLinked();
89 return $this->_cleanObjectDatas($this->project);
90 }
91
104 public function getByRef($ref)
105 {
106 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
107 throw new RestException(403);
108 }
109
110 $result = $this->project->fetch(0, $ref);
111 if (!$result) {
112 throw new RestException(404, 'Project with supplied ref not found');
113 }
114
115 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
116 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
117 }
118
119 $this->project->fetchObjectLinked();
120 return $this->_cleanObjectDatas($this->project);
121 }
122
135 public function getByRefExt($ref_ext)
136 {
137 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
138 throw new RestException(403);
139 }
140
141 $result = $this->project->fetch(0, '', $ref_ext);
142 if (!$result) {
143 throw new RestException(404, 'Project with supplied ref_ext not found');
144 }
145
146 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
147 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
148 }
149
150 $this->project->fetchObjectLinked();
151 return $this->_cleanObjectDatas($this->project);
152 }
153
166 public function getByMsgId($email_msgid)
167 {
168 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
169 throw new RestException(403);
170 }
171
172 $result = $this->project->fetch(0, '', '', $email_msgid);
173 if (!$result) {
174 throw new RestException(404, 'Project with supplied email_msgid not found');
175 }
176
177 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
178 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
179 }
180
181 $this->project->fetchObjectLinked();
182 return $this->_cleanObjectDatas($this->project);
183 }
184
203 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
204 {
205 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
206 throw new RestException(403);
207 }
208
209 $obj_ret = array();
210
211 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
212 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
213
214 // If the internal user must only see his customers, force searching by him
215 $search_sale = 0;
216 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
217 $search_sale = DolibarrApiAccess::$user->id;
218 }
219
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"; // So we will be able to filter on extrafields
223 if ($category > 0) {
224 $sql .= ", ".MAIN_DB_PREFIX."categorie_project as c";
225 }
226 $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
227 if ($socids) {
228 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
229 }
230 // Search on sale representative
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).")";
236 }
237 }
238 // Select projects of given category
239 if ($category > 0) {
240 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_project = t.rowid ";
241 }
242 // Add sql filters
243 if ($sqlfilters) {
244 $errormessage = '';
245 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
246 if ($errormessage) {
247 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
248 }
249 }
250
251 //this query will return total orders with the filters given
252 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
253
254 $sql .= $this->db->order($sortfield, $sortorder);
255 if ($limit) {
256 if ($page < 0) {
257 $page = 0;
258 }
259 $offset = $limit * $page;
260
261 $sql .= $this->db->plimit($limit + 1, $offset);
262 }
263
264 dol_syslog("API Rest request");
265 $result = $this->db->query($sql);
266
267 if ($result) {
268 $num = $this->db->num_rows($result);
269 $min = min($num, ($limit <= 0 ? $num : $limit));
270 $i = 0;
271 while ($i < $min) {
272 $obj = $this->db->fetch_object($result);
273 $project_static = new Project($this->db);
274 if ($project_static->fetch($obj->rowid)) {
275 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($project_static), $properties);
276 }
277 $i++;
278 }
279 } else {
280 throw new RestException(503, 'Error when retrieve project list : '.$this->db->lasterror());
281 }
282
283 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
284 if ($pagination_data) {
285 $totalsResult = $this->db->query($sqlTotals);
286 $total = $this->db->fetch_object($totalsResult)->total;
287
288 $tmp = $obj_ret;
289 $obj_ret = [];
290
291 $obj_ret['data'] = $tmp;
292 $obj_ret['pagination'] = [
293 'total' => (int) $total,
294 'page' => $page, //count starts from 0
295 'page_count' => ceil((int) $total / $limit),
296 'limit' => $limit
297 ];
298 }
299
300 return $obj_ret;
301 }
302
311 public function post($request_data = null)
312 {
313 global $conf;
314 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
315 throw new RestException(403, "Insuffisant rights");
316 }
317 // Check mandatory fields
318 $result = $this->_validate($request_data);
319
320 foreach ($request_data as $field => $value) {
321 if ($field === 'caller') {
322 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
323 $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
324 continue;
325 }
326
327 $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
328 }
329 /*if (isset($request_data["lines"])) {
330 $lines = array();
331 foreach ($request_data["lines"] as $line) {
332 array_push($lines, (object) $line);
333 }
334 $this->project->lines = $lines;
335 }*/
336
337 // Auto-generate the "ref" field if it is set to "auto"
338 if ($this->project->ref == -1 || $this->project->ref === 'auto') {
339 $reldir = '';
340 $defaultref = '';
341 $file = '';
342 $classname = '';
343 $filefound = 0;
344 $modele = getDolGlobalString('PROJECT_ADDON', 'mod_project_simple');
345
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)) {
350 $filefound = 1;
351 $classname = $modele;
352 break;
353 }
354 }
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);
361 } else {
362 dol_syslog("Failed to include module file or invalid classname: " . $reldir . "core/modules/project/" . $modele . '.php', LOG_ERR);
363 }
364 } else {
365 dol_syslog("Module file not found or classname is empty: " . $modele, LOG_ERR);
366 }
367
368 if (is_numeric($defaultref) && $defaultref <= 0) {
369 $defaultref = '';
370 }
371
372 if (empty($defaultref)) {
373 $defaultref = 'PJ' . dol_print_date(dol_now(), 'dayrfc');
374 }
375
376 $this->project->ref = $defaultref;
377 }
378
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));
381 }
382
383 return $this->project->id;
384 }
385
398 public function getLines($id, $includetimespent = 0)
399 {
400 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
401 throw new RestException(403);
402 }
403
404 $result = $this->project->fetch($id);
405 if (!$result) {
406 throw new RestException(404, 'Project not found');
407 }
408
409 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
410 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
411 }
412 $this->project->getLinesArray(DolibarrApiAccess::$user);
413 $result = array();
414 foreach ($this->project->lines as $line) { // $line is a task
415 if ($includetimespent == 1) {
416 $timespent = $line->getSummaryOfTimeSpent(0);
417 }
418 if ($includetimespent == 2) {
419 $timespent = $line->fetchTimeSpentOnTask();
420 }
421 array_push($result, $this->_cleanObjectDatas($line));
422 }
423 return $result;
424 }
425
426
438 public function getRoles($id, $userid = 0)
439 {
440 global $db;
441
442 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
443 throw new RestException(403);
444 }
445
446 $result = $this->project->fetch($id);
447 if (!$result) {
448 throw new RestException(404, 'Project not found');
449 }
450
451 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
452 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
453 }
454
455 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
456 $taskstatic = new Task($this->db);
457 $userp = DolibarrApiAccess::$user;
458 if ($userid > 0) {
459 $userp = new User($this->db);
460 $userp->fetch($userid);
461 }
462 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, null, $id, 0);
463 $result = array();
464 foreach ($this->project->roles as $line) {
465 array_push($result, $this->_cleanObjectDatas($line));
466 }
467
468 return $result;
469 }
470
471
484 /*
485 public function postLine($id, $request_data = null)
486 {
487 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
488 throw new RestException(403);
489 }
490
491 $result = $this->project->fetch($id);
492 if( ! $result ) {
493 throw new RestException(404, 'Project not found');
494 }
495
496 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
497 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
498 }
499
500 $request_data = (object) $request_data;
501
502 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
503
504 $updateRes = $this->project->addline(
505 $request_data->desc,
506 $request_data->subprice,
507 $request_data->qty,
508 $request_data->tva_tx,
509 $request_data->localtax1_tx,
510 $request_data->localtax2_tx,
511 $request_data->fk_product,
512 $request_data->remise_percent,
513 $request_data->info_bits,
514 $request_data->fk_remise_except,
515 'HT',
516 0,
517 $request_data->date_start,
518 $request_data->date_end,
519 $request_data->product_type,
520 $request_data->rang,
521 $request_data->special_code,
522 $fk_parent_line,
523 $request_data->fk_fournprice,
524 $request_data->pa_ht,
525 $request_data->label,
526 $request_data->array_options,
527 $request_data->fk_unit,
528 $this->element,
529 $request_data->id
530 );
531
532 if ($updateRes > 0) {
533 return $updateRes;
534
535 }
536 return false;
537 }
538 */
539
553 /*
554 public function putLine($id, $lineid, $request_data = null)
555 {
556 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
557 throw new RestException(403);
558 }
559
560 $result = $this->project->fetch($id);
561 if( ! $result ) {
562 throw new RestException(404, 'Project not found');
563 }
564
565 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
566 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
567 }
568
569 $request_data = (object) $request_data;
570
571 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
572
573 $updateRes = $this->project->updateline(
574 $lineid,
575 $request_data->desc,
576 $request_data->subprice,
577 $request_data->qty,
578 $request_data->remise_percent,
579 $request_data->tva_tx,
580 $request_data->localtax1_tx,
581 $request_data->localtax2_tx,
582 'HT',
583 $request_data->info_bits,
584 $request_data->date_start,
585 $request_data->date_end,
586 $request_data->product_type,
587 $request_data->fk_parent_line,
588 0,
589 $request_data->fk_fournprice,
590 $request_data->pa_ht,
591 $request_data->label,
592 $request_data->special_code,
593 $request_data->array_options,
594 $request_data->fk_unit
595 );
596
597 if ($updateRes > 0) {
598 $result = $this->get($id);
599 unset($result->line);
600 return $this->_cleanObjectDatas($result);
601 }
602 return false;
603 }*/
604
605
606
618 public function put($id, $request_data = null)
619 {
620 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
621 throw new RestException(403);
622 }
623
624 $result = $this->project->fetch($id);
625 if ($result <= 0) {
626 throw new RestException(404, 'Project not found');
627 }
628
629 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
630 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
631 }
632 foreach ($request_data as $field => $value) {
633 if ($field == 'id') {
634 continue;
635 }
636 if ($field === 'caller') {
637 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
638 $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
639 continue;
640 }
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);
644 }
645 continue;
646 }
647
648 $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
649 }
650
651 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
652 return $this->get($id);
653 } else {
654 throw new RestException(500, $this->project->error);
655 }
656 }
657
667 public function delete($id)
668 {
669 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
670 throw new RestException(403);
671 }
672 $result = $this->project->fetch($id);
673 if (!$result) {
674 throw new RestException(404, 'Project not found');
675 }
676
677 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
678 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
679 }
680
681 if (!$this->project->delete(DolibarrApiAccess::$user)) {
682 throw new RestException(500, 'Error when delete project : '.$this->project->error);
683 }
684
685 return array(
686 'success' => array(
687 'code' => 200,
688 'message' => 'Project deleted'
689 )
690 );
691 }
692
715 public function validate($id, $notrigger = 0)
716 {
717 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
718 throw new RestException(403);
719 }
720 $result = $this->project->fetch($id);
721 if (!$result) {
722 throw new RestException(404, 'Project not found');
723 }
724
725 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
726 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
727 }
728
729 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
730 if ($result == 0) {
731 throw new RestException(304, 'Error nothing done. May be object is already validated');
732 }
733 if ($result < 0) {
734 throw new RestException(500, 'Error when validating Project: '.$this->project->error);
735 }
736
737 return array(
738 'success' => array(
739 'code' => 200,
740 'message' => 'Project validated'
741 )
742 );
743 }
744
745
746 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
753 protected function _cleanObjectDatas($object)
754 {
755 // phpcs:enable
756 $object = parent::_cleanObjectDatas($object);
757
758 unset($object->datec);
759 unset($object->datem);
760 unset($object->barcode_type);
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);
768 unset($object->fk_account);
769 unset($object->note);
770 unset($object->fk_incoterms);
771 unset($object->label_incoterms);
772 unset($object->location_incoterms);
773 unset($object->name);
774 unset($object->lastname);
775 unset($object->firstname);
776 unset($object->civility_id);
777 unset($object->mode_reglement_id);
778 unset($object->country);
779 unset($object->country_id);
780 unset($object->country_code);
781
782 unset($object->weekWorkLoad);
783 unset($object->weekWorkLoad);
784
785 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
786
787 unset($object->total_ht);
788 unset($object->total_tva);
789 unset($object->total_localtax1);
790 unset($object->total_localtax2);
791 unset($object->total_ttc);
792
793 unset($object->comments);
794
795 return $object;
796 }
797
805 private function _validate($data)
806 {
807 $object = array();
808 foreach (self::$FIELDS as $field) {
809 if (!isset($data[$field])) {
810 throw new RestException(400, "$field field missing");
811 }
812 $object[$field] = $data[$field];
813 }
814 return $object;
815 }
816
817
818 // TODO
819 // getSummaryOfTimeSpent
820}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class for API REST v1.
Definition api.class.php:30
_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.
Definition api.class.php:82
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 tasks.
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...
Definition member.php:79