From c434e9cffe8c5a484252114937a13069159051e2 Mon Sep 17 00:00:00 2001 From: jpb Date: Tue, 7 Jun 2022 19:01:57 +0200 Subject: [PATCH 01/16] add card and feature --- htdocs/core/menus/standard/eldy.lib.php | 2 + htdocs/holiday/card_group.php | 1093 +++++++++++++++++++++++ htdocs/holiday/class/holiday.class.php | 6 +- htdocs/langs/en_US/holiday.lang | 8 + 4 files changed, 1106 insertions(+), 3 deletions(-) create mode 100644 htdocs/holiday/card_group.php diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index 61d9872e09f..6427c0b07d7 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -2270,6 +2270,7 @@ function get_left_menu_hrm($mainmenu, &$newmenu, $usemenuhider = 1, $leftmenu = $newmenu->add("/holiday/list.php?mainmenu=hrm&leftmenu=holiday", $langs->trans("CPTitreMenu"), 0, $user->rights->holiday->read, '', $mainmenu, 'holiday', 0, '', '', '', img_picto('', 'holiday', 'class="pictofixedwidth"')); $newmenu->add("/holiday/card.php?mainmenu=hrm&leftmenu=holiday&action=create", $langs->trans("New"), 1, $user->rights->holiday->write, '', $mainmenu); + $newmenu->add("/holiday/card_group.php?mainmenu=hrm&leftmenu=holiday&action=request", $langs->trans("NewHolidayForGroup"), 1, ($user->rights->holiday->writeall && $user->rights->holiday->readall) , '', $mainmenu, 'holiday_sm'); $newmenu->add("/holiday/list.php?mainmenu=hrm&leftmenu=holiday", $langs->trans("List"), 1, $user->rights->holiday->read, '', $mainmenu); if ($usemenuhider || empty($leftmenu) || $leftmenu == "holiday") { $newmenu->add("/holiday/list.php?search_status=1&mainmenu=hrm&leftmenu=holiday", $langs->trans("DraftCP"), 2, $user->rights->holiday->read, '', $mainmenu, 'holiday_sm'); @@ -2281,6 +2282,7 @@ function get_left_menu_hrm($mainmenu, &$newmenu, $usemenuhider = 1, $leftmenu = $newmenu->add("/holiday/define_holiday.php?mainmenu=hrm&action=request", $langs->trans("MenuConfCP"), 1, $user->rights->holiday->read, '', $mainmenu, 'holiday_sm'); $newmenu->add("/holiday/month_report.php?mainmenu=hrm&leftmenu=holiday", $langs->trans("MenuReportMonth"), 1, $user->rights->holiday->readall, '', $mainmenu, 'holiday_sm'); $newmenu->add("/holiday/view_log.php?mainmenu=hrm&leftmenu=holiday&action=request", $langs->trans("MenuLogCP"), 1, $user->rights->holiday->define_holiday, '', $mainmenu, 'holiday_sm'); + } // Trips and expenses (old module) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php new file mode 100644 index 00000000000..05694934589 --- /dev/null +++ b/htdocs/holiday/card_group.php @@ -0,0 +1,1093 @@ + + * Copyright (C) 2012-2016 Laurent Destailleur + * Copyright (C) 2012-2016 Regis Houssin + * Copyright (C) 2013 Juanjo Menent + * Copyright (C) 2017 Alexandre Spangaro + * Copyright (C) 2014-2017 Ferran Marcet + * Copyright (C) 2018 Frédéric France + * Copyright (C) 2020-2021 Udo Tamm + * Copyright (C) 2022 Anthony Berton + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, orwrite + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/holiday/card.php + * \ingroup holiday + * \brief Form and file creation of paid holiday. + */ + +require '../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; +require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/holiday.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; + +// Get parameters +$action = GETPOST('action', 'aZ09'); +$cancel = GETPOST('cancel', 'alpha'); +$confirm = GETPOST('confirm', 'alpha'); +$id = GETPOST('id', 'int'); +$ref = GETPOST('ref', 'alpha'); +$fuserid = (GETPOST('fuserid', 'int') ?GETPOST('fuserid', 'int') : $user->id); +$users = (GETPOST('users', 'array') ?GETPOST('users', 'array') : array($user->id)); +$groups = GETPOST('groups', 'array') ; +$socid = GETPOST('socid', 'int'); +$autoApproval = GETPOST('autoApproval','int'); +$AutoSendMail = GETPOST('AutoSendMail','int'); +// Load translation files required by the page +$langs->loadLangs(array("other", "holiday", "mails", "trips")); + +$error = 0; + +$now = dol_now(); + +$childids = $user->getAllChildIds(1); + +$morefilter = ''; +if (!empty($conf->global->HOLIDAY_HIDE_FOR_NON_SALARIES)) { + $morefilter = 'AND employee = 1'; +} + +$object = new Holiday($db); + +$extrafields = new ExtraFields($db); + +// fetch optionals attributes and labels +$extrafields->fetch_name_optionals_label($object->table_element); + +if (($id > 0) || $ref) { + $object->fetch($id, $ref); + + // Check current user can read this leave request + $canread = 0; + if (!empty($user->rights->holiday->readall)) { + $canread = 1; + } + if (!empty($user->rights->holiday->read) && in_array($object->fk_user, $childids)) { + $canread = 1; + } + if (!$canread) { + accessforbidden(); + } +} + +// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context +$hookmanager->initHooks(array('holidaycard', 'globalcard')); + +$cancreate = 0; +$cancreateall = 0; +if (!empty($user->rights->holiday->write) && in_array($fuserid, $childids)) { + $cancreate = 1; +} +if (!empty($user->rights->holiday->writeall)) { + $cancreate = 1; + $cancreateall = 1; +} + +$candelete = 0; +if (!empty($user->rights->holiday->delete)) { + $candelete = 1; +} +if ($object->statut == Holiday::STATUS_DRAFT && $user->rights->holiday->write && in_array($object->fk_user, $childids)) { + $candelete = 1; +} + +// Protection if external user +if ($user->socid) { + $socid = $user->socid; +} +$result = restrictedArea($user, 'holiday', $object->id, 'holiday', '', '', 'rowid', $object->statut); + + +/* + * Actions + */ + +$parameters = array('socid' => $socid); +$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks +if ($reshook < 0) { + setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); +} + +if (empty($reshook)) { + $backurlforlist = DOL_URL_ROOT.'/holiday/list.php'; + + if (empty($backtopage) || ($cancel && empty($id))) { + if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) { + if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) { + $backtopage = $backurlforlist; + } else { + $backtopage = DOL_URL_ROOT.'/holiday/card_group.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__'); + } + } + } + + if ($cancel) { + if (!empty($backtopageforcancel)) { + header("Location: ".$backtopageforcancel); + exit; + } elseif (!empty($backtopage)) { + header("Location: ".$backtopage); + exit; + } + $action = ''; + } + + // Add leave request + if ($action == 'add') { + // If no right to create a request + if (!$cancreate) { + $error++; + setEventMessages($langs->trans('CantCreateCP'), null, 'errors'); + $action = 'create'; + } + + if (!$error) { + + $users = GETPOST('users', 'array'); + $groups = GETPOST('groups', 'array') ; + + $date_debut = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year')); + $date_fin = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year')); + $date_debut_gmt = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year'), 1); + $date_fin_gmt = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year'), 1); + $starthalfday = GETPOST('starthalfday'); + $endhalfday = GETPOST('endhalfday'); + $type = GETPOST('type'); + + $halfday = 0; + if ($starthalfday == 'afternoon' && $endhalfday == 'morning') { + $halfday = 2; + } elseif ($starthalfday == 'afternoon') { + $halfday = -1; + } elseif ($endhalfday == 'morning') { + $halfday = 1; + } + + $approverid = GETPOST('valideur', 'int'); + $description = trim(GETPOST('description', 'restricthtml')); + + // Check that leave is for a user inside the hierarchy or advanced permission for all is set + if (!$cancreateall) { + if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) { + if (empty($user->rights->holiday->write)) { + $error++; + setEventMessages($langs->trans("NotEnoughPermissions"), null, 'errors'); + } elseif (!in_array($fuserid, $childids)) { + $error++; + setEventMessages($langs->trans("UserNotInHierachy"), null, 'errors'); + $action = 'create'; + } + } else { + if (empty($user->rights->holiday->write) && empty($user->rights->holiday->writeall_advance)) { + $error++; + setEventMessages($langs->trans("NotEnoughPermissions"), null, 'errors'); + } elseif (empty($user->rights->holiday->writeall_advance) && !in_array($fuserid, $childids)) { + $error++; + setEventMessages($langs->trans("UserNotInHierachy"), null, 'errors'); + $action = 'create'; + } + } + } + // If no groups and no users + if (empty($groups) && empty($users)){ + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("User")), null, 'errors'); + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Group")), null, 'errors'); + $error++; + $action = 'create'; + } + // If no type + if ($type <= 0) { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors'); + $error++; + $action = 'create'; + } + + // If no start date + if (empty($date_debut)) { + setEventMessages($langs->trans("NoDateDebut"), null, 'errors'); + $error++; + $action = 'create'; + } + // If no end date + if (empty($date_fin)) { + setEventMessages($langs->trans("NoDateFin"), null, 'errors'); + $error++; + $action = 'create'; + } + // If start date after end date + if ($date_debut > $date_fin) { + setEventMessages($langs->trans("ErrorEndDateCP"), null, 'errors'); + $error++; + $action = 'create'; + } + + // If there is no Business Days within request + $nbopenedday = num_open_day($date_debut_gmt, $date_fin_gmt, 0, 1, $halfday); + if ($nbopenedday < 0.5) { + setEventMessages($langs->trans("ErrorDureeCP"), null, 'errors'); // No working day + $error++; + $action = 'create'; + } + + // If no validator designated + if ($approverid < 1) { + setEventMessages($langs->transnoentitiesnoconv('InvalidValidatorCP'), null, 'errors'); + $error++; + } + + $result = 0; + + + if (!$error) { + + $TusersToProcess = array(); + + /** GROUPS */ + $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from ' . MAIN_DB_PREFIX . 'user as u'; + $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'usergroup_user as ug on ug.fk_user = u.rowid '; + $sql .= ' WHERE fk_usergroup in (' . implode(',', $groups) . ')'; + $resql = $db->query($sql); + + if ($resql) { + while ($obj = $db->fetch_object($resql)) { + if (!array_key_exists($obj->rowid, $TusersToProcess)) + $TusersToProcess[] = $obj->rowid; + } + } + /** USERS */ + if (is_array($users) && count($users) > 0) { + foreach ($users as $u) { + if (!array_key_exists($u, $TusersToProcess)) + $TusersToProcess[] = $u; + } + } + foreach ($TusersToProcess as $u) { + // Check if there is already holiday for this period pour chaque user + $verifCP = $object->verifDateHolidayCP($u, $date_debut, $date_fin, $halfday); + if (!$verifCP) { + setEventMessages($langs->trans("alreadyCPexist"), null, 'errors'); + + $userError = new User($db); + $result = $userError->fetch($u); + + if ($result){ + setEventMessages($langs->trans("UseralreadyCPexist",$userError->firstname . ' '. $userError->lastname), null, 'errors'); + }else{ + setEventMessages($langs->trans("ErrorUserFetch",$u), null, 'errors'); + } + + $error++; + $action = 'create'; + } + } + + if (!$error) { + $db->begin(); + // non errors we can insert all + foreach ($TusersToProcess as $u) { + + $object = new Holiday($db); + $object->fk_user = $u; + $object->description = $description; + $object->fk_validator = $approverid; + $object->fk_type = $type; + $object->date_debut = $date_debut; + $object->date_fin = $date_fin; + $object->halfday = $halfday; + + $result = $object->create($user); + + if ($result <= 0) { + setEventMessages($object->error, $object->errors, 'errors'); + $error++; + }else{ + // AUTO APPROUVAL /VALIDATED + //@TODO changer le nom si approuved / validated + if ($autoApproval){ + $htemp = new Holiday($db); + $htemp->fetch ($result); + // must set the status before approve call ... + $htemp->statut = Holiday::STATUS_VALIDATED; + // $resultApproved = $htemp->approve($approverid); + + //@todo à voir avec nico validated ou approuved ? + $resultValidated = $htemp->update($approverid); + + if ($resultValidated < 0 ){ + setEventMessages($object->error, $object->errors, 'errors'); + $error++; + } + } + + if ($AutoSendMail && !$error){ + // send a mail to the user + sendMail($result, $cancreate, $now); + } + + } + } + } + // If no SQL error we redirect to the request card + if (!$error) { + $db->commit(); + header('Location: '.DOL_URL_ROOT.'/holiday/list.php?restore_lastsearch_values=1'); + exit; + } else { + $db->rollback(); + } + } + } + } + + + if ($action == 'update_extras') { + $object->oldcopy = dol_clone($object); + + // Fill array 'array_options' with data from update form + $ret = $extrafields->setOptionalsFromPost(null, $object, GETPOST('attribute', 'restricthtml')); + if ($ret < 0) { + $error++; + } + + if (!$error) { + // Actions on extra fields + $result = $object->insertExtraFields('HOLIDAY_MODIFY'); + if ($result < 0) { + setEventMessages($object->error, $object->errors, 'errors'); + $error++; + } + } + + if ($error) { + $action = 'edit_extras'; + } + } + + // Approve leave request + if ($action == 'confirm_valid') { + $object->fetch($id); + + // If status is waiting approval and approver is also user + if ($object->statut == Holiday::STATUS_VALIDATED && $user->id == $object->fk_validator) { + $object->oldcopy = dol_clone($object); + + $object->date_valid = dol_now(); + $object->fk_user_valid = $user->id; + $object->statut = Holiday::STATUS_APPROVED; + + $db->begin(); + + $verif = $object->approve($user); + if ($verif <= 0) { + setEventMessages($object->error, $object->errors, 'errors'); + $error++; + } + + // If no SQL error, we redirect to the request form + if (!$error) { + // Calculcate number of days consummed + $nbopenedday = num_open_day($object->date_debut_gmt, $object->date_fin_gmt, 0, 1, $object->halfday); + $soldeActuel = $object->getCpforUser($object->fk_user, $object->fk_type); + $newSolde = ($soldeActuel - $nbopenedday); + $label = $langs->transnoentitiesnoconv("Holidays").' - '.$object->ref; + + // The modification is added to the LOG + $result = $object->addLogCP($user->id, $object->fk_user, $label, $newSolde, $object->fk_type); + if ($result < 0) { + $error++; + setEventMessages(null, $object->errors, 'errors'); + } + + // Update balance + $result = $object->updateSoldeCP($object->fk_user, $newSolde, $object->fk_type); + if ($result < 0) { + $error++; + setEventMessages(null, $object->errors, 'errors'); + } + } + + if (!$error) { + // To + $destinataire = new User($db); + $destinataire->fetch($object->fk_user); + $emailTo = $destinataire->email; + + if (!$emailTo) { + dol_syslog("User that request leave has no email, so we redirect directly to finished page without sending email"); + } else { + // From + $expediteur = new User($db); + $expediteur->fetch($object->fk_validator); + //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email. + $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM; + + // Subject + $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM; + if (!empty($conf->global->MAIN_APPLICATION_TITLE)) { + $societeName = $conf->global->MAIN_APPLICATION_TITLE; + } + + $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysValidated"); + + // Content + $message = "

".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",

\n"; + + $message .= "

".$langs->transnoentities("HolidaysValidatedBody", dol_print_date($object->date_debut, 'day'), dol_print_date($object->date_fin, 'day'))."

\n"; + + $link = dol_buildpath('/holiday/card.php', 3).'?id='.$object->id; + + $message .= "
    \n"; + $message .= "
  • ".$langs->transnoentitiesnoconv("ValidatedBy")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."
  • \n"; + $message .= "
  • ".$langs->transnoentitiesnoconv("Link").' : '.$link."
  • \n"; + $message .= "
\n"; + + $trackid = 'leav'.$object->id; + + $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid); + + // Sending email + $result = $mail->sendfile(); + + if (!$result) { + setEventMessages($mail->error, $mail->errors, 'warnings'); // Show error, but do no make rollback, so $error is not set to 1 + $action = ''; + } + } + } + + if (!$error) { + $db->commit(); + + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); + exit; + } else { + $db->rollback(); + $action = ''; + } + } + } + + if ($action == 'confirm_refuse' && GETPOST('confirm', 'alpha') == 'yes') { + if (GETPOST('detail_refuse')) { + $object->fetch($id); + + // If status pending validation and validator = user + if ($object->statut == Holiday::STATUS_VALIDATED && $user->id == $object->fk_validator) { + $object->date_refuse = dol_print_date('dayhour', dol_now()); + $object->fk_user_refuse = $user->id; + $object->statut = Holiday::STATUS_REFUSED; + $object->detail_refuse = GETPOST('detail_refuse', 'alphanohtml'); + + $db->begin(); + + $verif = $object->update($user); + if ($verif <= 0) { + $error++; + setEventMessages($object->error, $object->errors, 'errors'); + } + + // If no SQL error, we redirect to the request form + if (!$error) { + // To + $destinataire = new User($db); + $destinataire->fetch($object->fk_user); + $emailTo = $destinataire->email; + + if (!$emailTo) { + dol_syslog("User that request leave has no email, so we redirect directly to finished page without sending email"); + } else { + // From + $expediteur = new User($db); + $expediteur->fetch($object->fk_validator); + //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email. + $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM; + + // Subject + $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM; + if (!empty($conf->global->MAIN_APPLICATION_TITLE)) { + $societeName = $conf->global->MAIN_APPLICATION_TITLE; + } + + $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysRefused"); + + // Content + $message = "

".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",

\n"; + + $message .= "

".$langs->transnoentities("HolidaysRefusedBody", dol_print_date($object->date_debut, 'day'), dol_print_date($object->date_fin, 'day'))."

\n"; + $message .= "

".GETPOST('detail_refuse', 'alpha')."

"; + + $link = dol_buildpath('/holiday/card.php', 3).'?id='.$object->id; + + $message .= "
    \n"; + $message .= "
  • ".$langs->transnoentitiesnoconv("ModifiedBy")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."
  • \n"; + $message .= "
  • ".$langs->transnoentitiesnoconv("Link").' : '.$link."
  • \n"; + $message .= "
"; + + $trackid = 'leav'.$object->id; + + $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid); + + // sending email + $result = $mail->sendfile(); + + if (!$result) { + setEventMessages($mail->error, $mail->errors, 'warnings'); // Show error, but do no make rollback, so $error is not set to 1 + $action = ''; + } + } + } else { + $action = ''; + } + + if (!$error) { + $db->commit(); + + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); + exit; + } else { + $db->rollback(); + $action = ''; + } + } + } else { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DetailRefusCP")), null, 'errors'); + $action = 'refuse'; + } + } + + + // If the request is validated + if ($action == 'confirm_draft' && GETPOST('confirm') == 'yes') { + $error = 0; + + $object->fetch($id); + + $oldstatus = $object->statut; + $object->statut = Holiday::STATUS_DRAFT; + + $result = $object->update($user); + if ($result < 0) { + $error++; + setEventMessages($langs->trans('ErrorBackToDraft').' '.$object->error, $object->errors, 'errors'); + } + + if (!$error) { + $db->commit(); + + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); + exit; + } else { + $db->rollback(); + } + } + + // If confirmation of cancellation + if ($action == 'confirm_cancel' && GETPOST('confirm') == 'yes') { + $error = 0; + + $object->fetch($id); + + // If status pending validation and validator = validator or user, or rights to do for others + if (($object->statut == Holiday::STATUS_VALIDATED || $object->statut == Holiday::STATUS_APPROVED) && + (!empty($user->admin) || $user->id == $object->fk_validator || $cancreate || $cancreateall)) { + $db->begin(); + + $oldstatus = $object->statut; + $object->date_cancel = dol_now(); + $object->fk_user_cancel = $user->id; + $object->statut = Holiday::STATUS_CANCELED; + + $result = $object->update($user); + + if ($result >= 0 && $oldstatus == Holiday::STATUS_APPROVED) { // holiday was already validated, status 3, so we must increase back the balance + // Call trigger + $result = $object->call_trigger('HOLIDAY_CANCEL', $user); + if ($result < 0) { + $error++; + } + + // Calculcate number of days consummed + $nbopenedday = num_open_day($object->date_debut_gmt, $object->date_fin_gmt, 0, 1, $object->halfday); + + $soldeActuel = $object->getCpforUser($object->fk_user, $object->fk_type); + $newSolde = ($soldeActuel + $nbopenedday); + + // The modification is added to the LOG + $result1 = $object->addLogCP($user->id, $object->fk_user, $langs->transnoentitiesnoconv("HolidaysCancelation"), $newSolde, $object->fk_type); + + // Update of the balance + $result2 = $object->updateSoldeCP($object->fk_user, $newSolde, $object->fk_type); + + if ($result1 < 0 || $result2 < 0) { + $error++; + setEventMessages($langs->trans('ErrorCantDeleteCP').' '.$object->error, $object->errors, 'errors'); + } + } + + if (!$error) { + $db->commit(); + } else { + $db->rollback(); + } + + // If no SQL error, we redirect to the request form + if (!$error && $result > 0) { + // To + $destinataire = new User($db); + $destinataire->fetch($object->fk_user); + $emailTo = $destinataire->email; + + if (!$emailTo) { + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); + exit; + } + + // From + $expediteur = new User($db); + $expediteur->fetch($object->fk_user_cancel); + //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email. + $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM; + + // Subject + $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM; + if (!empty($conf->global->MAIN_APPLICATION_TITLE)) { + $societeName = $conf->global->MAIN_APPLICATION_TITLE; + } + + $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysCanceled"); + + // Content + $message = "

