diff --git a/ema/templates/ema/detail/includes/deadlines.html b/ema/templates/ema/detail/includes/deadlines.html index 761ce067..78c70ff1 100644 --- a/ema/templates/ema/detail/includes/deadlines.html +++ b/ema/templates/ema/detail/includes/deadlines.html @@ -49,7 +49,7 @@ {{ deadline.date|default_if_none:"---" }}
- {{ deadline.comment }} + {{ deadline.comment|linebreaks }}
diff --git a/konova/management/commands/kspMigrater/compensation_migrater.py b/konova/management/commands/kspMigrater/compensation_migrater.py index a56e3649..8f6dcf64 100644 --- a/konova/management/commands/kspMigrater/compensation_migrater.py +++ b/konova/management/commands/kspMigrater/compensation_migrater.py @@ -3,7 +3,8 @@ from django.db import transaction from codelist.models import KonovaCode from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID, \ - CODELIST_COMPENSATION_ACTION_DETAIL_ID, CODELIST_COMPENSATION_ACTION_ID + CODELIST_COMPENSATION_ACTION_DETAIL_ID, CODELIST_COMPENSATION_ACTION_ID, CODELIST_CONSERVATION_OFFICE_ID, \ + CODELIST_HANDLER_ID, CODELIST_COMPENSATION_HANDLER_ID from compensation.models import Compensation, CompensationState, CompensationAction, UnitChoices, CompensationDocument from ema.models import Ema, EmaDocument from intervention.models import Responsibility, Handler, Intervention @@ -63,6 +64,7 @@ class CompensationMigrater(BaseMigrater): compensation = self._migrate_compensation_type(compensation, kom) compensation = self._migrate_states(compensation, kom) compensation = self._migrate_deadlines(compensation, kom) + compensation = self._migrate_action_control_deadlines(compensation, kom) compensation = self._migrate_actions(compensation, kom) compensation = self._migrate_documents(compensation, CompensationDocument, kom) try: @@ -219,6 +221,92 @@ class CompensationMigrater(BaseMigrater): tmp_cursor.close() return compensation + def _migrate_action_control_deadlines(self, compensation, kom): + kom_identifier = f"'{kom[0]}'" + tmp_cursor = self.db_connection.cursor() + tmp_cursor.execute( + 'select ' + 'k."Datum", ' + 'k."Typ", ' + 'k."Ergebnis", ' + 'k."Stelle", ' + 'k."Bemerkung" ' + 'from "OBJ_MASTER" om ' + 'left join "MASSN" m on m."GISPADID"=om."GISPADID" ' + 'left join "Kontrolle" k on m."PKEY"=k."FKEY" ' + 'where ' + f'om."KENNUNG"={kom_identifier}' + ) + + control_types = { + 707175: "Zielerreichungskontrolle", + 707176: "Zustandskontrolle", + 707174: "Ausführungskontrolle", + } + control_results = { + 153097: "nicht beurteilbar", + 153954: "vollständig umgesetzt", + 153956: "teilweise umgesetzt", + 153957: "eher nicht umgesetzt", + 153960: "mit leichten Mängeln", + 153963: "ohne Mängel", + 187136: "Ziel nicht erreicht", + 187137: "Ziel teilweise erreicht", + 187139: "Ziel vollständig umgesetzt", + } + + db_results = tmp_cursor.fetchall() + for result in db_results: + control_date = result[0] + if control_date is None: + # useless data + continue + control_type = result[1] + control_result = result[2] + control_responsible = result[3] + control_comment = result[4] or "" + + try: + control_type = control_types[control_type] + except KeyError: + control_type = "Unbekannt" + try: + control_result = control_results[control_result] + except KeyError: + control_result = "Unbekannt" + + try: + control_responsible = KonovaCode.objects.get( + atom_id=control_responsible, + is_selectable=True, + is_archived=False, + code_lists__in=[CODELIST_COMPENSATION_HANDLER_ID] + ) + control_responsible = control_responsible.long_name + except ObjectDoesNotExist: + control_responsible = "Unbekannt" + + control_comment += f"\n\nKontrolltyp: {control_type}" + control_comment += f"\nKontrollergebnis: {control_result}" + control_comment += f"\nKontrollstelle: {control_responsible}" + + try: + deadline = compensation.deadlines.get( + type=DeadlineType.CONTROL, + date=control_date, + comment=control_comment + ) + except ObjectDoesNotExist: + deadline = Deadline.objects.create( + type=DeadlineType.CONTROL, + date=control_date, + comment=control_comment + ) + compensation.deadlines.add(deadline) + + tmp_cursor.close() + return compensation + def _migrate_deadlines(self, compensation, kom): compensation.deadlines.all().delete() kom_identifier = f"'{kom[0]}'" @@ -461,9 +549,110 @@ class EmaMigrater(CompensationMigrater): ema_obj = self._migrate_compensation_type(ema_obj, ema) ema_obj = self._migrate_states(ema_obj, ema) ema_obj = self._migrate_deadlines(ema_obj, ema) + ema_obj = self._migrate_action_control_deadlines(ema_obj, ema) ema_obj = self._migrate_actions(ema_obj, ema) ema_obj = self._migrate_documents(ema_obj, EmaDocument, ema) ema_obj.save() num_processed += 1 cursor.close() + + def _migrate_deadlines(self, ema_obj, ema_result): + ema_obj.deadlines.all().delete() + ema_identifier = f"'{ema_result[0]}'" + tmp_cursor = self.db_connection.cursor() + tmp_cursor.execute( + 'select ' + 't."Terminart", ' + 't."K_Termin"::date ' + 'from "OBJ_MASTER" om ' + 'left join "Termine" t on om."GISPADID"=t."GISPADID" ' + 'where ' + f'om."KENNUNG"={ema_identifier}' + ) + db_results = tmp_cursor.fetchall() + for result in db_results: + deadline_type = result[0] + deadline_comment = None + if deadline_type == 708166: + deadline_comment = "Wiedervorlage" + elif deadline_type == 708163: + deadline_comment = "Projektbeginn" + deadline_date = result[1] + if deadline_date is None: + # Useless data + continue + + try: + deadline = ema_obj.deadlines.get( + type=DeadlineType.OTHER, + date=deadline_date, + comment=deadline_comment + ) + except ObjectDoesNotExist: + deadline = Deadline.objects.create( + type=DeadlineType.OTHER, + date=deadline_date, + comment=deadline_comment + ) + ema_obj.deadlines.add(deadline) + + return ema_obj + + def _migrate_responsibility(self, ema_obj, ema_result): + ema_identifier = f"'{ema_result[0]}'" + tmp_cursor = self.db_connection.cursor() + tmp_cursor.execute( + 'select ' + 'adr."adr_pruef" as ets, ' + 'linf."AZ", ' + 'adr.behoerde, ' + 'adr.angaben ' + 'from "OBJ_MASTER" om ' + 'left join "LINFOS" linf on om."GISPADID"=linf."GISPADID" ' + 'left join adressrolle adr on adr."GISPADID"=om."GISPADID" ' + 'where ' + f'om."KENNUNG"={ema_identifier} ' + ) + db_results = tmp_cursor.fetchall() + if len(db_results) != 1: + raise AssertionError(f"{ema_identifier} has invalid responsibilities: {db_results}") + + db_results = db_results[0] + cons_office_code = db_results[0] + cons_file_number = db_results[1] + handler_type = db_results[2] + handler_detail = db_results[3] + + responsible = ema_obj.responsible or Responsibility.objects.create() + try: + conservation_office = KonovaCode.objects.get( + atom_id=cons_office_code, + is_selectable=True, + is_archived=False, + code_lists__in=[CODELIST_CONSERVATION_OFFICE_ID] + ) + except ObjectDoesNotExist: + raise ObjectDoesNotExist(f"{ema_identifier}, {db_results}") + try: + handler_type = KonovaCode.objects.get( + atom_id=handler_type, + is_selectable=True, + is_archived=False, + code_lists__in=[CODELIST_COMPENSATION_HANDLER_ID] + ) + except ObjectDoesNotExist: + handler_type = None + responsible.conservation_file_number = db_results[1] + + ema_obj.responsible.conservation_office = conservation_office + ema_obj.responsible.conservation_file_number = cons_file_number + handler = ema_obj.responsible.handler or Handler.objects.create() + handler.type = handler_type + handler.detail = handler_detail + ema_obj.responsible.handler = handler + ema_obj.responsible.handler.save() + ema_obj.responsible.save() + + tmp_cursor.close() + return ema_obj