card_group.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. <?php
  2. /* Copyright (C) 2011 Dimitri Mouillard <dmouillard@teclib.com>
  3. * Copyright (C) 2012-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2012-2016 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  7. * Copyright (C) 2014-2017 Ferran Marcet <fmarcet@2byte.es>
  8. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  9. * Copyright (C) 2020-2021 Udo Tamm <dev@dolibit.de>
  10. * Copyright (C) 2022 Anthony Berton <anthony.berton@bb2a.fr>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, orwrite
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/holiday/card.php
  27. * \ingroup holiday
  28. * \brief Form and file creation of paid holiday.
  29. */
  30. // Load Dolibarr environment
  31. require '../main.inc.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/lib/holiday.lib.php';
  40. require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  42. // Get parameters
  43. $action = GETPOST('action', 'aZ09');
  44. $cancel = GETPOST('cancel', 'alpha');
  45. $confirm = GETPOST('confirm', 'alpha');
  46. $id = GETPOST('id', 'int');
  47. $ref = GETPOST('ref', 'alpha');
  48. $fuserid = (GETPOST('fuserid', 'int') ? GETPOST('fuserid', 'int') : $user->id);
  49. $users = (GETPOST('users', 'array') ? GETPOST('users', 'array') : array($user->id));
  50. $groups = GETPOST('groups', 'array');
  51. $socid = GETPOST('socid', 'int');
  52. $autoValidation = GETPOST('autoValidation', 'int');
  53. $AutoSendMail = GETPOST('AutoSendMail', 'int');
  54. // Load translation files required by the page
  55. $langs->loadLangs(array("other", "holiday", "mails", "trips"));
  56. $error = 0;
  57. $now = dol_now();
  58. $childids = $user->getAllChildIds(1);
  59. $morefilter = '';
  60. if (getDolGlobalString('HOLIDAY_HIDE_FOR_NON_SALARIES')) {
  61. $morefilter = 'AND employee = 1';
  62. }
  63. $object = new Holiday($db);
  64. $extrafields = new ExtraFields($db);
  65. // fetch optionals attributes and labels
  66. $extrafields->fetch_name_optionals_label($object->table_element);
  67. if (($id > 0) || $ref) {
  68. $object->fetch($id, $ref);
  69. // Check current user can read this leave request
  70. $canread = 0;
  71. if ($user->hasRight('holiday', 'readall')) {
  72. $canread = 1;
  73. }
  74. if ($user->hasRight('holiday', 'read') && in_array($object->fk_user, $childids)) {
  75. $canread = 1;
  76. }
  77. if (!$canread) {
  78. accessforbidden();
  79. }
  80. }
  81. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  82. $hookmanager->initHooks(array('holidaycard', 'globalcard'));
  83. $cancreate = 0;
  84. $cancreateall = 0;
  85. if ($user->hasRight('holiday', 'write') && in_array($fuserid, $childids)) {
  86. $cancreate = 1;
  87. }
  88. if ($user->hasRight('holiday', 'writeall')) {
  89. $cancreate = 1;
  90. $cancreateall = 1;
  91. }
  92. $candelete = 0;
  93. if ($user->hasRight('holiday', 'delete')) {
  94. $candelete = 1;
  95. }
  96. if ($object->statut == Holiday::STATUS_DRAFT && $user->hasRight('holiday', 'write') && in_array($object->fk_user, $childids)) {
  97. $candelete = 1;
  98. }
  99. // Protection if external user
  100. if ($user->socid) {
  101. $socid = $user->socid;
  102. }
  103. $result = restrictedArea($user, 'holiday', $object->id, 'holiday', '', '', 'rowid', $object->statut);
  104. /*
  105. * Actions
  106. */
  107. $parameters = array('socid' => $socid);
  108. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  109. if ($reshook < 0) {
  110. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  111. }
  112. if (empty($reshook)) {
  113. $backurlforlist = DOL_URL_ROOT.'/holiday/list.php';
  114. if (empty($backtopage) || ($cancel && empty($id))) {
  115. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  116. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  117. $backtopage = $backurlforlist;
  118. } else {
  119. $backtopage = DOL_URL_ROOT.'/holiday/card_group.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
  120. }
  121. }
  122. }
  123. if ($cancel) {
  124. if (!empty($backtopageforcancel)) {
  125. header("Location: ".$backtopageforcancel);
  126. exit;
  127. } elseif (!empty($backtopage)) {
  128. header("Location: ".$backtopage);
  129. exit;
  130. }
  131. $action = '';
  132. }
  133. // Add leave request
  134. if ($action == 'add') {
  135. // If no right to create a request
  136. if (!$cancreate) {
  137. $error++;
  138. setEventMessages($langs->trans('CantCreateCP'), null, 'errors');
  139. $action = 'create';
  140. }
  141. if (!$error) {
  142. $users = GETPOST('users', 'array');
  143. $groups = GETPOST('groups', 'array');
  144. $date_debut = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year'));
  145. $date_fin = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year'));
  146. $date_debut_gmt = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year'), 1);
  147. $date_fin_gmt = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year'), 1);
  148. $starthalfday = GETPOST('starthalfday');
  149. $endhalfday = GETPOST('endhalfday');
  150. $type = GETPOST('type');
  151. $halfday = 0;
  152. if ($starthalfday == 'afternoon' && $endhalfday == 'morning') {
  153. $halfday = 2;
  154. } elseif ($starthalfday == 'afternoon') {
  155. $halfday = -1;
  156. } elseif ($endhalfday == 'morning') {
  157. $halfday = 1;
  158. }
  159. $approverid = GETPOST('valideur', 'int');
  160. $description = trim(GETPOST('description', 'restricthtml'));
  161. // Check that leave is for a user inside the hierarchy or advanced permission for all is set
  162. if (!$cancreateall) {
  163. if (!getDolGlobalString('MAIN_USE_ADVANCED_PERMS')) {
  164. if (!$user->hasRight('holiday', 'write')) {
  165. $error++;
  166. setEventMessages($langs->trans("NotEnoughPermissions"), null, 'errors');
  167. } elseif (!in_array($fuserid, $childids)) {
  168. $error++;
  169. setEventMessages($langs->trans("UserNotInHierachy"), null, 'errors');
  170. $action = 'create';
  171. }
  172. } else {
  173. if (!$user->hasRight('holiday', 'write') && !$user->hasRight('holiday', 'writeall_advance')) {
  174. $error++;
  175. setEventMessages($langs->trans("NotEnoughPermissions"), null, 'errors');
  176. } elseif (!$user->hasRight('holiday', 'writeall_advance') && !in_array($fuserid, $childids)) {
  177. $error++;
  178. setEventMessages($langs->trans("UserNotInHierachy"), null, 'errors');
  179. $action = 'create';
  180. }
  181. }
  182. }
  183. // If no groups and no users
  184. if (empty($groups) && empty($users)) {
  185. setEventMessages($langs->trans("ErrorFieldRequiredUserOrGroup"), null, 'errors');
  186. //setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("UserOrGroup")), null, 'errors');
  187. //setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Group")), null, 'errors');
  188. $error++;
  189. $action = 'create';
  190. }
  191. // If no type
  192. if ($type <= 0) {
  193. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
  194. $error++;
  195. $action = 'create';
  196. }
  197. // If no start date
  198. if (empty($date_debut)) {
  199. setEventMessages($langs->trans("NoDateDebut"), null, 'errors');
  200. $error++;
  201. $action = 'create';
  202. }
  203. // If no end date
  204. if (empty($date_fin)) {
  205. setEventMessages($langs->trans("NoDateFin"), null, 'errors');
  206. $error++;
  207. $action = 'create';
  208. }
  209. // If start date after end date
  210. if ($date_debut > $date_fin) {
  211. setEventMessages($langs->trans("ErrorEndDateCP"), null, 'errors');
  212. $error++;
  213. $action = 'create';
  214. }
  215. // If there is no Business Days within request
  216. $nbopenedday = num_open_day($date_debut_gmt, $date_fin_gmt, 0, 1, $halfday);
  217. if ($nbopenedday < 0.5) {
  218. setEventMessages($langs->trans("ErrorDureeCP"), null, 'errors'); // No working day
  219. $error++;
  220. $action = 'create';
  221. }
  222. // If no validator designated
  223. if ($approverid < 1) {
  224. setEventMessages($langs->transnoentitiesnoconv('InvalidValidatorCP'), null, 'errors');
  225. $error++;
  226. }
  227. $result = 0;
  228. if (!$error) {
  229. $TusersToProcess = array();
  230. // usergroup select
  231. // better perf on single sql
  232. /** GROUPS */
  233. $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from ' . MAIN_DB_PREFIX . 'user as u';
  234. $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'usergroup_user as ug on ug.fk_user = u.rowid ';
  235. $sql .= ' WHERE fk_usergroup in (' .$db->sanitize(implode(',', $groups)) . ')';
  236. $resql = $db->query($sql);
  237. if ($resql) {
  238. while ($obj = $db->fetch_object($resql)) {
  239. $TusersToProcess[$obj->rowid] = $obj->rowid;
  240. }
  241. }
  242. /** USERS */
  243. if (is_array($users) && count($users) > 0) {
  244. foreach ($users as $u) {
  245. $TusersToProcess[$u] = $u;
  246. }
  247. }
  248. foreach ($TusersToProcess as $u) {
  249. // Check if there is already holiday for this period pour chaque user
  250. $verifCP = $object->verifDateHolidayCP($u, $date_debut, $date_fin, $halfday);
  251. if (!$verifCP) {
  252. //setEventMessages($langs->trans("alreadyCPexist"), null, 'errors');
  253. $userError = new User($db);
  254. $result = $userError->fetch($u);
  255. if ($result) {
  256. setEventMessages($langs->trans("UseralreadyCPexist", $userError->firstname . ' '. $userError->lastname), null, 'errors');
  257. } else {
  258. setEventMessages($langs->trans("ErrorUserFetch", $u), null, 'errors');
  259. }
  260. $error++;
  261. $action = 'create';
  262. }
  263. }
  264. if (!$error) {
  265. $db->begin();
  266. // non errors we can insert all
  267. foreach ($TusersToProcess as $u) {
  268. $object = new Holiday($db);
  269. $object->fk_user = $u;
  270. $object->description = $description;
  271. $object->fk_validator = $approverid;
  272. $object->fk_type = $type;
  273. $object->date_debut = $date_debut;
  274. $object->date_fin = $date_fin;
  275. $object->halfday = $halfday;
  276. $result = $object->create($user);
  277. if ($result <= 0) {
  278. setEventMessages($object->error, $object->errors, 'errors');
  279. $error++;
  280. } else {
  281. //@TODO changer le nom si validated
  282. if ($autoValidation) {
  283. $htemp = new Holiday($db);
  284. $htemp->fetch($result);
  285. $htemp->statut = Holiday::STATUS_VALIDATED;
  286. $resultValidated = $htemp->update($approverid);
  287. if ($resultValidated < 0) {
  288. setEventMessages($object->error, $object->errors, 'errors');
  289. $error++;
  290. }
  291. // we can auto send mail if we are in auto validation behavior
  292. if ($AutoSendMail && !$error) {
  293. // send a mail to the user
  294. $returnSendMail = sendMail($result, $cancreate, $now, $autoValidation);
  295. if (!empty($returnSendMail->msg)) {
  296. setEventMessage($returnSendMail->msg, $returnSendMail->style);
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303. // If no SQL error we redirect to the request card
  304. if (!$error) {
  305. $db->commit();
  306. header('Location: '.DOL_URL_ROOT.'/holiday/list.php');
  307. exit;
  308. } else {
  309. $db->rollback();
  310. }
  311. }
  312. }
  313. }
  314. }
  315. /*
  316. * View
  317. */
  318. $form = new Form($db);
  319. $object = new Holiday($db);
  320. $listhalfday = array('morning'=>$langs->trans("Morning"), "afternoon"=>$langs->trans("Afternoon"));
  321. $title = $langs->trans('Leave');
  322. $help_url = 'EN:Module_Holiday';
  323. llxHeader('', $title, $help_url);
  324. if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') {
  325. // If user has no permission to create a leave
  326. if ((in_array($fuserid, $childids) && !$user->hasRight('holiday', 'writeall')) || (!in_array($fuserid, $childids) && (!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') || !$user->hasRight('holiday', 'writeall_advance')))) {
  327. $errors[] = $langs->trans('CantCreateCP');
  328. } else {
  329. // Form to add a leave request
  330. print load_fiche_titre($langs->trans('MenuCollectiveAddCP'), '', 'title_hrm.png');
  331. // Error management
  332. if (GETPOST('error')) {
  333. switch (GETPOST('error')) {
  334. case 'datefin':
  335. $errors[] = $langs->trans('ErrorEndDateCP');
  336. break;
  337. case 'SQL_Create':
  338. $errors[] = $langs->trans('ErrorSQLCreateCP');
  339. break;
  340. case 'CantCreate':
  341. $errors[] = $langs->trans('CantCreateCP');
  342. break;
  343. case 'Valideur':
  344. $errors[] = $langs->trans('InvalidValidatorCP');
  345. break;
  346. case 'nodatedebut':
  347. $errors[] = $langs->trans('NoDateDebut');
  348. break;
  349. case 'nodatefin':
  350. $errors[] = $langs->trans('NoDateFin');
  351. break;
  352. case 'DureeHoliday':
  353. $errors[] = $langs->trans('ErrorDureeCP');
  354. break;
  355. case 'alreadyCP':
  356. $errors[] = $langs->trans('alreadyCPexist');
  357. break;
  358. }
  359. setEventMessages($errors, null, 'errors');
  360. }
  361. print '<script type="text/javascript">
  362. $( document ).ready(function() {
  363. if( $("input[name=autoValidation]").is(":checked") ){
  364. $("#AutoSendMail").prop("disabled", false);
  365. $("#AutoSendMail").prop("checked", true);
  366. } else {
  367. $("#AutoSendMail").prop("disabled", true);
  368. $("#AutoSendMail").prop("checked", false);
  369. }
  370. $("input[name=autoValidation]").click( function(e) {
  371. if( $("input[name=autoValidation]").is(":checked") ){
  372. $("#AutoSendMail").prop("disabled", false);
  373. $("#AutoSendMail").prop("checked", true);
  374. } else {
  375. $("#AutoSendMail").prop("disabled", true);
  376. $("#AutoSendMail").prop("checked", false);
  377. }
  378. });
  379. $("input.button-save").click("submit", function(e) {
  380. console.log("Call valider()");
  381. if (document.demandeCP.date_debut_.value != "")
  382. {
  383. if(document.demandeCP.date_fin_.value != "")
  384. {
  385. if(document.demandeCP.valideur.value != "-1") {
  386. return true;
  387. }
  388. else {
  389. alert("'.dol_escape_js($langs->transnoentities('InvalidValidatorCP')).'");
  390. return false;
  391. }
  392. }
  393. else
  394. {
  395. alert("'.dol_escape_js($langs->transnoentities('NoDateFin')).'");
  396. return false;
  397. }
  398. }
  399. else
  400. {
  401. alert("'.dol_escape_js($langs->transnoentities('NoDateDebut')).'");
  402. return false;
  403. }
  404. })
  405. $("#autoValidation").change(function(){
  406. if( $("input[name=autoValidation]").is(":checked") ){
  407. $("#AutoSendMail").prop("disabled", false);
  408. } else {
  409. $("#AutoSendMail").prop("disabled", true);
  410. $("#AutoSendMail").prop("checked", false);
  411. }
  412. })
  413. });
  414. </script>'."\n";
  415. // Formulaire de demande
  416. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" name="demandeCP">'."\n";
  417. print '<input type="hidden" name="token" value="'.newToken().'" />'."\n";
  418. print '<input type="hidden" name="action" value="add" />'."\n";
  419. print dol_get_fiche_head();
  420. print '<table class="border centpercent">';
  421. print '<tbody>';
  422. // Groups of users
  423. print '<tr>';
  424. print '<td class="titlefield fieldrequired">';
  425. print $form->textwithpicto($langs->trans("groups"), $langs->trans("fusionGroupsUsers"));
  426. print '</td>';
  427. print '<td>';
  428. print img_picto($langs->trans("groups"), 'group', 'class="pictofixedwidth"');
  429. $sql =' SELECT rowid, nom from '.MAIN_DB_PREFIX.'usergroup WHERE entity IN ('.getEntity('usergroup').')';
  430. $resql = $db->query($sql);
  431. $Tgroup = array();
  432. while ($obj = $db->fetch_object($resql)) {
  433. $Tgroup[$obj->rowid] = $obj->nom;
  434. }
  435. print $form->multiselectarray('groups', $Tgroup, GETPOST('groups', 'array'), '', 0, 'quatrevingtpercent widthcentpercentminusx', 0, 0);
  436. print '</td>';
  437. // Users
  438. print '<tr>';
  439. print '<td class="titlefield fieldrequired">';
  440. print $form->textwithpicto($langs->trans("users"), $langs->trans("fusionGroupsUsers"));
  441. print '<td>';
  442. print img_picto($langs->trans("users"), 'user', 'class="pictofixedwidth"');
  443. $sql = ' SELECT u.rowid, u.lastname, u.firstname from '.MAIN_DB_PREFIX.'user as u';
  444. $sql .= ' WHERE 1=1';
  445. $sql .= !empty($morefilter) ? $morefilter : '';
  446. $resql = $db->query($sql);
  447. if ($resql) {
  448. while ($obj = $db->fetch_object($resql)) {
  449. $userlist[$obj->rowid] = dolGetFirstLastname($obj->firstname, $obj->lastname);
  450. }
  451. }
  452. print img_picto('', 'users') . $form->multiselectarray('users', $userlist, GETPOST('users', 'array'), '', 0, 'quatrevingtpercent widthcentpercentminusx', 0, 0);
  453. print '</td>';
  454. // Type
  455. print '<tr>';
  456. print '<td class="fieldrequired">'.$langs->trans("Type").'</td>';
  457. print '<td>';
  458. $typeleaves = $object->getTypes(1, -1);
  459. $arraytypeleaves = array();
  460. foreach ($typeleaves as $key => $val) {
  461. $labeltoshow = ($langs->trans($val['code']) != $val['code'] ? $langs->trans($val['code']) : $val['label']);
  462. $labeltoshow .= ($val['delay'] > 0 ? ' ('.$langs->trans("NoticePeriod").': '.$val['delay'].' '.$langs->trans("days").')' : '');
  463. $arraytypeleaves[$val['rowid']] = $labeltoshow;
  464. }
  465. print $form->selectarray('type', $arraytypeleaves, (GETPOST('type', 'alpha') ? GETPOST('type', 'alpha') : ''), 1, 0, 0, '', 0, 0, 0, '', '', true);
  466. if ($user->admin) {
  467. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  468. }
  469. print '</td>';
  470. print '</tr>';
  471. // Date start
  472. print '<tr>';
  473. print '<td class="fieldrequired">';
  474. print $form->textwithpicto($langs->trans("DateDebCP"), $langs->trans("FirstDayOfHoliday"));
  475. print '</td>';
  476. print '<td>';
  477. // Si la demande ne vient pas de l'agenda
  478. if (!GETPOST('date_debut_')) {
  479. print $form->selectDate(-1, 'date_debut_', 0, 0, 0, '', 1, 1);
  480. } else {
  481. $tmpdate = dol_mktime(0, 0, 0, GETPOST('date_debut_month', 'int'), GETPOST('date_debut_day', 'int'), GETPOST('date_debut_year', 'int'));
  482. print $form->selectDate($tmpdate, 'date_debut_', 0, 0, 0, '', 1, 1);
  483. }
  484. print ' &nbsp; &nbsp; ';
  485. print $form->selectarray('starthalfday', $listhalfday, (GETPOST('starthalfday', 'alpha') ? GETPOST('starthalfday', 'alpha') : 'morning'));
  486. print '</td>';
  487. print '</tr>';
  488. // Date end
  489. print '<tr>';
  490. print '<td class="fieldrequired">';
  491. print $form->textwithpicto($langs->trans("DateFinCP"), $langs->trans("LastDayOfHoliday"));
  492. print '</td>';
  493. print '<td>';
  494. if (!GETPOST('date_fin_')) {
  495. print $form->selectDate(-1, 'date_fin_', 0, 0, 0, '', 1, 1);
  496. } else {
  497. $tmpdate = dol_mktime(0, 0, 0, GETPOST('date_fin_month', 'int'), GETPOST('date_fin_day', 'int'), GETPOST('date_fin_year', 'int'));
  498. print $form->selectDate($tmpdate, 'date_fin_', 0, 0, 0, '', 1, 1);
  499. }
  500. print ' &nbsp; &nbsp; ';
  501. print $form->selectarray('endhalfday', $listhalfday, (GETPOST('endhalfday', 'alpha') ? GETPOST('endhalfday', 'alpha') : 'afternoon'));
  502. print '</td>';
  503. print '</tr>';
  504. // Approver
  505. print '<tr>';
  506. print '<td class="fieldrequired">'.$langs->trans("ReviewedByCP").'</td>';
  507. print '<td>';
  508. $object = new Holiday($db);
  509. $include_users = $object->fetch_users_approver_holiday();
  510. if (empty($include_users)) {
  511. print img_warning().' '.$langs->trans("NobodyHasPermissionToValidateHolidays");
  512. } else {
  513. // Defined default approver (the forced approved of user or the supervisor if no forced value defined)
  514. // Note: This use will be set only if the deinfed approvr has permission to approve so is inside include_users
  515. $defaultselectuser = (empty($user->fk_user_holiday_validator) ? $user->fk_user : $user->fk_user_holiday_validator);
  516. if (getDolGlobalString('HOLIDAY_DEFAULT_VALIDATOR')) {
  517. $defaultselectuser = $conf->global->HOLIDAY_DEFAULT_VALIDATOR; // Can force default approver
  518. }
  519. if (GETPOST('valideur', 'int') > 0) {
  520. $defaultselectuser = GETPOST('valideur', 'int');
  521. }
  522. $s = $form->select_dolusers($defaultselectuser, "valideur", 1, '', 0, $include_users, '', '0,'.$conf->entity, 0, 0, '', 0, '', 'minwidth200 maxwidth500');
  523. print img_picto('', 'user').$form->textwithpicto($s, $langs->trans("AnyOtherInThisListCanValidate"));
  524. }
  525. print '</td>';
  526. print '</tr>';
  527. //auto validation ON CREATE
  528. print '<tr><td>'.$langs->trans("AutoValidationOnCreate").'</td><td>';
  529. print '<input type="checkbox" id="autoValidation" name="autoValidation" value="1"'.($autoValidation ? ' checked="checked"' : '').'>';
  530. print '</td></tr>'."\n";
  531. //no auto SEND MAIL
  532. print '<tr><td>'.$langs->trans("AutoSendMail").'</td><td>';
  533. print '<input type="checkbox" id="AutoSendMail" name="AutoSendMail" value="1"'.($AutoSendMail ? ' checked="checked"' : '').'>';
  534. print '</td></tr>'."\n";
  535. // Description
  536. print '<tr>';
  537. print '<td>'.$langs->trans("DescCP").'</td>';
  538. print '<td class="tdtop">';
  539. $doleditor = new DolEditor('description', GETPOST('description', 'restricthtml'), '', 80, 'dolibarr_notes', 'In', 0, false, isModEnabled('fckeditor'), ROWS_3, '90%');
  540. print $doleditor->Create(1);
  541. print '</td></tr>';
  542. // Other attributes
  543. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
  544. print '</tbody>';
  545. print '</table>';
  546. print dol_get_fiche_end();
  547. print $form->buttonsSaveCancel("SendRequestCollectiveCP");
  548. print '</from>'."\n";
  549. }
  550. } else {
  551. if ($error) {
  552. print '<div class="tabBar">';
  553. print $error;
  554. print '<br><br><input type="button" value="'.$langs->trans("ReturnCP").'" class="button" onclick="history.go(-1)" />';
  555. print '</div>';
  556. }
  557. }
  558. // End of page
  559. llxFooter();
  560. if (is_object($db)) {
  561. $db->close();
  562. }
  563. /**
  564. * send email to validator for current leave represented by (id)
  565. *
  566. * @param int $id validator for current leave represented by (id)
  567. * @param int $cancreate flag for user right
  568. * @param int $now date
  569. * @param int $autoValidation boolean flag on autovalidation
  570. *
  571. * @return stdClass
  572. * @throws Exception
  573. */
  574. function sendMail($id, $cancreate, $now, $autoValidation)
  575. {
  576. $objStd = new stdClass();
  577. $objStd->msg = '';
  578. $objStd->status = 'success';
  579. $objStd->error = 0;
  580. $objStd->style = '';
  581. global $db, $user, $conf, $langs;
  582. $object = new Holiday($db);
  583. $result = $object->fetch($id);
  584. if ($result) {
  585. // If draft and owner of leave
  586. if ($object->statut == Holiday::STATUS_VALIDATED && $cancreate) {
  587. $object->oldcopy = dol_clone($object, 2);
  588. //if ($autoValidation) $object->statut = Holiday::STATUS_VALIDATED;
  589. $verif = $object->validate($user);
  590. if ($verif > 0) {
  591. // To
  592. $destinataire = new User($db);
  593. $destinataire->fetch($object->fk_validator);
  594. $emailTo = $destinataire->email;
  595. if (!$emailTo) {
  596. dol_syslog("Expected validator has no email, so we redirect directly to finished page without sending email");
  597. $objStd->error++;
  598. $objStd->msg = $langs->trans('ErroremailTo');
  599. $objStd->status = 'error';
  600. $objStd->style="warnings";
  601. return $objStd;
  602. }
  603. // From
  604. $expediteur = new User($db);
  605. $expediteur->fetch($object->fk_user);
  606. //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email.
  607. $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM;
  608. // Subject
  609. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  610. if (getDolGlobalString('MAIN_APPLICATION_TITLE')) {
  611. $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  612. }
  613. $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysToValidate");
  614. // Content
  615. $message = "<p>".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",</p>\n";
  616. $message .= "<p>".$langs->transnoentities("HolidaysToValidateBody")."</p>\n";
  617. // option to warn the validator in case of too short delay
  618. if (!getDolGlobalString('HOLIDAY_HIDE_APPROVER_ABOUT_TOO_LOW_DELAY')) {
  619. $delayForRequest = 0; // TODO Set delay depending of holiday leave type
  620. if ($delayForRequest) {
  621. $nowplusdelay = dol_time_plus_duree($now, $delayForRequest, 'd');
  622. if ($object->date_debut < $nowplusdelay) {
  623. $message = "<p>".$langs->transnoentities("HolidaysToValidateDelay", $delayForRequest)."</p>\n";
  624. }
  625. }
  626. }
  627. // option to notify the validator if the balance is less than the request
  628. if (!getDolGlobalString('HOLIDAY_HIDE_APPROVER_ABOUT_NEGATIVE_BALANCE')) {
  629. $nbopenedday = num_open_day($object->date_debut_gmt, $object->date_fin_gmt, 0, 1, $object->halfday);
  630. if ($nbopenedday > $object->getCPforUser($object->fk_user, $object->fk_type)) {
  631. $message .= "<p>".$langs->transnoentities("HolidaysToValidateAlertSolde")."</p>\n";
  632. }
  633. }
  634. $typeleaves = $object->getTypes(1, -1);
  635. $labeltoshow = (($typeleaves[$object->fk_type]['code'] && $langs->trans($typeleaves[$object->fk_type]['code']) != $typeleaves[$object->fk_type]['code']) ? $langs->trans($typeleaves[$object->fk_type]['code']) : $typeleaves[$object->fk_type]['label']);
  636. if ($object->halfday == 2) {
  637. $starthalfdaykey = "Afternoon";
  638. $endhalfdaykey = "Morning";
  639. } elseif ($object->halfday == -1) {
  640. $starthalfdaykey = "Afternoon";
  641. $endhalfdaykey = "Afternoon";
  642. } elseif ($object->halfday == 1) {
  643. $starthalfdaykey = "Morning";
  644. $endhalfdaykey = "Morning";
  645. } elseif ($object->halfday == 0 || $object->halfday == 2) {
  646. $starthalfdaykey = "Morning";
  647. $endhalfdaykey = "Afternoon";
  648. }
  649. $link = dol_buildpath("/holiday/card.php", 3) . '?id='.$object->id;
  650. $message .= "<ul>";
  651. $message .= "<li>".$langs->transnoentitiesnoconv("Name")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."</li>\n";
  652. $message .= "<li>".$langs->transnoentitiesnoconv("Type")." : ".(empty($labeltoshow) ? $langs->trans("TypeWasDisabledOrRemoved", $object->fk_type) : $labeltoshow)."</li>\n";
  653. $message .= "<li>".$langs->transnoentitiesnoconv("Period")." : ".dol_print_date($object->date_debut, 'day')." ".$langs->transnoentitiesnoconv($starthalfdaykey)." ".$langs->transnoentitiesnoconv("To")." ".dol_print_date($object->date_fin, 'day')." ".$langs->transnoentitiesnoconv($endhalfdaykey)."</li>\n";
  654. $message .= "<li>".$langs->transnoentitiesnoconv("Link").' : <a href="'.$link.'" target="_blank">'.$link."</a></li>\n";
  655. $message .= "</ul>\n";
  656. $trackid = 'leav'.$object->id;
  657. $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid);
  658. // Sending the email
  659. $result = $mail->sendfile();
  660. if (!$result) {
  661. $objStd->error++;
  662. $objStd->msg = $langs->trans('ErroreSendmail');
  663. $objStd->style="warnings";
  664. $objStd->status = 'error';
  665. } else {
  666. $objStd->msg = $langs->trans('mailSended');
  667. }
  668. return $objStd;
  669. } else {
  670. $objStd->error++;
  671. $objStd->msg = $langs->trans('ErroreVerif');
  672. $objStd->status = 'error';
  673. $objStd->style="errors";
  674. return $objStd;
  675. }
  676. }
  677. } else {
  678. $objStd->error++;
  679. $objStd->msg = $langs->trans('ErrorloadUserOnSendingMail');
  680. $objStd->status = 'error';
  681. $objStd->style="warnings";
  682. return $objStd;
  683. }
  684. return $objStd;
  685. }