".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",

\n"; + + $message .= "

".$langs->transnoentities("HolidaysCanceledBody", dol_print_date($object->date_debut, 'day'), dol_print_date($object->date_fin, 'day'))."

\n"; + + $link = dol_buildpath('/holiday/card.php', 3).'?id='.$object->id; + + $message .= "
    \n"; + $message .= "
  • ".$langs->transnoentitiesnoconv("ModifiedBy")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."
  • \n"; + $message .= "
  • ".$langs->transnoentitiesnoconv("Link").' : '.$link."
  • \n"; + $message .= "
\n"; + + $trackid = 'leav'.$object->id; + + $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid); + + // sending email + $result = $mail->sendfile(); + + if (!$result) { + setEventMessages($mail->error, $mail->errors, 'warnings'); + $action = ''; + } else { + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); + exit; + } + } + } + } + +} + + + +/* + * View + */ + +$form = new Form($db); +$object = new Holiday($db); + +$listhalfday = array('morning'=>$langs->trans("Morning"), "afternoon"=>$langs->trans("Afternoon")); + +$title = $langs->trans('Leave'); +$help_url = 'EN:Module_Holiday'; + +llxHeader('', $title, $help_url); + +if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { + // If user has no permission to create a leave + 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)))) { + $errors[] = $langs->trans('CantCreateCP'); + } else { + // Form to add a leave request + print load_fiche_titre($langs->trans('MenuCollectiveAddCP'), '', 'title_hrm.png'); + + // Error management + if (GETPOST('error')) { + switch (GETPOST('error')) { + case 'datefin': + $errors[] = $langs->trans('ErrorEndDateCP'); + break; + case 'SQL_Create': + $errors[] = $langs->trans('ErrorSQLCreateCP').' '.htmlentities($_GET['msg']).''; + break; + case 'CantCreate': + $errors[] = $langs->trans('CantCreateCP'); + break; + case 'Valideur': + $errors[] = $langs->trans('InvalidValidatorCP'); + break; + case 'nodatedebut': + $errors[] = $langs->trans('NoDateDebut'); + break; + case 'nodatefin': + $errors[] = $langs->trans('NoDateFin'); + break; + case 'DureeHoliday': + $errors[] = $langs->trans('ErrorDureeCP'); + break; + case 'alreadyCP': + $errors[] = $langs->trans('alreadyCPexist'); + break; + } + + setEventMessages($errors, null, 'errors'); + } + + + print ''."\n"; + + + // Formulaire de demande + print '
'."\n"; + print ''."\n"; + print ''."\n"; + + + + print dol_get_fiche_head(); + + //print ''.$langs->trans('DelayToRequestCP',$object->getConfCP('delayForRequest')).'

