Merge branch 'fronkdev' of code.fronk.at:fronk/thetool into fronkdev

This commit is contained in:
Frank Schubert
2025-07-16 17:37:38 +02:00
6 changed files with 750 additions and 16 deletions

View File

@@ -110,6 +110,171 @@ class AddressHelper
return [$strasse_name, $hausnummer_name, $addresszusatz];
}
public function findUpdateAddressFromCitycomLocation($location) {
$create_address_parts = false;
$update_freigabe = false;
$update_address = true;
if ($this->netzgebiet) {
$create_address_parts = $this->netzgebiet->getOption("create_address_parts");
$update_freigabe = $this->netzgebiet->getOption("update_freigabe");
$option_update_address = $this->netzgebiet->getOption("update_address");
if($option_update_address !== null) {
$update_address = $option_update_address;
}
}
$cc_id = $location->id;
$strasse_name = $location->street;
$hausnummer_name = $location->number;
$plz_name = $location->zip;
$gem_name = $location->city;
$stag = $location->stag;
if($gem_name == "Raaba") {
$gem_name = "Raaba-Grambach";
}
$addresszusatz = false;
$addr_dbg_str = "gem_name: $gem_name | plz_name: $plz_name | cc-id: $cc_id";
$extref = $this->citycomIdToHausnummerExtref($cc_id);
// get Gemeinde
$gemeinde = ADBGemeindeModel::getFirst(["name" => $gem_name]);
if (!$gemeinde) {
//$this->log->warning("[EE] Gemeinde $gem_name $gem_kz nicht gefunden ($addr_dbg_str)");
$this->logFindAddressError("[EE] Gemeinde '$gem_name' nicht gefunden ($addr_dbg_str)");
return false;
}
$gemeinde_id = $gemeinde->id;
// find by Extref
$hausnummer = ADBHausnummerModel::getFirst(["extref" => $extref]);
if(!$hausnummer) {
// find by address
$hausnummer = $this->findHausnummerByStreet($strasse_name, $hausnummer_name, $gemeinde_id);
}
if(!$hausnummer) {
// hausnummer anlegen
$plz = ADBPlzModel::getFirst(["gemeinde_id" => $gemeinde_id, "plz" => $plz_name]);
if (!$plz) {
$this->logFindAddressError("[EE] PLZ '$plz_name' nicht gefunden ($addr_dbg_str)");
return false;
}
$strasse = ADBStrasseModel::getFirst(["gemeinde_id" => $gemeinde_id, "name" => $strasse_name]);
if (!$strasse && $create_address_parts) {
$strasse = $this->createStreet($gemeinde_id, $strasse_name);
}
if (!$strasse) {
$this->logFindAddressError("[EE] Konnte Strasse '$strasse_name' in Gemeinde '$gemeinde_id' nicht finden bzw. anlegen ($addr_dbg_str)");
return false;
}
$hausnummer_data = [
"netzgebiet_id" => $this->netzgebiet->id,
"extref" => $extref,
"plz_id" => $plz->id,
"strasse_id" => $strasse->id,
"hausnummer" => $hausnummer_name,
"zusatz" => ($addresszusatz) ? $addresszusatz : null,
];
$hausnummer = ADBHausnummerModel::create($hausnummer_data);
//var_dump($hausnummer);exit;
//echo "----------------------\ncreating hausnummer:\n";
//print_r($hausnummer);
if(!$hausnummer->save()) {
$this->logFindAddressError("[EE] Error saving new Hausnummer ($addr_dbg_str)");
return false;
}
}
if(!$hausnummer->id) {
$this->logFindAddressError("[EE] Unable to find or create Hausnummer ($addr_dbg_str)");
return false;
}
$this->checkGemeindeNetzgebiet($gemeinde, $this->netzgebiet);
// update hausnummer
if ($hausnummer->netzgebiet_id != $this->netzgebiet->id) {
// only if overwriting netzgebiet_id is allowed in old netzgebiet
$dont_overwrite_netzgbiet = $hausnummer->netzgebiet->getOption("hausnummer_dont_overwrite_netzgebiet");
if (!$dont_overwrite_netzgbiet) {
$hausnummer->netzgebiet_id = $this->netzgebiet->id;
if(!$hausnummer->save()) {
$this->logFindAddressError("[EE] Error saving Hausnummer ($addr_dbg_str)");
return false;
}
}
}
if($update_address) {
if ($strasse_name != $hausnummer->strasse->name) {
$new_strasse = ADBStrasseModel::getFirst(["gemeinde_id" => $gemeinde_id, "name" => $strasse_name]);
if ($new_strasse) {
$this->logFindAddressError("[II] Updating Strasse from " . $hausnummer->strasse->name . " to " . $new_strasse->name . " ($addr_dbg_str)");
$hausnummer->strasse_id = $new_strasse->id;
if(!$hausnummer->save()) {
$this->logFindAddressError("[EE] Error saving Hausnummer ($addr_dbg_str)");
return false;
}
} else {
$this->logFindAddressError("[WW] Cannot update Strasse from " . $hausnummer->strasse->name . " to " . $strasse_name . " because not found ($addr_dbg_str)");
}
}
if ($hausnummer_name != $hausnummer->hausnummer) {
$this->logFindAddressError("[II] Updating Hausnummer from " . $hausnummer->hausnummer . " to " . $hausnummer_name . " ($addr_dbg_str)");
$hausnummer->hausnummer = $hausnummer_name;
if(!$hausnummer->save()) {
$this->logFindAddressError("[EE] Error saving Hausnummer ($addr_dbg_str)");
return false;
}
}
if ($plz_name != $hausnummer->plz->plz) {
$new_plz = ADBPlzModel::getFirst(["gemeinde_id" => $gemeinde_id, "plz" => $plz_name]);
if ($new_plz) {
$this->logFindAddressError("[II] Updating PLZ from " . $hausnummer->plz->plz . " to " . $new_plz->plz . " ($addr_dbg_str)");
$hausnummer->plz_id = $new_plz->id;
if(!$hausnummer->save()) {
$this->logFindAddressError("[EE] Error saving Hausnummer ($addr_dbg_str)");
return false;
}
} else {
$this->logFindAddressError("[WW] Cannot update PLZ from " . $hausnummer->plz->plz . " to " . $plz_name . " because not found ($addr_dbg_str)");
}
}
}
if ($hausnummer->extref != $extref) {
$hausnummer->extref = $extref;
if(!$hausnummer->save()) {
$this->logFindAddressError("[EE] Error saving Hausnummer ($addr_dbg_str)");
return false;
}
}
return $hausnummer;
}
public function citycomIdToHausnummerExtref($id) {
if(!$id) return false;
return "citycom-$id";
}
public function hausnummerExtrefToCitycomId($extref) {
if(!$extref) return false;
return str_replace("citycom-", "", $extref);
}
public function findUpdateAddressFromRimoBuilding($building)
{
$hausnummer = false;
@@ -134,7 +299,6 @@ class AddressHelper
}
}
$name = $this->trimExtra($building->name);
$strasse_hausnummer = trim($this->trimExtra($building->address->name));
$split_result = $this->splitStreetHausnummer($strasse_hausnummer);

