23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
41 public static $FIELDS = array(
61 $this->holiday =
new Holiday($this->db);
76 public function get(
$id)
78 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read')) {
79 throw new RestException(403);
82 $result = $this->holiday->fetch(
$id);
84 throw new RestException(404,
'Leave not found');
88 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
91 $this->holiday->fetchObjectLinked();
114 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
118 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'readall')) {
119 throw new RestException(403);
127 $sql =
"SELECT t.rowid";
128 $sql .=
" FROM ".MAIN_DB_PREFIX.
"holiday AS t LEFT JOIN ".MAIN_DB_PREFIX.
"holiday_extrafields AS ef ON (ef.fk_object = t.rowid)";
129 $sql .=
' WHERE t.entity IN ('.getEntity(
'holiday').
')';
131 $sql .=
" AND t.fk_user IN (".$this->db->sanitize($user_ids).
")";
139 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
144 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
146 $sql .= $this->db->order($sortfield, $sortorder);
151 $offset = $limit * $page;
153 $sql .= $this->db->plimit($limit + 1, $offset);
156 $result = $this->db->query($sql);
159 $num = $this->db->num_rows($result);
160 $min = min($num, ($limit <= 0 ? $num : $limit));
163 $obj = $this->db->fetch_object($result);
164 $holiday_static =
new Holiday($this->db);
165 if ($holiday_static->fetch($obj->rowid)) {
171 throw new RestException(503,
'Error when retrieve Leave list : '.$this->db->lasterror());
175 if ($pagination_data) {
176 $totalsResult = $this->db->query($sqlTotals);
177 $total = $this->db->fetch_object($totalsResult)->total;
182 $obj_ret[
'data'] = $tmp;
183 $obj_ret[
'pagination'] = [
184 'total' => (int) $total,
186 'page_count' => ceil((
int) $total / $limit),
206 public function post($request_data =
null)
208 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
209 throw new RestException(403,
"Insufficiant rights");
213 $result = $this->
_validate($request_data);
215 foreach ($request_data as $field => $value) {
216 if ($field ===
'caller') {
218 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
222 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
231 if ($this->holiday->create(DolibarrApiAccess::$user) < 0) {
232 throw new RestException(500,
"Error creating holiday", array_merge(array($this->holiday->error), $this->holiday->errors));
235 return $this->holiday->id;
256 public function put(
$id, $request_data =
null)
258 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
259 throw new RestException(403);
262 $result = $this->holiday->fetch(
$id);
264 throw new RestException(404,
'Leave not found');
268 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
270 foreach ($request_data as $field => $value) {
271 if ($field ==
'id') {
274 if ($field ===
'caller') {
276 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
280 if ($field ==
'array_options' && is_array($value)) {
281 foreach ($value as $index => $val) {
282 $this->holiday->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->holiday);
287 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
290 if ($this->holiday->update(DolibarrApiAccess::$user) > 0) {
291 return $this->
get(
$id);
293 throw new RestException(500, $this->holiday->error);
309 public function delete(
$id)
311 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'delete')) {
312 throw new RestException(403);
315 $result = $this->holiday->fetch(
$id);
317 throw new RestException(404,
'Leave not found');
321 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
324 if (!$this->holiday->delete(DolibarrApiAccess::$user)) {
325 throw new RestException(500,
'Error when deleting Leave : '.$this->holiday->error);
331 'message' =>
'Leave deleted'
357 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
358 throw new RestException(403,
"Insufficiant rights");
360 $result = $this->holiday->fetch(
$id);
362 throw new RestException(404,
'Leave not found');
366 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
370 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
372 throw new RestException(304,
'Error nothing done. May be object is already validated');
375 throw new RestException(500,
'Error when validating leave: '.$this->holiday->error);
403 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
404 throw new RestException(403,
"Insufficiant rights");
406 $result = $this->holiday->fetch(
$id);
408 throw new RestException(404,
'Leave not found');
412 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
416 $result = $this->holiday->approve(DolibarrApiAccess::$user, $notrigger);
418 throw new RestException(304,
'Error nothing done. May be object is already approved');
421 throw new RestException(500,
'Error when approving expense report: '.$this->holiday->error);
448 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
449 throw new RestException(403,
"Insufficient rights");
452 $result = $this->holiday->fetch(
$id);
454 throw new RestException(404,
'Leave not found');
458 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
462 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
464 throw new RestException(304,
'Error nothing done. May be object is already canceled');
467 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
493 public function refuse(
$id, $detail_refuse, $notrigger = 0)
495 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
496 throw new RestException(403,
"Insufficient rights");
499 $result = $this->holiday->fetch(
$id);
501 throw new RestException(404,
'Leave not found');
505 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
509 $this->holiday->detail_refuse = $detail_refuse;
510 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
512 throw new RestException(304,
'Error nothing done. May be object is already refused');
515 throw new RestException(500,
'Error when refusing holiday: '.$this->holiday->error);
545 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
546 throw new RestException(403,
"Insufficient rights");
549 $result = $this->holiday->fetch(
$id);
551 throw new RestException(404,
'Leave not found');
555 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
560 throw new RestException(400,
'Holiday is not canceled. Only canceled holidays can be reopened.');
563 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
565 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
592 unset(
$object->cond_reglement);
593 unset(
$object->shipping_method_id);
596 unset(
$object->barcode_type_code);
597 unset(
$object->barcode_type_label);
598 unset(
$object->barcode_type_coder);
600 unset(
$object->mode_reglement_id);
601 unset(
$object->cond_reglement_id);
607 unset(
$object->cond_reglement_id);
625 unset(
$object->totalpaid_multicurrency);
641 if ($data ===
null) {
644 $expensereport = array();
645 foreach (self::$FIELDS as $field) {
646 if (!isset($data[$field])) {
647 throw new RestException(400,
"$field field missing");
649 $expensereport[$field] = $data[$field];
651 return $expensereport;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $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.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
Class of the module paid holiday.
const STATUS_VALIDATED
Validated status.
const STATUS_REFUSED
Refused.
const STATUS_CANCELED
Canceled.
const STATUS_APPROVED
Approved.
_validate($data)
Validate fields before create or update object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List leaves.
put($id, $request_data=null)
Update expense report general fields.
validate($id, $notrigger=0)
Validate a holiday.
refuse($id, $detail_refuse, $notrigger=0)
Refuse a holiday.
__construct()
Constructor.
cancel($id, $notrigger=0)
Cancel a holiday.
reopen($id, $notrigger=0)
Reopen a canceled holiday.
approve($id, $notrigger=0)
Approve a leave.
post($request_data=null)
Create a leave.
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.