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 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
445 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
446 return $this->
get($id);
448 throw new RestException(500, $this->expensereport->error);
459 public function delete($id)
461 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
462 throw new RestException(403);
465 $result = $this->expensereport->fetch($id);
467 throw new RestException(404,
'Expense Report not found');
471 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
474 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
475 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
481 'message' =>
'Expense Report deleted'
544 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
548 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
549 throw new RestException(403);
552 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
553 $sql .=
" WHERE e.rowid = t.fk_expensereport";
554 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
556 $sql .= $this->db->order($sortfield, $sortorder);
561 $offset = $limit * $page;
563 $sql .= $this->db->plimit($limit + 1, $offset);
567 $result = $this->db->query($sql);
570 $num = $this->db->num_rows($result);
571 $min = min($num, ($limit <= 0 ? $num : $limit));
572 for ($i = 0; $i < $min; $i++) {
573 $obj = $this->db->fetch_object($result);
575 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
580 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
598 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
599 throw new RestException(403);
603 $result = $paymentExpenseReport->fetch($pid);
605 throw new RestException(404,
'paymentExpenseReport not found');
622 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
623 throw new RestException(403);
629 $paymentExpenseReport->fk_expensereport = $id;
630 foreach ($request_data as $field => $value) {
631 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
634 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
635 throw new RestException(500,
'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
637 if (isModEnabled(
"bank")) {
638 $paymentExpenseReport->addPaymentToBank(
639 DolibarrApiAccess::$user,
640 'payment_expensereport',
641 '(ExpenseReportPayment)',
642 (
int) $request_data[
'accountid'],
648 return $paymentExpenseReport->id;
662 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
663 throw new RestException(403);
667 $result = $paymentExpenseReport->fetch($id);
669 throw new RestException(404,
'payment of expense report not found');
672 foreach ($request_data as $field => $value) {
673 if ($field ==
'id') {
676 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
679 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
680 return $this->
get($id);
682 throw new RestException(500, $paymentExpenseReport->error);
736 unset(
$object->cond_reglement);
737 unset(
$object->shipping_method_id);
740 unset(
$object->barcode_type_code);
741 unset(
$object->barcode_type_label);
742 unset(
$object->barcode_type_coder);
748 unset(
$object->label_incoterms);
749 unset(
$object->location_incoterms);
750 unset(
$object->mode_reglement_id);
751 unset(
$object->cond_reglement_id);
757 unset(
$object->cond_reglement_id);
782 $expensereport = array();
783 foreach (ExpenseReports::$FIELDS as $field) {
784 if (!isset($data[$field])) {
785 throw new RestException(400,
"$field field missing");
787 $expensereport[$field] = $data[$field];
789 return $expensereport;
801 $expensereport = array();
802 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
803 if (!isset($data[$field])) {
804 throw new RestException(400,
"$field field missing");
806 $expensereport[$field] = $data[$field];
808 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.