273 lines
9.5 KiB
PHP
273 lines
9.5 KiB
PHP
<?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_OanApiClient(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);
|
|
}
|
|
|
|
if($unit->tuer != $home->door) {
|
|
$unit->tuer = $home->door;
|
|
$unit->save();
|
|
}
|
|
|
|
if($unit->block != $home->block) {
|
|
$unit->block = $home->block;
|
|
$unit->save();
|
|
}
|
|
|
|
if($unit->stock != $home->floor) {
|
|
$unit->stock = $home->floor;
|
|
$unit->save();
|
|
}
|
|
|
|
if($unit->stiege != $home->stairs) {
|
|
$unit->stiege = $home->stairs;
|
|
$unit->save();
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
// get Services to save ONT data
|
|
$services = $ccapi->getServices();
|
|
foreach($services as $service) {
|
|
|
|
if(!property_exists($service, "ont") || !$service->ont || !$service->ont->id) continue;
|
|
|
|
if(!property_exists($service, "location") || !$service->location || !$service->location->id) continue;
|
|
if(!property_exists($service->location, "sublocation") || !$service->location->sublocation || !$service->location->sublocation->id) continue;
|
|
//var_dump($service);
|
|
|
|
$extref = $this->citycomIdToHausnummerExtref($service->location->sublocation->id);
|
|
$unit = \ADBWohneinheitModel::getFirst(["extref" => $extref]);
|
|
if(!$unit) continue;
|
|
|
|
$preorder = \PreorderModel::getFirst(["adb_wohneinheit_id" => $unit->id]);
|
|
if(!$preorder) continue;
|
|
|
|
$pco = \PreorderCitycomOan::getFirst(["preorder_id" => $preorder->id]);
|
|
if(!$pco) {
|
|
$pco = \PreorderCitycomOan::create([
|
|
"preorder_id" => $preorder->id
|
|
]);
|
|
}
|
|
|
|
$ont_sn = $service->ont->serial;
|
|
$ont_gpid = $service->ont->fsan;
|
|
|
|
if($pco->ont_sn != $ont_sn || $pco->ont_gpid != $ont_gpid) {
|
|
$pco->ont_sn = $ont_sn;
|
|
$pco->ont_gpid = $ont_gpid;
|
|
$pco->save();
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |