dolibarr 19.0.4
holiday.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
3 * Copyright (C) 2012-2014 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2012-2016 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
6 * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
7 * Copyright (C) 2018-2023 Frédéric France <frederic.france@netlogic.fr>
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
28require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29
30
34class Holiday extends CommonObject
35{
39 public $element = 'holiday';
40
44 public $table_element = 'holiday';
45
50 public $ismultientitymanaged = 0;
51
55 public $fk_element = 'fk_holiday';
56
60 public $picto = 'holiday';
61
65 public $fk_user;
66
67 public $date_create = '';
68
72 public $description;
73
74 public $date_debut = ''; // Date start in PHP server TZ
75 public $date_fin = ''; // Date end in PHP server TZ
76 public $date_debut_gmt = ''; // Date start in GMT
77 public $date_fin_gmt = ''; // Date end in GMT
78 public $halfday = ''; // 0:Full days, 2:Start afternoon end morning, -1:Start afternoon end afternoon, 1:Start morning end morning
79 public $statut = ''; // 1=draft, 2=validated, 3=approved
80
84 public $fk_validator;
85
89 public $date_valid = '';
90
94 public $fk_user_valid;
95
99 public $date_approval;
100
104 public $fk_user_approve;
105
109 public $date_refuse = '';
110
114 public $fk_user_refuse;
115
119 public $date_cancel = '';
120
124 public $fk_user_cancel;
125
129 public $fk_user_create;
130
131
132 public $detail_refuse = '';
133
137 public $fk_type;
138
139 public $holiday = array();
140 public $events = array();
141 public $logs = array();
142
143 public $optName = '';
144 public $optValue = '';
145 public $optRowid = '';
146
150 const STATUS_DRAFT = 1;
166 const STATUS_REFUSED = 5;
167
168
174 public function __construct($db)
175 {
176 $this->db = $db;
177 }
178
179
187 public function getNextNumRef($objsoc)
188 {
189 global $langs, $conf;
190 $langs->load("order");
191
192 if (!getDolGlobalString('HOLIDAY_ADDON')) {
193 $conf->global->HOLIDAY_ADDON = 'mod_holiday_madonna';
194 }
195
196 if (getDolGlobalString('HOLIDAY_ADDON')) {
197 $mybool = false;
198
199 $file = getDolGlobalString('HOLIDAY_ADDON') . ".php";
200 $classname = $conf->global->HOLIDAY_ADDON;
201
202 // Include file with class
203 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
204 foreach ($dirmodels as $reldir) {
205 $dir = dol_buildpath($reldir."core/modules/holiday/");
206
207 // Load file with numbering class (if found)
208 $mybool |= @include_once $dir.$file;
209 }
210
211 if ($mybool === false) {
212 dol_print_error('', "Failed to include file ".$file);
213 return '';
214 }
215
216 $obj = new $classname();
217 $numref = $obj->getNextValue($objsoc, $this);
218
219 if ($numref != "") {
220 return $numref;
221 } else {
222 $this->error = $obj->error;
223 //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
224 return "";
225 }
226 } else {
227 print $langs->trans("Error")." ".$langs->trans("Error_HOLIDAY_ADDON_NotDefined");
228 return "";
229 }
230 }
231
237 public function updateBalance()
238 {
239 $this->db->begin();
240
241 // Update sold of vocations
242 $result = $this->updateSoldeCP();
243
244 // Check nb of users into table llx_holiday_users and update with empty lines
245 //if ($result > 0) $result = $this->verifNbUsers($this->countActiveUsersWithoutCP(), $this->getConfCP('nbUser'));
246
247 if ($result >= 0) {
248 $this->db->commit();
249 return 0; // for cronjob use (0 is OK, any other value is an error code)
250 } else {
251 $this->db->rollback();
252 return -1;
253 }
254 }
255
263 public function create($user, $notrigger = 0)
264 {
265 global $conf;
266 $error = 0;
267
268 $now = dol_now();
269
270 // Check parameters
271 if (empty($this->fk_user) || !is_numeric($this->fk_user) || $this->fk_user < 0) {
272 $this->error = "ErrorBadParameterFkUser";
273 return -1;
274 }
275 if (empty($this->fk_validator) || !is_numeric($this->fk_validator) || $this->fk_validator < 0) {
276 $this->error = "ErrorBadParameterFkValidator";
277 return -1;
278 }
279 if (empty($this->fk_type) || !is_numeric($this->fk_type) || $this->fk_type < 0) {
280 $this->error = "ErrorBadParameterFkType";
281 return -1;
282 }
283
284 // Insert request
285 $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday(";
286 $sql .= "ref,";
287 $sql .= "fk_user,";
288 $sql .= "date_create,";
289 $sql .= "description,";
290 $sql .= "date_debut,";
291 $sql .= "date_fin,";
292 $sql .= "halfday,";
293 $sql .= "statut,";
294 $sql .= "fk_validator,";
295 $sql .= "fk_type,";
296 $sql .= "fk_user_create,";
297 $sql .= "entity";
298 $sql .= ") VALUES (";
299 $sql .= "'(PROV)',";
300 $sql .= " ".((int) $this->fk_user).",";
301 $sql .= " '".$this->db->idate($now)."',";
302 $sql .= " '".$this->db->escape($this->description)."',";
303 $sql .= " '".$this->db->idate($this->date_debut)."',";
304 $sql .= " '".$this->db->idate($this->date_fin)."',";
305 $sql .= " ".((int) $this->halfday).",";
306 $sql .= " '1',";
307 $sql .= " ".((int) $this->fk_validator).",";
308 $sql .= " ".((int) $this->fk_type).",";
309 $sql .= " ".((int) $user->id).",";
310 $sql .= " ".((int) $conf->entity);
311 $sql .= ")";
312
313 $this->db->begin();
314
315 dol_syslog(get_class($this)."::create", LOG_DEBUG);
316 $resql = $this->db->query($sql);
317 if (!$resql) {
318 $error++;
319 $this->errors[] = "Error ".$this->db->lasterror();
320 }
321
322 if (!$error) {
323 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."holiday");
324
325 if ($this->id) {
326 // update ref
327 $initialref = '(PROV'.$this->id.')';
328 if (!empty($this->ref)) {
329 $initialref = $this->ref;
330 }
331
332 $sql = 'UPDATE '.MAIN_DB_PREFIX."holiday SET ref='".$this->db->escape($initialref)."' WHERE rowid=".((int) $this->id);
333 if ($this->db->query($sql)) {
334 $this->ref = $initialref;
335
336 if (!$error) {
337 $result = $this->insertExtraFields();
338 if ($result < 0) {
339 $error++;
340 }
341 }
342
343 if (!$error && !$notrigger) {
344 // Call trigger
345 $result = $this->call_trigger('HOLIDAY_CREATE', $user);
346 if ($result < 0) {
347 $error++;
348 }
349 // End call triggers
350 }
351 }
352 }
353 }
354
355 // Commit or rollback
356 if ($error) {
357 foreach ($this->errors as $errmsg) {
358 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
359 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
360 }
361 $this->db->rollback();
362 return -1 * $error;
363 } else {
364 $this->db->commit();
365 return $this->id;
366 }
367 }
368
369
377 public function fetch($id, $ref = '')
378 {
379 global $langs;
380
381 $sql = "SELECT";
382 $sql .= " cp.rowid,";
383 $sql .= " cp.ref,";
384 $sql .= " cp.fk_user,";
385 $sql .= " cp.date_create,";
386 $sql .= " cp.description,";
387 $sql .= " cp.date_debut,";
388 $sql .= " cp.date_fin,";
389 $sql .= " cp.halfday,";
390 $sql .= " cp.statut,";
391 $sql .= " cp.fk_validator,";
392 $sql .= " cp.date_valid,";
393 $sql .= " cp.fk_user_valid,";
394 $sql .= " cp.date_approval,";
395 $sql .= " cp.fk_user_approve,";
396 $sql .= " cp.date_refuse,";
397 $sql .= " cp.fk_user_refuse,";
398 $sql .= " cp.date_cancel,";
399 $sql .= " cp.fk_user_cancel,";
400 $sql .= " cp.detail_refuse,";
401 $sql .= " cp.note_private,";
402 $sql .= " cp.note_public,";
403 $sql .= " cp.fk_user_create,";
404 $sql .= " cp.fk_type,";
405 $sql .= " cp.entity";
406 $sql .= " FROM ".MAIN_DB_PREFIX."holiday as cp";
407 if ($id > 0) {
408 $sql .= " WHERE cp.rowid = ".((int) $id);
409 } else {
410 $sql .= " WHERE cp.ref = '".$this->db->escape($ref)."'";
411 }
412
413 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
414 $resql = $this->db->query($sql);
415 if ($resql) {
416 if ($this->db->num_rows($resql)) {
417 $obj = $this->db->fetch_object($resql);
418
419 $this->id = $obj->rowid;
420 $this->ref = ($obj->ref ? $obj->ref : $obj->rowid);
421 $this->fk_user = $obj->fk_user;
422 $this->date_create = $this->db->jdate($obj->date_create);
423 $this->description = $obj->description;
424 $this->date_debut = $this->db->jdate($obj->date_debut);
425 $this->date_fin = $this->db->jdate($obj->date_fin);
426 $this->date_debut_gmt = $this->db->jdate($obj->date_debut, 1);
427 $this->date_fin_gmt = $this->db->jdate($obj->date_fin, 1);
428 $this->halfday = $obj->halfday;
429 $this->statut = $obj->statut;
430 $this->fk_validator = $obj->fk_validator;
431 $this->date_valid = $this->db->jdate($obj->date_valid);
432 $this->fk_user_valid = $obj->fk_user_valid;
433 $this->user_validation_id = $obj->fk_user_valid;
434 $this->date_approval = $this->db->jdate($obj->date_approval);
435 $this->fk_user_approve = $obj->fk_user_approve;
436 $this->date_refuse = $this->db->jdate($obj->date_refuse);
437 $this->fk_user_refuse = $obj->fk_user_refuse;
438 $this->date_cancel = $this->db->jdate($obj->date_cancel);
439 $this->fk_user_cancel = $obj->fk_user_cancel;
440 $this->detail_refuse = $obj->detail_refuse;
441 $this->note_private = $obj->note_private;
442 $this->note_public = $obj->note_public;
443 $this->fk_user_create = $obj->fk_user_create;
444 $this->fk_type = $obj->fk_type;
445 $this->entity = $obj->entity;
446
447 $this->fetch_optionals();
448
449 $result = 1;
450 } else {
451 $result = 0;
452 }
453 $this->db->free($resql);
454
455 return $result;
456 } else {
457 $this->error = "Error ".$this->db->lasterror();
458 return -1;
459 }
460 }
461
470 public function fetchByUser($user_id, $order = '', $filter = '')
471 {
472 global $langs, $conf;
473
474 $sql = "SELECT";
475 $sql .= " cp.rowid,";
476 $sql .= " cp.ref,";
477
478 $sql .= " cp.fk_user,";
479 $sql .= " cp.fk_type,";
480 $sql .= " cp.date_create,";
481 $sql .= " cp.description,";
482 $sql .= " cp.date_debut,";
483 $sql .= " cp.date_fin,";
484 $sql .= " cp.halfday,";
485 $sql .= " cp.statut,";
486 $sql .= " cp.fk_validator,";
487 $sql .= " cp.date_valid,";
488 $sql .= " cp.fk_user_valid,";
489 $sql .= " cp.date_approval,";
490 $sql .= " cp.fk_user_approve,";
491 $sql .= " cp.date_refuse,";
492 $sql .= " cp.fk_user_refuse,";
493 $sql .= " cp.date_cancel,";
494 $sql .= " cp.fk_user_cancel,";
495 $sql .= " cp.detail_refuse,";
496
497 $sql .= " uu.lastname as user_lastname,";
498 $sql .= " uu.firstname as user_firstname,";
499 $sql .= " uu.login as user_login,";
500 $sql .= " uu.statut as user_statut,";
501 $sql .= " uu.photo as user_photo,";
502
503 $sql .= " ua.lastname as validator_lastname,";
504 $sql .= " ua.firstname as validator_firstname,";
505 $sql .= " ua.login as validator_login,";
506 $sql .= " ua.statut as validator_statut,";
507 $sql .= " ua.photo as validator_photo";
508
509 $sql .= " FROM ".MAIN_DB_PREFIX."holiday as cp, ".MAIN_DB_PREFIX."user as uu, ".MAIN_DB_PREFIX."user as ua";
510 $sql .= " WHERE cp.entity IN (".getEntity('holiday').")";
511 $sql .= " AND cp.fk_user = uu.rowid AND cp.fk_validator = ua.rowid"; // Hack pour la recherche sur le tableau
512 $sql .= " AND cp.fk_user IN (".$this->db->sanitize($user_id).")";
513
514 // Selection filter
515 if (!empty($filter)) {
516 $sql .= $filter;
517 }
518
519 // Order of display of the result
520 if (!empty($order)) {
521 $sql .= $order;
522 }
523
524 dol_syslog(get_class($this)."::fetchByUser", LOG_DEBUG);
525 $resql = $this->db->query($sql);
526
527 // If no SQL error
528 if ($resql) {
529 $i = 0;
530 $tab_result = $this->holiday;
531 $num = $this->db->num_rows($resql);
532
533 // If no registration
534 if (!$num) {
535 return 2;
536 }
537
538 // List the records and add them to the table
539 while ($i < $num) {
540 $obj = $this->db->fetch_object($resql);
541
542 $tab_result[$i]['rowid'] = $obj->rowid;
543 $tab_result[$i]['id'] = $obj->rowid;
544 $tab_result[$i]['ref'] = ($obj->ref ? $obj->ref : $obj->rowid);
545
546 $tab_result[$i]['fk_user'] = $obj->fk_user;
547 $tab_result[$i]['fk_type'] = $obj->fk_type;
548 $tab_result[$i]['date_create'] = $this->db->jdate($obj->date_create);
549 $tab_result[$i]['description'] = $obj->description;
550 $tab_result[$i]['date_debut'] = $this->db->jdate($obj->date_debut);
551 $tab_result[$i]['date_fin'] = $this->db->jdate($obj->date_fin);
552 $tab_result[$i]['date_debut_gmt'] = $this->db->jdate($obj->date_debut, 1);
553 $tab_result[$i]['date_fin_gmt'] = $this->db->jdate($obj->date_fin, 1);
554 $tab_result[$i]['halfday'] = $obj->halfday;
555 $tab_result[$i]['statut'] = $obj->statut;
556 $tab_result[$i]['fk_validator'] = $obj->fk_validator;
557 $tab_result[$i]['date_valid'] = $this->db->jdate($obj->date_valid);
558 $tab_result[$i]['fk_user_valid'] = $obj->fk_user_valid;
559 $tab_result[$i]['date_approval'] = $this->db->jdate($obj->date_approval);
560 $tab_result[$i]['fk_user_approve'] = $obj->fk_user_approve;
561 $tab_result[$i]['date_refuse'] = $this->db->jdate($obj->date_refuse);
562 $tab_result[$i]['fk_user_refuse'] = $obj->fk_user_refuse;
563 $tab_result[$i]['date_cancel'] = $this->db->jdate($obj->date_cancel);
564 $tab_result[$i]['fk_user_cancel'] = $obj->fk_user_cancel;
565 $tab_result[$i]['detail_refuse'] = $obj->detail_refuse;
566
567 $tab_result[$i]['user_firstname'] = $obj->user_firstname;
568 $tab_result[$i]['user_lastname'] = $obj->user_lastname;
569 $tab_result[$i]['user_login'] = $obj->user_login;
570 $tab_result[$i]['user_statut'] = $obj->user_statut;
571 $tab_result[$i]['user_photo'] = $obj->user_photo;
572
573 $tab_result[$i]['validator_firstname'] = $obj->validator_firstname;
574 $tab_result[$i]['validator_lastname'] = $obj->validator_lastname;
575 $tab_result[$i]['validator_login'] = $obj->validator_login;
576 $tab_result[$i]['validator_statut'] = $obj->validator_statut;
577 $tab_result[$i]['validator_photo'] = $obj->validator_photo;
578
579 $i++;
580 }
581
582 // Returns 1 with the filled array
583 $this->holiday = $tab_result;
584 return 1;
585 } else {
586 // SQL Error
587 $this->error = "Error ".$this->db->lasterror();
588 return -1;
589 }
590 }
591
599 public function fetchAll($order, $filter)
600 {
601 global $langs;
602
603 $sql = "SELECT";
604 $sql .= " cp.rowid,";
605 $sql .= " cp.ref,";
606
607 $sql .= " cp.fk_user,";
608 $sql .= " cp.fk_type,";
609 $sql .= " cp.date_create,";
610 $sql .= " cp.tms as date_update,";
611 $sql .= " cp.description,";
612 $sql .= " cp.date_debut,";
613 $sql .= " cp.date_fin,";
614 $sql .= " cp.halfday,";
615 $sql .= " cp.statut,";
616 $sql .= " cp.fk_validator,";
617 $sql .= " cp.date_valid,";
618 $sql .= " cp.fk_user_valid,";
619 $sql .= " cp.date_approval,";
620 $sql .= " cp.fk_user_approve,";
621 $sql .= " cp.date_refuse,";
622 $sql .= " cp.fk_user_refuse,";
623 $sql .= " cp.date_cancel,";
624 $sql .= " cp.fk_user_cancel,";
625 $sql .= " cp.detail_refuse,";
626
627 $sql .= " uu.lastname as user_lastname,";
628 $sql .= " uu.firstname as user_firstname,";
629 $sql .= " uu.login as user_login,";
630 $sql .= " uu.statut as user_statut,";
631 $sql .= " uu.photo as user_photo,";
632
633 $sql .= " ua.lastname as validator_lastname,";
634 $sql .= " ua.firstname as validator_firstname,";
635 $sql .= " ua.login as validator_login,";
636 $sql .= " ua.statut as validator_statut,";
637 $sql .= " ua.photo as validator_photo";
638
639 $sql .= " FROM ".MAIN_DB_PREFIX."holiday as cp, ".MAIN_DB_PREFIX."user as uu, ".MAIN_DB_PREFIX."user as ua";
640 $sql .= " WHERE cp.entity IN (".getEntity('holiday').")";
641 $sql .= " AND cp.fk_user = uu.rowid AND cp.fk_validator = ua.rowid "; // Hack pour la recherche sur le tableau
642
643 // Selection filtering
644 if (!empty($filter)) {
645 $sql .= $filter;
646 }
647
648 // order of display
649 if (!empty($order)) {
650 $sql .= $order;
651 }
652
653 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
654 $resql = $this->db->query($sql);
655
656 // If no SQL error
657 if ($resql) {
658 $i = 0;
659 $tab_result = $this->holiday;
660 $num = $this->db->num_rows($resql);
661
662 // If no registration
663 if (!$num) {
664 return 2;
665 }
666
667 // List the records and add them to the table
668 while ($i < $num) {
669 $obj = $this->db->fetch_object($resql);
670
671 $tab_result[$i]['rowid'] = $obj->rowid;
672 $tab_result[$i]['id'] = $obj->rowid;
673 $tab_result[$i]['ref'] = ($obj->ref ? $obj->ref : $obj->rowid);
674
675 $tab_result[$i]['fk_user'] = $obj->fk_user;
676 $tab_result[$i]['fk_type'] = $obj->fk_type;
677 $tab_result[$i]['date_create'] = $this->db->jdate($obj->date_create);
678 $tab_result[$i]['date_update'] = $this->db->jdate($obj->date_update);
679 $tab_result[$i]['description'] = $obj->description;
680 $tab_result[$i]['date_debut'] = $this->db->jdate($obj->date_debut);
681 $tab_result[$i]['date_fin'] = $this->db->jdate($obj->date_fin);
682 $tab_result[$i]['date_debut_gmt'] = $this->db->jdate($obj->date_debut, 1);
683 $tab_result[$i]['date_fin_gmt'] = $this->db->jdate($obj->date_fin, 1);
684 $tab_result[$i]['halfday'] = $obj->halfday;
685 $tab_result[$i]['statut'] = $obj->statut;
686 $tab_result[$i]['fk_validator'] = $obj->fk_validator;
687 $tab_result[$i]['date_valid'] = $this->db->jdate($obj->date_valid);
688 $tab_result[$i]['fk_user_valid'] = $obj->fk_user_valid;
689 $tab_result[$i]['date_approval'] = $this->db->jdate($obj->date_approval);
690 $tab_result[$i]['fk_user_approve'] = $obj->fk_user_approve;
691 $tab_result[$i]['date_refuse'] = $obj->date_refuse;
692 $tab_result[$i]['fk_user_refuse'] = $obj->fk_user_refuse;
693 $tab_result[$i]['date_cancel'] = $obj->date_cancel;
694 $tab_result[$i]['fk_user_cancel'] = $obj->fk_user_cancel;
695 $tab_result[$i]['detail_refuse'] = $obj->detail_refuse;
696
697 $tab_result[$i]['user_firstname'] = $obj->user_firstname;
698 $tab_result[$i]['user_lastname'] = $obj->user_lastname;
699 $tab_result[$i]['user_login'] = $obj->user_login;
700 $tab_result[$i]['user_statut'] = $obj->user_statut;
701 $tab_result[$i]['user_photo'] = $obj->user_photo;
702
703 $tab_result[$i]['validator_firstname'] = $obj->validator_firstname;
704 $tab_result[$i]['validator_lastname'] = $obj->validator_lastname;
705 $tab_result[$i]['validator_login'] = $obj->validator_login;
706 $tab_result[$i]['validator_statut'] = $obj->validator_statut;
707 $tab_result[$i]['validator_photo'] = $obj->validator_photo;
708
709 $i++;
710 }
711 // Returns 1 and adds the array to the variable
712 $this->holiday = $tab_result;
713 return 1;
714 } else {
715 // SQL Error
716 $this->error = "Error ".$this->db->lasterror();
717 return -1;
718 }
719 }
720
721
729 public function validate($user = null, $notrigger = 0)
730 {
731 global $conf, $langs;
732 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
733 $error = 0;
734
735 $checkBalance = getDictionaryValue('c_holiday_types', 'block_if_negative', $this->fk_type);
736
737 if ($checkBalance > 0) {
738 $balance = $this->getCPforUser($this->fk_user, $this->fk_type);
739
740 if ($balance < 0) {
741 $this->error = 'LeaveRequestCreationBlockedBecauseBalanceIsNegative';
742 return -1;
743 }
744 }
745
746 // Define new ref
747 if (!$error && (preg_match('/^[\‍(]?PROV/i', $this->ref) || empty($this->ref) || $this->ref == $this->id)) {
748 $num = $this->getNextNumRef(null);
749 } else {
750 $num = $this->ref;
751 }
752 $this->newref = dol_sanitizeFileName($num);
753
754 // Update status
755 $sql = "UPDATE ".MAIN_DB_PREFIX."holiday SET";
756 $sql .= " fk_user_valid = ".((int) $user->id).",";
757 $sql .= " date_valid = '".$this->db->idate(dol_now())."',";
758 if (!empty($this->statut) && is_numeric($this->statut)) {
759 $sql .= " statut = ".((int) $this->statut).",";
760 } else {
761 $this->error = 'Property status must be a numeric value';
762 $error++;
763 }
764 $sql .= " ref = '".$this->db->escape($num)."'";
765 $sql .= " WHERE rowid = ".((int) $this->id);
766
767 $this->db->begin();
768
769 dol_syslog(get_class($this)."::validate", LOG_DEBUG);
770 $resql = $this->db->query($sql);
771 if (!$resql) {
772 $error++;
773 $this->errors[] = "Error ".$this->db->lasterror();
774 }
775
776 if (!$error) {
777 if (!$notrigger) {
778 // Call trigger
779 $result = $this->call_trigger('HOLIDAY_VALIDATE', $user);
780 if ($result < 0) {
781 $error++;
782 }
783 // End call triggers
784 }
785 }
786
787 if (!$error) {
788 $this->oldref = $this->ref;
789
790 // Rename directory if dir was a temporary ref
791 if (preg_match('/^[\‍(]?PROV/i', $this->ref)) {
792 // Now we rename also files into index
793 $sql = 'UPDATE ' . MAIN_DB_PREFIX . "ecm_files set filename = CONCAT('" . $this->db->escape($this->newref) . "', SUBSTR(filename, " . (strlen($this->ref) + 1) . ")), filepath = 'holiday/" . $this->db->escape($this->newref) . "'";
794 $sql .= " WHERE filename LIKE '" . $this->db->escape($this->ref) . "%' AND filepath = 'holiday/" . $this->db->escape($this->ref) . "' and entity = " . ((int) $conf->entity);
795 $resql = $this->db->query($sql);
796 if (!$resql) {
797 $error++;
798 $this->error = $this->db->lasterror();
799 }
800 $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'holiday/".$this->db->escape($this->newref)."'";
801 $sql .= " WHERE filepath = 'holiday/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
802 $resql = $this->db->query($sql);
803 if (!$resql) {
804 $error++;
805 $this->error = $this->db->lasterror();
806 }
807
808 // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
809 $oldref = dol_sanitizeFileName($this->ref);
810 $newref = dol_sanitizeFileName($num);
811 $dirsource = $conf->holiday->multidir_output[$this->entity] . '/' . $oldref;
812 $dirdest = $conf->holiday->multidir_output[$this->entity] . '/' . $newref;
813 if (!$error && file_exists($dirsource)) {
814 dol_syslog(get_class($this) . "::validate rename dir " . $dirsource . " into " . $dirdest);
815 if (@rename($dirsource, $dirdest)) {
816 dol_syslog("Rename ok");
817 // Rename docs starting with $oldref with $newref
818 $listoffiles = dol_dir_list($dirdest, 'files', 1, '^' . preg_quote($oldref, '/'));
819 foreach ($listoffiles as $fileentry) {
820 $dirsource = $fileentry['name'];
821 $dirdest = preg_replace('/^' . preg_quote($oldref, '/') . '/', $newref, $dirsource);
822 $dirsource = $fileentry['path'] . '/' . $dirsource;
823 $dirdest = $fileentry['path'] . '/' . $dirdest;
824 @rename($dirsource, $dirdest);
825 }
826 }
827 }
828 }
829 }
830
831
832 // Commit or rollback
833 if ($error) {
834 foreach ($this->errors as $errmsg) {
835 dol_syslog(get_class($this)."::validate ".$errmsg, LOG_ERR);
836 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
837 }
838 $this->db->rollback();
839 return -1 * $error;
840 } else {
841 $this->db->commit();
842 return 1;
843 }
844 }
845
846
854 public function approve($user = null, $notrigger = 0)
855 {
856 global $conf, $langs;
857 $error = 0;
858
859 $checkBalance = getDictionaryValue('c_holiday_types', 'block_if_negative', $this->fk_type);
860
861 if ($checkBalance > 0) {
862 $balance = $this->getCPforUser($this->fk_user, $this->fk_type);
863
864 if ($balance < 0) {
865 $this->error = 'LeaveRequestCreationBlockedBecauseBalanceIsNegative';
866 return -1;
867 }
868 }
869
870 // Update request
871 $sql = "UPDATE ".MAIN_DB_PREFIX."holiday SET";
872
873 $sql .= " description= '".$this->db->escape($this->description)."',";
874
875 if (!empty($this->date_debut)) {
876 $sql .= " date_debut = '".$this->db->idate($this->date_debut)."',";
877 } else {
878 $error++;
879 }
880 if (!empty($this->date_fin)) {
881 $sql .= " date_fin = '".$this->db->idate($this->date_fin)."',";
882 } else {
883 $error++;
884 }
885 $sql .= " halfday = ".((int) $this->halfday).",";
886 if (!empty($this->statut) && is_numeric($this->statut)) {
887 $sql .= " statut = ".((int) $this->statut).",";
888 } else {
889 $error++;
890 }
891 if (!empty($this->fk_validator)) {
892 $sql .= " fk_validator = ".((int) $this->fk_validator).",";
893 } else {
894 $error++;
895 }
896 if (!empty($this->date_valid)) {
897 $sql .= " date_valid = '".$this->db->idate($this->date_valid)."',";
898 } else {
899 $sql .= " date_valid = NULL,";
900 }
901 if (!empty($this->fk_user_valid)) {
902 $sql .= " fk_user_valid = ".((int) $this->fk_user_valid).",";
903 } else {
904 $sql .= " fk_user_valid = NULL,";
905 }
906 if (!empty($this->date_approval)) {
907 $sql .= " date_approval = '".$this->db->idate($this->date_approval)."',";
908 } else {
909 $sql .= " date_approval = NULL,";
910 }
911 if (!empty($this->fk_user_approve)) {
912 $sql .= " fk_user_approve = ".((int) $this->fk_user_approve).",";
913 } else {
914 $sql .= " fk_user_approve = NULL,";
915 }
916 if (!empty($this->date_refuse)) {
917 $sql .= " date_refuse = '".$this->db->idate($this->date_refuse)."',";
918 } else {
919 $sql .= " date_refuse = NULL,";
920 }
921 if (!empty($this->fk_user_refuse)) {
922 $sql .= " fk_user_refuse = ".((int) $this->fk_user_refuse).",";
923 } else {
924 $sql .= " fk_user_refuse = NULL,";
925 }
926 if (!empty($this->date_cancel)) {
927 $sql .= " date_cancel = '".$this->db->idate($this->date_cancel)."',";
928 } else {
929 $sql .= " date_cancel = NULL,";
930 }
931 if (!empty($this->fk_user_cancel)) {
932 $sql .= " fk_user_cancel = ".((int) $this->fk_user_cancel).",";
933 } else {
934 $sql .= " fk_user_cancel = NULL,";
935 }
936 if (!empty($this->detail_refuse)) {
937 $sql .= " detail_refuse = '".$this->db->escape($this->detail_refuse)."'";
938 } else {
939 $sql .= " detail_refuse = NULL";
940 }
941 $sql .= " WHERE rowid = ".((int) $this->id);
942
943 $this->db->begin();
944
945 dol_syslog(get_class($this)."::approve", LOG_DEBUG);
946 $resql = $this->db->query($sql);
947 if (!$resql) {
948 $error++;
949 $this->errors[] = "Error ".$this->db->lasterror();
950 }
951
952 if (!$error) {
953 if (!$notrigger) {
954 // Call trigger
955 $result = $this->call_trigger('HOLIDAY_APPROVE', $user);
956 if ($result < 0) {
957 $error++;
958 }
959 // End call triggers
960 }
961 }
962
963 // Commit or rollback
964 if ($error) {
965 foreach ($this->errors as $errmsg) {
966 dol_syslog(get_class($this)."::approve ".$errmsg, LOG_ERR);
967 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
968 }
969 $this->db->rollback();
970 return -1 * $error;
971 } else {
972 $this->db->commit();
973 return 1;
974 }
975 }
976
984 public function update($user = null, $notrigger = 0)
985 {
986 global $conf, $langs;
987 $error = 0;
988
989 $checkBalance = getDictionaryValue('c_holiday_types', 'block_if_negative', $this->fk_type);
990
991 if ($checkBalance > 0 && $this->statut != self::STATUS_DRAFT) {
992 $balance = $this->getCPforUser($this->fk_user, $this->fk_type);
993
994 if ($balance < 0) {
995 $this->error = 'LeaveRequestCreationBlockedBecauseBalanceIsNegative';
996 return -1;
997 }
998 }
999
1000 // Update request
1001 $sql = "UPDATE ".MAIN_DB_PREFIX."holiday SET";
1002
1003 $sql .= " description= '".$this->db->escape($this->description)."',";
1004
1005 if (!empty($this->date_debut)) {
1006 $sql .= " date_debut = '".$this->db->idate($this->date_debut)."',";
1007 } else {
1008 $error++;
1009 }
1010 if (!empty($this->date_fin)) {
1011 $sql .= " date_fin = '".$this->db->idate($this->date_fin)."',";
1012 } else {
1013 $error++;
1014 }
1015 $sql .= " halfday = ".$this->halfday.",";
1016 if (!empty($this->statut) && is_numeric($this->statut)) {
1017 $sql .= " statut = ".$this->statut.",";
1018 } else {
1019 $error++;
1020 }
1021 if (!empty($this->fk_validator)) {
1022 $sql .= " fk_validator = '".$this->db->escape($this->fk_validator)."',";
1023 } else {
1024 $error++;
1025 }
1026 if (!empty($this->date_valid)) {
1027 $sql .= " date_valid = '".$this->db->idate($this->date_valid)."',";
1028 } else {
1029 $sql .= " date_valid = NULL,";
1030 }
1031 if (!empty($this->fk_user_valid)) {
1032 $sql .= " fk_user_valid = ".((int) $this->fk_user_valid).",";
1033 } else {
1034 $sql .= " fk_user_valid = NULL,";
1035 }
1036 if (!empty($this->date_approval)) {
1037 $sql .= " date_approval = '".$this->db->idate($this->date_approval)."',";
1038 } else {
1039 $sql .= " date_approval = NULL,";
1040 }
1041 if (!empty($this->fk_user_approve)) {
1042 $sql .= " fk_user_approve = ".((int) $this->fk_user_approve).",";
1043 } else {
1044 $sql .= " fk_user_approve = NULL,";
1045 }
1046 if (!empty($this->date_refuse)) {
1047 $sql .= " date_refuse = '".$this->db->idate($this->date_refuse)."',";
1048 } else {
1049 $sql .= " date_refuse = NULL,";
1050 }
1051 if (!empty($this->fk_user_refuse)) {
1052 $sql .= " fk_user_refuse = ".((int) $this->fk_user_refuse).",";
1053 } else {
1054 $sql .= " fk_user_refuse = NULL,";
1055 }
1056 if (!empty($this->date_cancel)) {
1057 $sql .= " date_cancel = '".$this->db->idate($this->date_cancel)."',";
1058 } else {
1059 $sql .= " date_cancel = NULL,";
1060 }
1061 if (!empty($this->fk_user_cancel)) {
1062 $sql .= " fk_user_cancel = ".((int) $this->fk_user_cancel).",";
1063 } else {
1064 $sql .= " fk_user_cancel = NULL,";
1065 }
1066 if (!empty($this->detail_refuse)) {
1067 $sql .= " detail_refuse = '".$this->db->escape($this->detail_refuse)."'";
1068 } else {
1069 $sql .= " detail_refuse = NULL";
1070 }
1071
1072 $sql .= " WHERE rowid = ".((int) $this->id);
1073
1074 $this->db->begin();
1075
1076 dol_syslog(get_class($this)."::update", LOG_DEBUG);
1077 $resql = $this->db->query($sql);
1078 if (!$resql) {
1079 $error++;
1080 $this->errors[] = "Error ".$this->db->lasterror();
1081 }
1082
1083 if (!$error) {
1084 $result = $this->insertExtraFields();
1085 if ($result < 0) {
1086 $error++;
1087 }
1088 }
1089
1090 if (!$error) {
1091 if (!$notrigger) {
1092 // Call trigger
1093 $result = $this->call_trigger('HOLIDAY_MODIFY', $user);
1094 if ($result < 0) {
1095 $error++;
1096 }
1097 // End call triggers
1098 }
1099 }
1100
1101 // Commit or rollback
1102 if ($error) {
1103 foreach ($this->errors as $errmsg) {
1104 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1105 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1106 }
1107 $this->db->rollback();
1108 return -1 * $error;
1109 } else {
1110 $this->db->commit();
1111 return 1;
1112 }
1113 }
1114
1115
1123 public function delete($user, $notrigger = 0)
1124 {
1125 global $conf, $langs;
1126 $error = 0;
1127
1128 $sql = "DELETE FROM ".MAIN_DB_PREFIX."holiday";
1129 $sql .= " WHERE rowid=".((int) $this->id);
1130
1131 $this->db->begin();
1132
1133 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1134 $resql = $this->db->query($sql);
1135 if (!$resql) {
1136 $error++;
1137 $this->errors[] = "Error ".$this->db->lasterror();
1138 }
1139
1140 if (!$error) {
1141 if (!$notrigger) {
1142 // Call trigger
1143 $result = $this->call_trigger('HOLIDAY_DELETE', $user);
1144 if ($result < 0) {
1145 $error++;
1146 }
1147 // End call triggers
1148 }
1149 }
1150
1151 // Commit or rollback
1152 if ($error) {
1153 foreach ($this->errors as $errmsg) {
1154 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1155 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1156 }
1157 $this->db->rollback();
1158 return -1 * $error;
1159 } else {
1160 $this->db->commit();
1161 return 1;
1162 }
1163 }
1164
1178 public function verifDateHolidayCP($fk_user, $dateStart, $dateEnd, $halfday = 0)
1179 {
1180 $this->fetchByUser($fk_user, '', '');
1181
1182 foreach ($this->holiday as $infos_CP) {
1183 if ($infos_CP['statut'] == Holiday::STATUS_CANCELED) {
1184 continue; // ignore not validated holidays
1185 }
1186 if ($infos_CP['statut'] == Holiday::STATUS_REFUSED) {
1187 continue; // ignore refused holidays
1188 }
1189 //var_dump("--");
1190 //var_dump("old: ".dol_print_date($infos_CP['date_debut'],'dayhour').' '.dol_print_date($infos_CP['date_fin'],'dayhour').' '.$infos_CP['halfday']);
1191 //var_dump("new: ".dol_print_date($dateStart,'dayhour').' '.dol_print_date($dateEnd,'dayhour').' '.$halfday);
1192
1193 if ($halfday == 0) {
1194 if ($dateStart >= $infos_CP['date_debut'] && $dateStart <= $infos_CP['date_fin']) {
1195 return false;
1196 }
1197 if ($dateEnd <= $infos_CP['date_fin'] && $dateEnd >= $infos_CP['date_debut']) {
1198 return false;
1199 }
1200 } elseif ($halfday == -1) {
1201 // new start afternoon, new end afternoon
1202 if ($dateStart >= $infos_CP['date_debut'] && $dateStart <= $infos_CP['date_fin']) {
1203 if ($dateStart < $infos_CP['date_fin'] || in_array($infos_CP['halfday'], array(0, -1))) {
1204 return false;
1205 }
1206 }
1207 if ($dateEnd <= $infos_CP['date_fin'] && $dateEnd >= $infos_CP['date_debut']) {
1208 if ($dateStart < $dateEnd) {
1209 return false;
1210 }
1211 if ($dateEnd < $infos_CP['date_fin'] || in_array($infos_CP['halfday'], array(0, -1))) {
1212 return false;
1213 }
1214 }
1215 } elseif ($halfday == 1) {
1216 // new start morning, new end morning
1217 if ($dateStart >= $infos_CP['date_debut'] && $dateStart <= $infos_CP['date_fin']) {
1218 if ($dateStart < $dateEnd) {
1219 return false;
1220 }
1221 if ($dateStart > $infos_CP['date_debut'] || in_array($infos_CP['halfday'], array(0, 1))) {
1222 return false;
1223 }
1224 }
1225 if ($dateEnd <= $infos_CP['date_fin'] && $dateEnd >= $infos_CP['date_debut']) {
1226 if ($dateEnd > $infos_CP['date_debut'] || in_array($infos_CP['halfday'], array(0, 1))) {
1227 return false;
1228 }
1229 }
1230 } elseif ($halfday == 2) {
1231 // new start afternoon, new end morning
1232 if ($dateStart >= $infos_CP['date_debut'] && $dateStart <= $infos_CP['date_fin']) {
1233 if ($dateStart < $infos_CP['date_fin'] || in_array($infos_CP['halfday'], array(0, -1))) {
1234 return false;
1235 }
1236 }
1237 if ($dateEnd <= $infos_CP['date_fin'] && $dateEnd >= $infos_CP['date_debut']) {
1238 if ($dateEnd > $infos_CP['date_debut'] || in_array($infos_CP['halfday'], array(0, 1))) {
1239 return false;
1240 }
1241 }
1242 } else {
1243 dol_print_error('', 'Bad value of parameter halfday when calling function verifDateHolidayCP');
1244 }
1245 }
1246
1247 return true;
1248 }
1249
1250
1260 public function verifDateHolidayForTimestamp($fk_user, $timestamp, $status = '-1')
1261 {
1262 global $langs, $conf;
1263
1264 $isavailablemorning = true;
1265 $isavailableafternoon = true;
1266
1267 // Check into leave requests
1268 $sql = "SELECT cp.rowid, cp.date_debut as date_start, cp.date_fin as date_end, cp.halfday, cp.statut";
1269 $sql .= " FROM ".MAIN_DB_PREFIX."holiday as cp";
1270 $sql .= " WHERE cp.entity IN (".getEntity('holiday').")";
1271 $sql .= " AND cp.fk_user = ".(int) $fk_user;
1272 $sql .= " AND cp.date_debut <= '".$this->db->idate($timestamp)."' AND cp.date_fin >= '".$this->db->idate($timestamp)."'";
1273 if ($status != '-1') {
1274 $sql .= " AND cp.statut IN (".$this->db->sanitize($status).")";
1275 }
1276
1277 $resql = $this->db->query($sql);
1278 if ($resql) {
1279 $num_rows = $this->db->num_rows($resql); // Note, we can have 2 records if on is morning and the other one is afternoon
1280 if ($num_rows > 0) {
1281 $arrayofrecord = array();
1282 $i = 0;
1283 while ($i < $num_rows) {
1284 $obj = $this->db->fetch_object($resql);
1285
1286 // Note: $obj->halfday is 0:Full days, 2:Sart afternoon end morning, -1:Start afternoon, 1:End morning
1287 $arrayofrecord[$obj->rowid] = array('date_start'=>$this->db->jdate($obj->date_start), 'date_end'=>$this->db->jdate($obj->date_end), 'halfday'=>$obj->halfday);
1288 $i++;
1289 }
1290
1291 // We found a record, user is on holiday by default, so is not available is true.
1292 $isavailablemorning = true;
1293 foreach ($arrayofrecord as $record) {
1294 if ($timestamp == $record['date_start'] && $record['halfday'] == 2) {
1295 continue;
1296 }
1297 if ($timestamp == $record['date_start'] && $record['halfday'] == -1) {
1298 continue;
1299 }
1300 $isavailablemorning = false;
1301 break;
1302 }
1303 $isavailableafternoon = true;
1304 foreach ($arrayofrecord as $record) {
1305 if ($timestamp == $record['date_end'] && $record['halfday'] == 2) {
1306 continue;
1307 }
1308 if ($timestamp == $record['date_end'] && $record['halfday'] == 1) {
1309 continue;
1310 }
1311 $isavailableafternoon = false;
1312 break;
1313 }
1314 }
1315 } else {
1316 dol_print_error($this->db);
1317 }
1318
1319 $result = array('morning'=>$isavailablemorning, 'afternoon'=>$isavailableafternoon);
1320 if (!$isavailablemorning) {
1321 $result['morning_reason'] = 'leave_request';
1322 }
1323 if (!$isavailableafternoon) {
1324 $result['afternoon_reason'] = 'leave_request';
1325 }
1326 return $result;
1327 }
1328
1336 public function getTooltipContentArray($params)
1337 {
1338 global $conf, $langs;
1339
1340 $langs->load('holiday');
1341 $nofetch = !empty($params['nofetch']);
1342
1343 $datas = array();
1344 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Holiday").'</u>';
1345 if (isset($this->statut)) {
1346 $datas['picto'] .= ' '.$this->getLibStatut(5);
1347 }
1348 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1349 // show type for this record only in ajax to not overload lists
1350 if (!$nofetch && !empty($this->fk_type)) {
1351 $typeleaves = $this->getTypes(1, -1);
1352 $labeltoshow = (($typeleaves[$this->fk_type]['code'] && $langs->trans($typeleaves[$this->fk_type]['code']) != $typeleaves[$this->fk_type]['code']) ? $langs->trans($typeleaves[$this->fk_type]['code']) : $typeleaves[$this->fk_type]['label']);
1353 $datas['type'] = '<br><b>'.$langs->trans("Type") . ':</b> ' . (empty($labeltoshow) ? $langs->trans("TypeWasDisabledOrRemoved", $this->fk_type) : $labeltoshow);
1354 }
1355 if (isset($this->halfday) && !empty($this->date_debut) && !empty($this->date_fin)) {
1356 $listhalfday = array(
1357 'morning' => $langs->trans("Morning"),
1358 "afternoon" => $langs->trans("Afternoon")
1359 );
1360 $starthalfday = ($this->halfday == -1 || $this->halfday == 2) ? 'afternoon' : 'morning';
1361 $endhalfday = ($this->halfday == 1 || $this->halfday == 2) ? 'morning' : 'afternoon';
1362 $datas['date_start'] = '<br><b>'.$langs->trans('DateDebCP') . '</b>: '. dol_print_date($this->date_debut, 'day') . '&nbsp;&nbsp;<span class="opacitymedium">'.$langs->trans($listhalfday[$starthalfday]).'</span>';
1363 $datas['date_end'] = '<br><b>'.$langs->trans('DateFinCP') . '</b>: '. dol_print_date($this->date_fin, 'day') . '&nbsp;&nbsp;<span class="opacitymedium">'.$langs->trans($listhalfday[$endhalfday]).'</span>';
1364 }
1365
1366
1367 return $datas;
1368 }
1369
1379 public function getNomUrl($withpicto = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss = '')
1380 {
1381 global $conf, $langs, $hookmanager;
1382
1383 if (!empty($conf->dol_no_mouse_hover)) {
1384 $notooltip = 1; // Force disable tooltips
1385 }
1386
1387 $result = '';
1388 $params = [
1389 'id' => $this->id,
1390 'objecttype' => $this->element,
1391 'nofetch' => 1,
1392 ];
1393 $classfortooltip = 'classfortooltip';
1394 $dataparams = '';
1395 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1396 $classfortooltip = 'classforajaxtooltip';
1397 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1398 $label = '';
1399 } else {
1400 $label = implode($this->getTooltipContentArray($params));
1401 }
1402
1403 $url = DOL_URL_ROOT.'/holiday/card.php?id='.$this->id;
1404
1405 //if ($option != 'nolink')
1406 //{
1407 // Add param to save lastsearch_values or not
1408 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1409 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1410 $add_save_lastsearch_values = 1;
1411 }
1412 if ($add_save_lastsearch_values) {
1413 $url .= '&save_lastsearch_values=1';
1414 }
1415 //}
1416
1417 $linkclose = '';
1418 if (empty($notooltip)) {
1419 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1420 $label = $langs->trans("ShowMyObject");
1421 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1422 }
1423 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1424 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1425 } else {
1426 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1427 }
1428
1429 $linkstart = '<a href="'.$url.'"';
1430 $linkstart .= $linkclose.'>';
1431 $linkend = '</a>';
1432
1433 $result .= $linkstart;
1434
1435 if ($withpicto) {
1436 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1437 }
1438 if ($withpicto != 2) {
1439 $result .= $this->ref;
1440 }
1441 $result .= $linkend;
1442
1443 global $action;
1444 $hookmanager->initHooks(array($this->element . 'dao'));
1445 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
1446 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1447 if ($reshook > 0) {
1448 $result = $hookmanager->resPrint;
1449 } else {
1450 $result .= $hookmanager->resPrint;
1451 }
1452 return $result;
1453 }
1454
1455
1462 public function getLibStatut($mode = 0)
1463 {
1464 return $this->LibStatut($this->statut, $mode, $this->date_debut);
1465 }
1466
1467 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1476 public function LibStatut($status, $mode = 0, $startdate = '')
1477 {
1478 // phpcs:enable
1479 global $langs;
1480
1481 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1482 global $langs;
1483 //$langs->load("mymodule");
1484 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('DraftCP');
1485 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ToReviewCP');
1486 $this->labelStatus[self::STATUS_APPROVED] = $langs->transnoentitiesnoconv('ApprovedCP');
1487 $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('CancelCP');
1488 $this->labelStatus[self::STATUS_REFUSED] = $langs->transnoentitiesnoconv('RefuseCP');
1489 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('DraftCP');
1490 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ToReviewCP');
1491 $this->labelStatusShort[self::STATUS_APPROVED] = $langs->transnoentitiesnoconv('ApprovedCP');
1492 $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('CancelCP');
1493 $this->labelStatusShort[self::STATUS_REFUSED] = $langs->transnoentitiesnoconv('RefuseCP');
1494 }
1495
1496 $params = array();
1497 $statusType = 'status6';
1498 if (!empty($startdate) && $startdate >= dol_now()) { // If not yet passed, we use a green "in live" color
1499 $statusType = 'status4';
1500 $params = array('tooltip'=>$this->labelStatus[$status].' - '.$langs->trans("Forthcoming"));
1501 }
1502 if ($status == self::STATUS_DRAFT) {
1503 $statusType = 'status0';
1504 }
1505 if ($status == self::STATUS_VALIDATED) {
1506 $statusType = 'status1';
1507 }
1508 if ($status == self::STATUS_CANCELED) {
1509 $statusType = 'status9';
1510 }
1511 if ($status == self::STATUS_REFUSED) {
1512 $statusType = 'status9';
1513 }
1514
1515 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode, '', $params);
1516 }
1517
1518
1527 public function selectStatutCP($selected = '', $htmlname = 'select_statut', $morecss = 'minwidth125')
1528 {
1529 global $langs;
1530
1531 // List of status label
1532 $name = array('DraftCP', 'ToReviewCP', 'ApprovedCP', 'CancelCP', 'RefuseCP');
1533 $nb = count($name) + 1;
1534
1535 // Select HTML
1536 $out = '<select name="'.$htmlname.'" id="'.$htmlname.'" class="flat'.($morecss ? ' '.$morecss : '').'">'."\n";
1537 $out .= '<option value="-1">&nbsp;</option>'."\n";
1538
1539 // Loop on status
1540 for ($i = 1; $i < $nb; $i++) {
1541 if ($i == $selected) {
1542 $out .= '<option value="'.$i.'" selected>'.$langs->trans($name[$i - 1]).'</option>'."\n";
1543 } else {
1544 $out .= '<option value="'.$i.'">'.$langs->trans($name[$i - 1]).'</option>'."\n";
1545 }
1546 }
1547
1548 $out .= '</select>'."\n";
1549
1550 $showempty= 0;
1551 $out .= ajax_combobox($htmlname, array(), 0, 0, 'resolve', ($showempty < 0 ? (string) $showempty : '-1'), $morecss);
1552
1553 return $out;
1554 }
1555
1563 public function updateConfCP($name, $value)
1564 {
1565 $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
1566 $sql .= " value = '".$this->db->escape($value)."'";
1567 $sql .= " WHERE name = '".$this->db->escape($name)."'";
1568
1569 dol_syslog(get_class($this).'::updateConfCP name='.$name, LOG_DEBUG);
1570 $result = $this->db->query($sql);
1571 if ($result) {
1572 return true;
1573 }
1574
1575 return false;
1576 }
1577
1586 public function getConfCP($name, $createifnotfound = '')
1587 {
1588 $sql = "SELECT value";
1589 $sql .= " FROM ".MAIN_DB_PREFIX."holiday_config";
1590 $sql .= " WHERE name = '".$this->db->escape($name)."'";
1591
1592 dol_syslog(get_class($this).'::getConfCP name='.$name.' createifnotfound='.$createifnotfound, LOG_DEBUG);
1593 $result = $this->db->query($sql);
1594
1595 if ($result) {
1596 $obj = $this->db->fetch_object($result);
1597 // Return value
1598 if (empty($obj)) {
1599 if ($createifnotfound) {
1600 $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_config(name, value)";
1601 $sql .= " VALUES('".$this->db->escape($name)."', '".$this->db->escape($createifnotfound)."')";
1602 $result = $this->db->query($sql);
1603 if ($result) {
1604 return $createifnotfound;
1605 } else {
1606 $this->error = $this->db->lasterror();
1607 return -2;
1608 }
1609 } else {
1610 return '';
1611 }
1612 } else {
1613 return $obj->value;
1614 }
1615 } else {
1616 // Erreur SQL
1617 $this->error = $this->db->lasterror();
1618 return -1;
1619 }
1620 }
1621
1630 public function updateSoldeCP($userID = '', $nbHoliday = '', $fk_type = '')
1631 {
1632 global $user, $langs;
1633
1634 $error = 0;
1635
1636 if (empty($userID) && empty($nbHoliday) && empty($fk_type)) {
1637 $langs->load("holiday");
1638
1639 // Si mise à jour pour tout le monde en début de mois
1640 $now = dol_now();
1641
1642 $month = date('m', $now);
1643 $newdateforlastupdate = dol_print_date($now, '%Y%m%d%H%M%S');
1644
1645 // Get month of last update
1646 $lastUpdate = $this->getConfCP('lastUpdate', $newdateforlastupdate);
1647 $monthLastUpdate = $lastUpdate[4].$lastUpdate[5];
1648 //print 'month: '.$month.' lastUpdate:'.$lastUpdate.' monthLastUpdate:'.$monthLastUpdate;exit;
1649
1650 // If month date is not same than the one of last update (the one we saved in database), then we update the timestamp and balance of each open user.
1651 if ($month != $monthLastUpdate) {
1652 $this->db->begin();
1653
1654 $users = $this->fetchUsers(false, false, ' AND u.statut > 0');
1655 $nbUser = count($users);
1656
1657 $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
1658 $sql .= " value = '".$this->db->escape($newdateforlastupdate)."'";
1659 $sql .= " WHERE name = 'lastUpdate'";
1660 $result = $this->db->query($sql);
1661
1662 $typeleaves = $this->getTypes(1, 1);
1663
1664 // Update each user counter
1665 foreach ($users as $userCounter) {
1666 $nbDaysToAdd = (isset($typeleaves[$userCounter['type']]['newbymonth']) ? $typeleaves[$userCounter['type']]['newbymonth'] : 0);
1667 if (empty($nbDaysToAdd)) {
1668 continue;
1669 }
1670
1671 dol_syslog("We update leave type id ".$userCounter['type']." for user id ".$userCounter['rowid'], LOG_DEBUG);
1672
1673 $nowHoliday = $userCounter['nb_holiday'];
1674 $newSolde = $nowHoliday + $nbDaysToAdd;
1675
1676 // We add a log for each user
1677 $this->addLogCP($user->id, $userCounter['rowid'], $langs->trans('HolidaysMonthlyUpdate'), $newSolde, $userCounter['type']);
1678
1679 $result = $this->updateSoldeCP($userCounter['rowid'], $newSolde, $userCounter['type']);
1680
1681 if ($result < 0) {
1682 $error++;
1683 break;
1684 }
1685 }
1686
1687 if (!$error) {
1688 $this->db->commit();
1689 return 1;
1690 } else {
1691 $this->db->rollback();
1692 return -1;
1693 }
1694 }
1695
1696 return 0;
1697 } else {
1698 // Mise à jour pour un utilisateur
1699 $nbHoliday = price2num($nbHoliday, 5);
1700
1701 $sql = "SELECT nb_holiday FROM ".MAIN_DB_PREFIX."holiday_users";
1702 $sql .= " WHERE fk_user = ".(int) $userID." AND fk_type = ".(int) $fk_type;
1703 $resql = $this->db->query($sql);
1704 if ($resql) {
1705 $num = $this->db->num_rows($resql);
1706
1707 if ($num > 0) {
1708 // Update for user
1709 $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_users SET";
1710 $sql .= " nb_holiday = ".((float) $nbHoliday);
1711 $sql .= " WHERE fk_user = ".(int) $userID." AND fk_type = ".(int) $fk_type;
1712 $result = $this->db->query($sql);
1713 if (!$result) {
1714 $error++;
1715 $this->errors[] = $this->db->lasterror();
1716 }
1717 } else {
1718 // Insert for user
1719 $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_users(nb_holiday, fk_user, fk_type) VALUES (";
1720 $sql .= ((float) $nbHoliday);
1721 $sql .= ", ".(int) $userID.", ".(int) $fk_type.")";
1722 $result = $this->db->query($sql);
1723 if (!$result) {
1724 $error++;
1725 $this->errors[] = $this->db->lasterror();
1726 }
1727 }
1728 } else {
1729 $this->errors[] = $this->db->lasterror();
1730 $error++;
1731 }
1732
1733 if (!$error) {
1734 return 1;
1735 } else {
1736 return -1;
1737 }
1738 }
1739 }
1740
1748 public function createCPusers($single = false, $userid = '')
1749 {
1750 // do we have to add balance for all users ?
1751 if (!$single) {
1752 dol_syslog(get_class($this).'::createCPusers');
1753 $arrayofusers = $this->fetchUsers(false, true);
1754
1755 foreach ($arrayofusers as $users) {
1756 $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_users";
1757 $sql .= " (fk_user, nb_holiday)";
1758 $sql .= " VALUES (".((int) $users['rowid'])."', '0')";
1759
1760 $resql = $this->db->query($sql);
1761 if (!$resql) {
1762 dol_print_error($this->db);
1763 }
1764 }
1765 } else {
1766 $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_users";
1767 $sql .= " (fk_user, nb_holiday)";
1768 $sql .= " VALUES (".((int) $userid)."', '0')";
1769
1770 $resql = $this->db->query($sql);
1771 if (!$resql) {
1772 dol_print_error($this->db);
1773 }
1774 }
1775 }
1776
1784 public function getCPforUser($user_id, $fk_type = 0)
1785 {
1786 $sql = "SELECT nb_holiday";
1787 $sql .= " FROM ".MAIN_DB_PREFIX."holiday_users";
1788 $sql .= " WHERE fk_user = ".(int) $user_id;
1789 if ($fk_type > 0) {
1790 $sql .= " AND fk_type = ".(int) $fk_type;
1791 }
1792
1793 dol_syslog(get_class($this).'::getCPforUser user_id='.$user_id.' type_id='.$fk_type, LOG_DEBUG);
1794 $result = $this->db->query($sql);
1795 if ($result) {
1796 $obj = $this->db->fetch_object($result);
1797 //return number_format($obj->nb_holiday,2);
1798 if ($obj) {
1799 return $obj->nb_holiday;
1800 } else {
1801 return null;
1802 }
1803 } else {
1804 return null;
1805 }
1806 }
1807
1816 public function fetchUsers($stringlist = true, $type = true, $filters = '')
1817 {
1818 global $conf;
1819
1820 dol_syslog(get_class($this)."::fetchUsers", LOG_DEBUG);
1821
1822 if ($stringlist) {
1823 if ($type) {
1824 // If user of Dolibarr
1825 $sql = "SELECT";
1826 if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
1827 $sql .= " DISTINCT";
1828 }
1829 $sql .= " u.rowid";
1830 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1831
1832 if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
1833 $sql .= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
1834 $sql .= " WHERE ((ug.fk_user = u.rowid";
1835 $sql .= " AND ug.entity IN (".getEntity('usergroup')."))";
1836 $sql .= " OR u.entity = 0)"; // Show always superadmin
1837 } else {
1838 $sql .= " WHERE u.entity IN (".getEntity('user').")";
1839 }
1840 $sql .= " AND u.statut > 0";
1841 $sql .= " AND u.employee = 1"; // We only want employee users for holidays
1842 if ($filters) {
1843 $sql .= $filters;
1844 }
1845
1846 $resql = $this->db->query($sql);
1847
1848 // Si pas d'erreur SQL
1849 if ($resql) {
1850 $i = 0;
1851 $num = $this->db->num_rows($resql);
1852 $stringlist = '';
1853
1854 // Boucles du listage des utilisateurs
1855 while ($i < $num) {
1856 $obj = $this->db->fetch_object($resql);
1857
1858 if ($i == 0) {
1859 $stringlist .= $obj->rowid;
1860 } else {
1861 $stringlist .= ', '.$obj->rowid;
1862 }
1863
1864 $i++;
1865 }
1866 // Retoune le tableau des utilisateurs
1867 return $stringlist;
1868 } else {
1869 // Erreur SQL
1870 $this->error = "Error ".$this->db->lasterror();
1871 return -1;
1872 }
1873 } else {
1874 // We want only list of vacation balance for user ids
1875 $sql = "SELECT DISTINCT cpu.fk_user";
1876 $sql .= " FROM ".MAIN_DB_PREFIX."holiday_users as cpu, ".MAIN_DB_PREFIX."user as u";
1877 $sql .= " WHERE cpu.fk_user = u.rowid";
1878 if ($filters) {
1879 $sql .= $filters;
1880 }
1881
1882 $resql = $this->db->query($sql);
1883
1884 // Si pas d'erreur SQL
1885 if ($resql) {
1886 $i = 0;
1887 $num = $this->db->num_rows($resql);
1888 $stringlist = '';
1889
1890 // Boucles du listage des utilisateurs
1891 while ($i < $num) {
1892 $obj = $this->db->fetch_object($resql);
1893
1894 if ($i == 0) {
1895 $stringlist .= $obj->fk_user;
1896 } else {
1897 $stringlist .= ', '.$obj->fk_user;
1898 }
1899
1900 $i++;
1901 }
1902 // Retoune le tableau des utilisateurs
1903 return $stringlist;
1904 } else {
1905 // Erreur SQL
1906 $this->error = "Error ".$this->db->lasterror();
1907 return -1;
1908 }
1909 }
1910 } else {
1911 // Si faux donc return array
1912 // List for Dolibarr users
1913 if ($type) {
1914 // If we need users of Dolibarr
1915 $sql = "SELECT";
1916 if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
1917 $sql .= " DISTINCT";
1918 }
1919 $sql .= " u.rowid, u.lastname, u.firstname, u.gender, u.photo, u.employee, u.statut, u.fk_user";
1920 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
1921
1922 if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
1923 $sql .= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
1924 $sql .= " WHERE ((ug.fk_user = u.rowid";
1925 $sql .= " AND ug.entity IN (".getEntity('usergroup')."))";
1926 $sql .= " OR u.entity = 0)"; // Show always superadmin
1927 } else {
1928 $sql .= " WHERE u.entity IN (".getEntity('user').")";
1929 }
1930
1931 $sql .= " AND u.statut > 0";
1932 $sql .= " AND u.employee = 1"; // We only want employee users for holidays
1933 if ($filters) {
1934 $sql .= $filters;
1935 }
1936
1937 $resql = $this->db->query($sql);
1938
1939 // Si pas d'erreur SQL
1940 if ($resql) {
1941 $i = 0;
1942 $tab_result = $this->holiday;
1943 $num = $this->db->num_rows($resql);
1944
1945 // Boucles du listage des utilisateurs
1946 while ($i < $num) {
1947 $obj = $this->db->fetch_object($resql);
1948
1949 $tab_result[$i]['rowid'] = $obj->rowid; // rowid of user
1950 $tab_result[$i]['id'] = $obj->rowid; // id of user
1951 $tab_result[$i]['name'] = $obj->lastname; // deprecated
1952 $tab_result[$i]['lastname'] = $obj->lastname;
1953 $tab_result[$i]['firstname'] = $obj->firstname;
1954 $tab_result[$i]['gender'] = $obj->gender;
1955 $tab_result[$i]['status'] = $obj->statut;
1956 $tab_result[$i]['employee'] = $obj->employee;
1957 $tab_result[$i]['photo'] = $obj->photo;
1958 $tab_result[$i]['fk_user'] = $obj->fk_user; // rowid of manager
1959 //$tab_result[$i]['type'] = $obj->type;
1960 //$tab_result[$i]['nb_holiday'] = $obj->nb_holiday;
1961
1962 $i++;
1963 }
1964 // Retoune le tableau des utilisateurs
1965 return $tab_result;
1966 } else {
1967 // Erreur SQL
1968 $this->errors[] = "Error ".$this->db->lasterror();
1969 return -1;
1970 }
1971 } else {
1972 // List of vacation balance users
1973 $sql = "SELECT cpu.fk_type, cpu.nb_holiday, u.rowid, u.lastname, u.firstname, u.gender, u.photo, u.employee, u.statut, u.fk_user";
1974 $sql .= " FROM ".MAIN_DB_PREFIX."holiday_users as cpu, ".MAIN_DB_PREFIX."user as u";
1975 $sql .= " WHERE cpu.fk_user = u.rowid";
1976 if ($filters) {
1977 $sql .= $filters;
1978 }
1979
1980 $resql = $this->db->query($sql);
1981
1982 // Si pas d'erreur SQL
1983 if ($resql) {
1984 $i = 0;
1985 $tab_result = $this->holiday;
1986 $num = $this->db->num_rows($resql);
1987
1988 // Boucles du listage des utilisateurs
1989 while ($i < $num) {
1990 $obj = $this->db->fetch_object($resql);
1991
1992 $tab_result[$i]['rowid'] = $obj->rowid; // rowid of user
1993 $tab_result[$i]['id'] = $obj->rowid; // id of user
1994 $tab_result[$i]['name'] = $obj->lastname; // deprecated
1995 $tab_result[$i]['lastname'] = $obj->lastname;
1996 $tab_result[$i]['firstname'] = $obj->firstname;
1997 $tab_result[$i]['gender'] = $obj->gender;
1998 $tab_result[$i]['status'] = $obj->statut;
1999 $tab_result[$i]['employee'] = $obj->employee;
2000 $tab_result[$i]['photo'] = $obj->photo;
2001 $tab_result[$i]['fk_user'] = $obj->fk_user; // rowid of manager
2002
2003 $tab_result[$i]['type'] = $obj->fk_type;
2004 $tab_result[$i]['nb_holiday'] = $obj->nb_holiday;
2005
2006 $i++;
2007 }
2008 // Retoune le tableau des utilisateurs
2009 return $tab_result;
2010 } else {
2011 // Erreur SQL
2012 $this->error = "Error ".$this->db->lasterror();
2013 return -1;
2014 }
2015 }
2016 }
2017 }
2018
2019
2020 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2028 {
2029 // phpcs:enable
2030 $users_validator = array();
2031
2032 $sql = "SELECT DISTINCT ur.fk_user";
2033 $sql .= " FROM ".MAIN_DB_PREFIX."user_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
2034 $sql .= " WHERE ur.fk_id = rd.id and rd.module = 'holiday' AND rd.perms = 'approve'"; // Permission 'Approve';
2035 $sql .= "UNION";
2036 $sql .= " SELECT DISTINCT ugu.fk_user";
2037 $sql .= " FROM ".MAIN_DB_PREFIX."usergroup_user as ugu, ".MAIN_DB_PREFIX."usergroup_rights as ur, ".MAIN_DB_PREFIX."rights_def as rd";
2038 $sql .= " WHERE ugu.fk_usergroup = ur.fk_usergroup AND ur.fk_id = rd.id and rd.module = 'holiday' AND rd.perms = 'approve'"; // Permission 'Approve';
2039 //print $sql;
2040
2041 dol_syslog(get_class($this)."::fetch_users_approver_holiday sql=".$sql);
2042 $result = $this->db->query($sql);
2043 if ($result) {
2044 $num_rows = $this->db->num_rows($result);
2045 $i = 0;
2046 while ($i < $num_rows) {
2047 $objp = $this->db->fetch_object($result);
2048 array_push($users_validator, $objp->fk_user);
2049 $i++;
2050 }
2051 return $users_validator;
2052 } else {
2053 $this->error = $this->db->lasterror();
2054 dol_syslog(get_class($this)."::fetch_users_approver_holiday Error ".$this->error, LOG_ERR);
2055 return -1;
2056 }
2057 }
2058
2059
2065 public function countActiveUsers()
2066 {
2067 $sql = "SELECT count(u.rowid) as compteur";
2068 $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
2069 $sql .= " WHERE u.statut > 0";
2070
2071 $result = $this->db->query($sql);
2072 $objet = $this->db->fetch_object($result);
2073
2074 return $objet->compteur;
2075 }
2082 {
2083 $sql = "SELECT count(u.rowid) as compteur";
2084 $sql .= " FROM ".MAIN_DB_PREFIX."user as u LEFT OUTER JOIN ".MAIN_DB_PREFIX."holiday_users hu ON (hu.fk_user=u.rowid)";
2085 $sql .= " WHERE u.statut > 0 AND hu.fk_user IS NULL";
2086
2087 $result = $this->db->query($sql);
2088 $objet = $this->db->fetch_object($result);
2089
2090 return $objet->compteur;
2091 }
2092
2100 public function verifNbUsers($userDolibarrWithoutCP, $userCP)
2101 {
2102 if (empty($userCP)) {
2103 $userCP = 0;
2104 }
2105 dol_syslog(get_class($this).'::verifNbUsers userDolibarr='.$userDolibarrWithoutCP.' userCP='.$userCP);
2106 return 1;
2107 }
2108
2109
2120 public function addLogCP($fk_user_action, $fk_user_update, $label, $new_solde, $fk_type)
2121 {
2122 global $conf, $langs;
2123
2124 $error = 0;
2125
2126 $prev_solde = price2num($this->getCPforUser($fk_user_update, $fk_type), 5);
2127 $new_solde = price2num($new_solde, 5);
2128 //print "$prev_solde == $new_solde";
2129
2130 if ($prev_solde == $new_solde) {
2131 return 0;
2132 }
2133
2134 $this->db->begin();
2135
2136 // Insert request
2137 $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_logs (";
2138 $sql .= "date_action,";
2139 $sql .= "fk_user_action,";
2140 $sql .= "fk_user_update,";
2141 $sql .= "type_action,";
2142 $sql .= "prev_solde,";
2143 $sql .= "new_solde,";
2144 $sql .= "fk_type";
2145 $sql .= ") VALUES (";
2146 $sql .= " '".$this->db->idate(dol_now())."',";
2147 $sql .= " ".((int) $fk_user_action).",";
2148 $sql .= " ".((int) $fk_user_update).",";
2149 $sql .= " '".$this->db->escape($label)."',";
2150 $sql .= " ".((float) $prev_solde).",";
2151 $sql .= " ".((float) $new_solde).",";
2152 $sql .= " ".((int) $fk_type);
2153 $sql .= ")";
2154
2155 $resql = $this->db->query($sql);
2156 if (!$resql) {
2157 $error++;
2158 $this->errors[] = "Error ".$this->db->lasterror();
2159 }
2160
2161 if (!$error) {
2162 $this->optRowid = $this->db->last_insert_id(MAIN_DB_PREFIX."holiday_logs");
2163 }
2164
2165 // Commit or rollback
2166 if ($error) {
2167 foreach ($this->errors as $errmsg) {
2168 dol_syslog(get_class($this)."::addLogCP ".$errmsg, LOG_ERR);
2169 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2170 }
2171 $this->db->rollback();
2172 return -1 * $error;
2173 } else {
2174 $this->db->commit();
2175 return $this->optRowid;
2176 }
2177 }
2178
2186 public function fetchLog($order, $filter)
2187 {
2188 $sql = "SELECT";
2189 $sql .= " cpl.rowid,";
2190 $sql .= " cpl.date_action,";
2191 $sql .= " cpl.fk_user_action,";
2192 $sql .= " cpl.fk_user_update,";
2193 $sql .= " cpl.type_action,";
2194 $sql .= " cpl.prev_solde,";
2195 $sql .= " cpl.new_solde,";
2196 $sql .= " cpl.fk_type";
2197 $sql .= " FROM ".MAIN_DB_PREFIX."holiday_logs as cpl";
2198 $sql .= " WHERE cpl.rowid > 0"; // To avoid error with other search and criteria
2199
2200 // Filtrage de séléction
2201 if (!empty($filter)) {
2202 $sql .= " ".$filter;
2203 }
2204
2205 // Ordre d'affichage
2206 if (!empty($order)) {
2207 $sql .= " ".$order;
2208 }
2209
2210 dol_syslog(get_class($this)."::fetchLog", LOG_DEBUG);
2211 $resql = $this->db->query($sql);
2212
2213 // Si pas d'erreur SQL
2214 if ($resql) {
2215 $i = 0;
2216 $tab_result = $this->logs;
2217 $num = $this->db->num_rows($resql);
2218
2219 // Si pas d'enregistrement
2220 if (!$num) {
2221 return 2;
2222 }
2223
2224 // On liste les résultats et on les ajoutent dans le tableau
2225 while ($i < $num) {
2226 $obj = $this->db->fetch_object($resql);
2227
2228 $tab_result[$i]['rowid'] = $obj->rowid;
2229 $tab_result[$i]['id'] = $obj->rowid;
2230 $tab_result[$i]['date_action'] = $obj->date_action;
2231 $tab_result[$i]['fk_user_action'] = $obj->fk_user_action;
2232 $tab_result[$i]['fk_user_update'] = $obj->fk_user_update;
2233 $tab_result[$i]['type_action'] = $obj->type_action;
2234 $tab_result[$i]['prev_solde'] = $obj->prev_solde;
2235 $tab_result[$i]['new_solde'] = $obj->new_solde;
2236 $tab_result[$i]['fk_type'] = $obj->fk_type;
2237
2238 $i++;
2239 }
2240 // Retourne 1 et ajoute le tableau à la variable
2241 $this->logs = $tab_result;
2242 return 1;
2243 } else {
2244 // Erreur SQL
2245 $this->error = "Error ".$this->db->lasterror();
2246 return -1;
2247 }
2248 }
2249
2250
2258 public function getTypes($active = -1, $affect = -1)
2259 {
2260 global $mysoc;
2261
2262 $sql = "SELECT rowid, code, label, affect, delay, newbymonth";
2263 $sql .= " FROM ".MAIN_DB_PREFIX."c_holiday_types";
2264 $sql .= " WHERE (fk_country IS NULL OR fk_country = ".((int) $mysoc->country_id).')';
2265 if ($active >= 0) {
2266 $sql .= " AND active = ".((int) $active);
2267 }
2268 if ($affect >= 0) {
2269 $sql .= " AND affect = ".((int) $affect);
2270 }
2271 $sql .= " ORDER BY sortorder";
2272
2273 $result = $this->db->query($sql);
2274 if ($result) {
2275 $num = $this->db->num_rows($result);
2276 if ($num) {
2277 while ($obj = $this->db->fetch_object($result)) {
2278 $types[$obj->rowid] = array('id'=> $obj->rowid, 'rowid'=> $obj->rowid, 'code'=> $obj->code, 'label'=>$obj->label, 'affect'=>$obj->affect, 'delay'=>$obj->delay, 'newbymonth'=>$obj->newbymonth);
2279 }
2280
2281 return $types;
2282 }
2283 } else {
2284 dol_print_error($this->db);
2285 }
2286
2287 return array();
2288 }
2289
2290
2297 public function info($id)
2298 {
2299 global $conf;
2300
2301 $sql = "SELECT f.rowid, f.statut as status,";
2302 $sql .= " f.date_create as datec,";
2303 $sql .= " f.tms as date_modification,";
2304 $sql .= " f.date_valid as datev,";
2305 $sql .= " f.date_approval as datea,";
2306 $sql .= " f.date_refuse as dater,";
2307 $sql .= " f.fk_user_create as fk_user_creation,";
2308 $sql .= " f.fk_user_modif as fk_user_modification,";
2309 $sql .= " f.fk_user_valid as fk_user_validation,";
2310 $sql .= " f.fk_user_approve as fk_user_approval_done,";
2311 $sql .= " f.fk_validator as fk_user_approval_expected,";
2312 $sql .= " f.fk_user_refuse as fk_user_refuse";
2313 $sql .= " FROM ".MAIN_DB_PREFIX."holiday as f";
2314 $sql .= " WHERE f.rowid = ".((int) $id);
2315 $sql .= " AND f.entity = ".$conf->entity;
2316
2317 $resql = $this->db->query($sql);
2318 if ($resql) {
2319 if ($this->db->num_rows($resql)) {
2320 $obj = $this->db->fetch_object($resql);
2321
2322 $this->id = $obj->rowid;
2323
2324 $this->date_creation = $this->db->jdate($obj->datec);
2325 $this->date_modification = $this->db->jdate($obj->date_modification);
2326 $this->date_validation = $this->db->jdate($obj->datev);
2327 $this->date_approval = $this->db->jdate($obj->datea);
2328
2329 $this->user_creation_id = $obj->fk_user_creation;
2330 $this->user_validation_id = $obj->fk_user_valid;
2331 $this->user_modification_id = $obj->fk_user_modification;
2332
2333 if ($obj->status == Holiday::STATUS_APPROVED || $obj->status == Holiday::STATUS_CANCELED) {
2334 if ($obj->fk_user_approval_done) {
2335 $this->fk_user_approve = $obj->fk_user_approval_done;
2336 }
2337 }
2338 }
2339 $this->db->free($resql);
2340 } else {
2341 dol_print_error($this->db);
2342 }
2343 }
2344
2345
2353 public function initAsSpecimen()
2354 {
2355 global $user, $langs;
2356
2357 // Initialise parameters
2358 $this->id = 0;
2359 $this->specimen = 1;
2360
2361 $this->fk_user = $user->id;
2362 $this->description = 'SPECIMEN description';
2363 $this->date_debut = dol_now();
2364 $this->date_fin = dol_now() + (24 * 3600);
2365 $this->date_valid = dol_now();
2366 $this->fk_validator = $user->id;
2367 $this->halfday = 0;
2368 $this->fk_type = 1;
2370 }
2371
2372 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2378 public function load_state_board()
2379 {
2380 // phpcs:enable
2381 global $user;
2382
2383 $this->nb = array();
2384
2385 $sql = "SELECT count(h.rowid) as nb";
2386 $sql .= " FROM ".MAIN_DB_PREFIX."holiday as h";
2387 $sql .= " WHERE h.statut > 1";
2388 $sql .= " AND h.entity IN (".getEntity('holiday').")";
2389 if (!$user->hasRight('expensereport', 'readall')) {
2390 $userchildids = $user->getAllChildIds(1);
2391 $sql .= " AND (h.fk_user IN (".$this->db->sanitize(join(',', $userchildids)).")";
2392 $sql .= " OR h.fk_validator IN (".$this->db->sanitize(join(',', $userchildids))."))";
2393 }
2394
2395 $resql = $this->db->query($sql);
2396 if ($resql) {
2397 while ($obj = $this->db->fetch_object($resql)) {
2398 $this->nb["holidays"] = $obj->nb;
2399 }
2400 $this->db->free($resql);
2401 return 1;
2402 } else {
2403 dol_print_error($this->db);
2404 $this->error = $this->db->error();
2405 return -1;
2406 }
2407 }
2408
2409 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2416 public function load_board($user)
2417 {
2418 // phpcs:enable
2419 global $conf, $langs;
2420
2421 if ($user->socid) {
2422 return -1; // protection pour eviter appel par utilisateur externe
2423 }
2424
2425 $now = dol_now();
2426
2427 $sql = "SELECT h.rowid, h.date_debut";
2428 $sql .= " FROM ".MAIN_DB_PREFIX."holiday as h";
2429 $sql .= " WHERE h.statut = 2";
2430 $sql .= " AND h.entity IN (".getEntity('holiday').")";
2431 if (!$user->hasRight('expensereport', 'read_all')) {
2432 $userchildids = $user->getAllChildIds(1);
2433 $sql .= " AND (h.fk_user IN (".$this->db->sanitize(join(',', $userchildids)).")";
2434 $sql .= " OR h.fk_validator IN (".$this->db->sanitize(join(',', $userchildids))."))";
2435 }
2436
2437 $resql = $this->db->query($sql);
2438 if ($resql) {
2439 $langs->load("members");
2440
2441 $response = new WorkboardResponse();
2442 $response->warning_delay = $conf->holiday->approve->warning_delay / 60 / 60 / 24;
2443 $response->label = $langs->trans("HolidaysToApprove");
2444 $response->labelShort = $langs->trans("ToApprove");
2445 $response->url = DOL_URL_ROOT.'/holiday/list.php?search_status=2&amp;mainmenu=hrm&amp;leftmenu=holiday';
2446 $response->img = img_object('', "holiday");
2447
2448 while ($obj = $this->db->fetch_object($resql)) {
2449 $response->nbtodo++;
2450
2451 if ($this->db->jdate($obj->date_debut) < ($now - $conf->holiday->approve->warning_delay)) {
2452 $response->nbtodolate++;
2453 }
2454 }
2455
2456 return $response;
2457 } else {
2458 dol_print_error($this->db);
2459 $this->error = $this->db->error();
2460 return -1;
2461 }
2462 }
2470 public function getKanbanView($option = '', $arraydata = null)
2471 {
2472 global $langs;
2473
2474 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2475
2476 $return = '<div class="box-flex-item box-flex-grow-zero">';
2477 $return .= '<div class="info-box info-box-sm">';
2478 $return .= '<span class="info-box-icon bg-infobox-action">';
2479 $return .= img_picto('', $this->picto);
2480 $return .= '</span>';
2481 $return .= '<div class="info-box-content">';
2482 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.$arraydata['user']->getNomUrl(-1).'</span>';
2483 if ($selected >= 0) {
2484 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2485 }
2486 if (property_exists($this, 'fk_type')) {
2487 $return .= '<br>';
2488 //$return .= '<span class="opacitymedium">'.$langs->trans("Type").'</span> : ';
2489 $return .= '<div class="info_box-label tdoverflowmax100" title="'.dol_escape_htmltag($arraydata['labeltype']).'">'.dol_escape_htmltag($arraydata['labeltype']).'</div>';
2490 }
2491 if (property_exists($this, 'date_debut') && property_exists($this, 'date_fin')) {
2492 $return .= '<span class="info-box-label small">'.dol_print_date($this->date_debut, 'day').'</span>';
2493 $return .= ' <span class="opacitymedium small">'.$langs->trans("To").'</span> ';
2494 $return .= '<span class="info-box-label small">'.dol_print_date($this->date_fin, 'day').'</span>';
2495 if (!empty($arraydata['nbopenedday'])) {
2496 $return .= ' ('.$arraydata['nbopenedday'].')';
2497 }
2498 }
2499 if (method_exists($this, 'getLibStatut')) {
2500 $return .= '<div class="info-box-status">'.$this->getLibStatut(3).'</div>';
2501 }
2502 $return .= '</div>';
2503 $return .= '</div>';
2504 $return .= '</div>';
2505 return $return;
2506 }
2507}
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:455
$object ref
Definition info.php:79
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class of the module paid holiday.
getTypes($active=-1, $affect=-1)
Return array with list of types.
getNomUrl($withpicto=0, $save_lastsearch_value=-1, $notooltip=0, $morecss='')
Return clicable name (with picto eventually)
verifDateHolidayCP($fk_user, $dateStart, $dateEnd, $halfday=0)
Check if a user is on holiday (partially or completely) into a period.
validate($user=null, $notrigger=0)
Validate leave request.
info($id)
Load information on object.
updateBalance()
Update balance of vacations and check table of users for holidays is complete.
updateConfCP($name, $value)
Met à jour une option du module Holiday Payés.
const STATUS_VALIDATED
Validated status.
load_state_board()
Load this->nb for dashboard.
const STATUS_DRAFT
Draft status.
createCPusers($single=false, $userid='')
Create entries for each user at setup step.
fetch($id, $ref='')
Load object in memory from database.
addLogCP($fk_user_action, $fk_user_update, $label, $new_solde, $fk_type)
addLogCP
verifNbUsers($userDolibarrWithoutCP, $userCP)
Compare le nombre d'utilisateur actif de Dolibarr à celui des utilisateurs des congés payés.
verifDateHolidayForTimestamp($fk_user, $timestamp, $status='-1')
Check that a user is not on holiday for a particular timestamp.
updateSoldeCP($userID='', $nbHoliday='', $fk_type='')
Met à jour le timestamp de la dernière mise à jour du solde des CP.
approve($user=null, $notrigger=0)
Approve leave request.
getNextNumRef($objsoc)
Returns the reference to the following non used Order depending on the active numbering module define...
const STATUS_REFUSED
Refused.
getConfCP($name, $createifnotfound='')
Return value of a conf parameter for leave module TODO Move this into llx_const table.
load_board($user)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
initAsSpecimen()
Initialise an instance with random values.
selectStatutCP($selected='', $htmlname='select_statut', $morecss='minwidth125')
Show select with list of leave status.
create($user, $notrigger=0)
Créer un congés payés dans la base de données.
countActiveUsersWithoutCP()
Compte le nombre d'utilisateur actifs dans Dolibarr sans CP.
fetchLog($order, $filter)
Liste le log des congés payés.
update($user=null, $notrigger=0)
Update database.
getCPforUser($user_id, $fk_type=0)
Return balance of holiday for one user.
fetch_users_approver_holiday()
Return list of people with permission to validate leave requests.
__construct($db)
Constructor.
getLibStatut($mode=0)
Returns the label status.
fetchAll($order, $filter)
List all holidays of all users.
const STATUS_CANCELED
Canceled.
countActiveUsers()
Compte le nombre d'utilisateur actifs dans Dolibarr.
fetchByUser($user_id, $order='', $filter='')
List holidays for a particular user or list of users.
const STATUS_APPROVED
Approved.
getTooltipContentArray($params)
getTooltipContentArray
fetchUsers($stringlist=true, $type=true, $filters='')
Get list of Users or list of vacation balance.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
LibStatut($status, $mode=0, $startdate='')
Returns the label of a status.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:62
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
getDictionaryValue($tablename, $field, $id, $checkentity=false, $rowidfield='rowid')
Return the value of a filed into a dictionary for the record $id.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1926