From 64b7788887ab6aa50a294e8bc4846afd141913ac Mon Sep 17 00:00:00 2001 From: mariusrklein <47218379+mariusrklein@users.noreply.github.com> Date: Sun, 6 Apr 2025 14:24:22 +0200 Subject: [PATCH] fix(excursion/ljp): added intervention-based calculation of seminar duration. --- jdav_web/members/excel.py | 2 +- jdav_web/members/models.py | 34 ++++++++++++++++++- .../templates/members/seminar_report_docx.tex | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/jdav_web/members/excel.py b/jdav_web/members/excel.py index 20bbbc1..d8b55e3 100644 --- a/jdav_web/members/excel.py +++ b/jdav_web/members/excel.py @@ -117,7 +117,7 @@ def generate_ljp_vbk(excursion): sheet['D19'] = settings.SEKTION sheet['G19'] = title sheet['I19'] = f"von {excursion.date:%d.%m.%y} bis {excursion.end:%d.%m.%y}" - sheet['J19'] = excursion.duration + sheet['J19'] = excursion.ljp_duration sheet['L19'] = f"{excursion.ljp_participant_count}" sheet['H19'] = excursion.get_ljp_activity_category() sheet['M19'] = f"{excursion.postcode}, {excursion.place}" diff --git a/jdav_web/members/models.py b/jdav_web/members/models.py index a1f179d..5e77482 100644 --- a/jdav_web/members/models.py +++ b/jdav_web/members/models.py @@ -8,6 +8,7 @@ import csv from django.db import models from django.db.models import TextField, ManyToManyField, ForeignKey, Count,\ Sum, Case, Q, F, When, Value, IntegerField, Subquery, OuterRef +from django.db.models.functions import TruncDate from django.utils.translation import gettext_lazy as _ from django.utils import timezone from django.utils.html import format_html @@ -1242,7 +1243,31 @@ class Freizeit(CommonModel): return sum([i.duration for i in self.ljpproposal.intervention_set.all()]) else: return 0 + + @property + def total_seminar_days(self): + """calculate seminar days based on intervention hours in every day""" + if hasattr(self, 'ljpproposal'): + hours_per_day = ( + Intervention.objects + .filter(ljp_proposal_id=self.ljpproposal.id) + .annotate(day=TruncDate('date_start')) # Extract the date (without time) + .values('day') # Group by day + .annotate(total_duration=Sum('duration')) # Sum durations for each day + .order_by('day') # Sort results by date + ) + # Calculate the total number of seminar days + # Each day is counted as 1 if total_duration is >= 5 hours, as 0.5 if total_duration is >= 2.5 + # otherwise 0 + return sum([min(math.floor(h['total_duration']/cvt_to_decimal(2.5))/2, 1) for h in hours_per_day]) + else: + return 0 + @property + def ljp_duration(self): + """calculate the duration in days for the LJP""" + return min(self.duration, self.total_seminar_days) + @property def staff_count(self): return self.jugendleiter.count() @@ -1331,12 +1356,19 @@ class Freizeit(CommonModel): return cvt_to_decimal(min(self.maximal_ljp_contributions, 0.9 * float(self.statement.total_bills_theoretic) + float(self.statement.total_staff))) + @property + def payable_ljp_contributions(self): + """from the requested ljp contributions, a tax may be deducted for risk reduction""" + if self.statement.ljp_to: + return self.statement.paid_ljp_contributions + return cvt_to_decimal(self.potential_ljp_contributions * cvt_to_decimal(1 - settings.LJP_TAX)) + @property def total_relative_costs(self): if not self.statement: return 0 total_costs = self.statement.total_bills_theoretic - total_contributions = self.statement.total_subsidies + self.potential_ljp_contributions + total_contributions = self.statement.total_subsidies + self.payable_ljp_contributions return total_costs - total_contributions @property diff --git a/jdav_web/members/templates/members/seminar_report_docx.tex b/jdav_web/members/templates/members/seminar_report_docx.tex index 21c58b8..73539a2 100644 --- a/jdav_web/members/templates/members/seminar_report_docx.tex +++ b/jdav_web/members/templates/members/seminar_report_docx.tex @@ -36,7 +36,7 @@ \textbf{Sektion:} & {{ settings.SEKTION }} \\ \textbf{Titel der Maßnahme:} & {% if not memberlist.ljpproposal %}{{ memberlist.name|esc_all }}{% else %}{{ memberlist.ljpproposal.title }} {% endif %} \\ \textbf{Interne Ordnungsnummer:} & {{ memberlist.code|esc_all }} \\ - \textbf{Anzahl der durchgeführten Lehrgangstage:} & {{ memberlist.duration }} \\ + \textbf{Anzahl der durchgeführten Lehrgangstage:} & {{ memberlist.ljp_duration }} \\ \end{tabular} \end{table}