24use Luracast\Restler\RestException;
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';
43 public static $FIELDS = array(
52 public static $FIELDSLINE = array(
63 public static $FIELDSPAYMENT = array(
72 public $expensereport;
98 public function get(
$id)
100 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
101 throw new RestException(403);
104 $result = $this->expensereport->fetch(
$id);
106 throw new RestException(404,
'Expense report not found');
110 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
113 $this->expensereport->fetchObjectLinked();
138 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
140 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
141 throw new RestException(403);
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)";
151 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
153 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize($user_ids).
")";
157 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'readall')
158 && (!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') || !DolibarrApiAccess::$user->hasRight(
'expensereport',
'writeall_advance'))) {
159 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
160 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize(implode(
',', $childids)).
")";
168 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
173 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
175 $sql .= $this->db->order($sortfield, $sortorder);
180 $offset = $limit * $page;
182 $sql .= $this->db->plimit($limit + 1, $offset);
185 $result = $this->db->query($sql);
188 $num = $this->db->num_rows($result);
189 $min = min($num, ($limit <= 0 ? $num : $limit));
192 $obj = $this->db->fetch_object($result);
194 if ($expensereport_static->fetch($obj->rowid)) {
200 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->db->lasterror());
204 if ($pagination_data) {
205 $totalsResult = $this->db->query($sqlTotals);
206 $total = $this->db->fetch_object($totalsResult)->total;
211 $obj_ret[
'data'] = $tmp;
212 $obj_ret[
'pagination'] = [
213 'total' => (int) $total,
215 'page_count' => ceil((
int) $total / $limit),
235 public function post($request_data =
null)
237 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
238 throw new RestException(403,
"Insufficiant rights");
242 $result = $this->
_validate($request_data);
244 foreach ($request_data as $field => $value) {
245 if ($field ===
'caller') {
247 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
251 if ($field ==
'array_options' && is_array($value)) {
252 foreach ($value as $index => $val) {
258 if (!in_array($field, array(
'fk_statut',
'fk_user_approve'))) {
259 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
269 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
270 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
273 return $this->expensereport->id;
294 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
295 throw new RestException(403);
298 $result = $this->expensereport->fetch(
$id);
300 throw new RestException(404,
'Expense report not found');
304 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
306 $this->expensereport->fetch_lines();
308 foreach ($this->expensereport->lines as $line) {
332 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
333 throw new RestException(403);
338 $result = $this->expensereport->fetch(
$id);
340 throw new RestException(404,
'Expense report not found');
344 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
348 throw new RestException(403,
'Expense report must be in draft status to add lines');
351 $request_data = (object) $request_data;
353 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
355 $result = $this->expensereport->addline(
357 $request_data->value_unit,
358 (
int) $request_data->fk_c_type_fees,
359 $request_data->vatrate,
361 $request_data->comments,
362 $request_data->fk_project,
363 (
int) $request_data->fk_c_exp_tax_cat,
365 $request_data->fk_ecm_files
371 throw new RestException(500,
'Error adding line to expense report: '.$this->expensereport->errorsToString());
396 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
397 throw new RestException(403);
400 $result = $this->expensereport->fetch(
$id);
402 throw new RestException(404,
'Expense report not found');
406 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
410 throw new RestException(403,
'Expense report must be in draft status to update lines');
414 $result = $line->fetch($lineid);
416 throw new RestException(404,
'Expense report line not found');
419 $request_data = (object) $request_data;
421 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
423 $updateRes = $this->expensereport->updateline(
425 (
int) $request_data->fk_c_type_fees,
426 $request_data->fk_project,
427 $request_data->vatrate,
428 $request_data->comments,
430 $request_data->value_unit,
433 (
int) $request_data->fk_c_exp_tax_cat,
434 $request_data->fk_ecm_files
437 if ($updateRes > 0) {
438 $result = $this->
get(
$id);
439 unset($result->line);
442 throw new RestException(500,
'Error updating line: '.$this->expensereport->errorsToString());
464 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
465 throw new RestException(403);
468 $result = $this->expensereport->fetch(
$id);
470 throw new RestException(404,
'Expense report not found');
474 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
479 $this->expensereport->fetch_lines();
480 foreach ($this->expensereport->lines as $line) {
481 if ($line->id == $lineid) {
488 throw new RestException(404,
'Line not found');
492 throw new RestException(403,
'Expense report must be in draft status to delete lines');
495 $result = $this->expensereport->deleteLine($lineid);
497 return $this->
get(
$id);
499 throw new RestException(500,
'Error deleting line: '.$this->expensereport->errorsToString());
520 public function put(
$id, $request_data =
null)
522 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
523 throw new RestException(403);
526 $result = $this->expensereport->fetch(
$id);
528 throw new RestException(404,
'Expense report not found');
532 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
534 foreach ($request_data as $field => $value) {
535 if ($field ==
'id') {
538 if ($field ===
'caller') {
540 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
544 if ($field ==
'array_options' && is_array($value)) {
545 foreach ($value as $index => $val) {
551 if (!in_array($field, array(
'fk_statut',
'fk_user_approve'))) {
552 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
556 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
557 return $this->
get(
$id);
559 throw new RestException(500, $this->expensereport->errorsToString());
575 public function delete(
$id)
577 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
578 throw new RestException(403);
581 $result = $this->expensereport->fetch(
$id);
583 throw new RestException(404,
'Expense report not found');
587 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
590 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
591 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->errorsToString());
597 'message' =>
'Expense Report deleted'
619 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
620 throw new RestException(403,
"Insufficiant rights");
622 $result = $this->expensereport->fetch(
$id);
624 throw new RestException(404,
'Expense report not found');
628 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
633 throw new RestException(304,
'Error nothing done. May be object is already draft');
636 throw new RestException(500,
'Error when setting to draft expense report: '.$this->expensereport->errorsToString());
663 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
664 throw new RestException(403,
"Insufficiant rights");
666 $result = $this->expensereport->fetch(
$id);
668 throw new RestException(404,
'Expense report not found');
672 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
675 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
677 throw new RestException(304,
'Error nothing done. May be object is already validated');
680 throw new RestException(500,
'Error when validating expense report: '.$this->expensereport->errorsToString());
708 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
709 throw new RestException(403,
"Insufficiant rights");
711 $result = $this->expensereport->fetch(
$id);
713 throw new RestException(404,
'Expense report not found');
717 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
720 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
722 throw new RestException(304,
'Error nothing done. May be object is already approved');
725 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->errorsToString());
752 public function deny(
$id, $details, $notrigger = 0)
754 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
755 throw new RestException(403,
"Insufficiant rights");
757 $result = $this->expensereport->fetch(
$id);
759 throw new RestException(404,
'Expense report not found');
763 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
766 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
768 throw new RestException(304,
'Error nothing done. May be object is already denied');
771 throw new RestException(500,
'Error when denying expense report: '.$this->expensereport->errorsToString());
800 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'to_paid')) {
801 throw new RestException(403,
"Insufficiant rights");
803 $result = $this->expensereport->fetch(
$id);
805 throw new RestException(404,
'Expense report not found');
809 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
812 $result = $this->expensereport->setPaid(
$id, DolibarrApiAccess::$user, $notrigger);
814 throw new RestException(304,
'Error nothing done. May be object is already approved');
817 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->errorsToString());
842 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
843 throw new RestException(403,
"Insufficiant rights");
845 $result = $this->expensereport->fetch(
$id);
847 throw new RestException(404,
'Expense report not found');
851 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
855 throw new RestException(403,
'Expense report already canceled');
857 $result = $this->expensereport->set_cancel(DolibarrApiAccess::$user, $detail, $notrigger);
859 throw new RestException(500,
'Error when cancelling expense report: '.$this->expensereport->errorsToString());
862 $result = $this->expensereport->fetch(
$id);
883 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
887 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
888 throw new RestException(403);
891 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
892 $sql .=
" WHERE e.rowid = t.fk_expensereport";
893 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
896 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'readall')) {
897 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
898 $sql .=
" AND e.fk_user_author IN (".$this->db->sanitize(implode(
',', $childids)).
")";
901 $sql .= $this->db->order($sortfield, $sortorder);
906 $offset = $limit * $page;
908 $sql .= $this->db->plimit($limit + 1, $offset);
912 $result = $this->db->query($sql);
915 $num = $this->db->num_rows($result);
916 $min = min($num, ($limit <= 0 ? $num : $limit));
917 for ($i = 0; $i < $min; $i++) {
918 $obj = $this->db->fetch_object($result);
920 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
925 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
945 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
946 throw new RestException(403);
950 $result = $paymentExpenseReport->fetch($pid);
952 throw new RestException(404,
'paymentExpenseReport not found');
956 $result = $this->expensereport->fetch($paymentExpenseReport->fk_expensereport);
958 throw new RestException(404,
'Expense report not found');
962 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
984 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
985 throw new RestException(403);
990 if (
isModEnabled(
"bank") && !((
int) ($request_data[
'accountid'] ?? 0) > 0)) {
991 throw new RestException(400,
"accountid field missing");
995 $result = $this->expensereport->fetch(
$id);
997 throw new RestException(404,
'Expense report not found');
1001 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1005 $paymentExpenseReport->fk_expensereport =
$id;
1006 foreach ($request_data as $field => $value) {
1007 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
1013 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
1014 $this->db->rollback();
1015 throw new RestException(400,
'Payment error : '.$paymentExpenseReport->errorsToString());
1018 $result = $paymentExpenseReport->addPaymentToBank(
1019 DolibarrApiAccess::$user,
1020 'payment_expensereport',
1021 '(ExpenseReportPayment)',
1022 (
int) $request_data[
'accountid'],
1027 $this->db->rollback();
1028 throw new RestException(400,
'Add payment to bank error : '.$paymentExpenseReport->errorsToString());
1032 $remaintopay =
price2num($this->expensereport->total_ttc - $this->expensereport->getSumPayments(),
'MT');
1033 if ($remaintopay == 0 && $this->expensereport->setPaid($this->expensereport->id, DolibarrApiAccess::$user) < 0) {
1034 $this->db->rollback();
1035 throw new RestException(400,
'Set paid error : '.$this->expensereport->errorsToString());
1038 $this->db->commit();
1040 return $paymentExpenseReport->id;
1060 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
1061 throw new RestException(403);
1065 $result = $paymentExpenseReport->fetch($idp);
1067 throw new RestException(404,
'Payment of expense report not found');
1071 if (
$id != $paymentExpenseReport->fk_expensereport) {
1072 throw new RestException(404,
'Payment id does not belongs to the Expense id');
1076 $result = $this->expensereport->fetch($paymentExpenseReport->fk_expensereport);
1078 throw new RestException(404,
'Expense report not found');
1082 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1085 foreach ($request_data as $field => $value) {
1086 if ($field ==
'id') {
1089 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
1092 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
1095 throw new RestException(500, $paymentExpenseReport->errorsToString());
1152 unset(
$object->cond_reglement);
1153 unset(
$object->shipping_method_id);
1156 unset(
$object->barcode_type_code);
1157 unset(
$object->barcode_type_label);
1158 unset(
$object->barcode_type_coder);
1160 unset(
$object->code_paiement);
1162 unset(
$object->fk_c_paiement);
1164 unset(
$object->label_incoterms);
1165 unset(
$object->location_incoterms);
1166 unset(
$object->mode_reglement_id);
1167 unset(
$object->cond_reglement_id);
1173 unset(
$object->cond_reglement_id);
1198 if ($data ===
null) {
1201 $expensereport = array();
1202 foreach (ExpenseReports::$FIELDS as $field) {
1203 if (!isset($data[$field])) {
1204 throw new RestException(400,
"$field field missing");
1206 $expensereport[$field] = $data[$field];
1208 return $expensereport;
1220 if ($data ===
null) {
1223 $expensereport = array();
1224 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
1225 if (!isset($data[$field])) {
1226 throw new RestException(400,
"$field field missing");
1228 $expensereport[$field] = $data[$field];
1230 return $expensereport;
1243 if ($data ===
null) {
1246 $expenseReport = array();
1247 foreach (ExpenseReports::$FIELDSLINE as $field) {
1248 if (!isset($data[$field])) {
1249 throw new RestException(400,
"$field field missing");
1251 $expenseReport[$field] = $data[$field];
1253 return $expenseReport;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_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.
deny($id, $details, $notrigger=0)
Deny an expense report.
_cleanObjectDatas($object)
Delete paymentExpenseReport.
_validate($data)
Validate fields before create or update object.
updatePayment($id, $idp, $request_data=null)
Update a payment of an expense report.
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.
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.
getPayment($pid)
Get an expense report payment.
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.
__construct()
Constructor.
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.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
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.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.