From: PA Parent Date: Wed, 30 Nov 2011 19:58:27 +0000 (-0500) Subject: Refactor pour somme remun X-Git-Tag: DAE+RH~238^2~23 X-Git-Url: http://git.auf.org/?p=auf_rh_dae.git;a=commitdiff_plain;h=49c98347e08ab9ea332cb3af59bd5a2325f06bae Refactor pour somme remun --- diff --git a/project/rh/lib.py b/project/rh/lib.py index 416cf8b..279b0d2 100644 --- a/project/rh/lib.py +++ b/project/rh/lib.py @@ -300,28 +300,13 @@ class DossierAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin,) instance.save() def render_change_form(self, request, context, *args, **kwargs): - obj = kwargs['obj'] + obj = kwargs.get('obj', None) - thisyear = datetime.date.today().year - thisyearfilter = Q(date_debut__year=thisyear) | Q(date_fin__year=thisyear) - - remunnow = obj.rh_remuneration_remunerations.filter(thisyearfilter) + if not obj: + return super(DossierAdmin, self).render_change_form(request, context, *args, **kwargs) - remun_sum = 0 - remun_sum_euro = 0 - sums = defaultdict(int) - sums_euro = defaultdict(int) - for r in remunnow: - nature = r.type.nature_remuneration - sums[nature] += r.montant - sums_euro[nature] += r.montant_euro() - remun_sum += r.montant - remun_sum_euro += r.montant_euro() - remun = {} - sums = dict(sums) - for n, s in sums.iteritems(): - remun[n] = [sums[n], sums_euro[n]] + remun, remun_sum, remun_sum_euro = calc_remun(obj) extra = { 'remun': remun, @@ -568,3 +553,28 @@ class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin): def _devise_nom(self, obj): return obj.devise.nom _devise_nom.short_description = "Nom de la devise" + + +def calc_remun(dossier): + thisyear = datetime.date.today().year + thisyearfilter = Q(date_debut__year=thisyear) | Q(date_fin__year=thisyear) + + remunnow = dossier.rh_remuneration_remunerations.filter(thisyearfilter) + + remun_sum = 0 + remun_sum_euro = 0 + sums = defaultdict(int) + sums_euro = defaultdict(int) + for r in remunnow: + nature = r.type.nature_remuneration + sums[nature] += r.montant + sums_euro[nature] += r.montant_euro() + remun_sum += r.montant + remun_sum_euro += r.montant_euro() + + remun = {} + sums = dict(sums) + for n, s in sums.iteritems(): + remun[n] = [sums[n], sums_euro[n]] + + return remun, remun_sum, remun_sum_euro