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;
97 public function get(
$id)
99 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
100 throw new RestException(403);
103 $result = $this->expensereport->fetch(
$id);
105 throw new RestException(404,
'Expense report not found');
109 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
112 $this->expensereport->fetchObjectLinked();
137 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
139 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
140 throw new RestException(403);
148 $sql =
"SELECT t.rowid";
149 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport AS t LEFT JOIN ".MAIN_DB_PREFIX.
"expensereport_extrafields AS ef ON (ef.fk_object = t.rowid)";
150 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
152 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize($user_ids).
")";
160 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
165 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
167 $sql .= $this->db->order($sortfield, $sortorder);
172 $offset = $limit * $page;
174 $sql .= $this->db->plimit($limit + 1, $offset);
177 $result = $this->db->query($sql);
180 $num = $this->db->num_rows($result);
181 $min = min($num, ($limit <= 0 ? $num : $limit));
184 $obj = $this->db->fetch_object($result);
186 if ($expensereport_static->fetch($obj->rowid)) {
192 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->db->lasterror());
196 if ($pagination_data) {
197 $totalsResult = $this->db->query($sqlTotals);
198 $total = $this->db->fetch_object($totalsResult)->total;
203 $obj_ret[
'data'] = $tmp;
204 $obj_ret[
'pagination'] = [
205 'total' => (int) $total,
207 'page_count' => ceil((
int) $total / $limit),
227 public function post($request_data =
null)
229 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
230 throw new RestException(403,
"Insufficiant rights");
234 $result = $this->
_validate($request_data);
236 foreach ($request_data as $field => $value) {
237 if ($field ===
'caller') {
239 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
243 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
252 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
253 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
256 return $this->expensereport->id;
277 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
278 throw new RestException(403);
281 $result = $this->expensereport->fetch(
$id);
283 throw new RestException(404,
'Expense report not found');
287 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
289 $this->expensereport->fetch_lines();
291 foreach ($this->expensereport->lines as $line) {
315 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
316 throw new RestException(403);
321 $result = $this->expensereport->fetch(
$id);
323 throw new RestException(404,
'Expense report not found');
327 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
331 throw new RestException(403,
'Expense report must be in draft status to add lines');
334 $request_data = (object) $request_data;
336 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
338 $result = $this->expensereport->addline(
340 $request_data->value_unit,
341 (
int) $request_data->fk_c_type_fees,
342 $request_data->vatrate,
344 $request_data->comments,
345 $request_data->fk_project,
346 $request_data->fk_c_exp_tax_cat,
348 $request_data->fk_ecm_files
354 throw new RestException(500,
'Error adding line to expense report: '.$this->expensereport->error);
379 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
380 throw new RestException(403);
383 $result = $this->expensereport->fetch(
$id);
385 throw new RestException(404,
'Expense report not found');
389 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
393 throw new RestException(403,
'Expense report must be in draft status to update lines');
397 $result = $line->fetch($lineid);
399 throw new RestException(404,
'Expense report line not found');
402 $request_data = (object) $request_data;
404 $request_data->comments =
sanitizeVal($request_data->comments,
'restricthtml');
406 $updateRes = $this->expensereport->updateline(
408 (
int) $request_data->fk_c_type_fees,
409 $request_data->fk_project,
410 $request_data->vatrate,
411 $request_data->comments,
413 $request_data->value_unit,
416 $request_data->fk_c_exp_tax_cat,
417 $request_data->fk_ecm_files
420 if ($updateRes > 0) {
421 $result = $this->
get(
$id);
422 unset($result->line);
425 throw new RestException(500,
'Error updating line: '.$this->expensereport->error);
447 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
448 throw new RestException(403);
451 $result = $this->expensereport->fetch(
$id);
453 throw new RestException(404,
'Expense report not found');
457 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
462 $this->expensereport->fetch_lines();
463 foreach ($this->expensereport->lines as $line) {
464 if ($line->id == $lineid) {
471 throw new RestException(404,
'Line not found');
475 throw new RestException(403,
'Expense report must be in draft status to delete lines');
478 $result = $this->expensereport->deleteLine($lineid);
480 return $this->
get(
$id);
482 throw new RestException(500,
'Error deleting line: '.$this->expensereport->error);
503 public function put(
$id, $request_data =
null)
505 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
506 throw new RestException(403);
509 $result = $this->expensereport->fetch(
$id);
511 throw new RestException(404,
'Expense report not found');
515 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
517 foreach ($request_data as $field => $value) {
518 if ($field ==
'id') {
521 if ($field ===
'caller') {
523 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
527 if ($field ==
'array_options' && is_array($value)) {
528 foreach ($value as $index => $val) {
529 $this->expensereport->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->expensereport);
534 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
537 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
538 return $this->
get(
$id);
540 throw new RestException(500, $this->expensereport->error);
556 public function delete(
$id)
558 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
559 throw new RestException(403);
562 $result = $this->expensereport->fetch(
$id);
564 throw new RestException(404,
'Expense report not found');
568 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
571 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
572 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
578 'message' =>
'Expense Report deleted'
600 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
601 throw new RestException(403,
"Insufficiant rights");
603 $result = $this->expensereport->fetch(
$id);
605 throw new RestException(404,
'Expense report not found');
609 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
612 $result = $this->expensereport->setStatut(0);
614 throw new RestException(304,
'Error nothing done. May be object is already draft');
617 throw new RestException(500,
'Error when setting to draft expense report: '.$this->expensereport->error);
644 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
645 throw new RestException(403,
"Insufficiant rights");
647 $result = $this->expensereport->fetch(
$id);
649 throw new RestException(404,
'Expense report not found');
653 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
656 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
658 throw new RestException(304,
'Error nothing done. May be object is already validated');
661 throw new RestException(500,
'Error when validating expense report: '.$this->expensereport->error);
689 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
690 throw new RestException(403,
"Insufficiant rights");
692 $result = $this->expensereport->fetch(
$id);
694 throw new RestException(404,
'Expense report not found');
698 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
701 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
703 throw new RestException(304,
'Error nothing done. May be object is already approved');
706 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->error);
733 public function deny(
$id, $details, $notrigger = 0)
735 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
736 throw new RestException(403,
"Insufficiant rights");
738 $result = $this->expensereport->fetch(
$id);
740 throw new RestException(404,
'Expense report not found');
744 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
747 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
749 throw new RestException(304,
'Error nothing done. May be object is already denied');
752 throw new RestException(500,
'Error when denying expense report: '.$this->expensereport->error);
779 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
780 throw new RestException(403,
"Insufficiant rights");
782 $result = $this->expensereport->fetch(
$id);
784 throw new RestException(404,
'Expense report not found');
788 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
792 throw new RestException(403,
'Expense report already canceled');
794 $result = $this->expensereport->set_cancel(DolibarrApiAccess::$user, $detail, $notrigger);
796 throw new RestException(500,
'Error when cancelling expense report: '.$this->expensereport->error);
799 $result = $this->expensereport->fetch(
$id);
820 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
824 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
825 throw new RestException(403);
828 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
829 $sql .=
" WHERE e.rowid = t.fk_expensereport";
830 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
832 $sql .= $this->db->order($sortfield, $sortorder);
837 $offset = $limit * $page;
839 $sql .= $this->db->plimit($limit + 1, $offset);
843 $result = $this->db->query($sql);
846 $num = $this->db->num_rows($result);
847 $min = min($num, ($limit <= 0 ? $num : $limit));
848 for ($i = 0; $i < $min; $i++) {
849 $obj = $this->db->fetch_object($result);
851 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
856 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
876 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
877 throw new RestException(403);
881 $result = $paymentExpenseReport->fetch($pid);
883 throw new RestException(404,
'paymentExpenseReport not found');
905 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
906 throw new RestException(403);
912 $paymentExpenseReport->fk_expensereport =
$id;
913 foreach ($request_data as $field => $value) {
914 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
917 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
918 throw new RestException(500,
'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
921 $paymentExpenseReport->addPaymentToBank(
922 DolibarrApiAccess::$user,
923 'payment_expensereport',
924 '(ExpenseReportPayment)',
925 (
int) $request_data[
'accountid'],
931 return $paymentExpenseReport->id;
950 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
951 throw new RestException(403);
955 $result = $paymentExpenseReport->fetch(
$id);
957 throw new RestException(404,
'payment of expense report not found');
960 foreach ($request_data as $field => $value) {
961 if ($field ==
'id') {
964 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
967 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
968 return $this->
get(
$id);
970 throw new RestException(500, $paymentExpenseReport->error);
1027 unset(
$object->cond_reglement);
1028 unset(
$object->shipping_method_id);
1031 unset(
$object->barcode_type_code);
1032 unset(
$object->barcode_type_label);
1033 unset(
$object->barcode_type_coder);
1035 unset(
$object->code_paiement);
1037 unset(
$object->fk_c_paiement);
1039 unset(
$object->label_incoterms);
1040 unset(
$object->location_incoterms);
1041 unset(
$object->mode_reglement_id);
1042 unset(
$object->cond_reglement_id);
1048 unset(
$object->cond_reglement_id);
1073 if ($data ===
null) {
1076 $expensereport = array();
1077 foreach (ExpenseReports::$FIELDS as $field) {
1078 if (!isset($data[$field])) {
1079 throw new RestException(400,
"$field field missing");
1081 $expensereport[$field] = $data[$field];
1083 return $expensereport;
1095 if ($data ===
null) {
1098 $expensereport = array();
1099 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
1100 if (!isset($data[$field])) {
1101 throw new RestException(400,
"$field field missing");
1103 $expensereport[$field] = $data[$field];
1105 return $expensereport;
1118 if ($data ===
null) {
1121 $expenseReport = array();
1122 foreach (ExpenseReports::$FIELDSLINE as $field) {
1123 if (!isset($data[$field])) {
1124 throw new RestException(400,
"$field field missing");
1126 $expenseReport[$field] = $data[$field];
1128 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_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.
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)
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.