dolibarr  17.0.4
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 // Load Dolibarr environment
30 require '../main.inc.php';
31 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadlangs(array('users', 'other', 'holiday', 'hrm'));
36 
37 $action = GETPOST('action', 'aZ09');
38 $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'defineholidaylist';
39 $massaction = GETPOST('massaction', 'alpha');
40 $optioncss = GETPOST('optioncss', 'alpha');
41 
42 $search_name = GETPOST('search_name', 'alpha');
43 $search_supervisor = GETPOST('search_supervisor', 'int');
44 
45 // Load variable for pagination
46 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
47 $sortfield = GETPOST('sortfield', 'aZ09comma');
48 $sortorder = GETPOST('sortorder', 'aZ09comma');
49 $toselect = GETPOST('toselect', 'array'); // Array of ids of elements selected into a list
50 $confirm = GETPOST('confirm', 'alpha');
51 
52 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
53 if (empty($page) || $page == -1) {
54  $page = 0;
55 } // If $page is not defined, or '' or -1
56 $offset = $limit * $page;
57 $pageprev = $page - 1;
58 $pagenext = $page + 1;
59 if (!$sortfield) {
60  $sortfield = "t.rowid"; // Set here default search field
61 }
62 if (!$sortorder) {
63  $sortorder = "ASC";
64 }
65 
66 
67 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
68 $hookmanager->initHooks(array('defineholidaylist'));
69 $extrafields = new ExtraFields($db);
70 
71 $holiday = new Holiday($db);
72 
73 
74 if (empty($conf->holiday->enabled)) {
75  accessforbidden('Module not enabled');
76 }
77 
78 // Protection if external user
79 if ($user->socid > 0) {
81 }
82 
83 // If the user does not have perm to read the page
84 if (empty($user->rights->holiday->read)) {
86 }
87 
88 
89 /*
90  * Actions
91  */
92 
93 if (GETPOST('cancel', 'alpha')) {
94  $action = 'list'; $massaction = '';
95 }
96 if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
97  $massaction = '';
98 }
99 
100 $parameters = array();
101 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
102 if ($reshook < 0) {
103  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
104 }
105 
106 if (empty($reshook)) {
107  // Selection of new fields
108  include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
109 
110  // Purge search criteria
111  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
112  $search_name = '';
113  $search_supervisor = '';
114  $toselect = array();
115  $search_array_options = array();
116  }
117 
118  // Mass actions
119  $objectclass = 'Holiday';
120  $objectlabel = 'Holiday';
121  $permissiontoread = $user->hasRight('holiday', 'read');
122  $permissiontodelete = $user->hasRight('holiday', 'delete');
123  $permissiontoapprove = $user->hasRight('holiday', 'approve');
124  $uploaddir = $conf->holiday->dir_output;
125  include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
126 
127  // If there is an update action
128  if ($action == 'update' && GETPOSTISSET('update_cp')) {
129  $error = 0;
130  $nbok = 0;
131 
132  $typeleaves = $holiday->getTypes(1, 1);
133 
134  $userID = array_keys(GETPOST('update_cp'));
135  $userID = $userID[0];
136 
137  $db->begin();
138 
139  foreach ($typeleaves as $key => $val) {
140  $userValue = GETPOST('nb_holiday_'.$val['rowid']);
141  $userValue = $userValue[$userID];
142 
143  if (!empty($userValue) || (string) $userValue == '0') {
144  $userValue = price2num($userValue, 5);
145  } else {
146  $userValue = '';
147  }
148 
149  //If the user set a comment, we add it to the log comment
150  $note_holiday = GETPOST('note_holiday');
151  $comment = ((isset($note_holiday[$userID]) && !empty($note_holiday[$userID])) ? ' ('.$note_holiday[$userID].')' : '');
152 
153  //print 'holiday: '.$val['rowid'].'-'.$userValue;exit;
154  if ($userValue != '') {
155  // 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)
156  $result = $holiday->addLogCP($user->id, $userID, $langs->transnoentitiesnoconv('ManualUpdate').$comment, $userValue, $val['rowid']);
157  if ($result < 0) {
158  setEventMessages($holiday->error, $holiday->errors, 'errors');
159  $error++;
160  } elseif ($result == 0) {
161  setEventMessages($langs->trans("HolidayQtyNotModified", $user->login), null, 'warnings');
162  }
163 
164  // Update of the days of the employee
165  if ($result > 0) {
166  $nbok++;
167 
168  $result = $holiday->updateSoldeCP($userID, $userValue, $val['rowid']);
169  if ($result < 0) {
170  setEventMessages($holiday->error, $holiday->errors, 'errors');
171  $error++;
172  }
173  }
174 
175  // If it first update of balance, we set date to avoid to have sold incremented by new month
176  /*
177  $now=dol_now();
178  $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
179  $sql.= " value = '".dol_print_date($now,'%Y%m%d%H%M%S')."'";
180  $sql.= " WHERE name = 'lastUpdate' and value IS NULL"; // Add value IS NULL to be sure to update only at init.
181  dol_syslog('define_holiday update lastUpdate entry', LOG_DEBUG);
182  $result = $db->query($sql);
183  */
184  }
185  }
186 
187  if (!$error) {
188  $db->commit();
189 
190  if ($nbok > 0) {
191  setEventMessages('UpdateConfCPOK', '', 'mesgs');
192  }
193  } else {
194  $db->rollback();
195  }
196  }
197 }
198 
199 
200 /*
201  * View
202  */
203 
204 $form = new Form($db);
205 $userstatic = new User($db);
206 
207 
208 $title = $langs->trans('CPTitreMenu');
209 
210 llxHeader('', $title);
211 
212 $typeleaves = $holiday->getTypes(1, 1);
213 $result = $holiday->updateBalance(); // Create users into table holiday if they don't exists. TODO Remove this whif we use field into table user.
214 if ($result < 0) {
215  setEventMessages($holiday->error, $holiday->errors, 'errors');
216 }
217 
218 // List of mass actions available
219 $arrayofmassactions = array(
220  //'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
221  //'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
222  //'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
223 );
224 if ($user->hasRight("holiday", "approve")) {
225  $arrayofmassactions['preincreaseholiday'] = img_picto('', 'add', 'class="pictofixedwidth"').$langs->trans("IncreaseHolidays");
226 }
227 $massactionbutton = $form->selectMassAction('', $arrayofmassactions);
228 
229 
230 print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
231 if ($optioncss != '') {
232  print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
233 }
234 print '<input type="hidden" name="token" value="'.newToken().'">';
235 print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
236 print '<input type="hidden" name="action" value="update">';
237 print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
238 print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
239 print '<input type="hidden" name="page" value="'.$page.'">';
240 print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
241 
242 $title = $langs->trans("MenuConfCP");
243 print_barre_liste($title, $page, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, $massactionbutton, '', '', 'title_hrm', 0, '', '', $limit, 0, 0, 1);
244 
245 include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
246 
247 if ($massaction == 'preincreaseholiday') {
248  $langs->load("holiday", "hrm");
249  require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
250  $staticholiday = new Holiday($db);
251  $arraytypeholidays = $staticholiday->getTypes(1, 1);
252  $formquestion[] = array();
253  $labeltypes = array();
254  foreach ($typeleaves as $key => $val) {
255  $labeltypes[$val['id']] = ($langs->trans($val['code']) != $val['code']) ? $langs->trans($val['code']) : $langs->trans($val['label']);
256  }
257  $formquestion [] = array( 'type' => 'other',
258  'name' => 'typeofholiday',
259  'label' => $langs->trans("Type"),
260  'value' => $form->selectarray('typeholiday', $labeltypes, GETPOST('typeholiday', 'alpha'), 1)
261  );
262  $formquestion [] = array( 'type' => 'other',
263  'name' => 'nbdaysholydays',
264  'label' => $langs->trans("NumberDayAddMass"),
265  'value' => '<input name="nbdaysholidays" class="maxwidth75" id="nbdaysholidays" value="'.GETPOST('nbdaysholidays', 'int').'">'
266  );
267  print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassIncreaseHoliday"), $langs->trans("ConfirmMassIncreaseHolidayQuestion", count($toselect)), "increaseholiday", $formquestion, 1, 0, 200, 500, 1);
268 }
269 
270 print '<div class="info">'.$langs->trans('LastUpdateCP').': '."\n";
271 $lastUpdate = $holiday->getConfCP('lastUpdate');
272 if ($lastUpdate) {
273  print '<strong>'.dol_print_date($db->jdate($lastUpdate), 'dayhour').'</strong>';
274  print '<br>'.$langs->trans("MonthOfLastMonthlyUpdate").': <strong>'.$langs->trans('Month'.substr($lastUpdate, 4, 2)).' '.substr($lastUpdate, 0, 4).'</strong>'."\n";
275 } else {
276  print $langs->trans('None');
277 }
278 print "</div><br>\n";
279 
280 
281 $filters = '';
282 
283 // Filter on array of ids of all childs
284 $userchilds = array();
285 if (empty($user->rights->holiday->readall)) {
286  $userchilds = $user->getAllChildIds(1);
287  $filters .= ' AND u.rowid IN ('.$db->sanitize(join(', ', $userchilds)).')';
288 }
289 if (!empty($search_name)) {
290  $filters .= natural_search(array('u.firstname', 'u.lastname'), $search_name);
291 }
292 if ($search_supervisor > 0) {
293  $filters .= natural_search(array('u.fk_user'), $search_supervisor, 2);
294 }
295 $filters .= ' AND employee = 1'; // Only employee users are visible
296 
297 $listUsers = $holiday->fetchUsers(false, true, $filters);
298 if (is_numeric($listUsers) && $listUsers < 0) {
299  setEventMessages($holiday->error, $holiday->errors, 'errors');
300 }
301 
302 $i = 0;
303 
304 
305 if (count($typeleaves) == 0) {
306  //print '<div class="info">';
307  print $langs->trans("NoLeaveWithCounterDefined")."<br>\n";
308  print $langs->trans("GoIntoDictionaryHolidayTypes");
309  //print '</div>';
310 } else {
311  $canedit = 0;
312  if (!empty($user->rights->holiday->define_holiday)) {
313  $canedit = 1;
314  }
315 
316  $moreforfilter = '';
317 
318  print '<div class="div-table-responsive">';
319  print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'" id="tablelines3">'."\n";
320 
321  print '<tr class="liste_titre_filter">';
322 
323  // User
324  print '<td class="liste_titre">';
325  print '<input type="text" name="search_name" value="'.dol_escape_htmltag($search_name).'" class="maxwidth100">';
326  print '</td>';
327 
328  // Supervisor
329  print '<td class="liste_titre">';
330  print $form->select_dolusers($search_supervisor, 'search_supervisor', 1, null, 0, null, null, 0, 0, 0, '', 0, '', 'maxwidth150');
331  print '</td>';
332 
333  // Type of leave request
334  if (count($typeleaves)) {
335  foreach ($typeleaves as $key => $val) {
336  print '<td class="liste_titre" style="text-align:center"></td>';
337  }
338  } else {
339  print '<td class="liste_titre"></td>';
340  }
341  print '<td class="liste_titre"></td>';
342  print '<td class="liste_titre"></td>';
343 
344  // Action column
345  print '<td class="liste_titre maxwidthsearch center">';
346  $searchpicto = $form->showFilterButtons();
347  print $searchpicto;
348  print '</td>';
349 
350  print '</tr>';
351 
352  print '<tr class="liste_titre">';
353  print_liste_field_titre('Employee', $_SERVER["PHP_SELF"]);
354  print_liste_field_titre('Supervisor', $_SERVER["PHP_SELF"]);
355  if (count($typeleaves)) {
356  foreach ($typeleaves as $key => $val) {
357  $labeltype = ($langs->trans($val['code']) != $val['code']) ? $langs->trans($val['code']) : $langs->trans($val['label']);
358  print_liste_field_titre($labeltype, $_SERVER["PHP_SELF"], '', '', '', '', '', '', 'center ');
359  }
360  } else {
361  print_liste_field_titre('NoLeaveWithCounterDefined', $_SERVER["PHP_SELF"], '', '', '', '');
362  }
363  print_liste_field_titre((empty($user->rights->holiday->define_holiday) ? '' : 'Note'), $_SERVER["PHP_SELF"]);
365 
366  $selectedfields = '';
367  if ($massactionbutton) {
368  $selectedfields = $form->showCheckAddButtons('checkforselect', 1);
369  }
370 
371  print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
372  print '</tr>';
373 
374  $usersupervisor = new User($db);
375 
376  foreach ($listUsers as $users) {
377  $arrayofselected = is_array($toselect) ? $toselect : array();
378 
379  // If user has not permission to edit/read all, we must see only subordinates
380  if (empty($user->rights->holiday->readall)) {
381  if (($users['rowid'] != $user->id) && (!in_array($users['rowid'], $userchilds))) {
382  continue; // This user is not into hierarchy of current user, we hide it.
383  }
384  }
385 
386  $userstatic->id = $users['rowid'];
387  $userstatic->lastname = $users['lastname'];
388  $userstatic->firstname = $users['firstname'];
389  $userstatic->gender = $users['gender'];
390  $userstatic->photo = $users['photo'];
391  $userstatic->statut = $users['status'];
392  $userstatic->employee = $users['employee'];
393  $userstatic->fk_user = $users['fk_user'];
394 
395  if ($userstatic->fk_user > 0) {
396  $usersupervisor->fetch($userstatic->fk_user);
397  }
398 
399  print '<tr class="oddeven">';
400 
401  // User
402  print '<td>';
403  print $userstatic->getNomUrl(-1);
404  print '</td>';
405 
406  // Supervisor
407  print '<td>';
408  if ($userstatic->fk_user > 0) {
409  print $usersupervisor->getNomUrl(-1);
410  }
411  print '</td>';
412 
413  // Amount for each type
414  if (count($typeleaves)) {
415  foreach ($typeleaves as $key => $val) {
416  $nbtoshow = '';
417  if ($holiday->getCPforUser($users['rowid'], $val['rowid']) != '') {
418  $nbtoshow = price2num($holiday->getCPforUser($users['rowid'], $val['rowid']), 5);
419  }
420 
421  //var_dump($users['rowid'].' - '.$val['rowid']);
422  print '<td style="text-align:center">';
423  if ($canedit) {
424  print '<input type="text"'.($canedit ? '' : ' disabled="disabled"').' value="'.$nbtoshow.'" name="nb_holiday_'.$val['rowid'].'['.$users['rowid'].']" class="width75 center" />';
425  } else {
426  print $nbtoshow;
427  }
428  //print ' '.$langs->trans('days');
429  print '</td>'."\n";
430  }
431  } else {
432  print '<td></td>';
433  }
434 
435  // Note
436  print '<td>';
437  if ($canedit) {
438  print '<input type="text"'.($canedit ? '' : ' disabled="disabled"').' class="maxwidthonsmartphone" value="" name="note_holiday['.$users['rowid'].']" size="30"/>';
439  }
440  print '</td>';
441 
442  // Button modify
443  print '<td class="center">';
444  if (!empty($user->rights->holiday->define_holiday)) { // Allowed to set the balance of any user
445  print '<input type="submit" name="update_cp['.$users['rowid'].']" value="'.dol_escape_htmltag($langs->trans("Save")).'" class="button smallpaddingimp"/>';
446  }
447  print '</td>'."\n";
448  print '<td class="nowrap center">';
449 
450  if ($massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
451  $selected = 0;
452  if (in_array($userstatic->id, $arrayofselected)) {
453  $selected = 1;
454  }
455  print '<input id="cb'.$userstatic->id.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$userstatic->id.'"'.($selected ? ' checked="checked"' : '').'>';
456  }
457  print '</td>';
458  print '</tr>';
459 
460  $i++;
461  }
462 
463  print '</table>';
464  print '</div>';
465 }
466 
467 print '</form>';
468 
469 // End of page
470 llxFooter();
471 $db->close();
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class to manage standard extra fields.
Class to manage generation of HTML components Only common components must be here.
Class of the module paid holiday.
Class to manage Dolibarr users.
Definition: user.class.php:47
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
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.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.