define_holiday.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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@capnetworks.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. // Protection if external user
  31. if ($user->societe_id > 0) accessforbidden();
  32. // If the user does not have perm to read the page
  33. if(!$user->rights->holiday->read) accessforbidden();
  34. $action=GETPOST('action');
  35. $holiday = new Holiday($db);
  36. $langs->load('users');
  37. $langs->load('hrm');
  38. /*
  39. * Actions
  40. */
  41. // Si il y a une action de mise à jour
  42. if ($action == 'update' && isset($_POST['update_cp']))
  43. {
  44. $error = 0;
  45. $typeleaves=$holiday->getTypes(1,1);
  46. $userID = array_keys($_POST['update_cp']);
  47. $userID = $userID[0];
  48. foreach($typeleaves as $key => $val)
  49. {
  50. $userValue = $_POST['nb_holiday_'.$val['rowid']];
  51. $userValue = $userValue[$userID];
  52. if (!empty($userValue))
  53. {
  54. $userValue = price2num($userValue,5);
  55. } else {
  56. $userValue = '';
  57. }
  58. //If the user set a comment, we add it to the log comment
  59. $comment = ((isset($_POST['note_holiday'][$userID]) && !empty($_POST['note_holiday'][$userID])) ? ' ('.$_POST['note_holiday'][$userID].')' : '');
  60. //print 'eee'.$val['rowid'].'-'.$userValue;
  61. if ($userValue != '')
  62. {
  63. // We add the modification to the log (must be before update of sold because we read current value of sold)
  64. $result=$holiday->addLogCP($user->id, $userID, $langs->transnoentitiesnoconv('ManualUpdate').$comment, $userValue, $val['rowid']);
  65. if ($result < 0)
  66. {
  67. setEventMessages($holiday->error, $holiday->errors, 'errors');
  68. $error++;
  69. }
  70. // Update of the days of the employee
  71. $result = $holiday->updateSoldeCP($userID, $userValue, $val['rowid']);
  72. if ($result < 0)
  73. {
  74. setEventMessages($holiday->error, $holiday->errors, 'errors');
  75. $error++;
  76. }
  77. // If it first update of balance, we set date to avoid to have sold incremented by new month
  78. /*
  79. $now=dol_now();
  80. $sql = "UPDATE ".MAIN_DB_PREFIX."holiday_config SET";
  81. $sql.= " value = '".dol_print_date($now,'%Y%m%d%H%M%S')."'";
  82. $sql.= " WHERE name = 'lastUpdate' and value IS NULL"; // Add value IS NULL to be sure to update only at init.
  83. dol_syslog('define_holiday update lastUpdate entry', LOG_DEBUG);
  84. $result = $db->query($sql);
  85. */
  86. }
  87. }
  88. if (! $error) setEventMessages('UpdateConfCPOK', '', 'mesgs');
  89. }
  90. elseif($action == 'add_event')
  91. {
  92. $error = 0;
  93. $typeleaves=$holiday->getTypes(1,1);
  94. if(!empty($_POST['list_event']) && $_POST['list_event'] > 0) {
  95. $event = $_POST['list_event'];
  96. } else { $error++;
  97. }
  98. if(!empty($_POST['userCP']) && $_POST['userCP'] > 0) {
  99. $userCP = $_POST['userCP'];
  100. } else { $erro++;
  101. }
  102. if ($error)
  103. {
  104. setEventMessages('ErrorAddEventToUserCP', '', 'errors');
  105. }
  106. else
  107. {
  108. $nb_holiday = $holiday->getCPforUser($userCP);
  109. $add_holiday = $holiday->getValueEventCp($event);
  110. $new_holiday = $nb_holiday + $add_holiday;
  111. // add event to existing types of vacation
  112. foreach ($typeleaves as $key => $leave)
  113. {
  114. $vacationTypeID = $leave['rowid'];
  115. // On ajoute la modification dans le LOG
  116. $holiday->addLogCP($user->id,$userCP, $holiday->getNameEventCp($event),$new_holiday, $vacationTypeID);
  117. $holiday->updateSoldeCP($userCP,$new_holiday, $vacationTypeID);
  118. }
  119. setEventMessages('AddEventToUserOkCP', '', 'mesgs');
  120. }
  121. }
  122. /*
  123. * View
  124. */
  125. $form = new Form($db);
  126. $userstatic=new User($db);
  127. llxHeader('', $langs->trans('CPTitreMenu'));
  128. print load_fiche_titre($langs->trans('MenuConfCP'), '', 'title_hrm.png');
  129. print '<div class="info">'.$langs->trans('LastUpdateCP').': '."\n";
  130. $lastUpdate = $holiday->getConfCP('lastUpdate');
  131. if ($lastUpdate)
  132. {
  133. $monthLastUpdate = $lastUpdate[4].$lastUpdate[5];
  134. $yearLastUpdate = $lastUpdate[0].$lastUpdate[1].$lastUpdate[2].$lastUpdate[3];
  135. print '<strong>'.dol_print_date($db->jdate($holiday->getConfCP('lastUpdate')),'dayhour','tzuser').'</strong>';
  136. print '<br>'.$langs->trans("MonthOfLastMonthlyUpdate").': <strong>'.$yearLastUpdate.'-'.$monthLastUpdate.'</strong>'."\n";
  137. }
  138. else print $langs->trans('None');
  139. print "</div><br>\n";
  140. $result = $holiday->updateBalance(); // Create users into table holiday if they don't exists. TODO Remove this whif we use field into table user.
  141. if ($result < 0)
  142. {
  143. setEventMessages($holiday->error, $holiday->errors, 'errors');
  144. }
  145. $listUsers = $holiday->fetchUsers(false,true);
  146. if (is_numeric($listUsers) && $listUsers < 0)
  147. {
  148. setEventMessages($holiday->error, $holiday->errors, 'errors');
  149. }
  150. $var=true;
  151. $i = 0;
  152. $cp_events = $holiday->fetchEventsCP();
  153. if ($cp_events == 1)
  154. {
  155. print '<br><form method="POST" action="'.$_SERVER["PHP_SELF"].'">'."\n";
  156. print '<input type="hidden" name="action" value="add_event" />';
  157. print load_fiche_titre($langs->trans('DefineEventUserCP'),'','');
  158. print $langs->trans('MotifCP').' : ';
  159. print $holiday->selectEventCP();
  160. print ' &nbsp; '.$langs->trans('UserCP').' : ';
  161. print $form->select_dolusers('', 'userCP', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
  162. print ' <input type="submit" value="'.$langs->trans("addEventToUserCP").'" name="bouton" class="button"/>';
  163. print '</form><br>';
  164. }
  165. $typeleaves=$holiday->getTypes(1,1);
  166. if (count($typeleaves) == 0)
  167. {
  168. //print '<div class="info">';
  169. print $langs->trans("NoLeaveWithCounterDefined")."<br>\n";
  170. print $langs->trans("GoIntoDictionaryHolidayTypes");
  171. //print '</div>';
  172. }
  173. else
  174. {
  175. $canedit=0;
  176. if (! empty($user->rights->holiday->define_holiday)) $canedit=1;
  177. // Get array of ids of all childs
  178. $userchilds=array();
  179. if (empty($user->rights->holiday->read_all))
  180. {
  181. $userchilds=$user->getAllChildIds();
  182. }
  183. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">'."\n";
  184. print '<input type="hidden" name="action" value="update" />';
  185. print '<table class="noborder" width="100%;">';
  186. print "<tr class=\"liste_titre\">";
  187. print '<td width="55%">'.$langs->trans('Employee').'</td>';
  188. if (count($typeleaves))
  189. {
  190. foreach($typeleaves as $key => $val)
  191. {
  192. print '<td width="20%" style="text-align:center">'.$val['label'].'</td>';
  193. }
  194. }
  195. else
  196. {
  197. print '<td>'.$langs->trans("NoLeaveWithCounterDefined").'</td>';
  198. }
  199. print '<td width="20%" style="text-align:center">';
  200. if ($canedit) print $langs->trans('Note');
  201. print '</td>';
  202. print '<td></td>';
  203. print '</tr>';
  204. foreach($listUsers as $users)
  205. {
  206. $var=!$var;
  207. // If user has not permission to edit/read all, we must see only subordinates
  208. if (empty($user->rights->holiday->read_all))
  209. {
  210. if (($users['rowid'] != $user->id) && (! in_array($users['rowid'], $userchilds))) continue; // This user is not into hierarchy of current user, we hide it.
  211. }
  212. print '<tr '.$bc[$var].' style="height: 20px;">';
  213. print '<td>';
  214. $userstatic->id=$users['rowid'];
  215. $userstatic->lastname=$users['lastname'];
  216. $userstatic->firstname=$users['firstname'];
  217. $userstatic->gender=$users['gender'];
  218. $userstatic->photo=$users['photo'];
  219. $userstatic->statut=$users['status'];
  220. $userstatic->employee=$users['employee'];
  221. print $userstatic->getNomUrl(-1);
  222. print '</td>';
  223. if (count($typeleaves))
  224. {
  225. foreach($typeleaves as $key => $val)
  226. {
  227. $nbtoshow='';
  228. if ($holiday->getCPforUser($users['rowid'], $val['rowid']) != '') $nbtoshow=price2num($holiday->getCPforUser($users['rowid'], $val['rowid']), 5);
  229. print '<td style="text-align:center">';
  230. if ($canedit) print '<input type="text"'.($canedit?'':' disabled="disabled"').' value="'.$nbtoshow.'" name="nb_holiday_'.$val['rowid'].'['.$users['rowid'].']" size="5" style="text-align: center;"/>';
  231. else print $nbtoshow;
  232. //print ' '.$langs->trans('days');
  233. print '</td>'."\n";
  234. }
  235. }
  236. else
  237. {
  238. print '<td></td>';
  239. }
  240. print '<td style="text-align:center">';
  241. if ($canedit) print '<input type="text"'.($canedit?'':' disabled="disabled"').' value="" name="note_holiday['.$users['rowid'].']" size="30"/>';
  242. print '</td>';
  243. print '<td>';
  244. if (! empty($user->rights->holiday->define_holiday))
  245. {
  246. print '<input type="submit" name="update_cp['.$users['rowid'].']" value="'.dol_escape_htmltag($langs->trans("Update")).'" class="button"/>';
  247. }
  248. print '</td>'."\n";
  249. print '</tr>';
  250. $i++;
  251. }
  252. print '</table>';
  253. print '</form>';
  254. }
  255. llxFooter();
  256. $db->close();