ajout PJ au dossier (NON TESTE, ajax fait disparaitre mon form...)
[auf_rh_dae.git] / project / dae / views.py
index 8b5e6b0..1fe65fd 100644 (file)
@@ -1,18 +1,22 @@
 # -*- encoding: utf-8 -*-
+from datetime import date
 from json import dumps
+import warnings
 
 from django.http import Http404, HttpResponse
 from django.shortcuts import redirect, render_to_response, get_object_or_404
 from django.template import RequestContext
 
 from project.dae.forms import (ChoosePosteForm, DossierForm, EmployeForm,
-                               PosteForm, PosteFinancementForm)
+                               PosteForm, PosteFinancementForm, PostePieceForm,
+                               DossierPieceForm)
 from project.dae import models as dae
 from project.rh_v1 import models as rh
 
 from project.decorators import admin_required
 
 
+
 def index(request):
     return render_to_response('dae/index.html', {}, RequestContext(request))
 
@@ -46,15 +50,18 @@ def poste(request, key=None):
     if request.POST:
         data.update(dict(request.POST.items()))
         form = PosteForm(data, instance=poste)
+        piecesForm = PostePieceForm(request.POST, request.FILES, instance=poste)
         if 'save' in data and form.is_valid():
             poste = form.save()
+            piecesForm.save()
             return redirect('poste', key='dae-%s' % poste.id)
     else:
         # 'initial' évite la validation prémature lors d'une copie de poste de
         # rh_v1 vers dae.
         form = PosteForm(initial=data, instance=poste)
+        piecesForm = PostePieceForm(instance=poste)
 
-    vars.update(dict(form=form, poste=poste, poste_key=key))
+    vars.update(dict(form=form, poste=poste, poste_key=key, piecesForm=piecesForm))
 
     return render_to_response('dae/poste.html', vars, RequestContext(request))
 
@@ -119,34 +126,67 @@ def embauche(request, key=None, dossier=None):
         if source != 'dae':
             return Http404
         poste = get_object_or_404(dae.Poste, pk=id)
-        employe_form = EmployeForm()
 
         if request.POST:
-            employe_source, id = request.POST['employe'].split('-')
-            if employe_source == 'dae':
-                employe = get_object_or_404(dae.Employe, pk=id)
-            elif employe_source == 'rh':
-                e = get_object_or_404(rh.Employe, pk=id)
-                employe = dae.Employe(id_rh=e, prenom=e.prenom, nom=e.nom,
-                                      genre=e.genre)
+            if request.POST['employe'] == '':
+                # Nouvel employé
+                employe = dae.Employe()
             else:
-                raise Http404
+                employe_source, id = request.POST['employe'].split('-')
+                if employe_source == 'dae':
+                    # Employé DAE
+                    employe = get_object_or_404(dae.Employe, pk=id)
+                elif employe_source == 'rh':
+                    # Employé RH, on le copie dans DAE
+                    e = get_object_or_404(rh.Employe, pk=id)
+                    employe = dae.Employe(id_rh=e, prenom=e.prenom, nom=e.nom,
+                                          genre=e.genre)
+                else:
+                    raise Http404
+
             employe_form = EmployeForm(request.POST, instance=employe)
-            if 'save' in request.POST and employe_form.is_valid():
-                employe_form.save()
+
+            if 'save' in request.POST:
+                if employe_form.is_valid():
+                    data = dict(request.POST.items())
+                    with warnings.catch_warnings():
+                        warnings.simplefilter('ignore')
+                        employe = employe_form.save()
+                    data['employe'] = 'dae-%s' % employe.id
+                    employe_form = EmployeForm(data, instance=employe)
                 if not dossier:
                     dossier = dae.Dossier(poste=poste, employe=employe)
                 else:
                     dossier = get_object_or_404(dae.Dossier, pk=dossier)
                 dossier_form = DossierForm(request.POST, instance=dossier)
