23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
41 public static $FIELDS = array(
53 public static $FIELDS_FORBIDDEN_FOR_API = array(
80 $this->holiday =
new Holiday($this->db);
95 public function get(
$id)
97 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read')) {
98 throw new RestException(403);
101 $result = $this->holiday->fetch(
$id);
103 throw new RestException(404,
'Leave not found');
107 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
110 $this->holiday->fetchObjectLinked();
133 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
135 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read') && !DolibarrApiAccess::$user->hasRight(
'holiday',
'readall')) {
136 throw new RestException(403);
144 $sql =
"SELECT t.rowid";
145 $sql .=
" FROM ".MAIN_DB_PREFIX.
"holiday AS t LEFT JOIN ".MAIN_DB_PREFIX.
"holiday_extrafields AS ef ON (ef.fk_object = t.rowid)";
146 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user AS u ON t.fk_user = u.rowid";
147 $sql .=
' WHERE t.entity IN ('.getEntity(
'holiday').
')';
149 $sql .=
" AND t.fk_user IN (".$this->db->sanitize($user_ids).
")";
151 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'readall')) {
152 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
153 $sql .=
" AND t.fk_user IN (".$this->db->sanitize(implode(
',', $childids)).
")";
161 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
166 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
168 $sql .= $this->db->order($sortfield, $sortorder);
173 $offset = $limit * $page;
175 $sql .= $this->db->plimit($limit + 1, $offset);
178 $result = $this->db->query($sql);
181 $num = $this->db->num_rows($result);
182 $min = min($num, ($limit <= 0 ? $num : $limit));
185 $obj = $this->db->fetch_object($result);
186 $holiday_static =
new Holiday($this->db);
187 if ($holiday_static->fetch($obj->rowid)) {
193 throw new RestException(503,
'Error when retrieve Leave list : '.$this->db->lasterror());
197 if ($pagination_data) {
198 $totalsResult = $this->db->query($sqlTotals);
199 $total = $this->db->fetch_object($totalsResult)->total;
204 $obj_ret[
'data'] = $tmp;
205 $obj_ret[
'pagination'] = [
206 'total' => (int) $total,
208 'page_count' => ceil((
int) $total / $limit),
228 public function post($request_data =
null)
230 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
231 throw new RestException(403,
"Insufficiant rights");
235 $result = $this->
_validate($request_data);
237 foreach ($request_data as $field => $value) {
238 if ($field ===
'caller') {
240 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
243 if (in_array($field, self::$FIELDS_FORBIDDEN_FOR_API) && $field !==
'fk_validator') {
244 throw new RestException(400,
"Field '".$field.
"' is not allowed in create endpoint. Use dedicated routes (validate, approve, refuse, cancel, reopen) to change the workflow status.");
247 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
256 if ($this->holiday->create(DolibarrApiAccess::$user) < 0) {
257 throw new RestException(500,
"Error creating holiday", array_merge(array($this->holiday->error), $this->holiday->errors));
260 return $this->holiday->id;
281 public function put(
$id, $request_data =
null)
283 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
284 throw new RestException(403);
287 $result = $this->holiday->fetch(
$id);
289 throw new RestException(404,
'Leave not found');
293 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
296 if (!is_array($request_data)) {
297 $request_data = array();
300 foreach ($request_data as $field => $value) {
301 if ($field ==
'id') {
304 if ($field ===
'caller') {
306 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
309 if (in_array($field, self::$FIELDS_FORBIDDEN_FOR_API)) {
310 throw new RestException(400,
"Field '".$field.
"' is not allowed in update endpoint. Use dedicated routes (validate, approve, refuse, cancel, reopen) to change the workflow status.");
313 if ($field ==
'array_options' && is_array($value)) {
314 foreach ($value as $index => $val) {
320 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
323 if ($this->holiday->update(DolibarrApiAccess::$user) > 0) {
324 return $this->
get(
$id);
326 throw new RestException(500, $this->holiday->error);
342 public function delete(
$id)
344 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'delete')) {
345 throw new RestException(403);
348 $result = $this->holiday->fetch(
$id);
350 throw new RestException(404,
'Leave not found');
354 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
357 if (!$this->holiday->delete(DolibarrApiAccess::$user)) {
358 throw new RestException(500,
'Error when deleting Leave : '.$this->holiday->error);
364 'message' =>
'Leave deleted'
390 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
391 throw new RestException(403,
"Insufficiant rights");
393 $result = $this->holiday->fetch(
$id);
395 throw new RestException(404,
'Leave not found');
399 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
403 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
405 throw new RestException(304,
'Error nothing done. May be object is already validated');
408 throw new RestException(500,
'Error when validating leave: '.$this->holiday->error);
436 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
437 throw new RestException(403,
"Insufficiant rights");
439 $result = $this->holiday->fetch(
$id);
441 throw new RestException(404,
'Leave not found');
445 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
449 $result = $this->holiday->approve(DolibarrApiAccess::$user, $notrigger);
451 throw new RestException(304,
'Error nothing done. May be object is already approved');
454 throw new RestException(500,
'Error when approving holiday: '.$this->holiday->error);
481 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
482 throw new RestException(403,
"Insufficient rights");
485 $result = $this->holiday->fetch(
$id);
487 throw new RestException(404,
'Leave not found');
491 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
497 throw new RestException(304,
'Error nothing done. May be object is already canceled');
500 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
526 public function refuse(
$id, $detail_refuse, $notrigger = 0)
528 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
529 throw new RestException(403,
"Insufficient rights");
532 $result = $this->holiday->fetch(
$id);
534 throw new RestException(404,
'Leave not found');
538 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
542 $this->holiday->detail_refuse = $detail_refuse;
543 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
545 throw new RestException(304,
'Error nothing done. May be object is already refused');
548 throw new RestException(500,
'Error when refusing holiday: '.$this->holiday->error);
578 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
579 throw new RestException(403,
"Insufficient rights");
582 $result = $this->holiday->fetch(
$id);
584 throw new RestException(404,
'Leave not found');
588 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
593 throw new RestException(400,
'Holiday is not canceled. Only canceled holidays can be reopened.');
596 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
598 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
625 unset(
$object->cond_reglement);
626 unset(
$object->shipping_method_id);
629 unset(
$object->barcode_type_code);
630 unset(
$object->barcode_type_label);
631 unset(
$object->barcode_type_coder);
633 unset(
$object->mode_reglement_id);
634 unset(
$object->cond_reglement_id);
640 unset(
$object->cond_reglement_id);
658 unset(
$object->totalpaid_multicurrency);
674 if ($data ===
null) {
678 foreach (self::$FIELDS as $field) {
679 if (!isset($data[$field])) {
680 throw new RestException(400,
"$field field missing");
682 $holiday[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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 holiday 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, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.