290 lines
9.9 KiB
PHP
290 lines
9.9 KiB
PHP
<?php
|
|
|
|
class ADBWohneinheitController extends mfBaseController {
|
|
|
|
protected function init() {
|
|
$this->needlogin=true;
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->me = $me;
|
|
$this->layout()->set("me",$me);
|
|
|
|
if(!$me->is(["Admin", "netowner"]) && !$me->can("Preorder")) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
}
|
|
|
|
protected function addAction() {
|
|
$this->layout()->setTemplate("ADBWohneinheit/Form");
|
|
|
|
if($this->me->is("Admin")) {
|
|
$my_networks = NetworkModel::getAll();
|
|
} else {
|
|
$my_networks = $this->me->myNetworks(["netowner", "salespartner"]);
|
|
}
|
|
|
|
$netzgebiet_ids = [];
|
|
$my_adb_networks = [];
|
|
foreach($my_networks as $network) {
|
|
if($network->adb_netzgebiet_id && !in_array($network->adb_netzgebiet_id, $netzgebiet_ids)) {
|
|
$netzgebiet_ids[] = $network->adb_netzgebiet_id;
|
|
|
|
$adb_network = new ADBNetzgebiet($network->adb_netzgebiet_id);
|
|
if(!$adb_network->isLoaded()) continue;
|
|
$my_adb_networks[$network->adb_netzgebiet_id] = $adb_network;
|
|
}
|
|
}
|
|
$this->layout()->set("my_adb_networks", $my_adb_networks);
|
|
|
|
}
|
|
|
|
protected function editAction() {
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || $id < 1) {
|
|
$this->layout()->setFlash("Adresse nicht gefunden", "error");
|
|
$this->redirect("AddressDB");
|
|
}
|
|
|
|
$unit = new ADBWohneinheit($id);
|
|
if(!$unit->id) {
|
|
$this->layout()->setFlash("Wohneinheit nicht gefunden", "error");
|
|
$this->redirect("AddressDB");
|
|
}
|
|
|
|
/*if(!in_array($hausnummer->netzgebiet_id, $my_adb_networks)) {
|
|
$this->layout()->setFlash("Adresse nicht gefunden", "error");
|
|
$this->redirect("AddressDB");
|
|
}*/
|
|
|
|
$this->layout()->set("unit", $unit);
|
|
|
|
return $this->addAction();
|
|
}
|
|
|
|
protected function saveAction() {
|
|
$r = $this->request;
|
|
$id = $r->id;
|
|
//var_dump($r->get());exit;
|
|
|
|
$unit_data = [];
|
|
|
|
if(is_numeric($id) && $id > 0) {
|
|
$mode = "edit";
|
|
$unit = new ADBWohneinheit($id);
|
|
if(!$unit->id) {
|
|
$this->layout()->setFlash("Wohneinheit nicht gefunden", "error");
|
|
$this->redirect("AddressDB");
|
|
}
|
|
} else {
|
|
$mode = "add";
|
|
}
|
|
|
|
|
|
//$unit_data["oaid"] = ($r->oaid) ? trim($r->oaid) : null;
|
|
$unit_data["extref"] = ($r->extref) ? trim($r->extref) : null;
|
|
$unit_data["block"] = ($r->block) ? trim($r->block) : null;
|
|
$unit_data["stiege"] = ($r->stiege) ? trim($r->stiege) : null;
|
|
$unit_data["stock"] = ($r->stock) ? trim($r->stock) : null;
|
|
$unit_data["tuer"] = ($r->tuer) ? trim($r->tuer) : null;
|
|
$unit_data["zusatz"] = ($r->zusatz) ? trim($r->zusatz) : null;
|
|
$unit_data["bezeichner"] = ($r->bezeichner) ? trim($r->bezeichner) : null;
|
|
|
|
if($mode == "add") {
|
|
$unit = ADBWohneinheitModel::create($unit_data);
|
|
} else {
|
|
$unit->update($unit_data);
|
|
}
|
|
|
|
if(!$unit->save()) {
|
|
$this->layout()->setFlash("Beim Speichern der Wohneinheit ist ein Fehler aufgetreten.", "error");
|
|
$this->layout()->set("unit", $unit);
|
|
return $this->addAction();
|
|
}
|
|
|
|
$this->layout()->setFlash("Wohneinheit erfolgreich gespeichert.", "success");
|
|
$this->redirect("AddressDB", "view", ["id" => $unit->hausnummer_id]);
|
|
}
|
|
|
|
protected function deleteAction() {
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || $id < 1) {
|
|
$this->layout()->setFlash("Wohneinheit nicht gefunden", "error");
|
|
$this->redirect("AddressDB");
|
|
}
|
|
|
|
$unit = new ADBWohneinheit($id);
|
|
if(!$unit->id) {
|
|
$this->layout()->setFlash("Wohneinheit nicht gefunden", "error");
|
|
$this->redirect("AddressDB");
|
|
}
|
|
|
|
$hausnummer_id = $unit->hausnummer_id;
|
|
|
|
if($unit->oaid) {
|
|
$oaid = OpenAccessIdModel::getFirstOaid($unit->oaid);
|
|
if($oaid && $oaid->origin == "ofaa") {
|
|
$this->layout()->setFlash("Wohneinheit kann nicht gelöscht werden, da sie eine extern generierte OAID zugeordnet hat.", "error");
|
|
$this->redirect("AddressDB", "view", ["id" => $hausnummer_id]);
|
|
}
|
|
}
|
|
|
|
if(PreorderModel::getFirstActive(["adb_wohneinheit_id" => $unit->id])) {
|
|
$this->layout()->setFlash("Wohneinheit kann nicht gelöscht werden, da sie in einer Bestellung verwendet wird.", "error");
|
|
$this->redirect("AddressDB", "view", ["id" => $hausnummer_id]);
|
|
}
|
|
|
|
// delete workorders
|
|
foreach(RimoWorkorderModel::search(["adb_wohneinheit_id" => $unit->id]) as $wo) {
|
|
$wo->delete();
|
|
}
|
|
|
|
if(!$unit->delete()) {
|
|
$this->layout()->setFlash("Beim Löschen der Wohneinheit ist ein Fehler aufgetreten.", "error");
|
|
$this->redirect("AddressDB", "view", ["id" => $hausnummer_id]);
|
|
}
|
|
|
|
if(ADBWohneinheitModel::count(["hausnummer_id" => $hausnummer_id])) {
|
|
$count_unit = ADBWohneinheitModel::getFirst(["hausnummer_id" => $hausnummer_id]);
|
|
if($count_unit) {
|
|
$count_unit->refreshUnitCount();
|
|
}
|
|
} else {
|
|
$hausnummer = new ADBHausnummer($hausnummer_id);
|
|
if($hausnummer->id) {
|
|
$hausnummer->unit_count = 0;
|
|
$hausnummer->save();
|
|
}
|
|
}
|
|
|
|
|
|
$this->layout()->setFlash("Wohneinheit erfolgreich gelöscht.", "success");
|
|
$this->redirect("AddressDB", "view", ["id" => $hausnummer_id]);
|
|
|
|
}
|
|
|
|
protected function removeOaid() {
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || $id < 1) {
|
|
$this->layout()->setFlash("Wohneinheit nicht gefunden", "error");
|
|
$this->redirect("AddressDB");
|
|
}
|
|
|
|
$unit = new ADBWohneinheit($id);
|
|
if(!$unit->id) {
|
|
$this->layout()->setFlash("Wohneinheit nicht gefunden", "error");
|
|
$this->redirect("AddressDB");
|
|
}
|
|
|
|
if(!$unit->oaid) {
|
|
$this->layout()->setFlash("Wohneinheit hat keine OAID.", "error");
|
|
$this->redirect("AddressDB", "view", ["id" => $unit->hausnummer_id]);
|
|
}
|
|
|
|
$unit->oaid = null;
|
|
$unit->save();
|
|
|
|
$this->layout()->setFlash("OAID erfolgreich entfernt.", "success");
|
|
$this->redirect("AddressDB", "view", ["id" => $unit->hausnummer_id]);
|
|
|
|
}
|
|
|
|
protected function duplicateAction()
|
|
{
|
|
if (!$this->me->is("Admin") && !$this->me->can("ADBExtended")) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
|
|
$address_id = $this->me->is("Admin") ? null : $this->me->address->id;
|
|
$isAdmin = $this->me->is("Admin");
|
|
|
|
$duplicateHomes = array_merge(
|
|
ADBWohneinheitModel::searchDuplicateExtref([], $address_id),
|
|
ADBWohneinheitModel::searchDuplicateOAID([], $address_id),
|
|
ADBWohneinheitModel::getRimoDeletedHomes([], $address_id)
|
|
);
|
|
|
|
if ($isAdmin || $address_id === "4807") {
|
|
$duplicateHomes = array_merge($duplicateHomes, ADBWohneinheitModel::getUnscheduledOrderHomes([], 4807));
|
|
}
|
|
|
|
if ($isAdmin) {
|
|
$duplicateHomes = array_merge($duplicateHomes, ADBWohneinheitModel::getGreenfieldWithActivePreorders([], $address_id));
|
|
}
|
|
|
|
$getUniqueValues = fn($key) => array_values(array_unique(array_filter(array_column($duplicateHomes, $key))));
|
|
|
|
$networkOwners = array_map(
|
|
fn($owner) => ['value' => $owner, 'text' => $owner],
|
|
$getUniqueValues('netzgebiet_owner')
|
|
);
|
|
|
|
$networks = array_map(
|
|
fn($id) => ['value' => $id, 'text' => (new ADBNetzgebiet($id))->name],
|
|
$getUniqueValues('netzgebiet_id')
|
|
);
|
|
|
|
$this->layout()->set("vueViewName", "ADBWohneinheitDuplicate");
|
|
$this->layout()->setTemplate("VueViews/Vue");
|
|
$this->layout()->set("JSGlobals", [
|
|
"BASE_URL" => self::getUrl(""),
|
|
"DASHBOARD_URL" => self::getUrl("Dashboard"),
|
|
"MFAPPNAME" => MFAPPNAME_SLUG,
|
|
"PAGE_TITLE" => "Datenqualitäts-Checks",
|
|
"PATH" => [
|
|
["text" => MFAPPNAME_SLUG, "href" => self::getUrl("Dashboard")],
|
|
["text" => "Datenqualitäts-Checks", "href" => self::getUrl("ADBWohneinheit", "duplicate")],
|
|
],
|
|
"DUPLICATE_HOMES" => $duplicateHomes,
|
|
"ADB_NETZGEBIETE" => $networks,
|
|
"NETWORK_OWNERS" => $networkOwners,
|
|
"IS_ADMIN" => $isAdmin,
|
|
]);
|
|
}
|
|
|
|
protected function getContactsAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['id'])) {
|
|
self::returnJson(['success' => false, 'message' => 'Wohneinheit ID fehlt.']);
|
|
return;
|
|
}
|
|
|
|
$unit = new ADBWohneinheit($post['id']);
|
|
if (!$unit->id) {
|
|
self::returnJson(['success' => false, 'message' => 'Wohneinheit nicht gefunden.']);
|
|
return;
|
|
}
|
|
|
|
$contact = $unit->contact;
|
|
|
|
$contacts = !empty($contact) ? json_decode($contact, true) : [];
|
|
|
|
self::returnJson(['success' => true, 'contacts' => $contacts, 'header' =>
|
|
($unit->hausnummer->strasse ? $unit->hausnummer->strasse->name : '') . ' ' .
|
|
($unit->hausnummer ? $unit->hausnummer->hausnummer : '') . ', ' .
|
|
($unit->hausnummer->plz ? $unit->hausnummer->plz->plz : '') . ' ' .
|
|
($unit->hausnummer->ortschaft ? $unit->hausnummer->ortschaft->name : '')
|
|
]);
|
|
}
|
|
|
|
protected function saveContactsAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['id']) || !isset($post['data'])) {
|
|
self::returnJson(['success' => false, 'message' => 'ID oder Daten fehlen.']);
|
|
return;
|
|
}
|
|
|
|
$unit = new ADBWohneinheit($post['id']);
|
|
if (!$unit->id) {
|
|
self::returnJson(['success' => false, 'message' => 'Wohneinheit nicht gefunden.']);
|
|
return;
|
|
}
|
|
|
|
$unit->contact = json_encode($post['data']);
|
|
if ($unit->save()) {
|
|
self::returnJson(['success' => true, 'message' => 'Kontakt erfolgreich gespeichert.']);
|
|
} else {
|
|
self::returnJson(['success' => false, 'message' => 'Fehler beim Speichern der Kontakt.']);
|
|
}
|
|
}
|
|
}
|