Ajout de la suppression des demandes de congé #69

Open
fatima wants to merge 2 commits from feature/suppression_demande_conge into main
4 changed files with 121 additions and 1 deletions
Showing only changes of commit 65497cd669 - Show all commits

View File

@@ -2,6 +2,34 @@ const $ = (element) => document.getElementById(element);
const url_liste_conge_attente = $("liste-demande-conges").dataset.url
function supprimerDemandeConge(congeId) {
if (!window.confirm("Voulez-vous vraiment supprimer cette demande de congé en attente ?")) {
return;
}
const form = $("form-detail-conge");
const csrftoken = new FormData(form).get("csrfmiddlewaretoken");
const url = $("bouton-supprimer-demande-conge").dataset.url.replace(
"/0/",
`/${congeId}/`
);
fetch(url, {
method: "POST",
headers: {"X-CSRFToken": csrftoken},
})
.then(response => response.json().then(data => ({response, data})))
.then(({response, data}) => {
window.alert(data.message);
if (response.ok && data.success) {
window.location.reload();
}
})
.catch(() => {
window.alert("La suppression a échoué. Veuillez réessayer.");
});
}
const tableau_liste_demande_conge = new Tabulator("#liste-demande-conges", {
columns: [
{"title": "Nom et Prénom", "field": "prenom_nom"},
@@ -21,12 +49,31 @@ const tableau_liste_demande_conge = new Tabulator("#liste-demande-conges", {
{"title": "Validation par supérieur hiérarchique", "field": "validation_direction", formatter:"tickCross", formatterParams :{
allowEmpty : true ,
}},
{
title: "Action",
hozAlign: "center",
formatter: (cell) => {
const data = cell.getRow().getData();
if (!data.est_proprietaire || !data.en_attente) {
return "";
}
return '<button type="button" class="btn btn-danger btn-sm">Supprimer</button>';
},
cellClick: (event, cell) => {
const data = cell.getRow().getData();
if (data.est_proprietaire && data.en_attente) {
event.stopPropagation();
supprimerDemandeConge(data.id);
}
},
},
],
pagination: true,
paginationSize: 5,
})
const bouton_demande_conges = $("bouton-demande-conge");
const bouton_supprimer_conge = $("bouton-supprimer-demande-conge");
bouton_demande_conges.addEventListener("click", (e) => {
var modalDemandeConge = new bootstrap.Modal(document.getElementById('modalDemandeConge'));
@@ -44,6 +91,11 @@ tableau_liste_demande_conge.on("rowClick", function(row, rowData) {
$("nombre_jours").value = data.nombre_jours;
$("solde_restant").value = data.solde_conge;
$("motif_refus").value = data.motif_refus;
bouton_supprimer_conge.classList.toggle(
"d-none",
!(data.est_proprietaire && data.en_attente)
);
bouton_supprimer_conge.dataset.congeId = data.id;
if($("validation_hierarchique_valide") & $("validation_hierarchique_refuse")){
$("validation_hierarchique_valide").checked = data.validation_hierarchique === true;
@@ -59,6 +111,10 @@ tableau_liste_demande_conge.on("rowClick", function(row, rowData) {
modal.show();
});
bouton_supprimer_conge.addEventListener("click", () => {
supprimerDemandeConge(bouton_supprimer_conge.dataset.congeId);
});
fetch(url_liste_conge_attente)
.then(response => response.json())
.then(data => {

View File

@@ -83,6 +83,13 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
{% if conge.validation_hierarchique == None and conge.validation_direction == None%}
<button type="button" class="btn btn-danger " id="bouton-supprimer-demande-conge"
data-url="{% url 'gestion_conges:supprimer-demande-conge' 0 %}"
>
Supprimer ma demande
</button>
{% endif %}
{% if employe_est_il_chef or membre_de_la_direction or est_chef_projet %}
<button type="button" class="btn btn-success" id="bouton-enregistrer-detail-conge">Enregistrer</button>
{% endif %}

View File

@@ -24,4 +24,9 @@ urlpatterns = [
views.validation_de_conge,
name='validation-des-conges'
),
path(
'demande/<int:conge_id>/supprimer/',
views.supprimer_demande_conge,
name='supprimer-demande-conge',
),
]

View File

@@ -235,3 +235,55 @@ def validation_de_conge(request):
conge.save()
return JsonResponse({"message": "La décision a été enregistrée avec succès."})
def _conge_est_en_attente(conge):
return (
conge.validation_hierarchique is not False
and conge.validation_direction is not False
and not (
conge.validation_hierarchique is True
and conge.validation_direction is True
)
)
@login_required
def supprimer_demande_conge(request, conge_id):
if request.method != "POST":
return JsonResponse(
{"success": False, "message": "Méthode non autorisée."},
status=405,
)
try:
employe = Employe.objects.get(user=request.user)
except Employe.DoesNotExist:
return JsonResponse(
{"success": False, "message": "Profil employé introuvable."},
status=403,
)
try:
conge = Conge.objects.get(pk=conge_id, employe=employe)
except Conge.DoesNotExist:
return JsonResponse(
{"success": False, "message": "Demande introuvable ou accès non autorisé."},
status=404,
)
if not _conge_est_en_attente(conge):
return JsonResponse(
{
"success": False,
"message": "Seules les demandes en attente peuvent être supprimées.",
},
status=400,
)
conge.delete()
return JsonResponse(
{"success": True, "message": "Votre demande de congé en attente a été supprimée."}
)