define_holiday.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. /**
  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. // Load Dolibarr environment
  28. require '../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
  31. // Load translation files required by the page
  32. $langs->loadlangs(array('users', 'other', 'holiday', 'hrm'));
  33. $action = GETPOST('action', 'aZ09');
  34. $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'defineholidaylist';
  35. $massaction = GETPOST('massaction', 'alpha');
  36. $optioncss = GETPOST('optioncss', 'alpha');
  37. $search_name = GETPOST('search_name', 'alpha');
  38. $search_supervisor = GETPOST('search_supervisor', 'int');
  39. // Load variable for pagination
  40. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  41. $sortfield = GETPOST('sortfield', 'aZ09comma');
  42. $sortorder = GETPOST('sortorder', 'aZ09comma');
  43. $toselect = GETPOST('toselect', 'array'); // Array of ids of elements selected into a list
  44. $confirm = GETPOST('confirm', 'alpha');
  45. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  46. if (empty($page) || $page == -1) {
  47. $page = 0;
  48. } // If $page is not defined, or '' or -1
  49. $offset = $limit * $page;
  50. $pageprev = $page - 1;
  51. $pagenext = $page + 1;
  52. if (!$sortfield) {
  53. $sortfield = "t.rowid"; // Set here default search field
  54. }
  55. if (!$sortorder) {
  56. $sortorder = "ASC";
  57. }
  58. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
  59. $hookmanager->initHooks(array('defineholidaylist'));
  60. $extrafields = new ExtraFields($db);
  61. $holiday = new Holiday($db);
  62. if (empty($conf->holiday->enabled)) {
  63. accessforbidden('Module not enabled');
  64. }
  65. // Protection if external user
  66. if ($user->socid > 0) {
  67. accessforbidden();
  68. }
  69. // If the user does not have perm to read the page
  70. if (empty($user->rights->holiday->read)) {
  71. accessforbidden();
  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. $objectclass = 'Holiday';
  99. $objectlabel = 'Holiday';
  100. $permissiontoread = $user->hasRight('holiday', 'read');
  101. $permissiontodelete = $user->hasRight('holiday', 'delete');
  102. $permissiontoapprove = $user->hasRight('holiday', 'approve');
  103. $uploaddir = $conf->holiday->dir_output;
  104. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  105. // If there is an update action
  106. if ($action == 'update' && GETPOSTISSET('update_cp')) {
  107. $error = 0;
  108. $nbok = 0;
  109. $typeleaves = $holiday->getTypes(1, 1);
  110. $userID = array_keys(GETPOST('update_cp'));
  111. $userID = $userID[0];
  112. $db->begin();
  113. foreach ($typeleaves as $key => $val) {
  114. $userValue = GETPOST('nb_holiday_'.$val['rowid']);
  115. $userValue = $userValue[$userID];
  116. if (!empty($userValue) || (string) $userValue == '0') {
  117. $userValue = price2num($userValue, 5);
  118. } else {
  119. $userValue = '';
  120. }
  121. //If the user set a comment, we add it to the log comment
  122. $note_holiday = GETPOST('note_holiday');
  123. $comment = ((isset($note_holiday[$userID]) && !empty($note_holiday[$userID])) ? ' ('.$note_holiday[$userID].')' : '');
  124. //print 'holiday: '.$val['rowid'].'-'.$userValue;exit;
  125. if ($userValue != '') {
  126. // 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)
  127. $result = $holiday->addLogCP($user->id, $userID, $langs->transnoentitiesnoconv('ManualUpdate').$comment, $userValue, $val['rowid']);
  128. if ($result < 0) {
  129. setEventMessages($holiday->error, $holiday->errors, 'errors');
  130. $error++;
  131. } elseif ($result == 0) {
  132. setEventMessages($langs->trans("HolidayQtyNotModified", $user->login), null, 'warnings');
  133. }
  134. // Update of the days of the employee
  135. if ($result > 0) {
  136. $nbok++;
  137. $result = $holiday->updateSoldeCP($userID, $userValue, $val['rowid']);
  138. if ($result < 0) {
  139. setEventMessages($holiday->error, $holiday->errors, 'errors');
  140. $error++;
  141. }
  142. }
  143. // If it first update of balance, we set date to avoid to have sold incremented by new month
  144. /*
  145. $now=dol_now();
  146. $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
  147. $sql.= " value = '".dol_print_date($now,'%Y%m%d%H%M%S')."'";
  148. $sql.= " WHERE name = 'lastUpdate' and value IS NULL"; // Add value IS NULL to be sure to update only at init.
  149. dol_syslog('define_holiday update lastUpdate entry', LOG_DEBUG);
  150. $result = $db->query($sql);
  151. */
  152. }
  153. }
  154. if (!$error) {
  155. $db->commit();
  156. if ($nbok > 0) {
  157. setEventMessages('UpdateConfCPOK', null, 'mesgs');
  158. }
  159. } else {
  160. $db->rollback();
  161. }
  162. }
  163. }
  164. /*
  165. * View
  166. */
  167. $form = new Form($db);
  168. $userstatic = new User($db);
  169. $title = $langs->trans('CPTitreMenu');
  170. llxHeader('', $title);
  171. $typeleaves = $holiday->getTypes(1, 1);
  172. $result = $holiday->updateBalance(); // Create users into table holiday if they don't exists. TODO Remove this whif we use field into table user.
  173. if ($result < 0) {
  174. setEventMessages($holiday->error, $holiday->errors, 'errors');
  175. }
  176. // List of mass actions available
  177. $arrayofmassactions = array(
  178. //'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
  179. //'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
  180. //'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
  181. );
  182. if ($user->hasRight("holiday", "approve")) {
  183. $arrayofmassactions['preincreaseholiday'] = img_picto('', 'add', 'class="pictofixedwidth"').$langs->trans("IncreaseHolidays");
  184. }
  185. $massactionbutton = $form->selectMassAction('', $arrayofmassactions);
  186. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  187. if ($optioncss != '') {
  188. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  189. }
  190. print '<input type="hidden" name="token" value="'.newToken().'">';
  191. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  192. print '<input type="hidden" name="action" value="update">';
  193. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  194. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  195. print '<input type="hidden" name="page" value="'.$page.'">';
  196. print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
  197. $title = $langs->trans("MenuConfCP");
  198. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, $massactionbutton, '', '', 'title_hrm', 0, '', '', $limit, 0, 0, 1);
  199. include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
  200. if ($massaction == 'preincreaseholiday') {
  201. $langs->load("holiday", "hrm");
  202. require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
  203. $staticholiday = new Holiday($db);
  204. $arraytypeholidays = $staticholiday->getTypes(1, 1);
  205. $formquestion[] = array();
  206. $labeltypes = array();
  207. foreach ($typeleaves as $key => $val) {
  208. $labeltypes[$val['id']] = ($langs->trans($val['code']) != $val['code']) ? $langs->trans($val['code']) : $langs->trans($val['label']);
  209. }
  210. $formquestion [] = array( 'type' => 'other',
  211. 'name' => 'typeofholiday',
  212. 'label' => $langs->trans("Type"),
  213. 'value' => $form->selectarray('typeholiday', $labeltypes, GETPOST('typeholiday', 'alpha'), 1)
  214. );
  215. $formquestion [] = array( 'type' => 'other',
  216. 'name' => 'nbdaysholydays',
  217. 'label' => $langs->trans("NumberDayAddMass"),
  218. 'value' => '<input name="nbdaysholidays" class="maxwidth75" id="nbdaysholidays" value="'.GETPOST('nbdaysholidays', 'int').'">'
  219. );
  220. print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassIncreaseHoliday"), $langs->trans("ConfirmMassIncreaseHolidayQuestion", count($toselect)), "increaseholiday", $formquestion, 1, 0, 200, 500, 1);
  221. }
  222. print '<div class="info">'.$langs->trans('LastUpdateCP').': '."\n";
  223. $lastUpdate = $holiday->getConfCP('lastUpdate');
  224. if ($lastUpdate) {
  225. print '<strong>'.dol_print_date($db->jdate($lastUpdate), 'dayhour').'</strong>';
  226. print '<br>'.$langs->trans("MonthOfLastMonthlyUpdate").': <strong>'.$langs->trans('Month'.substr($lastUpdate, 4, 2)).' '.substr($lastUpdate, 0, 4).'</strong>'."\n";
  227. } else {
  228. print $langs->trans('None');
  229. }
  230. print "</div><br>\n";
  231. $filters = '';
  232. // Filter on array of ids of all childs
  233. $userchilds = array();
  234. if (empty($user->rights->holiday->readall)) {
  235. $userchilds = $user->getAllChildIds(1);
  236. $filters .= ' AND u.rowid IN ('.$db->sanitize(join(', ', $userchilds)).')';
  237. }
  238. if (!empty($search_name)) {
  239. $filters .= natural_search(array('u.firstname', 'u.lastname'), $search_name);
  240. }
  241. if ($search_supervisor > 0) {
  242. $filters .= natural_search(array('u.fk_user'), $search_supervisor, 2);
  243. }
  244. $filters .= ' AND employee = 1'; // Only employee users are visible
  245. $listUsers = $holiday->fetchUsers(false, true, $filters);
  246. if (is_numeric($listUsers) && $listUsers < 0) {
  247. setEventMessages($holiday->error, $holiday->errors, 'errors');
  248. }
  249. $i = 0;
  250. if (count($typeleaves) == 0) {
  251. //print '<div class="info">';
  252. print $langs->trans("NoLeaveWithCounterDefined")."<br>\n";
  253. print $langs->trans("GoIntoDictionaryHolidayTypes");
  254. //print '</div>';
  255. } else {
  256. $canedit = 0;
  257. if (!empty($user->rights->holiday->define_holiday)) {
  258. $canedit = 1;
  259. }
  260. $moreforfilter = '';
  261. print '<div class="div-table-responsive">';
  262. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'" id="tablelines3">'."\n";
  263. print '<tr class="liste_titre_filter">';
  264. // User
  265. print '<td class="liste_titre">';
  266. print '<input type="text" name="search_name" value="'.dol_escape_htmltag($search_name).'" class="maxwidth100">';
  267. print '</td>';
  268. // Supervisor
  269. print '<td class="liste_titre">';
  270. print $form->select_dolusers($search_supervisor, 'search_supervisor', 1, null, 0, null, null, 0, 0, 0, '', 0, '', 'maxwidth150');
  271. print '</td>';
  272. // Type of leave request
  273. if (count($typeleaves)) {
  274. foreach ($typeleaves as $key => $val) {
  275. print '<td class="liste_titre" style="text-align:center"></td>';
  276. }
  277. } else {
  278. print '<td class="liste_titre"></td>';
  279. }
  280. print '<td class="liste_titre"></td>';
  281. print '<td class="liste_titre"></td>';
  282. // Action column
  283. print '<td class="liste_titre maxwidthsearch center">';
  284. $searchpicto = $form->showFilterButtons();
  285. print $searchpicto;
  286. print '</td>';
  287. print '</tr>';
  288. print '<tr class="liste_titre">';
  289. print_liste_field_titre('Employee', $_SERVER["PHP_SELF"]);
  290. print_liste_field_titre('Supervisor', $_SERVER["PHP_SELF"]);
  291. if (count($typeleaves)) {
  292. foreach ($typeleaves as $key => $val) {
  293. $labeltype = ($langs->trans($val['code']) != $val['code']) ? $langs->trans($val['code']) : $langs->trans($val['label']);
  294. print_liste_field_titre($labeltype, $_SERVER["PHP_SELF"], '', '', '', '', '', '', 'center ');
  295. }
  296. } else {
  297. print_liste_field_titre('NoLeaveWithCounterDefined', $_SERVER["PHP_SELF"], '', '', '', '');
  298. }
  299. print_liste_field_titre((empty($user->rights->holiday->define_holiday) ? '' : 'Note'), $_SERVER["PHP_SELF"]);
  300. print_liste_field_titre('');
  301. $selectedfields = '';
  302. if ($massactionbutton) {
  303. $selectedfields = $form->showCheckAddButtons('checkforselect', 1);
  304. }
  305. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
  306. print '</tr>';
  307. $usersupervisor = new User($db);
  308. foreach ($listUsers as $users) {
  309. $arrayofselected = is_array($toselect) ? $toselect : array();
  310. // If user has not permission to edit/read all, we must see only subordinates
  311. if (empty($user->rights->holiday->readall)) {
  312. if (($users['rowid'] != $user->id) && (!in_array($users['rowid'], $userchilds))) {
  313. continue; // This user is not into hierarchy of current user, we hide it.
  314. }
  315. }
  316. $userstatic->id = $users['rowid'];
  317. $userstatic->lastname = $users['lastname'];
  318. $userstatic->firstname = $users['firstname'];
  319. $userstatic->gender = $users['gender'];
  320. $userstatic->photo = $users['photo'];
  321. $userstatic->statut = $users['status'];
  322. $userstatic->employee = $users['employee'];
  323. $userstatic->fk_user = $users['fk_user'];
  324. if ($userstatic->fk_user > 0) {
  325. $usersupervisor->fetch($userstatic->fk_user);
  326. }
  327. print '<tr class="oddeven">';
  328. // User
  329. print '<td>';
  330. print $userstatic->getNomUrl(-1);
  331. print '</td>';
  332. // Supervisor
  333. print '<td>';
  334. if ($userstatic->fk_user > 0) {
  335. print $usersupervisor->getNomUrl(-1);
  336. }
  337. print '</td>';
  338. // Amount for each type
  339. if (count($typeleaves)) {
  340. foreach ($typeleaves as $key => $val) {
  341. $nbtoshow = '';
  342. if ($holiday->getCPforUser($users['rowid'], $val['rowid']) != '') {
  343. $nbtoshow = price2num($holiday->getCPforUser($users['rowid'], $val['rowid']), 5);
  344. }
  345. //var_dump($users['rowid'].' - '.$val['rowid']);
  346. print '<td style="text-align:center">';
  347. if ($canedit) {
  348. print '<input type="text"'.($canedit ? '' : ' disabled="disabled"').' value="'.$nbtoshow.'" name="nb_holiday_'.$val['rowid'].'['.$users['rowid'].']" class="width75 center" />';
  349. } else {
  350. print $nbtoshow;
  351. }
  352. //print ' '.$langs->trans('days');
  353. print '</td>'."\n";
  354. }
  355. } else {
  356. print '<td></td>';
  357. }
  358. // Note
  359. print '<td>';
  360. if ($canedit) {
  361. print '<input type="text"'.($canedit ? '' : ' disabled="disabled"').' class="maxwidthonsmartphone" value="" name="note_holiday['.$users['rowid'].']" size="30"/>';
  362. }
  363. print '</td>';
  364. // Button modify
  365. print '<td class="center">';
  366. if (!empty($user->rights->holiday->define_holiday)) { // Allowed to set the balance of any user
  367. print '<input type="submit" name="update_cp['.$users['rowid'].']" value="'.dol_escape_htmltag($langs->trans("Save")).'" class="button smallpaddingimp"/>';
  368. }
  369. print '</td>'."\n";
  370. print '<td class="nowrap center">';
  371. if ($massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
  372. $selected = 0;
  373. if (in_array($userstatic->id, $arrayofselected)) {
  374. $selected = 1;
  375. }
  376. print '<input id="cb'.$userstatic->id.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$userstatic->id.'"'.($selected ? ' checked="checked"' : '').'>';
  377. }
  378. print '</td>';
  379. print '</tr>';
  380. $i++;
  381. }
  382. print '</table>';
  383. print '</div>';
  384. }
  385. print '</form>';
  386. // End of page
  387. llxFooter();
  388. $db->close();