20 use Luracast\Restler\RestException;
22 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
36 public static $FIELDS = array(
43 public $expensereport;
66 public function get($id)
68 if (!DolibarrApiAccess::$user->rights->expensereport->lire) {
69 throw new RestException(401);
72 $result = $this->expensereport->fetch($id);
74 throw new RestException(404,
'Expense report not found');
78 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 $this->expensereport->fetchObjectLinked();
98 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters =
'')
102 if (!DolibarrApiAccess::$user->rights->expensereport->lire) {
103 throw new RestException(401);
111 $sql =
"SELECT t.rowid";
112 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport as t";
113 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
115 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize($user_ids).
")";
122 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
124 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
125 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
128 $sql .= $this->
db->order($sortfield, $sortorder);
133 $offset = $limit * $page;
135 $sql .= $this->
db->plimit($limit + 1, $offset);
138 $result = $this->
db->query($sql);
141 $num = $this->
db->num_rows($result);
142 $min = min($num, ($limit <= 0 ? $num : $limit));
145 $obj = $this->
db->fetch_object($result);
147 if ($expensereport_static->fetch($obj->rowid)) {
153 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->
db->lasterror());
155 if (!count($obj_ret)) {
156 throw new RestException(404,
'No Expense Report found');
167 public function post($request_data =
null)
169 if (!DolibarrApiAccess::$user->rights->expensereport->creer) {
170 throw new RestException(401,
"Insuffisant rights");
174 $result = $this->
_validate($request_data);
176 foreach ($request_data as $field => $value) {
177 $this->expensereport->$field = $value;
186 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
187 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
190 return $this->expensereport->id;
406 public function put($id, $request_data =
null)
408 if (!DolibarrApiAccess::$user->rights->expensereport->creer) {
409 throw new RestException(401);
412 $result = $this->expensereport->fetch($id);
414 throw new RestException(404,
'expensereport not found');
418 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
420 foreach ($request_data as $field => $value) {
421 if ($field ==
'id') {
424 $this->expensereport->$field = $value;
427 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
428 return $this->
get($id);
430 throw new RestException(500, $this->expensereport->error);
441 public function delete($id)
443 if (!DolibarrApiAccess::$user->rights->expensereport->supprimer) {
444 throw new RestException(401);
447 $result = $this->expensereport->fetch($id);
449 throw new RestException(404,
'Expense Report not found');
453 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
456 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
457 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
463 'message' =>
'Expense Report deleted'
521 $object = parent::_cleanObjectDatas($object);
523 unset($object->fk_statut);
524 unset($object->statut);
525 unset($object->user);
526 unset($object->thirdparty);
528 unset($object->cond_reglement);
529 unset($object->shipping_method_id);
531 unset($object->barcode_type);
532 unset($object->barcode_type_code);
533 unset($object->barcode_type_label);
534 unset($object->barcode_type_coder);
536 unset($object->code_paiement);
537 unset($object->code_statut);
538 unset($object->fk_c_paiement);
539 unset($object->fk_incoterms);
540 unset($object->label_incoterms);
541 unset($object->location_incoterms);
542 unset($object->mode_reglement_id);
543 unset($object->cond_reglement_id);
545 unset($object->name);
546 unset($object->lastname);
547 unset($object->firstname);
548 unset($object->civility_id);
549 unset($object->cond_reglement_id);
550 unset($object->contact);
551 unset($object->contact_id);
553 unset($object->state);
554 unset($object->state_id);
555 unset($object->state_code);
556 unset($object->country);
557 unset($object->country_id);
558 unset($object->country_code);
560 unset($object->note);
574 $expensereport = array();
575 foreach (ExpenseReports::$FIELDS as $field) {
576 if (!isset($data[$field])) {
577 throw new RestException(400,
"$field field missing");
579 $expensereport[$field] = $data[$field];
581 return $expensereport;
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Class to manage Trips and Expenses.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters='')
List Expense Reports.
_cleanObjectDatas($object)
Validate an Expense Report.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Get lines of an Expense Report.
post($request_data=null)
Create Expense Report object.
__construct()
Constructor.
$conf db
API class for accounts.