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)
116 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read') && !DolibarrApiAccess::$user->hasRight(
'holiday',
'readall')) {
117 throw new RestException(403);
125 $sql =
"SELECT t.rowid";
126 $sql .=
" FROM ".MAIN_DB_PREFIX.
"holiday AS t LEFT JOIN ".MAIN_DB_PREFIX.
"holiday_extrafields AS ef ON (ef.fk_object = t.rowid)";
127 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user AS u ON t.fk_user = u.rowid";
128 $sql .=
' WHERE t.entity IN ('.getEntity(
'holiday').
')';
130 $sql .=
" AND t.fk_user IN (".$this->db->sanitize($user_ids).
")";
132 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'readall')) {
133 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
134 $sql .=
" AND t.fk_user IN (".$this->db->sanitize(implode(
',', $childids)).
")";
140 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
142 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
147 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
149 $sql .= $this->db->order($sortfield, $sortorder);
154 $offset = $limit * $page;
156 $sql .= $this->db->plimit($limit + 1, $offset);
159 $result = $this->db->query($sql);
162 $num = $this->db->num_rows($result);
163 $min = min($num, ($limit <= 0 ? $num : $limit));
166 $obj = $this->db->fetch_object($result);
167 $holiday_static =
new Holiday($this->db);
168 if ($holiday_static->fetch($obj->rowid)) {
174 throw new RestException(503,
'Error when retrieve Leave list : '.$this->db->lasterror());
178 if ($pagination_data) {
179 $totalsResult = $this->db->query($sqlTotals);
180 $total = $this->db->fetch_object($totalsResult)->total;
185 $obj_ret[
'data'] = $tmp;
186 $obj_ret[
'pagination'] = [
187 'total' => (int) $total,
189 'page_count' => ceil((
int) $total / $limit),
209 public function post($request_data =
null)
211 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
212 throw new RestException(403,
"Insufficiant rights");
216 $result = $this->
_validate($request_data);
218 foreach ($request_data as $field => $value) {
219 if ($field ===
'caller') {
221 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
225 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
234 if ($this->holiday->create(DolibarrApiAccess::$user) < 0) {
235 throw new RestException(500,
"Error creating holiday", array_merge(array($this->holiday->error), $this->holiday->errors));
238 return $this->holiday->id;
259 public function put(
$id, $request_data =
null)
261 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
262 throw new RestException(403);
265 $result = $this->holiday->fetch(
$id);
267 throw new RestException(404,
'Leave not found');
271 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
273 foreach ($request_data as $field => $value) {
274 if ($field ==
'id') {
277 if ($field ===
'caller') {
279 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
283 if ($field ==
'array_options' && is_array($value)) {
284 foreach ($value as $index => $val) {
290 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
293 if ($this->holiday->update(DolibarrApiAccess::$user) > 0) {
294 return $this->
get(
$id);
296 throw new RestException(500, $this->holiday->error);
312 public function delete(
$id)
314 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'delete')) {
315 throw new RestException(403);
318 $result = $this->holiday->fetch(
$id);
320 throw new RestException(404,
'Leave not found');
324 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
327 if (!$this->holiday->delete(DolibarrApiAccess::$user)) {
328 throw new RestException(500,
'Error when deleting Leave : '.$this->holiday->error);
334 'message' =>
'Leave deleted'
360 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
361 throw new RestException(403,
"Insufficiant rights");
363 $result = $this->holiday->fetch(
$id);
365 throw new RestException(404,
'Leave not found');
369 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
373 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
375 throw new RestException(304,
'Error nothing done. May be object is already validated');
378 throw new RestException(500,
'Error when validating leave: '.$this->holiday->error);
406 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
407 throw new RestException(403,
"Insufficiant rights");
409 $result = $this->holiday->fetch(
$id);
411 throw new RestException(404,
'Leave not found');
415 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
419 $result = $this->holiday->approve(DolibarrApiAccess::$user, $notrigger);
421 throw new RestException(304,
'Error nothing done. May be object is already approved');
424 throw new RestException(500,
'Error when approving holiday: '.$this->holiday->error);
451 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
452 throw new RestException(403,
"Insufficient rights");
455 $result = $this->holiday->fetch(
$id);
457 throw new RestException(404,
'Leave not found');
461 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
465 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
467 throw new RestException(304,
'Error nothing done. May be object is already canceled');
470 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
496 public function refuse(
$id, $detail_refuse, $notrigger = 0)
498 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
499 throw new RestException(403,
"Insufficient rights");
502 $result = $this->holiday->fetch(
$id);
504 throw new RestException(404,
'Leave not found');
508 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
512 $this->holiday->detail_refuse = $detail_refuse;
513 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
515 throw new RestException(304,
'Error nothing done. May be object is already refused');
518 throw new RestException(500,
'Error when refusing holiday: '.$this->holiday->error);
548 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
549 throw new RestException(403,
"Insufficient rights");
552 $result = $this->holiday->fetch(
$id);
554 throw new RestException(404,
'Leave not found');
558 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
563 throw new RestException(400,
'Holiday is not canceled. Only canceled holidays can be reopened.');
566 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
568 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
595 unset(
$object->cond_reglement);
596 unset(
$object->shipping_method_id);
599 unset(
$object->barcode_type_code);
600 unset(
$object->barcode_type_label);
601 unset(
$object->barcode_type_coder);
603 unset(
$object->mode_reglement_id);
604 unset(
$object->cond_reglement_id);
610 unset(
$object->cond_reglement_id);
628 unset(
$object->totalpaid_multicurrency);
644 if ($data ===
null) {
648 foreach (self::$FIELDS as $field) {
649 if (!isset($data[$field])) {
650 throw new RestException(400,
"$field field missing");
652 $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.
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 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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.