apercu
[auf_rh_dae.git] / project / rh / views.py
index 2aae70a..ce2802d 100644 (file)
@@ -1,37 +1,48 @@
 # -*- encoding: utf-8 -*-
+from datetime import date
+
+from django.contrib.auth.decorators import login_required
+from django.utils.encoding import smart_str
 from django.shortcuts import redirect, render_to_response, get_object_or_404
 from django.template import RequestContext
 
-#from datamaster_modeles import models as ref
-from project.rh import models as rh
+from datamaster_modeles import models as ref
+from project.lib import get_employe_from_id
 
+from rh import models as rh
+from rh.lib import calc_remun
 
 # homes
-def index(request):
-    return render_to_response('rh/index.html', {}, RequestContext(request))
+@login_required
+def profil(request):
+    """Profil personnel de l'employé - éditable"""
+    rc = RequestContext(request)
+    c = {}
     
-def perso(request, id):
-    """Espace personnel de l'employé."""
-    try:
-        employe = rh.Employe.objects.get(pk=id)
-    except:
-        employe = rh.Employe.none()
-    c = {
-        'user':request.user,
-        'employe':employe,
-        }
-    return render_to_response('rh/perso.html', c, RequestContext(request))
+    employe = rc['this_employe']
+        
+    c['user'] = request.user
+    c['employe'] = employe
+    return render_to_response('rh/profil.html', c, rc)
     
 # employes
+@login_required
 def employes_liste(request):
     """Liste des employés."""
-    employes = rh.Employe.objects.all()
+    today = date.today()
+    employes = rh.Employe.objects   \
+                    .filter(actif=True, supprime=False)   \
+                    .filter(dossiers__actif=True, dossiers__supprime=False) \
+                    .exclude(dossiers__date_debut__gt=today) \
+                    .exclude(dossiers__date_fin__lt=today) \
+                    .order_by('nom')
     c = {
         'user':request.user,
         'employes':employes,
         }
     return render_to_response('rh/employes_liste.html', c, RequestContext(request))
     
+@login_required
 def employe(request, id):
     """Information publique sur un employé."""
     try:
@@ -43,3 +54,150 @@ def employe(request, id):
         'employe':employe,
         }
     return render_to_response('rh/employe.html', c, RequestContext(request))
+
+
+# Rapports
+
+@login_required
+def rapports_poste(request):
+
+    lookup_params = dict(request.GET.items())
+
+    comble = 'all'
+
+    for key, value in lookup_params.items():
+        if not isinstance(key, str):
+            # 'key' will be used as a keyword argument later, so Python
+            # requires it to be a string.
+            del lookup_params[key]
+            lookup_params[smart_str(key)] = value
+
+        if key == 'comble':
+            comble = value
+            del lookup_params[key]
+
+    postes = rh.Poste.actifs.select_related('implantation').all()
+    postes = postes.filter(**lookup_params)
+
+    poste_ids = set([p.pk for p in postes])
+
+    dossiers = rh.Dossier.objects.select_related('poste', 'employe').filter(poste__in=poste_ids)
+
+    employes = dict((d.poste.id, d.employe) for d in dossiers)
+
+    out = []
+
+    for p in postes:
+        employe = employes.get(p.id)
+
+        if comble == 'c' and not employe:
+            continue
+
+        if comble == 'n' and employe:
+            continue
+
+        line = {}
+        out.append(line)
+
+        line['id'] = p.id
+        line['nom'] = p.nom
+        line['implantation'] = p.implantation.nom
+        if employe:
+            line['employe_id'] = employe.id
+            line['employe_nom'] = employe.nom
+            line['employe_prenom'] = employe.prenom
+
+    c = {
+        'title': 'Rapport des postes',
+        'postes': out,
+    }
+
+    return render_to_response('rh/rapports/postes.html', c, RequestContext(request))
+
+
+def rapports_contrat(request):
+
+    lookup_params = dict(request.GET.items())
+
+    for key, value in lookup_params.items():
+        if not isinstance(key, str):
+            # 'key' will be used as a keyword argument later, so Python
+            # requires it to be a string.
+            del lookup_params[key]
+            lookup_params[smart_str(key)] = value
+
+    contrats = rh.Contrat.objects.select_related('dossier', 'dossier__poste', 'dossier__poste__implantation', 'type_contrat', 'dossier__employe').all()
+    contrats = contrats.filter(**lookup_params)
+
+    c = {
+        'title': 'Rapport des contrats',
+        'contrats': contrats,
+    }
+
+    return render_to_response('rh/rapports/contrats.html', c, RequestContext(request))
+
+
+def rapports_remuneration(request):
+
+    lookup_params = dict(request.GET.items())
+
+    for key, value in lookup_params.items():
+        if not isinstance(key, str):
+            # 'key' will be used as a keyword argument later, so Python
+            # requires it to be a string.
+            del lookup_params[key]
+            lookup_params[smart_str(key)] = value
+
+    employes = rh.Employe.objects.all()
+    employes = employes.filter(**lookup_params)
+
+    output = []
+
+    for employe in employes:
+        line = {}
+        output.append(line)
+
+        dossiers = employe.dossiers.all()
+
+        remun = {}
+        remun_sum_euro = 0
+
+        for dossier in dossiers:
+            this_remun, this_remun_sum, this_remun_sum_euro = calc_remun(dossier)
+
+            for item in this_remun:
+                if item not in remun:
+                    remun[item] = this_remun[item]
+                else:
+                    remun[item][0] += this_remun[item][0]
+                    remun[item][1] += this_remun[item][1]
+
+            remun_sum_euro += this_remun_sum_euro
+
+        line['remun_sum_euro'] = remun_sum_euro
+
+        for r in remun:
+            if r == u'Indemnité':
+                line['Indemnite'] = remun[r][1]
+            else:
+                line[r] = remun[r][1]
+
+        line['id'] = employe.id
+        line['nom'] = employe.nom
+        line['prenom'] = employe.prenom
+
+
+    c = {
+        'title': 'Rapport de remuneration',
+        'employes': output,
+    }
+
+    return render_to_response('rh/rapports/remuneration.html', c, RequestContext(request))
+
+def dossier_apercu(request, dossier_id):
+    c = {
+        'is_popup' : request.GET.get('_popup', False),
+        'dossier' : get_object_or_404(rh.Dossier, pk=dossier_id)
+
+    }
+    return render_to_response('admin/rh/dossier/apercu.html', c, RequestContext(request))