dolibarr  7.0.0-beta
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@capnetworks.com>
5  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
6  * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
28 
29 
33 class Holiday extends CommonObject
34 {
35  public $element='holiday';
36  public $table_element='holiday';
37  public $ismultientitymanaged = 0; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
38  public $picto = 'holiday';
39 
44  var $rowid;
45 
46  var $fk_user;
47  var $date_create='';
48  var $description;
49  var $date_debut=''; // Date start in PHP server TZ
50  var $date_fin=''; // Date end in PHP server TZ
51  var $date_debut_gmt=''; // Date start in GMT
52  var $date_fin_gmt=''; // Date end in GMT
53  var $halfday='';
54  var $statut=''; // 1=draft, 2=validated, 3=approved
55  var $fk_validator;
56  var $date_valid='';
57  var $fk_user_valid;
58  var $date_refuse='';
59  var $fk_user_refuse;
60  var $date_cancel='';
61  var $fk_user_cancel;
62  var $detail_refuse='';
63  var $fk_type;
64 
65  var $holiday = array();
66  var $events = array();
67  var $logs = array();
68 
69  var $optName = '';
70  var $optValue = '';
71  var $optRowid = '';
72 
73 
79  function __construct($db)
80  {
81  $this->db = $db;
82  }
83 
84 
90  function updateBalance()
91  {
92  $this->db->begin();
93 
94  // Update sold of vocations
95  $result = $this->updateSoldeCP();
96 
97  // Check nb of users into table llx_holiday_users and update with empty lines
98  //if ($result > 0) $result = $this->verifNbUsers($this->countActiveUsersWithoutCP(), $this->getConfCP('nbUser'));
99 
100  if ($result >= 0)
101  {
102  $this->db->commit();
103  return 1;
104  }
105  else
106  {
107  $this->db->rollback();
108  return -1;
109  }
110  }
111 
119  function create($user, $notrigger=0)
120  {
121  global $conf;
122  $error=0;
123 
124  $now=dol_now();
125 
126  // Check parameters
127  if (empty($this->fk_user) || ! is_numeric($this->fk_user) || $this->fk_user < 0) { $this->error="ErrorBadParameterFkUser"; return -1; }
128  if (empty($this->fk_validator) || ! is_numeric($this->fk_validator) || $this->fk_validator < 0) { $this->error="ErrorBadParameterFkValidator"; return -1; }
129  if (empty($this->fk_type) || ! is_numeric($this->fk_type) || $this->fk_type < 0) { $this->error="ErrorBadParameterFkType"; return -1; }
130 
131  // Insert request
132  $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday(";
133  $sql.= "fk_user,";
134  $sql.= "date_create,";
135  $sql.= "description,";
136  $sql.= "date_debut,";
137  $sql.= "date_fin,";
138  $sql.= "halfday,";
139  $sql.= "statut,";
140  $sql.= "fk_validator,";
141  $sql.= "fk_type,";
142  $sql.= "fk_user_create,";
143  $sql.= "entity";
144  $sql.= ") VALUES (";
145  $sql.= "'".$this->db->escape($this->fk_user)."',";
146  $sql.= " '".$this->db->idate($now)."',";
147  $sql.= " '".$this->db->escape($this->description)."',";
148  $sql.= " '".$this->db->idate($this->date_debut)."',";
149  $sql.= " '".$this->db->idate($this->date_fin)."',";
150  $sql.= " ".$this->halfday.",";
151  $sql.= " '1',";
152  $sql.= " '".$this->db->escape($this->fk_validator)."',";
153  $sql.= " ".$this->fk_type.",";
154  $sql.= " ".$user->id.",";
155  $sql.= " ".$conf->entity;
156  $sql.= ")";
157 
158  $this->db->begin();
159 
160  dol_syslog(get_class($this)."::create", LOG_DEBUG);
161  $resql=$this->db->query($sql);
162  if (! $resql) {
163  $error++; $this->errors[]="Error ".$this->db->lasterror();
164  }
165 
166  if (! $error)
167  {
168  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."holiday");
169 
170  if (! $notrigger)
171  {
172  // Call trigger
173  $result=$this->call_trigger('HOLIDAY_CREATE',$user);
174  if ($result < 0) { $error++; }
175  // End call triggers
176  }
177  }
178 
179  // Commit or rollback
180  if ($error)
181  {
182  foreach($this->errors as $errmsg)
183  {
184  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
185  $this->error.=($this->error?', '.$errmsg:$errmsg);
186  }
187  $this->db->rollback();
188  return -1*$error;
189  }
190  else
191  {
192  $this->db->commit();
193  return $this->id;
194  }
195  }
196 
197 
204  function fetch($id)
205  {
206  global $langs;
207 
208  $sql = "SELECT";
209  $sql.= " cp.rowid,";
210  $sql.= " cp.fk_user,";
211  $sql.= " cp.date_create,";
212  $sql.= " cp.description,";
213  $sql.= " cp.date_debut,";
214  $sql.= " cp.date_fin,";
215  $sql.= " cp.halfday,";
216  $sql.= " cp.statut,";
217  $sql.= " cp.fk_validator,";
218  $sql.= " cp.date_valid,";
219  $sql.= " cp.fk_user_valid,";
220  $sql.= " cp.date_refuse,";
221  $sql.= " cp.fk_user_refuse,";
222  $sql.= " cp.date_cancel,";
223  $sql.= " cp.fk_user_cancel,";
224  $sql.= " cp.detail_refuse,";
225  $sql.= " cp.note_private,";
226  $sql.= " cp.note_public,";
227  $sql.= " cp.fk_user_create,";
228  $sql.= " cp.fk_type,";
229  $sql.= " cp.entity";
230  $sql.= " FROM ".MAIN_DB_PREFIX."holiday as cp";
231  $sql.= " WHERE cp.rowid = ".$id;
232 
233  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
234  $resql=$this->db->query($sql);
235  if ($resql)
236  {
237  if ($this->db->num_rows($resql))
238  {
239  $obj = $this->db->fetch_object($resql);
240 
241  $this->id = $obj->rowid;
242  $this->rowid = $obj->rowid; // deprecated
243  $this->ref = $obj->rowid;
244  $this->fk_user = $obj->fk_user;
245  $this->date_create = $this->db->jdate($obj->date_create);
246  $this->description = $obj->description;
247  $this->date_debut = $this->db->jdate($obj->date_debut);
248  $this->date_fin = $this->db->jdate($obj->date_fin);
249  $this->date_debut_gmt = $this->db->jdate($obj->date_debut,1);
250  $this->date_fin_gmt = $this->db->jdate($obj->date_fin,1);
251  $this->halfday = $obj->halfday;
252  $this->statut = $obj->statut;
253  $this->fk_validator = $obj->fk_validator;
254  $this->date_valid = $this->db->jdate($obj->date_valid);
255  $this->fk_user_valid = $obj->fk_user_valid;
256  $this->date_refuse = $this->db->jdate($obj->date_refuse);
257  $this->fk_user_refuse = $obj->fk_user_refuse;
258  $this->date_cancel = $this->db->jdate($obj->date_cancel);
259  $this->fk_user_cancel = $obj->fk_user_cancel;
260  $this->detail_refuse = $obj->detail_refuse;
261  $this->note_private = $obj->note_private;
262  $this->note_public = $obj->note_public;
263  $this->fk_user_create = $obj->fk_user_create;
264  $this->fk_type = $obj->fk_type;
265  $this->entity = $obj->entity;
266  }
267  $this->db->free($resql);
268 
269  return 1;
270  }
271  else
272  {
273  $this->error="Error ".$this->db->lasterror();
274  return -1;
275  }
276  }
277 
286  function fetchByUser($user_id, $order='', $filter='')
287  {
288  global $langs, $conf;
289 
290  $sql = "SELECT";
291  $sql.= " cp.rowid,";
292 
293  $sql.= " cp.fk_user,";
294  $sql.= " cp.fk_type,";
295  $sql.= " cp.date_create,";
296  $sql.= " cp.description,";
297  $sql.= " cp.date_debut,";
298  $sql.= " cp.date_fin,";
299  $sql.= " cp.halfday,";
300  $sql.= " cp.statut,";
301  $sql.= " cp.fk_validator,";
302  $sql.= " cp.date_valid,";
303  $sql.= " cp.fk_user_valid,";
304  $sql.= " cp.date_refuse,";
305  $sql.= " cp.fk_user_refuse,";
306  $sql.= " cp.date_cancel,";
307  $sql.= " cp.fk_user_cancel,";
308  $sql.= " cp.detail_refuse,";
309 
310  $sql.= " uu.lastname as user_lastname,";
311  $sql.= " uu.firstname as user_firstname,";
312  $sql.= " uu.login as user_login,";
313  $sql.= " uu.statut as user_statut,";
314  $sql.= " uu.photo as user_photo,";
315 
316  $sql.= " ua.lastname as validator_lastname,";
317  $sql.= " ua.firstname as validator_firstname,";
318  $sql.= " ua.login as validator_login,";
319  $sql.= " ua.statut as validator_statut,";
320  $sql.= " ua.photo as validator_photo";
321 
322  $sql.= " FROM ".MAIN_DB_PREFIX."holiday as cp, ".MAIN_DB_PREFIX."user as uu, ".MAIN_DB_PREFIX."user as ua";
323  $sql.= " WHERE cp.entity IN (".getEntity('holiday').")";
324  $sql.= " AND cp.fk_user = uu.rowid AND cp.fk_validator = ua.rowid "; // Hack pour la recherche sur le tableau
325  $sql.= " AND cp.fk_user = ".$user_id;
326 
327  // Filtre de séléction
328  if(!empty($filter)) {
329  $sql.= $filter;
330  }
331 
332  // Ordre d'affichage du résultat
333  if(!empty($order)) {
334  $sql.= $order;
335  }
336 
337  dol_syslog(get_class($this)."::fetchByUser", LOG_DEBUG);
338  $resql=$this->db->query($sql);
339 
340  // Si pas d'erreur SQL
341  if ($resql) {
342 
343  $i = 0;
344  $tab_result = $this->holiday;
345  $num = $this->db->num_rows($resql);
346 
347  // Si pas d'enregistrement
348  if(!$num) {
349  return 2;
350  }
351 
352  // Liste les enregistrements et les ajoutent au tableau
353  while($i < $num) {
354 
355  $obj = $this->db->fetch_object($resql);
356 
357  $tab_result[$i]['rowid'] = $obj->rowid;
358  $tab_result[$i]['ref'] = $obj->rowid;
359  $tab_result[$i]['fk_user'] = $obj->fk_user;
360  $tab_result[$i]['fk_type'] = $obj->fk_type;
361  $tab_result[$i]['date_create'] = $this->db->jdate($obj->date_create);
362  $tab_result[$i]['description'] = $obj->description;
363  $tab_result[$i]['date_debut'] = $this->db->jdate($obj->date_debut);
364  $tab_result[$i]['date_fin'] = $this->db->jdate($obj->date_fin);
365  $tab_result[$i]['date_debut_gmt'] = $this->db->jdate($obj->date_debut,1);
366  $tab_result[$i]['date_fin_gmt'] = $this->db->jdate($obj->date_fin,1);
367  $tab_result[$i]['halfday'] = $obj->halfday;
368  $tab_result[$i]['statut'] = $obj->statut;
369  $tab_result[$i]['fk_validator'] = $obj->fk_validator;
370  $tab_result[$i]['date_valid'] = $this->db->jdate($obj->date_valid);
371  $tab_result[$i]['fk_user_valid'] = $obj->fk_user_valid;
372  $tab_result[$i]['date_refuse'] = $this->db->jdate($obj->date_refuse);
373  $tab_result[$i]['fk_user_refuse'] = $obj->fk_user_refuse;
374  $tab_result[$i]['date_cancel'] = $this->db->jdate($obj->date_cancel);
375  $tab_result[$i]['fk_user_cancel'] = $obj->fk_user_cancel;
376  $tab_result[$i]['detail_refuse'] = $obj->detail_refuse;
377 
378  $tab_result[$i]['user_firstname'] = $obj->user_firstname;
379  $tab_result[$i]['user_lastname'] = $obj->user_lastname;
380  $tab_result[$i]['user_login'] = $obj->user_login;
381  $tab_result[$i]['user_statut'] = $obj->user_statut;
382  $tab_result[$i]['user_photo'] = $obj->user_photo;
383 
384  $tab_result[$i]['validator_firstname'] = $obj->validator_firstname;
385  $tab_result[$i]['validator_lastname'] = $obj->validator_lastname;
386  $tab_result[$i]['validator_login'] = $obj->validator_login;
387  $tab_result[$i]['validator_statut'] = $obj->validator_statut;
388  $tab_result[$i]['validator_photo'] = $obj->validator_photo;
389 
390  $i++;
391  }
392 
393  // Retourne 1 avec le tableau rempli
394  $this->holiday = $tab_result;
395  return 1;
396  }
397  else
398  {
399  // Erreur SQL
400  $this->error="Error ".$this->db->lasterror();
401  return -1;
402  }
403  }
404 
412  function fetchAll($order,$filter)
413  {
414  global $langs;
415 
416  $sql = "SELECT";
417  $sql.= " cp.rowid,";
418 
419  $sql.= " cp.fk_user,";
420  $sql.= " cp.fk_type,";
421  $sql.= " cp.date_create,";
422  $sql.= " cp.description,";
423  $sql.= " cp.date_debut,";
424  $sql.= " cp.date_fin,";
425  $sql.= " cp.halfday,";
426  $sql.= " cp.statut,";
427  $sql.= " cp.fk_validator,";
428  $sql.= " cp.date_valid,";
429  $sql.= " cp.fk_user_valid,";
430  $sql.= " cp.date_refuse,";
431  $sql.= " cp.fk_user_refuse,";
432  $sql.= " cp.date_cancel,";
433  $sql.= " cp.fk_user_cancel,";
434  $sql.= " cp.detail_refuse,";
435 
436  $sql.= " uu.lastname as user_lastname,";
437  $sql.= " uu.firstname as user_firstname,";
438  $sql.= " uu.login as user_login,";
439  $sql.= " uu.statut as user_statut,";
440  $sql.= " uu.photo as user_photo,";
441 
442  $sql.= " ua.lastname as validator_lastname,";
443  $sql.= " ua.firstname as validator_firstname,";
444  $sql.= " ua.login as validator_login,";
445  $sql.= " ua.statut as validator_statut,";
446  $sql.= " ua.photo as validator_photo";
447 
448  $sql.= " FROM ".MAIN_DB_PREFIX."holiday as cp, ".MAIN_DB_PREFIX."user as uu, ".MAIN_DB_PREFIX."user as ua";
449  $sql.= " WHERE cp.entity IN (".getEntity('holiday').")";
450  $sql.= " AND cp.fk_user = uu.rowid AND cp.fk_validator = ua.rowid "; // Hack pour la recherche sur le tableau
451 
452  // Filtrage de séléction
453  if(!empty($filter)) {
454  $sql.= $filter;
455  }
456 
457  // Ordre d'affichage
458  if(!empty($order)) {
459  $sql.= $order;
460  }
461 
462  dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
463  $resql=$this->db->query($sql);
464 
465  // Si pas d'erreur SQL
466  if ($resql) {
467 
468  $i = 0;
469  $tab_result = $this->holiday;
470  $num = $this->db->num_rows($resql);
471 
472  // Si pas d'enregistrement
473  if(!$num) {
474  return 2;
475  }
476 
477  // On liste les résultats et on les ajoutent dans le tableau
478  while($i < $num) {
479 
480  $obj = $this->db->fetch_object($resql);
481 
482  $tab_result[$i]['rowid'] = $obj->rowid;
483  $tab_result[$i]['ref'] = $obj->rowid;
484  $tab_result[$i]['fk_user'] = $obj->fk_user;
485  $tab_result[$i]['fk_type'] = $obj->fk_type;
486  $tab_result[$i]['date_create'] = $this->db->jdate($obj->date_create);
487  $tab_result[$i]['description'] = $obj->description;
488  $tab_result[$i]['date_debut'] = $this->db->jdate($obj->date_debut);
489  $tab_result[$i]['date_fin'] = $this->db->jdate($obj->date_fin);
490  $tab_result[$i]['date_debut_gmt'] = $this->db->jdate($obj->date_debut,1);
491  $tab_result[$i]['date_fin_gmt'] = $this->db->jdate($obj->date_fin,1);
492  $tab_result[$i]['halfday'] = $obj->halfday;
493  $tab_result[$i]['statut'] = $obj->statut;
494  $tab_result[$i]['fk_validator'] = $obj->fk_validator;
495  $tab_result[$i]['date_valid'] = $this->db->jdate($obj->date_valid);
496  $tab_result[$i]['fk_user_valid'] = $obj->fk_user_valid;
497  $tab_result[$i]['date_refuse'] = $obj->date_refuse;
498  $tab_result[$i]['fk_user_refuse'] = $obj->fk_user_refuse;
499  $tab_result[$i]['date_cancel'] = $obj->date_cancel;
500  $tab_result[$i]['fk_user_cancel'] = $obj->fk_user_cancel;
501  $tab_result[$i]['detail_refuse'] = $obj->detail_refuse;
502 
503  $tab_result[$i]['user_firstname'] = $obj->user_firstname;
504  $tab_result[$i]['user_lastname'] = $obj->user_lastname;
505  $tab_result[$i]['user_login'] = $obj->user_login;
506  $tab_result[$i]['user_statut'] = $obj->user_statut;
507  $tab_result[$i]['user_photo'] = $obj->user_photo;
508 
509  $tab_result[$i]['validator_firstname'] = $obj->validator_firstname;
510  $tab_result[$i]['validator_lastname'] = $obj->validator_lastname;
511  $tab_result[$i]['validator_login'] = $obj->validator_login;
512  $tab_result[$i]['validator_statut'] = $obj->validator_statut;
513  $tab_result[$i]['validator_photo'] = $obj->validator_photo;
514 
515  $i++;
516  }
517  // Retourne 1 et ajoute le tableau à la variable
518  $this->holiday = $tab_result;
519  return 1;
520  }
521  else
522  {
523  // Erreur SQL
524  $this->error="Error ".$this->db->lasterror();
525  return -1;
526  }
527  }
528 
536  function update($user=null, $notrigger=0)
537  {
538  global $conf, $langs;
539  $error=0;
540 
541  // Update request
542  $sql = "UPDATE ".MAIN_DB_PREFIX."holiday SET";
543 
544  $sql.= " description= '".$this->db->escape($this->description)."',";
545 
546  if(!empty($this->date_debut)) {
547  $sql.= " date_debut = '".$this->db->idate($this->date_debut)."',";
548  } else {
549  $error++;
550  }
551  if(!empty($this->date_fin)) {
552  $sql.= " date_fin = '".$this->db->idate($this->date_fin)."',";
553  } else {
554  $error++;
555  }
556  $sql.= " halfday = ".$this->halfday.",";
557  if(!empty($this->statut) && is_numeric($this->statut)) {
558  $sql.= " statut = ".$this->statut.",";
559  } else {
560  $error++;
561  }
562  if(!empty($this->fk_validator)) {
563  $sql.= " fk_validator = '".$this->db->escape($this->fk_validator)."',";
564  } else {
565  $error++;
566  }
567  if(!empty($this->date_valid)) {
568  $sql.= " date_valid = '".$this->db->idate($this->date_valid)."',";
569  } else {
570  $sql.= " date_valid = NULL,";
571  }
572  if(!empty($this->fk_user_valid)) {
573  $sql.= " fk_user_valid = '".$this->db->escape($this->fk_user_valid)."',";
574  } else {
575  $sql.= " fk_user_valid = NULL,";
576  }
577  if(!empty($this->date_refuse)) {
578  $sql.= " date_refuse = '".$this->db->idate($this->date_refuse)."',";
579  } else {
580  $sql.= " date_refuse = NULL,";
581  }
582  if(!empty($this->fk_user_refuse)) {
583  $sql.= " fk_user_refuse = '".$this->db->escape($this->fk_user_refuse)."',";
584  } else {
585  $sql.= " fk_user_refuse = NULL,";
586  }
587  if(!empty($this->date_cancel)) {
588  $sql.= " date_cancel = '".$this->db->idate($this->date_cancel)."',";
589  } else {
590  $sql.= " date_cancel = NULL,";
591  }
592  if(!empty($this->fk_user_cancel)) {
593  $sql.= " fk_user_cancel = '".$this->db->escape($this->fk_user_cancel)."',";
594  } else {
595  $sql.= " fk_user_cancel = NULL,";
596  }
597  if(!empty($this->detail_refuse)) {
598  $sql.= " detail_refuse = '".$this->db->escape($this->detail_refuse)."'";
599  } else {
600  $sql.= " detail_refuse = NULL";
601  }
602 
603  $sql.= " WHERE rowid= ".$this->id;
604 
605  $this->db->begin();
606 
607  dol_syslog(get_class($this)."::update", LOG_DEBUG);
608  $resql = $this->db->query($sql);
609  if (! $resql) {
610  $error++; $this->errors[]="Error ".$this->db->lasterror();
611  }
612 
613  if (! $error)
614  {
615  if (! $notrigger)
616  {
617  // Call trigger
618  $result=$this->call_trigger('HOLIDAY_MODIFY',$user);
619  if ($result < 0) { $error++; }
620  // End call triggers
621  }
622  }
623 
624  // Commit or rollback
625  if ($error)
626  {
627  foreach($this->errors as $errmsg)
628  {
629  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
630  $this->error.=($this->error?', '.$errmsg:$errmsg);
631  }
632  $this->db->rollback();
633  return -1*$error;
634  }
635  else
636  {
637  $this->db->commit();
638  return 1;
639  }
640  }
641 
642 
650  function delete($user, $notrigger=0)
651  {
652  global $conf, $langs;
653  $error=0;
654 
655  $sql = "DELETE FROM ".MAIN_DB_PREFIX."holiday";
656  $sql.= " WHERE rowid=".$this->id;
657 
658  $this->db->begin();
659 
660  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
661  $resql = $this->db->query($sql);
662  if (! $resql) {
663  $error++; $this->errors[]="Error ".$this->db->lasterror();
664  }
665 
666  if (! $error)
667  {
668  if (! $notrigger)
669  {
670  // Call trigger
671  $result=$this->call_trigger('HOLIDAY_DELETE',$user);
672  if ($result < 0) { $error++; }
673  // End call triggers
674  }
675  }
676 
677  // Commit or rollback
678  if ($error)
679  {
680  foreach($this->errors as $errmsg)
681  {
682  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
683  $this->error.=($this->error?', '.$errmsg:$errmsg);
684  }
685  $this->db->rollback();
686  return -1*$error;
687  }
688  else
689  {
690  $this->db->commit();
691  return 1;
692  }
693  }
694 
708  function verifDateHolidayCP($fk_user, $dateStart, $dateEnd, $halfday=0)
709  {
710  $this->fetchByUser($fk_user,'','');
711 
712  foreach($this->holiday as $infos_CP)
713  {
714  if ($infos_CP['statut'] == 4) continue; // ignore not validated holidays
715  if ($infos_CP['statut'] == 5) continue; // ignore not validated holidays
716  /*
717  var_dump("--");
718  var_dump("old: ".dol_print_date($infos_CP['date_debut'],'dayhour').' '.dol_print_date($infos_CP['date_fin'],'dayhour').' '.$infos_CP['halfday']);
719  var_dump("new: ".dol_print_date($dateStart,'dayhour').' '.dol_print_date($dateEnd,'dayhour').' '.$halfday);
720  */
721 
722  if ($halfday == 0)
723  {
724  if ($dateStart >= $infos_CP['date_debut'] && $dateStart <= $infos_CP['date_fin'])
725  {
726  return false;
727  }
728  if ($dateEnd <= $infos_CP['date_fin'] && $dateEnd >= $infos_CP['date_debut'])
729  {
730  return false;
731  }
732  }
733  elseif ($halfday == -1)
734  {
735  // new start afternoon, new end afternoon
736  if ($dateStart >= $infos_CP['date_debut'] && $dateStart <= $infos_CP['date_fin'])
737  {
738  if ($dateStart < $infos_CP['date_fin'] || in_array($infos_CP['halfday'], array(0, -1))) return false;
739  }
740  if ($dateEnd <= $infos_CP['date_fin'] && $dateEnd >= $infos_CP['date_debut'])
741  {
742  if ($dateStart < $dateEnd) return false;
743  if ($dateEnd < $infos_CP['date_fin'] || in_array($infos_CP['halfday'], array(0, -1))) return false;
744  }
745  }
746  elseif ($halfday == 1)
747  {
748  // new start morning, new end morning
749  if ($dateStart >= $infos_CP['date_debut'] && $dateStart <= $infos_CP['date_fin'])
750  {
751  if ($dateStart < $dateEnd) return false;
752  if ($dateStart > $infos_CP['date_debut'] || in_array($infos_CP['halfday'], array(0, 1))) return false;
753  }
754  if ($dateEnd <= $infos_CP['date_fin'] && $dateEnd >= $infos_CP['date_debut'])
755  {
756  if ($dateEnd > $infos_CP['date_debut'] || in_array($infos_CP['halfday'], array(0, 1))) return false;
757  }
758  }
759  elseif ($halfday == 2)
760  {
761  // new start afternoon, new end morning
762  if ($dateStart >= $infos_CP['date_debut'] && $dateStart <= $infos_CP['date_fin'])
763  {
764  if ($dateStart < $infos_CP['date_fin'] || in_array($infos_CP['halfday'], array(0, -1))) return false;
765  }
766  if ($dateEnd <= $infos_CP['date_fin'] && $dateEnd >= $infos_CP['date_debut'])
767  {
768  if ($dateEnd > $infos_CP['date_debut'] || in_array($infos_CP['halfday'], array(0, 1))) return false;
769  }
770  }
771  else
772  {
773  dol_print_error('', 'Bad value of parameter halfday when calling function verifDateHolidayCP');
774  }
775  }
776 
777  return true;
778  }
779 
780 
789  function verifDateHolidayForTimestamp($fk_user, $timestamp)
790  {
791  global $langs, $conf;
792 
793  $isavailablemorning=true;
794  $isavailableafternoon=true;
795 
796  $sql = "SELECT cp.rowid, cp.date_debut as date_start, cp.date_fin as date_end, cp.halfday";
797  $sql.= " FROM ".MAIN_DB_PREFIX."holiday as cp";
798  $sql.= " WHERE cp.entity IN (".getEntity('holiday').")";
799  $sql.= " AND cp.fk_user = ".(int) $fk_user;
800  $sql.= " AND date_debut <= '".$this->db->idate($timestamp)."' AND date_fin >= '".$this->db->idate($timestamp)."'";
801 
802  $resql = $this->db->query($sql);
803  if ($resql)
804  {
805  $num_rows = $this->db->num_rows($resql); // Note, we can have 2 records if on is morning and the other one is afternoon
806  if ($num_rows > 0)
807  {
808  $arrayofrecord=array();
809  $i=0;
810  while ($i < $num_rows)
811  {
812  $obj = $this->db->fetch_object($resql);
813 
814  // Note: $obj->halfday is 0:Full days, 2:Sart afternoon end morning, -1:Start afternoon, 1:End morning
815  $arrayofrecord[$obj->rowid]=array('date_start'=>$this->db->jdate($obj->date_start), 'date_end'=>$this->db->jdate($obj->date_end), 'halfday'=>$obj->halfday);
816  $i++;
817  }
818 
819  // We found a record, user is on holiday by default, so is not available is true.
820  $isavailablemorning = true;
821  foreach($arrayofrecord as $record)
822  {
823  if ($timestamp == $record['date_start'] && $record['halfday'] == 2) continue;
824  if ($timestamp == $record['date_start'] && $record['halfday'] == -1) continue;
825  $isavailablemorning = false;
826  break;
827  }
828  $isavailableafternoon = true;
829  foreach($arrayofrecord as $record)
830  {
831  if ($timestamp == $record['date_end'] && $record['halfday'] == 2) continue;
832  if ($timestamp == $record['date_end'] && $record['halfday'] == 1) continue;
833  $isavailableafternoon = false;
834  break;
835  }
836  }
837  }
838  else dol_print_error($this->db);
839 
840  return array('morning'=>$isavailablemorning, 'afternoon'=>$isavailableafternoon);
841  }
842 
843 
851  function getNomUrl($withpicto=0, $save_lastsearch_value=-1)
852  {
853  global $langs;
854 
855  $result='';
856 
857  $label=$langs->trans("Show").': '.$this->ref;
858 
859  $url = DOL_URL_ROOT.'/holiday/card.php?id='.$this->id;
860 
861  //if ($option != 'nolink')
862  //{
863  // Add param to save lastsearch_values or not
864  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
865  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
866  if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
867  //}
868 
869  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
870  $linkend='</a>';
871 
872  $result .= $linkstart;
873  if ($withpicto) $result.=img_object(($notooltip?'':$label), $this->picto, ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
874  if ($withpicto != 2) $result.= $this->ref;
875  $result .= $linkend;
876 
877  return $result;
878  }
879 
880 
887  function getLibStatut($mode=0)
888  {
889  return $this->LibStatut($this->statut, $mode, $this->date_debut);
890  }
891 
900  function LibStatut($statut, $mode=0, $startdate='')
901  {
902  global $langs;
903 
904  if ($mode == 0)
905  {
906  if ($statut == 1) return $langs->trans('DraftCP');
907  if ($statut == 2) return $langs->trans('ToReviewCP');
908  if ($statut == 3) return $langs->trans('ApprovedCP');
909  if ($statut == 4) return $langs->trans('CancelCP');
910  if ($statut == 5) return $langs->trans('RefuseCP');
911  }
912  if ($mode == 2)
913  {
914  $pictoapproved='statut6';
915  if (! empty($startdate) && $startdate > dol_now()) $pictoapproved='statut4';
916  if ($statut == 1) return img_picto($langs->trans('DraftCP'),'statut0').' '.$langs->trans('DraftCP'); // Draft
917  if ($statut == 2) return img_picto($langs->trans('ToReviewCP'),'statut1').' '.$langs->trans('ToReviewCP'); // Waiting approval
918  if ($statut == 3) return img_picto($langs->trans('ApprovedCP'),$pictoapproved).' '.$langs->trans('ApprovedCP');
919  if ($statut == 4) return img_picto($langs->trans('CancelCP'),'statut5').' '.$langs->trans('CancelCP');
920  if ($statut == 5) return img_picto($langs->trans('RefuseCP'),'statut5').' '.$langs->trans('RefuseCP');
921  }
922  if ($mode == 3)
923  {
924  $pictoapproved='statut6';
925  if (! empty($startdate) && $startdate > dol_now()) $pictoapproved='statut4';
926  if ($statut == 1) return img_picto($langs->trans('DraftCP'),'statut0');
927  if ($statut == 2) return img_picto($langs->trans('ToReviewCP'),'statut1');
928  if ($statut == 3) return img_picto($langs->trans('ApprovedCP'),$pictoapproved);
929  if ($statut == 4) return img_picto($langs->trans('CancelCP'),'statut5');
930  if ($statut == 5) return img_picto($langs->trans('RefuseCP'),'statut5');
931  }
932  if ($mode == 5)
933  {
934  $pictoapproved='statut6';
935  if (! empty($startdate) && $startdate > dol_now()) $pictoapproved='statut4';
936  if ($statut == 1) return $langs->trans('DraftCP').' '.img_picto($langs->trans('DraftCP'),'statut0'); // Draft
937  if ($statut == 2) return $langs->trans('ToReviewCP').' '.img_picto($langs->trans('ToReviewCP'),'statut1'); // Waiting approval
938  if ($statut == 3) return $langs->trans('ApprovedCP').' '.img_picto($langs->trans('ApprovedCP'),$pictoapproved);
939  if ($statut == 4) return $langs->trans('CancelCP').' '.img_picto($langs->trans('CancelCP'),'statut5');
940  if ($statut == 5) return $langs->trans('RefuseCP').' '.img_picto($langs->trans('RefuseCP'),'statut5');
941  }
942  if ($mode == 6)
943  {
944  $pictoapproved='statut6';
945  if (! empty($startdate) && $startdate > dol_now()) $pictoapproved='statut4';
946  if ($statut == 1) return $langs->trans('DraftCP').' '.img_picto($langs->trans('DraftCP'),'statut0'); // Draft
947  if ($statut == 2) return $langs->trans('ToReviewCP').' '.img_picto($langs->trans('ToReviewCP'),'statut1'); // Waiting approval
948  if ($statut == 3) return $langs->trans('ApprovedCP').' '.img_picto($langs->trans('ApprovedCP'),$pictoapproved);
949  if ($statut == 4) return $langs->trans('CancelCP').' '.img_picto($langs->trans('CancelCP'),'statut5');
950  if ($statut == 5) return $langs->trans('RefuseCP').' '.img_picto($langs->trans('RefuseCP'),'statut5');
951  }
952 
953  return $statut;
954  }
955 
956 
963  function selectStatutCP($selected='') {
964 
965  global $langs;
966 
967  // Liste des statuts
968  $name = array('DraftCP','ToReviewCP','ApprovedCP','CancelCP','RefuseCP');
969  $nb = count($name)+1;
970 
971  // Select HTML
972  $statut = '<select name="select_statut" class="flat">'."\n";
973  $statut.= '<option value="-1">&nbsp;</option>'."\n";
974 
975  // Boucle des statuts
976  for($i=1; $i < $nb; $i++) {
977  if($i==$selected) {
978  $statut.= '<option value="'.$i.'" selected>'.$langs->trans($name[$i-1]).'</option>'."\n";
979  }
980  else {
981  $statut.= '<option value="'.$i.'">'.$langs->trans($name[$i-1]).'</option>'."\n";
982  }
983  }
984 
985  $statut.= '</select>'."\n";
986  print $statut;
987 
988  }
989 
997  function updateConfCP($name,$value) {
998 
999  $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
1000  $sql.= " value = '".$value."'";
1001  $sql.= " WHERE name = '".$name."'";
1002 
1003  dol_syslog(get_class($this).'::updateConfCP name='.$name.'', LOG_DEBUG);
1004  $result = $this->db->query($sql);
1005  if($result) {
1006  return true;
1007  }
1008 
1009  return false;
1010  }
1011 
1020  function getConfCP($name, $createifnotfound='')
1021  {
1022  $sql = "SELECT value";
1023  $sql.= " FROM ".MAIN_DB_PREFIX."holiday_config";
1024  $sql.= " WHERE name = '".$this->db->escape($name)."'";
1025 
1026  dol_syslog(get_class($this).'::getConfCP name='.$name.' createifnotfound='.$createifnotfound, LOG_DEBUG);
1027  $result = $this->db->query($sql);
1028 
1029  if($result) {
1030 
1031  $obj = $this->db->fetch_object($result);
1032  // Return value
1033  if (empty($obj))
1034  {
1035  if ($createifnotfound)
1036  {
1037  $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_config(name, value)";
1038  $sql.= " VALUES('".$this->db->escape($name)."', '".$this->db->escape($createifnotfound)."')";
1039  $result = $this->db->query($sql);
1040  if ($result)
1041  {
1042  return $createifnotfound;
1043  }
1044  else
1045  {
1046  $this->error=$this->db->lasterror();
1047  return -2;
1048  }
1049  }
1050  else
1051  {
1052  return '';
1053  }
1054  }
1055  else
1056  {
1057  return $obj->value;
1058  }
1059  } else {
1060 
1061  // Erreur SQL
1062  $this->error=$this->db->lasterror();
1063  return -1;
1064  }
1065  }
1066 
1075  function updateSoldeCP($userID='',$nbHoliday='', $fk_type='')
1076  {
1077  global $user, $langs;
1078 
1079  $error = 0;
1080 
1081  if (empty($userID) && empty($nbHoliday) && empty($fk_type))
1082  {
1083  $langs->load("holiday");
1084 
1085  // Si mise à jour pour tout le monde en début de mois
1086  $now=dol_now();
1087 
1088  $month = date('m',$now);
1089  $newdateforlastupdate = dol_print_date($now, '%Y%m%d%H%M%S');
1090 
1091  // Get month of last update
1092  $lastUpdate = $this->getConfCP('lastUpdate', $newdateforlastupdate);
1093  $monthLastUpdate = $lastUpdate[4].$lastUpdate[5];
1094  //print 'month: '.$month.' lastUpdate:'.$lastUpdate.' monthLastUpdate:'.$monthLastUpdate;exit;
1095 
1096  // Si la date du mois n'est pas la même que celle sauvegardée, on met à jour le timestamp
1097  if ($month != $monthLastUpdate)
1098  {
1099  $this->db->begin();
1100 
1101  $users = $this->fetchUsers(false,false);
1102  $nbUser = count($users);
1103 
1104  $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
1105  $sql.= " value = '".$this->db->escape($newdateforlastupdate)."'";
1106  $sql.= " WHERE name = 'lastUpdate'";
1107  $result = $this->db->query($sql);
1108 
1109  $typeleaves=$this->getTypes(1,1);
1110  foreach($typeleaves as $key => $val)
1111  {
1112  // On ajoute x jours à chaque utilisateurs
1113  $nb_holiday = $val['newByMonth'];
1114  if (empty($nb_holiday)) $nb_holiday=0;
1115 
1116  if ($nb_holiday > 0)
1117  {
1118  dol_syslog("We update leavefor everybody for type ".$key, LOG_DEBUG);
1119 
1120  $i = 0;
1121  while ($i < $nbUser)
1122  {
1123  $now_holiday = $this->getCPforUser($users[$i]['rowid'], $val['rowid']);
1124  $new_solde = $now_holiday + $nb_holiday;
1125 
1126  // We add a log for each user
1127  $this->addLogCP($user->id, $users[$i]['rowid'], $langs->trans('HolidaysMonthlyUpdate'), $new_solde, $val['rowid']);
1128 
1129  $i++;
1130  }
1131 
1132  // Now we update counter for all users at once
1133  $sql2 = "UPDATE ".MAIN_DB_PREFIX."holiday_users SET";
1134  $sql2.= " nb_holiday = nb_holiday + ".$nb_holiday;
1135  $sql2.= " WHERE fk_type = ".$val['rowid'];
1136 
1137  $result= $this->db->query($sql2);
1138 
1139  if (! $result)
1140  {
1141  dol_print_error($this->db);
1142  break;
1143  }
1144  }
1145  else dol_syslog("No change for leave of type ".$key, LOG_DEBUG);
1146  }
1147 
1148  if ($result)
1149  {
1150  $this->db->commit();
1151  return 1;
1152  }
1153  else
1154  {
1155  $this->db->rollback();
1156  return -1;
1157  }
1158  }
1159 
1160  return 0;
1161  }
1162  else
1163  {
1164  // Mise à jour pour un utilisateur
1165  $nbHoliday = price2num($nbHoliday,5);
1166 
1167  $sql = "SELECT nb_holiday FROM ".MAIN_DB_PREFIX."holiday_users";
1168  $sql.= " WHERE fk_user = '".$userID."' AND fk_type = ".$fk_type;
1169  $resql = $this->db->query($sql);
1170  if ($resql)
1171  {
1172  $num = $this->db->num_rows($resql);
1173 
1174  if ($num > 0)
1175  {
1176  // Update for user
1177  $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_users SET";
1178  $sql.= " nb_holiday = ".$nbHoliday;
1179  $sql.= " WHERE fk_user = '".$userID."' AND fk_type = ".$fk_type;
1180  $result = $this->db->query($sql);
1181  if (! $result)
1182  {
1183  $error++;
1184  $this->errors[]=$this->db->lasterror();
1185  }
1186  }
1187  else
1188  {
1189  // Insert for user
1190  $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_users(nb_holiday, fk_user, fk_type) VALUES (";
1191  $sql.= $nbHoliday;
1192  $sql.= ", '".$userID."', ".$fk_type.")";
1193  $result = $this->db->query($sql);
1194  if (! $result)
1195  {
1196  $error++;
1197  $this->errors[]=$this->db->lasterror();
1198  }
1199  }
1200  }
1201  else
1202  {
1203  $this->errors[]=$this->db->lasterror();
1204  $error++;
1205  }
1206 
1207  if (! $error)
1208  {
1209  return 1;
1210  }
1211  else
1212  {
1213  return -1;
1214  }
1215  }
1216 
1217  }
1218 
1225  function getCheckOption($name) {
1226 
1227  $sql = "SELECT value";
1228  $sql.= " FROM ".MAIN_DB_PREFIX."holiday_config";
1229  $sql.= " WHERE name = '".$name."'";
1230 
1231  $result = $this->db->query($sql);
1232 
1233  if($result) {
1234  $obj = $this->db->fetch_object($result);
1235 
1236  // Si la valeur est 1 on retourne checked
1237  if($obj->value) {
1238  return 'checked';
1239  }
1240  }
1241  }
1242 
1243 
1251  function createCPusers($single=false,$userid='')
1252  {
1253  // Si c'est l'ensemble des utilisateurs à ajouter
1254  if (! $single)
1255  {
1256  dol_syslog(get_class($this).'::createCPusers');
1257  $arrayofusers = $this->fetchUsers(false,true);
1258 
1259  foreach($arrayofusers as $users)
1260  {
1261  $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_users";
1262  $sql.= " (fk_user, nb_holiday)";
1263  $sql.= " VALUES ('".$users['rowid']."','0')";
1264 
1265  $resql=$this->db->query($sql);
1266  if (! $resql) dol_print_error($this->db);
1267  }
1268  }
1269  else
1270  {
1271  $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_users";
1272  $sql.= " (fk_user, nb_holiday)";
1273  $sql.= " VALUES ('".$userid."','0')";
1274 
1275  $resql=$this->db->query($sql);
1276  if (! $resql) dol_print_error($this->db);
1277  }
1278  }
1279 
1286  function deleteCPuser($user_id) {
1287 
1288  $sql = "DELETE FROM ".MAIN_DB_PREFIX."holiday_users";
1289  $sql.= " WHERE fk_user = '".$user_id."'";
1290 
1291  $this->db->query($sql);
1292 
1293  }
1294 
1295 
1303  function getCPforUser($user_id, $fk_type=0)
1304  {
1305  $sql = "SELECT nb_holiday";
1306  $sql.= " FROM ".MAIN_DB_PREFIX."holiday_users";
1307  $sql.= " WHERE fk_user = '".$user_id."'";
1308  if ($fk_type > 0) $sql.=" AND fk_type = ".$fk_type;
1309 
1310  dol_syslog(get_class($this).'::getCPforUser', LOG_DEBUG);
1311  $result = $this->db->query($sql);
1312  if($result)
1313  {
1314  $obj = $this->db->fetch_object($result);
1315  //return number_format($obj->nb_holiday,2);
1316  if ($obj) return $obj->nb_holiday;
1317  else return null;
1318  }
1319  else
1320  {
1321  return null;
1322  }
1323  }
1324 
1333  function fetchUsers($stringlist=true, $type=true, $filters='')
1334  {
1335  global $conf;
1336 
1337  dol_syslog(get_class($this)."::fetchUsers", LOG_DEBUG);
1338 
1339  if ($stringlist)
1340  {
1341  if ($type)
1342  {
1343  // Si utilisateur de Dolibarr
1344 
1345  $sql = "SELECT u.rowid";
1346  $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
1347 
1348  if (! empty($conf->multicompany->enabled) && ! empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE))
1349  {
1350  $sql.= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
1351  $sql.= " WHERE (ug.fk_user = u.rowid";
1352  $sql.= " AND ug.entity = ".$conf->entity.")";
1353  $sql.= " OR u.admin = 1";
1354  }
1355  else
1356  {
1357  $sql.= " WHERE u.entity IN (0,".$conf->entity.")";
1358  }
1359  $sql.= " AND u.statut > 0";
1360  if ($filters) $sql.=$filters;
1361 
1362  $resql=$this->db->query($sql);
1363 
1364  // Si pas d'erreur SQL
1365  if ($resql) {
1366 
1367  $i = 0;
1368  $num = $this->db->num_rows($resql);
1369  $stringlist = '';
1370 
1371  // Boucles du listage des utilisateurs
1372  while($i < $num)
1373  {
1374  $obj = $this->db->fetch_object($resql);
1375 
1376  if ($i == 0) {
1377  $stringlist.= $obj->rowid;
1378  } else {
1379  $stringlist.= ', '.$obj->rowid;
1380  }
1381 
1382  $i++;
1383  }
1384  // Retoune le tableau des utilisateurs
1385  return $stringlist;
1386  }
1387  else
1388  {
1389  // Erreur SQL
1390  $this->error="Error ".$this->db->lasterror();
1391  return -1;
1392  }
1393 
1394  }
1395  else
1396  {
1397  // We want only list of vacation balance for user ids
1398  $sql = "SELECT DISTINCT cpu.fk_user";
1399  $sql.= " FROM ".MAIN_DB_PREFIX."holiday_users as cpu, ".MAIN_DB_PREFIX."user as u";
1400  $sql.= " WHERE cpu.fk_user = u.user";
1401  if ($filters) $sql.=$filters;
1402 
1403  $resql=$this->db->query($sql);
1404 
1405  // Si pas d'erreur SQL
1406  if ($resql) {
1407 
1408  $i = 0;
1409  $num = $this->db->num_rows($resql);
1410  $stringlist = '';
1411 
1412  // Boucles du listage des utilisateurs
1413  while($i < $num)
1414  {
1415  $obj = $this->db->fetch_object($resql);
1416 
1417  if($i == 0) {
1418  $stringlist.= $obj->fk_user;
1419  } else {
1420  $stringlist.= ', '.$obj->fk_user;
1421  }
1422 
1423  $i++;
1424  }
1425  // Retoune le tableau des utilisateurs
1426  return $stringlist;
1427  }
1428  else
1429  {
1430  // Erreur SQL
1431  $this->error="Error ".$this->db->lasterror();
1432  return -1;
1433  }
1434  }
1435 
1436  }
1437  else
1438  { // Si faux donc return array
1439 
1440  // List for Dolibarr users
1441  if ($type)
1442  {
1443  $sql = "SELECT u.rowid, u.lastname, u.firstname, u.gender, u.photo, u.employee, u.statut, u.fk_user";
1444  $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
1445 
1446  if (! empty($conf->multicompany->enabled) && ! empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE))
1447  {
1448  $sql.= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
1449  $sql.= " WHERE (ug.fk_user = u.rowid";
1450  $sql.= " AND ug.entity = ".$conf->entity.")";
1451  $sql.= " OR u.admin = 1";
1452  }
1453  else
1454  $sql.= " WHERE u.entity IN (0,".$conf->entity.")";
1455 
1456  $sql.= " AND u.statut > 0";
1457  if ($filters) $sql.=$filters;
1458 
1459  $resql=$this->db->query($sql);
1460 
1461  // Si pas d'erreur SQL
1462  if ($resql)
1463  {
1464  $i = 0;
1465  $tab_result = $this->holiday;
1466  $num = $this->db->num_rows($resql);
1467 
1468  // Boucles du listage des utilisateurs
1469  while($i < $num) {
1470 
1471  $obj = $this->db->fetch_object($resql);
1472 
1473  $tab_result[$i]['rowid'] = $obj->rowid;
1474  $tab_result[$i]['name'] = $obj->lastname; // deprecated
1475  $tab_result[$i]['lastname'] = $obj->lastname;
1476  $tab_result[$i]['firstname'] = $obj->firstname;
1477  $tab_result[$i]['gender'] = $obj->gender;
1478  $tab_result[$i]['status'] = $obj->statut;
1479  $tab_result[$i]['employee'] = $obj->employee;
1480  $tab_result[$i]['photo'] = $obj->photo;
1481  $tab_result[$i]['fk_user'] = $obj->fk_user;
1482  //$tab_result[$i]['type'] = $obj->type;
1483  //$tab_result[$i]['nb_holiday'] = $obj->nb_holiday;
1484 
1485  $i++;
1486  }
1487  // Retoune le tableau des utilisateurs
1488  return $tab_result;
1489  }
1490  else
1491  {
1492  // Erreur SQL
1493  $this->errors[]="Error ".$this->db->lasterror();
1494  return -1;
1495  }
1496  }
1497  else
1498  {
1499  // List of vacation balance users
1500  $sql = "SELECT cpu.fk_user, cpu.fk_type, cpu.nb_holiday, u.lastname, u.firstname, u.gender, u.photo, u.employee, u.statut, u.fk_user";
1501  $sql.= " FROM ".MAIN_DB_PREFIX."holiday_users as cpu, ".MAIN_DB_PREFIX."user as u";
1502  $sql.= " WHERE cpu.fk_user = u.rowid";
1503  if ($filters) $sql.=$filters;
1504 
1505  $resql=$this->db->query($sql);
1506 
1507  // Si pas d'erreur SQL
1508  if ($resql)
1509  {
1510  $i = 0;
1511  $tab_result = $this->holiday;
1512  $num = $this->db->num_rows($resql);
1513 
1514  // Boucles du listage des utilisateurs
1515  while($i < $num)
1516  {
1517  $obj = $this->db->fetch_object($resql);
1518 
1519  $tab_result[$i]['rowid'] = $obj->fk_user;
1520  $tab_result[$i]['name'] = $obj->lastname; // deprecated
1521  $tab_result[$i]['lastname'] = $obj->lastname;
1522  $tab_result[$i]['firstname'] = $obj->firstname;
1523  $tab_result[$i]['gender'] = $obj->gender;
1524  $tab_result[$i]['status'] = $obj->statut;
1525  $tab_result[$i]['employee'] = $obj->employee;
1526  $tab_result[$i]['photo'] = $obj->photo;
1527  $tab_result[$i]['fk_user'] = $obj->fk_user;
1528 
1529  $tab_result[$i]['type'] = $obj->type;
1530  $tab_result[$i]['nb_holiday'] = $obj->nb_holiday;
1531 
1532  $i++;
1533  }
1534  // Retoune le tableau des utilisateurs
1535  return $tab_result;
1536  }
1537  else
1538  {
1539  // Erreur SQL
1540  $this->error="Error ".$this->db->lasterror();
1541  return -1;
1542  }
1543  }
1544  }
1545  }
1546 
1552  function countActiveUsers()
1553  {
1554  $sql = "SELECT count(u.rowid) as compteur";
1555  $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
1556  $sql.= " WHERE u.statut > 0";
1557 
1558  $result = $this->db->query($sql);
1559  $objet = $this->db->fetch_object($result);
1560 
1561  return $objet->compteur;
1562  }
1569 
1570  $sql = "SELECT count(u.rowid) as compteur";
1571  $sql.= " FROM ".MAIN_DB_PREFIX."user as u LEFT OUTER JOIN ".MAIN_DB_PREFIX."holiday_users hu ON (hu.fk_user=u.rowid)";
1572  $sql.= " WHERE u.statut > 0 AND hu.fk_user IS NULL";
1573 
1574  $result = $this->db->query($sql);
1575  $objet = $this->db->fetch_object($result);
1576 
1577  return $objet->compteur;
1578  }
1579 
1587  function verifNbUsers($userDolibarrWithoutCP, $userCP)
1588  {
1589  if (empty($userCP)) $userCP=0;
1590  dol_syslog(get_class($this).'::verifNbUsers userDolibarr='.$userDolibarrWithoutCP.' userCP='.$userCP);
1591  return 1;
1592  }
1593 
1594 
1605  function addLogCP($fk_user_action, $fk_user_update, $label, $new_solde, $fk_type)
1606  {
1607  global $conf, $langs;
1608 
1609  $error=0;
1610 
1611  $prev_solde = price2num($this->getCPforUser($fk_user_update, $fk_type), 5);
1612  $new_solde = price2num($new_solde, 5);
1613  //print "$prev_solde == $new_solde";
1614 
1615  if ($prev_solde == $new_solde) return 0;
1616 
1617  $this->db->begin();
1618 
1619  // Insert request
1620  $sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_logs (";
1621  $sql.= "date_action,";
1622  $sql.= "fk_user_action,";
1623  $sql.= "fk_user_update,";
1624  $sql.= "type_action,";
1625  $sql.= "prev_solde,";
1626  $sql.= "new_solde,";
1627  $sql.= "fk_type";
1628  $sql.= ") VALUES (";
1629  $sql.= " '".$this->db->idate(dol_now())."',";
1630  $sql.= " '".$fk_user_action."',";
1631  $sql.= " '".$fk_user_update."',";
1632  $sql.= " '".$this->db->escape($label)."',";
1633  $sql.= " '".$prev_solde."',";
1634  $sql.= " '".$new_solde."',";
1635  $sql.= " ".$fk_type;
1636  $sql.= ")";
1637 
1638  $resql=$this->db->query($sql);
1639  if (! $resql)
1640  {
1641  $error++; $this->errors[]="Error ".$this->db->lasterror();
1642  }
1643 
1644  if (! $error)
1645  {
1646  $this->optRowid = $this->db->last_insert_id(MAIN_DB_PREFIX."holiday_logs");
1647  }
1648 
1649  // Commit or rollback
1650  if ($error)
1651  {
1652  foreach($this->errors as $errmsg)
1653  {
1654  dol_syslog(get_class($this)."::addLogCP ".$errmsg, LOG_ERR);
1655  $this->error.=($this->error?', '.$errmsg:$errmsg);
1656  }
1657  $this->db->rollback();
1658  return -1*$error;
1659  }
1660  else
1661  {
1662  $this->db->commit();
1663  return $this->optRowid;
1664  }
1665  }
1666 
1674  function fetchLog($order,$filter)
1675  {
1676  global $langs;
1677 
1678  $sql = "SELECT";
1679  $sql.= " cpl.rowid,";
1680  $sql.= " cpl.date_action,";
1681  $sql.= " cpl.fk_user_action,";
1682  $sql.= " cpl.fk_user_update,";
1683  $sql.= " cpl.type_action,";
1684  $sql.= " cpl.prev_solde,";
1685  $sql.= " cpl.new_solde,";
1686  $sql.= " cpl.fk_type";
1687  $sql.= " FROM ".MAIN_DB_PREFIX."holiday_logs as cpl";
1688  $sql.= " WHERE cpl.rowid > 0"; // To avoid error with other search and criteria
1689 
1690  // Filtrage de séléction
1691  if(!empty($filter)) {
1692  $sql.= " ".$filter;
1693  }
1694 
1695  // Ordre d'affichage
1696  if(!empty($order)) {
1697  $sql.= " ".$order;
1698  }
1699 
1700  dol_syslog(get_class($this)."::fetchLog", LOG_DEBUG);
1701  $resql=$this->db->query($sql);
1702 
1703  // Si pas d'erreur SQL
1704  if ($resql) {
1705 
1706  $i = 0;
1707  $tab_result = $this->logs;
1708  $num = $this->db->num_rows($resql);
1709 
1710  // Si pas d'enregistrement
1711  if(!$num) {
1712  return 2;
1713  }
1714 
1715  // On liste les résultats et on les ajoutent dans le tableau
1716  while($i < $num) {
1717 
1718  $obj = $this->db->fetch_object($resql);
1719 
1720  $tab_result[$i]['rowid'] = $obj->rowid;
1721  $tab_result[$i]['date_action'] = $obj->date_action;
1722  $tab_result[$i]['fk_user_action'] = $obj->fk_user_action;
1723  $tab_result[$i]['fk_user_update'] = $obj->fk_user_update;
1724  $tab_result[$i]['type_action'] = $obj->type_action;
1725  $tab_result[$i]['prev_solde'] = $obj->prev_solde;
1726  $tab_result[$i]['new_solde'] = $obj->new_solde;
1727  $tab_result[$i]['fk_type'] = $obj->fk_type;
1728 
1729  $i++;
1730  }
1731  // Retourne 1 et ajoute le tableau à la variable
1732  $this->logs = $tab_result;
1733  return 1;
1734  }
1735  else
1736  {
1737  // Erreur SQL
1738  $this->error="Error ".$this->db->lasterror();
1739  return -1;
1740  }
1741  }
1742 
1743 
1751  function getTypes($active=-1, $affect=-1)
1752  {
1753  global $mysoc;
1754 
1755  $sql = "SELECT rowid, code, label, affect, delay, newByMonth";
1756  $sql.= " FROM " . MAIN_DB_PREFIX . "c_holiday_types";
1757  $sql.= " WHERE (fk_country IS NULL OR fk_country = ".$mysoc->country_id.')';
1758  if ($active >= 0) $sql.=" AND active = ".((int) $active);
1759  if ($affect >= 0) $sql.=" AND affect = ".((int) $affect);
1760 
1761  $result = $this->db->query($sql);
1762  if ($result)
1763  {
1764  $num = $this->db->num_rows($result);
1765  if ($num)
1766  {
1767  while ($obj = $this->db->fetch_object($result))
1768  {
1769  $types[$obj->rowid] = array('rowid'=> $obj->rowid, 'code'=> $obj->code, 'label'=>$obj->label, 'affect'=>$obj->affect, 'delay'=>$obj->delay, 'newByMonth'=>$obj->newByMonth);
1770  }
1771 
1772  return $types;
1773  }
1774  }
1775  else dol_print_error($this->db);
1776 
1777  return array();
1778  }
1779 
1780 
1788  function initAsSpecimen()
1789  {
1790  global $user,$langs;
1791 
1792  // Initialise parameters
1793  $this->id=0;
1794  $this->specimen=1;
1795 
1796  $this->fk_user=1;
1797  $this->description='SPECIMEN description';
1798  $this->date_debut=dol_now();
1799  $this->date_fin=dol_now()+(24*3600);
1800  $this->fk_validator=1;
1801  $this->halfday=0;
1802  $this->fk_type=1;
1803  }
1804 
1805 }
Class of the module paid holiday.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
getTypes($active=-1, $affect=-1)
Return array with list of types.
</td >< tdclass="liste_titre"align="right"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow img yes disabled img no img no< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="'.$i.'"'.$disabled.'></td >< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"></td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:554
getLibStatut($mode=0)
Returns the label status.
fetchUsers($stringlist=true, $type=true, $filters='')
Get list of Users or list of vacation balance.
getCPforUser($user_id, $fk_type=0)
Retourne le solde de congés payés pour un utilisateur.
deleteCPuser($user_id)
Supprime un utilisateur du module Congés Payés.
updateConfCP($name, $value)
Met à jour une option du module Holiday Payés.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
update($user=null, $notrigger=0)
Update database.
updateBalance()
Update balance of vacations and check table of users for holidays is complete.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
initAsSpecimen()
Initialise an instance with random values.
fetchByUser($user_id, $order='', $filter='')
List holidays for a particular user.
fetchLog($order, $filter)
Liste le log des congés payés.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
updateSoldeCP($userID='', $nbHoliday='', $fk_type='')
Met à jour le timestamp de la dernière mise à jour du solde des CP.
countActiveUsers()
Compte le nombre d'utilisateur actifs dans Dolibarr.
create($user, $notrigger=0)
Créer un congés payés dans la base de données.
getConfCP($name, $createifnotfound='')
Return value of a conf parameterfor leave module TODO Move this into llx_const table.
verifDateHolidayForTimestamp($fk_user, $timestamp)
Check a user is not on holiday for a particular timestamp.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
LibStatut($statut, $mode=0, $startdate='')
Returns the label of a statut.
fetch($id)
Load object in memory from database.
dol_now($mode='gmt')
Return date for now.
verifDateHolidayCP($fk_user, $dateStart, $dateEnd, $halfday=0)
Check if a user is on holiday (partially or completely) into a period.
fetchAll($order, $filter)
List all holidays of all users.
addLogCP($fk_user_action, $fk_user_update, $label, $new_solde, $fk_type)
addLogCP
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print
Draft customers invoices.
Definition: index.php:91
selectStatutCP($selected='')
Affiche un select HTML des statuts de congés payés.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
verifNbUsers($userDolibarrWithoutCP, $userCP)
Compare le nombre d'utilisateur actif de Dolibarr à celui des utilisateurs des congés payés...
__construct($db)
Constructor.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
createCPusers($single=false, $userid='')
Créer les entrées pour chaque utilisateur au moment de la configuration.
countActiveUsersWithoutCP()
Compte le nombre d'utilisateur actifs dans Dolibarr sans CP.
getCheckOption($name)
Retourne un checked si vrai.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
getNomUrl($withpicto=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)