diff --git a/Layout/default/AddressDB/Index.php b/Layout/default/AddressDB/Index.php index 150c6d06d..1dec1b35b 100644 --- a/Layout/default/AddressDB/Index.php +++ b/Layout/default/AddressDB/Index.php @@ -234,6 +234,7 @@ $address->id])?>"> $address->id])?>" class="pl-1"> + $address->id])?>" onclick="if(!confirm('Addresse und alle Wohneinheiten wirklich löschen?')) return false;"> diff --git a/application/ADBWohneinheit/ADBWohneinheit.php b/application/ADBWohneinheit/ADBWohneinheit.php index 552ef9c6b..3a919910b 100644 --- a/application/ADBWohneinheit/ADBWohneinheit.php +++ b/application/ADBWohneinheit/ADBWohneinheit.php @@ -15,6 +15,13 @@ class ADBWohneinheit extends mfBaseModel { $this->table = "Wohneinheit"; } + protected function afterDelete() { + foreach(ADBWohneinheitStatusflagValueModel::search(["wohneinheit_id" => $this->id]) as $flag_value) { + $flag_value->delete(); + } + $this->refreshUnitCount(); + } + protected function afterSave() { if(!$this->id) return true; if(!$this->hausnummer_id) return true; diff --git a/application/AddressDB/AddressDBController.php b/application/AddressDB/AddressDBController.php index 409dccfcf..2d5d8fe62 100644 --- a/application/AddressDB/AddressDBController.php +++ b/application/AddressDB/AddressDBController.php @@ -411,6 +411,45 @@ class AddressDBController extends mfBaseController { } + protected function deleteAction() { + $id = $this->request->id; + if(!is_numeric($id) || $id < 1) { + $this->layout()->setFlash("Adresse nicht gefunden", "error"); + $this->redirect("AddressDB"); + } + + $hausnummer = new ADBHausnummer($id); + if(!$hausnummer->id) { + $this->layout()->setFlash("Adresse nicht gefunden", "error"); + $this->redirect("AddressDB"); + } + + foreach($hausnummer->wohneinheiten as $unit) { + if(PreorderModel::count(["adb_wohneinheit_id" => $unit->id])) { + $this->layout()->setFlash("Adresse kann nicht gelöscht werden, da Wohneinheiten in Verwendung sind", "error"); + $this->redirect("AddressDB"); + } + + $oaid = OpenAccessIdModel::getFirstOaid($unit->oaid); + if($oaid && $oaid->origin == "ofaa") { + $this->layout()->setFlash("Adresse kann nicht gelöscht werden, da Wohneinheiten mit vergebenen OFAA OAIDs vorhanden sind", "error"); + $this->redirect("AddressDB"); + } + } + + foreach($hausnummer->wohneinheiten as $unit) { + // delete workorders + foreach(RimoWorkorderModel::search(["adb_wohneinheit_id" => $unit->id]) as $wo) { + $wo->delete(); + } + } + + $hausnummer->delete(); + + $this->layout()->setFlash("Adresse erfolgreich gelöscht", "success"); + $this->redirect("AddressDB"); + } + protected function exportAction() { return $this->exportAddressdb(); }