'; + + print ''; + print ''; + + // groupe + print ''; + print ''; + + print ''; + + // users + print ''; + print ''; + print ''; + + + + // Type + print ''; + print ''; + print ''; + print ''; + + // Date start + print ''; + print ''; + print ''; + print ''; + + // Date end + print ''; + print ''; + print ''; + print ''; + + // Approver + print ''; + print ''; + print ''; + print ''; + + //auto APPROVAL ON CREATE + print ''."\n"; + + + //no auto SEND MAIL + print ''."\n"; + + // Description + print ''; + print ''; + print ''; + + // Other attributes + include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php'; + + print ''; + print '
'.$langs->trans("groups"); + + print ''; + //@todo ajouter entity ! + $sql =' SELECT rowid, nom from '.MAIN_DB_PREFIX.'usergroup '; + + $resql = $db->query($sql); + $Tgroup = array(); + while ($obj = $db->fetch_object($resql)){ + $Tgroup[$obj->rowid] = $obj->nom; + } + print $form->multiselectarray('groups', $Tgroup, GETPOST('groups', 'array'), '', 0, 'quatrevingtpercent widthcentpercentminusx', 0, 0); + + print '
'.$langs->trans("users").''; + + $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from '.MAIN_DB_PREFIX.'user as u'; + $sql .= ' WHERE 1=1 '; + $sql .= !empty($morefilter) ? $morefilter : ''; + + + if ($cancreate && !$cancreateall) { + + }else{ + //$sql .= ' AND u.fk_user = '.$user->id; + //$sql .= ' OR u.rowid ='.$user->id; + } + + $resql = $db->query($sql); + if ($resql){ + while ($obj = $db->fetch_object($resql)){ + $userlist[$obj->rowid] = $obj->firstname . ' '. $obj->lastname; + } + } + + print img_picto('', 'users') . $form->multiselectarray('users', $userlist, GETPOST('users', 'array'), '', 0, 'quatrevingtpercent widthcentpercentminusx', 0, 0); + print '
'.$langs->trans("Type").''; + $typeleaves = $object->getTypes(1, -1); + $arraytypeleaves = array(); + foreach ($typeleaves as $key => $val) { + $labeltoshow = ($langs->trans($val['code']) != $val['code'] ? $langs->trans($val['code']) : $val['label']); + $labeltoshow .= ($val['delay'] > 0 ? ' ('.$langs->trans("NoticePeriod").': '.$val['delay'].' '.$langs->trans("days").')' : ''); + $arraytypeleaves[$val['rowid']] = $labeltoshow; + } + print $form->selectarray('type', $arraytypeleaves, (GETPOST('type', 'alpha') ?GETPOST('type', 'alpha') : ''), 1, 0, 0, '', 0, 0, 0, '', '', true); + if ($user->admin) { + print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1); + } + print '
'; + print $form->textwithpicto($langs->trans("DateDebCP"), $langs->trans("FirstDayOfHoliday")); + print ''; + // Si la demande ne vient pas de l'agenda + if (!GETPOST('date_debut_')) { + print $form->selectDate(-1, 'date_debut_', 0, 0, 0, '', 1, 1); + } else { + $tmpdate = dol_mktime(0, 0, 0, GETPOST('date_debut_month', 'int'), GETPOST('date_debut_day', 'int'), GETPOST('date_debut_year', 'int')); + print $form->selectDate($tmpdate, 'date_debut_', 0, 0, 0, '', 1, 1); + } + print '     '; + print $form->selectarray('starthalfday', $listhalfday, (GETPOST('starthalfday', 'alpha') ?GETPOST('starthalfday', 'alpha') : 'morning')); + print '
'; + print $form->textwithpicto($langs->trans("DateFinCP"), $langs->trans("LastDayOfHoliday")); + print ''; + if (!GETPOST('date_fin_')) { + print $form->selectDate(-1, 'date_fin_', 0, 0, 0, '', 1, 1); + } else { + $tmpdate = dol_mktime(0, 0, 0, GETPOST('date_fin_month', 'int'), GETPOST('date_fin_day', 'int'), GETPOST('date_fin_year', 'int')); + print $form->selectDate($tmpdate, 'date_fin_', 0, 0, 0, '', 1, 1); + } + print '     '; + print $form->selectarray('endhalfday', $listhalfday, (GETPOST('endhalfday', 'alpha') ?GETPOST('endhalfday', 'alpha') : 'afternoon')); + print '
'.$langs->trans("ReviewedByCP").''; + + $object = new Holiday($db); + $include_users = $object->fetch_users_approver_holiday(); + if (empty($include_users)) { + print img_warning().' '.$langs->trans("NobodyHasPermissionToValidateHolidays"); + } else { + // Defined default approver (the forced approved of user or the supervisor if no forced value defined) + // Note: This use will be set only if the deinfed approvr has permission to approve so is inside include_users + $defaultselectuser = (empty($user->fk_user_holiday_validator) ? $user->fk_user : $user->fk_user_holiday_validator); + if (!empty($conf->global->HOLIDAY_DEFAULT_VALIDATOR)) { + $defaultselectuser = $conf->global->HOLIDAY_DEFAULT_VALIDATOR; // Can force default approver + } + if (GETPOST('valideur', 'int') > 0) { + $defaultselectuser = GETPOST('valideur', 'int'); + } + $s = $form->select_dolusers($defaultselectuser, "valideur", 1, '', 0, $include_users, '', '0,'.$conf->entity, 0, 0, '', 0, '', 'minwidth200 maxwidth500'); + print img_picto('', 'user').$form->textwithpicto($s, $langs->trans("AnyOtherInThisListCanValidate")); + } + + + print '
'.$langs->trans("AutoApprovalOnCreate").''; + print ''; + print '
'.$langs->trans("AutoSendMail").''; + print ''; + print '
'.$langs->trans("DescCP").''; + $doleditor = new DolEditor('description', GETPOST('description', 'restricthtml'), '', 80, 'dolibarr_notes', 'In', 0, false, empty($conf->fckeditor->enabled) ? false : $conf->fckeditor->enabled, ROWS_3, '90%'); + print $doleditor->Create(1); + print '
'; + + print dol_get_fiche_end(); + + print $form->buttonsSaveCancel("SendRequestCollectiveCP"); + + print ''."\n"; + } +} else { + if ($error) { + print '
'; + print $error; + print '

'; + print '
'; + } +} + +// End of page +llxFooter(); + +if (is_object($db)) { + $db->close(); +} + +// automatic send mail +function sendMail ($id, $cancreate, $now){ + + global $db, $user, $conf, $langs; + + $object = new Holiday($db); + + $result = $object->fetch($id); + + if ($result){ + // If draft and owner of leave + if ($object->statut == Holiday::STATUS_DRAFT && $cancreate) { + $object->oldcopy = dol_clone($object); + + $object->statut = Holiday::STATUS_VALIDATED; + + $verif = $object->validate($user); + + // If no SQL error, we redirect to the request form + if ($verif > 0) { + // To + $destinataire = new User($db); + $destinataire->fetch($object->fk_validator); + $emailTo = $destinataire->email; + + if (!$emailTo) { + dol_syslog("Expected validator has no email, so we redirect directly to finished page without sending email"); + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); + exit; + } + + // From + $expediteur = new User($db); + $expediteur->fetch($object->fk_user); + //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email. + $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM; + + // Subject + $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM; + if (!empty($conf->global->MAIN_APPLICATION_TITLE)) { + $societeName = $conf->global->MAIN_APPLICATION_TITLE; + } + + $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysToValidate"); + + // Content + $message = "

".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",

\n"; + + $message .= "

".$langs->transnoentities("HolidaysToValidateBody")."

\n"; + + + // option to warn the validator in case of too short delay + if (empty($conf->global->HOLIDAY_HIDE_APPROVER_ABOUT_TOO_LOW_DELAY)) { + $delayForRequest = 0; // TODO Set delay depending of holiday leave type + if ($delayForRequest) { + $nowplusdelay = dol_time_plus_duree($now, $delayForRequest, 'd'); + + if ($object->date_debut < $nowplusdelay) { + $message = "

".$langs->transnoentities("HolidaysToValidateDelay", $delayForRequest)."

\n"; + } + } + } + + // option to notify the validator if the balance is less than the request + if (empty($conf->global->HOLIDAY_HIDE_APPROVER_ABOUT_NEGATIVE_BALANCE)) { + $nbopenedday = num_open_day($object->date_debut_gmt, $object->date_fin_gmt, 0, 1, $object->halfday); + + if ($nbopenedday > $object->getCPforUser($object->fk_user, $object->fk_type)) { + $message .= "

".$langs->transnoentities("HolidaysToValidateAlertSolde")."

\n"; + } + } + + $link = dol_buildpath("/holiday/card.php", 3) . '?id='.$object->id; + + $message .= "
    "; + $message .= "
  • ".$langs->transnoentitiesnoconv("Name")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."
  • \n"; + $message .= "
  • ".$langs->transnoentitiesnoconv("Period")." : ".dol_print_date($object->date_debut, 'day')." ".$langs->transnoentitiesnoconv("To")." ".dol_print_date($object->date_fin, 'day')."
  • \n"; + $message .= "
  • ".$langs->transnoentitiesnoconv("Link").' : '.$link."
  • \n"; + $message .= "
\n"; + + $trackid = 'leav'.$object->id; + + $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid); + + // Sending the email + $result = $mail->sendfile(); + + if (!$result) { + setEventMessages($mail->error, $mail->errors, 'warnings'); + $action = ''; + } else { + header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); + exit; + } + } else { + setEventMessages($object->error, $object->errors, 'errors'); + $action = ''; + } + } + }else{ + setEventMessage($langs->trans('ErrorloadUserOnSendingMail'),'warning'); + } + + +} diff --git a/htdocs/holiday/class/holiday.class.php b/htdocs/holiday/class/holiday.class.php index bef89f4462e..1f6dbb6b44d 100644 --- a/htdocs/holiday/class/holiday.class.php +++ b/htdocs/holiday/class/holiday.class.php @@ -1111,11 +1111,11 @@ class Holiday extends CommonObject $this->fetchByUser($fk_user, '', ''); foreach ($this->holiday as $infos_CP) { - if ($infos_CP['statut'] == 4) { + if ($infos_CP['statut'] == Holiday::STATUS_CANCELED) { continue; // ignore not validated holidays } - if ($infos_CP['statut'] == 5) { - continue; // ignore not validated holidays + if ($infos_CP['statut'] == Holiday::STATUS_REFUSED) { + continue; // ignore refused holidays } //var_dump("--"); //var_dump("old: ".dol_print_date($infos_CP['date_debut'],'dayhour').' '.dol_print_date($infos_CP['date_fin'],'dayhour').' '.$infos_CP['halfday']); diff --git a/htdocs/langs/en_US/holiday.lang b/htdocs/langs/en_US/holiday.lang index 3d0ae64be0f..944b0b54db8 100644 --- a/htdocs/langs/en_US/holiday.lang +++ b/htdocs/langs/en_US/holiday.lang @@ -4,6 +4,7 @@ Holidays=Leave CPTitreMenu=Leave MenuReportMonth=Monthly statement MenuAddCP=New leave request +MenuCollectiveAddCP=New collective leave request NotActiveModCP=You must enable the module Leave to view this page. AddCP=Make a leave request DateDebCP=Start date @@ -86,6 +87,13 @@ UserUpdateCP=Updated for PrevSoldeCP=Previous Balance NewSoldeCP=New Balance alreadyCPexist=A leave request has already been done on this period. +UseralreadyCPexist=A leave request has already been done on this period for %s. +groups=Groups +users=Users +AutoSendMail=Automatic mailing +NewHolidayForGroup=Collective leaves +SendRequestCollectiveCP=Send collective leave request +AutoApprovalOnCreate=Automatic approval FirstDayOfHoliday=Beginning day of leave request LastDayOfHoliday=Ending day of leave request BoxTitleLastLeaveRequests=Latest %s modified leave requests From 02a4e10fd3e6ba1edc0d2daa5c1861ef11986296 Mon Sep 17 00:00:00 2001 From: jpb Date: Tue, 7 Jun 2022 19:12:40 +0200 Subject: [PATCH 02/16] add todo for some jquery --- htdocs/holiday/card_group.php | 35 ++++++++++++++++++++------------- htdocs/langs/en_US/holiday.lang | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 05694934589..5d96bbbbc21 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -51,7 +51,7 @@ $fuserid = (GETPOST('fuserid', 'int') ?GETPOST('fuserid', 'int') : $user->id); $users = (GETPOST('users', 'array') ?GETPOST('users', 'array') : array($user->id)); $groups = GETPOST('groups', 'array') ; $socid = GETPOST('socid', 'int'); -$autoApproval = GETPOST('autoApproval','int'); +$autoValidation = GETPOST('autoValidation','int'); $AutoSendMail = GETPOST('AutoSendMail','int'); // Load translation files required by the page $langs->loadLangs(array("other", "holiday", "mails", "trips")); @@ -323,26 +323,26 @@ if (empty($reshook)) { }else{ // AUTO APPROUVAL /VALIDATED //@TODO changer le nom si approuved / validated - if ($autoApproval){ + if ($autoValidation){ $htemp = new Holiday($db); $htemp->fetch ($result); - // must set the status before approve call ... - $htemp->statut = Holiday::STATUS_VALIDATED; - // $resultApproved = $htemp->approve($approverid); - //@todo à voir avec nico validated ou approuved ? + $htemp->statut = Holiday::STATUS_VALIDATED; $resultValidated = $htemp->update($approverid); if ($resultValidated < 0 ){ setEventMessages($object->error, $object->errors, 'errors'); $error++; } + // we can auto send mail if we are in auto validation behavior + //@todo jquery disable if checkbox autovalidation unchecked + if ($AutoSendMail && !$error){ + // send a mail to the user + sendMail($result, $cancreate, $now, $autoValidation); + } } - if ($AutoSendMail && !$error){ - // send a mail to the user - sendMail($result, $cancreate, $now); - } + } } @@ -985,9 +985,16 @@ llxFooter(); if (is_object($db)) { $db->close(); } - -// automatic send mail -function sendMail ($id, $cancreate, $now){ +/** + * we send email to validator for current leave request (id) + * @param $id + * @param $cancreate + * @param $now + * @param $autoValidation + * @return void + * @throws Exception + */ +function sendMail ($id, $cancreate, $now, $autoValidation){ global $db, $user, $conf, $langs; @@ -1000,7 +1007,7 @@ function sendMail ($id, $cancreate, $now){ if ($object->statut == Holiday::STATUS_DRAFT && $cancreate) { $object->oldcopy = dol_clone($object); - $object->statut = Holiday::STATUS_VALIDATED; + if ($autoValidation) $object->statut = Holiday::STATUS_VALIDATED; $verif = $object->validate($user); diff --git a/htdocs/langs/en_US/holiday.lang b/htdocs/langs/en_US/holiday.lang index 944b0b54db8..2005d7e613c 100644 --- a/htdocs/langs/en_US/holiday.lang +++ b/htdocs/langs/en_US/holiday.lang @@ -93,7 +93,7 @@ users=Users AutoSendMail=Automatic mailing NewHolidayForGroup=Collective leaves SendRequestCollectiveCP=Send collective leave request -AutoApprovalOnCreate=Automatic approval +AutoValidationOnCreate=Automatic validation FirstDayOfHoliday=Beginning day of leave request LastDayOfHoliday=Ending day of leave request BoxTitleLastLeaveRequests=Latest %s modified leave requests From bf0650900d339c8b11459f0bfc9574648e4d18df Mon Sep 17 00:00:00 2001 From: jpb Date: Tue, 7 Jun 2022 19:16:57 +0200 Subject: [PATCH 03/16] remove useless code --- htdocs/holiday/card_group.php | 348 ---------------------------------- 1 file changed, 348 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 5d96bbbbc21..06157ee3e94 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -358,354 +358,6 @@ if (empty($reshook)) { } } } - - - if ($action == 'update_extras') { - $object->oldcopy = dol_clone($object); - - // Fill array 'array_options' with data from update form - $ret = $extrafields->setOptionalsFromPost(null, $object, GETPOST('attribute', 'restricthtml')); - if ($ret < 0) { - $error++; - } - - if (!$error) { - // Actions on extra fields - $result = $object->insertExtraFields('HOLIDAY_MODIFY'); - if ($result < 0) { - setEventMessages($object->error, $object->errors, 'errors'); - $error++; - } - } - - if ($error) { - $action = 'edit_extras'; - } - } - - // Approve leave request - if ($action == 'confirm_valid') { - $object->fetch($id); - - // If status is waiting approval and approver is also user - if ($object->statut == Holiday::STATUS_VALIDATED && $user->id == $object->fk_validator) { - $object->oldcopy = dol_clone($object); - - $object->date_valid = dol_now(); - $object->fk_user_valid = $user->id; - $object->statut = Holiday::STATUS_APPROVED; - - $db->begin(); - - $verif = $object->approve($user); - if ($verif <= 0) { - setEventMessages($object->error, $object->errors, 'errors'); - $error++; - } - - // If no SQL error, we redirect to the request form - if (!$error) { - // Calculcate number of days consummed - $nbopenedday = num_open_day($object->date_debut_gmt, $object->date_fin_gmt, 0, 1, $object->halfday); - $soldeActuel = $object->getCpforUser($object->fk_user, $object->fk_type); - $newSolde = ($soldeActuel - $nbopenedday); - $label = $langs->transnoentitiesnoconv("Holidays").' - '.$object->ref; - - // The modification is added to the LOG - $result = $object->addLogCP($user->id, $object->fk_user, $label, $newSolde, $object->fk_type); - if ($result < 0) { - $error++; - setEventMessages(null, $object->errors, 'errors'); - } - - // Update balance - $result = $object->updateSoldeCP($object->fk_user, $newSolde, $object->fk_type); - if ($result < 0) { - $error++; - setEventMessages(null, $object->errors, 'errors'); - } - } - - if (!$error) { - // To - $destinataire = new User($db); - $destinataire->fetch($object->fk_user); - $emailTo = $destinataire->email; - - if (!$emailTo) { - dol_syslog("User that request leave has no email, so we redirect directly to finished page without sending email"); - } else { - // From - $expediteur = new User($db); - $expediteur->fetch($object->fk_validator); - //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email. - $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM; - - // Subject - $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM; - if (!empty($conf->global->MAIN_APPLICATION_TITLE)) { - $societeName = $conf->global->MAIN_APPLICATION_TITLE; - } - - $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysValidated"); - - // Content - $message = "

