dolibarr  16.0.5
define_holiday.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2022 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
4  * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
5  * Copyright (C) 2016 Regis Houssin <regis.houssin@inodbox.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
29 require '../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
32 
33 // Load translation files required by the page
34 $langs->loadlangs(array('users', 'other', 'holiday', 'hrm'));
35 
36 $action = GETPOST('action', 'aZ09');
37 $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'defineholidaylist';
38 $massaction = GETPOST('massaction', 'alpha');
39 $optioncss = GETPOST('optioncss', 'alpha');
40 
41 $search_name = GETPOST('search_name', 'alpha');
42 $search_supervisor = GETPOST('search_supervisor', 'int');
43 
44 // Load variable for pagination
45 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
46 $sortfield = GETPOST('sortfield', 'aZ09comma');
47 $sortorder = GETPOST('sortorder', 'aZ09comma');
48 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
49 if (empty($page) || $page == -1) {
50  $page = 0;
51 } // If $page is not defined, or '' or -1
52 $offset = $limit * $page;
53 $pageprev = $page - 1;
54 $pagenext = $page + 1;
55 if (!$sortfield) {
56  $sortfield = "t.rowid"; // Set here default search field
57 }
58 if (!$sortorder) {
59  $sortorder = "ASC";
60 }
61 
62 
63 // Protection if external user
64 if ($user->socid > 0) {
66 }
67 
68 // If the user does not have perm to read the page
69 if (empty($user->rights->holiday->read)) {
71 }
72 
73 
74 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
75 $hookmanager->initHooks(array('defineholidaylist'));
76 $extrafields = new ExtraFields($db);
77 
78 $holiday = new Holiday($db);
79 
80 if (empty($conf->holiday->enabled)) {
81  llxHeader('', $langs->trans('CPTitreMenu'));
82  print '<div class="tabBar">';
83  print '<span style="color: #FF0000;">'.$langs->trans('NotActiveModCP').'</span>';
84  print '</div>';
85  llxFooter();
86  exit();
87 }
88 
89 
90 
91 /*
92  * Actions
93  */
94 
95 if (GETPOST('cancel', 'alpha')) {
96  $action = 'list'; $massaction = '';
97 }
98 if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
99  $massaction = '';
100 }
101 
102 $parameters = array();
103 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
104 if ($reshook < 0) {
105  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
106 }
107 
108 if (empty($reshook)) {
109  // Selection of new fields
110  include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
111 
112  // Purge search criteria
113  if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
114  $search_name = '';
115  $search_supervisor = '';
116  $toselect = array();
117  $search_array_options = array();
118  }
119 
120  // Mass actions
121  /*
122  $objectclass='Skeleton';
123  $objectlabel='Skeleton';
124  $permissiontoread = $user->rights->skeleton->read;
125  $permissiontodelete = $user->rights->skeleton->delete;
126  $uploaddir = $conf->skeleton->dir_output;
127  include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
128  */
129 
130  // Si il y a une action de mise à jour
131  if ($action == 'update' && GETPOSTISSET('update_cp')) {
132  $error = 0;
133  $nbok = 0;
134 
135  $typeleaves = $holiday->getTypes(1, 1);
136 
137  $userID = array_keys(GETPOST('update_cp'));
138  $userID = $userID[0];
139 
140  $db->begin();
141 
142  foreach ($typeleaves as $key => $val) {
143  $userValue = GETPOST('nb_holiday_'.$val['rowid']);
144  $userValue = $userValue[$userID];
145 
146  if (!empty($userValue) || (string) $userValue == '0') {
147  $userValue = price2num($userValue, 5);
148  } else {
149  $userValue = '';
150  }
151 
152  //If the user set a comment, we add it to the log comment
153  $note_holiday = GETPOST('note_holiday');
154  $comment = ((isset($note_holiday[$userID]) && !empty($note_holiday[$userID])) ? ' ('.$note_holiday[$userID].')' : '');
155 
156  //print 'holiday: '.$val['rowid'].'-'.$userValue;exit;
157  if ($userValue != '') {
158  // We add the modification to the log (must be done before the update of balance because we read current value of balance inside this method)
159  $result = $holiday->addLogCP($user->id, $userID, $langs->transnoentitiesnoconv('ManualUpdate').$comment, $userValue, $val['rowid']);
160  if ($result < 0) {
161  setEventMessages($holiday->error, $holiday->errors, 'errors');
162  $error++;
163  } elseif ($result == 0) {
164  setEventMessages($langs->trans("HolidayQtyNotModified", $user->login), null, 'warnings');
165  }
166 
167  // Update of the days of the employee
168  if ($result > 0) {
169  $nbok++;
170 
171  $result = $holiday->updateSoldeCP($userID, $userValue, $val['rowid']);
172  if ($result < 0) {
173  setEventMessages($holiday->error, $holiday->errors, 'errors');
174  $error++;
175  }
176  }
177 
178  // If it first update of balance, we set date to avoid to have sold incremented by new month
179  /*
180  $now=dol_now();
181  $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
182  $sql.= " value = '".dol_print_date($now,'%Y%m%d%H%M%S')."'";
183  $sql.= " WHERE name = 'lastUpdate' and value IS NULL"; // Add value IS NULL to be sure to update only at init.
184  dol_syslog('define_holiday update lastUpdate entry', LOG_DEBUG);
185  $result = $db->query($sql);
186  */
187  }
188  }
189 
190  if (!$error) {
191  $db->commit();
192 
193  if ($nbok > 0) {
194  setEventMessages('UpdateConfCPOK', '', 'mesgs');
195  }
196  } else {
197  $db->rollback();
198  }
199  }
200 }
201 
202 
203 /*
204  * View
205  */
206 
207 $form = new Form($db);
208 $userstatic = new User($db);
209 
210 
211 $title = $langs->trans('CPTitreMenu');
212 
213 llxHeader('', $title);
214 
215 
216 $typeleaves = $holiday->getTypes(1, 1);
217 $result = $holiday->updateBalance(); // Create users into table holiday if they don't exists. TODO Remove this whif we use field into table user.
218 if ($result < 0) {
219  setEventMessages($holiday->error, $holiday->errors, 'errors');
220 }
221 
222 
223 print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
224 if ($optioncss != '') {
225  print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
226 }
227 print '<input type="hidden" name="token" value="'.newToken().'">';
228 print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
229 print '<input type="hidden" name="action" value="update">';
230 print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
231 print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
232 print '<input type="hidden" name="page" value="'.$page.'">';
233 print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
234 
235 print load_fiche_titre($langs->trans('MenuConfCP'), '', 'title_hrm.png');
236 
237 print '<div class="info">'.$langs->trans('LastUpdateCP').': '."\n";
238 $lastUpdate = $holiday->getConfCP('lastUpdate');
239 if ($lastUpdate) {
240  print '<strong>'.dol_print_date($db->jdate($lastUpdate), 'dayhour').'</strong>';
241  print '<br>'.$langs->trans("MonthOfLastMonthlyUpdate").': <strong>'.$langs->trans('Month'.substr($lastUpdate, 4, 2)).' '.substr($lastUpdate, 0, 4).'</strong>'."\n";
242 } else {
243  print $langs->trans('None');
244 }
245 print "</div><br>\n";
246 
247 
248 $filters = '';
249 
250 // Filter on array of ids of all childs
251 $userchilds = array();
252 if (empty($user->rights->holiday->readall)) {
253  $userchilds = $user->getAllChildIds(1);
254  $filters .= ' AND u.rowid IN ('.$db->sanitize(join(', ', $userchilds)).')';
255 }
256 if (!empty($search_name)) {
257  $filters .= natural_search(array('u.firstname', 'u.lastname'), $search_name);
258 }
259 if ($search_supervisor > 0) {
260  $filters .= natural_search(array('u.fk_user'), $search_supervisor, 2);
261 }
262 $filters .= ' AND employee = 1'; // Only employee users are visible
263 
264 $listUsers = $holiday->fetchUsers(false, true, $filters);
265 if (is_numeric($listUsers) && $listUsers < 0) {
266  setEventMessages($holiday->error, $holiday->errors, 'errors');
267 }
268 
269 $i = 0;
270 
271 
272 if (count($typeleaves) == 0) {
273  //print '<div class="info">';
274  print $langs->trans("NoLeaveWithCounterDefined")."<br>\n";
275  print $langs->trans("GoIntoDictionaryHolidayTypes");
276  //print '</div>';
277 } else {
278  $canedit = 0;
279  if (!empty($user->rights->holiday->define_holiday)) {
280  $canedit = 1;
281  }
282 
283  $moreforfilter = '';
284 
285  print '<div class="div-table-responsive">';
286  print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'" id="tablelines3">'."\n";
287 
288  print '<tr class="liste_titre_filter">';
289 
290  // User
291  print '<td class="liste_titre"><input type="text" name="search_name" value="'.dol_escape_htmltag($search_name).'"></td>';
292 
293  // Supervisor
294  print '<td class="liste_titre">';
295  print $form->select_dolusers($search_supervisor, 'search_supervisor', 1, null, 0, null, null, 0, 0, 0, '', 0, '', 'maxwidth200');
296  print '</td>';
297 
298  // Type of leave request
299  if (count($typeleaves)) {
300  foreach ($typeleaves as $key => $val) {
301  print '<td class="liste_titre" style="text-align:center"></td>';
302  }
303  } else {
304  print '<td class="liste_titre"></td>';
305  }
306  print '<td class="liste_titre"></td>';
307 
308  // Action column
309  print '<td class="liste_titre maxwidthsearch center">';
310  $searchpicto = $form->showFilterButtons();
311  print $searchpicto;
312  print '</td>';
313 
314  print '</tr>';
315 
316  print '<tr class="liste_titre">';
317  print_liste_field_titre('Employee', $_SERVER["PHP_SELF"]);
318  print_liste_field_titre('Supervisor', $_SERVER["PHP_SELF"]);
319  if (count($typeleaves)) {
320  foreach ($typeleaves as $key => $val) {
321  $labeltype = ($langs->trans($val['code']) != $val['code']) ? $langs->trans($val['code']) : $langs->trans($val['label']);
322  print_liste_field_titre($labeltype, $_SERVER["PHP_SELF"], '', '', '', '', '', '', 'center ');
323  }
324  } else {
325  print_liste_field_titre('NoLeaveWithCounterDefined', $_SERVER["PHP_SELF"], '', '', '', '');
326  }
327  print_liste_field_titre((empty($user->rights->holiday->define_holiday) ? '' : 'Note'), $_SERVER["PHP_SELF"]);
329  print '</tr>';
330 
331  $usersupervisor = new User($db);
332 
333  foreach ($listUsers as $users) {
334  // If user has not permission to edit/read all, we must see only subordinates
335  if (empty($user->rights->holiday->readall)) {
336  if (($users['rowid'] != $user->id) && (!in_array($users['rowid'], $userchilds))) {
337  continue; // This user is not into hierarchy of current user, we hide it.
338  }
339  }
340 
341  $userstatic->id = $users['rowid'];
342  $userstatic->lastname = $users['lastname'];
343  $userstatic->firstname = $users['firstname'];
344  $userstatic->gender = $users['gender'];
345  $userstatic->photo = $users['photo'];
346  $userstatic->statut = $users['status'];
347  $userstatic->employee = $users['employee'];
348  $userstatic->fk_user = $users['fk_user'];
349 
350  if ($userstatic->fk_user > 0) {
351  $usersupervisor->fetch($userstatic->fk_user);
352  }
353 
354  print '<tr class="oddeven">';
355 
356  // User
357  print '<td>';
358  print $userstatic->getNomUrl(-1);
359  print '</td>';
360 
361  // Supervisor
362  print '<td>';
363  if ($userstatic->fk_user > 0) {
364  print $usersupervisor->getNomUrl(-1);
365  }
366  print '</td>';
367 
368  // Amount for each type
369  if (count($typeleaves)) {
370  foreach ($typeleaves as $key => $val) {
371  $nbtoshow = '';
372  if ($holiday->getCPforUser($users['rowid'], $val['rowid']) != '') {
373  $nbtoshow = price2num($holiday->getCPforUser($users['rowid'], $val['rowid']), 5);
374  }
375 
376  //var_dump($users['rowid'].' - '.$val['rowid']);
377  print '<td style="text-align:center">';
378  if ($canedit) {
379  print '<input type="text"'.($canedit ? '' : ' disabled="disabled"').' value="'.$nbtoshow.'" name="nb_holiday_'.$val['rowid'].'['.$users['rowid'].']" class="width75 center" />';
380  } else {
381  print $nbtoshow;
382  }
383  //print ' '.$langs->trans('days');
384  print '</td>'."\n";
385  }
386  } else {
387  print '<td></td>';
388  }
389 
390  // Note
391  print '<td>';
392  if ($canedit) {
393  print '<input type="text"'.($canedit ? '' : ' disabled="disabled"').' class="maxwidthonsmartphone" value="" name="note_holiday['.$users['rowid'].']" size="30"/>';
394  }
395  print '</td>';
396 
397  // Button modify
398  print '<td>';
399  if (!empty($user->rights->holiday->define_holiday)) { // Allowed to set the balance of any user
400  print '<input type="submit" name="update_cp['.$users['rowid'].']" value="'.dol_escape_htmltag($langs->trans("Save")).'" class="button smallpaddingimp"/>';
401  }
402  print '</td>'."\n";
403  print '</tr>';
404 
405  $i++;
406  }
407 
408  print '</table>';
409  print '</div>';
410 }
411 
412 print '</form>';
413 
414 // End of page
415 llxFooter();
416 $db->close();
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Holiday
Class of the module paid holiday.
Definition: holiday.class.php:34
User
Class to manage Dolibarr users.
Definition: user.class.php:44
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
print_liste_field_titre
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
Definition: functions.lib.php:5026
natural_search
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
Definition: functions.lib.php:9420
ExtraFields
Class to manage standard extra fields.
Definition: extrafields.class.php:39
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59