23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
42 public static $FIELDS = array(
54 public static $FIELDS_FORBIDDEN_FOR_API = array(
81 $this->holiday =
new Holiday($this->db);
96 public function get(
$id)
98 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read')) {
99 throw new RestException(403);
102 $result = $this->holiday->fetch(
$id);
104 throw new RestException(404,
'Leave not found');
108 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
111 $this->holiday->fetchObjectLinked();
134 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
136 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read') && !DolibarrApiAccess::$user->hasRight(
'holiday',
'readall')) {
137 throw new RestException(403);
145 $sql =
"SELECT t.rowid";
146 $sql .=
" FROM ".MAIN_DB_PREFIX.
"holiday AS t LEFT JOIN ".MAIN_DB_PREFIX.
"holiday_extrafields AS ef ON (ef.fk_object = t.rowid)";
147 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user AS u ON t.fk_user = u.rowid";
148 $sql .=
' WHERE t.entity IN ('.getEntity(
'holiday').
')';
150 $sql .=
" AND t.fk_user IN (".$this->db->sanitize($user_ids).
")";
152 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'readall')) {
153 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
154 $sql .=
" AND t.fk_user IN (".$this->db->sanitize(implode(
',', $childids)).
")";
161 $forbiddenfilterfields = array(
167 'national_registration_number',
181 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
186 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
188 $sql .= $this->db->order($sortfield, $sortorder);
193 $offset = $limit * $page;
195 $sql .= $this->db->plimit($limit + 1, $offset);
198 $result = $this->db->query($sql);
201 $num = $this->db->num_rows($result);
202 $min = min($num, ($limit <= 0 ? $num : $limit));
205 $obj = $this->db->fetch_object($result);
206 $holiday_static =
new Holiday($this->db);
207 if ($holiday_static->fetch($obj->rowid)) {
213 throw new RestException(503,
'Error when retrieve Leave list : '.$this->db->lasterror());
217 if ($pagination_data) {
218 $totalsResult = $this->db->query($sqlTotals);
219 $total = $this->db->fetch_object($totalsResult)->total;
224 $obj_ret[
'data'] = $tmp;
225 $obj_ret[
'pagination'] = [
226 'total' => (int) $total,
228 'page_count' => ceil((
int) $total / $limit),
248 public function post($request_data =
null)
250 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
251 throw new RestException(403,
"Insufficiant rights");
255 $result = $this->
_validate($request_data);
258 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $request_data[
'fk_user'], DolibarrApiAccess::$user->getAllChildIds(1))) {
259 throw new RestException(403,
'UserNotInHierachy');
262 foreach ($request_data as $field => $value) {
263 if ($field ===
'caller') {
265 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
268 if (in_array($field, self::$FIELDS_FORBIDDEN_FOR_API) && $field !==
'fk_validator') {
269 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.");
272 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
281 if ($this->holiday->create(DolibarrApiAccess::$user) < 0) {
282 throw new RestException(500,
"Error creating holiday", array_merge(array($this->holiday->error), $this->holiday->errors));
285 return $this->holiday->id;
306 public function put(
$id, $request_data =
null)
308 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
309 throw new RestException(403);
312 $result = $this->holiday->fetch(
$id);
314 throw new RestException(404,
'Leave not found');
318 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
319 throw new RestException(403,
'UserNotInHierachy');
322 throw new RestException(400,
'Only a draft leave can be modified');
326 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
329 if (!is_array($request_data)) {
330 $request_data = array();
333 foreach ($request_data as $field => $value) {
334 if ($field ==
'id') {
337 if ($field ===
'caller') {
339 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
342 if (in_array($field, self::$FIELDS_FORBIDDEN_FOR_API)) {
343 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.");
346 if ($field ==
'array_options' && is_array($value)) {
347 foreach ($value as $index => $val) {
353 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
356 if ($this->holiday->update(DolibarrApiAccess::$user) > 0) {
357 return $this->
get(
$id);
359 throw new RestException(500, $this->holiday->errorsToString());
375 public function delete(
$id)
377 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'delete')) {
378 throw new RestException(403);
381 $result = $this->holiday->fetch(
$id);
383 throw new RestException(404,
'Leave not found');
387 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
392 throw new RestException(400,
'Only a draft, canceled or refused leave can be deleted');
395 if (!$this->holiday->delete(DolibarrApiAccess::$user)) {
396 throw new RestException(500,
'Error when deleting Leave : '.$this->holiday->errorsToString());
402 'message' =>
'Leave deleted'
428 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
429 throw new RestException(403,
"Insufficiant rights");
431 $result = $this->holiday->fetch(
$id);
433 throw new RestException(404,
'Leave not found');
437 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
441 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
442 throw new RestException(403,
'UserNotInHierachy');
445 throw new RestException(400,
'Only a draft leave can be validated');
449 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
451 throw new RestException(304,
'Error nothing done. May be object is already validated');
454 throw new RestException(500,
'Error when validating leave: '.$this->holiday->errorsToString());
482 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
483 throw new RestException(403,
"Insufficiant 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);
496 throw new RestException(400,
'Only a validated leave can be approved');
498 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall')) {
499 throw new RestException(403,
'Only the approver of the leave can approve it');
502 $this->holiday->date_approval =
dol_now();
503 $this->holiday->fk_user_approve = DolibarrApiAccess::$user->id;
506 $result = $this->holiday->approve(DolibarrApiAccess::$user, $notrigger);
508 if ($result > 0 && !
getDolGlobalInt(
'HOLIDAY_DECREASE_AT_END_OF_MONTH')) {
510 $langs->load(
'holiday');
512 $tmpUser =
new User($this->db);
513 $tmpUser->fetch($this->holiday->fk_user);
516 $nbopenedday =
num_open_day($this->holiday->date_debut_gmt, $this->holiday->date_fin_gmt, 0, 1, $this->holiday->halfday, $tmpUser->country_id);
517 $soldeActuel = $this->holiday->getCpforUser($this->holiday->fk_user, $this->holiday->fk_type);
518 $newSolde = ($soldeActuel - $nbopenedday);
519 $label = $this->holiday->ref.
' - '.$langs->transnoentitiesnoconv(
"HolidayConsumption");
522 if ($this->holiday->addLogCP(DolibarrApiAccess::$user->id, $this->holiday->fk_user, $label, $newSolde, $this->holiday->fk_type) < 0
523 || $this->holiday->updateSoldeCP($this->holiday->fk_user, $newSolde, $this->holiday->fk_type) < 0) {
530 $this->db->rollback();
533 throw new RestException(304,
'Error nothing done. May be object is already approved');
536 throw new RestException(500,
'Error when approving holiday: '.$this->holiday->errorsToString());
563 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
564 throw new RestException(403,
"Insufficient rights");
567 $result = $this->holiday->fetch(
$id);
569 throw new RestException(404,
'Leave not found');
573 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
578 throw new RestException(400,
'Only a validated or approved leave can be canceled');
580 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')
581 && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
582 throw new RestException(403,
'UserNotInHierachy');
585 $oldstatus = $this->holiday->status;
586 $this->holiday->date_cancel =
dol_now();
587 $this->holiday->fk_user_cancel = DolibarrApiAccess::$user->id;
590 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
594 $langs->load(
'holiday');
596 if (!$notrigger && $this->holiday->call_trigger(
'HOLIDAY_CANCEL', DolibarrApiAccess::$user) < 0) {
600 $startDate = $this->holiday->date_debut_gmt;
601 $endDate = $this->holiday->date_fin_gmt;
602 $alreadydebited =
true;
605 $lastUpdate = strtotime($this->holiday->getConfCP(
'lastUpdate',
dol_print_date(
dol_now(),
'%Y%m%d%H%M%S')));
606 $date = strtotime(
'-1 month', $lastUpdate);
607 $endOfMonthBeforeLastUpdate =
dol_mktime(0, 0, 0, (
int)
date(
'm', $date), (
int)
date(
't', $date), (
int)
date(
'Y', $date), 1);
608 if ($this->holiday->date_debut_gmt < $endOfMonthBeforeLastUpdate && $this->holiday->date_fin_gmt > $endOfMonthBeforeLastUpdate) {
609 $endDate = $endOfMonthBeforeLastUpdate;
610 } elseif ($this->holiday->date_debut_gmt > $endOfMonthBeforeLastUpdate) {
611 $alreadydebited =
false;
615 $tmpUser =
new User($this->db);
616 $tmpUser->fetch($this->holiday->fk_user);
619 $nbopenedday = $alreadydebited ?
num_open_day($startDate, $endDate, 0, 1, $this->holiday->halfday, $tmpUser->country_id) : 0;
620 $soldeActuel = $this->holiday->getCpforUser($this->holiday->fk_user, $this->holiday->fk_type);
621 $newSolde = ($soldeActuel + $nbopenedday);
622 $label = $this->holiday->ref.
' - '.$langs->transnoentitiesnoconv(
"HolidayCreditAfterCancellation");
625 if ($this->holiday->addLogCP(DolibarrApiAccess::$user->id, $this->holiday->fk_user, $label, $newSolde, $this->holiday->fk_type) < 0
626 || $this->holiday->updateSoldeCP($this->holiday->fk_user, $newSolde, $this->holiday->fk_type) < 0) {
633 $this->db->rollback();
636 throw new RestException(304,
'Error nothing done. May be object is already canceled');
639 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->errorsToString());
665 public function refuse(
$id, $detail_refuse, $notrigger = 0)
667 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
668 throw new RestException(403,
"Insufficient rights");
671 $result = $this->holiday->fetch(
$id);
673 throw new RestException(404,
'Leave not found');
677 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
682 throw new RestException(400,
'Only a validated leave can be refused');
684 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall')) {
685 throw new RestException(403,
'Only the approver of the leave can refuse it');
688 $this->holiday->date_refuse =
dol_now();
689 $this->holiday->fk_user_refuse = DolibarrApiAccess::$user->id;
691 $this->holiday->detail_refuse = $detail_refuse;
692 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
694 throw new RestException(304,
'Error nothing done. May be object is already refused');
697 throw new RestException(500,
'Error when refusing holiday: '.$this->holiday->errorsToString());
727 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
728 throw new RestException(403,
"Insufficient rights");
731 $result = $this->holiday->fetch(
$id);
733 throw new RestException(404,
'Leave not found');
737 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
742 throw new RestException(400,
'Holiday is not canceled. Only canceled holidays can be reopened.');
745 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
747 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->errorsToString());
774 unset(
$object->cond_reglement);
775 unset(
$object->shipping_method_id);
778 unset(
$object->barcode_type_code);
779 unset(
$object->barcode_type_label);
780 unset(
$object->barcode_type_coder);
782 unset(
$object->mode_reglement_id);
783 unset(
$object->cond_reglement_id);
789 unset(
$object->cond_reglement_id);
807 unset(
$object->totalpaid_multicurrency);
823 if ($data ===
null) {
827 foreach (self::$FIELDS as $field) {
828 if (!isset($data[$field])) {
829 throw new RestException(400,
"$field field missing");
831 $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_DRAFT
Draft 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.
Class to manage Dolibarr users.
num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $countryCodeOrId='', $user_id=0)
Function to return number of working days (and text of units) between two dates (working days)
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date