#132 WIP Migrate OEKs

* migration for deductions added
This commit is contained in:
mpeltriaux 2022-03-24 18:09:59 +01:00
parent a77cfa7fe3
commit f9db0e7596
2 changed files with 66 additions and 7 deletions

View File

@ -163,10 +163,13 @@ class CompensationMigrater(BaseMigrater):
) )
except ObjectDoesNotExist: except ObjectDoesNotExist:
# Very old data might contain entries from this deprecated list # Very old data might contain entries from this deprecated list
state_code = KonovaCode.objects.get( try:
atom_id=state_type, state_code = KonovaCode.objects.get(
code_lists__in=[CODELIST_AFTER_STATE_BIOTOPES_ID] atom_id=state_type,
) code_lists__in=[CODELIST_AFTER_STATE_BIOTOPES_ID]
)
except ObjectDoesNotExist:
raise ObjectDoesNotExist(f"{compensation.identifier} has unknown state code: {state_type}")
tmp_cursor_z_code = self.db_connection.cursor() tmp_cursor_z_code = self.db_connection.cursor()
tmp_cursor_z_code.execute(z_code_sql.format(pkey_entry)) tmp_cursor_z_code.execute(z_code_sql.format(pkey_entry))
z_code_results = tmp_cursor_z_code.fetchall() z_code_results = tmp_cursor_z_code.fetchall()

View File

@ -4,8 +4,8 @@ from django.db import transaction
from codelist.models import KonovaCode from codelist.models import KonovaCode
from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_COMPENSATION_HANDLER_ID from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_COMPENSATION_HANDLER_ID
from compensation.models import EcoAccount, EcoAccountDocument from compensation.models import EcoAccount, EcoAccountDocument, EcoAccountDeduction
from intervention.models import Responsibility, Handler from intervention.models import Responsibility, Handler, Intervention
from konova.management.commands.kspMigrater.compensation_migrater import CompensationMigrater from konova.management.commands.kspMigrater.compensation_migrater import CompensationMigrater
from konova.models import Geometry from konova.models import Geometry
from konova.sub_settings.lanis_settings import DEFAULT_SRID_RLP, DEFAULT_SRID from konova.sub_settings.lanis_settings import DEFAULT_SRID_RLP, DEFAULT_SRID
@ -62,6 +62,7 @@ class EcoAccountMigrater(CompensationMigrater):
eco_account = self._migrate_action_control_deadlines(eco_account, oek) eco_account = self._migrate_action_control_deadlines(eco_account, oek)
eco_account = self._migrate_actions(eco_account, oek) eco_account = self._migrate_actions(eco_account, oek)
eco_account = self._migrate_log(eco_account, oek) eco_account = self._migrate_log(eco_account, oek)
eco_account = self._migrate_deductions(eco_account, oek)
eco_account = self._migrate_documents(eco_account, EcoAccountDocument, oek) eco_account = self._migrate_documents(eco_account, EcoAccountDocument, oek)
eco_account.save() eco_account.save()
@ -115,7 +116,7 @@ class EcoAccountMigrater(CompensationMigrater):
try: try:
# Calculate area by transforming # Calculate area by transforming
rlp_geom = db_result_geom.transform(ct=DEFAULT_SRID_RLP, clone=True) rlp_geom = db_result_geom.transform(ct=DEFAULT_SRID_RLP, clone=True)
area = int(rlp_geom.area) area = round(rlp_geom.area)
instance.deductable_surface = area instance.deductable_surface = area
instance.geometry.geom = db_result_geom if not db_result_geom.empty else None instance.geometry.geom = db_result_geom if not db_result_geom.empty else None
instance.geometry.save() instance.geometry.save()
@ -181,3 +182,58 @@ class EcoAccountMigrater(CompensationMigrater):
tmp_cursor.close() tmp_cursor.close()
return eco_account return eco_account
def _migrate_deductions(self, eco_account, oek):
identifier = f"'{oek[0]}'"
empty_str = "''"
tmp_cursor = self.db_connection.cursor()
tmp_cursor.execute(
'select '
'om_kom."KENNUNG", '
'auf."Anteil", '
'ref."REFERENZ" '
'from "OBJ_MASTER" om '
'left join "Aufwertung" auf on om."GISPADID"=auf."GISPADID" '
'left join "OBJ_MASTER" om_kom on auf."Infos"=om_kom."KENNUNG" '
'left join "REFERENZ" ref on om_kom."GISPADID"=ref."GISPADID" '
'where '
f'(auf."Infos" is not null and auf."Infos"!={empty_str}) and '
f'(ref."REFERENZ" is not null and ref."REFERENZ"!={empty_str}) and '
f'om."KENNUNG"={identifier}'
)
fetched_results = tmp_cursor.fetchall()
eco_account.deductions.all().delete()
for result in fetched_results:
old_deduction_kom_identifier = result[0]
deduction_amount_percentage = result[1]
target_intervention_identifier = result[2]
if target_intervention_identifier is None:
# old garbage data - skip
continue
try:
intervention = Intervention.objects.get(
identifier=target_intervention_identifier
)
except ObjectDoesNotExist:
# old garbage data
print(f"{identifier} has deduction for {target_intervention_identifier} which does not exist")
continue
deduction_amount_sqm = round(eco_account.deductable_surface * (float(deduction_amount_percentage)/100))
rest_available_surface = eco_account.deductable_surface - eco_account.get_deductions_surface()
rest_after_deduction = rest_available_surface - deduction_amount_sqm
if rest_after_deduction < 0:
print(f"{identifier} has {eco_account.deductable_surface} sqm left but old deduction {old_deduction_kom_identifier} requires {deduction_amount_sqm} sqm.")
print(f"Increase deductable surface by {rest_after_deduction} sqm")
eco_account.deductable_surface += abs(rest_after_deduction)
eco_account.save()
deduction = EcoAccountDeduction.objects.get_or_create(
account=eco_account,
surface=deduction_amount_sqm,
intervention=intervention
)
tmp_cursor.close()
return eco_account