define_holiday.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /* Copyright (C) 2007-2016 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 <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * File that defines the balance of paid holiday of users.
  22. *
  23. * \file htdocs/holiday/define_holiday.php
  24. * \ingroup holiday
  25. * \brief File that defines the balance of paid holiday of users.
  26. */
  27. require '../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/holiday/common.inc.php';
  30. // Load translation files required by the page
  31. $langs->loadlangs(array('users', 'hrm'));
  32. $action=GETPOST('action','aZ09');
  33. $contextpage=GETPOST('contextpage','aZ')?GETPOST('contextpage','aZ'):'defineholidaylist';
  34. $search_name=GETPOST('search_name', 'alpha');
  35. $search_supervisor=GETPOST('search_supervisor', 'int');
  36. // Load variable for pagination
  37. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  38. $sortfield = GETPOST('sortfield','alpha');
  39. $sortorder = GETPOST('sortorder','alpha');
  40. $page = GETPOST('page','int');
  41. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  42. $offset = $limit * $page;
  43. $pageprev = $page - 1;
  44. $pagenext = $page + 1;
  45. if (! $sortfield) $sortfield="t.rowid"; // Set here default search field
  46. if (! $sortorder) $sortorder="ASC";
  47. // Protection if external user
  48. if ($user->societe_id > 0) accessforbidden();
  49. // If the user does not have perm to read the page
  50. if (!$user->rights->holiday->read) accessforbidden();
  51. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
  52. $hookmanager->initHooks(array('defineholidaylist'));
  53. $extrafields = new ExtraFields($db);
  54. $holiday = new Holiday($db);
  55. /*
  56. * Actions
  57. */
  58. if (GETPOST('cancel','alpha')) { $action='list'; $massaction=''; }
  59. if (! GETPOST('confirmmassaction','alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  60. $parameters=array();
  61. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  62. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  63. if (empty($reshook))
  64. {
  65. // Selection of new fields
  66. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  67. // Purge search criteria
  68. 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
  69. {
  70. $search_name='';
  71. $search_supervisor='';
  72. $toselect='';
  73. $search_array_options=array();
  74. }
  75. // Mass actions
  76. /*
  77. $objectclass='Skeleton';
  78. $objectlabel='Skeleton';
  79. $permtoread = $user->rights->skeleton->read;
  80. $permtodelete = $user->rights->skeleton->delete;
  81. $uploaddir = $conf->skeleton->dir_output;
  82. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  83. */
  84. // Si il y a une action de mise à jour
  85. if ($action == 'update' && isset($_POST['update_cp']))
  86. {
  87. $error = 0;
  88. $typeleaves=$holiday->getTypes(1,1);
  89. $userID = array_keys($_POST['update_cp']);
  90. $userID = $userID[0];
  91. foreach($typeleaves as $key => $val)
  92. {
  93. $userValue = $_POST['nb_holiday_'.$val['rowid']];
  94. $userValue = $userValue[$userID];
  95. if (!empty($userValue) || (string) $userValue == '0')
  96. {
  97. $userValue = price2num($userValue,5);
  98. } else {
  99. $userValue = '';
  100. }
  101. //If the user set a comment, we add it to the log comment
  102. $comment = ((isset($_POST['note_holiday'][$userID]) && !empty($_POST['note_holiday'][$userID])) ? ' ('.$_POST['note_holiday'][$userID].')' : '');
  103. //print 'holiday: '.$val['rowid'].'-'.$userValue;
  104. if ($userValue != '')
  105. {
  106. // We add the modification to the log (must be before update of sold because we read current value of sold)
  107. $result=$holiday->addLogCP($user->id, $userID, $langs->transnoentitiesnoconv('ManualUpdate').$comment, $userValue, $val['rowid']);
  108. if ($result < 0)
  109. {
  110. setEventMessages($holiday->error, $holiday->errors, 'errors');
  111. $error++;
  112. }
  113. // Update of the days of the employee
  114. $result = $holiday->updateSoldeCP($userID, $userValue, $val['rowid']);
  115. if ($result < 0)
  116. {
  117. setEventMessages($holiday->error, $holiday->errors, 'errors');
  118. $error++;
  119. }
  120. // If it first update of balance, we set date to avoid to have sold incremented by new month
  121. /*
  122. $now=dol_now();
  123. $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
  124. $sql.= " value = '".dol_print_date($now,'%Y%m%d%H%M%S')."'";
  125. $sql.= " WHERE name = 'lastUpdate' and value IS NULL"; // Add value IS NULL to be sure to update only at init.
  126. dol_syslog('define_holiday update lastUpdate entry', LOG_DEBUG);
  127. $result = $db->query($sql);
  128. */
  129. }
  130. }
  131. if (! $error) setEventMessages('UpdateConfCPOK', '', 'mesgs');
  132. }
  133. }
  134. /*
  135. * View
  136. */
  137. $form = new Form($db);
  138. $userstatic=new User($db);
  139. llxHeader('', $langs->trans('CPTitreMenu'));
  140. $typeleaves=$holiday->getTypes(1,1);
  141. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  142. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  143. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  144. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  145. print '<input type="hidden" name="action" value="update">';
  146. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  147. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  148. print '<input type="hidden" name="page" value="'.$page.'">';
  149. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  150. print load_fiche_titre($langs->trans('MenuConfCP'), '', 'title_hrm.png');
  151. print '<div class="info">'.$langs->trans('LastUpdateCP').': '."\n";
  152. $lastUpdate = $holiday->getConfCP('lastUpdate');
  153. if ($lastUpdate)
  154. {
  155. $monthLastUpdate = $lastUpdate[4].$lastUpdate[5];
  156. $yearLastUpdate = $lastUpdate[0].$lastUpdate[1].$lastUpdate[2].$lastUpdate[3];
  157. print '<strong>'.dol_print_date($db->jdate($holiday->getConfCP('lastUpdate')),'dayhour','tzuser').'</strong>';
  158. print '<br>'.$langs->trans("MonthOfLastMonthlyUpdate").': <strong>'.$yearLastUpdate.'-'.$monthLastUpdate.'</strong>'."\n";
  159. }
  160. else print $langs->trans('None');
  161. print "</div><br>\n";
  162. $result = $holiday->updateBalance(); // Create users into table holiday if they don't exists. TODO Remove this whif we use field into table user.
  163. if ($result < 0)
  164. {
  165. setEventMessages($holiday->error, $holiday->errors, 'errors');
  166. }
  167. $filters = '';
  168. // Filter on array of ids of all childs
  169. $userchilds=array();
  170. if (empty($user->rights->holiday->read_all))
  171. {
  172. $userchilds=$user->getAllChildIds(1);
  173. $filters.=' AND u.rowid IN ('.join(', ',$userchilds).')';
  174. }
  175. if (!empty($search_name)) {
  176. $filters.=natural_search(array('u.firstname','u.lastname'), $search_name);
  177. }
  178. if ($search_supervisor > 0) $filters.=natural_search(array('u.fk_user'), $search_supervisor, 2);
  179. $filters.= ' AND employee = 1'; // Only employee users are visible
  180. $listUsers = $holiday->fetchUsers(false, true, $filters);
  181. if (is_numeric($listUsers) && $listUsers < 0)
  182. {
  183. setEventMessages($holiday->error, $holiday->errors, 'errors');
  184. }
  185. $i = 0;
  186. if (count($typeleaves) == 0)
  187. {
  188. //print '<div class="info">';
  189. print $langs->trans("NoLeaveWithCounterDefined")."<br>\n";
  190. print $langs->trans("GoIntoDictionaryHolidayTypes");
  191. //print '</div>';
  192. }
  193. else
  194. {
  195. $canedit=0;
  196. if (! empty($user->rights->holiday->define_holiday)) $canedit=1;
  197. $moreforfilter='';
  198. print '<div class="div-table-responsive">';
  199. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'" id="tablelines3">'."\n";
  200. print '<tr class="liste_titre_filter">';
  201. // User
  202. print '<td class="liste_titre"><input type="text" name="search_name" value="'.dol_escape_htmltag($search_name).'"></td>';
  203. // Supervisor
  204. print '<td class="liste_titre">';
  205. print $form->select_dolusers($search_supervisor, 'search_supervisor', 1, null, 0, null, null, 0, 0, 0, '', 0, '', 'maxwidth200');
  206. print '</td>';
  207. // Type of leave request
  208. if (count($typeleaves))
  209. {
  210. foreach($typeleaves as $key => $val)
  211. {
  212. print '<td class="liste_titre" style="text-align:center"></td>';
  213. }
  214. }
  215. else
  216. {
  217. print '<td class="liste_titre"></td>';
  218. }
  219. print '<td class="liste_titre"></td>';
  220. // Action column
  221. print '<td class="liste_titre" align="right">';
  222. $searchpicto=$form->showFilterButtons();
  223. print $searchpicto;
  224. print '</td>';
  225. print '</tr>';
  226. print '<tr class="liste_titre">';
  227. print_liste_field_titre('Employee', $_SERVER["PHP_SELF"]);
  228. print_liste_field_titre('Supervisor', $_SERVER["PHP_SELF"]);
  229. if (count($typeleaves))
  230. {
  231. foreach($typeleaves as $key => $val)
  232. {
  233. $labeltype = ($langs->trans($val['code'])!=$val['code']) ? $langs->trans($val['code']) : $langs->trans($val['label']);
  234. print_liste_field_titre($labeltype, $_SERVER["PHP_SELF"], '', '', '', 'align="center"');
  235. }
  236. }
  237. else
  238. {
  239. print_liste_field_titre('NoLeaveWithCounterDefined', $_SERVER["PHP_SELF"], '', '', '', '');
  240. }
  241. print_liste_field_titre((empty($user->rights->holiday->define_holiday) ? '' : 'Note'), $_SERVER["PHP_SELF"]);
  242. print_liste_field_titre('');
  243. print '</tr>';
  244. $usersupervisor = new User($db);
  245. foreach($listUsers as $users)
  246. {
  247. // If user has not permission to edit/read all, we must see only subordinates
  248. if (empty($user->rights->holiday->read_all))
  249. {
  250. if (($users['rowid'] != $user->id) && (! in_array($users['rowid'], $userchilds))) continue; // This user is not into hierarchy of current user, we hide it.
  251. }
  252. $userstatic->id=$users['rowid'];
  253. $userstatic->lastname=$users['lastname'];
  254. $userstatic->firstname=$users['firstname'];
  255. $userstatic->gender=$users['gender'];
  256. $userstatic->photo=$users['photo'];
  257. $userstatic->statut=$users['status'];
  258. $userstatic->employee=$users['employee'];
  259. $userstatic->fk_user=$users['fk_user'];
  260. if ($userstatic->fk_user > 0) $usersupervisor->fetch($userstatic->fk_user);
  261. print '<tr class="oddeven">';
  262. // User
  263. print '<td>';
  264. print $userstatic->getNomUrl(-1);
  265. print '</td>';
  266. // Supervisor
  267. print '<td>';
  268. if ($userstatic->fk_user > 0) print $usersupervisor->getNomUrl(-1);
  269. print '</td>';
  270. // Amount for each type
  271. if (count($typeleaves))
  272. {
  273. foreach($typeleaves as $key => $val)
  274. {
  275. $nbtoshow='';
  276. if ($holiday->getCPforUser($users['rowid'], $val['rowid']) != '') $nbtoshow=price2num($holiday->getCPforUser($users['rowid'], $val['rowid']), 5);
  277. //var_dump($users['rowid'].' - '.$val['rowid']);
  278. print '<td style="text-align:center">';
  279. if ($canedit) print '<input type="text"'.($canedit?'':' disabled="disabled"').' value="'.$nbtoshow.'" name="nb_holiday_'.$val['rowid'].'['.$users['rowid'].']" size="5" style="text-align: center;"/>';
  280. else print $nbtoshow;
  281. //print ' '.$langs->trans('days');
  282. print '</td>'."\n";
  283. }
  284. }
  285. else
  286. {
  287. print '<td></td>';
  288. }
  289. // Note
  290. print '<td>';
  291. if ($canedit) print '<input type="text"'.($canedit?'':' disabled="disabled"').' class="maxwidthonsmartphone" value="" name="note_holiday['.$users['rowid'].']" size="30"/>';
  292. print '</td>';
  293. // Button modify
  294. print '<td>';
  295. if (! empty($user->rights->holiday->define_holiday)) // Allowed to set the balance of any user
  296. {
  297. print '<input type="submit" name="update_cp['.$users['rowid'].']" value="'.dol_escape_htmltag($langs->trans("Update")).'" class="button"/>';
  298. }
  299. print '</td>'."\n";
  300. print '</tr>';
  301. $i++;
  302. }
  303. print '</table>';
  304. print '</div>';
  305. }
  306. print '</form>';
  307. // End of page
  308. llxFooter();
  309. $db->close();