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).
")";
161 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
166 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
168 $sql .= $this->db->order($sortfield, $sortorder);
173 $offset = $limit * $page;
175 $sql .= $this->db->plimit($limit + 1, $offset);
178 $result = $this->db->query($sql);
181 $num = $this->db->num_rows($result);
182 $min = min($num, ($limit <= 0 ? $num : $limit));
185 $obj = $this->db->fetch_object($result);
187 if ($expensereport_static->fetch($obj->rowid)) {
193 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->db->lasterror());
197 if ($pagination_data) {
198 $totalsResult = $this->db->query($sqlTotals);
199 $total = $this->db->fetch_object($totalsResult)->total;
204 $obj_ret[
'data'] = $tmp;
205 $obj_ret[
'pagination'] = [
206 'total' => (int) $total,
208 'page_count' => ceil((
int) $total / $limit),
228 public function post($request_data =
null)
230 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
231 throw new RestException(403,
"Insufficiant rights");
235 $result = $this->
_validate($request_data);
237 foreach ($request_data as $field => $value) {
238 if ($field ===
'caller') {
240 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
244 if ($field ==
'array_options' && is_array($value)) {
245 foreach ($value as $index => $val) {
251 if (!in_array($field, array(
'fk_statut',
'fk_user_approve'))) {
252 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
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));
266 return $this->expensereport->id;
287 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
288 throw new RestException(403);
291 $result = $this->expensereport->fetch(
$id);
293 throw new RestException(404,
'Expense report not found');
297 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
299 $this->expensereport->fetch_lines();
301 foreach ($this->expensereport->lines as $line) {
325 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
326 throw new RestException(403);
331 $result = $this->expensereport->fetch(
$id);
333 throw new RestException(404,
'Expense report not found');
337 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
341 throw new RestException(403,
'Expense report must be in draft status to add lines');
344 $request_data = (object) $request_data;
346 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
348 $result = $this->expensereport->addline(
350 $request_data->value_unit,
351 (
int) $request_data->fk_c_type_fees,
352 $request_data->vatrate,
354 $request_data->comments,
355 $request_data->fk_project,
356 (
int) $request_data->fk_c_exp_tax_cat,
358 $request_data->fk_ecm_files
364 throw new RestException(500,
'Error adding line to expense report: '.$this->expensereport->error);
389 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
390 throw new RestException(403);
393 $result = $this->expensereport->fetch(
$id);
395 throw new RestException(404,
'Expense report not found');
399 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
403 throw new RestException(403,
'Expense report must be in draft status to update lines');
407 $result = $line->fetch($lineid);
409 throw new RestException(404,
'Expense report line not found');
412 $request_data = (object) $request_data;
414 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
416 $updateRes = $this->expensereport->updateline(
418 (
int) $request_data->fk_c_type_fees,
419 $request_data->fk_project,
420 $request_data->vatrate,
421 $request_data->comments,
423 $request_data->value_unit,
426 (
int) $request_data->fk_c_exp_tax_cat,
427 $request_data->fk_ecm_files
430 if ($updateRes > 0) {
431 $result = $this->
get(
$id);
432 unset($result->line);
435 throw new RestException(500,
'Error updating line: '.$this->expensereport->error);
457 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
458 throw new RestException(403);
461 $result = $this->expensereport->fetch(
$id);
463 throw new RestException(404,
'Expense report not found');
467 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
472 $this->expensereport->fetch_lines();
473 foreach ($this->expensereport->lines as $line) {
474 if ($line->id == $lineid) {
481 throw new RestException(404,
'Line not found');
485 throw new RestException(403,
'Expense report must be in draft status to delete lines');
488 $result = $this->expensereport->deleteLine($lineid);
490 return $this->
get(
$id);
492 throw new RestException(500,
'Error deleting line: '.$this->expensereport->error);
513 public function put(
$id, $request_data =
null)
515 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
516 throw new RestException(403);
519 $result = $this->expensereport->fetch(
$id);
521 throw new RestException(404,
'Expense report not found');
525 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
527 foreach ($request_data as $field => $value) {
528 if ($field ==
'id') {
531 if ($field ===
'caller') {
533 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
537 if ($field ==
'array_options' && is_array($value)) {
538 foreach ($value as $index => $val) {
544 if (!in_array($field, array(
'fk_statut',
'fk_user_approve'))) {
545 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
549 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
550 return $this->
get(
$id);
552 throw new RestException(500, $this->expensereport->error);
568 public function delete(
$id)
570 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
571 throw new RestException(403);
574 $result = $this->expensereport->fetch(
$id);
576 throw new RestException(404,
'Expense report not found');
580 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
583 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
584 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
590 'message' =>
'Expense Report deleted'
612 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
613 throw new RestException(403,
"Insufficiant rights");
615 $result = $this->expensereport->fetch(
$id);
617 throw new RestException(404,
'Expense report not found');
621 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
626 throw new RestException(304,
'Error nothing done. May be object is already draft');
629 throw new RestException(500,
'Error when setting to draft expense report: '.$this->expensereport->error);
656 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
657 throw new RestException(403,
"Insufficiant rights");
659 $result = $this->expensereport->fetch(
$id);
661 throw new RestException(404,
'Expense report not found');
665 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
668 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
670 throw new RestException(304,
'Error nothing done. May be object is already validated');
673 throw new RestException(500,
'Error when validating expense report: '.$this->expensereport->error);
701 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
702 throw new RestException(403,
"Insufficiant rights");
704 $result = $this->expensereport->fetch(
$id);
706 throw new RestException(404,
'Expense report not found');
710 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
713 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
715 throw new RestException(304,
'Error nothing done. May be object is already approved');
718 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->error);
745 public function deny(
$id, $details, $notrigger = 0)
747 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
748 throw new RestException(403,
"Insufficiant rights");
750 $result = $this->expensereport->fetch(
$id);
752 throw new RestException(404,
'Expense report not found');
756 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
759 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
761 throw new RestException(304,
'Error nothing done. May be object is already denied');
764 throw new RestException(500,
'Error when denying expense report: '.$this->expensereport->error);
793 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'to_paid')) {
794 throw new RestException(403,
"Insufficiant rights");
796 $result = $this->expensereport->fetch(
$id);
798 throw new RestException(404,
'Expense report not found');
802 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
805 $result = $this->expensereport->setPaid(
$id, DolibarrApiAccess::$user, $notrigger);
807 throw new RestException(304,
'Error nothing done. May be object is already approved');
810 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->error);
835 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
836 throw new RestException(403,
"Insufficiant rights");
838 $result = $this->expensereport->fetch(
$id);
840 throw new RestException(404,
'Expense report not found');
844 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
848 throw new RestException(403,
'Expense report already canceled');
850 $result = $this->expensereport->set_cancel(DolibarrApiAccess::$user, $detail, $notrigger);
852 throw new RestException(500,
'Error when cancelling expense report: '.$this->expensereport->error);
855 $result = $this->expensereport->fetch(
$id);
876 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
880 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
881 throw new RestException(403);
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').
')';
888 $sql .= $this->db->order($sortfield, $sortorder);
893 $offset = $limit * $page;
895 $sql .= $this->db->plimit($limit + 1, $offset);
899 $result = $this->db->query($sql);
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);
907 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
912 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
932 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
933 throw new RestException(403);
937 $result = $paymentExpenseReport->fetch($pid);
939 throw new RestException(404,
'paymentExpenseReport not found');
961 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
962 throw new RestException(403);
968 $paymentExpenseReport->fk_expensereport =
$id;
969 foreach ($request_data as $field => $value) {
970 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
973 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
974 throw new RestException(500,
'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
977 $paymentExpenseReport->addPaymentToBank(
978 DolibarrApiAccess::$user,
979 'payment_expensereport',
980 '(ExpenseReportPayment)',
981 (
int) $request_data[
'accountid'],
987 return $paymentExpenseReport->id;
1006 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
1007 throw new RestException(403);
1011 $result = $paymentExpenseReport->fetch(
$id);
1013 throw new RestException(404,
'payment of expense report not found');
1016 foreach ($request_data as $field => $value) {
1017 if ($field ==
'id') {
1020 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
1023 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
1024 return $this->
get(
$id);
1026 throw new RestException(500, $paymentExpenseReport->error);
1083 unset(
$object->cond_reglement);
1084 unset(
$object->shipping_method_id);
1087 unset(
$object->barcode_type_code);
1088 unset(
$object->barcode_type_label);
1089 unset(
$object->barcode_type_coder);
1091 unset(
$object->code_paiement);
1093 unset(
$object->fk_c_paiement);
1095 unset(
$object->label_incoterms);
1096 unset(
$object->location_incoterms);
1097 unset(
$object->mode_reglement_id);
1098 unset(
$object->cond_reglement_id);
1104 unset(
$object->cond_reglement_id);
1129 if ($data ===
null) {
1132 $expensereport = array();
1133 foreach (ExpenseReports::$FIELDS as $field) {
1134 if (!isset($data[$field])) {
1135 throw new RestException(400,
"$field field missing");
1137 $expensereport[$field] = $data[$field];
1139 return $expensereport;
1151 if ($data ===
null) {
1154 $expensereport = array();
1155 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
1156 if (!isset($data[$field])) {
1157 throw new RestException(400,
"$field field missing");
1159 $expensereport[$field] = $data[$field];
1161 return $expensereport;
1174 if ($data ===
null) {
1177 $expenseReport = array();
1178 foreach (ExpenseReports::$FIELDSLINE as $field) {
1179 if (!isset($data[$field])) {
1180 throw new RestException(400,
"$field field missing");
1182 $expenseReport[$field] = $data[$field];
1184 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.
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.
__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.
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.