dolibarr 24.0.0-beta
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 $this->expensereport->$field = $this->_checkValForAPI($field, $value, $this->expensereport);
245 }
246 /*if (isset($request_data["lines"])) {
247 $lines = array();
248 foreach ($request_data["lines"] as $line) {
249 array_push($lines, (object) $line);
250 }
251 $this->expensereport->lines = $lines;
252 }*/
253 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
254 throw new RestException(500, "Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
255 }
256
257 return $this->expensereport->id;
258 }
259
276 public function getLines($id)
277 {
278 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
279 throw new RestException(403);
280 }
281
282 $result = $this->expensereport->fetch($id);
283 if (!$result) {
284 throw new RestException(404, 'Expense report not found');
285 }
286
287 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
288 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
289 }
290 $this->expensereport->fetch_lines();
291 $result = array();
292 foreach ($this->expensereport->lines as $line) {
293 $result[] = $this->_cleanObjectDatas($line);
294 }
295 return $result;
296 }
297
314 public function postLine($id, $request_data = null)
315 {
316 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
317 throw new RestException(403);
318 }
319
320 $result = $this->_validateLine($request_data);
321
322 $result = $this->expensereport->fetch($id);
323 if (!$result) {
324 throw new RestException(404, 'Expense report not found');
325 }
326
327 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
328 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
329 }
330
331 if ($this->expensereport->status != ExpenseReport::STATUS_DRAFT) {
332 throw new RestException(403, 'Expense report must be in draft status to add lines');
333 }
334
335 $request_data = (object) $request_data;
336
337 $request_data->comments = sanitizeVal($request_data->comments, 'restricthtml');
338
339 $result = $this->expensereport->addline(
340 $request_data->qty,
341 $request_data->value_unit,
342 (int) $request_data->fk_c_type_fees,
343 $request_data->vatrate,
344 $request_data->date,
345 $request_data->comments,
346 $request_data->fk_project,
347 $request_data->fk_c_exp_tax_cat,
348 $request_data->type,
349 $request_data->fk_ecm_files
350 );
351
352 if ($result > 0) {
353 return $result;
354 } else {
355 throw new RestException(500, 'Error adding line to expense report: '.$this->expensereport->error);
356 }
357 }
358
378 public function putLine($id, $lineid, $request_data = null)
379 {
380 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
381 throw new RestException(403);
382 }
383
384 $result = $this->expensereport->fetch($id);
385 if (!$result) {
386 throw new RestException(404, 'Expense report not found');
387 }
388
389 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
390 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
391 }
392
393 if ($this->expensereport->status != ExpenseReport::STATUS_DRAFT) {
394 throw new RestException(403, 'Expense report must be in draft status to update lines');
395 }
396
397 $line = new ExpenseReportLine($this->db);
398 $result = $line->fetch($lineid);
399 if ($result <= 0) {
400 throw new RestException(404, 'Expense report line not found');
401 }
402
403 $request_data = (object) $request_data;
404
405 $request_data->comments = sanitizeVal($request_data->comments, 'restricthtml');
406
407 $updateRes = $this->expensereport->updateline(
408 $lineid,
409 (int) $request_data->fk_c_type_fees,
410 $request_data->fk_project,
411 $request_data->vatrate,
412 $request_data->comments,
413 $request_data->qty,
414 $request_data->value_unit,
415 $request_data->date,
416 $id,
417 $request_data->fk_c_exp_tax_cat,
418 $request_data->fk_ecm_files
419 );
420
421 if ($updateRes > 0) {
422 $result = $this->get($id);
423 unset($result->line);
424 return $this->_cleanObjectDatas($result);
425 } else {
426 throw new RestException(500, 'Error updating line: '.$this->expensereport->error);
427 }
428 }
429
446 public function deleteLine($id, $lineid)
447 {
448 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
449 throw new RestException(403);
450 }
451
452 $result = $this->expensereport->fetch($id);
453 if (!$result) {
454 throw new RestException(404, 'Expense report not found');
455 }
456
457 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
458 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 }
460
461 // Check if line exists
462 $lineExists = false;
463 $this->expensereport->fetch_lines();
464 foreach ($this->expensereport->lines as $line) {
465 if ($line->id == $lineid) {
466 $lineExists = true;
467 break;
468 }
469 }
470
471 if (!$lineExists) {
472 throw new RestException(404, 'Line not found');
473 }
474
475 if ($this->expensereport->status != ExpenseReport::STATUS_DRAFT) {
476 throw new RestException(403, 'Expense report must be in draft status to delete lines');
477 }
478
479 $result = $this->expensereport->deleteLine($lineid);
480 if ($result > 0) {
481 return $this->get($id);
482 } else {
483 throw new RestException(500, 'Error deleting line: '.$this->expensereport->error);
484 }
485 }
486
504 public function put($id, $request_data = null)
505 {
506 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
507 throw new RestException(403);
508 }
509
510 $result = $this->expensereport->fetch($id);
511 if (!$result) {
512 throw new RestException(404, 'Expense report not found');
513 }
514
515 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
516 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
517 }
518 foreach ($request_data as $field => $value) {
519 if ($field == 'id') {
520 continue;
521 }
522 if ($field === 'caller') {
523 // 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
524 $this->expensereport->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
525 continue;
526 }
527
528 if ($field == 'array_options' && is_array($value)) {
529 foreach ($value as $index => $val) {
530 $this->expensereport->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->expensereport);
531 }
532 continue;
533 }
534
535 $this->expensereport->$field = $this->_checkValForAPI($field, $value, $this->expensereport);
536 }
537
538 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
539 return $this->get($id);
540 } else {
541 throw new RestException(500, $this->expensereport->error);
542 }
543 }
544
557 public function delete($id)
558 {
559 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'supprimer')) {
560 throw new RestException(403);
561 }
562
563 $result = $this->expensereport->fetch($id);
564 if (!$result) {
565 throw new RestException(404, 'Expense report not found');
566 }
567
568 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
569 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
570 }
571
572 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
573 throw new RestException(500, 'Error when delete Expense Report : '.$this->expensereport->error);
574 }
575
576 return array(
577 'success' => array(
578 'code' => 200,
579 'message' => 'Expense Report deleted'
580 )
581 );
582 }
583
599 public function setToDraft($id)
600 {
601 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
602 throw new RestException(403, "Insufficiant rights");
603 }
604 $result = $this->expensereport->fetch($id);
605 if (!$result) {
606 throw new RestException(404, 'Expense report not found');
607 }
608
609 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
610 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
611 }
612
613 $result = $this->expensereport->setStatut(ExpenseReport::STATUS_DRAFT);
614 if ($result == 0) {
615 throw new RestException(304, 'Error nothing done. May be object is already draft');
616 }
617 if ($result < 0) {
618 throw new RestException(500, 'Error when setting to draft expense report: '.$this->expensereport->error);
619 }
620
621 return $this->_cleanObjectDatas($this->expensereport);
622 }
623
643 public function validate($id, $notrigger = 0)
644 {
645 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
646 throw new RestException(403, "Insufficiant rights");
647 }
648 $result = $this->expensereport->fetch($id);
649 if (!$result) {
650 throw new RestException(404, 'Expense report not found');
651 }
652
653 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
654 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
655 }
656
657 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
658 if ($result == 0) {
659 throw new RestException(304, 'Error nothing done. May be object is already validated');
660 }
661 if ($result < 0) {
662 throw new RestException(500, 'Error when validating expense report: '.$this->expensereport->error);
663 }
664
665 return $this->_cleanObjectDatas($this->expensereport);
666 }
667
668
688 public function approve($id, $notrigger = 0)
689 {
690 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'approve')) {
691 throw new RestException(403, "Insufficiant rights");
692 }
693 $result = $this->expensereport->fetch($id);
694 if (!$result) {
695 throw new RestException(404, 'Expense report not found');
696 }
697
698 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
699 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
700 }
701
702 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
703 if ($result == 0) {
704 throw new RestException(304, 'Error nothing done. May be object is already approved');
705 }
706 if ($result < 0) {
707 throw new RestException(500, 'Error when approving expense report: '.$this->expensereport->error);
708 }
709
710 return $this->_cleanObjectDatas($this->expensereport);
711 }
712
713
734 public function deny($id, $details, $notrigger = 0)
735 {
736 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'approve')) {
737 throw new RestException(403, "Insufficiant rights");
738 }
739 $result = $this->expensereport->fetch($id);
740 if (!$result) {
741 throw new RestException(404, 'Expense report not found');
742 }
743
744 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
745 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
746 }
747
748 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
749 if ($result == 0) {
750 throw new RestException(304, 'Error nothing done. May be object is already denied');
751 }
752 if ($result < 0) {
753 throw new RestException(500, 'Error when denying expense report: '.$this->expensereport->error);
754 }
755
756
757
758 return $this->_cleanObjectDatas($this->expensereport);
759 }
760
780 public function setPaid($id, $notrigger = 0)
781 {
782 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'to_paid')) {
783 throw new RestException(403, "Insufficiant rights");
784 }
785 $result = $this->expensereport->fetch($id);
786 if (!$result) {
787 throw new RestException(404, 'Expense report not found');
788 }
789
790 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
791 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
792 }
793
794 $result = $this->expensereport->setPaid($id, DolibarrApiAccess::$user, $notrigger);
795 if ($result == 0) {
796 throw new RestException(304, 'Error nothing done. May be object is already approved');
797 }
798 if ($result < 0) {
799 throw new RestException(500, 'Error when approving expense report: '.$this->expensereport->error);
800 }
801
802 return $this->_cleanObjectDatas($this->expensereport);
803 }
804
822 public function cancel($id, $detail, $notrigger = 0)
823 {
824 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
825 throw new RestException(403, "Insufficiant rights");
826 }
827 $result = $this->expensereport->fetch($id);
828 if (!$result) {
829 throw new RestException(404, 'Expense report not found');
830 }
831
832 if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport)) {
833 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
834 }
835
836 if ($this->expensereport->status == ExpenseReport::STATUS_CANCELED) {
837 throw new RestException(403, 'Expense report already canceled');
838 }
839 $result = $this->expensereport->set_cancel(DolibarrApiAccess::$user, $detail, $notrigger);
840 if ($result < 0) {
841 throw new RestException(500, 'Error when cancelling expense report: '.$this->expensereport->error);
842 }
843
844 $result = $this->expensereport->fetch($id);
845 return $this->_cleanObjectDatas($this->expensereport);
846 }
847
865 public function getAllPayments($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
866 {
867 $list = array();
868
869 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
870 throw new RestException(403);
871 }
872
873 $sql = "SELECT t.rowid FROM " . MAIN_DB_PREFIX . "payment_expensereport as t, ".MAIN_DB_PREFIX."expensereport as e";
874 $sql .= " WHERE e.rowid = t.fk_expensereport";
875 $sql .= ' AND e.entity IN ('.getEntity('expensereport').')';
876
877 $sql .= $this->db->order($sortfield, $sortorder);
878 if ($limit) {
879 if ($page < 0) {
880 $page = 0;
881 }
882 $offset = $limit * $page;
883
884 $sql .= $this->db->plimit($limit + 1, $offset);
885 }
886
887 dol_syslog("API Rest request");
888 $result = $this->db->query($sql);
889
890 if ($result) {
891 $num = $this->db->num_rows($result);
892 $min = min($num, ($limit <= 0 ? $num : $limit));
893 for ($i = 0; $i < $min; $i++) {
894 $obj = $this->db->fetch_object($result);
895 $paymentExpenseReport = new PaymentExpenseReport($this->db);
896 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
897 $list[] = $this->_cleanObjectDatas($paymentExpenseReport);
898 }
899 }
900 } else {
901 throw new RestException(503, 'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
902 }
903
904 return $list;
905 }
906
919 public function getPayments($pid)
920 {
921 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'lire')) {
922 throw new RestException(403);
923 }
924
925 $paymentExpenseReport = new PaymentExpenseReport($this->db);
926 $result = $paymentExpenseReport->fetch($pid);
927 if (!$result) {
928 throw new RestException(404, 'paymentExpenseReport not found');
929 }
930
931 return $this->_cleanObjectDatas($paymentExpenseReport);
932 }
933
948 public function addPayment($id, $request_data = null)
949 {
950 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
951 throw new RestException(403);
952 }
953 // Check mandatory fields
954 $result = $this->_validatepayment($request_data);
955
956 $paymentExpenseReport = new PaymentExpenseReport($this->db);
957 $paymentExpenseReport->fk_expensereport = $id;
958 foreach ($request_data as $field => $value) {
959 $paymentExpenseReport->$field = $this->_checkValForAPI($field, $value, $paymentExpenseReport);
960 }
961
962 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
963 throw new RestException(500, 'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
964 }
965 if (isModEnabled("bank")) {
966 $paymentExpenseReport->addPaymentToBank(
967 DolibarrApiAccess::$user,
968 'payment_expensereport',
969 '(ExpenseReportPayment)',
970 (int) $request_data['accountid'],
971 '',
972 ''
973 );
974 }
975
976 return $paymentExpenseReport->id;
977 }
978
993 public function updatePayment($id, $request_data = null)
994 {
995 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) {
996 throw new RestException(403);
997 }
998
999 $paymentExpenseReport = new PaymentExpenseReport($this->db);
1000 $result = $paymentExpenseReport->fetch($id);
1001 if (!$result) {
1002 throw new RestException(404, 'payment of expense report not found');
1003 }
1004
1005 foreach ($request_data as $field => $value) {
1006 if ($field == 'id') {
1007 continue;
1008 }
1009 $paymentExpenseReport->$field = $this->_checkValForAPI($field, $value, $paymentExpenseReport);
1010 }
1011
1012 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
1013 return $this->get($id);
1014 } else {
1015 throw new RestException(500, $paymentExpenseReport->error);
1016 }
1017 }
1018
1027 /*public function delete($id)
1028 {
1029 if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer') {
1030 throw new RestException(403);
1031 }
1032 $paymentExpenseReport = new PaymentExpenseReport($this->db);
1033 $result = $paymentExpenseReport->fetch($id);
1034 if (!$result) {
1035 throw new RestException(404, 'paymentExpenseReport not found');
1036 }
1037
1038 if ($paymentExpenseReport->delete(DolibarrApiAccess::$user) < 0) {
1039 throw new RestException(403, 'error when deleting paymentExpenseReport');
1040 }
1041
1042 return array(
1043 'success' => array(
1044 'code' => 200,
1045 'message' => 'paymentExpenseReport deleted'
1046 )
1047 );
1048 }*/
1049
1050
1051
1052 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1062 protected function _cleanObjectDatas($object)
1063 {
1064 // phpcs:enable
1065 $object = parent::_cleanObjectDatas($object);
1066
1067 unset($object->fk_statut);
1068 unset($object->statut);
1069 unset($object->user);
1070 unset($object->thirdparty);
1071
1072 unset($object->cond_reglement);
1073 unset($object->shipping_method_id);
1074
1075 unset($object->barcode_type);
1076 unset($object->barcode_type_code);
1077 unset($object->barcode_type_label);
1078 unset($object->barcode_type_coder);
1079
1080 unset($object->code_paiement);
1081 unset($object->code_statut);
1082 unset($object->fk_c_paiement);
1083 unset($object->fk_incoterms);
1084 unset($object->label_incoterms);
1085 unset($object->location_incoterms);
1086 unset($object->mode_reglement_id);
1087 unset($object->cond_reglement_id);
1088
1089 unset($object->name);
1090 unset($object->lastname);
1091 unset($object->firstname);
1092 unset($object->civility_id);
1093 unset($object->cond_reglement_id);
1094 unset($object->contact);
1095 unset($object->contact_id);
1096
1097 unset($object->state);
1098 unset($object->state_id);
1099 unset($object->state_code);
1100 unset($object->country);
1101 unset($object->country_id);
1102 unset($object->country_code);
1103
1104 unset($object->note); // We already use note_public and note_pricate
1105
1106 return $object;
1107 }
1108
1116 private function _validate($data)
1117 {
1118 if ($data === null) {
1119 $data = array();
1120 }
1121 $expensereport = array();
1122 foreach (ExpenseReports::$FIELDS as $field) {
1123 if (!isset($data[$field])) {
1124 throw new RestException(400, "$field field missing");
1125 }
1126 $expensereport[$field] = $data[$field];
1127 }
1128 return $expensereport;
1129 }
1130
1138 private function _validatepayment($data)
1139 {
1140 if ($data === null) {
1141 $data = array();
1142 }
1143 $expensereport = array();
1144 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
1145 if (!isset($data[$field])) {
1146 throw new RestException(400, "$field field missing");
1147 }
1148 $expensereport[$field] = $data[$field];
1149 }
1150 return $expensereport;
1151 }
1152
1161 private function _validateLine($data)
1162 {
1163 if ($data === null) {
1164 $data = array();
1165 }
1166 $expenseReport = array();
1167 foreach (ExpenseReports::$FIELDSLINE as $field) {
1168 if (!isset($data[$field])) {
1169 throw new RestException(400, "$field field missing");
1170 }
1171 $expenseReport[$field] = $data[$field];
1172 }
1173 return $expenseReport;
1174 }
1175}
$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 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.
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.