@@ -5,13 +5,16 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 18.10.21
|
||||
|
||||
"""
|
||||
from django.contrib.gis.db.models import MultiPolygonField
|
||||
from django.contrib.gis.db.models.functions import NumGeometries
|
||||
from django.contrib.gis.measure import Area
|
||||
from django.db.models import Count, Sum, Q
|
||||
from django.db.models.functions import Cast
|
||||
|
||||
from analysis.settings import LKOMPVZVO_PUBLISH_DATE
|
||||
from codelist.models import KonovaCode
|
||||
from codelist.settings import CODELIST_LAW_ID
|
||||
from compensation.models import Compensation, Payment, EcoAccountDeduction
|
||||
from compensation.models import Compensation, Payment, EcoAccountDeduction, EcoAccount
|
||||
from intervention.models import Intervention
|
||||
from konova.models import Geometry
|
||||
|
||||
@@ -217,7 +220,9 @@ class TimespanReport:
|
||||
return Geometry.objects.filter(
|
||||
id__in=ids
|
||||
).annotate(
|
||||
num=NumGeometries("geom")
|
||||
geom_cast=Cast("geom", MultiPolygonField())
|
||||
).annotate(
|
||||
num=NumGeometries("geom_cast")
|
||||
).aggregate(
|
||||
num_geoms=Sum("num")
|
||||
)["num_geoms"] or 0
|
||||
@@ -265,6 +270,61 @@ class TimespanReport:
|
||||
intervention__checked__isnull=False,
|
||||
)
|
||||
|
||||
class EcoAccountReport:
|
||||
queryset = EcoAccount.objects.none()
|
||||
queryset_recorded = EcoAccount.objects.none()
|
||||
queryset_old = EcoAccount.objects.none()
|
||||
queryset_deductions = EcoAccountDeduction.objects.none()
|
||||
queryset_deductions_recorded = EcoAccountDeduction.objects.none()
|
||||
queryset_has_deductions = EcoAccountDeduction.objects.none()
|
||||
|
||||
# Total size of deductions
|
||||
deductions_sq_m = -1
|
||||
recorded_deductions_sq_m = -1
|
||||
|
||||
def __init__(self, id: str):
|
||||
# First fetch all eco account for this office
|
||||
self.queryset_total = EcoAccount.objects.filter(
|
||||
responsible__conservation_office__id=id,
|
||||
deleted=None,
|
||||
)
|
||||
self.queryset_recorded = self.queryset_total.filter(
|
||||
recorded__isnull=False
|
||||
)
|
||||
# Then fetch the old ones (pre-LKompVzVo)
|
||||
self.queryset_old = self.queryset_total.filter(
|
||||
recorded__timestamp__lte=LKOMPVZVO_PUBLISH_DATE,
|
||||
)
|
||||
# Then fetch the default queryset with the new ones (post-LKompVzVo)
|
||||
self.queryset = self.queryset_total.filter(
|
||||
recorded__timestamp__gte=LKOMPVZVO_PUBLISH_DATE,
|
||||
)
|
||||
# Fetch all related deductions
|
||||
self.queryset_deductions = EcoAccountDeduction.objects.filter(
|
||||
account__id__in=self.queryset.values_list("id")
|
||||
)
|
||||
# Fetch deductions for interventions which are already recorded
|
||||
self.queryset_deductions_recorded = self.queryset_deductions.filter(
|
||||
intervention__recorded__isnull=False
|
||||
)
|
||||
self._create_report()
|
||||
|
||||
def _create_report(self):
|
||||
""" Creates all report information
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self._evaluate_deductions()
|
||||
|
||||
def _evaluate_deductions(self):
|
||||
self.deductions_sq_m = self.queryset_deductions.aggregate(
|
||||
sum=Sum("surface")
|
||||
)["sum"]
|
||||
self.recorded_deductions_sq_m = self.queryset_deductions_recorded.aggregate(
|
||||
sum=Sum("surface")
|
||||
)["sum"]
|
||||
|
||||
class OldInterventionReport:
|
||||
queryset = Compensation.objects.none()
|
||||
queryset_checked = Compensation.objects.none()
|
||||
@@ -287,4 +347,5 @@ class TimespanReport:
|
||||
self.office_id = office_id
|
||||
self.intervention_report = self.InterventionReport(self.office_id)
|
||||
self.compensation_report = self.CompensationReport(self.office_id)
|
||||
self.eco_account_report = self.EcoAccountReport(self.office_id)
|
||||
self.old_intervention_report = self.OldInterventionReport(self.office_id)
|
||||
|
||||
Reference in New Issue
Block a user