diff --git a/konova/management/commands/kspMigrater/compensation_migrater.py b/konova/management/commands/kspMigrater/compensation_migrater.py index 9e9ea634..2c3c85c7 100644 --- a/konova/management/commands/kspMigrater/compensation_migrater.py +++ b/konova/management/commands/kspMigrater/compensation_migrater.py @@ -163,10 +163,13 @@ class CompensationMigrater(BaseMigrater): ) except ObjectDoesNotExist: # Very old data might contain entries from this deprecated list - state_code = KonovaCode.objects.get( - atom_id=state_type, - code_lists__in=[CODELIST_AFTER_STATE_BIOTOPES_ID] - ) + try: + state_code = KonovaCode.objects.get( + 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.execute(z_code_sql.format(pkey_entry)) z_code_results = tmp_cursor_z_code.fetchall() diff --git a/konova/management/commands/kspMigrater/eco_account_migrater.py b/konova/management/commands/kspMigrater/eco_account_migrater.py index 02fb0955..57ab15e0 100644 --- a/konova/management/commands/kspMigrater/eco_account_migrater.py +++ b/konova/management/commands/kspMigrater/eco_account_migrater.py @@ -4,8 +4,8 @@ from django.db import transaction from codelist.models import KonovaCode from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_COMPENSATION_HANDLER_ID -from compensation.models import EcoAccount, EcoAccountDocument -from intervention.models import Responsibility, Handler +from compensation.models import EcoAccount, EcoAccountDocument, EcoAccountDeduction +from intervention.models import Responsibility, Handler, Intervention from konova.management.commands.kspMigrater.compensation_migrater import CompensationMigrater from konova.models import Geometry 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_actions(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.save() @@ -115,7 +116,7 @@ class EcoAccountMigrater(CompensationMigrater): try: # Calculate area by transforming 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.geometry.geom = db_result_geom if not db_result_geom.empty else None instance.geometry.save() @@ -179,5 +180,60 @@ class EcoAccountMigrater(CompensationMigrater): eco_account.responsible.handler.save() eco_account.responsible.save() + tmp_cursor.close() + 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 \ No newline at end of file