".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",

\n"; - - $message .= "

".$langs->transnoentities("HolidaysValidatedBody", dol_print_date($object->date_debut, 'day'), dol_print_date($object->date_fin, 'day'))."

\n"; - - $link = dol_buildpath('/holiday/card.php', 3).'?id='.$object->id; - - $message .= "
    \n"; - $message .= "
  • ".$langs->transnoentitiesnoconv("ValidatedBy")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."
  • \n"; - $message .= "
  • ".$langs->transnoentitiesnoconv("Link").' : '.$link."
  • \n"; - $message .= "
\n"; - - $trackid = 'leav'.$object->id; - - $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid); - - // Sending email - $result = $mail->sendfile(); - - if (!$result) { - setEventMessages($mail->error, $mail->errors, 'warnings'); // Show error, but do no make rollback, so $error is not set to 1 - $action = ''; - } - } - } - - if (!$error) { - $db->commit(); - - header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); - exit; - } else { - $db->rollback(); - $action = ''; - } - } - } - - if ($action == 'confirm_refuse' && GETPOST('confirm', 'alpha') == 'yes') { - if (GETPOST('detail_refuse')) { - $object->fetch($id); - - // If status pending validation and validator = user - if ($object->statut == Holiday::STATUS_VALIDATED && $user->id == $object->fk_validator) { - $object->date_refuse = dol_print_date('dayhour', dol_now()); - $object->fk_user_refuse = $user->id; - $object->statut = Holiday::STATUS_REFUSED; - $object->detail_refuse = GETPOST('detail_refuse', 'alphanohtml'); - - $db->begin(); - - $verif = $object->update($user); - if ($verif <= 0) { - $error++; - setEventMessages($object->error, $object->errors, 'errors'); - } - - // If no SQL error, we redirect to the request form - if (!$error) { - // To - $destinataire = new User($db); - $destinataire->fetch($object->fk_user); - $emailTo = $destinataire->email; - - if (!$emailTo) { - dol_syslog("User that request leave has no email, so we redirect directly to finished page without sending email"); - } else { - // From - $expediteur = new User($db); - $expediteur->fetch($object->fk_validator); - //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email. - $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM; - - // Subject - $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM; - if (!empty($conf->global->MAIN_APPLICATION_TITLE)) { - $societeName = $conf->global->MAIN_APPLICATION_TITLE; - } - - $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysRefused"); - - // Content - $message = "

