dolibarr 24.0.0-beta
api_holidays.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2020-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2025 William Mead <william@m34d.com>
7 * Copyright (C) 2025-2026 Charlene Benke <charlene@patas-monkey.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23use Luracast\Restler\RestException;
24
25require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
26
27
36class Holidays extends DolibarrApi
37{
41 public static $FIELDS = array(
42 'fk_user',
43 'date_debut',
44 'date_fin',
45 );
46
50 public $holiday;
51
52
56 public function __construct()
57 {
58 global $db;
59
60 $this->db = $db;
61 $this->holiday = new Holiday($this->db);
62 }
63
76 public function get($id)
77 {
78 if (!DolibarrApiAccess::$user->hasRight('holiday', 'read')) {
79 throw new RestException(403);
80 }
81
82 $result = $this->holiday->fetch($id);
83 if (!$result) {
84 throw new RestException(404, 'Leave not found');
85 }
86
87 if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday)) {
88 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
89 }
90
91 $this->holiday->fetchObjectLinked();
92 return $this->_cleanObjectDatas($this->holiday);
93 }
94
114 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false)
115 {
116 if (!DolibarrApiAccess::$user->hasRight('holiday', 'read') && !DolibarrApiAccess::$user->hasRight('holiday', 'readall')) {
117 throw new RestException(403);
118 }
119
120 $obj_ret = array();
121
122 // case of external user, $societe param is ignored and replaced by user's socid
123 //$socid = DolibarrApiAccess::$user->socid ?: $societe;
124
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)"; // Link to extrafields is to allow to search parameters in the API GET call, so we will be able to filter on extrafields
127 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user AS u ON t.fk_user = u.rowid";
128 $sql .= ' WHERE t.entity IN ('.getEntity('holiday').')';
129 if ($user_ids) {
130 $sql .= " AND t.fk_user IN (".$this->db->sanitize($user_ids).")";
131 }
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)).")";
135 }
136
137 // Add sql filters
138 if ($sqlfilters) {
139 $errormessage = '';
140 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
141 if ($errormessage) {
142 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
143 }
144 }
145
146 //this query will return total orders with the filters given
147 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
148
149 $sql .= $this->db->order($sortfield, $sortorder);
150 if ($limit) {
151 if ($page < 0) {
152 $page = 0;
153 }
154 $offset = $limit * $page;
155
156 $sql .= $this->db->plimit($limit + 1, $offset);
157 }
158
159 $result = $this->db->query($sql);
160
161 if ($result) {
162 $num = $this->db->num_rows($result);
163 $min = min($num, ($limit <= 0 ? $num : $limit));
164 $i = 0;
165 while ($i < $min) {
166 $obj = $this->db->fetch_object($result);
167 $holiday_static = new Holiday($this->db);
168 if ($holiday_static->fetch($obj->rowid)) {
169 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($holiday_static), $properties);
170 }
171 $i++;
172 }
173 } else {
174 throw new RestException(503, 'Error when retrieve Leave list : '.$this->db->lasterror());
175 }
176
177 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
178 if ($pagination_data) {
179 $totalsResult = $this->db->query($sqlTotals);
180 $total = $this->db->fetch_object($totalsResult)->total;
181
182 $tmp = $obj_ret;
183 $obj_ret = [];
184
185 $obj_ret['data'] = $tmp;
186 $obj_ret['pagination'] = [
187 'total' => (int) $total,
188 'page' => $page, //count starts from 0
189 'page_count' => ceil((int) $total / $limit),
190 'limit' => $limit
191 ];
192 }
193
194 return $obj_ret;
195 }
196
209 public function post($request_data = null)
210 {
211 if (!DolibarrApiAccess::$user->hasRight('holiday', 'write')) {
212 throw new RestException(403, "Insufficiant rights");
213 }
214
215 // Check mandatory fields
216 $result = $this->_validate($request_data);
217
218 foreach ($request_data as $field => $value) {
219 if ($field === 'caller') {
220 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
221 $this->holiday->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
222 continue;
223 }
224
225 $this->holiday->$field = $this->_checkValForAPI($field, $value, $this->holiday);
226 }
227 /*if (isset($request_data["lines"])) {
228 $lines = array();
229 foreach ($request_data["lines"] as $line) {
230 array_push($lines, (object) $line);
231 }
232 $this->holiday->lines = $lines;
233 }*/
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));
236 }
237
238 return $this->holiday->id;
239 }
240
241
259 public function put($id, $request_data = null)
260 {
261 if (!DolibarrApiAccess::$user->hasRight('holiday', 'write')) {
262 throw new RestException(403);
263 }
264
265 $result = $this->holiday->fetch($id);
266 if (!$result) {
267 throw new RestException(404, 'Leave not found');
268 }
269
270 if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday)) {
271 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
272 }
273 foreach ($request_data as $field => $value) {
274 if ($field == 'id') {
275 continue;
276 }
277 if ($field === 'caller') {
278 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
279 $this->holiday->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
280 continue;
281 }
282
283 if ($field == 'array_options' && is_array($value)) {
284 foreach ($value as $index => $val) {
285 $this->holiday->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->holiday);
286 }
287 continue;
288 }
289
290 $this->holiday->$field = $this->_checkValForAPI($field, $value, $this->holiday);
291 }
292
293 if ($this->holiday->update(DolibarrApiAccess::$user) > 0) {
294 return $this->get($id);
295 } else {
296 throw new RestException(500, $this->holiday->error);
297 }
298 }
299
312 public function delete($id)
313 {
314 if (!DolibarrApiAccess::$user->hasRight('holiday', 'delete')) {
315 throw new RestException(403);
316 }
317
318 $result = $this->holiday->fetch($id);
319 if (!$result) {
320 throw new RestException(404, 'Leave not found');
321 }
322
323 if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday)) {
324 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
325 }
326
327 if (!$this->holiday->delete(DolibarrApiAccess::$user)) {
328 throw new RestException(500, 'Error when deleting Leave : '.$this->holiday->error);
329 }
330
331 return array(
332 'success' => array(
333 'code' => 200,
334 'message' => 'Leave deleted'
335 )
336 );
337 }
338
358 public function validate($id, $notrigger = 0)
359 {
360 if (!DolibarrApiAccess::$user->hasRight('holiday', 'write')) {
361 throw new RestException(403, "Insufficiant rights");
362 }
363 $result = $this->holiday->fetch($id);
364 if (!$result) {
365 throw new RestException(404, 'Leave not found');
366 }
367
368 if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday)) {
369 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
370 }
371
372 $this->holiday->status = Holiday::STATUS_VALIDATED;
373 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
374 if ($result == 0) {
375 throw new RestException(304, 'Error nothing done. May be object is already validated');
376 }
377 if ($result < 0) {
378 throw new RestException(500, 'Error when validating leave: '.$this->holiday->error);
379 }
380
381 return $this->_cleanObjectDatas($this->holiday);
382 }
383
384
404 public function approve($id, $notrigger = 0)
405 {
406 if (!DolibarrApiAccess::$user->hasRight('holiday', 'approve')) {
407 throw new RestException(403, "Insufficiant rights");
408 }
409 $result = $this->holiday->fetch($id);
410 if (!$result) {
411 throw new RestException(404, 'Leave not found');
412 }
413
414 if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday)) {
415 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
416 }
417
418 $this->holiday->status = Holiday::STATUS_APPROVED;
419 $result = $this->holiday->approve(DolibarrApiAccess::$user, $notrigger);
420 if ($result == 0) {
421 throw new RestException(304, 'Error nothing done. May be object is already approved');
422 }
423 if ($result < 0) {
424 throw new RestException(500, 'Error when approving holiday: '.$this->holiday->error);
425 }
426
427 return $this->_cleanObjectDatas($this->holiday);
428 }
429
449 public function cancel($id, $notrigger = 0)
450 {
451 if (!DolibarrApiAccess::$user->hasRight('holiday', 'write')) {
452 throw new RestException(403, "Insufficient rights");
453 }
454
455 $result = $this->holiday->fetch($id);
456 if (!$result) {
457 throw new RestException(404, 'Leave not found');
458 }
459
460 if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday)) {
461 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
462 }
463
464 $this->holiday->status = Holiday::STATUS_CANCELED;
465 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
466 if ($result == 0) {
467 throw new RestException(304, 'Error nothing done. May be object is already canceled');
468 }
469 if ($result < 0) {
470 throw new RestException(500, 'Error when canceling holiday: '.$this->holiday->error);
471 }
472
473 return $this->_cleanObjectDatas($this->holiday);
474 }
475
496 public function refuse($id, $detail_refuse, $notrigger = 0)
497 {
498 if (!DolibarrApiAccess::$user->hasRight('holiday', 'approve')) {
499 throw new RestException(403, "Insufficient rights");
500 }
501
502 $result = $this->holiday->fetch($id);
503 if (!$result) {
504 throw new RestException(404, 'Leave not found');
505 }
506
507 if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday)) {
508 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
509 }
510
511 $this->holiday->status = Holiday::STATUS_REFUSED;
512 $this->holiday->detail_refuse = $detail_refuse;
513 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
514 if ($result == 0) {
515 throw new RestException(304, 'Error nothing done. May be object is already refused');
516 }
517 if ($result < 0) {
518 throw new RestException(500, 'Error when refusing holiday: '.$this->holiday->error);
519 }
520
521 return $this->_cleanObjectDatas($this->holiday);
522 }
523
546 public function reopen($id, $notrigger = 0)
547 {
548 if (!DolibarrApiAccess::$user->hasRight('holiday', 'write')) {
549 throw new RestException(403, "Insufficient rights");
550 }
551
552 $result = $this->holiday->fetch($id);
553 if (!$result) {
554 throw new RestException(404, 'Leave not found');
555 }
556
557 if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday)) {
558 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
559 }
560
561 // Check if the holiday is actually canceled
562 if ($this->holiday->statut != Holiday::STATUS_CANCELED) {
563 throw new RestException(400, 'Holiday is not canceled. Only canceled holidays can be reopened.');
564 }
565 $this->holiday->status = Holiday::STATUS_VALIDATED;
566 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
567 if ($result < 0) {
568 throw new RestException(500, 'Error when canceling holiday: '.$this->holiday->error);
569 }
570
571 return $this->_cleanObjectDatas($this->holiday);
572 }
573
574 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
584 protected function _cleanObjectDatas($object)
585 {
586 // phpcs:enable
587 $object = parent::_cleanObjectDatas($object);
591 unset($object->statut);
592 unset($object->user);
593 unset($object->thirdparty);
594
595 unset($object->cond_reglement);
596 unset($object->shipping_method_id);
597
598 unset($object->barcode_type);
599 unset($object->barcode_type_code);
600 unset($object->barcode_type_label);
601 unset($object->barcode_type_coder);
602
603 unset($object->mode_reglement_id);
604 unset($object->cond_reglement_id);
605
606 unset($object->name);
607 unset($object->lastname);
608 unset($object->firstname);
609 unset($object->civility_id);
610 unset($object->cond_reglement_id);
611 unset($object->contact);
612 unset($object->contact_id);
613
614 unset($object->state);
615 unset($object->state_id);
616 unset($object->state_code);
617 unset($object->country);
618 unset($object->country_id);
619 unset($object->country_code);
620
621 unset($object->logs);
622 unset($object->events);
623 unset($object->holiday);
624 unset($object->canvas);
625 unset($object->lines);
626
627 unset($object->totalpaid);
628 unset($object->totalpaid_multicurrency);
629
630 unset($object->note); // We already use note_public and note_pricate
631
632 return $object;
633 }
634
642 private function _validate($data)
643 {
644 if ($data === null) {
645 $data = array();
646 }
647 $holiday = array();
648 foreach (self::$FIELDS as $field) {
649 if (!isset($data[$field])) {
650 throw new RestException(400, "$field field missing");
651 }
652 $holiday[$field] = $data[$field];
653 }
654 return $holiday;
655 }
656}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.