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 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
466 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
467 return $this->
get(
$id);
469 throw new RestException(500, $this->expensereport->error);
480 public function delete(
$id)
482 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
483 throw new RestException(403);
486 $result = $this->expensereport->fetch(
$id);
488 throw new RestException(404,
'Expense Report not found');
492 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
496 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
502 'message' =>
'Expense Report deleted'
565 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
569 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
570 throw new RestException(403);
573 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
574 $sql .=
" WHERE e.rowid = t.fk_expensereport";
575 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
577 $sql .= $this->db->order($sortfield, $sortorder);
582 $offset = $limit * $page;
584 $sql .= $this->db->plimit($limit + 1, $offset);
588 $result = $this->db->query($sql);
591 $num = $this->db->num_rows($result);
592 $min = min($num, ($limit <= 0 ? $num : $limit));
593 for ($i = 0; $i < $min; $i++) {
594 $obj = $this->db->fetch_object($result);
596 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
601 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
619 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
620 throw new RestException(403);
624 $result = $paymentExpenseReport->fetch($pid);
626 throw new RestException(404,
'paymentExpenseReport not found');
643 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
644 throw new RestException(403);
650 $paymentExpenseReport->fk_expensereport =
$id;
651 foreach ($request_data as $field => $value) {
652 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
655 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
656 throw new RestException(500,
'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
658 if (isModEnabled(
"bank")) {
659 $paymentExpenseReport->addPaymentToBank(
660 DolibarrApiAccess::$user,
661 'payment_expensereport',
662 '(ExpenseReportPayment)',
663 (
int) $request_data[
'accountid'],
669 return $paymentExpenseReport->id;
683 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
684 throw new RestException(403);
688 $result = $paymentExpenseReport->fetch(
$id);
690 throw new RestException(404,
'payment of expense report not found');
693 foreach ($request_data as $field => $value) {
694 if ($field ==
'id') {
697 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
700 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
701 return $this->
get(
$id);
703 throw new RestException(500, $paymentExpenseReport->error);
757 unset(
$object->cond_reglement);
758 unset(
$object->shipping_method_id);
761 unset(
$object->barcode_type_code);
762 unset(
$object->barcode_type_label);
763 unset(
$object->barcode_type_coder);
769 unset(
$object->label_incoterms);
770 unset(
$object->location_incoterms);
771 unset(
$object->mode_reglement_id);
772 unset(
$object->cond_reglement_id);
778 unset(
$object->cond_reglement_id);
803 $expensereport = array();
804 foreach (ExpenseReports::$FIELDS as $field) {
805 if (!isset($data[$field])) {
806 throw new RestException(400,
"$field field missing");
808 $expensereport[$field] = $data[$field];
810 return $expensereport;
822 $expensereport = array();
823 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
824 if (!isset($data[$field])) {
825 throw new RestException(400,
"$field field missing");
827 $expensereport[$field] = $data[$field];
829 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.