diff --git a/konova/management/commands/migrate_ksp_konova.py b/konova/management/commands/migrate_ksp_konova.py index 330fc94d..fc4e31c3 100644 --- a/konova/management/commands/migrate_ksp_konova.py +++ b/konova/management/commands/migrate_ksp_konova.py @@ -1,12 +1,14 @@ import psycopg2 from django.contrib.gis.geos import MultiPolygon, GEOSException, MultiPoint, MultiLineString, Polygon from django.core.exceptions import ObjectDoesNotExist +from django.core.files.uploadedfile import UploadedFile from django.db import transaction from codelist.models import KonovaCode from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_REGISTRATION_OFFICE_ID, \ CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID, CODELIST_HANDLER_ID -from intervention.models import Intervention, Legal, Responsibility, Handler +from compensation.models import Payment +from intervention.models import Intervention, Legal, Responsibility, Handler, InterventionDocument from konova.management.commands.setup import BaseKonovaCommand from konova.models import Geometry from konova.tasks import celery_update_parcels @@ -148,6 +150,72 @@ class Command(BaseKonovaCommand): intervention.legal.save() return intervention + def _migrate_intervention_payment(self, intervention, eiv): + payment_date = eiv[16] + payment_amount = eiv[17] + + if payment_amount is not None and payment_amount != 0: + payment_exists = intervention.payments.filter( + amount=payment_amount + ).exists() + if payment_exists: + return intervention + + payment = Payment( + amount=payment_amount, + ) + if payment_date is None: + payment.comment = "Datenmigration: Kein Zahlungsdatum hinterlegt! Schnellstmöglich nachtragen oder diesen Kommentar mit einer Begründung ersetzen, falls kein Datum existiert." + else: + payment.due_on = payment_date + payment.save() + intervention.payments.add(payment) + return intervention + + def _migrate_intervention_documents(self, intervention, eiv): + eiv_identifier = f"'{eiv[0]}'" + tmp_cursor = self.db_connection.cursor() + tmp_cursor.execute( + 'select ' + 'doc.pfad, ' + 'doc."Bemerkung", ' + 'doc."Datum" ' + 'from "OBJ_MASTER" om ' + 'left join "LINFOS" linf on om."GISPADID"=linf."GISPADID" ' + 'left join "Objektphotos" doc on linf."PKEY"=doc."FKEY" ' + 'where ' + f'om."KENNUNG"={eiv_identifier} and ' + 'doc.pfad is not null ' + ) + doc_results = tmp_cursor.fetchall() + if len(doc_results) > 0: + for doc_result in doc_results: + doc_path = doc_result[0] + doc_comment = doc_result[1] + doc_date = doc_result[2] + with open(doc_path, encoding="latin1") as file: + file = UploadedFile(file) + doc_title = "Migrierte Datei" + doc_date = doc_date or "1970-01-01" + doc_exists = InterventionDocument.objects.filter( + instance=intervention, + title=doc_title, + comment=doc_comment, + date_of_creation=doc_date + ).exists() + if doc_exists: + continue + doc = InterventionDocument.objects.create( + title=doc_title, + comment=doc_comment, + file=file, + date_of_creation=doc_date, + instance=intervention, + ) + + tmp_cursor.close() + return intervention + def migrate_interventions(self): cursor = self.db_connection.cursor() cursor.execute( @@ -167,7 +235,9 @@ class Command(BaseKonovaCommand): 'adr."Bemerkung" as eingriffsverursacher_bemerkung, ' 'linf."Bemerkung" as eiv_comment, ' 'zt.erlass as Zulassungsdatum, ' - 'zt.rechtskraft as Bestandskraftdatum ' + 'zt.rechtskraft as Bestandskraftdatum, ' + 'zt.baubeginn as ersatzzahlungstermin, ' + 'eiv.ersatzzahlung ' 'from "OBJ_MASTER" om ' 'left join "LINFOS" linf on om."GISPADID"=linf."GISPADID" ' 'left join eiv on om."GISPADID"=eiv.gispadid ' @@ -188,10 +258,10 @@ class Command(BaseKonovaCommand): len_all_eivs = len(all_eivs) num_processed = 0 self._write_warning(f"Migrate EIVs to interventions...") - self._write_warning(f"Found {len_all_eivs} entries. Process now...") + self._write_warning(f"--Found {len_all_eivs} entries. Process now...") for eiv in all_eivs: if num_processed % 500 == 0: - self._write_warning(f" {num_processed}/{len_all_eivs} processed") + self._write_warning(f"----{num_processed}/{len_all_eivs} processed") with transaction.atomic(): eiv_comment = eiv[13] or "" intervention = Intervention.objects.get_or_create( @@ -203,7 +273,10 @@ class Command(BaseKonovaCommand): intervention = self._migrate_intervention_geometry(intervention, eiv) intervention = self._migrate_intervention_legal(intervention, eiv) intervention = self._migrate_intervention_responsibility(intervention, eiv) + intervention = self._migrate_intervention_payment(intervention, eiv) + intervention = self._migrate_intervention_documents(intervention, eiv) intervention.save() num_processed += 1 + cursor.close()