From 1cef6d31e89108f5cb96596208657befebf7ee9a Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Mon, 2 Aug 2021 15:45:55 +0200 Subject: [PATCH] Close #18302 : Mass action leave requests --- htdocs/core/actions_massactions.inc.php | 120 ++++++++++++++++++++++++ htdocs/core/tpl/massactions_pre.tpl.php | 4 + htdocs/holiday/list.php | 4 + htdocs/langs/en_US/main.lang | 4 + htdocs/langs/fr_FR/main.lang | 4 + 5 files changed, 136 insertions(+) diff --git a/htdocs/core/actions_massactions.inc.php b/htdocs/core/actions_massactions.inc.php index 8d70f7c3042..c986a2e5ac1 100644 --- a/htdocs/core/actions_massactions.inc.php +++ b/htdocs/core/actions_massactions.inc.php @@ -1544,6 +1544,126 @@ if (!$error && ($massaction == 'disable' || ($action == 'disable' && $confirm == } } +if (!$error && ($massaction == 'approveleave' || ($action == 'approveleave' && $confirm == 'yes')) && $permissiontoapprove) { + $db->begin(); + + $objecttmp = new $objectclass($db); + $nbok = 0; + foreach ($toselect as $toselectid) { + $result = $objecttmp->fetch($toselectid); + if ($result>0) { + if ($objecttmp->statut == Holiday::STATUS_VALIDATED && $user->id == $objecttmp->fk_validator) { + $objecttmp->oldcopy = dol_clone($objecttmp); + + $objecttmp->date_valid = dol_now(); + $objecttmp->fk_user_valid = $user->id; + $objecttmp->statut = Holiday::STATUS_APPROVED; + + $db->begin(); + + $verif = $objecttmp->approve($user); + if ($verif <= 0) { + setEventMessages($objecttmp->error, $objecttmp->errors, 'errors'); + $error++; + } + + // If no SQL error, we redirect to the request form + if (!$error) { + // Calculcate number of days consummed + $nbopenedday = num_open_day($objecttmp->date_debut_gmt, $objecttmp->date_fin_gmt, 0, 1, $objecttmp->halfday); + $soldeActuel = $objecttmp->getCpforUser($objecttmp->fk_user, $objecttmp->fk_type); + $newSolde = ($soldeActuel - $nbopenedday); + + // The modification is added to the LOG + $result = $objecttmp->addLogCP($user->id, $objecttmp->fk_user, $langs->transnoentitiesnoconv("Holidays"), $newSolde, $objecttmp->fk_type); + if ($result < 0) { + $error++; + setEventMessages(null, $objecttmp->errors, 'errors'); + } + + // Update balance + $result = $objecttmp->updateSoldeCP($objecttmp->fk_user, $newSolde, $objecttmp->fk_type); + if ($result < 0) { + $error++; + setEventMessages(null, $objecttmp->errors, 'errors'); + } + } + + if (!$error) { + // To + $destinataire = new User($db); + $destinataire->fetch($objecttmp->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($objecttmp->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 .= "\n"; + + $message .= $langs->transnoentities("HolidaysValidatedBody", dol_print_date($objecttmp->date_debut, 'day'), dol_print_date($objecttmp->date_fin, 'day'))."\n"; + + $message .= "- ".$langs->transnoentitiesnoconv("ValidatedBy")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."\n"; + + $message .= "- ".$langs->transnoentitiesnoconv("Link")." : ".$dolibarr_main_url_root."/holiday/card.php?id=".$objecttmp->id."\n\n"; + $message .= "\n"; + + $trackid = 'leav'.$objecttmp->id; + + $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 0, '', '', $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(); + $nbok++; + } else { + $db->rollback(); + $action = ''; + } + } + } else { + setEventMessages($objecttmp->error, $objecttmp->errors, 'errors'); + $error++; + break; + } + } + + if (!$error) { + if ($nbok > 1) { + setEventMessages($langs->trans("RecordsApproved", $nbok), null, 'mesgs'); + } else { + setEventMessages($langs->trans("RecordAproved"), null, 'mesgs'); + } + $db->commit(); + } else { + $db->rollback(); + } +} + $parameters['toselect'] = $toselect; $parameters['uploaddir'] = $uploaddir; $parameters['massaction'] = $massaction; diff --git a/htdocs/core/tpl/massactions_pre.tpl.php b/htdocs/core/tpl/massactions_pre.tpl.php index e2e56e1a060..4d715ed8797 100644 --- a/htdocs/core/tpl/massactions_pre.tpl.php +++ b/htdocs/core/tpl/massactions_pre.tpl.php @@ -202,6 +202,10 @@ if ($massaction == 'predisable') { print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassDisabling"), $langs->trans("ConfirmMassDisablingQuestion", count($toselect)), "disable", null, '', 0, 200, 500, 1); } +if ($massaction == 'preapproveleave') { + print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassLeaveApproval"), $langs->trans("ConfirmMassLeaveApprovalQuestion", count($toselect)), "approveleave", null, '', 0, 200, 500, 1); +} + // Allow Pre-Mass-Action hook (eg for confirmation dialog) $parameters = array( 'toselect' => $toselect, diff --git a/htdocs/holiday/list.php b/htdocs/holiday/list.php index e7a09abea6e..334f68da404 100644 --- a/htdocs/holiday/list.php +++ b/htdocs/holiday/list.php @@ -213,6 +213,7 @@ if (empty($reshook)) { $objectlabel = 'Holiday'; $permissiontoread = $user->rights->holiday->read; $permissiontodelete = $user->rights->holiday->delete; + $permissiontoapprove = $user->rights->holiday->approve; $uploaddir = $conf->holiday->dir_output; include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php'; } @@ -441,6 +442,9 @@ if ($resql) { if (!empty($user->rights->holiday->delete)) { $arrayofmassactions['predelete'] = img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete"); } + if (!empty($user->rights->holiday->approve)) { + $arrayofmassactions['preapproveleave'] = img_picto('', 'check', 'class="pictofixedwidth"').$langs->trans("Approve"); + } if (in_array($massaction, array('presend', 'predelete'))) { $arrayofmassactions = array(); } diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang index 6c1ef6aa659..569c709e169 100644 --- a/htdocs/langs/en_US/main.lang +++ b/htdocs/langs/en_US/main.lang @@ -1149,3 +1149,7 @@ RecordEnabled=Record enabled RecordDisabled=Record disabled Forthcoming=Forthcoming Currently=Currently +ConfirmMassLeaveApprovalQuestion=Are you sure you want to approve the %s selected record(s)? +ConfirmMassLeaveApproval=Mass leave approval confirmation +RecordAproved=Record approved +RecordsApproved=%s Record(s) approved diff --git a/htdocs/langs/fr_FR/main.lang b/htdocs/langs/fr_FR/main.lang index 2453fab1e16..9aecafe3f34 100644 --- a/htdocs/langs/fr_FR/main.lang +++ b/htdocs/langs/fr_FR/main.lang @@ -1147,3 +1147,7 @@ RecordsEnabled=%s enregistrement(s) activé(s) RecordsDisabled=%s enregistrement(s) désactivé(s) RecordEnabled=Enregistrement activé RecordDisabled=Enregistrement désactivé +ConfirmMassLeaveApprovalQuestion=Êtes-vous sur de vouloir approuver les %s enregistrement(s) sélectionné(s) ? +ConfirmMassLeaveApproval=Confirmation d'approbation de congé en masse +RecordAproved=Enregistrement approuvé +RecordsApproved=%s enregistrement(s) approuvé(s)