23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/paymentexpensereport.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
42 public static $FIELDS = array(
51 public static $FIELDSLINE = array(
62 public static $FIELDSPAYMENT = array(
71 public $expensereport;
76 public static $FIELDSNOTUPDATABLE = array(
77 'fk_statut',
'status',
'statut',
'paid',
78 'fk_user_valid',
'fk_user_approve',
'fk_user_refuse',
'fk_user_cancel',
'fk_user_creat',
'fk_user_modif',
79 'date_valid',
'date_approve',
'date_refuse',
'date_cancel',
'detail_refuse',
'detail_cancel',
80 'total_ht',
'total_tva',
'total_ttc',
'total_localtax1',
'total_localtax2',
110 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
111 throw new RestException(403);
114 $result = $this->expensereport->fetch(
$id);
116 throw new RestException(404,
'Expense report not found');
120 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
123 $this->expensereport->fetchObjectLinked();
148 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
150 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
151 throw new RestException(403);
159 $sql =
"SELECT t.rowid";
160 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport AS t LEFT JOIN ".MAIN_DB_PREFIX.
"expensereport_extrafields AS ef ON (ef.fk_object = t.rowid)";
161 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
163 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize($user_ids).
")";
167 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'readall')
168 && (!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') || !DolibarrApiAccess::$user->hasRight(
'expensereport',
'writeall_advance'))) {
169 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
170 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize(implode(
',', $childids)).
")";
178 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
183 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
185 $sql .= $this->db->order($sortfield, $sortorder);
190 $offset = $limit * $page;
192 $sql .= $this->db->plimit($limit + 1, $offset);
195 $result = $this->db->query($sql);
198 $num = $this->db->num_rows($result);
199 $min = min($num, ($limit <= 0 ? $num : $limit));
202 $obj = $this->db->fetch_object($result);
204 if ($expensereport_static->fetch($obj->rowid)) {
210 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->db->lasterror());
214 if ($pagination_data) {
215 $totalsResult = $this->db->query($sqlTotals);
216 $total = $this->db->fetch_object($totalsResult)->total;
221 $obj_ret[
'data'] = $tmp;
222 $obj_ret[
'pagination'] = [
223 'total' => (int) $total,
225 'page_count' => ceil((
int) $total / $limit),
245 public function post($request_data =
null)
247 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
248 throw new RestException(403,
"Insufficiant rights");
252 $result = $this->
_validate($request_data);
255 $fk_user_author = (int) ($request_data[
'fk_user_author'] ?? 0);
256 if ($fk_user_author > 0 && $fk_user_author != DolibarrApiAccess::$user->
id
257 && (!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') || !DolibarrApiAccess::$user->hasRight(
'expensereport',
'writeall_advance'))
258 && !in_array($fk_user_author, DolibarrApiAccess::$user->getAllChildIds(1))) {
259 throw new RestException(403,
'User '.$fk_user_author.
' is not in the hierarchy of login '.DolibarrApiAccess::$user->login);
262 foreach (ExpenseReports::$FIELDSNOTUPDATABLE as $field) {
263 unset($request_data[$field]);
266 foreach ($request_data as $field => $value) {
267 if ($field ===
'caller') {
269 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
273 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
282 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
283 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
286 return $this->expensereport->id;
307 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
308 throw new RestException(403);
311 $result = $this->expensereport->fetch(
$id);
313 throw new RestException(404,
'Expense report not found');
317 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
319 $this->expensereport->fetch_lines();
321 foreach ($this->expensereport->lines as $line) {
345 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
346 throw new RestException(403);
351 $result = $this->expensereport->fetch(
$id);
353 throw new RestException(404,
'Expense report not found');
357 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
361 throw new RestException(403,
'Expense report must be in draft status to add lines');
364 $request_data = (object) $request_data;
366 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
368 $result = $this->expensereport->addline(
370 $request_data->value_unit,
371 (
int) $request_data->fk_c_type_fees,
372 $request_data->vatrate,
374 $request_data->comments,
375 $request_data->fk_project,
376 $request_data->fk_c_exp_tax_cat,
378 $request_data->fk_ecm_files
384 throw new RestException(500,
'Error adding line to expense report: '.$this->expensereport->error);
409 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
410 throw new RestException(403);
413 $result = $this->expensereport->fetch(
$id);
415 throw new RestException(404,
'Expense report not found');
419 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
423 throw new RestException(403,
'Expense report must be in draft status to update lines');
427 $result = $line->fetch($lineid);
429 throw new RestException(404,
'Expense report line not found');
432 $request_data = (object) $request_data;
434 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
436 $updateRes = $this->expensereport->updateline(
438 (
int) $request_data->fk_c_type_fees,
439 $request_data->fk_project,
440 $request_data->vatrate,
441 $request_data->comments,
443 $request_data->value_unit,
446 $request_data->fk_c_exp_tax_cat,
447 $request_data->fk_ecm_files
450 if ($updateRes > 0) {
451 $result = $this->
get(
$id);
452 unset($result->line);
455 throw new RestException(500,
'Error updating line: '.$this->expensereport->error);
477 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
478 throw new RestException(403);
481 $result = $this->expensereport->fetch(
$id);
483 throw new RestException(404,
'Expense report not found');
487 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
492 $this->expensereport->fetch_lines();
493 foreach ($this->expensereport->lines as $line) {
494 if ($line->id == $lineid) {
501 throw new RestException(404,
'Line not found');
505 throw new RestException(403,
'Expense report must be in draft status to delete lines');
508 $result = $this->expensereport->deleteLine($lineid);
510 return $this->
get(
$id);
512 throw new RestException(500,
'Error deleting line: '.$this->expensereport->error);
533 public function put(
$id, $request_data =
null)
535 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
536 throw new RestException(403);
539 $result = $this->expensereport->fetch(
$id);
541 throw new RestException(404,
'Expense report not found');
545 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
549 throw new RestException(403,
'Only a draft or refused expense report can be modified');
552 foreach (ExpenseReports::$FIELDSNOTUPDATABLE as $field) {
553 unset($request_data[$field]);
555 foreach ($request_data as $field => $value) {
556 if ($field ==
'id') {
559 if ($field ===
'caller') {
561 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
565 if ($field ==
'array_options' && is_array($value)) {
566 foreach ($value as $index => $val) {
567 $this->expensereport->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->expensereport);
572 if (!in_array($field, array(
'fk_statut',
'fk_user_approve'))) {
573 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
577 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
578 return $this->
get(
$id);
580 throw new RestException(500, $this->expensereport->error);
596 public function delete(
$id)
598 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
599 throw new RestException(403);
602 $result = $this->expensereport->fetch(
$id);
604 throw new RestException(404,
'Expense report not found');
608 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
611 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
612 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
618 'message' =>
'Expense Report deleted'
640 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
641 throw new RestException(403,
"Insufficiant rights");
643 $result = $this->expensereport->fetch(
$id);
645 throw new RestException(404,
'Expense report not found');
649 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
652 $result = $this->expensereport->setStatut(0);
654 throw new RestException(304,
'Error nothing done. May be object is already draft');
657 throw new RestException(500,
'Error when setting to draft expense report: '.$this->expensereport->error);
684 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
685 throw new RestException(403,
"Insufficiant rights");
687 $result = $this->expensereport->fetch(
$id);
689 throw new RestException(404,
'Expense report not found');
693 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
696 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
698 throw new RestException(304,
'Error nothing done. May be object is already validated');
701 throw new RestException(500,
'Error when validating expense report: '.$this->expensereport->error);
729 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
730 throw new RestException(403,
"Insufficiant rights");
732 $result = $this->expensereport->fetch(
$id);
734 throw new RestException(404,
'Expense report not found');
738 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
741 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
743 throw new RestException(304,
'Error nothing done. May be object is already approved');
746 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->error);
773 public function deny(
$id, $details, $notrigger = 0)
775 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
776 throw new RestException(403,
"Insufficiant rights");
778 $result = $this->expensereport->fetch(
$id);
780 throw new RestException(404,
'Expense report not found');
784 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
787 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
789 throw new RestException(304,
'Error nothing done. May be object is already denied');
792 throw new RestException(500,
'Error when denying expense report: '.$this->expensereport->error);
819 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
820 throw new RestException(403,
"Insufficiant rights");
822 $result = $this->expensereport->fetch(
$id);
824 throw new RestException(404,
'Expense report not found');
828 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
832 throw new RestException(403,
'Expense report already canceled');
834 $result = $this->expensereport->set_cancel(DolibarrApiAccess::$user, $detail, $notrigger);
836 throw new RestException(500,
'Error when cancelling expense report: '.$this->expensereport->error);
839 $result = $this->expensereport->fetch(
$id);
860 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
864 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
865 throw new RestException(403);
868 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
869 $sql .=
" WHERE e.rowid = t.fk_expensereport";
870 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
873 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'readall')) {
874 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
875 $sql .=
" AND e.fk_user_author IN (".$this->db->sanitize(implode(
',', $childids)).
")";
878 $sql .= $this->db->order($sortfield, $sortorder);
883 $offset = $limit * $page;
885 $sql .= $this->db->plimit($limit + 1, $offset);
889 $result = $this->db->query($sql);
892 $num = $this->db->num_rows($result);
893 $min = min($num, ($limit <= 0 ? $num : $limit));
894 for ($i = 0; $i < $min; $i++) {
895 $obj = $this->db->fetch_object($result);
897 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
902 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
922 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
923 throw new RestException(403);
927 $result = $paymentExpenseReport->fetch($pid);
929 throw new RestException(404,
'paymentExpenseReport not found');
933 $result = $this->expensereport->fetch($paymentExpenseReport->fk_expensereport);
935 throw new RestException(404,
'Expense report not found');
939 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
961 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'to_paid')) {
962 throw new RestException(403);
967 if ($this->expensereport->fetch(
$id) <= 0) {
968 throw new RestException(404,
'Expense report not found');
973 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
975 if (
isModEnabled(
"bank") && !((
int) ($request_data[
'accountid'] ?? 0) > 0)) {
976 throw new RestException(400,
"accountid field missing");
980 $paymentExpenseReport->fk_expensereport =
$id;
981 foreach ($request_data as $field => $value) {
982 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
987 throw new RestException(400,
'Expense report must be approved to be paid');
990 foreach ((array) $paymentExpenseReport->amounts as $value) {
991 $totalpayment += (float)
price2num($value,
'MT');
993 $remaintopay = (float)
price2num($this->expensereport->total_ttc - $this->expensereport->getSumPayments(),
'MT');
994 if ((
float)
price2num($totalpayment,
'MT') > $remaintopay) {
995 throw new RestException(400,
'Payment higher than remainder to pay ('.$remaintopay.
')');
1001 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
1002 $this->db->rollback();
1003 throw new RestException(400,
'Payment error : '.$paymentExpenseReport->errorsToString());
1006 $result = $paymentExpenseReport->addPaymentToBank(
1007 DolibarrApiAccess::$user,
1008 'payment_expensereport',
1009 '(ExpenseReportPayment)',
1010 (
int) $request_data[
'accountid'],
1015 $this->db->rollback();
1016 throw new RestException(400,
'Add payment to bank error : '.$paymentExpenseReport->errorsToString());
1020 $remaintopay =
price2num($this->expensereport->total_ttc - $this->expensereport->getSumPayments(),
'MT');
1021 if ($remaintopay == 0 && $this->expensereport->setPaid($this->expensereport->id, DolibarrApiAccess::$user) < 0) {
1022 $this->db->rollback();
1023 throw new RestException(400,
'Set paid error : '.$this->expensereport->errorsToString());
1026 $this->db->commit();
1028 return $paymentExpenseReport->id;
1047 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'to_paid')) {
1048 throw new RestException(403);
1052 $result = $paymentExpenseReport->fetch(
$id);
1054 throw new RestException(404,
'payment of expense report not found');
1058 $result = $this->expensereport->fetch($paymentExpenseReport->fk_expensereport);
1060 throw new RestException(404,
'Expense report not found');
1064 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1067 foreach ($request_data as $field => $value) {
1068 if ($field ==
'id' || $field ==
'fk_expensereport') {
1071 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
1076 $oldpayment->fetch($paymentExpenseReport->id);
1077 $remaintopay = (float)
price2num($this->expensereport->total_ttc - $this->expensereport->getSumPayments() + (float) $oldpayment->amount,
'MT');
1078 if ((
float)
price2num($paymentExpenseReport->amount,
'MT') > $remaintopay) {
1079 throw new RestException(400,
'Payment higher than remainder to pay ('.$remaintopay.
')');
1082 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
1083 return $this->
get(
$id);
1085 throw new RestException(500, $paymentExpenseReport->error);
1142 unset(
$object->cond_reglement);
1143 unset(
$object->shipping_method_id);
1146 unset(
$object->barcode_type_code);
1147 unset(
$object->barcode_type_label);
1148 unset(
$object->barcode_type_coder);
1150 unset(
$object->code_paiement);
1152 unset(
$object->fk_c_paiement);
1154 unset(
$object->label_incoterms);
1155 unset(
$object->location_incoterms);
1156 unset(
$object->mode_reglement_id);
1157 unset(
$object->cond_reglement_id);
1163 unset(
$object->cond_reglement_id);
1188 if ($data ===
null) {
1191 $expensereport = array();
1192 foreach (ExpenseReports::$FIELDS as $field) {
1193 if (!isset($data[$field])) {
1194 throw new RestException(400,
"$field field missing");
1196 $expensereport[$field] = $data[$field];
1198 return $expensereport;
1210 if ($data ===
null) {
1213 $expensereport = array();
1214 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
1215 if (!isset($data[$field])) {
1216 throw new RestException(400,
"$field field missing");
1218 $expensereport[$field] = $data[$field];
1220 return $expensereport;
1233 if ($data ===
null) {
1236 $expenseReport = array();
1237 foreach (ExpenseReports::$FIELDSLINE as $field) {
1238 if (!isset($data[$field])) {
1239 throw new RestException(400,
"$field field missing");
1241 $expenseReport[$field] = $data[$field];
1243 return $expenseReport;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_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_APPROVED
Classified approved.
const STATUS_CANCELED
Classified canceled.
const STATUS_REFUSED
Classified refused.
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.
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.
__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)
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.