266 lines
8.3 KiB
PHP
Executable File
266 lines
8.3 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
|
|
namespace ADBRimoImport;
|
|
use ADBRimoImport\ADBAddressHelper;
|
|
|
|
//require 'vendor/autoload.php';
|
|
require("../../config/config.php");
|
|
require("ADBAddressHelper/address_helper.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);
|
|
|
|
define("INTERNAL_USER_ID", $me->id);
|
|
define("INTERNAL_USER_USERNAME", $me->username);
|
|
|
|
$mainlog = \mfLoghandler::singleton();
|
|
$log = new \mfLog_File();
|
|
$log->init(BASEDIR."/var/log/rimo-import.log");
|
|
|
|
$apiOwner = "estmk";
|
|
$apiEdition = "prod";
|
|
$apiData = TT_RIMO_API_CREDS[$apiOwner][$apiEdition];
|
|
|
|
$apiUrl = $apiData["url"];
|
|
$apiToken = $apiData["key"];
|
|
|
|
$epGetClusters = $apiUrl.RIMO_API_JSON_EP_GET_CLUSTERS;
|
|
$epGetBuildings = $apiUrl.RIMO_API_JSON_EP_GET_BUILDINGS;
|
|
|
|
/*
|
|
if(!defined("RIMO_API_JSON_APIKEY_PROD")) {
|
|
die("rimo api token not defined!");
|
|
}*/
|
|
|
|
$import_count = 0;
|
|
|
|
$baseParams = ['apiKey' => $apiToken];
|
|
|
|
$ctxOptsPost = [
|
|
'http' => [
|
|
'method' => 'POST',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$ctxOptsGet = [
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$adb = \FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
|
|
|
|
/*
|
|
* Get RIMO Sales Clusters
|
|
*/
|
|
|
|
$params = $baseParams;
|
|
$qs = http_build_query($params);
|
|
|
|
$req_url = $epGetClusters."?".$qs;
|
|
$req_ctx = stream_context_create($ctxOptsGet);
|
|
|
|
echo $req_url."\n";
|
|
$responseText = file_get_contents($req_url, false, $req_ctx);
|
|
if($responseText === false) {
|
|
echo "Error fetching clusters\n";
|
|
exit;
|
|
}
|
|
|
|
$clustersResponse = json_decode($responseText);
|
|
//var_dump($clustersResponse);
|
|
//exit;
|
|
if(!is_object($clustersResponse) || !property_exists($clustersResponse, "item") || !is_array($clustersResponse->item) || !count($clustersResponse->item)) {
|
|
die("Invalid GetClusters Response\n");
|
|
}
|
|
|
|
foreach($clustersResponse->item as $cluster) {
|
|
$cluster_rimo_id = $cluster->id;
|
|
echo "$cluster_rimo_id | name: ".$cluster->name."; label: ".$cluster->userLabel."\n";
|
|
//continue;
|
|
$adb_netzgebiet = \ADBNetzgebietModel::getFirst(['rimo_id' => $cluster_rimo_id]);
|
|
if(!$adb_netzgebiet) {
|
|
echo "Kein Netzgebiet für Salescluster $cluster_rimo_id (".$cluster->name.")\n";
|
|
continue;
|
|
}
|
|
|
|
$AddressHelper = new ADBAddressHelper\AddressHelper(["log" => $log, "db" => $adb, "netzgebiet" => $adb_netzgebiet]);
|
|
|
|
/*
|
|
* Get Buildings per SalesCluster
|
|
*/
|
|
$params = $baseParams;
|
|
$params["clusterId"] = $cluster_rimo_id;
|
|
$qs = http_build_query($params);
|
|
|
|
$req_url = $epGetBuildings."?".$qs;
|
|
$req_ctx = stream_context_create($ctxOptsGet);
|
|
|
|
echo $req_url."\n";
|
|
$responseText = file_get_contents($req_url, false, $req_ctx);
|
|
|
|
if($responseText === false) {
|
|
echo "Error fetching Buildings in cluster $cluster_rimo_id (".$cluster->name.")\n";
|
|
exit;
|
|
}
|
|
|
|
$buildingsResponse = json_decode($responseText);
|
|
|
|
if(is_object($buildingsResponse) && property_exists($buildingsResponse, "item") && is_array($buildingsResponse->item) && count($buildingsResponse->item)) {
|
|
foreach($buildingsResponse->item as $building) {
|
|
//var_dump($building);
|
|
if($building->buildingType && $building->buildingType->userLabel != "Greenfield") {
|
|
//var_dump($building);
|
|
} else {
|
|
continue;
|
|
}
|
|
// ignore buildings without units
|
|
if(!$building->plannedTU) {
|
|
continue;
|
|
}
|
|
if($building->buildingType && $building->buildingType->userLabel != "Greenfield" && $building->homesCount > 0 && property_exists($building->homes, "item") && is_array($building->homes->item) && count($building->homes->item)) {
|
|
//print_r($building);exit;
|
|
|
|
$rimo_building_id = $building->id;
|
|
$hausnummer = $AddressHelper->findAddressFromRimoBuilding($building);
|
|
if(!$hausnummer) {
|
|
echo "Adresse nicht gefunden: $rimo_building_id\n";
|
|
continue;
|
|
}
|
|
|
|
$rimo_home_count = count($building->homes->item);
|
|
|
|
$last_unit_num = 0;
|
|
$existing_units = [];
|
|
foreach(\ADBWohneinheitModel::search(["hausnummer_id" => $hausnummer->id]) as $unit) {
|
|
$existing_units[$unit->extref] = $unit;
|
|
if($last_unit_num < $unit->num) {
|
|
$last_unit_num++;
|
|
}
|
|
}
|
|
|
|
foreach($building->homes->item as $home) {
|
|
print_r($home);//exit;
|
|
$home_rimo_id = $home->id;
|
|
$home_name = $home->name;
|
|
|
|
if(array_key_exists($home_rimo_id, $existing_units)) {
|
|
$unit = $existing_units[$home_rimo_id];
|
|
} else {
|
|
// find free home without rimo_id
|
|
$unit = \ADBWohneinheitModel::getFirst(["hausnummer_id" => $hausnummer->id, "extref" => null]);
|
|
}
|
|
|
|
if(!$unit) {
|
|
// create unit
|
|
$unit = \ADBWohneinheitModel::create([
|
|
"hausnummer_id" => $hausnummer->id,
|
|
"extref" => $home_rimo_id,
|
|
"num" => ++$last_unit_num
|
|
]);
|
|
if(!$unit->save()) {
|
|
die("Error saving new unit\n".print_r($home, true));
|
|
}
|
|
}
|
|
|
|
/*
|
|
* TODO: Status based on execution-/operational-state
|
|
*/
|
|
|
|
/*
|
|
* update FTU data
|
|
*/
|
|
$unit_extdata = new \StdClass();
|
|
if($unit->external_data) {
|
|
$extdata = json_decode($unit->external_data);
|
|
|
|
if(is_object($extdata)) {
|
|
$unit_extdata = $extdata;
|
|
}
|
|
}
|
|
|
|
if(is_array($home->ftus->item) && $home->ftus->item[0]) {
|
|
echo "in ftu update\n";
|
|
$ftu_data = new \StdClass();
|
|
$ftu_data->id = $home->ftus->item[0]->id;
|
|
$ftu_data->name = $home->ftus->item[0]->name;
|
|
|
|
if(!isset($unit_extdata->rimo)) {
|
|
$unit_extdata->rimo = new \StdClass();
|
|
}
|
|
|
|
$unit_extdata->rimo->ftu = $ftu_data;
|
|
$unit->external_data = json_encode($unit_extdata);
|
|
if(!$unit->save()) {
|
|
die("Error saving unit extdata\n".print_f($unit));
|
|
}
|
|
|
|
// check OAID
|
|
if($home->ftus->item[0]->oaidObject->name) {
|
|
if($home->ftus->item[0]->oaidObject->name != $unit->oaid) {
|
|
if(!$unit->oaid) {
|
|
// assign oad to home
|
|
$rimo_oaid = $home->ftus->item[0]->oaidObject->name;
|
|
$oaid = \OpenAccessIdModel::getFirst(["oaid" => $rimo_oaid]);
|
|
if(!$oaid) {
|
|
echo "Home hat fremde OAID: $rimo_oaid (Home ".$unit->id."\n";
|
|
} else {
|
|
$unit->oaid = $rimo_oaid;
|
|
|
|
$oaid->assigned = date("U");
|
|
$oaid->adb_wohneinheit_id = $unit->id;
|
|
$oaid->termination_id = null;
|
|
$oaid->address = $unit->hausnummer->getAddress();
|
|
$oaid->unit_string = (string)$unit;
|
|
if(!$oaid->exported) {
|
|
$oaid->exported = date("U");
|
|
}
|
|
|
|
if($oaid->export_data) {
|
|
$exp_data = json_decode($oaid->export_data);
|
|
} else {
|
|
$exp_data = new \stdClass();
|
|
}
|
|
if(!property_exists($exp_data, "rimo")) {
|
|
$exp_data->rimo = new \StdClass();
|
|
}
|
|
$exp_data->rimo->oaid_id = $home->ftus->item[0]->oaidObject->id;
|
|
$exp_data->rimo->name = $rimo_oaid;
|
|
$exp_data->rimo->ftu_id = $ftu_data->id;
|
|
$exp_data->rimo->ftu_name = $ftu_data->name;
|
|
$exp_data->rimo->ftu_assigned_date = date("U");
|
|
|
|
$oaid->export_data = json_encode($exp_data);
|
|
$oaid->save();
|
|
|
|
$unit->save();
|
|
}
|
|
|
|
} else {
|
|
//
|
|
}
|
|
}
|
|
}
|
|
}
|
|
exit;
|
|
}
|
|
|
|
//exit;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
//echo $response;
|
|
echo "\n"; |