22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/paymentexpensereport.class.php';
41 public static $FIELDS = array(
50 public static $FIELDSPAYMENT = array(
59 public $expensereport;
85 public function get(
$id)
87 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
88 throw new RestException(403);
91 $result = $this->expensereport->fetch(
$id);
93 throw new RestException(404,
'Expense report not found');
97 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
100 $this->expensereport->fetchObjectLinked();
125 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
127 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
128 throw new RestException(403);
136 $sql =
"SELECT t.rowid";
137 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport AS t LEFT JOIN ".MAIN_DB_PREFIX.
"expensereport_extrafields AS ef ON (ef.fk_object = t.rowid)";
138 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
140 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize($user_ids).
")";
148 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
153 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
155 $sql .= $this->db->order($sortfield, $sortorder);
160 $offset = $limit * $page;
162 $sql .= $this->db->plimit($limit + 1, $offset);
165 $result = $this->db->query($sql);
168 $num = $this->db->num_rows($result);
169 $min = min($num, ($limit <= 0 ? $num : $limit));
172 $obj = $this->db->fetch_object($result);
174 if ($expensereport_static->fetch($obj->rowid)) {
180 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->db->lasterror());
184 if ($pagination_data) {
185 $totalsResult = $this->db->query($sqlTotals);
186 $total = $this->db->fetch_object($totalsResult)->total;
191 $obj_ret[
'data'] = $tmp;
192 $obj_ret[
'pagination'] = [
193 'total' => (int) $total,
195 'page_count' => ceil((
int) $total / $limit),
215 public function post($request_data =
null)
217 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
218 throw new RestException(403,
"Insuffisant rights");
222 $result = $this->
_validate($request_data);
224 foreach ($request_data as $field => $value) {
225 if ($field ===
'caller') {
227 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
231 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
240 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
241 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
244 return $this->expensereport->id;
469 public function put(
$id, $request_data =
null)
471 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
472 throw new RestException(403);
475 $result = $this->expensereport->fetch(
$id);
477 throw new RestException(404,
'expensereport not found');
481 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
483 foreach ($request_data as $field => $value) {
484 if ($field ==
'id') {
487 if ($field ===
'caller') {
489 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
493 if ($field ==
'array_options' && is_array($value)) {
494 foreach ($value as $index => $val) {
495 $this->expensereport->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->expensereport);
500 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
503 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
504 return $this->
get(
$id);
506 throw new RestException(500, $this->expensereport->error);
522 public function delete(
$id)
524 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
525 throw new RestException(403);
528 $result = $this->expensereport->fetch(
$id);
530 throw new RestException(404,
'Expense Report not found');
534 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
537 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
538 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
544 'message' =>
'Expense Report deleted'
570 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
571 throw new RestException(403,
"Insuffisant rights");
573 $result = $this->expensereport->fetch(
$id);
575 throw new RestException(404,
'Expense report not found');
579 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
582 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
584 throw new RestException(304,
'Error nothing done. May be object is already validated');
587 throw new RestException(500,
'Error when validating expense report: '.$this->expensereport->error);
590 $this->expensereport->fetchObjectLinked();
617 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
618 throw new RestException(403,
"Insuffisant rights");
620 $result = $this->expensereport->fetch(
$id);
622 throw new RestException(404,
'Expense report not found');
626 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
629 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
631 throw new RestException(304,
'Error nothing done. May be object is already approved');
634 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->error);
637 $this->expensereport->fetchObjectLinked();
663 public function deny(
$id, $details, $notrigger = 0)
665 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
666 throw new RestException(403,
"Insuffisant rights");
668 $result = $this->expensereport->fetch(
$id);
670 throw new RestException(404,
'Expense report not found');
674 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
677 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
679 throw new RestException(304,
'Error nothing done. May be object is already denied');
682 throw new RestException(500,
'Error when denying expense report: '.$this->expensereport->error);
685 $this->expensereport->fetchObjectLinked();
708 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
712 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
713 throw new RestException(403);
716 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
717 $sql .=
" WHERE e.rowid = t.fk_expensereport";
718 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
720 $sql .= $this->db->order($sortfield, $sortorder);
725 $offset = $limit * $page;
727 $sql .= $this->db->plimit($limit + 1, $offset);
731 $result = $this->db->query($sql);
734 $num = $this->db->num_rows($result);
735 $min = min($num, ($limit <= 0 ? $num : $limit));
736 for ($i = 0; $i < $min; $i++) {
737 $obj = $this->db->fetch_object($result);
739 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
744 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
764 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
765 throw new RestException(403);
769 $result = $paymentExpenseReport->fetch($pid);
771 throw new RestException(404,
'paymentExpenseReport not found');
793 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
794 throw new RestException(403);
800 $paymentExpenseReport->fk_expensereport =
$id;
801 foreach ($request_data as $field => $value) {
802 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
805 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
806 throw new RestException(500,
'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
808 if (isModEnabled(
"bank")) {
809 $paymentExpenseReport->addPaymentToBank(
810 DolibarrApiAccess::$user,
811 'payment_expensereport',
812 '(ExpenseReportPayment)',
813 (
int) $request_data[
'accountid'],
819 return $paymentExpenseReport->id;
838 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
839 throw new RestException(403);
843 $result = $paymentExpenseReport->fetch(
$id);
845 throw new RestException(404,
'payment of expense report not found');
848 foreach ($request_data as $field => $value) {
849 if ($field ==
'id') {
852 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
855 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
856 return $this->
get(
$id);
858 throw new RestException(500, $paymentExpenseReport->error);
912 unset(
$object->cond_reglement);
913 unset(
$object->shipping_method_id);
916 unset(
$object->barcode_type_code);
917 unset(
$object->barcode_type_label);
918 unset(
$object->barcode_type_coder);
924 unset(
$object->label_incoterms);
925 unset(
$object->location_incoterms);
926 unset(
$object->mode_reglement_id);
927 unset(
$object->cond_reglement_id);
933 unset(
$object->cond_reglement_id);
958 if ($data ===
null) {
961 $expensereport = array();
962 foreach (ExpenseReports::$FIELDS as $field) {
963 if (!isset($data[$field])) {
964 throw new RestException(400,
"$field field missing");
966 $expensereport[$field] = $data[$field];
968 return $expensereport;
980 if ($data ===
null) {
983 $expensereport = array();
984 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
985 if (!isset($data[$field])) {
986 throw new RestException(400,
"$field field missing");
988 $expensereport[$field] = $data[$field];
990 return $expensereport;
$id
Support class for third parties, contacts, members, users or resources.
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 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.
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)
Get lines of 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.
__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.