card_group.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 (!empty($conf->global->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 (!empty($user->rights->holiday->readall)) {
  72. $canread = 1;
  73. }
  74. if (!empty($user->rights->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 (!empty($user->rights->holiday->write) && in_array($fuserid, $childids)) {
  86. $cancreate = 1;
  87. }
  88. if (!empty($user->rights->holiday->writeall)) {
  89. $cancreate = 1;
  90. $cancreateall = 1;
  91. }
  92. $candelete = 0;
  93. if (!empty($user->rights->holiday->delete)) {
  94. $candelete = 1;
  95. }
  96. if ($object->statut == Holiday::STATUS_DRAFT && $user->rights->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 (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) {
  164. if (empty($user->rights->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 (empty($user->rights->holiday->write) && empty($user->rights->holiday->writeall_advance)) {
  174. $error++;
  175. setEventMessages($langs->trans("NotEnoughPermissions"), null, 'errors');
  176. } elseif (empty($user->rights->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)) setEventMessage($returnSendMail->msg, $returnSendMail->style);
  296. }
  297. }
  298. }
  299. }
  300. }
  301. // If no SQL error we redirect to the request card
  302. if (!$error) {
  303. $db->commit();
  304. header('Location: '.DOL_URL_ROOT.'/holiday/list.php');
  305. exit;
  306. } else {
  307. $db->rollback();
  308. }
  309. }
  310. }
  311. }
  312. }
  313. /*
  314. * View
  315. */
  316. $form = new Form($db);
  317. $object = new Holiday($db);
  318. $listhalfday = array('morning'=>$langs->trans("Morning"), "afternoon"=>$langs->trans("Afternoon"));
  319. $title = $langs->trans('Leave');
  320. $help_url = 'EN:Module_Holiday';
  321. llxHeader('', $title, $help_url);
  322. if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') {
  323. // If user has no permission to create a leave
  324. if ((in_array($fuserid, $childids) && empty($user->rights->holiday->writeall)) || (!in_array($fuserid, $childids) && (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || empty($user->rights->holiday->writeall_advance)))) {
  325. $errors[] = $langs->trans('CantCreateCP');
  326. } else {
  327. // Form to add a leave request
  328. print load_fiche_titre($langs->trans('MenuCollectiveAddCP'), '', 'title_hrm.png');
  329. // Error management
  330. if (GETPOST('error')) {
  331. switch (GETPOST('error')) {
  332. case 'datefin':
  333. $errors[] = $langs->trans('ErrorEndDateCP');
  334. break;
  335. case 'SQL_Create':
  336. $errors[] = $langs->trans('ErrorSQLCreateCP');
  337. break;
  338. case 'CantCreate':
  339. $errors[] = $langs->trans('CantCreateCP');
  340. break;
  341. case 'Valideur':
  342. $errors[] = $langs->trans('InvalidValidatorCP');
  343. break;
  344. case 'nodatedebut':
  345. $errors[] = $langs->trans('NoDateDebut');
  346. break;
  347. case 'nodatefin':
  348. $errors[] = $langs->trans('NoDateFin');
  349. break;
  350. case 'DureeHoliday':
  351. $errors[] = $langs->trans('ErrorDureeCP');
  352. break;
  353. case 'alreadyCP':
  354. $errors[] = $langs->trans('alreadyCPexist');
  355. break;
  356. }
  357. setEventMessages($errors, null, 'errors');
  358. }
  359. print '<script type="text/javascript">
  360. $( document ).ready(function() {
  361. if( $("input[name=autoValidation]").is(":checked") ){
  362. $("#AutoSendMail").prop("disabled", false);
  363. $("#AutoSendMail").prop("checked", true);
  364. } else {
  365. $("#AutoSendMail").prop("disabled", true);
  366. $("#AutoSendMail").prop("checked", false);
  367. }
  368. $("input[name=autoValidation]").click( function(e) {
  369. if( $("input[name=autoValidation]").is(":checked") ){
  370. $("#AutoSendMail").prop("disabled", false);
  371. $("#AutoSendMail").prop("checked", true);
  372. } else {
  373. $("#AutoSendMail").prop("disabled", true);
  374. $("#AutoSendMail").prop("checked", false);
  375. }
  376. });
  377. $("input.button-save").click("submit", function(e) {
  378. console.log("Call valider()");
  379. if (document.demandeCP.date_debut_.value != "")
  380. {
  381. if(document.demandeCP.date_fin_.value != "")
  382. {
  383. if(document.demandeCP.valideur.value != "-1") {
  384. return true;
  385. }
  386. else {
  387. alert("'.dol_escape_js($langs->transnoentities('InvalidValidatorCP')).'");
  388. return false;
  389. }
  390. }
  391. else
  392. {
  393. alert("'.dol_escape_js($langs->transnoentities('NoDateFin')).'");
  394. return false;
  395. }
  396. }
  397. else
  398. {
  399. alert("'.dol_escape_js($langs->transnoentities('NoDateDebut')).'");
  400. return false;
  401. }
  402. })
  403. $("#autoValidation").change(function(){
  404. if( $("input[name=autoValidation]").is(":checked") ){
  405. $("#AutoSendMail").prop("disabled", false);
  406. } else {
  407. $("#AutoSendMail").prop("disabled", true);
  408. $("#AutoSendMail").prop("checked", false);
  409. }
  410. })
  411. });
  412. </script>'."\n";
  413. // Formulaire de demande
  414. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" name="demandeCP">'."\n";
  415. print '<input type="hidden" name="token" value="'.newToken().'" />'."\n";
  416. print '<input type="hidden" name="action" value="add" />'."\n";
  417. print dol_get_fiche_head();
  418. print '<table class="border centpercent">';
  419. print '<tbody>';
  420. // groupe
  421. print '<tr>';
  422. print '<td class="titlefield fieldrequired">';
  423. print $form->textwithpicto($langs->trans("groups"), $langs->trans("fusionGroupsUsers"));
  424. print '</td>';
  425. print '<td>';
  426. //@todo ajouter entity !
  427. $sql =' SELECT rowid, nom from '.MAIN_DB_PREFIX.'usergroup ';
  428. $resql = $db->query($sql);
  429. $Tgroup = array();
  430. while ($obj = $db->fetch_object($resql)) {
  431. $Tgroup[$obj->rowid] = $obj->nom;
  432. }
  433. print $form->multiselectarray('groups', $Tgroup, GETPOST('groups', 'array'), '', 0, 'quatrevingtpercent widthcentpercentminusx', 0, 0);
  434. print '</td>';
  435. // users
  436. print '<tr>';
  437. print '<td class="titlefield fieldrequired">';
  438. print $form->textwithpicto($langs->trans("users"), $langs->trans("fusionGroupsUsers"));
  439. print '<td>';
  440. $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from '.MAIN_DB_PREFIX.'user as u';
  441. $sql .= ' WHERE 1=1 ';
  442. $sql .= !empty($morefilter) ? $morefilter : '';
  443. $resql = $db->query($sql);
  444. if ($resql) {
  445. while ($obj = $db->fetch_object($resql)) {
  446. $userlist[$obj->rowid] = $obj->firstname . ' '. $obj->lastname;
  447. }
  448. }
  449. print img_picto('', 'users') . $form->multiselectarray('users', $userlist, GETPOST('users', 'array'), '', 0, 'quatrevingtpercent widthcentpercentminusx', 0, 0);
  450. print '</td>';
  451. // Type
  452. print '<tr>';
  453. print '<td class="fieldrequired">'.$langs->trans("Type").'</td>';
  454. print '<td>';
  455. $typeleaves = $object->getTypes(1, -1);
  456. $arraytypeleaves = array();
  457. foreach ($typeleaves as $key => $val) {
  458. $labeltoshow = ($langs->trans($val['code']) != $val['code'] ? $langs->trans($val['code']) : $val['label']);
  459. $labeltoshow .= ($val['delay'] > 0 ? ' ('.$langs->trans("NoticePeriod").': '.$val['delay'].' '.$langs->trans("days").')' : '');
  460. $arraytypeleaves[$val['rowid']] = $labeltoshow;
  461. }
  462. print $form->selectarray('type', $arraytypeleaves, (GETPOST('type', 'alpha') ?GETPOST('type', 'alpha') : ''), 1, 0, 0, '', 0, 0, 0, '', '', true);
  463. if ($user->admin) {
  464. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  465. }
  466. print '</td>';
  467. print '</tr>';
  468. // Date start
  469. print '<tr>';
  470. print '<td class="fieldrequired">';
  471. print $form->textwithpicto($langs->trans("DateDebCP"), $langs->trans("FirstDayOfHoliday"));
  472. print '</td>';
  473. print '<td>';
  474. // Si la demande ne vient pas de l'agenda
  475. if (!GETPOST('date_debut_')) {
  476. print $form->selectDate(-1, 'date_debut_', 0, 0, 0, '', 1, 1);
  477. } else {
  478. $tmpdate = dol_mktime(0, 0, 0, GETPOST('date_debut_month', 'int'), GETPOST('date_debut_day', 'int'), GETPOST('date_debut_year', 'int'));
  479. print $form->selectDate($tmpdate, 'date_debut_', 0, 0, 0, '', 1, 1);
  480. }
  481. print ' &nbsp; &nbsp; ';
  482. print $form->selectarray('starthalfday', $listhalfday, (GETPOST('starthalfday', 'alpha') ?GETPOST('starthalfday', 'alpha') : 'morning'));
  483. print '</td>';
  484. print '</tr>';
  485. // Date end
  486. print '<tr>';
  487. print '<td class="fieldrequired">';
  488. print $form->textwithpicto($langs->trans("DateFinCP"), $langs->trans("LastDayOfHoliday"));
  489. print '</td>';
  490. print '<td>';
  491. if (!GETPOST('date_fin_')) {
  492. print $form->selectDate(-1, 'date_fin_', 0, 0, 0, '', 1, 1);
  493. } else {
  494. $tmpdate = dol_mktime(0, 0, 0, GETPOST('date_fin_month', 'int'), GETPOST('date_fin_day', 'int'), GETPOST('date_fin_year', 'int'));
  495. print $form->selectDate($tmpdate, 'date_fin_', 0, 0, 0, '', 1, 1);
  496. }
  497. print ' &nbsp; &nbsp; ';
  498. print $form->selectarray('endhalfday', $listhalfday, (GETPOST('endhalfday', 'alpha') ?GETPOST('endhalfday', 'alpha') : 'afternoon'));
  499. print '</td>';
  500. print '</tr>';
  501. // Approver
  502. print '<tr>';
  503. print '<td class="fieldrequired">'.$langs->trans("ReviewedByCP").'</td>';
  504. print '<td>';
  505. $object = new Holiday($db);
  506. $include_users = $object->fetch_users_approver_holiday();
  507. if (empty($include_users)) {
  508. print img_warning().' '.$langs->trans("NobodyHasPermissionToValidateHolidays");
  509. } else {
  510. // Defined default approver (the forced approved of user or the supervisor if no forced value defined)
  511. // Note: This use will be set only if the deinfed approvr has permission to approve so is inside include_users
  512. $defaultselectuser = (empty($user->fk_user_holiday_validator) ? $user->fk_user : $user->fk_user_holiday_validator);
  513. if (!empty($conf->global->HOLIDAY_DEFAULT_VALIDATOR)) {
  514. $defaultselectuser = $conf->global->HOLIDAY_DEFAULT_VALIDATOR; // Can force default approver
  515. }
  516. if (GETPOST('valideur', 'int') > 0) {
  517. $defaultselectuser = GETPOST('valideur', 'int');
  518. }
  519. $s = $form->select_dolusers($defaultselectuser, "valideur", 1, '', 0, $include_users, '', '0,'.$conf->entity, 0, 0, '', 0, '', 'minwidth200 maxwidth500');
  520. print img_picto('', 'user').$form->textwithpicto($s, $langs->trans("AnyOtherInThisListCanValidate"));
  521. }
  522. print '</td>';
  523. print '</tr>';
  524. //auto validation ON CREATE
  525. print '<tr><td>'.$langs->trans("AutoValidationOnCreate").'</td><td>';
  526. print '<input type="checkbox" id="autoValidation" name="autoValidation" value="1"'.($autoValidation ? ' checked="checked"' : '').'>';
  527. print '</td></tr>'."\n";
  528. //no auto SEND MAIL
  529. print '<tr><td>'.$langs->trans("AutoSendMail").'</td><td>';
  530. print '<input type="checkbox" id="AutoSendMail" name="AutoSendMail" value="1"'.($AutoSendMail ? ' checked="checked"' : '').'>';
  531. print '</td></tr>'."\n";
  532. // Description
  533. print '<tr>';
  534. print '<td>'.$langs->trans("DescCP").'</td>';
  535. print '<td class="tdtop">';
  536. $doleditor = new DolEditor('description', GETPOST('description', 'restricthtml'), '', 80, 'dolibarr_notes', 'In', 0, false, isModEnabled('fckeditor'), ROWS_3, '90%');
  537. print $doleditor->Create(1);
  538. print '</td></tr>';
  539. // Other attributes
  540. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
  541. print '</tbody>';
  542. print '</table>';
  543. print dol_get_fiche_end();
  544. print $form->buttonsSaveCancel("SendRequestCollectiveCP");
  545. print '</from>'."\n";
  546. }
  547. } else {
  548. if ($error) {
  549. print '<div class="tabBar">';
  550. print $error;
  551. print '<br><br><input type="button" value="'.$langs->trans("ReturnCP").'" class="button" onclick="history.go(-1)" />';
  552. print '</div>';
  553. }
  554. }
  555. // End of page
  556. llxFooter();
  557. if (is_object($db)) {
  558. $db->close();
  559. }
  560. /**
  561. * send email to validator for current leave represented by (id)
  562. *
  563. * @param int $id validator for current leave represented by (id)
  564. * @param int $cancreate flag for user right
  565. * @param int $now date
  566. * @param int $autoValidation boolean flag on autovalidation
  567. *
  568. * @return stdClass
  569. * @throws Exception
  570. */
  571. function sendMail($id, $cancreate, $now, $autoValidation)
  572. {
  573. $objStd = new stdClass();
  574. $objStd->msg = '';
  575. $objStd->status = 'success';
  576. $objStd->error = 0;
  577. $objStd->style = '';
  578. global $db, $user, $conf, $langs;
  579. $object = new Holiday($db);
  580. $result = $object->fetch($id);
  581. if ($result) {
  582. // If draft and owner of leave
  583. if ($object->statut == Holiday::STATUS_VALIDATED && $cancreate) {
  584. $object->oldcopy = dol_clone($object);
  585. //if ($autoValidation) $object->statut = Holiday::STATUS_VALIDATED;
  586. $verif = $object->validate($user);
  587. if ($verif > 0) {
  588. // To
  589. $destinataire = new User($db);
  590. $destinataire->fetch($object->fk_validator);
  591. $emailTo = $destinataire->email;
  592. if (!$emailTo) {
  593. dol_syslog("Expected validator has no email, so we redirect directly to finished page without sending email");
  594. $objStd->error++;
  595. $objStd->msg = $langs->trans('ErroremailTo');
  596. $objStd->status = 'error';
  597. $objStd->style="warnings";
  598. return $objStd;
  599. }
  600. // From
  601. $expediteur = new User($db);
  602. $expediteur->fetch($object->fk_user);
  603. //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email.
  604. $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM;
  605. // Subject
  606. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  607. if (!empty($conf->global->MAIN_APPLICATION_TITLE)) {
  608. $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  609. }
  610. $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysToValidate");
  611. // Content
  612. $message = "<p>".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",</p>\n";
  613. $message .= "<p>".$langs->transnoentities("HolidaysToValidateBody")."</p>\n";
  614. // option to warn the validator in case of too short delay
  615. if (empty($conf->global->HOLIDAY_HIDE_APPROVER_ABOUT_TOO_LOW_DELAY)) {
  616. $delayForRequest = 0; // TODO Set delay depending of holiday leave type
  617. if ($delayForRequest) {
  618. $nowplusdelay = dol_time_plus_duree($now, $delayForRequest, 'd');
  619. if ($object->date_debut < $nowplusdelay) {
  620. $message = "<p>".$langs->transnoentities("HolidaysToValidateDelay", $delayForRequest)."</p>\n";
  621. }
  622. }
  623. }
  624. // option to notify the validator if the balance is less than the request
  625. if (empty($conf->global->HOLIDAY_HIDE_APPROVER_ABOUT_NEGATIVE_BALANCE)) {
  626. $nbopenedday = num_open_day($object->date_debut_gmt, $object->date_fin_gmt, 0, 1, $object->halfday);
  627. if ($nbopenedday > $object->getCPforUser($object->fk_user, $object->fk_type)) {
  628. $message .= "<p>".$langs->transnoentities("HolidaysToValidateAlertSolde")."</p>\n";
  629. }
  630. }
  631. $link = dol_buildpath("/holiday/card.php", 3) . '?id='.$object->id;
  632. $message .= "<ul>";
  633. $message .= "<li>".$langs->transnoentitiesnoconv("Name")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."</li>\n";
  634. $message .= "<li>".$langs->transnoentitiesnoconv("Period")." : ".dol_print_date($object->date_debut, 'day')." ".$langs->transnoentitiesnoconv("To")." ".dol_print_date($object->date_fin, 'day')."</li>\n";
  635. $message .= "<li>".$langs->transnoentitiesnoconv("Link").' : <a href="'.$link.'" target="_blank">'.$link."</a></li>\n";
  636. $message .= "</ul>\n";
  637. $trackid = 'leav'.$object->id;
  638. $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid);
  639. // Sending the email
  640. $result = $mail->sendfile();
  641. if (!$result) {
  642. $objStd->error++;
  643. $objStd->msg = $langs->trans('ErroreSendmail');
  644. $objStd->style="warnings";
  645. $objStd->status = 'error';
  646. } else {
  647. $objStd->msg = $langs->trans('mailSended');
  648. }
  649. return $objStd;
  650. } else {
  651. $objStd->error++;
  652. $objStd->msg = $langs->trans('ErroreVerif');
  653. $objStd->status = 'error';
  654. $objStd->style="errors";
  655. return $objStd;
  656. }
  657. }
  658. } else {
  659. $objStd->error++;
  660. $objStd->msg = $langs->trans('ErrorloadUserOnSendingMail');
  661. $objStd->status = 'error';
  662. $objStd->style="warnings";
  663. return $objStd;
  664. }
  665. return $objStd;
  666. }