dolibarr 24.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-2026 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
6 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2025 Jessica Kowal <jessicakowal69@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23use Luracast\Restler\RestException;
24
25require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
26require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php';
27
35class Projects extends DolibarrApi
36{
40 public static $FIELDS = array(
41 'ref',
42 'title'
43 );
44
48 public $project;
49
53 public $task;
54
55
59 public function __construct()
60 {
61 global $db, $conf;
62 $this->db = $db;
63 $this->project = new Project($this->db);
64 $this->task = new Task($this->db);
65 }
66
78 public function get($id)
79 {
80 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
81 throw new RestException(403);
82 }
83
84 $result = $this->project->fetch($id);
85 if (!$result) {
86 throw new RestException(404, 'Project with supplied id not found');
87 }
88
89 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
90 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
91 }
92
93 $this->project->fetchObjectLinked();
94 return $this->_cleanObjectDatas($this->project);
95 }
96
110 public function getByRef($ref)
111 {
112 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
113 throw new RestException(403);
114 }
115
116 $result = $this->project->fetch(0, $ref);
117 if (!$result) {
118 throw new RestException(404, 'Project with supplied ref not found');
119 }
120
121 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
122 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
123 }
124
125 $this->project->fetchObjectLinked();
126 return $this->_cleanObjectDatas($this->project);
127 }
128
142 public function getByRefExt($ref_ext)
143 {
144 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
145 throw new RestException(403);
146 }
147
148 $result = $this->project->fetch(0, '', $ref_ext);
149 if (!$result) {
150 throw new RestException(404, 'Project with supplied ref_ext not found');
151 }
152
153 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
154 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
155 }
156
157 $this->project->fetchObjectLinked();
158 return $this->_cleanObjectDatas($this->project);
159 }
160
174 public function getByMsgId($email_msgid)
175 {
176 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
177 throw new RestException(403);
178 }
179
180 $result = $this->project->fetch(0, '', '', $email_msgid);
181 if (!$result) {
182 throw new RestException(404, 'Project with supplied email_msgid not found');
183 }
184
185 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
186 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
187 }
188
189 $this->project->fetchObjectLinked();
190 return $this->_cleanObjectDatas($this->project);
191 }
192
212 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
213 {
214 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
215 throw new RestException(403);
216 }
217
218 $obj_ret = array();
219
220 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
221 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
222
223 // If the internal user must only see his customers, force searching by him
224 $search_sale = 0;
225 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
226 $search_sale = DolibarrApiAccess::$user->id;
227 }
228
229 $sql = "SELECT t.rowid";
230 $sql .= " FROM " . MAIN_DB_PREFIX . "projet as t";
231 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe AS s ON (s.rowid = t.fk_soc)";
232 $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
233 if ($category > 0) {
234 $sql .= ", " . MAIN_DB_PREFIX . "categorie_project as c";
235 }
236 $sql .= ' WHERE t.entity IN (' . getEntity('project') . ')';
237 if ($socids) {
238 $sql .= " AND t.fk_soc IN (" . $this->db->sanitize($socids) . ")";
239 }
240 // Search on sale representative
241 if ($search_sale && $search_sale != '-1') {
242 if ($search_sale == -2) {
243 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
244 } elseif ($search_sale > 0) {
245 $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) . ")";
246 }
247 }
248 // Select projects of given category
249 if ($category > 0) {
250 $sql .= " AND c.fk_categorie = " . ((int) $category) . " AND c.fk_project = t.rowid ";
251 }
252 // Add sql filters
253 if ($sqlfilters) {
254 $errormessage = '';
255 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
256 if ($errormessage) {
257 throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage);
258 }
259 }
260
261 //this query will return total orders with the filters given
262 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
263
264 $sql .= $this->db->order($sortfield, $sortorder);
265 if ($limit) {
266 if ($page < 0) {
267 $page = 0;
268 }
269 $offset = $limit * $page;
270
271 $sql .= $this->db->plimit($limit + 1, $offset);
272 }
273
274 dol_syslog("API Rest request");
275 $result = $this->db->query($sql);
276
277 if ($result) {
278 $num = $this->db->num_rows($result);
279 $min = min($num, ($limit <= 0 ? $num : $limit));
280 $i = 0;
281 while ($i < $min) {
282 $obj = $this->db->fetch_object($result);
283 $project_static = new Project($this->db);
284 if ($project_static->fetch($obj->rowid)) {
285 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($project_static), $properties);
286 }
287 $i++;
288 }
289 } else {
290 throw new RestException(503, 'Error when retrieve project list : ' . $this->db->lasterror());
291 }
292
293 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
294 if ($pagination_data) {
295 $totalsResult = $this->db->query($sqlTotals);
296 $total = $this->db->fetch_object($totalsResult)->total;
297
298 $tmp = $obj_ret;
299 $obj_ret = [];
300
301 $obj_ret['data'] = $tmp;
302 $obj_ret['pagination'] = [
303 'total' => (int) $total,
304 'page' => $page, //count starts from 0
305 'page_count' => ceil((int) $total / $limit),
306 'limit' => $limit
307 ];
308 }
309
310 return $obj_ret;
311 }
312
322 public function post($request_data = null)
323 {
324 global $conf;
325 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
326 throw new RestException(403, "Insufficiant rights");
327 }
328 // Check mandatory fields
329 $result = $this->_validate($request_data);
330
331 foreach ($request_data as $field => $value) {
332 if ($field === 'caller') {
333 // 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
334 $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
335 continue;
336 }
337
338 $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
339 }
340 /*if (isset($request_data["lines"])) {
341 $lines = array();
342 foreach ($request_data["lines"] as $line) {
343 array_push($lines, (object) $line);
344 }
345 $this->project->lines = $lines;
346 }*/
347
348 // Auto-generate the "ref" field if it is set to "auto"
349 if ($this->project->ref == -1 || $this->project->ref === 'auto') {
350 $reldir = '';
351 $defaultref = '';
352 $file = '';
353 $classname = '';
354 $filefound = 0;
355 $modele = getDolGlobalString('PROJECT_ADDON', 'mod_project_simple');
356
357 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
358 foreach ($dirmodels as $reldir) {
359 $file = dol_buildpath($reldir . "core/modules/project/" . $modele . '.php', 0);
360 if (file_exists($file)) {
361 $filefound = 1;
362 $classname = $modele;
363 break;
364 }
365 }
366 if ($filefound && !empty($classname)) {
367 $result = dol_include_once($reldir . "core/modules/project/" . $modele . '.php');
368 if ($result !== false && class_exists($classname)) {
369 $modProject = new $classname();
370 '@phan-var-force ModeleNumRefProjects $modProject';
371 $defaultref = $modProject->getNextValue(null, $this->project);
372 } else {
373 dol_syslog("Failed to include module file or invalid classname: " . $reldir . "core/modules/project/" . $modele . '.php', LOG_ERR);
374 }
375 } else {
376 dol_syslog("Module file not found or classname is empty: " . $modele, LOG_ERR);
377 }
378
379 if (is_numeric($defaultref) && $defaultref <= 0) {
380 $defaultref = '';
381 }
382
383 if (empty($defaultref)) {
384 $defaultref = 'PJ' . dol_print_date(dol_now(), 'dayrfc');
385 }
386
387 $this->project->ref = $defaultref;
388 }
389
390 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
391 throw new RestException(500, "Error creating project", array_merge(array($this->project->error), $this->project->errors));
392 }
393
394 return $this->project->id;
395 }
396
416 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
417 {
418 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
419 throw new RestException(403);
420 }
421
422 $result = $this->project->fetch($id);
423 if (!$result) {
424 throw new RestException(404, 'project not found');
425 }
426
427 if (!DolibarrApi::_checkAccessToResource('ficheinter', $this->project->id)) {
428 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
429 }
430
431 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
432 if ($result < 0) {
433 throw new RestException(500, 'Error : ' . $this->project->error);
434 }
435
436 return $this->_cleanObjectDatas($this->project);
437 }
438
454 public function deleteContact($id, $contactid, $type)
455 {
456 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
457 throw new RestException(403);
458 }
459
460 $result = $this->project->fetch($id);
461
462 if (!$result) {
463 throw new RestException(404, 'Project not found');
464 }
465
466 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
467 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
468 }
469 foreach (array('internal', 'external') as $source) {
470 $contacts = $this->project->liste_contact(-1, $source);
471
472 foreach ($contacts as $contact) {
473 if ($contact['id'] == $contactid && $contact['code'] == $type) {
474 $result = $this->project->delete_contact($contact['rowid']);
475 if (!$result) {
476 throw new RestException(500, 'Error when deleted the contact');
477 }
478 }
479 }
480 }
481 return $this->_cleanObjectDatas($this->project);
482 }
496 public function getLines($id, $includetimespent = 0)
497 {
498 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
499 throw new RestException(403);
500 }
501
502 $result = $this->project->fetch($id);
503 if (!$result) {
504 throw new RestException(404, 'Project not found');
505 }
506
507 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
508 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
509 }
510 $this->project->getLinesArray(DolibarrApiAccess::$user);
511 $result = array();
512 foreach ($this->project->lines as $line) { // $line is a task
513 if ($includetimespent == 1) {
514 $timespent = $line->getSummaryOfTimeSpent(0);
515 }
516 if ($includetimespent == 2) {
517 $timespent = $line->fetchTimeSpentOnTask();
518 }
519 array_push($result, $this->_cleanObjectDatas($line));
520 }
521 return $result;
522 }
523
524
537 public function getRoles($id, $userid = 0)
538 {
539 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
540 throw new RestException(403);
541 }
542
543 $result = $this->project->fetch($id);
544 if (!$result) {
545 throw new RestException(404, 'Project not found');
546 }
547
548 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
549 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
550 }
551
552 require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php';
553 $taskstatic = new Task($this->db);
554 $userp = DolibarrApiAccess::$user;
555 if ($userid > 0) {
556 $userp = new User($this->db);
557 $userp->fetch($userid);
558 }
559 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, null, (string) $id, 0);
560 $result = array();
561 foreach ($this->project->roles as $line) {
562 array_push($result, $this->_cleanObjectDatas($line));
563 }
564
565 return $result;
566 }
567
568
581 /*
582 public function postLine($id, $request_data = null)
583 {
584 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
585 throw new RestException(403);
586 }
587
588 $result = $this->project->fetch($id);
589 if( ! $result ) {
590 throw new RestException(404, 'Project not found');
591 }
592
593 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
594 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
595 }
596
597 $request_data = (object) $request_data;
598
599 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
600
601 $updateRes = $this->project->addline(
602 $request_data->desc,
603 $request_data->subprice,
604 $request_data->qty,
605 $request_data->tva_tx,
606 $request_data->localtax1_tx,
607 $request_data->localtax2_tx,
608 $request_data->fk_product,
609 $request_data->remise_percent,
610 $request_data->info_bits,
611 $request_data->fk_remise_except,
612 'HT',
613 0,
614 $request_data->date_start,
615 $request_data->date_end,
616 $request_data->product_type,
617 $request_data->rang,
618 $request_data->special_code,
619 $fk_parent_line,
620 $request_data->fk_fournprice,
621 $request_data->pa_ht,
622 $request_data->label,
623 $request_data->array_options,
624 $request_data->fk_unit,
625 $this->element,
626 $request_data->id
627 );
628
629 if ($updateRes > 0) {
630 return $updateRes;
631
632 }
633 return false;
634 }
635 */
636
650 /*
651 public function putLine($id, $lineid, $request_data = null)
652 {
653 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
654 throw new RestException(403);
655 }
656
657 $result = $this->project->fetch($id);
658 if( ! $result ) {
659 throw new RestException(404, 'Project not found');
660 }
661
662 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
663 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
664 }
665
666 $request_data = (object) $request_data;
667
668 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
669
670 $updateRes = $this->project->updateline(
671 $lineid,
672 $request_data->desc,
673 $request_data->subprice,
674 $request_data->qty,
675 $request_data->remise_percent,
676 $request_data->tva_tx,
677 $request_data->localtax1_tx,
678 $request_data->localtax2_tx,
679 'HT',
680 $request_data->info_bits,
681 $request_data->date_start,
682 $request_data->date_end,
683 $request_data->product_type,
684 $request_data->fk_parent_line,
685 0,
686 $request_data->fk_fournprice,
687 $request_data->pa_ht,
688 $request_data->label,
689 $request_data->special_code,
690 $request_data->array_options,
691 $request_data->fk_unit
692 );
693
694 if ($updateRes > 0) {
695 $result = $this->get($id);
696 unset($result->line);
697 return $this->_cleanObjectDatas($result);
698 }
699 return false;
700 }*/
701
702
703
716 public function put($id, $request_data = null)
717 {
718 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
719 throw new RestException(403);
720 }
721
722 $result = $this->project->fetch($id);
723 if ($result <= 0) {
724 throw new RestException(404, 'Project not found');
725 }
726
727 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
728 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
729 }
730 foreach ($request_data as $field => $value) {
731 if ($field == 'id') {
732 continue;
733 }
734 if ($field === 'caller') {
735 // 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
736 $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
737 continue;
738 }
739 if ($field == 'array_options' && is_array($value)) {
740 foreach ($value as $index => $val) {
741 $this->project->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->project);
742 }
743 continue;
744 }
745
746 $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
747 }
748
749 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
750 return $this->get($id);
751 } else {
752 throw new RestException(500, $this->project->error);
753 }
754 }
755
766 public function delete($id)
767 {
768 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
769 throw new RestException(403);
770 }
771 $result = $this->project->fetch($id);
772 if (!$result) {
773 throw new RestException(404, 'Project not found');
774 }
775
776 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
777 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
778 }
779
780 if (!$this->project->delete(DolibarrApiAccess::$user)) {
781 throw new RestException(500, 'Error when delete project : ' . $this->project->error);
782 }
783
784 return array(
785 'success' => array(
786 'code' => 200,
787 'message' => 'Project deleted'
788 )
789 );
790 }
791
815 public function validate($id, $notrigger = 0)
816 {
817 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
818 throw new RestException(403);
819 }
820 $result = $this->project->fetch($id);
821 if (!$result) {
822 throw new RestException(404, 'Project not found');
823 }
824
825 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
826 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
827 }
828
829 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
830 if ($result == 0) {
831 throw new RestException(304, 'Error nothing done. May be object is already validated');
832 }
833 if ($result < 0) {
834 throw new RestException(500, 'Error when validating Project: ' . $this->project->error);
835 }
836
837 return array(
838 'success' => array(
839 'code' => 200,
840 'message' => 'Project validated'
841 )
842 );
843 }
844
863 public function listTimespent($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
864 {
865 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
866 throw new RestException(403);
867 }
868
869 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
870 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
871
872 // If the internal user must only see his customers, force searching by him
873 $search_sale = 0;
874 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
875 $search_sale = DolibarrApiAccess::$user->id;
876 }
877
878 $sql = "SELECT et.rowid, et.element_duration, et.element_datehour, et.fk_user, et.note as time_note, et.thm,";
879 $sql .= " u.login as user_login, u.firstname as user_firstname, u.lastname as user_lastname,";
880 $sql .= " p.rowid as project_id, p.ref as project_ref, p.title as project_title,";
881 $sql .= " t.rowid as task_id, t.ref as task_ref, t.label as task_label,";
882 $sql .= " s.rowid as soc_id, s.nom as soc_name";
883 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
884 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."projet_task as t ON (t.fk_projet = p.rowid)";
885 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."element_time as et ON (et.fk_element = t.rowid AND et.elementtype = 'task')";
886 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe AS s ON (s.rowid = p.fk_soc)";
887 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user AS u ON (u.rowid = et.fk_user)";
888 $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
889 if ($socids) {
890 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
891 }
892
893 // Search on sale representative
894 if ($search_sale && $search_sale != '-1') {
895 if ($search_sale == -2) {
896 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
897 } elseif ($search_sale > 0) {
898 $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).")";
899 }
900 }
901 // Select projects of given category
902 if ($category > 0) {
903 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_project = t.rowid ";
904 }
905
906 // Add sql filters
907 if ($sqlfilters) {
908 $errormessage = '';
909 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
910 if ($errormessage) {
911 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
912 }
913 }
914
915 //this query will return total orders with the filters given
916 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
917
918 $sql .= $this->db->order($sortfield, $sortorder);
919 if ($limit) {
920 if ($page < 0) {
921 $page = 0;
922 }
923 $offset = $limit * $page;
924
925 $sql .= $this->db->plimit($limit + 1, $offset);
926 }
927
928 dol_syslog("API Rest request");
929 $result = $this->db->query($sql);
930 $obj_ret = array();
931 if ($result) {
932 $num = $this->db->num_rows($result);
933 $min = min($num, ($limit <= 0 ? $num : $limit));
934 $i = 0;
935 while ($i < $min) {
936 $obj = $this->db->fetch_object($result);
937 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($obj), $properties);
938 $i++;
939 }
940 } else {
941 throw new RestException(503, 'Error when retrieve timestamp list : '.$this->db->lasterror());
942 }
943
944
945 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
946 if ($pagination_data) {
947 $totalsResult = $this->db->query($sqlTotals);
948 $total = $this->db->fetch_object($totalsResult)->total;
949
950 $tmp = $obj_ret;
951 $obj_ret['data'] = $tmp;
952 $obj_ret['pagination'] = [
953 'total' => (int) $total,
954 'page' => $page, //count starts from 0
955 'page_count' => ceil((int) $total / $limit),
956 'limit' => $limit
957 ];
958 }
959
960 return $obj_ret;
961 }
962
963 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
973 protected function _cleanObjectDatas($object)
974 {
975 // phpcs:enable
976 $object = parent::_cleanObjectDatas($object);
977
978 unset($object->datec);
979 unset($object->datem);
980 unset($object->barcode_type);
981 unset($object->barcode_type_code);
982 unset($object->barcode_type_label);
983 unset($object->barcode_type_coder);
984 unset($object->cond_reglement_id);
985 unset($object->cond_reglement);
986 unset($object->fk_delivery_address);
987 unset($object->shipping_method_id);
988 unset($object->fk_account);
989 unset($object->note);
990 unset($object->fk_incoterms);
991 unset($object->label_incoterms);
992 unset($object->location_incoterms);
993 unset($object->name);
994 unset($object->lastname);
995 unset($object->firstname);
996 unset($object->civility_id);
997 unset($object->mode_reglement_id);
998 unset($object->country);
999 unset($object->country_id);
1000 unset($object->country_code);
1001
1002 unset($object->weekWorkLoad);
1003 unset($object->weekWorkLoad);
1004
1005 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
1006
1007 unset($object->total_ht);
1008 unset($object->total_tva);
1009 unset($object->total_localtax1);
1010 unset($object->total_localtax2);
1011 unset($object->total_ttc);
1012
1013 unset($object->comments);
1014
1015 return $object;
1016 }
1017
1025 private function _validate($data)
1026 {
1027 $object = array();
1028 foreach (self::$FIELDS as $field) {
1029 if (!isset($data[$field])) {
1030 throw new RestException(400, "$field field missing");
1031 }
1032 $object[$field] = $data[$field];
1033 }
1034 return $object;
1035 }
1036
1037
1038
1039
1054 public function getContacts($id, $type = '')
1055 {
1056 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
1057 throw new RestException(403);
1058 }
1059
1060 $result = $this->project->fetch($id);
1061 if (!$result) {
1062 throw new RestException(404, 'Project not found');
1063 }
1064
1065 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
1066 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1067 }
1068
1069 $contacts = $this->project->liste_contact(-1, 'external', 0, $type);
1070 $socpeoples = $this->project->liste_contact(-1, 'internal', 0, $type);
1071
1072 $contacts = array_merge($contacts, $socpeoples);
1073
1074 return $contacts;
1075 }
1076
1097 public function addToContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0, $affect_to_tasks = null)
1098 {
1099 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1100 throw new RestException(403);
1101 }
1102
1103 $result = $this->project->fetch($id);
1104 if (!$result) {
1105 throw new RestException(404, 'Project not found');
1106 }
1107
1108 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
1109 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1110 }
1111
1112 // Ajouter le contact au projet
1113 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1114 if ($result <= 0) {
1115 throw new RestException(500, 'Error : ' . $this->project->error . 'result :' . $result);
1116 }
1117
1118 // Si demandé, ajouter le contact aux tâches
1119 if ($affect_to_tasks !== null) {
1120 $this->project->getLinesArray(DolibarrApiAccess::$user);
1121
1122 foreach ($this->project->lines as $task) {
1123 // Si $affect_to_tasks est vide, on affecte à toutes les tâches
1124 // Sinon, on vérifie si la tâche est dans la liste
1125 if (empty($affect_to_tasks) || in_array($task->id, $affect_to_tasks)) {
1126 $task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1127 }
1128 }
1129 }
1130
1131 $result = $this->project->fetch($id);
1132 if (!$result) {
1133 throw new RestException(404, 'Project not found');
1134 }
1135
1136 return $this->_cleanObjectDatas($this->project);
1137 }
1153 public function deleteToContact($id, $contactid, $type)
1154 {
1155 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1156 throw new RestException(403);
1157 }
1158
1159 $result = $this->project->fetch($id);
1160 if (!$result) {
1161 throw new RestException(404, 'Project not found');
1162 }
1163
1164 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
1165 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1166 }
1167
1168 foreach (array('internal', 'external') as $source) {
1169 $contacts = $this->project->liste_contact(-1, $source);
1170
1171 foreach ($contacts as $contact) {
1172 if ($contact['id'] == $contactid && $contact['code'] == $type) {
1173 $result = $this->project->delete_contact($contact['rowid']);
1174 if (!$result) {
1175 throw new RestException(500, 'Error when deleted the contact');
1176 }
1177 break 2;
1178 }
1179 }
1180 }
1181
1182 return $this->_cleanObjectDatas($this->project);
1183 }
1184
1196 public function getTimespent($id)
1197 {
1198 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
1199 throw new RestException(403);
1200 }
1201
1202 $result = $this->project->fetch($id);
1203 if (!$result) {
1204 throw new RestException(404, 'Project not found');
1205 }
1206
1207 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
1208 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1209 }
1210
1211
1212 $this->project->getLinesArray(DolibarrApiAccess::$user);
1213 $allTimespent = array();
1214 foreach ($this->project->lines as $task) {
1215 $allTimespent[] = $task->getSummaryOfTimeSpent();
1216 //var_dump($taskTimespent);
1217 //foreach ($taskTimespent as $time) {
1218 // $this->_cleanObjectDatas($time);
1219
1220 //}
1221 }
1222
1223 return $allTimespent;
1224 }
1225 // TODO
1226 // getSummaryOfTimeSpent
1227}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_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.
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.
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 tasks.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
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.