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)
138 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'readall')) {
139 throw new RestException(403);
147 $sql =
"SELECT t.rowid";
148 $sql .=
" FROM ".MAIN_DB_PREFIX.
"holiday AS t LEFT JOIN ".MAIN_DB_PREFIX.
"holiday_extrafields AS ef ON (ef.fk_object = t.rowid)";
149 $sql .=
' WHERE t.entity IN ('.getEntity(
'holiday').
')';
151 $sql .=
" AND t.fk_user IN (".$this->db->sanitize($user_ids).
")";
159 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
164 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
166 $sql .= $this->db->order($sortfield, $sortorder);
171 $offset = $limit * $page;
173 $sql .= $this->db->plimit($limit + 1, $offset);
176 $result = $this->db->query($sql);
179 $num = $this->db->num_rows($result);
180 $min = min($num, ($limit <= 0 ? $num : $limit));
183 $obj = $this->db->fetch_object($result);
184 $holiday_static =
new Holiday($this->db);
185 if ($holiday_static->fetch($obj->rowid)) {
191 throw new RestException(503,
'Error when retrieve Leave list : '.$this->db->lasterror());
195 if ($pagination_data) {
196 $totalsResult = $this->db->query($sqlTotals);
197 $total = $this->db->fetch_object($totalsResult)->total;
202 $obj_ret[
'data'] = $tmp;
203 $obj_ret[
'pagination'] = [
204 'total' => (int) $total,
206 'page_count' => ceil((
int) $total / $limit),
226 public function post($request_data =
null)
228 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
229 throw new RestException(403,
"Insufficiant rights");
233 $result = $this->
_validate($request_data);
236 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $request_data[
'fk_user'], DolibarrApiAccess::$user->getAllChildIds(1))) {
237 throw new RestException(403,
'UserNotInHierachy');
240 foreach ($request_data as $field => $value) {
241 if ($field ===
'caller') {
243 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
246 if (in_array($field, self::$FIELDS_FORBIDDEN_FOR_API) && $field !==
'fk_validator') {
247 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.");
250 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
259 if ($this->holiday->create(DolibarrApiAccess::$user) < 0) {
260 throw new RestException(500,
"Error creating holiday", array_merge(array($this->holiday->error), $this->holiday->errors));
263 return $this->holiday->id;
284 public function put(
$id, $request_data =
null)
286 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
287 throw new RestException(403);
290 $result = $this->holiday->fetch(
$id);
292 throw new RestException(404,
'Leave not found');
296 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
297 throw new RestException(403,
'UserNotInHierachy');
300 throw new RestException(400,
'Only a draft leave can be modified');
304 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
307 if (!is_array($request_data)) {
308 $request_data = array();
311 foreach ($request_data as $field => $value) {
312 if ($field ==
'id') {
315 if ($field ===
'caller') {
317 $this->holiday->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
320 if (in_array($field, self::$FIELDS_FORBIDDEN_FOR_API)) {
321 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.");
324 if ($field ==
'array_options' && is_array($value)) {
325 foreach ($value as $index => $val) {
326 $this->holiday->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->holiday);
331 $this->holiday->$field = $this->
_checkValForAPI($field, $value, $this->holiday);
334 if ($this->holiday->update(DolibarrApiAccess::$user) > 0) {
335 return $this->
get(
$id);
337 throw new RestException(500, $this->holiday->error);
353 public function delete(
$id)
355 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'delete')) {
356 throw new RestException(403);
359 $result = $this->holiday->fetch(
$id);
361 throw new RestException(404,
'Leave not found');
365 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
370 throw new RestException(400,
'Only a draft, canceled or refused leave can be deleted');
373 if (!$this->holiday->delete(DolibarrApiAccess::$user)) {
374 throw new RestException(500,
'Error when deleting Leave : '.$this->holiday->error);
380 'message' =>
'Leave deleted'
406 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
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 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
420 throw new RestException(403,
'UserNotInHierachy');
423 throw new RestException(400,
'Only a draft leave can be validated');
427 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
429 throw new RestException(304,
'Error nothing done. May be object is already validated');
432 throw new RestException(500,
'Error when validating leave: '.$this->holiday->error);
460 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
461 throw new RestException(403,
"Insufficiant rights");
463 $result = $this->holiday->fetch(
$id);
465 throw new RestException(404,
'Leave not found');
469 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
474 throw new RestException(400,
'Only a validated leave can be approved');
476 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall')) {
477 throw new RestException(403,
'Only the approver of the leave can approve it');
480 $this->holiday->date_approval =
dol_now();
481 $this->holiday->fk_user_approve = DolibarrApiAccess::$user->id;
484 $result = $this->holiday->approve(DolibarrApiAccess::$user, $notrigger);
486 if ($result > 0 && !
getDolGlobalInt(
'HOLIDAY_DECREASE_AT_END_OF_MONTH')) {
488 $langs->load(
'holiday');
490 $tmpUser =
new User($this->db);
491 $tmpUser->fetch($this->holiday->fk_user);
494 $nbopenedday =
num_open_day($this->holiday->date_debut_gmt, $this->holiday->date_fin_gmt, 0, 1, $this->holiday->halfday, $tmpUser->country_id);
495 $soldeActuel = $this->holiday->getCpforUser($this->holiday->fk_user, $this->holiday->fk_type);
496 $newSolde = ($soldeActuel - $nbopenedday);
497 $label = $this->holiday->ref.
' - '.$langs->transnoentitiesnoconv(
"HolidayConsumption");
500 if ($this->holiday->addLogCP(DolibarrApiAccess::$user->id, $this->holiday->fk_user, $label, $newSolde, $this->holiday->fk_type) < 0
501 || $this->holiday->updateSoldeCP($this->holiday->fk_user, $newSolde, $this->holiday->fk_type) < 0) {
508 $this->db->rollback();
511 throw new RestException(304,
'Error nothing done. May be object is already approved');
514 throw new RestException(500,
'Error when approving expense report: '.$this->holiday->error);
541 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
542 throw new RestException(403,
"Insufficient rights");
545 $result = $this->holiday->fetch(
$id);
547 throw new RestException(404,
'Leave not found');
551 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
556 throw new RestException(400,
'Only a validated or approved leave can be canceled');
558 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall') && !DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')
559 && !in_array((
int) $this->holiday->fk_user, DolibarrApiAccess::$user->getAllChildIds(1))) {
560 throw new RestException(403,
'UserNotInHierachy');
563 $oldstatus = $this->holiday->status;
564 $this->holiday->date_cancel =
dol_now();
565 $this->holiday->fk_user_cancel = DolibarrApiAccess::$user->id;
568 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
572 $langs->load(
'holiday');
574 if (!$notrigger && $this->holiday->call_trigger(
'HOLIDAY_CANCEL', DolibarrApiAccess::$user) < 0) {
578 $startDate = $this->holiday->date_debut_gmt;
579 $endDate = $this->holiday->date_fin_gmt;
580 $alreadydebited =
true;
583 $lastUpdate = strtotime($this->holiday->getConfCP(
'lastUpdate',
dol_print_date(
dol_now(),
'%Y%m%d%H%M%S')));
584 $date = strtotime(
'-1 month', $lastUpdate);
585 $endOfMonthBeforeLastUpdate =
dol_mktime(0, 0, 0, (
int)
date(
'm', $date), (
int)
date(
't', $date), (
int)
date(
'Y', $date), 1);
586 if ($this->holiday->date_debut_gmt < $endOfMonthBeforeLastUpdate && $this->holiday->date_fin_gmt > $endOfMonthBeforeLastUpdate) {
587 $endDate = $endOfMonthBeforeLastUpdate;
588 } elseif ($this->holiday->date_debut_gmt > $endOfMonthBeforeLastUpdate) {
589 $alreadydebited =
false;
593 $tmpUser =
new User($this->db);
594 $tmpUser->fetch($this->holiday->fk_user);
597 $nbopenedday = $alreadydebited ?
num_open_day($startDate, $endDate, 0, 1, $this->holiday->halfday, $tmpUser->country_id) : 0;
598 $soldeActuel = $this->holiday->getCpforUser($this->holiday->fk_user, $this->holiday->fk_type);
599 $newSolde = ($soldeActuel + $nbopenedday);
600 $label = $this->holiday->ref.
' - '.$langs->transnoentitiesnoconv(
"HolidayCreditAfterCancellation");
603 if ($this->holiday->addLogCP(DolibarrApiAccess::$user->id, $this->holiday->fk_user, $label, $newSolde, $this->holiday->fk_type) < 0
604 || $this->holiday->updateSoldeCP($this->holiday->fk_user, $newSolde, $this->holiday->fk_type) < 0) {
611 $this->db->rollback();
614 throw new RestException(304,
'Error nothing done. May be object is already canceled');
617 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
643 public function refuse(
$id, $detail_refuse, $notrigger = 0)
645 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'approve')) {
646 throw new RestException(403,
"Insufficient rights");
649 $result = $this->holiday->fetch(
$id);
651 throw new RestException(404,
'Leave not found');
655 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
660 throw new RestException(400,
'Only a validated leave can be refused');
662 if (DolibarrApiAccess::$user->
id != $this->holiday->fk_validator && !DolibarrApiAccess::$user->hasRight(
'holiday',
'writeall')) {
663 throw new RestException(403,
'Only the approver of the leave can refuse it');
666 $this->holiday->date_refuse =
dol_now();
667 $this->holiday->fk_user_refuse = DolibarrApiAccess::$user->id;
669 $this->holiday->detail_refuse = $detail_refuse;
670 $result = $this->holiday->update(DolibarrApiAccess::$user, $notrigger);
672 throw new RestException(304,
'Error nothing done. May be object is already refused');
675 throw new RestException(500,
'Error when refusing holiday: '.$this->holiday->error);
705 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'write')) {
706 throw new RestException(403,
"Insufficient rights");
709 $result = $this->holiday->fetch(
$id);
711 throw new RestException(404,
'Leave not found');
715 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
720 throw new RestException(400,
'Holiday is not canceled. Only canceled holidays can be reopened.');
723 $result = $this->holiday->validate(DolibarrApiAccess::$user, $notrigger);
725 throw new RestException(500,
'Error when canceling holiday: '.$this->holiday->error);
752 unset(
$object->cond_reglement);
753 unset(
$object->shipping_method_id);
756 unset(
$object->barcode_type_code);
757 unset(
$object->barcode_type_label);
758 unset(
$object->barcode_type_coder);
760 unset(
$object->mode_reglement_id);
761 unset(
$object->cond_reglement_id);
767 unset(
$object->cond_reglement_id);
785 unset(
$object->totalpaid_multicurrency);
801 if ($data ===
null) {
804 $expensereport = array();
805 foreach (self::$FIELDS as $field) {
806 if (!isset($data[$field])) {
807 throw new RestException(400,
"$field field missing");
809 $expensereport[$field] = $data[$field];
811 return $expensereport;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_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_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 expense report 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='')
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...
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left 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 PaymentTypeShortLIQ right SELECT p pos_change as p datep as date