128 lines
5.1 KiB
Python
128 lines
5.1 KiB
Python
|
"""
|
||
|
Author: Michel Peltriaux
|
||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||
|
Created on: 18.10.21
|
||
|
|
||
|
"""
|
||
|
from django.db.models import Count, Sum, Q
|
||
|
|
||
|
from codelist.models import KonovaCode
|
||
|
from codelist.settings import CODELIST_LAW_ID
|
||
|
from compensation.models import Compensation, Payment, EcoAccountDeduction
|
||
|
from intervention.models import Intervention
|
||
|
|
||
|
|
||
|
class TimespanReport:
|
||
|
office_id = -1
|
||
|
|
||
|
class InterventionReport:
|
||
|
queryset = Intervention.objects.none()
|
||
|
queryset_checked = Intervention.objects.none()
|
||
|
queryset_recorded = Intervention.objects.none()
|
||
|
|
||
|
# Law related
|
||
|
law_sum = -1
|
||
|
law_sum_checked = -1
|
||
|
law_sum_recorded = -1
|
||
|
evaluated_laws = None
|
||
|
|
||
|
# Compensations related
|
||
|
compensation_sum = -1
|
||
|
compensation_sum_checked = -1
|
||
|
compensation_sum_recorded = -1
|
||
|
payment_sum = -1
|
||
|
payment_sum_checked = -1
|
||
|
payment_sum_recorded = -1
|
||
|
deduction_sum = -1
|
||
|
deduction_sum_checked = -1
|
||
|
deduction_sum_recorded = -1
|
||
|
|
||
|
def __init__(self, id: str):
|
||
|
self.queryset = Intervention.objects.filter(
|
||
|
responsible__conservation_office__id=id,
|
||
|
deleted=None,
|
||
|
)
|
||
|
self.queryset_checked = self.queryset.filter(
|
||
|
checked__isnull=False
|
||
|
)
|
||
|
self.queryset_recorded = self.queryset.filter(
|
||
|
recorded__isnull=False
|
||
|
)
|
||
|
self._create_report()
|
||
|
|
||
|
def _create_report(self):
|
||
|
""" Creates all report information
|
||
|
|
||
|
Returns:
|
||
|
|
||
|
"""
|
||
|
self._evaluate_laws()
|
||
|
self._evaluate_compensations()
|
||
|
|
||
|
def _evaluate_laws(self):
|
||
|
""" Analyzes the intervention-law distribution
|
||
|
|
||
|
Returns:
|
||
|
|
||
|
"""
|
||
|
# Count interventions based on law
|
||
|
# Fetch all KonovaCodes for laws, sorted alphabetically
|
||
|
laws = KonovaCode.objects.filter(
|
||
|
is_archived=False,
|
||
|
is_leaf=True,
|
||
|
code_lists__in=[CODELIST_LAW_ID],
|
||
|
).order_by(
|
||
|
"long_name"
|
||
|
)
|
||
|
# Fetch all law ids which are used by any .legal object of an intervention object
|
||
|
intervention_laws_total = self.queryset.values_list("legal__laws__id")
|
||
|
intervention_laws_checked = self.queryset.filter(checked__isnull=False).values_list("legal__laws__id")
|
||
|
intervention_laws_recorded = self.queryset.filter(recorded__isnull=False).values_list(
|
||
|
"legal__laws__id")
|
||
|
# Count how often which law id appears in the above list, return only the long_name of the law and the resulting
|
||
|
# count (here 'num'). This is for keeping the db fetch as small as possible
|
||
|
# Compute the sum for total, checked and recorded
|
||
|
self.evaluated_laws = laws.annotate(
|
||
|
num=Count("id", filter=Q(id__in=intervention_laws_total)),
|
||
|
num_checked=Count("id", filter=Q(id__in=intervention_laws_checked)),
|
||
|
num_recorded=Count("id", filter=Q(id__in=intervention_laws_recorded)),
|
||
|
).values_list("short_name", "long_name", "num_checked", "num_recorded", "num", named=True)
|
||
|
self.law_sum = self.evaluated_laws.aggregate(sum_num=Sum("num"))["sum_num"]
|
||
|
self.law_sum_checked = self.evaluated_laws.aggregate(sum_num_checked=Sum("num_checked"))["sum_num_checked"]
|
||
|
self.law_sum_recorded = self.evaluated_laws.aggregate(sum_num_recorded=Sum("num_recorded"))["sum_num_recorded"]
|
||
|
|
||
|
def _evaluate_compensations(self):
|
||
|
""" Analyzes the types of compensation distribution
|
||
|
|
||
|
Returns:
|
||
|
|
||
|
"""
|
||
|
# Count all compensations
|
||
|
comps = Compensation.objects.filter(
|
||
|
intervention__in=self.queryset
|
||
|
)
|
||
|
self.compensation_sum = comps.count()
|
||
|
self.compensation_sum_checked = comps.filter(intervention__checked__isnull=False).count()
|
||
|
self.compensation_sum_recorded = comps.filter(intervention__recorded__isnull=False).count()
|
||
|
|
||
|
# Count all payments
|
||
|
payments = Payment.objects.filter(
|
||
|
intervention__in=self.queryset
|
||
|
)
|
||
|
self.payment_sum = payments.count()
|
||
|
self.payment_sum_checked = payments.filter(intervention__checked__isnull=False).count()
|
||
|
self.payment_sum_recorded = payments.filter(intervention__recorded__isnull=False).count()
|
||
|
|
||
|
# Count all deductions
|
||
|
deductions = EcoAccountDeduction.objects.filter(
|
||
|
intervention__in=self.queryset
|
||
|
)
|
||
|
self.deduction_sum = deductions.count()
|
||
|
self.deduction_sum_checked = deductions.filter(intervention__checked__isnull=False).count()
|
||
|
self.deduction_sum_recorded = deductions.filter(intervention__recorded__isnull=False).count()
|
||
|
|
||
|
def __init__(self, office_id: str):
|
||
|
self.office_id = office_id
|
||
|
self.intervention_report = self.InterventionReport(self.office_id)
|