diff --git a/gestion_employe/static/gestion_employe/js/index.js b/gestion_employe/static/gestion_employe/js/index.js index 9c31f13..31bf495 100644 --- a/gestion_employe/static/gestion_employe/js/index.js +++ b/gestion_employe/static/gestion_employe/js/index.js @@ -29,6 +29,7 @@ tableau_liste_employe.on("rowClick", function (row, rowData) { const data = rowData.getData(); document.getElementById('detail-id').value = data.id; + document.querySelector("#modalContratsEmploye").dataset.employeId = data.id; document.getElementById('detail-matricule').value = data.matricule; document.getElementById('detail-employe').value = data.employe; document.getElementById('detail-fonction').value = data.fonction; @@ -270,68 +271,86 @@ const listeContratExpirant = new Tabulator("#listeContratExpirant", { ajaxURL: $("boutonContratExpirants").dataset.urlexpirants, }) -/* let tabContrats = null; - -document.getElementById("modalContratsEmploye").addEventListener("show.bs.modal", function () { - - const employeId = document.getElementById("detail-id").value; +let tabContrats = null; +$("boutonVoirContrats").addEventListener("click", function () { + const employeId = $("detail-id").value; if (!employeId) { - alert("Aucun employé sélectionné"); + $("nom-employe-contrat").textContent = "Aucun employé sélectionné."; return; } - fetch(`/employé/employe/${employeId}/contrats/`) + $("modalContratsEmploye").dataset.employeId = employeId; + + const modalDetail = $("modalDetailEmploye"); + modalDetail.addEventListener( + "hidden.bs.modal", + () => bootstrap.Modal.getOrCreateInstance($("modalContratsEmploye")).show(), + {once: true} + ); + bootstrap.Modal.getOrCreateInstance(modalDetail).hide(); +}); + +$("modalContratsEmploye").addEventListener("shown.bs.modal", function () { + const employeId = this.dataset.employeId; + const urlTemplate = this.dataset.urlTemplate; + const tableElement = $("table-contrats-tabulator"); + + if (tabContrats) { + tabContrats.destroy(); + tabContrats = null; + } + + if (!employeId) { + $("nom-employe-contrat").textContent = "Aucun employé sélectionné."; + return; + } + + tableElement.replaceChildren(); + $("nom-employe-contrat").textContent = "Chargement..."; + const url = urlTemplate.replace("123456789", employeId); + + fetch(url) .then(response => { if (!response.ok) { - throw new Error("Erreur HTTP " + response.status); + throw new Error(`Erreur HTTP ${response.status}`); } return response.json(); }) .then(data => { + $("nom-employe-contrat").textContent = data.employe; - document.getElementById("nom-employe-contrat").innerText = - data.employe; - - if (tabContrats) { - tabContrats.destroy(); - } - - tabContrats = new Tabulator( - "#table-contrats-tabulator", - { - data: data.contrats, - layout: "fitColumns", - placeholder: "Aucun contrat", - - columns: [ - { title: "Numéro", field: "numero_contrat" }, - { title: "Type", field: "type_contrat" }, - { title: "Date début", field: "date_debut" }, - { title: "Date fin", field: "date_fin" }, - { title: "Salaire", field: "salaire_mensuel" }, - { title: "Statut", field: "statut" }, - - { - title: "Contrat", - field: "fichier", - formatter: function(cell) { - const url = cell.getValue(); - - if (!url) { - return "Aucun fichier"; - } - - return ` - Ouvrir - `; - } - } - ] - } - ); + tabContrats = new Tabulator(tableElement, { + data: data.contrats, + layout: "fitColumns", + pagination: true, + paginationSize: 10, + placeholder: "Aucun contrat", + columns: [ + {title: "Numéro", field: "numero_contrat"}, + {title: "Type", field: "type_contrat"}, + {title: "Date début", field: "date_debut"}, + {title: "Date fin", field: "date_fin"}, + {title: "Salaire", field: "salaire_mensuel"}, + {title: "Statut", field: "statut"}, + { + title: "Contrat", + field: "fichier", + formatter: "link", + formatterParams: {target: "_blank"}, + }, + ], + }); }) .catch(error => { - console.error("Erreur :", error); + console.error("Erreur lors du chargement des contrats :", error); + $("nom-employe-contrat").textContent = + "Impossible de charger les contrats."; + tableElement.textContent = + "Une erreur est survenue lors du chargement des contrats."; }); -}); */ +}); + +$("modalContratsEmploye").addEventListener("hidden.bs.modal", function () { + bootstrap.Modal.getOrCreateInstance($("modalDetailEmploye")).show(); +}); diff --git a/gestion_employe/templates/gestion_employe/index.html b/gestion_employe/templates/gestion_employe/index.html index 7e37c58..bdb1dcc 100644 --- a/gestion_employe/templates/gestion_employe/index.html +++ b/gestion_employe/templates/gestion_employe/index.html @@ -58,6 +58,7 @@ {% block 'modal' %} {% include "gestion_employe/parts/modalDetailEmploye.html" %} {% include "gestion_employe/parts/modalCreationContrat.html" %} + {% include "gestion_employe/parts/modalContratsEmploye.html" %} {% include "gestion_employe/parts/modalAffectationProjet.html" %} {% include "gestion_employe/parts/modalDocument.html" %} {% include "gestion_employe/parts/modalListeContratExpirants.html" %} diff --git a/gestion_employe/templates/gestion_employe/parts/modalContratsEmploye.html b/gestion_employe/templates/gestion_employe/parts/modalContratsEmploye.html index 6b780bf..5465a93 100644 --- a/gestion_employe/templates/gestion_employe/parts/modalContratsEmploye.html +++ b/gestion_employe/templates/gestion_employe/parts/modalContratsEmploye.html @@ -1,4 +1,4 @@ - {% csrf_token %} diff --git a/gestion_employe/urls.py b/gestion_employe/urls.py index 5878495..caa520a 100644 --- a/gestion_employe/urls.py +++ b/gestion_employe/urls.py @@ -90,5 +90,10 @@ urlpatterns = [ "liste-contrat-expirants", views.liste_contrat_expirants, name='liste-contrat-expirants' - ) + ), + path( + "liste-contrats-employe//", + views.liste_contrats_employe, + name="liste-contrats-employe" + ) ] diff --git a/gestion_employe/views.py b/gestion_employe/views.py index a799abf..d6dec52 100644 --- a/gestion_employe/views.py +++ b/gestion_employe/views.py @@ -1,9 +1,8 @@ import json from datetime import datetime, timedelta -import requests from dateutil.relativedelta import relativedelta -from decouple import config +from django.contrib.auth import update_session_auth_hash from django.contrib import messages from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User @@ -300,6 +299,8 @@ def creation_contrat(request): @login_required def contrats_employe(request, employe_id): + if not request.user.groups.filter(name="ressource_humaine").exists(): + return JsonResponse({"error": "Accès non autorisé"}, status=403) try: employe = Employe.objects.get(id=employe_id) @@ -506,42 +507,55 @@ def supprimer_formation(request, id_formation): def modifier_mot_passe(request): """Vue pour permettre à un employé de modifier son mot de passe""" if request.method == "POST": - username = request.user.username ancien_mdp = request.POST.get("ancien-mdp") nouveau_mdp = request.POST.get("nouveau-mdp") confirmation_mdp = request.POST.get("confirmation-mdp") + if not request.user.check_password(ancien_mdp): + messages.error(request, "L'ancien mot de passe est incorrect.") + return redirect("gestion_employe:mon-profil") + + if nouveau_mdp != confirmation_mdp: + messages.error(request, "Les nouveaux mots de passe ne correspondent pas.") + return redirect("gestion_employe:mon-profil") + try: validate_password(nouveau_mdp, user=request.user) except ValidationError as e: messages.error(request, f"Erreur : {' '.join(e.messages)}") return redirect("gestion_employe:mon-profil") - donnees = { - "username": username, - "ancien_mdp": ancien_mdp, - "nouveau_mdp": nouveau_mdp, - "confirmation_mdp": confirmation_mdp - } - - headers = { - "Content-Type": "application/json", - "Accept": "application/json", - } - - response = requests.post( - f"{config('SERVEUR_AUTHENTIFICATION')}modifier-mot-de-passe/", - json=donnees, - headers=headers - ) - - data = response.json() - cle = next(iter(data.keys())) - valeur = next(iter(data.values())) - - if cle == "success": - messages.success(request, valeur) - else: - messages.error(request, valeur) + request.user.set_password(nouveau_mdp) + request.user.save(update_fields=["password"]) + update_session_auth_hash(request, request.user) + messages.success(request, "Mot de passe modifié avec succès.") return redirect("gestion_employe:mon-profil") + +def liste_contrats_employe(request, id_employe): + """Vue pour retourner la liste des contrats d'un employé spécifique""" + try: + employe = Employe.objects.get(id=id_employe) + except Employe.DoesNotExist: + return JsonResponse({"error": "Employé non trouvé"}, status=404) + + contrats = Contrat.objects.filter(employe=employe).order_by("-date_debut") + + data = [ + { + "id": contrat.id, + "numero_contrat": contrat.numero_contrat, + "type_contrat": dict(Contrat.TYPE_CONTRAT).get(contrat.type_contrat), + "date_debut": contrat.date_debut.strftime("%Y-%m-%d"), + "date_fin": contrat.date_fin.strftime("%Y-%m-%d") if contrat.date_fin else "", + "salaire_mensuel": contrat.salaire_mensuel, + "statut": dict(Contrat.STATUT_CONTRAT).get(contrat.statut), + "fichier_contrat": contrat.fichier_contrat.url if contrat.fichier_contrat else "", + } + for contrat in contrats + ] + + return JsonResponse({ + "employe": f"{employe.user.first_name} {employe.user.last_name}", + "contrats": data + }) \ No newline at end of file