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).
")";
159 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
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 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
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));
257 return $this->expensereport->id;
278 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
279 throw new RestException(403);
282 $result = $this->expensereport->fetch(
$id);
284 throw new RestException(404,
'Expense report not found');
288 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
290 $this->expensereport->fetch_lines();
292 foreach ($this->expensereport->lines as $line) {
316 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
317 throw new RestException(403);
322 $result = $this->expensereport->fetch(
$id);
324 throw new RestException(404,
'Expense report not found');
328 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
332 throw new RestException(403,
'Expense report must be in draft status to add lines');
335 $request_data = (object) $request_data;
337 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
339 $result = $this->expensereport->addline(
341 $request_data->value_unit,
342 (
int) $request_data->fk_c_type_fees,
343 $request_data->vatrate,
345 $request_data->comments,
346 $request_data->fk_project,
347 $request_data->fk_c_exp_tax_cat,
349 $request_data->fk_ecm_files
355 throw new RestException(500,
'Error adding line to expense report: '.$this->expensereport->error);
380 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
381 throw new RestException(403);
384 $result = $this->expensereport->fetch(
$id);
386 throw new RestException(404,
'Expense report not found');
390 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
394 throw new RestException(403,
'Expense report must be in draft status to update lines');
398 $result = $line->fetch($lineid);
400 throw new RestException(404,
'Expense report line not found');
403 $request_data = (object) $request_data;
405 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
407 $updateRes = $this->expensereport->updateline(
409 (
int) $request_data->fk_c_type_fees,
410 $request_data->fk_project,
411 $request_data->vatrate,
412 $request_data->comments,
414 $request_data->value_unit,
417 $request_data->fk_c_exp_tax_cat,
418 $request_data->fk_ecm_files
421 if ($updateRes > 0) {
422 $result = $this->
get(
$id);
423 unset($result->line);
426 throw new RestException(500,
'Error updating line: '.$this->expensereport->error);
448 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
449 throw new RestException(403);
452 $result = $this->expensereport->fetch(
$id);
454 throw new RestException(404,
'Expense report not found');
458 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
463 $this->expensereport->fetch_lines();
464 foreach ($this->expensereport->lines as $line) {
465 if ($line->id == $lineid) {
472 throw new RestException(404,
'Line not found');
476 throw new RestException(403,
'Expense report must be in draft status to delete lines');
479 $result = $this->expensereport->deleteLine($lineid);
481 return $this->
get(
$id);
483 throw new RestException(500,
'Error deleting line: '.$this->expensereport->error);
504 public function put(
$id, $request_data =
null)
506 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
507 throw new RestException(403);
510 $result = $this->expensereport->fetch(
$id);
512 throw new RestException(404,
'Expense report not found');
516 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
518 foreach ($request_data as $field => $value) {
519 if ($field ==
'id') {
522 if ($field ===
'caller') {
524 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
528 if ($field ==
'array_options' && is_array($value)) {
529 foreach ($value as $index => $val) {
535 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
538 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
539 return $this->
get(
$id);
541 throw new RestException(500, $this->expensereport->error);
557 public function delete(
$id)
559 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
560 throw new RestException(403);
563 $result = $this->expensereport->fetch(
$id);
565 throw new RestException(404,
'Expense report not found');
569 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
572 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
573 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
579 'message' =>
'Expense Report deleted'
601 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
602 throw new RestException(403,
"Insufficiant rights");
604 $result = $this->expensereport->fetch(
$id);
606 throw new RestException(404,
'Expense report not found');
610 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
615 throw new RestException(304,
'Error nothing done. May be object is already draft');
618 throw new RestException(500,
'Error when setting to draft expense report: '.$this->expensereport->error);
645 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
646 throw new RestException(403,
"Insufficiant rights");
648 $result = $this->expensereport->fetch(
$id);
650 throw new RestException(404,
'Expense report not found');
654 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
657 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
659 throw new RestException(304,
'Error nothing done. May be object is already validated');
662 throw new RestException(500,
'Error when validating expense report: '.$this->expensereport->error);
690 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
691 throw new RestException(403,
"Insufficiant rights");
693 $result = $this->expensereport->fetch(
$id);
695 throw new RestException(404,
'Expense report not found');
699 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
702 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
704 throw new RestException(304,
'Error nothing done. May be object is already approved');
707 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->error);
734 public function deny(
$id, $details, $notrigger = 0)
736 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
737 throw new RestException(403,
"Insufficiant rights");
739 $result = $this->expensereport->fetch(
$id);
741 throw new RestException(404,
'Expense report not found');
745 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
748 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
750 throw new RestException(304,
'Error nothing done. May be object is already denied');
753 throw new RestException(500,
'Error when denying expense report: '.$this->expensereport->error);
782 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'to_paid')) {
783 throw new RestException(403,
"Insufficiant rights");
785 $result = $this->expensereport->fetch(
$id);
787 throw new RestException(404,
'Expense report not found');
791 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
794 $result = $this->expensereport->setPaid(
$id, DolibarrApiAccess::$user, $notrigger);
796 throw new RestException(304,
'Error nothing done. May be object is already approved');
799 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->error);
824 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
825 throw new RestException(403,
"Insufficiant rights");
827 $result = $this->expensereport->fetch(
$id);
829 throw new RestException(404,
'Expense report not found');
833 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
837 throw new RestException(403,
'Expense report already canceled');
839 $result = $this->expensereport->set_cancel(DolibarrApiAccess::$user, $detail, $notrigger);
841 throw new RestException(500,
'Error when cancelling expense report: '.$this->expensereport->error);
844 $result = $this->expensereport->fetch(
$id);
865 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
869 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
870 throw new RestException(403);
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').
')';
877 $sql .= $this->db->order($sortfield, $sortorder);
882 $offset = $limit * $page;
884 $sql .= $this->db->plimit($limit + 1, $offset);
888 $result = $this->db->query($sql);
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);
896 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
901 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
921 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
922 throw new RestException(403);
926 $result = $paymentExpenseReport->fetch($pid);
928 throw new RestException(404,
'paymentExpenseReport not found');
950 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
951 throw new RestException(403);
957 $paymentExpenseReport->fk_expensereport =
$id;
958 foreach ($request_data as $field => $value) {
959 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
962 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
963 throw new RestException(500,
'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
966 $paymentExpenseReport->addPaymentToBank(
967 DolibarrApiAccess::$user,
968 'payment_expensereport',
969 '(ExpenseReportPayment)',
970 (
int) $request_data[
'accountid'],
976 return $paymentExpenseReport->id;
995 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
996 throw new RestException(403);
1000 $result = $paymentExpenseReport->fetch(
$id);
1002 throw new RestException(404,
'payment of expense report not found');
1005 foreach ($request_data as $field => $value) {
1006 if ($field ==
'id') {
1009 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
1012 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
1013 return $this->
get(
$id);
1015 throw new RestException(500, $paymentExpenseReport->error);
1072 unset(
$object->cond_reglement);
1073 unset(
$object->shipping_method_id);
1076 unset(
$object->barcode_type_code);
1077 unset(
$object->barcode_type_label);
1078 unset(
$object->barcode_type_coder);
1080 unset(
$object->code_paiement);
1082 unset(
$object->fk_c_paiement);
1084 unset(
$object->label_incoterms);
1085 unset(
$object->location_incoterms);
1086 unset(
$object->mode_reglement_id);
1087 unset(
$object->cond_reglement_id);
1093 unset(
$object->cond_reglement_id);
1118 if ($data ===
null) {
1121 $expensereport = array();
1122 foreach (ExpenseReports::$FIELDS as $field) {
1123 if (!isset($data[$field])) {
1124 throw new RestException(400,
"$field field missing");
1126 $expensereport[$field] = $data[$field];
1128 return $expensereport;
1140 if ($data ===
null) {
1143 $expensereport = array();
1144 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
1145 if (!isset($data[$field])) {
1146 throw new RestException(400,
"$field field missing");
1148 $expensereport[$field] = $data[$field];
1150 return $expensereport;
1163 if ($data ===
null) {
1166 $expenseReport = array();
1167 foreach (ExpenseReports::$FIELDSLINE as $field) {
1168 if (!isset($data[$field])) {
1169 throw new RestException(400,
"$field field missing");
1171 $expenseReport[$field] = $data[$field];
1173 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.
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.
__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.
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.