Ajout de la liste des contrats d'un employé
This commit is contained in:
@@ -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 "<span class='text-danger'>Aucun fichier</span>";
|
||||
}
|
||||
|
||||
return `<a href="${url}" target="_blank" class="btn btn-sm btn-primary">
|
||||
<i class="bi bi-file-earmark-pdf"></i> Ouvrir
|
||||
</a>`;
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
);
|
||||
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();
|
||||
});
|
||||
|
||||
@@ -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" %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<div class="modal fade" id="modalContratsEmploye" tabindex="-1">
|
||||
<div class="modal fade" id="modalContratsEmploye" data-url-template="{% url 'gestion_employe:contrats-employe' 123456789 %}" tabindex="-1">
|
||||
<div class="modal-dialog modal-xl">
|
||||
<div class="modal-content">
|
||||
|
||||
@@ -14,4 +14,3 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -97,11 +97,15 @@
|
||||
<i class="bi bi-file-earmark-text me-2"></i> Liste des Contrats
|
||||
</h5>
|
||||
{% if user|has_group:"ressource_humaine" %}
|
||||
<div>
|
||||
<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#modalContrat{{ item.employe.id }}">
|
||||
<div class="d-flex gap-2">
|
||||
<button type="button" class="btn btn-outline-primary" id="boutonVoirContrats">
|
||||
<i class="bi bi-files"></i> Voir tous les contrats
|
||||
</button>
|
||||
<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#modalContrat">
|
||||
<i class="bi bi-file-earmark-text"></i> Créer contrat
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{% endif %}
|
||||
</div>
|
||||
{% csrf_token %}
|
||||
|
||||
@@ -90,5 +90,10 @@ urlpatterns = [
|
||||
"liste-contrat-expirants",
|
||||
views.liste_contrat_expirants,
|
||||
name='liste-contrat-expirants'
|
||||
)
|
||||
),
|
||||
path(
|
||||
"liste-contrats-employe/<int:id_employe>/",
|
||||
views.liste_contrats_employe,
|
||||
name="liste-contrats-employe"
|
||||
)
|
||||
]
|
||||
|
||||
@@ -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
|
||||
})
|
||||
Reference in New Issue
Block a user