dolibarr 25.0.0-alpha
api_expensereports.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) 2020-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2025 William Mead <william@m34d.com>
7 * Copyright (C) 2025 Kowal Jessica <jessicakowal69@gmail.com>
8 * Copyright (C) 2026 Charlene Benke <charlene@patas-monkey.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24use Luracast\Restler\RestException;
25
26require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
27require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
29
39{
43 public static $FIELDS = array(
44 'fk_user_author',
45 'date_debut',
46 'date_fin',
47 );
48
52 public static $FIELDSLINE = array(
53 'date',
54 'fk_c_type_fees',
55 'qty',
56 'value_unit',
57 'vatrate'
58 );
59
63 public static $FIELDSPAYMENT = array(
64 "fk_typepayment",
65 'datep',
66 'amounts',
67 );
68
72 public $expensereport;
73
74
78 public function __construct()
79 {
80 global $db;
81
82 $this->db = $db;
83 $this->expensereport = new ExpenseReport($this->db);
84 }
85
98 public function get($id)
99 {
100 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
101 throw new RestException(403);
102 }
103
104 $result = $this->expensereport->fetch($id);
105 if (!$result) {
106 throw new RestException(404, 'Expense report not found');
107 }
108
109 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
110 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
111 }
112
113 $this->expensereport->fetchObjectLinked();
114 return $this->_cleanObjectDatas($this->expensereport);
115 }
116
138 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false)
139 {
140 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
141 throw new RestException(403);
142 }
143
144 $obj_ret = array();
145
146 // case of external user, $societe param is ignored and replaced by user's socid
147 //$socid = DolibarrApiAccess::$user->socid ?: $societe;
148
149 $sql = "SELECT t.rowid";
150 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport AS t LEFT JOIN ".MAIN_DB_PREFIX."expensereport_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
151 $sql .= ' WHERE t.entity IN ('.getEntity('expensereport').')';
152 if ($user_ids) {
153 $sql .= " AND t.fk_user_author IN (".$this->db->sanitize($user_ids).")";
154 }
155
156 // Add sql filters
157 if ($sqlfilters) {
158 $errormessage = '';
159 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
160 if ($errormessage) {
161 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
162 }
163 }
164
165 //this query will return total orders with the filters given
166 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
167
168 $sql .= $this->db->order($sortfield, $sortorder);
169 if ($limit) {
170 if ($page < 0) {
171 $page = 0;
172 }
173 $offset = $limit * $page;
174
175 $sql .= $this->db->plimit($limit + 1, $offset);
176 }
177
178 $result = $this->db->query($sql);
179
180 if ($result) {
181 $num = $this->db->num_rows($result);
182 $min = min($num, ($limit <= 0 ? $num : $limit));
183 $i = 0;
184 while ($i < $min) {
185 $obj = $this->db->fetch_object($result);
186 $expensereport_static = new ExpenseReport($this->db);
187 if ($expensereport_static->fetch($obj->rowid)) {
188 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($expensereport_static), $properties);
189 }
190 $i++;
191 }
192 } else {
193 throw new RestException(503, 'Error when retrieve Expense Report list : '.$this->db->lasterror());
194 }
195
196 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
197 if ($pagination_data) {
198 $totalsResult = $this->db->query($sqlTotals);
199 $total = $this->db->fetch_object($totalsResult)->total;
200
201 $tmp = $obj_ret;
202 $obj_ret = [];
203
204 $obj_ret['data'] = $tmp;
205 $obj_ret['pagination'] = [
206 'total' => (int) $total,
207 'page' => $page, //count starts from 0
208 'page_count' => ceil((int) $total / $limit),
209 'limit' => $limit
210 ];
211 }
212
213 return $obj_ret;
214 }
215
228 public function post($request_data = null)
229 {
230 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
231 throw new RestException(403, "Insufficiant rights");
232 }
233
234 // Check mandatory fields
235 $result = $this->_validate($request_data);
236
237 foreach ($request_data as $field => $value) {
238 if ($field === 'caller') {
239 // 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
240 $this->expensereport->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
241 continue;
242 }
243
244 if ($field == 'array_options' && is_array($value)) {
245 foreach ($value as $index => $val) {
246 $this->expensereport->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->expensereport);
247 }
248 continue;
249 }
250
251 if (!in_array($field, array('fk_statut', 'fk_user_approve'))) { // Exclude properties that must be set by other workflow methods
252 $this->expensereport->$field = $this->_checkValForAPI($field, $value, $this->expensereport);
253 }
254 }
255 /*if (isset($request_data["lines"])) {
256 $lines = array();
257 foreach ($request_data["lines"] as $line) {
258 array_push($lines, (object) $line);
259 }
260 $this->expensereport->lines = $lines;
261 }*/
262 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
263 throw new RestException(500, "Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
264 }
265
266 return $this->expensereport->id;
267 }
268
285 public function getLines($id)
286 {
287 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
288 throw new RestException(403);
289 }
290
291 $result = $this->expensereport->fetch($id);
292 if (!$result) {
293 throw new RestException(404, 'Expense report not found');
294 }
295
296 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
297 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
298 }
299 $this->expensereport->fetch_lines();
300 $result = array();
301 foreach ($this->expensereport->lines as $line) {
302 $result[] = $this->_cleanObjectDatas($line);
303 }
304 return $result;
305 }
306
323 public function postLine($id, $request_data = null)
324 {
325 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
326 throw new RestException(403);
327 }
328
329 $result = $this->_validateLine($request_data);
330
331 $result = $this->expensereport->fetch($id);
332 if (!$result) {
333 throw new RestException(404, 'Expense report not found');
334 }
335
336 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
337 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
338 }
339
340 if ($this->expensereport->status != ExpenseReport::STATUS_DRAFT) {
341 throw new RestException(403, 'Expense report must be in draft status to add lines');
342 }
343
344 $request_data = (object) $request_data;
345
346 $request_data->comments = sanitizeVal($request_data->comments, 'restricthtml');
347
348 $result = $this->expensereport->addline(
349 $request_data->qty,
350 $request_data->value_unit,
351 (int) $request_data->fk_c_type_fees,
352 $request_data->vatrate,
353 $request_data->date,
354 $request_data->comments,
355 $request_data->fk_project,
356 (int) $request_data->fk_c_exp_tax_cat,
357 $request_data->type,
358 $request_data->fk_ecm_files
359 );
360
361 if ($result > 0) {
362 return $result;
363 } else {
364 throw new RestException(500, 'Error adding line to expense report: '.$this->expensereport->error);
365 }
366 }
367
387 public function putLine($id, $lineid, $request_data = null)
388 {
389 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
390 throw new RestException(403);
391 }
392
393 $result = $this->expensereport->fetch($id);
394 if (!$result) {
395 throw new RestException(404, 'Expense report not found');
396 }
397
398 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
399 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
400 }
401
402 if ($this->expensereport->status != ExpenseReport::STATUS_DRAFT) {
403 throw new RestException(403, 'Expense report must be in draft status to update lines');
404 }
405
406 $line = new ExpenseReportLine($this->db);
407 $result = $line->fetch($lineid);
408 if ($result <= 0) {
409 throw new RestException(404, 'Expense report line not found');
410 }
411
412 $request_data = (object) $request_data;
413
414 $request_data->comments = sanitizeVal($request_data->comments, 'restricthtml');
415
416 $updateRes = $this->expensereport->updateline(
417 $lineid,
418 (int) $request_data->fk_c_type_fees,
419 $request_data->fk_project,
420 $request_data->vatrate,
421 $request_data->comments,
422 $request_data->qty,
423 $request_data->value_unit,
424 $request_data->date,
425 $id,
426 (int) $request_data->fk_c_exp_tax_cat,
427 $request_data->fk_ecm_files
428 );
429
430 if ($updateRes > 0) {
431 $result = $this->get($id);
432 unset($result->line);
433 return $this->_cleanObjectDatas($result);
434 } else {
435 throw new RestException(500, 'Error updating line: '.$this->expensereport->error);
436 }
437 }
438
455 public function deleteLine($id, $lineid)
456 {
457 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
458 throw new RestException(403);
459 }
460
461 $result = $this->expensereport->fetch($id);
462 if (!$result) {
463 throw new RestException(404, 'Expense report not found');
464 }
465
466 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
467 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
468 }
469
470 // Check if line exists
471 $lineExists = false;
472 $this->expensereport->fetch_lines();
473 foreach ($this->expensereport->lines as $line) {
474 if ($line->id == $lineid) {
475 $lineExists = true;
476 break;
477 }
478 }
479
480 if (!$lineExists) {
481 throw new RestException(404, 'Line not found');
482 }
483
484 if ($this->expensereport->status != ExpenseReport::STATUS_DRAFT) {
485 throw new RestException(403, 'Expense report must be in draft status to delete lines');
486 }
487
488 $result = $this->expensereport->deleteLine($lineid);
489 if ($result > 0) {
490 return $this->get($id);
491 } else {
492 throw new RestException(500, 'Error deleting line: '.$this->expensereport->error);
493 }
494 }
495
513 public function put($id, $request_data = null)
514 {
515 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
516 throw new RestException(403);
517 }
518
519 $result = $this->expensereport->fetch($id);
520 if (!$result) {
521 throw new RestException(404, 'Expense report not found');
522 }
523
524 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
525 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
526 }
527 foreach ($request_data as $field => $value) {
528 if ($field == 'id') {
529 continue;
530 }
531 if ($field === 'caller') {
532 // 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
533 $this->expensereport->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
534 continue;
535 }
536
537 if ($field == 'array_options' && is_array($value)) {
538 foreach ($value as $index => $val) {
539 $this->expensereport->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->expensereport);
540 }
541 continue;
542 }
543
544 if (!in_array($field, array('fk_statut', 'fk_user_approve'))) { // Exclude properties that must be set by other workflow methods
545 $this->expensereport->$field = $this->_checkValForAPI($field, $value, $this->expensereport);
546 }
547 }
548
549 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
550 return $this->get($id);
551 } else {
552 throw new RestException(500, $this->expensereport->error);
553 }
554 }
555
568 public function delete($id)
569 {
570 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'supprimer')) {
571 throw new RestException(403);
572 }
573
574 $result = $this->expensereport->fetch($id);
575 if (!$result) {
576 throw new RestException(404, 'Expense report not found');
577 }
578
579 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
580 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
581 }
582
583 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
584 throw new RestException(500, 'Error when delete Expense Report : '.$this->expensereport->error);
585 }
586
587 return array(
588 'success' => array(
589 'code' => 200,
590 'message' => 'Expense Report deleted'
591 )
592 );
593 }
594
610 public function setToDraft($id)
611 {
612 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
613 throw new RestException(403, "Insufficiant rights");
614 }
615 $result = $this->expensereport->fetch($id);
616 if (!$result) {
617 throw new RestException(404, 'Expense report not found');
618 }
619
620 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
621 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
622 }
623
624 $result = $this->expensereport->setStatut(ExpenseReport::STATUS_DRAFT);
625 if ($result == 0) {
626 throw new RestException(304, 'Error nothing done. May be object is already draft');
627 }
628 if ($result < 0) {
629 throw new RestException(500, 'Error when setting to draft expense report: '.$this->expensereport->error);
630 }
631
632 return $this->_cleanObjectDatas($this->expensereport);
633 }
634
654 public function validate($id, $notrigger = 0)
655 {
656 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
657 throw new RestException(403, "Insufficiant rights");
658 }
659 $result = $this->expensereport->fetch($id);
660 if (!$result) {
661 throw new RestException(404, 'Expense report not found');
662 }
663
664 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
665 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
666 }
667
668 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
669 if ($result == 0) {
670 throw new RestException(304, 'Error nothing done. May be object is already validated');
671 }
672 if ($result < 0) {
673 throw new RestException(500, 'Error when validating expense report: '.$this->expensereport->error);
674 }
675
676 return $this->_cleanObjectDatas($this->expensereport);
677 }
678
679
699 public function approve($id, $notrigger = 0)
700 {
701 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'approve')) {
702 throw new RestException(403, "Insufficiant rights");
703 }
704 $result = $this->expensereport->fetch($id);
705 if (!$result) {
706 throw new RestException(404, 'Expense report not found');
707 }
708
709 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
710 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
711 }
712
713 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
714 if ($result == 0) {
715 throw new RestException(304, 'Error nothing done. May be object is already approved');
716 }
717 if ($result < 0) {
718 throw new RestException(500, 'Error when approving expense report: '.$this->expensereport->error);
719 }
720
721 return $this->_cleanObjectDatas($this->expensereport);
722 }
723
724
745 public function deny($id, $details, $notrigger = 0)
746 {
747 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'approve')) {
748 throw new RestException(403, "Insufficiant rights");
749 }
750 $result = $this->expensereport->fetch($id);
751 if (!$result) {
752 throw new RestException(404, 'Expense report not found');
753 }
754
755 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
756 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
757 }
758
759 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
760 if ($result == 0) {
761 throw new RestException(304, 'Error nothing done. May be object is already denied');
762 }
763 if ($result < 0) {
764 throw new RestException(500, 'Error when denying expense report: '.$this->expensereport->error);
765 }
766
767
768
769 return $this->_cleanObjectDatas($this->expensereport);
770 }
771
791 public function setPaid($id, $notrigger = 0)
792 {
793 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'to_paid')) {
794 throw new RestException(403, "Insufficiant rights");
795 }
796 $result = $this->expensereport->fetch($id);
797 if (!$result) {
798 throw new RestException(404, 'Expense report not found');
799 }
800
801 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
802 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
803 }
804
805 $result = $this->expensereport->setPaid($id, DolibarrApiAccess::$user, $notrigger);
806 if ($result == 0) {
807 throw new RestException(304, 'Error nothing done. May be object is already approved');
808 }
809 if ($result < 0) {
810 throw new RestException(500, 'Error when approving expense report: '.$this->expensereport->error);
811 }
812
813 return $this->_cleanObjectDatas($this->expensereport);
814 }
815
833 public function cancel($id, $detail, $notrigger = 0)
834 {
835 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
836 throw new RestException(403, "Insufficiant rights");
837 }
838 $result = $this->expensereport->fetch($id);
839 if (!$result) {
840 throw new RestException(404, 'Expense report not found');
841 }
842
843 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
844 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
845 }
846
847 if ($this->expensereport->status == ExpenseReport::STATUS_CANCELED) {
848 throw new RestException(403, 'Expense report already canceled');
849 }
850 $result = $this->expensereport->set_cancel(DolibarrApiAccess::$user, $detail, $notrigger);
851 if ($result < 0) {
852 throw new RestException(500, 'Error when cancelling expense report: '.$this->expensereport->error);
853 }
854
855 $result = $this->expensereport->fetch($id);
856 return $this->_cleanObjectDatas($this->expensereport);
857 }
858
876 public function getAllPayments($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
877 {
878 $list = array();
879
880 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
881 throw new RestException(403);
882 }
883
884 $sql = "SELECT t.rowid FROM " . MAIN_DB_PREFIX . "payment_expensereport as t, ".MAIN_DB_PREFIX."expensereport as e";
885 $sql .= " WHERE e.rowid = t.fk_expensereport";
886 $sql .= ' AND e.entity IN ('.getEntity('expensereport').')';
887
888 $sql .= $this->db->order($sortfield, $sortorder);
889 if ($limit) {
890 if ($page < 0) {
891 $page = 0;
892 }
893 $offset = $limit * $page;
894
895 $sql .= $this->db->plimit($limit + 1, $offset);
896 }
897
898 dol_syslog("API Rest request");
899 $result = $this->db->query($sql);
900
901 if ($result) {
902 $num = $this->db->num_rows($result);
903 $min = min($num, ($limit <= 0 ? $num : $limit));
904 for ($i = 0; $i < $min; $i++) {
905 $obj = $this->db->fetch_object($result);
906 $paymentExpenseReport = new PaymentExpenseReport($this->db);
907 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
908 $list[] = $this->_cleanObjectDatas($paymentExpenseReport);
909 }
910 }
911 } else {
912 throw new RestException(503, 'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
913 }
914
915 return $list;
916 }
917
930 public function getPayments($pid)
931 {
932 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
933 throw new RestException(403);
934 }
935
936 $paymentExpenseReport = new PaymentExpenseReport($this->db);
937 $result = $paymentExpenseReport->fetch($pid);
938 if (!$result) {
939 throw new RestException(404, 'paymentExpenseReport not found');
940 }
941
942 return $this->_cleanObjectDatas($paymentExpenseReport);
943 }
944
959 public function addPayment($id, $request_data = null)
960 {
961 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
962 throw new RestException(403);
963 }
964 // Check mandatory fields
965 $result = $this->_validatepayment($request_data);
966
967 $paymentExpenseReport = new PaymentExpenseReport($this->db);
968 $paymentExpenseReport->fk_expensereport = $id;
969 foreach ($request_data as $field => $value) {
970 $paymentExpenseReport->$field = $this->_checkValForAPI($field, $value, $paymentExpenseReport);
971 }
972
973 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
974 throw new RestException(500, 'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
975 }
976 if (isModEnabled("bank")) {
977 $paymentExpenseReport->addPaymentToBank(
978 DolibarrApiAccess::$user,
979 'payment_expensereport',
980 '(ExpenseReportPayment)',
981 (int) $request_data['accountid'],
982 '',
983 ''
984 );
985 }
986
987 return $paymentExpenseReport->id;
988 }
989
1004 public function updatePayment($id, $request_data = null)
1005 {
1006 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
1007 throw new RestException(403);
1008 }
1009
1010 $paymentExpenseReport = new PaymentExpenseReport($this->db);
1011 $result = $paymentExpenseReport->fetch($id);
1012 if (!$result) {
1013 throw new RestException(404, 'payment of expense report not found');
1014 }
1015
1016 foreach ($request_data as $field => $value) {
1017 if ($field == 'id') {
1018 continue;
1019 }
1020 $paymentExpenseReport->$field = $this->_checkValForAPI($field, $value, $paymentExpenseReport);
1021 }
1022
1023 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
1024 return $this->get($id);
1025 } else {
1026 throw new RestException(500, $paymentExpenseReport->error);
1027 }
1028 }
1029
1038 /*public function delete($id)
1039 {
1040 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer') {
1041 throw new RestException(403);
1042 }
1043 $paymentExpenseReport = new PaymentExpenseReport($this->db);
1044 $result = $paymentExpenseReport->fetch($id);
1045 if (!$result) {
1046 throw new RestException(404, 'paymentExpenseReport not found');
1047 }
1048
1049 if ($paymentExpenseReport->delete(DolibarrApiAccess::$user) < 0) {
1050 throw new RestException(403, 'error when deleting paymentExpenseReport');
1051 }
1052
1053 return array(
1054 'success' => array(
1055 'code' => 200,
1056 'message' => 'paymentExpenseReport deleted'
1057 )
1058 );
1059 }*/
1060
1061
1062
1063 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1073 protected function _cleanObjectDatas($object)
1074 {
1075 // phpcs:enable
1076 $object = parent::_cleanObjectDatas($object);
1077
1078 unset($object->fk_statut);
1079 unset($object->statut);
1080 unset($object->user);
1081 unset($object->thirdparty);
1082
1083 unset($object->cond_reglement);
1084 unset($object->shipping_method_id);
1085
1086 unset($object->barcode_type);
1087 unset($object->barcode_type_code);
1088 unset($object->barcode_type_label);
1089 unset($object->barcode_type_coder);
1090
1091 unset($object->code_paiement);
1092 unset($object->code_statut);
1093 unset($object->fk_c_paiement);
1094 unset($object->fk_incoterms);
1095 unset($object->label_incoterms);
1096 unset($object->location_incoterms);
1097 unset($object->mode_reglement_id);
1098 unset($object->cond_reglement_id);
1099
1100 unset($object->name);
1101 unset($object->lastname);
1102 unset($object->firstname);
1103 unset($object->civility_id);
1104 unset($object->cond_reglement_id);
1105 unset($object->contact);
1106 unset($object->contact_id);
1107
1108 unset($object->state);
1109 unset($object->state_id);
1110 unset($object->state_code);
1111 unset($object->country);
1112 unset($object->country_id);
1113 unset($object->country_code);
1114
1115 unset($object->note); // We already use note_public and note_pricate
1116
1117 return $object;
1118 }
1119
1127 private function _validate($data)
1128 {
1129 if ($data === null) {
1130 $data = array();
1131 }
1132 $expensereport = array();
1133 foreach (ExpenseReports::$FIELDS as $field) {
1134 if (!isset($data[$field])) {
1135 throw new RestException(400, "$field field missing");
1136 }
1137 $expensereport[$field] = $data[$field];
1138 }
1139 return $expensereport;
1140 }
1141
1149 private function _validatepayment($data)
1150 {
1151 if ($data === null) {
1152 $data = array();
1153 }
1154 $expensereport = array();
1155 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
1156 if (!isset($data[$field])) {
1157 throw new RestException(400, "$field field missing");
1158 }
1159 $expensereport[$field] = $data[$field];
1160 }
1161 return $expensereport;
1162 }
1163
1172 private function _validateLine($data)
1173 {
1174 if ($data === null) {
1175 $data = array();
1176 }
1177 $expenseReport = array();
1178 foreach (ExpenseReports::$FIELDSLINE as $field) {
1179 if (!isset($data[$field])) {
1180 throw new RestException(400, "$field field missing");
1181 }
1182 $expenseReport[$field] = $data[$field];
1183 }
1184 return $expenseReport;
1185 }
1186}
$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.
_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 Trips and Expenses.
const STATUS_DRAFT
Draft status.
const STATUS_CANCELED
Classified canceled.
Class of expense report details lines.
getPayments($pid)
Get an expense report payment.
deny($id, $details, $notrigger=0)
Deny an expense report.
_cleanObjectDatas($object)
Delete paymentExpenseReport.
_validate($data)
Validate fields before create or update object.
setPaid($id, $notrigger=0)
Set to paid an expense report.
validate($id, $notrigger=0)
Validate an expense report.
deleteLine($id, $lineid)
Delete a line from an expense report.
getLines($id)
Get lines of an expense report.
updatePayment($id, $request_data=null)
Update a payment of an expense report.
approve($id, $notrigger=0)
Approve an expense report.
put($id, $request_data=null)
Update expense report general fields.
cancel($id, $detail, $notrigger=0)
Cancel an expense report.
addPayment($id, $request_data=null)
Create a payment for an expense report.
getAllPayments($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0)
Get the list of payments of an expense report.
post($request_data=null)
Create an expense report.
_validatepayment($data)
Validate fields before create or update object.
_validateLine($data)
Validate fields before create or update object.
setToDraft($id)
Set an expense report to draft.
putLine($id, $lineid, $request_data=null)
Update a line of an expense report.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List expense reports.
postLine($id, $request_data=null)
Add a line to an expense report.
Class to manage payments of expense report.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.