".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",

\n"; - - $message .= "

".$langs->transnoentities("HolidaysRefusedBody", dol_print_date($object->date_debut, 'day'), dol_print_date($object->date_fin, 'day'))."

\n"; - $message .= "

".GETPOST('detail_refuse', 'alpha')."

"; - - $link = dol_buildpath('/holiday/card.php', 3).'?id='.$object->id; - - $message .= "
    \n"; - $message .= "
  • ".$langs->transnoentitiesnoconv("ModifiedBy")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."
  • \n"; - $message .= "
  • ".$langs->transnoentitiesnoconv("Link").' : '.$link."
  • \n"; - $message .= "
"; - - $trackid = 'leav'.$object->id; - - $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid); - - // sending email - $result = $mail->sendfile(); - - if (!$result) { - setEventMessages($mail->error, $mail->errors, 'warnings'); // Show error, but do no make rollback, so $error is not set to 1 - $action = ''; - } - } - } else { - $action = ''; - } - - if (!$error) { - $db->commit(); - - header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); - exit; - } else { - $db->rollback(); - $action = ''; - } - } - } else { - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DetailRefusCP")), null, 'errors'); - $action = 'refuse'; - } - } - - - // If the request is validated - if ($action == 'confirm_draft' && GETPOST('confirm') == 'yes') { - $error = 0; - - $object->fetch($id); - - $oldstatus = $object->statut; - $object->statut = Holiday::STATUS_DRAFT; - - $result = $object->update($user); - if ($result < 0) { - $error++; - setEventMessages($langs->trans('ErrorBackToDraft').' '.$object->error, $object->errors, 'errors'); - } - - if (!$error) { - $db->commit(); - - header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); - exit; - } else { - $db->rollback(); - } - } - - // If confirmation of cancellation - if ($action == 'confirm_cancel' && GETPOST('confirm') == 'yes') { - $error = 0; - - $object->fetch($id); - - // If status pending validation and validator = validator or user, or rights to do for others - if (($object->statut == Holiday::STATUS_VALIDATED || $object->statut == Holiday::STATUS_APPROVED) && - (!empty($user->admin) || $user->id == $object->fk_validator || $cancreate || $cancreateall)) { - $db->begin(); - - $oldstatus = $object->statut; - $object->date_cancel = dol_now(); - $object->fk_user_cancel = $user->id; - $object->statut = Holiday::STATUS_CANCELED; - - $result = $object->update($user); - - if ($result >= 0 && $oldstatus == Holiday::STATUS_APPROVED) { // holiday was already validated, status 3, so we must increase back the balance - // Call trigger - $result = $object->call_trigger('HOLIDAY_CANCEL', $user); - if ($result < 0) { - $error++; - } - - // Calculcate number of days consummed - $nbopenedday = num_open_day($object->date_debut_gmt, $object->date_fin_gmt, 0, 1, $object->halfday); - - $soldeActuel = $object->getCpforUser($object->fk_user, $object->fk_type); - $newSolde = ($soldeActuel + $nbopenedday); - - // The modification is added to the LOG - $result1 = $object->addLogCP($user->id, $object->fk_user, $langs->transnoentitiesnoconv("HolidaysCancelation"), $newSolde, $object->fk_type); - - // Update of the balance - $result2 = $object->updateSoldeCP($object->fk_user, $newSolde, $object->fk_type); - - if ($result1 < 0 || $result2 < 0) { - $error++; - setEventMessages($langs->trans('ErrorCantDeleteCP').' '.$object->error, $object->errors, 'errors'); - } - } - - if (!$error) { - $db->commit(); - } else { - $db->rollback(); - } - - // If no SQL error, we redirect to the request form - if (!$error && $result > 0) { - // To - $destinataire = new User($db); - $destinataire->fetch($object->fk_user); - $emailTo = $destinataire->email; - - if (!$emailTo) { - header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); - exit; - } - - // From - $expediteur = new User($db); - $expediteur->fetch($object->fk_user_cancel); - //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email. - $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM; - - // Subject - $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM; - if (!empty($conf->global->MAIN_APPLICATION_TITLE)) { - $societeName = $conf->global->MAIN_APPLICATION_TITLE; - } - - $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysCanceled"); - - // Content - $message = "

