20 use Luracast\Restler\RestException;
22 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
36 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 =
'')
107 $sql =
"SELECT t.rowid";
108 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport as t";
109 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
110 if ($user_ids) $sql .=
" AND t.fk_user_author IN (".$user_ids.
")";
116 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
118 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
119 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
122 $sql .= $this->
db->order($sortfield, $sortorder);
127 $offset = $limit * $page;
129 $sql .= $this->
db->plimit($limit + 1, $offset);
132 $result = $this->
db->query($sql);
136 $num = $this->
db->num_rows($result);
137 $min = min($num, ($limit <= 0 ? $num : $limit));
141 $obj = $this->
db->fetch_object($result);
143 if ($expensereport_static->fetch($obj->rowid)) {
149 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->
db->lasterror());
151 if (!count($obj_ret)) {
152 throw new RestException(404,
'No Expense Report found');
163 public function post($request_data = null)
165 if (!DolibarrApiAccess::$user->rights->expensereport->creer) {
166 throw new RestException(401,
"Insuffisant rights");
169 $result = $this->
_validate($request_data);
171 foreach ($request_data as $field => $value) {
172 $this->expensereport->$field = $value;
181 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
182 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
185 return $this->expensereport->id;
391 public function put($id, $request_data = null)
393 if (!DolibarrApiAccess::$user->rights->expensereport->creer) {
394 throw new RestException(401);
397 $result = $this->expensereport->fetch($id);
399 throw new RestException(404,
'expensereport not found');
403 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
405 foreach ($request_data as $field => $value) {
406 if ($field ==
'id')
continue;
407 $this->expensereport->$field = $value;
410 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0)
412 return $this->
get($id);
414 throw new RestException(500, $this->expensereport->error);
425 public function delete($id)
427 if (!DolibarrApiAccess::$user->rights->expensereport->supprimer) {
428 throw new RestException(401);
430 $result = $this->expensereport->fetch($id);
432 throw new RestException(404,
'Expense Report not found');
436 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
439 if (!$this->expensereport->delete(DolibarrApiAccess::$user)) {
440 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
446 'message' =>
'Expense Report deleted'
503 $object = parent::_cleanObjectDatas($object);
505 unset($object->fk_statut);
506 unset($object->statut);
507 unset($object->user);
508 unset($object->thirdparty);
510 unset($object->cond_reglement);
511 unset($object->shipping_method_id);
513 unset($object->barcode_type);
514 unset($object->barcode_type_code);
515 unset($object->barcode_type_label);
516 unset($object->barcode_type_coder);
518 unset($object->code_paiement);
519 unset($object->code_statut);
520 unset($object->fk_c_paiement);
521 unset($object->fk_incoterms);
522 unset($object->label_incoterms);
523 unset($object->location_incoterms);
524 unset($object->mode_reglement_id);
525 unset($object->cond_reglement_id);
527 unset($object->name);
528 unset($object->lastname);
529 unset($object->firstname);
530 unset($object->civility_id);
531 unset($object->cond_reglement_id);
532 unset($object->contact);
533 unset($object->contact_id);
535 unset($object->state);
536 unset($object->state_id);
537 unset($object->state_code);
538 unset($object->country);
539 unset($object->country_id);
540 unset($object->country_code);
542 unset($object->note);
556 $expensereport = array();
557 foreach (ExpenseReports::$FIELDS as $field) {
558 if (!isset($data[$field]))
559 throw new RestException(400,
"$field field missing");
560 $expensereport[$field] = $data[$field];
562 return $expensereport;
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
post($request_data=null)
Create Expense Report object.
Class to manage Trips and Expenses.
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters= '')
List Expense Reports.
put($id, $request_data=null)
Get lines of an Expense Report.
_cleanObjectDatas($object)
Validate an Expense Report.