FIX Transaction on leave approval and decrease ko if setup not complete

This commit is contained in:
Laurent Destailleur 2019-02-08 14:47:03 +01:00
parent 1611c613f5
commit 2e1201b7b0
2 changed files with 125 additions and 95 deletions

View File

@ -64,6 +64,7 @@ if (! empty($user->rights->holiday->delete)) $candelete=1;
$morefilter = 'AND employee = 1';
if (! empty($conf->global->HOLIDAY_FOR_NON_SALARIES_TOO)) $morefilter = '';
$error = 0;
/*
* Actions
@ -431,7 +432,7 @@ if ($action == 'confirm_send')
}
// Si Validation de la demande
// Approve leave request
if ($action == 'confirm_valid')
{
$object = new Holiday($db);
@ -446,25 +447,36 @@ if ($action == 'confirm_valid')
$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++;
}
// Si pas d'erreur SQL on redirige vers la fiche de la demande
if ($verif > 0)
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 * $object->getConfCP('nbHolidayDeducted'));
$newSolde = ($soldeActuel - $nbopenedday);
// On ajoute la modification dans le LOG
$result=$object->addLogCP($user->id, $object->fk_user, $langs->transnoentitiesnoconv("Holidays"), $newSolde, $object->fk_type);
if ($result<0) {
if ($result < 0)
{
$error++;
setEventMessages(null, $object->errors,'errors');
}
// Mise à jour du solde
//Update balance
$result=$object->updateSoldeCP($object->fk_user, $newSolde, $object->fk_type);
if ($result<0) {
if ($result < 0)
{
$error++;
setEventMessages(null, $object->errors,'errors');
}
@ -476,10 +488,9 @@ if ($action == 'confirm_valid')
if (!$emailTo)
{
dol_syslog("User that request leave has no email, so we redirect directly to finished page without sending email");
header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
exit;
}
else
{
// From
$expediteur = new User($db);
$expediteur->fetch($object->fk_validator);
@ -510,18 +521,22 @@ if ($action == 'confirm_valid')
if (!$result)
{
setEventMessages($mail->error, $mail->errors, 'warnings');
setEventMessages($mail->error, $mail->errors, 'warnings'); // Show error, but do no make rollback, so $error is not set to 1
$action='';
}
else
}
}
if (! $error)
{
$db->commit();
header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
exit;
}
}
else
{
setEventMessages($object->error, $object->errors, 'errors');
$db->rollback();
$action='';
}
}
@ -542,10 +557,17 @@ if ($action == 'confirm_refuse' && GETPOST('confirm','alpha') == 'yes')
$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');
}
// Si pas d'erreur SQL on redirige vers la fiche de la demande
if ($verif > 0)
if (! $error)
{
// To
$destinataire = new User($db);
@ -554,10 +576,10 @@ if ($action == 'confirm_refuse' && GETPOST('confirm','alpha') == 'yes')
if (!$emailTo)
{
header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
exit;
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);
@ -587,20 +609,28 @@ if ($action == 'confirm_refuse' && GETPOST('confirm','alpha') == 'yes')
// Envoi du mail
$result=$mail->sendfile();
if (!$result)
if (! $result)
{
setEventMessages($mail->error, $mail->errors, 'warnings');
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
{
setEventMessages($object->error, $object->errors, 'errors');
$db->rollback();
$action='';
}
}
@ -642,7 +672,7 @@ if ($action == 'confirm_draft' && GETPOST('confirm') == 'yes')
}
}
// Si Validation de la demande
// Si confirmation of cancellation
if ($action == 'confirm_cancel' && GETPOST('confirm') == 'yes')
{
$error = 0;
@ -662,13 +692,13 @@ if ($action == 'confirm_cancel' && GETPOST('confirm') == 'yes')
$result = $object->update($user);
if ($result >= 0 && $oldstatus == Holiday::STATUS_APPROVED) // holiday was already validated, status 3, so we must increase back sold
if ($result >= 0 && $oldstatus == Holiday::STATUS_APPROVED) // holiday was already validated, status 3, so we must increase back the balance
{
// 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 * $object->getConfCP('nbHolidayDeducted'));
$newSolde = ($soldeActuel + $nbopenedday);
// On ajoute la modification dans le LOG
$result1=$object->addLogCP($user->id, $object->fk_user, $langs->transnoentitiesnoconv("HolidaysCancelation"), $newSolde, $object->fk_type);

View File

@ -697,7 +697,7 @@ class Holiday extends CommonObject
} else {
$error++;
}
$sql.= " ref = '".$num."'";
$sql.= " ref = '".$this->db->escape($num)."'";
$sql.= " WHERE rowid= ".$this->id;
$this->db->begin();
@ -1493,10 +1493,10 @@ class Holiday extends CommonObject
else
{
// Mise à jour pour un utilisateur
$nbHoliday = price2num($nbHoliday,5);
$nbHoliday = price2num($nbHoliday, 5);
$sql = "SELECT nb_holiday FROM ".MAIN_DB_PREFIX."holiday_users";
$sql.= " WHERE fk_user = '".$userID."' AND fk_type = ".$fk_type;
$sql.= " WHERE fk_user = ".(int) $userID." AND fk_type = ".(int) $fk_type;
$resql = $this->db->query($sql);
if ($resql)
{
@ -1507,7 +1507,7 @@ class Holiday extends CommonObject
// Update for user
$sql = "UPDATE ".MAIN_DB_PREFIX."holiday_users SET";
$sql.= " nb_holiday = ".$nbHoliday;
$sql.= " WHERE fk_user = '".$userID."' AND fk_type = ".$fk_type;
$sql.= " WHERE fk_user = ".(int) $userID." AND fk_type = ".(int) $fk_type;
$result = $this->db->query($sql);
if (! $result)
{
@ -1520,7 +1520,7 @@ class Holiday extends CommonObject
// Insert for user
$sql = "INSERT INTO ".MAIN_DB_PREFIX."holiday_users(nb_holiday, fk_user, fk_type) VALUES (";
$sql.= $nbHoliday;
$sql.= ", '".$userID."', ".$fk_type.")";
$sql.= ", ".(int) $userID.", ".(int) $fk_type.")";
$result = $this->db->query($sql);
if (! $result)
{