View File

@@ -9,7 +9,7 @@ posix_setrlimit(POSIX_RLIMIT_FSIZE, 1024*1024*1024*5, 1024*1024*1024*5); // Limi
if(pidislocked()) {
_debug_log("ADB Rimo Import Broker läuft bereits (pidfile vorhanden)");
die("ADB Rimo Import Broker läuft bereits (pidfile vorhanden)");
die("ADB Rimo Import Broker läuft bereits (pidfile vorhanden)\n");
}
if(!lockpid()) {
die("Error creating lock file!\n");
@@ -18,7 +18,7 @@ if(!lockpid()) {
require("../../config/config.php");
$debug_log = false;
$valid_adb_sources = ["rimo-rest-api", "citycom-oan-api"];
/*
* Redirecting output so rlimit for filesize works
*/
@@ -243,6 +243,7 @@ while(1) {
unlockpid();
function loadClusters() {
global $valid_adb_sources;
$clusters = [];
$netowners = ["estmk", "rml", "sbidi"];
@@ -293,10 +294,21 @@ function loadClusters() {
foreach ($clustersResponse->item as $cluster) {
$cluster_data = ["apiOwner" => $apiOwner, "apiKey" => $apiToken, "apiUrl" => $apiUrl, "cluster" => $cluster];
$clusters[] = $cluster_data;
$clusters[$cluster->id] = $cluster_data;
}
}
foreach(ADBNetzgebietModel::getAll() as $netzgebiet) {
if(!in_array($netzgebiet->source, $valid_adb_sources)) continue;
if(!array_key_exists($netzgebiet->source_id, $clusters)) {
$netzgebiet->id = $netzgebiet->source_id;
$clusters[$netzgebiet->source_id] = ["apiOwner" => $apiOwner, "apiKey" => $apiToken, "apiUrl" => $apiUrl, "cluster" => $netzgebiet];
}
}
return $clusters;
}

View File

@@ -0,0 +1,218 @@
<?php
namespace ADBRimoImport\importer;
use \ADBRimoImport\ADBAddressHelper\AddressHelper;
class CitycomImporter {
private $log;
private $db;
private $netzgebiet;
public $addressErrors = [];
public function __construct($dependencies = []) {
foreach (["log", "db", "netzgebiet"] as $type) {
if (array_key_exists($type, $dependencies)) {
$this->$type = $dependencies[$type];
}
}
}
public function runImport() {
// get locations (Hausnummer) + Sublocations (Wohneinheiten)
$hausnummer_found_count = 0;
$ccapi = new \Citycom_OanApi(CITYCOM_OAN_API_USER, CITYCOM_OAN_API_PASS);
$locations = $ccapi->getLocations();
if(!is_array($locations)) {
$this->log->debug(__METHOD__.": Keine Locations von Citycom OAN API. Exiting Import.");
return true;
}
$AddressHelper = new AddressHelper(["log" => $this->log, "db" => $this->db, "netzgebiet" => $this->netzgebiet]);
foreach($locations as $location) {
// find Hausnummer or create it
$hausnummer = $AddressHelper->findUpdateAddressFromCitycomLocation($location);
if (!$hausnummer) {
//echo "Adresse nicht gefunden: $rimo_building_id\n";
if ($AddressHelper->find_address_error) {
$this->addressErrors[] = $AddressHelper->find_address_error;
}
continue;
}
$hausnummer_found_count++;
$hausnummer_id = $hausnummer->id;
$homes = $ccapi->getHomes($location->id);
if(!is_array($homes)) {
continue;
}
$homes_count = 0;
// find Wohneinheit or create it
$last_unit_num = 0;
$existing_units = [];
$existing_units_extref = [];
foreach (\ADBWohneinheitModel::search(["hausnummer_id" => $hausnummer->id]) as $unit) {
$existing_units[] = $unit;
$existing_units_extref[$unit->extref] = $unit;
if ($last_unit_num < $unit->num) {
$last_unit_num++;
}
}
//var_dump($existing_units);exit;
$rimo_home_count = 0;
if (is_array($homes) && count($homes)) {
$rimo_home_count = count($homes);
}
$to_create_count = $rimo_home_count - count($existing_units);
if ($rimo_home_count && $to_create_count > 0) {
for ($i = 0; $i < $to_create_count; $i++) {
$unit = \ADBWohneinheitModel::create([
"hausnummer_id" => $hausnummer->id,
"num" => ++$last_unit_num
]);
if (!$unit->save()) {
die("Error saving new unit\n" . print_r($unit, true));
}
}
}
$rimo_home_list = [];
foreach($homes as $home) {
if(!$home->id) continue;
$extref = $this->citycomIdToHausnummerExtref($home->id);
//$hausnummer = new \ADBHausnummer($hausnummer_id);
$homes_count++;
$home_cc_id = $home->id;
$home_oaid = $home->oan_id;
$rimo_home_list[] = $extref;
//echo "$home_rimo_id\n";continue;
if (array_key_exists($extref, $existing_units_extref)) {
$unit = $existing_units_extref[$extref];
} else {
// find free home without rimo_id
$unit = \ADBWohneinheitModel::getFirst(["hausnummer_id" => $hausnummer->id, "extref" => null]);
//echo "-- Using free unit\n";
}
if (!$unit) {
// create unit
$unit = \ADBWohneinheitModel::create([
"hausnummer_id" => $hausnummer->id,
"extref" => $extref,
"num" => ++$last_unit_num
]);
if (!$unit->save()) {
die("Error saving new unit\n" . print_r($home, true));
}
//$unit = new \ADBWohneinheit($unit->id);
}
if($home->oan_id) {
if($unit->oaid != $home->oan_id) {
$unit->oaid = $home->oan_id;
$unit->save();
}
$status_300 = \ADBStatusModel::getFirst(["code" => 300]);
if(!$status_300) {
die("ADB Status 245 not found");
}
if($unit->status->code < 300) {
$unit->status_id = $status_300->id;
$unit->save();
}
$status_code_241 = \ADBStatusModel::getFirst(["code" => 241]);
if($hausnummer->status->code < 241) {
$hausnummer->status_id = $status_code_241->id;
$hausnummer->save();
}
}
\mfValuecache::singleton()->set("adbhausnummer-save-nesting-level-".$hausnummer->id, 0);
\mfValuecache::singleton()->set("adbwohneinheit-save-nesting-level-".$unit->id, 0);
if($unit->rimo_deleted) {
$unit->rimo_deleted = 0;
$unit->save(["no_updates" => 1]);
}
if ($unit->extref != $extref) {
$unit->extref = $extref;
if (!$unit->save()) {
die("Error saving new extref on unit\n" . print_r($home, true));
}
//$unit = new \ADBWohneinheit($unit->id);
\mfValuecache::singleton()->set("adbhausnummer-save-nesting-level-".$hausnummer->id, 0);
\mfValuecache::singleton()->set("adbwohneinheit-save-nesting-level-".$unit->id, 0);
}
}
}
return true;
}
public function citycomIdToHausnummerExtref($id) {
if(!$id) return false;
return "citycom-$id";
}
public function hausnummerExtrefToCitycomId($extref) {
if(!$extref) return false;
return str_replace("citycom-", "", $extref);
}
private function deleteHomesIfEligible($ext_home_list, $hausnummer) {
$return_strings = [];
foreach (\ADBWohneinheitModel::search(["hausnummer_id" => $hausnummer->id]) as $adb_unit) {
$adb_unit->rimo_deleted = 1;
/*if(!$adb_unit->extref) {
$adb_unit->save(["no_updates" => 1]);
continue;
}
*/ // auskommentiert, um auch homes ohne rimo_id zu löschen
if(!in_array($adb_unit->extref, $ext_home_list)) {
if(count($adb_unit->active_preorders)) {
// don't delete if there is an active order
$adb_unit->save(["no_updates" => 1]);
continue;
}
echo "[DD] Deleting Hausnummer ".$adb_unit->hausnummer_id." Wohneinheit ".$adb_unit->id." (extref ".$adb_unit->extref.")\n";
$return_strings[] = "[DD] Deleting Hausnummer ".$adb_unit->hausnummer_id." Wohneinheit ".$adb_unit->id." (extref ".$adb_unit->extref.")";
$adb_unit->delete();
}
}
return $return_strings;
}
}

View File

@@ -113,7 +113,7 @@ foreach ($netowners as $apiOwner) {
}
foreach ($clustersResponse->item as $cluster) {
$cluster_data = ["apiOwner" => $apiOwner, "apiKey" => $apiToken, "apiUrl" => $apiUrl, "cluster" => $cluster];
$cluster_data = ["apiOwner" => $apiOwner, "apiKey" => $apiToken, "apiUrl" => $apiUrl, "source_id" => $cluster->id, "cluster" => $cluster];
$clusters[$cluster->id] = $cluster_data;
}
}
@@ -126,12 +126,13 @@ foreach(\ADBNetzgebietModel::getAll() as $adb_cluster) {
}
if(!array_key_exists($adb_cluster->source_id, $clusters)) {
//$clusters[$adb_cluster->source_id] = ["apiOwner" => "", "apiKey" => "", "apiUrl" => "", "cluster" => $adb_cluster];
$clusters[$adb_cluster->source_id] = ["apiOwner" => "", "apiKey" => "", "apiUrl" => "", "source_id" => $adb_cluster->source_id, "cluster" => $adb_cluster];
}
}
foreach ($clusters as $cluster_data) {
$cluster = $cluster_data["cluster"];
$cluster_source_id = $cluster_data["source_id"];
$apiOwner = $cluster_data["apiOwner"];
$apiUrl = $cluster_data["apiUrl"];
$apiToken = $cluster_data["apiKey"];
@@ -142,34 +143,52 @@ foreach ($clusters as $cluster_data) {
$cluster_name = $cluster->name;
if ($command == "list-rimo-clusters") {
echo "($apiOwner) $cluster_rimo_id | name: " . $cluster->name . "; label: " . $cluster->userLabel . "\n";
echo "($apiOwner) $cluster_source_id | name: " . $cluster->name . "; label: " . $cluster->userLabel . "\n";
continue;
}
$adb_netzgebiet = \ADBNetzgebietModel::getFirst(['rimo_id' => $cluster_rimo_id]);
$adb_netzgebiet = \ADBNetzgebietModel::getFirst(['source_id' => $cluster_source_id]);
if (!$adb_netzgebiet) {
//echo "Kein Netzgebiet für Salescluster $cluster_rimo_id (".$cluster->name.")\n";
continue;
}
//echo "command: $command request_cluster: $request_cluster - cluster_source_id $cluster_source_id\n";
if ($command == "cluster_only" && $request_cluster) {
if ($cluster_source_id != $request_cluster) {
continue;
}
}
/*
* import non-rimo clusters
*/
//echo $adb_netzgebiet->source." ".$adb_netzgebiet->source_id."\n";continue;
if($adb_netzgebiet->source == "citycom-oan-api" ) {
// load and run citycom importer
/*
$citycom_importer = new CitycomImporter(["db" => $adb, "log" => $log, "netzgebiet" => $adb_netzgebiet]);
echo "Netzgebiet ".$adb_netzgebiet->name." is Citycom OAN\n";
require_once __DIR__."/importer/citycom.php";
$citycom_importer = new importer\CitycomImporter(["db" => $adb, "log" => $log, "netzgebiet" => $adb_netzgebiet]);
$citycom_importer->runImport();
*/
// log errors
if ($citycom_importer->addressErrors) {
$netzname = preg_replace('/[^a-z0-9.-]/i', "_", $adb_netzgebiet->name);
$out_folder = dirname(__FILE__) . "/output/$startdate";
if (!file_exists($out_folder)) {
mkdir($out_folder);
}
$out_filename = $out_folder . "/output-$netzname-$starttime.log";
file_put_contents($out_filename, join("\n", $citycom_importer->addressErrors));
}
continue;
}
$addressErrors = [];
if ($command == "cluster_only" && $request_cluster) {
if ($cluster_rimo_id != $request_cluster) {
continue;
}
}
$baseParams = ['apiKey' => $apiToken];