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();
110 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'')
112 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
113 throw new RestException(403);
121 $sql =
"SELECT t.rowid";
122 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport AS t LEFT JOIN ".MAIN_DB_PREFIX.
"expensereport_extrafields AS ef ON (ef.fk_object = t.rowid)";
123 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
125 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize($user_ids).
")";
133 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
137 $sql .= $this->db->order($sortfield, $sortorder);
142 $offset = $limit * $page;
144 $sql .= $this->db->plimit($limit + 1, $offset);
147 $result = $this->db->query($sql);
150 $num = $this->db->num_rows($result);
151 $min = min($num, ($limit <= 0 ? $num : $limit));
154 $obj = $this->db->fetch_object($result);
156 if ($expensereport_static->fetch($obj->rowid)) {
162 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->db->lasterror());
174 public function post($request_data =
null)
176 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
177 throw new RestException(403,
"Insuffisant rights");
181 $result = $this->
_validate($request_data);
183 foreach ($request_data as $field => $value) {
184 if ($field ===
'caller') {
186 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
190 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
199 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
200 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
203 return $this->expensereport->id;
418 public function put($id, $request_data =
null)
420 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
421 throw new RestException(403);
424 $result = $this->expensereport->fetch($id);
426 throw new RestException(404,
'expensereport not found');
430 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
432 foreach ($request_data as $field => $value) {
433 if ($field ==
'id') {
436 if ($field ===
'caller') {
438 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
442 if ($field ==
'array_options' && is_array($value)) {
443 foreach ($value as $index => $val) {
444 $this->expensereport->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->expensereport);
449 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
452 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
453 return $this->
get($id);
455 throw new RestException(500, $this->expensereport->error);
466 public function delete($id)
468 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
469 throw new RestException(403);
472 $result = $this->expensereport->fetch($id);
474 throw new RestException(404,
'Expense Report not found');
478 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
481 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
482 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
488 'message' =>
'Expense Report deleted'
551 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
555 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
556 throw new RestException(403);
559 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
560 $sql .=
" WHERE e.rowid = t.fk_expensereport";
561 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
563 $sql .= $this->db->order($sortfield, $sortorder);
568 $offset = $limit * $page;
570 $sql .= $this->db->plimit($limit + 1, $offset);
574 $result = $this->db->query($sql);
577 $num = $this->db->num_rows($result);
578 $min = min($num, ($limit <= 0 ? $num : $limit));
579 for ($i = 0; $i < $min; $i++) {
580 $obj = $this->db->fetch_object($result);
582 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
587 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
605 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
606 throw new RestException(403);
610 $result = $paymentExpenseReport->fetch($pid);
612 throw new RestException(404,
'paymentExpenseReport not found');
629 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
630 throw new RestException(403);
636 $paymentExpenseReport->fk_expensereport = $id;
637 foreach ($request_data as $field => $value) {
638 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
641 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
642 throw new RestException(500,
'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
644 if (isModEnabled(
"bank")) {
645 $paymentExpenseReport->addPaymentToBank(
646 DolibarrApiAccess::$user,
647 'payment_expensereport',
648 '(ExpenseReportPayment)',
649 (
int) $request_data[
'accountid'],
655 return $paymentExpenseReport->id;
669 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
670 throw new RestException(403);
674 $result = $paymentExpenseReport->fetch($id);
676 throw new RestException(404,
'payment of expense report not found');
679 foreach ($request_data as $field => $value) {
680 if ($field ==
'id') {
683 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
686 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
687 return $this->
get($id);
689 throw new RestException(500, $paymentExpenseReport->error);
743 unset(
$object->cond_reglement);
744 unset(
$object->shipping_method_id);
747 unset(
$object->barcode_type_code);
748 unset(
$object->barcode_type_label);
749 unset(
$object->barcode_type_coder);
755 unset(
$object->label_incoterms);
756 unset(
$object->location_incoterms);
757 unset(
$object->mode_reglement_id);
758 unset(
$object->cond_reglement_id);
764 unset(
$object->cond_reglement_id);
789 $expensereport = array();
790 foreach (ExpenseReports::$FIELDS as $field) {
791 if (!isset($data[$field])) {
792 throw new RestException(400,
"$field field missing");
794 $expensereport[$field] = $data[$field];
796 return $expensereport;
808 $expensereport = array();
809 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
810 if (!isset($data[$field])) {
811 throw new RestException(400,
"$field field missing");
813 $expensereport[$field] = $data[$field];
815 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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids='', $sqlfilters='', $properties='')
List Expense Reports.
_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.
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.