+                piecesForm = DossierPieceForm(request.POST, request.FILES, instance=dossier)
+
+                if dossier_form.is_valid():
+                    dossier = dossier_form.save()
+                    piecesForm.save()
+                    return redirect('embauche', key='dae-%s' % poste.id,
+                                    dossier=dossier.id)
+            else:
+                dossier_form = DossierForm(instance=dossier)
+                piecesForm = DossierPieceForm(instance=dossier)
         else:
+            # Initialisation d'un formulaire vide
             dossier_rh = rh.Dossier()
             poste_rh = poste.id_rh
-            dossier = pre_filled_dossier(dossier_rh, 'new', poste_rh)
+            if dossier:
+                dossier = get_object_or_404(dae.Dossier, pk=dossier)
+                employe = dossier.employe
+                data = dict(employe='dae-%s' % employe.id)
+                employe_form = EmployeForm(initial=data, instance=employe)
+            else:
+                dossier = pre_filled_dossier(dossier_rh, 'new', poste_rh)
+                employe_form = EmployeForm()
+
             dossier_form = DossierForm(instance=dossier)
+            piecesForm = DossierPieceForm(instance=dossier)
 
-        vars = dict(step='employe', poste=poste,
-                    forms=dict(employe=employe_form, dossier=dossier_form))
+        vars = dict(step='employe', poste=poste, dossier=dossier, piecesForm=piecesForm,
+                    forms=dict(employe=employe_form, dossier=dossier_form, ))
 
     return render_to_response('dae/embauche.html', vars,
                               RequestContext(request))
@@ -210,7 +250,11 @@ def dossier(request, poste_key, employe_key):
     else:
         dossier = pre_filled_dossier(dossier_rh, employe_source, poste_rh)
 
-    return HttpResponse(DossierForm(initial=data, instance=dossier).as_table())
+    dossier_form = DossierForm(initial=data, instance=dossier)
+    vars = dict(form=dossier_form)
+
+    return render_to_response('dae/embauche-dossier.html', vars,
+                          RequestContext(request))
 
 
 def salaire(request, implantation, devise, classement):
@@ -277,3 +321,60 @@ def pre_filled_dossier(dossier_rh, employe_source, poste_rh):
 
     return dossier
 
+def coefficient(request):
+    """ Appel AJAX : 
+    input : classement
+    output : coefficient
+    """
+    data = dict()
+    if request.POST and 'classement' in request.POST:
+        classement = request.POST.get('classement')
+        classement = rh.Classement.objects.get(pk=classement)
+        data['coefficient'] = classement.coefficient
+    return HttpResponse(dumps(data))
+
+def valeur(request):
+    """ Appel AJAX : 
+    input : valeur_point
+    output : valeur
+    """
+    data = dict()
+    if request.POST and 'valeur_point' in request.POST:
+        valeur_point = request.POST.get('valeur_point')
+        valeur_point = rh.ValeurPoint.objects.get(pk=valeur_point)
+        data['valeur'] = valeur_point.valeur
+    return HttpResponse(dumps(data))
+        
+def devise(request):
+    """ Appel AJAX : 
+    input : valeur_point
+    output : devise, devise_code, taux_euro
+    """
+    data = dict()
+    if request.POST and 'valeur_point' in request.POST:
+        valeur_point = request.POST.get('valeur_point')
+        valeur_point = rh.ValeurPoint.objects.get(pk=valeur_point)
+        annee = valeur_point.annee
+        implantation = valeur_point.implantation
+        taux = rh.TauxChange.objects.get(annee=annee, 
+                                        implantation=implantation)
+        data['devise'] = taux.devise.id
+        data['devise_code'] = taux.devise.code
+        data['taux_euro'] = taux.taux
+    return HttpResponse(dumps(data))
+    
+def devise_code(request):
+    """ Appel AJAX : 
+    input : devise
+    output : devise_code, taux_euro
+    """
+    data = dict()
+    if request.POST and 'devise' in request.POST:
+        devise = request.POST.get('devise')
+        devise = rh.Devise.objects.get(pk=devise)
+        annee = date.today().year
+        taux = rh.TauxChange.objects.filter(annee=annee, devise=devise)
+        data['devise_code'] = devise.code
+        data['taux_euro'] = taux[0].taux
+    return HttpResponse(dumps(data))
+