define_holiday.php 13 KB

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