Files
thetool/scripts/addressdb/gross-st-florian/02-addressdb_update_wohneinheiten.php
2023-10-03 16:45:51 +02:00

204 lines
6.0 KiB
PHP
Executable File

#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../../../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
$filename = __DIR__."/import/SDIHome__Homes_GSF_20231003.csv";
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$log = mfLoghandler::singleton();
$input = fopen($filename, "r");
$hausnummer_extref_trans = [
"25138081" => "25138353",
"25138090" => "25138402",
"25138104" => "25138399",
"25138112" => "25138285",
"25138116" => "25138401",
"25138172" => "25138384",
"25138229" => "25138283",
"25138246" => "25138346",
"25138249" => "25151924",
"25138261" => "25138413",
"25138282" => "25138388",
"25138284" => "25138391",
"25151923" => "25151924",
"25138547" => "25138605",
"25138576" => "25138603",
"25138578" => "25138599",
"25138427" => "25138493",
"25138431" => "25138494",
"25138480" => "25138532",
"25138507" => "25138479"
];
$wohnextref_zusatz = [
"SDIHome_92719443457_1666705401" => "Pfarrheim",
"SDIHome_92718863105_1666705401" => "Ölmühle",
"SDIHome_92718862081_1666705401" => "Ölmühle",
"SDIHome_92718073089_1666705401" => "Hinterhofgebäude",
"SDIHome_92718072065_1666705401" => "Hinterhofgebäude",
"SDIHome_92718071041_1666705401" => "Hinterhofgebäude",
"SDIHome_92718077185_1666705401" => "Lagerhalle und Werkstatt",
"SDIHome_92718076161_1666705401" => "Lagerhalle und Werkstatt",
"SDIHome_92719454721_1666705401" => "Zubau Musikheim",
"SDIHome_92719358977_1666705401" => "Firma",
"SDIHome_92719222273_1666705401" => "Wohnhaus",
"SDIHome_92719249921_1666705401" => "Wohnung",
"SDIBuilding_104216715265_1659686456" => "a",
"SDIHome_92719070465_1666705401" => "Nebengebäude"
];
$hausnummer_extref_ignore = [
"25138239",
"25158060",
"25158061"
];
$current_wohneinheit_nums = [];
$i = 0;
while($csv = fgetcsv($input, 0, ";")) {
$i++;
if($i == 1) continue;
if(!trim($csv[1])) {
continue;
}
$zusatz = false;
$wohneinheit_extref = trim($csv[37]);
$hausnummer_extref = trim($csv[35]);
$building_string = trim($csv[25]);
$adresse = "";
$m = [];
if(preg_match('/\[([^\]]+)\]/', $building_string, $m)) {
$adresse = $m[1];
}
if(array_key_exists($hausnummer_extref, $hausnummer_extref_trans)) {
$hausnummer_extref = $hausnummer_extref_trans[$hausnummer_extref];
}
if(in_array($hausnummer_extref, $hausnummer_extref_ignore)) continue;
$hausnummer = ADBHausnummerModel::getFirst(['extref' => $hausnummer_extref]);
if(!$hausnummer) {
if(preg_match('/(GST |^L\d)/', $adresse)) continue;
if($adresse) {
die("Hausnummer $hausnummer_extref nicht gefunden\n");
} else {
continue;
}
}
if(array_key_exists($wohneinheit_extref, $wohnextref_zusatz)) {
$zusatz = $wohnextref_zusatz[$wohneinheit_extref];
}
if(array_key_exists($hausnummer_extref, $current_wohneinheit_nums)) {
$current_wohneinheit_nums[$hausnummer_extref]++;
} else {
$current_wohneinheit_nums[$hausnummer_extref] = 1;
}
// get last wohneinheit
$num = $current_wohneinheit_nums[$hausnummer_extref];
$unit_count = ADBWohneinheitModel::count(["hausnummer_id" => $hausnummer->id]);
if($num > $unit_count) {
$last_we = getLastWohneinheit($hausnummer->id);
if($last_we) {
$num = ($last_we->num) ? $last_we->num + 1 : 1;
}
//var_dump($current_wohneinheit_nums[$hausnummer_extref]);
$wohneinheit_data = [
'extref' => $wohneinheit_extref,
'hausnummer_id' => $hausnummer->id,
'num' => $num,
'zusatz' => ($zusatz) ? $zusatz : null
];
$wohneinheit = ADBWohneinheitModel::create($wohneinheit_data);
$wohneinheit->save();
//echo "+ ".$wohneinheit->extref."\n";
} else {
$wohneinheit = ADBWohneinheitModel::getFirst(['hausnummer_id' => $hausnummer->id, "extref" => $wohneinheit_extref]);
if($wohneinheit) continue; // unit with extref exists so continue
if($hausnummer_extref == "25012281") {
$zusatz = "Haus ".(ceil($num / 12))." Top $num";
//echo "$num: $zusatz\n";
$wohneinheit = ADBWohneinheitModel::getFirst(['hausnummer_id' => $hausnummer->id, "zusatz" => $zusatz]);
if(!$wohneinheit) {
echo "top not found\n";
}
}
if($hausnummer_extref == "25012159") {
$m = [];
if(preg_match('/^Hauptstr.+Stg (\d+)/',$adresse, $m)) {
$stiege = $m[1];
if(!$stiege) die("Stiege nicht gefunden\n");
$wohneinheit = ADBWohneinheitModel::getFirst(['hausnummer_id' => $hausnummer->id, "zusatz" => "Stg. $stiege %", "extref" => null]);
$wohneinheit->stiege = $stiege;
} else {
die("no match: $adresse\n");
}
}
if(!$wohneinheit) {
// get unit with num
$wohneinheit = ADBWohneinheitModel::getFirst(['hausnummer_id' => $hausnummer->id, "num" => $num]);
}
// else get unit without num nor extref
if(!$wohneinheit) {
$wohneinheit = ADBWohneinheitModel::getFirst(['hausnummer_id' => $hausnummer->id, "num" => null, "extref" => null]);
}
}
if(!$wohneinheit) {
echo "Keine passende Wohneinheit gefunden für $hausnummer_extref ($num)\n";
exit;
}
if(!$wohneinheit->extref) {
$wohneinheit->extref = $wohneinheit_extref;
//var_dump("UPDATE UNIT",$wohneinheit);exit;
//echo "u ".$wohneinheit->extref."\n";
$wohneinheit->save();
//exit;
}
//continue;
//var_dump($wohneinheit);exit;
}
echo "\n";
function getLastWohneinheit($hausnummer_id) {
global $db;
$sql = "SELECT * FROM Wohneinheit WHERE hausnummer_id = $hausnummer_id ORDER BY num DESC LIMIT 1";
$res = $db->query($sql);
if($db->num_rows($res)) {
return new ADBWohneinheit($db->fetch_object($res));
}
return false;
}