20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/paymentexpensereport.class.php';
37 public static $FIELDS = array(
44 public static $FIELDSPAYMENT = array(
53 public $expensereport;
77 public function get(
$id)
79 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
80 throw new RestException(403);
83 $result = $this->expensereport->fetch(
$id);
85 throw new RestException(404,
'Expense report not found');
89 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
92 $this->expensereport->fetchObjectLinked();
111 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
113 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
114 throw new RestException(403);
122 $sql =
"SELECT t.rowid";
123 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport AS t LEFT JOIN ".MAIN_DB_PREFIX.
"expensereport_extrafields AS ef ON (ef.fk_object = t.rowid)";
124 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
126 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize($user_ids).
")";
134 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
139 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
141 $sql .= $this->db->order($sortfield, $sortorder);
146 $offset = $limit * $page;
148 $sql .= $this->db->plimit($limit + 1, $offset);
151 $result = $this->db->query($sql);
154 $num = $this->db->num_rows($result);
155 $min = min($num, ($limit <= 0 ? $num : $limit));
158 $obj = $this->db->fetch_object($result);
160 if ($expensereport_static->fetch($obj->rowid)) {
166 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->db->lasterror());
170 if ($pagination_data) {
171 $totalsResult = $this->db->query($sqlTotals);
172 $total = $this->db->fetch_object($totalsResult)->total;
177 $obj_ret[
'data'] = $tmp;
178 $obj_ret[
'pagination'] = [
179 'total' => (int) $total,
181 'page_count' => ceil((
int) $total / $limit),
195 public function post($request_data =
null)
197 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
198 throw new RestException(403,
"Insuffisant rights");
202 $result = $this->
_validate($request_data);
204 foreach ($request_data as $field => $value) {
205 if ($field ===
'caller') {
207 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
211 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
220 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
221 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
224 return $this->expensereport->id;
439 public function put(
$id, $request_data =
null)
441 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
442 throw new RestException(403);
445 $result = $this->expensereport->fetch(
$id);
447 throw new RestException(404,
'expensereport not found');
451 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
453 foreach ($request_data as $field => $value) {
454 if ($field ==
'id') {
457 if ($field ===
'caller') {
459 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
463 if ($field ==
'array_options' && is_array($value)) {
464 foreach ($value as $index => $val) {
465 $this->expensereport->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->expensereport);
470 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
473 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
474 return $this->
get(
$id);
476 throw new RestException(500, $this->expensereport->error);
487 public function delete(
$id)
489 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
490 throw new RestException(403);
493 $result = $this->expensereport->fetch(
$id);
495 throw new RestException(404,
'Expense Report not found');
499 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
502 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
503 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
509 'message' =>
'Expense Report deleted'
572 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
576 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
577 throw new RestException(403);
580 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
581 $sql .=
" WHERE e.rowid = t.fk_expensereport";
582 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
584 $sql .= $this->db->order($sortfield, $sortorder);
589 $offset = $limit * $page;
591 $sql .= $this->db->plimit($limit + 1, $offset);
595 $result = $this->db->query($sql);
598 $num = $this->db->num_rows($result);
599 $min = min($num, ($limit <= 0 ? $num : $limit));
600 for ($i = 0; $i < $min; $i++) {
601 $obj = $this->db->fetch_object($result);
603 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
608 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
626 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
627 throw new RestException(403);
631 $result = $paymentExpenseReport->fetch($pid);
633 throw new RestException(404,
'paymentExpenseReport not found');
650 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
651 throw new RestException(403);
657 $paymentExpenseReport->fk_expensereport =
$id;
658 foreach ($request_data as $field => $value) {
659 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
662 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
663 throw new RestException(500,
'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
665 if (isModEnabled(
"bank")) {
666 $paymentExpenseReport->addPaymentToBank(
667 DolibarrApiAccess::$user,
668 'payment_expensereport',
669 '(ExpenseReportPayment)',
670 (
int) $request_data[
'accountid'],
676 return $paymentExpenseReport->id;
690 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
691 throw new RestException(403);
695 $result = $paymentExpenseReport->fetch(
$id);
697 throw new RestException(404,
'payment of expense report not found');
700 foreach ($request_data as $field => $value) {
701 if ($field ==
'id') {
704 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
707 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
708 return $this->
get(
$id);
710 throw new RestException(500, $paymentExpenseReport->error);
764 unset(
$object->cond_reglement);
765 unset(
$object->shipping_method_id);
768 unset(
$object->barcode_type_code);
769 unset(
$object->barcode_type_label);
770 unset(
$object->barcode_type_coder);
776 unset(
$object->label_incoterms);
777 unset(
$object->location_incoterms);
778 unset(
$object->mode_reglement_id);
779 unset(
$object->cond_reglement_id);
785 unset(
$object->cond_reglement_id);
810 $expensereport = array();
811 foreach (ExpenseReports::$FIELDS as $field) {
812 if (!isset($data[$field])) {
813 throw new RestException(400,
"$field field missing");
815 $expensereport[$field] = $data[$field];
817 return $expensereport;
829 $expensereport = array();
830 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
831 if (!isset($data[$field])) {
832 throw new RestException(400,
"$field field missing");
834 $expensereport[$field] = $data[$field];
836 return $expensereport;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.
getPayments($pid)
Get a given payment.
_cleanObjectDatas($object)
Delete paymentExpenseReport.
_validate($data)
Validate fields before create or update object.
updatePayment($id, $request_data=null)
Update a payment of ExpenseReport.
put($id, $request_data=null)
Get lines of an Expense Report.
addPayment($id, $request_data=null)
Create payment of ExpenseReport.
getAllPayments($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0)
Validate an Expense Report.
post($request_data=null)
Create Expense Report object.
_validatepayment($data)
Validate fields before create or update object.
__construct()
Constructor.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List Expense Reports.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.