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)).
")";
162 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
167 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
169 $sql .= $this->db->order($sortfield, $sortorder);
174 $offset = $limit * $page;
176 $sql .= $this->db->plimit($limit + 1, $offset);
179 $result = $this->db->query($sql);
182 $num = $this->db->num_rows($result);
183 $min = min($num, ($limit <= 0 ? $num : $limit));
186 $obj = $this->db->fetch_object($result);
187 $holiday_static =
new Holiday($this->db);
188 if ($holiday_static->fetch($obj->rowid)) {
194 throw new RestException(503,
'Error when retrieve Leave list : '.$this->db->lasterror());
198 if ($pagination_data) {
199 $totalsResult = $this->db->query($sqlTotals);
200 $total = $this->db->fetch_object($totalsResult)->total;
205 $obj_ret[
'data'] = $tmp;
206 $obj_ret[
'pagination'] = [
207 'total' => (int) $total,
209 'page_count' => ceil((
int) $total / $limit),
229 public function post($request_data =
null)
231 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
232 throw new RestException(403,
"Insufficiant rights");
236 $result = $this->
_validate($request_data);
239 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $request_data[
'fk_user'], DolibarrApiAccess::$user->getAllChildIds(1))) {
240 throw new RestException(403,
'UserNotInHierachy');
243 foreach ($request_data as $field => $value) {
244 if ($field ===
'caller') {
246 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
249 if (in_array($field, self::$FIELDS_FORBIDDEN_FOR_API) && $field !==
'fk_validator') {
250 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.");
253 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
262 if ($this->holiday->create(DolibarrApiAccess::$user) < 0) {
263 throw new RestException(500,
"Error creating holiday", array_merge(array($this->holiday->error), $this->holiday->errors));
266 return $this->holiday->id;
287 public function put(
$id, $request_data =
null)
289 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
290 throw new RestException(403);
293 $result = $this->holiday->fetch(
$id);
295 throw new RestException(404,
'Leave not found');
299 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
300 throw new RestException(403,
'UserNotInHierachy');
303 throw new RestException(400,
'Only a draft leave can be modified');
307 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
310 if (!is_array($request_data)) {
311 $request_data = array();
314 foreach ($request_data as $field => $value) {
315 if ($field ==
'id') {
318 if ($field ===
'caller') {
320 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
323 if (in_array($field, self::$FIELDS_FORBIDDEN_FOR_API)) {
324 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.");
327 if ($field ==
'array_options' && is_array($value)) {
328 foreach ($value as $index => $val) {
334 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
337 if ($this->holiday->update(DolibarrApiAccess::$user) > 0) {
338 return $this->
get(
$id);
340 throw new RestException(500, $this->holiday->error);
356 public function delete(
$id)
358 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'delete')) {
359 throw new RestException(403);
362 $result = $this->holiday->fetch(
$id);
364 throw new RestException(404,
'Leave not found');
368 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
373 throw new RestException(400,
'Only a draft, canceled or refused leave can be deleted');
376 if (!$this->holiday->delete(DolibarrApiAccess::$user)) {
377 throw new RestException(500,
'Error when deleting Leave : '.$this->holiday->error);
383 'message' =>
'Leave deleted'
409 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
410 throw new RestException(403,
"Insufficiant rights");
412 $result = $this->holiday->fetch(
$id);
414 throw new RestException(404,
'Leave not found');
418 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
422 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
423 throw new RestException(403,
'UserNotInHierachy');
426 throw new RestException(400,
'Only a draft leave can be validated');
430 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
432 throw new RestException(304,
'Error nothing done. May be object is already validated');
435 throw new RestException(500,
'Error when validating leave: '.$this->holiday->error);
463 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
464 throw new RestException(403,
"Insufficiant rights");
466 $result = $this->holiday->fetch(
$id);
468 throw new RestException(404,
'Leave not found');
472 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
477 throw new RestException(400,
'Only a validated leave can be approved');
479 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall')) {
480 throw new RestException(403,
'Only the approver of the leave can approve it');
483 $this->holiday->date_approval =
dol_now();
484 $this->holiday->fk_user_approve = DolibarrApiAccess::$user->id;
487 $result = $this->holiday->approve(DolibarrApiAccess::$user, $notrigger);
489 if ($result > 0 && !
getDolGlobalInt(
'HOLIDAY_DECREASE_AT_END_OF_MONTH')) {
491 $langs->load(
'holiday');
493 $tmpUser =
new User($this->db);
494 $tmpUser->fetch($this->holiday->fk_user);
497 $nbopenedday =
num_open_day($this->holiday->date_debut_gmt, $this->holiday->date_fin_gmt, 0, 1, $this->holiday->halfday, $tmpUser->country_id);
498 $soldeActuel = $this->holiday->getCpforUser($this->holiday->fk_user, $this->holiday->fk_type);
499 $newSolde = ($soldeActuel - $nbopenedday);
500 $label = $this->holiday->ref.
' - '.$langs->transnoentitiesnoconv(
"HolidayConsumption");
503 if ($this->holiday->addLogCP(DolibarrApiAccess::$user->id, $this->holiday->fk_user, $label, $newSolde, $this->holiday->fk_type) < 0
504 || $this->holiday->updateSoldeCP($this->holiday->fk_user, $newSolde, $this->holiday->fk_type) < 0) {
511 $this->db->rollback();
514 throw new RestException(304,
'Error nothing done. May be object is already approved');
517 throw new RestException(500,
'Error when approving holiday: '.$this->holiday->error);
544 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
545 throw new RestException(403,
"Insufficient rights");
548 $result = $this->holiday->fetch(
$id);
550 throw new RestException(404,
'Leave not found');
554 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
559 throw new RestException(400,
'Only a validated or approved leave can be canceled');
561 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')
562 && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
563 throw new RestException(403,
'UserNotInHierachy');
566 $oldstatus = $this->holiday->status;
567 $this->holiday->date_cancel =
dol_now();
568 $this->holiday->fk_user_cancel = DolibarrApiAccess::$user->id;
571 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
575 $langs->load(
'holiday');
577 if (!$notrigger && $this->holiday->call_trigger(
'HOLIDAY_CANCEL', DolibarrApiAccess::$user) < 0) {
581 $startDate = $this->holiday->date_debut_gmt;
582 $endDate = $this->holiday->date_fin_gmt;
583 $alreadydebited =
true;
586 $lastUpdate = strtotime($this->holiday->getConfCP(
'lastUpdate',
dol_print_date(
dol_now(),
'%Y%m%d%H%M%S')));
587 $date = strtotime(
'-1 month', $lastUpdate);
588 $endOfMonthBeforeLastUpdate =
dol_mktime(0, 0, 0, (
int)
date(
'm', $date), (
int)
date(
't', $date), (
int)
date(
'Y', $date), 1);
589 if ($this->holiday->date_debut_gmt < $endOfMonthBeforeLastUpdate && $this->holiday->date_fin_gmt > $endOfMonthBeforeLastUpdate) {
590 $endDate = $endOfMonthBeforeLastUpdate;
591 } elseif ($this->holiday->date_debut_gmt > $endOfMonthBeforeLastUpdate) {
592 $alreadydebited =
false;
596 $tmpUser =
new User($this->db);
597 $tmpUser->fetch($this->holiday->fk_user);
600 $nbopenedday = $alreadydebited ?
num_open_day($startDate, $endDate, 0, 1, $this->holiday->halfday, $tmpUser->country_id) : 0;
601 $soldeActuel = $this->holiday->getCpforUser($this->holiday->fk_user, $this->holiday->fk_type);
602 $newSolde = ($soldeActuel + $nbopenedday);
603 $label = $this->holiday->ref.
' - '.$langs->transnoentitiesnoconv(
"HolidayCreditAfterCancellation");
606 if ($this->holiday->addLogCP(DolibarrApiAccess::$user->id, $this->holiday->fk_user, $label, $newSolde, $this->holiday->fk_type) < 0
607 || $this->holiday->updateSoldeCP($this->holiday->fk_user, $newSolde, $this->holiday->fk_type) < 0) {
614 $this->db->rollback();
617 throw new RestException(304,
'Error nothing done. May be object is already canceled');
620 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
646 public function refuse(
$id, $detail_refuse, $notrigger = 0)
648 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
649 throw new RestException(403,
"Insufficient rights");
652 $result = $this->holiday->fetch(
$id);
654 throw new RestException(404,
'Leave not found');
658 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
663 throw new RestException(400,
'Only a validated leave can be refused');
665 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall')) {
666 throw new RestException(403,
'Only the approver of the leave can refuse it');
669 $this->holiday->date_refuse =
dol_now();
670 $this->holiday->fk_user_refuse = DolibarrApiAccess::$user->id;
672 $this->holiday->detail_refuse = $detail_refuse;
673 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
675 throw new RestException(304,
'Error nothing done. May be object is already refused');
678 throw new RestException(500,
'Error when refusing holiday: '.$this->holiday->error);
708 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
709 throw new RestException(403,
"Insufficient rights");
712 $result = $this->holiday->fetch(
$id);
714 throw new RestException(404,
'Leave not found');
718 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
723 throw new RestException(400,
'Holiday is not canceled. Only canceled holidays can be reopened.');
726 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
728 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
755 unset(
$object->cond_reglement);
756 unset(
$object->shipping_method_id);
759 unset(
$object->barcode_type_code);
760 unset(
$object->barcode_type_label);
761 unset(
$object->barcode_type_coder);
763 unset(
$object->mode_reglement_id);
764 unset(
$object->cond_reglement_id);
770 unset(
$object->cond_reglement_id);
788 unset(
$object->totalpaid_multicurrency);
804 if ($data ===
null) {
808 foreach (self::$FIELDS as $field) {
809 if (!isset($data[$field])) {
810 throw new RestException(400,
"$field field missing");
812 $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