".$langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",

\n"; - - $message .= "

".$langs->transnoentities("HolidaysCanceledBody", dol_print_date($object->date_debut, 'day'), dol_print_date($object->date_fin, 'day'))."

\n"; - - $link = dol_buildpath('/holiday/card.php', 3).'?id='.$object->id; - - $message .= "
    \n"; - $message .= "
  • ".$langs->transnoentitiesnoconv("ModifiedBy")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."
  • \n"; - $message .= "
  • ".$langs->transnoentitiesnoconv("Link").' : '.$link."
  • \n"; - $message .= "
\n"; - - $trackid = 'leav'.$object->id; - - $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 1, '', '', $trackid); - - // sending email - $result = $mail->sendfile(); - - if (!$result) { - setEventMessages($mail->error, $mail->errors, 'warnings'); - $action = ''; - } else { - header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); - exit; - } - } - } - } - } From 8c607b8101a580736259420a50694ecbeca4e5e6 Mon Sep 17 00:00:00 2001 From: jpb Date: Tue, 7 Jun 2022 19:20:53 +0200 Subject: [PATCH 04/16] add todo --- htdocs/holiday/card_group.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 06157ee3e94..a51f9c6ab20 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -638,12 +638,12 @@ if (is_object($db)) { $db->close(); } /** - * we send email to validator for current leave request (id) + * send email to validator for current leave represented by (id) * @param $id * @param $cancreate * @param $now * @param $autoValidation - * @return void + * @return string|void * @throws Exception */ function sendMail ($id, $cancreate, $now, $autoValidation){ @@ -670,6 +670,8 @@ function sendMail ($id, $cancreate, $now, $autoValidation){ $destinataire->fetch($object->fk_validator); $emailTo = $destinataire->email; + //@todo make object return errors + //@todo remove redisrection here ! if (!$emailTo) { dol_syslog("Expected validator has no email, so we redirect directly to finished page without sending email"); header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); @@ -736,17 +738,23 @@ function sendMail ($id, $cancreate, $now, $autoValidation){ setEventMessages($mail->error, $mail->errors, 'warnings'); $action = ''; } else { + //@todo make object return errors + //@todo remove redisrection here ! header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); exit; } } else { + //@todo make object return errors + //@todo remove redisrection here ! setEventMessages($object->error, $object->errors, 'errors'); $action = ''; } } }else{ + //@todo make object return errors + //@todo remove redisrection here ! setEventMessage($langs->trans('ErrorloadUserOnSendingMail'),'warning'); } - + return 'objerrors'; } From c87c6ad042b2881018a59fabed7f5a9a43da13a9 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Wed, 8 Jun 2022 07:26:00 +0000 Subject: [PATCH 05/16] Fixing style errors. --- htdocs/core/menus/standard/eldy.lib.php | 3 +- htdocs/holiday/card_group.php | 54 +++++++++++-------------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index 6427c0b07d7..3e0ac2d371d 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -2270,7 +2270,7 @@ function get_left_menu_hrm($mainmenu, &$newmenu, $usemenuhider = 1, $leftmenu = $newmenu->add("/holiday/list.php?mainmenu=hrm&leftmenu=holiday", $langs->trans("CPTitreMenu"), 0, $user->rights->holiday->read, '', $mainmenu, 'holiday', 0, '', '', '', img_picto('', 'holiday', 'class="pictofixedwidth"')); $newmenu->add("/holiday/card.php?mainmenu=hrm&leftmenu=holiday&action=create", $langs->trans("New"), 1, $user->rights->holiday->write, '', $mainmenu); - $newmenu->add("/holiday/card_group.php?mainmenu=hrm&leftmenu=holiday&action=request", $langs->trans("NewHolidayForGroup"), 1, ($user->rights->holiday->writeall && $user->rights->holiday->readall) , '', $mainmenu, 'holiday_sm'); + $newmenu->add("/holiday/card_group.php?mainmenu=hrm&leftmenu=holiday&action=request", $langs->trans("NewHolidayForGroup"), 1, ($user->rights->holiday->writeall && $user->rights->holiday->readall), '', $mainmenu, 'holiday_sm'); $newmenu->add("/holiday/list.php?mainmenu=hrm&leftmenu=holiday", $langs->trans("List"), 1, $user->rights->holiday->read, '', $mainmenu); if ($usemenuhider || empty($leftmenu) || $leftmenu == "holiday") { $newmenu->add("/holiday/list.php?search_status=1&mainmenu=hrm&leftmenu=holiday", $langs->trans("DraftCP"), 2, $user->rights->holiday->read, '', $mainmenu, 'holiday_sm'); @@ -2282,7 +2282,6 @@ function get_left_menu_hrm($mainmenu, &$newmenu, $usemenuhider = 1, $leftmenu = $newmenu->add("/holiday/define_holiday.php?mainmenu=hrm&action=request", $langs->trans("MenuConfCP"), 1, $user->rights->holiday->read, '', $mainmenu, 'holiday_sm'); $newmenu->add("/holiday/month_report.php?mainmenu=hrm&leftmenu=holiday", $langs->trans("MenuReportMonth"), 1, $user->rights->holiday->readall, '', $mainmenu, 'holiday_sm'); $newmenu->add("/holiday/view_log.php?mainmenu=hrm&leftmenu=holiday&action=request", $langs->trans("MenuLogCP"), 1, $user->rights->holiday->define_holiday, '', $mainmenu, 'holiday_sm'); - } // Trips and expenses (old module) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index a51f9c6ab20..a786191acd1 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -49,10 +49,10 @@ $id = GETPOST('id', 'int'); $ref = GETPOST('ref', 'alpha'); $fuserid = (GETPOST('fuserid', 'int') ?GETPOST('fuserid', 'int') : $user->id); $users = (GETPOST('users', 'array') ?GETPOST('users', 'array') : array($user->id)); -$groups = GETPOST('groups', 'array') ; +$groups = GETPOST('groups', 'array'); $socid = GETPOST('socid', 'int'); -$autoValidation = GETPOST('autoValidation','int'); -$AutoSendMail = GETPOST('AutoSendMail','int'); +$autoValidation = GETPOST('autoValidation', 'int'); +$AutoSendMail = GETPOST('AutoSendMail', 'int'); // Load translation files required by the page $langs->loadLangs(array("other", "holiday", "mails", "trips")); @@ -162,9 +162,8 @@ if (empty($reshook)) { } if (!$error) { - $users = GETPOST('users', 'array'); - $groups = GETPOST('groups', 'array') ; + $groups = GETPOST('groups', 'array'); $date_debut = dol_mktime(0, 0, 0, GETPOST('date_debut_month'), GETPOST('date_debut_day'), GETPOST('date_debut_year')); $date_fin = dol_mktime(0, 0, 0, GETPOST('date_fin_month'), GETPOST('date_fin_day'), GETPOST('date_fin_year')); @@ -209,7 +208,7 @@ if (empty($reshook)) { } } // If no groups and no users - if (empty($groups) && empty($users)){ + if (empty($groups) && empty($users)) { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("User")), null, 'errors'); setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Group")), null, 'errors'); $error++; @@ -259,7 +258,6 @@ if (empty($reshook)) { if (!$error) { - $TusersToProcess = array(); /** GROUPS */ @@ -290,10 +288,10 @@ if (empty($reshook)) { $userError = new User($db); $result = $userError->fetch($u); - if ($result){ - setEventMessages($langs->trans("UseralreadyCPexist",$userError->firstname . ' '. $userError->lastname), null, 'errors'); - }else{ - setEventMessages($langs->trans("ErrorUserFetch",$u), null, 'errors'); + if ($result) { + setEventMessages($langs->trans("UseralreadyCPexist", $userError->firstname . ' '. $userError->lastname), null, 'errors'); + } else { + setEventMessages($langs->trans("ErrorUserFetch", $u), null, 'errors'); } $error++; @@ -305,7 +303,6 @@ if (empty($reshook)) { $db->begin(); // non errors we can insert all foreach ($TusersToProcess as $u) { - $object = new Holiday($db); $object->fk_user = $u; $object->description = $description; @@ -320,30 +317,27 @@ if (empty($reshook)) { if ($result <= 0) { setEventMessages($object->error, $object->errors, 'errors'); $error++; - }else{ + } else { // AUTO APPROUVAL /VALIDATED //@TODO changer le nom si approuved / validated - if ($autoValidation){ + if ($autoValidation) { $htemp = new Holiday($db); - $htemp->fetch ($result); + $htemp->fetch($result); $htemp->statut = Holiday::STATUS_VALIDATED; $resultValidated = $htemp->update($approverid); - if ($resultValidated < 0 ){ + if ($resultValidated < 0 ) { setEventMessages($object->error, $object->errors, 'errors'); $error++; } // we can auto send mail if we are in auto validation behavior //@todo jquery disable if checkbox autovalidation unchecked - if ($AutoSendMail && !$error){ + if ($AutoSendMail && !$error) { // send a mail to the user sendMail($result, $cancreate, $now, $autoValidation); } } - - - } } } @@ -475,7 +469,7 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { $resql = $db->query($sql); $Tgroup = array(); - while ($obj = $db->fetch_object($resql)){ + while ($obj = $db->fetch_object($resql)) { $Tgroup[$obj->rowid] = $obj->nom; } print $form->multiselectarray('groups', $Tgroup, GETPOST('groups', 'array'), '', 0, 'quatrevingtpercent widthcentpercentminusx', 0, 0); @@ -493,15 +487,14 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { if ($cancreate && !$cancreateall) { - - }else{ + } else { //$sql .= ' AND u.fk_user = '.$user->id; //$sql .= ' OR u.rowid ='.$user->id; } $resql = $db->query($sql); - if ($resql){ - while ($obj = $db->fetch_object($resql)){ + if ($resql) { + while ($obj = $db->fetch_object($resql)) { $userlist[$obj->rowid] = $obj->firstname . ' '. $obj->lastname; } } @@ -646,7 +639,8 @@ if (is_object($db)) { * @return string|void * @throws Exception */ -function sendMail ($id, $cancreate, $now, $autoValidation){ +function sendMail($id, $cancreate, $now, $autoValidation) +{ global $db, $user, $conf, $langs; @@ -654,7 +648,7 @@ function sendMail ($id, $cancreate, $now, $autoValidation){ $result = $object->fetch($id); - if ($result){ + if ($result) { // If draft and owner of leave if ($object->statut == Holiday::STATUS_DRAFT && $cancreate) { $object->oldcopy = dol_clone($object); @@ -750,11 +744,11 @@ function sendMail ($id, $cancreate, $now, $autoValidation){ $action = ''; } } - }else{ + } else { //@todo make object return errors //@todo remove redisrection here ! - setEventMessage($langs->trans('ErrorloadUserOnSendingMail'),'warning'); + setEventMessage($langs->trans('ErrorloadUserOnSendingMail'), 'warning'); } - return 'objerrors'; + return 'objerrors'; } From 7df97d1f3dad1ec19fa5b0dcd3b7220af1fe04c0 Mon Sep 17 00:00:00 2001 From: jpb Date: Wed, 8 Jun 2022 10:09:30 +0200 Subject: [PATCH 06/16] rename autovalidation --- htdocs/holiday/card_group.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index a51f9c6ab20..49605a69511 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -591,9 +591,9 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { print ''; print ''; - //auto APPROVAL ON CREATE + //auto validation ON CREATE print ''.$langs->trans("AutoApprovalOnCreate").''; - print ''; + print ''; print ''."\n"; From bf10e8f68771f9e125b76d09a248bd6a6244b98f Mon Sep 17 00:00:00 2001 From: jpb Date: Wed, 8 Jun 2022 17:08:05 +0200 Subject: [PATCH 07/16] add js behaviour on auvalidation and automail remove todo --- htdocs/holiday/card_group.php | 95 +++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 37 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 8ffe0f3e1fc..898d9f53be8 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -318,8 +318,8 @@ if (empty($reshook)) { setEventMessages($object->error, $object->errors, 'errors'); $error++; } else { - // AUTO APPROUVAL /VALIDATED - //@TODO changer le nom si approuved / validated + + //@TODO changer le nom si validated if ($autoValidation) { $htemp = new Holiday($db); $htemp->fetch($result); @@ -332,10 +332,11 @@ if (empty($reshook)) { $error++; } // we can auto send mail if we are in auto validation behavior - //@todo jquery disable if checkbox autovalidation unchecked + if ($AutoSendMail && !$error) { // send a mail to the user - sendMail($result, $cancreate, $now, $autoValidation); + $returnSendMail = sendMail($result, $cancreate, $now, $autoValidation); + if (!empty($returnSendMail->msg)) setEventMessage($returnSendMail->msg,$returnSendMail->style); } } } @@ -413,6 +414,13 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { print ''."\n"; @@ -448,12 +465,8 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { print ''."\n"; print ''."\n"; - - print dol_get_fiche_head(); - //print ''.$langs->trans('DelayToRequestCP',$object->getConfCP('delayForRequest')).'

'; - print ''; print ''; @@ -485,13 +498,6 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { $sql .= ' WHERE 1=1 '; $sql .= !empty($morefilter) ? $morefilter : ''; - - if ($cancreate && !$cancreateall) { - } else { - //$sql .= ' AND u.fk_user = '.$user->id; - //$sql .= ' OR u.rowid ='.$user->id; - } - $resql = $db->query($sql); if ($resql) { while ($obj = $db->fetch_object($resql)) { @@ -586,13 +592,13 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { //auto validation ON CREATE print ''."\n"; //no auto SEND MAIL print ''."\n"; // Description @@ -641,6 +647,11 @@ if (is_object($db)) { */ function sendMail($id, $cancreate, $now, $autoValidation) { + $objStd = new stdClass(); + $objStd->msg = ''; + $objStd->status = 'success'; + $objStd->error = 0; + $objStd->style = ''; global $db, $user, $conf, $langs; @@ -657,19 +668,21 @@ function sendMail($id, $cancreate, $now, $autoValidation) $verif = $object->validate($user); - // If no SQL error, we redirect to the request form if ($verif > 0) { // To $destinataire = new User($db); $destinataire->fetch($object->fk_validator); $emailTo = $destinataire->email; - //@todo make object return errors - //@todo remove redisrection here ! + if (!$emailTo) { dol_syslog("Expected validator has no email, so we redirect directly to finished page without sending email"); - header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); - exit; + + $objStd->error++; + $objStd->msg = $langs->trans('ErroremailTo'); + $objStd->status = 'error'; + $objStd->style="warnings"; + return $objStd; } // From @@ -729,26 +742,34 @@ function sendMail($id, $cancreate, $now, $autoValidation) $result = $mail->sendfile(); if (!$result) { - setEventMessages($mail->error, $mail->errors, 'warnings'); - $action = ''; + + $objStd->error++; + $objStd->msg = $langs->trans('ErroreSendmail'); + $objStd->style="warnings"; + $objStd->status = 'error'; } else { - //@todo make object return errors - //@todo remove redisrection here ! - header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id); - exit; + + $objStd->msg = $langs->trans('mailSended'); } + + return $objStd; } else { - //@todo make object return errors - //@todo remove redisrection here ! - setEventMessages($object->error, $object->errors, 'errors'); - $action = ''; + + $objStd->error++; + $objStd->msg = $langs->trans('ErroreVerif'); + $objStd->status = 'error'; + $objStd->style="errors"; + return $objStd; } } } else { - //@todo make object return errors - //@todo remove redisrection here ! - setEventMessage($langs->trans('ErrorloadUserOnSendingMail'), 'warning'); + + $objStd->error++; + $objStd->msg = $langs->trans('ErrorloadUserOnSendingMail'); + $objStd->status = 'error'; + $objStd->style="warnings"; + return $objStd; } - return 'objerrors'; + return $objStd; } From 189d918181f906e0caf7083d287481079f9b8e94 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Wed, 8 Jun 2022 15:14:18 +0000 Subject: [PATCH 08/16] Fixing style errors. --- htdocs/holiday/card_group.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 898d9f53be8..19f5e1a5fd8 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -318,7 +318,6 @@ if (empty($reshook)) { setEventMessages($object->error, $object->errors, 'errors'); $error++; } else { - //@TODO changer le nom si validated if ($autoValidation) { $htemp = new Holiday($db); @@ -336,7 +335,7 @@ if (empty($reshook)) { if ($AutoSendMail && !$error) { // send a mail to the user $returnSendMail = sendMail($result, $cancreate, $now, $autoValidation); - if (!empty($returnSendMail->msg)) setEventMessage($returnSendMail->msg,$returnSendMail->style); + if (!empty($returnSendMail->msg)) setEventMessage($returnSendMail->msg, $returnSendMail->style); } } } @@ -742,19 +741,16 @@ function sendMail($id, $cancreate, $now, $autoValidation) $result = $mail->sendfile(); if (!$result) { - $objStd->error++; $objStd->msg = $langs->trans('ErroreSendmail'); $objStd->style="warnings"; $objStd->status = 'error'; } else { - $objStd->msg = $langs->trans('mailSended'); } return $objStd; } else { - $objStd->error++; $objStd->msg = $langs->trans('ErroreVerif'); $objStd->status = 'error'; @@ -763,7 +759,6 @@ function sendMail($id, $cancreate, $now, $autoValidation) } } } else { - $objStd->error++; $objStd->msg = $langs->trans('ErrorloadUserOnSendingMail'); $objStd->status = 'error'; From 19cb81e3ea3247ded71b35c58bbab6e4e35d1070 Mon Sep 17 00:00:00 2001 From: jpb Date: Sat, 11 Jun 2022 10:16:04 +0200 Subject: [PATCH 09/16] change filter array method on users selection from groups --- htdocs/holiday/card_group.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 898d9f53be8..07beaeeb678 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -259,7 +259,7 @@ if (empty($reshook)) { if (!$error) { $TusersToProcess = array(); - + // usergroup select /** GROUPS */ $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from ' . MAIN_DB_PREFIX . 'user as u'; $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'usergroup_user as ug on ug.fk_user = u.rowid '; @@ -268,15 +268,13 @@ if (empty($reshook)) { if ($resql) { while ($obj = $db->fetch_object($resql)) { - if (!array_key_exists($obj->rowid, $TusersToProcess)) - $TusersToProcess[] = $obj->rowid; + $TusersToProcess[$obj->rowid] = $obj->rowid; } } /** USERS */ if (is_array($users) && count($users) > 0) { foreach ($users as $u) { - if (!array_key_exists($u, $TusersToProcess)) - $TusersToProcess[] = $u; + $TusersToProcess[$u] = $u; } } foreach ($TusersToProcess as $u) { From 1af3b6ccbddd6ba5fdc3c12ba36babb281ebcc94 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sat, 11 Jun 2022 08:23:08 +0000 Subject: [PATCH 10/16] Fixing style errors. --- htdocs/holiday/card_group.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 93560b9bb7c..c52ea39681d 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -259,7 +259,7 @@ if (empty($reshook)) { if (!$error) { $TusersToProcess = array(); - // usergroup select + // usergroup select /** GROUPS */ $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from ' . MAIN_DB_PREFIX . 'user as u'; $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'usergroup_user as ug on ug.fk_user = u.rowid '; From 74d23cccd8c6e84efb87d4be862c82bd8e8b91ec Mon Sep 17 00:00:00 2001 From: jpb Date: Sat, 11 Jun 2022 14:01:54 +0200 Subject: [PATCH 11/16] remove seteventMessage * precheck automail on selected auto validation --- htdocs/holiday/card_group.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 93560b9bb7c..57fe16ea23e 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -281,7 +281,7 @@ if (empty($reshook)) { // Check if there is already holiday for this period pour chaque user $verifCP = $object->verifDateHolidayCP($u, $date_debut, $date_fin, $halfday); if (!$verifCP) { - setEventMessages($langs->trans("alreadyCPexist"), null, 'errors'); + //setEventMessages($langs->trans("alreadyCPexist"), null, 'errors'); $userError = new User($db); $result = $userError->fetch($u); @@ -342,7 +342,7 @@ if (empty($reshook)) { // If no SQL error we redirect to the request card if (!$error) { $db->commit(); - header('Location: '.DOL_URL_ROOT.'/holiday/list.php?restore_lastsearch_values=1'); + header('Location: '.DOL_URL_ROOT.'/holiday/list.php'); exit; } else { $db->rollback(); @@ -414,10 +414,27 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { if( $("input[name=autoValidation]").is(":checked") ){ $("#AutoSendMail").prop("disabled", false); + $("#AutoSendMail").prop("checked", true); + } else { $("#AutoSendMail").prop("disabled", true); + $("#AutoSendMail").prop("checked", false); } + $("input[name=autoValidation]").click( function(e) { + console.log("chang") + + if( $("input[name=autoValidation]").is(":checked") ){ + $("#AutoSendMail").prop("disabled", false); + $("#AutoSendMail").prop("checked", true); + } else { + $("#AutoSendMail").prop("disabled", true); + $("#AutoSendMail").prop("checked", false); + } + }); + + + $("input.button-save").click("submit", function(e) { console.log("Call valider()"); if (document.demandeCP.date_debut_.value != "") @@ -588,7 +605,7 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { print ''; //auto validation ON CREATE - print ''."\n"; @@ -658,10 +675,10 @@ function sendMail($id, $cancreate, $now, $autoValidation) if ($result) { // If draft and owner of leave - if ($object->statut == Holiday::STATUS_DRAFT && $cancreate) { + if ($object->statut == Holiday::STATUS_VALIDATED && $cancreate) { $object->oldcopy = dol_clone($object); - if ($autoValidation) $object->statut = Holiday::STATUS_VALIDATED; + //if ($autoValidation) $object->statut = Holiday::STATUS_VALIDATED; $verif = $object->validate($user); From 0e72085be681eaf485080490fddea2cc76973007 Mon Sep 17 00:00:00 2001 From: jpb Date: Mon, 13 Jun 2022 08:36:17 +0200 Subject: [PATCH 12/16] add trads on form fields --- htdocs/holiday/card_group.php | 11 +++++++---- htdocs/langs/en_US/holiday.lang | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 9655bb24320..5dee6e54f6c 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -209,8 +209,9 @@ if (empty($reshook)) { } // If no groups and no users if (empty($groups) && empty($users)) { - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("User")), null, 'errors'); - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Group")), null, 'errors'); + setEventMessages($langs->trans("ErrorFieldRequiredUserOrGroup"), null, 'errors'); + //setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("UserOrGroup")), null, 'errors'); + //setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Group")), null, 'errors'); $error++; $action = 'create'; } @@ -486,7 +487,8 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { // groupe print ''; - print ''; @@ -505,7 +507,8 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { // users print ''; - print ''; + print '
'.$langs->trans("AutoApprovalOnCreate").''; - print ''; + print ''; print '
'.$langs->trans("AutoSendMail").''; - print ''; + print ''; print '
'.$langs->trans("AutoApprovalOnCreate").''; + print '
'.$langs->trans("AutoValidationOnCreate").''; print ''; print '
'.$langs->trans("groups"); + print ''; + print $form->textwithpicto($langs->trans("groups"), $langs->trans("fusionGroupsUsers")); print '
'.$langs->trans("users").''; + print $form->textwithpicto($langs->trans("users"), $langs->trans("fusionGroupsUsers")); print ''; $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from '.MAIN_DB_PREFIX.'user as u'; diff --git a/htdocs/langs/en_US/holiday.lang b/htdocs/langs/en_US/holiday.lang index 2005d7e613c..36f1e839e67 100644 --- a/htdocs/langs/en_US/holiday.lang +++ b/htdocs/langs/en_US/holiday.lang @@ -80,6 +80,8 @@ MotifCP=Reason UserCP=User ErrorAddEventToUserCP=An error occurred while adding the exceptional leave. AddEventToUserOkCP=The addition of the exceptional leave has been completed. +ErrorFieldRequiredUserOrGroup=The "group" field or the "user" field must be filled in +fusionGroupsUsers=The groups field and the user field will be merged MenuLogCP=View change logs LogCP=Log of all updates made to "Balance of Leave" ActionByCP=Updated by From 2209d31b2c6666cdd7917c48cedf80f5cf7e5819 Mon Sep 17 00:00:00 2001 From: jpb Date: Mon, 13 Jun 2022 08:51:49 +0200 Subject: [PATCH 13/16] add comment on function --- htdocs/holiday/card_group.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 5dee6e54f6c..f244c6374e5 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -655,11 +655,11 @@ if (is_object($db)) { } /** * send email to validator for current leave represented by (id) - * @param $id - * @param $cancreate - * @param $now - * @param $autoValidation - * @return string|void + * @param $id validator for current leave represented by (id) + * @param $cancreate flag for user right + * @param $now date + * @param $autoValidation boolean flag on autovalidation + * @return stdClass * @throws Exception */ function sendMail($id, $cancreate, $now, $autoValidation) From af0846e2f7b22e129628ee699b9dcb1ab6e00ccc Mon Sep 17 00:00:00 2001 From: jpb Date: Mon, 13 Jun 2022 09:09:19 +0200 Subject: [PATCH 14/16] add sanitize to sql --- htdocs/holiday/card_group.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index f244c6374e5..8de636da6c8 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -264,7 +264,7 @@ if (empty($reshook)) { /** GROUPS */ $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from ' . MAIN_DB_PREFIX . 'user as u'; $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'usergroup_user as ug on ug.fk_user = u.rowid '; - $sql .= ' WHERE fk_usergroup in (' . implode(',', $groups) . ')'; + $sql .= ' WHERE fk_usergroup in (' .$this->db->sanitize(implode(',', $groups)) . ')'; $resql = $db->query($sql); if ($resql) { @@ -423,7 +423,7 @@ if ((empty($id) && empty($ref)) || $action == 'create' || $action == 'add') { } $("input[name=autoValidation]").click( function(e) { - console.log("chang") + if( $("input[name=autoValidation]").is(":checked") ){ $("#AutoSendMail").prop("disabled", false); From d7ca52f4ef996b9711a321823da0e19c11730d0d Mon Sep 17 00:00:00 2001 From: jpb Date: Mon, 13 Jun 2022 09:17:19 +0200 Subject: [PATCH 15/16] remove $this on sanitize --- htdocs/holiday/card_group.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index 8de636da6c8..b63a3ae657b 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -264,7 +264,7 @@ if (empty($reshook)) { /** GROUPS */ $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from ' . MAIN_DB_PREFIX . 'user as u'; $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'usergroup_user as ug on ug.fk_user = u.rowid '; - $sql .= ' WHERE fk_usergroup in (' .$this->db->sanitize(implode(',', $groups)) . ')'; + $sql .= ' WHERE fk_usergroup in (' .$db->sanitize(implode(',', $groups)) . ')'; $resql = $db->query($sql); if ($resql) { From 09c5b8be79204817c5fc359df27b7690a6de7f19 Mon Sep 17 00:00:00 2001 From: jpb Date: Mon, 13 Jun 2022 10:38:49 +0200 Subject: [PATCH 16/16] add comment --- htdocs/holiday/card_group.php | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/holiday/card_group.php b/htdocs/holiday/card_group.php index b63a3ae657b..5e571f077c8 100644 --- a/htdocs/holiday/card_group.php +++ b/htdocs/holiday/card_group.php @@ -261,6 +261,7 @@ if (empty($reshook)) { if (!$error) { $TusersToProcess = array(); // usergroup select + // better perf on single sql /** GROUPS */ $sql = ' SELECT DISTINCT u.rowid,u.lastname,u.firstname from ' . MAIN_DB_PREFIX . 'user as u'; $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'usergroup_user as ug on ug.fk_user = u.rowid ';