#!/usr/bin/php id); define("INTERNAL_USER_USERNAME", $me->username); define("MFBASE_BYPASS_LOGIN", true); $mainlog = \mfLoghandler::singleton(); $log = new \mfLog_File(); $log->init(BASEDIR . "/var/log/rimo-import.log"); $disable_delete = false; $usage = "Usage: " . $argv[0] . " [argument|rimocluster]\n\nAvailable arguments:\n\t--list-rimo-clusters\tLists Clusters returned by Rimo API, then exits\n\t--help\t\t\tLists available arguments\n\nRuns import when no argument is given. If rimocluster is given, only imports this cluster.\n\n"; $command = false; $request_cluster = false; if ($argc > 1) { if ($argv[1] == "--list-rimo-clusters") { $command = "list-rimo-clusters"; } elseif ($argv[1] == "--help") { echo $usage; exit(0); } elseif (strpos($argv[1], "SDI") === 0) { $command = "cluster_only"; $request_cluster = $argv[1]; } else { die("Invalid argument.\n\n$usage"); } } $valid_adb_sources = ["rimo-rest-api", "citycom-oan-api"]; //$netowners = ["estmk", "rml"]; $netowners = ["estmk", "rml", "sbidi"]; //$netowners = ["sbidi"]; //$netowners = ["rml"]; $apiEdition = "prod"; $startdate = date("Y-m-d"); $starttime = date("Y-m-d.H-i"); $clusters = []; foreach ($netowners as $apiOwner) { $apiData = TT_RIMO_API_CREDS[$apiOwner][$apiEdition]; $apiUrl = $apiData["url"]; $apiToken = $apiData["key"]; //$processType = $apiData["type"]; if (!$apiUrl || !$apiToken) { echo "Api Daten für $apiOwner unvollständig\n"; } $epGetClusters = $apiUrl . RIMO_API_JSON_EP_GET_CLUSTERS; $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 "($apiOwner) 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("($apiOwner) Invalid GetClusters Response\n"); } foreach ($clustersResponse->item as $cluster) { $cluster_data = ["apiOwner" => $apiOwner, "apiKey" => $apiToken, "apiUrl" => $apiUrl, "source_id" => $cluster->id, "cluster" => $cluster]; $clusters[$cluster->id] = $cluster_data; } } // add Netzgebiete not from rimo foreach(\ADBNetzgebietModel::getAll() as $adb_cluster) { if(!in_array($adb_cluster->source, $valid_adb_sources)) { echo "Cluster ".$adb_cluster->name." not found in rimo\n"; continue; } if(!array_key_exists($adb_cluster->source_id, $clusters)) { $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"]; //$processType = $apiData["type"]; $cluster_rimo_id = $cluster->id; $cluster_name = $cluster->name; if ($command == "list-rimo-clusters") { echo "($apiOwner) $cluster_source_id | name: " . $cluster->name . "; label: " . $cluster->userLabel . "\n"; continue; } $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 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 = []; $baseParams = ['apiKey' => $apiToken]; $epGetBuilding = $apiUrl . RIMO_API_JSON_EP_QUERY_BUILDING; $epGetBuildings = $apiUrl . RIMO_API_JSON_EP_GET_BUILDINGS; $epGetWorkorders = $apiUrl . RIMO_API_JSON_EP_QUERY_WORKORDERS; $epGetService = $apiUrl . RIMO_API_JSON_EP_QUERY_SERVICE; $epGetGeoJsonForBuilding = $apiUrl . RIMO_API_JSON_EP_GET_JSON_FOR_BUILDING; $epGetGeoJsonForCluster = $apiUrl . RIMO_API_JSON_EP_GET_JSON_FOR_CLUSTER; /* * get Cluster Outline Coords */ $params = $baseParams; $params['clusterId'] = $cluster_rimo_id; $qs = http_build_query($params); $req_url = $epGetGeoJsonForCluster . "?" . $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 GeoJson for cluster $cluster_rimo_id\n"; continue; } $geodataResponse = json_decode($responseText); if (is_object($geodataResponse) && is_array($geodataResponse->features)) { foreach ($geodataResponse->features as $feature) { if (is_object($feature) && property_exists($feature, "geometry") && is_array($feature->geometry->coordinates)) { $coords = $feature->geometry->coordinates[0]; $poly = []; foreach ($coords as $coord) { $long = $coord[0]; $lat = $coord[1]; $poly[] = [$lat, $long]; } if ($poly) $poly = json_encode($poly); if ($adb_netzgebiet->borderpoly != $poly) { $adb_netzgebiet->borderpoly = $poly; $adb_netzgebiet->save(); } } } } $option_create_preorder = false; $preorder_only_oaid = false; $option_import_sbidi_orders = false; $option_import_sbidi_orders = $adb_netzgebiet->getOption("create_preorder"); $option_create_preorder = $adb_netzgebiet->getOption("create_preorder"); $preorder_only_oaid = $adb_netzgebiet->getOption("preorder_only_oaid"); $option_wo_ignore_status = $adb_netzgebiet->getOption("wo_ignore_status"); $option_delete_units = $adb_netzgebiet->getOption("delete_units"); $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); $hausnummer_count = 0; $hausnummer_found_count = 0; $homes_count = 0; $rimo_building_list = []; if (is_object($buildingsResponse) && property_exists($buildingsResponse, "item") && is_array($buildingsResponse->item) && count($buildingsResponse->item)) { foreach ($buildingsResponse->item as $building) { $hausnummer_count++; $rimo_building_id = $building->id; //$disable_delete = true; //if($rimo_building_id != "SDIBuilding_898208091393_1647266063") continue; //echo $rimo_building_id."\n"; $rimo_building_list[] = $rimo_building_id; $hausnummer = $AddressHelper->findUpdateAddressFromRimoBuilding($building); if (!$hausnummer) { //echo "Adresse nicht gefunden: $rimo_building_id\n"; if ($AddressHelper->find_address_error) { $addressErrors[] = $AddressHelper->find_address_error; } continue; } $hausnummer_found_count++; $hausnummer_id = $hausnummer->id; $hausnummer_changed = false; /* * Set Building Status based on Operational-/Executionstate */ if(is_object($building->executionState) && property_exists($building->executionState, "userLabel")) { $b_executionstate_id = $building->executionState->name; $b_executionstate_label = $building->executionState->userLabel; $b_operationalstate_id = $building->operationalState->name; $b_operationalstate_label = $building->operationalState->userLabel; if($b_executionstate_label != $hausnummer->rimo_ex_state) { $hausnummer->rimo_ex_state = $b_executionstate_label; $hausnummer->save(); $hausnummer_changed = true; } if($b_operationalstate_label != $hausnummer->rimo_op_state) { $hausnummer->rimo_op_state = $b_operationalstate_label; $hausnummer_changed = true; } if($b_executionstate_id == "99" && $hausnummer->visibility != "private") { echo "== Setting visibility to private because execution state $b_executionstate_id ($b_executionstate_label) [".$hausnummer->id."]\n"; $hausnummer->visibility = "private"; $hausnummer_changed = true; } if($b_operationalstate_id == "99" && $b_executionstate_id == "1") { //echo "is not2connect / gross planning\n"; if($hausnummer->rollout_info != "unscheduled" || $hausnummer->rollout != null) { $hausnummer->rollout = null; $hausnummer->rollout_info = "unscheduled"; $hausnummer_changed = true; } } // update Preorders /* should be done already in hausnummer->afterSave() * foreach(\PreorderModel::searchActive(["adb_hausnummer_id" => $hausnummer_id]) as $preorder) { $preorder->syncStatusFlagsFromAdb(); $preorder->resetSaveNesting(); } */ } if(is_object($building->buildingType) && property_exists($building->buildingType, "userLabel") && $building->buildingType->userLabel) { $hausnummer->rimo_type = strtolower($building->buildingType->userLabel); $hausnummer_changed = true; } if(!$building->userLabel && $hausnummer->rimo_user_label) { $hausnummer->rimo_user_label = null; $hausnummer_changed = true; } elseif($building->userLabel != $hausnummer->rimo_user_label) { $hausnummer->rimo_user_label = $building->userLabel; $hausnummer_changed = true; } if($hausnummer_changed) { $hausnummer->save(); $hausnummer->resetSaveNesting(); $hausnummer_changed = false; } $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++; } } $rimo_home_count = 0; if (property_exists($building->homes, "item") && is_array($building->homes->item) && count($building->homes->item)) { $rimo_home_count = count($building->homes->item); } $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)); } if(!$unit->oaid && $adb_netzgebiet->unit_create_oaid) { $unit->oaid = $unit->getNewOAID(); if(!$unit->save()) { die("Error saving new unit OAID\n" . print_r($unit, true)); } } \mfValuecache::singleton()->set("mfObjectmodel-adb_wohneinheit-".$unit->id, $unit); } } if (!property_exists($building->homes, "item") || !is_array($building->homes->item) | !count($building->homes->item)) { continue; } $rimo_home_count = count($building->homes->item); $rimo_home_list = []; foreach ($building->homes->item as $home) { if(!$home->id) continue; //$hausnummer = new \ADBHausnummer($hausnummer_id); //print_r($home);exit; $homes_count++; $home_rimo_id = $home->id; $home_name = $home->name; $unit_changed = false; $rimo_home_list[] = $home_rimo_id; //echo "$home_rimo_id\n";continue; if (array_key_exists($home_rimo_id, $existing_units_extref)) { $unit = $existing_units_extref[$home_rimo_id]; } 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" => $home_rimo_id, "num" => ++$last_unit_num ]); if (!$unit->save()) { die("Error saving new unit\n" . print_r($home, true)); } if(!$unit->oaid && $adb_netzgebiet->unit_create_oaid) { $unit->oaid = $unit->getNewOAID(); if(!$unit->save()) { die("Error saving new unit OAID\n" . print_r($unit, true)); } } \mfValuecache::singleton()->set("mfObjectmodel-adb_wohneinheit-".$unit->id, $unit); //$unit = new \ADBWohneinheit($unit->id); $hausnummer->resetSaveNesting(); $unit->resetSaveNesting(); //\mfValuecache::singleton()->set("adbhausnummer-save-nesting-level-".$hausnummer->id, 0); //\mfValuecache::singleton()->set("adbwohneinheit-save-nesting-level-".$unit->id, 0); } //$unit = new \ADBWohneinheit($unit->id); if($unit->rimo_deleted) { $unit->rimo_deleted = 0; $unit_changed = true; //echo "{$unit->id} unit undelete\n"; //$unit->save(["no_updates" => 1]); } if ($unit->extref != $home_rimo_id) { $unit->extref = $home_rimo_id; $unit_changed = true; /*if (!$unit->save()) { die("Error saving new extref on unit\n" . print_r($home, true)); }*/ //$unit = new \ADBWohneinheit($unit->id); /*$hausnummer->resetSaveNesting(); $unit->resetSaveNesting();*/ } /* * TODO: Status based on execution-/operationalstate */ if($home->executionState->userLabel != $unit->rimo_ex_state) { $unit->rimo_ex_state = $home->executionState->userLabel; $unit_changed = true; } if($home->operationalState->userLabel != $unit->rimo_op_state) { $unit->rimo_op_state = $home->operationalState->userLabel; $unit_changed = true; } /* * 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); $unit_changed = true; // check OAID if (property_exists($home->ftus->item[0], "oaidObject") && is_object($home->ftus->item[0]->oaidObject) && $home->ftus->item[0]->oaidObject->name) { if ($home->ftus->item[0]->oaidObject->name != $unit->oaid) { if (!$unit->oaid) { // assign oaid to home $rimo_oaid = $home->ftus->item[0]->oaidObject->name; $oaid = \OpenAccessIdModel::getFirst(["oaid" => $rimo_oaid]); if (!$oaid) { if($preorder_only_oaid) { // if campaign oaid handling "other" just import OAID and nothing else $unit->oaid = $rimo_oaid; $unit_changed = true; } else { 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->exported_to = "rmio"; $oaid->export_data = json_encode($exp_data); $oaid->save(); } } else { // TODO maybe check if OAIDs match? } } } } if($unit_changed) { $unit->save(); $unit->resetSaveNesting(); $unit_changed = false; } // get ports from service $params = $baseParams; $params["homeId"] = $home_rimo_id; $qs = http_build_query($params); $req_url = $epGetService . "?" . $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 Service for Home $home_rimo_id\n"; continue; } $serviceResponse = json_decode($responseText); if (is_object($serviceResponse) && property_exists($serviceResponse, "item") && is_array($serviceResponse->item) && count($serviceResponse->item)) { foreach ($serviceResponse->item as $service) { if ($service->masterItem->name != "FTTx Service (red)") continue; // get service state if(property_exists($service, "customerState") && is_object($service->customerState) && property_exists($service->customerState, "userLabel") && strtolower($service->customerState->userLabel) == "active") { // active => order 500 $preorder = \PreorderModel::getFirst(["adb_wohneinheit_id" => $unit->id]); if($preorder) { $preorder->resetSaveNesting(); $new_status = \PreorderstatusModel::getFirst(["code" => 500]); if($new_status) { if($preorder->status->code < $new_status->code) { $preorder->status_id = $new_status->id; $preorder->save(); $preorder->resetSaveNesting(); } } } } // update patch position if (!is_object($service->portA) || !$service->portA) continue; $fixedDN = $service->portA->fixEndDN; if ($fixedDN) { //echo "fixedDn found: $fixedDN\n"; $dn = getServiceDnValues($fixedDN); if (array_key_exists("Shelf", $dn) && array_key_exists("PatchPanel", $dn) && array_key_exists("Port", $dn)) { $sdicluster = null; if (array_key_exists("SDICluster", $dn)) { $sdicluster = $dn['SDICluster']; } $shelf = $dn['Shelf']; $patchpanel = $dn['PatchPanel']; $port = $dn['Port']; if ($sdicluster) { $unit->patch_cluster = $sdicluster; $unit_changed = true; } if ($shelf) { $unit->patch_shelf = $shelf; $unit_changed = true; } if ($patchpanel) { $unit->patch_module = $patchpanel; $unit_changed = true; } if ($port) { $unit->patch_port = $port; $unit_changed = true; } } } } } if($unit_changed) { $unit->save(); $unit->resetSaveNesting(); $unit_changed = false; } } if($option_delete_units) { // delete Homes not existing in Rimo $delete_strings = deleteHomesIfEligible($rimo_home_list, $hausnummer); if(is_array($delete_strings) && count($delete_strings)) { foreach($delete_strings as $delete_string) { $addressErrors[] = $delete_string; } } } /* * Get Geodata */ $params = $baseParams; $params["buildingId"] = $rimo_building_id; $qs = http_build_query($params); $req_url = $epGetGeoJsonForBuilding . "?" . $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 Workorders for building $rimo_building_id\n"; continue; } $geodataResponse = json_decode($responseText); //print_r($geodataResponse);exit; if (is_object($geodataResponse)) { if (property_exists($geodataResponse, "homeSection")) { if (property_exists($geodataResponse->homeSection, "features") && is_array($geodataResponse->homeSection->features)) { foreach ($geodataResponse->homeSection->features as $feature) { $home_trench = []; foreach ($feature->geometry->coordinates as $coords) { $long = $coords[0]; $lat = $coords[1]; $home_trench[] = [$lat, $long]; } if ($hausnummer->home_trench != $home_trench) { $hausnummer->home_trench = json_encode($home_trench); $hausnummer_changed = true; //$hausnummer = new \ADBHausnummer($hausnummer_id); } } } } if (property_exists($geodataResponse, "borderPoint")) { if (property_exists($geodataResponse->borderPoint, "features") && is_array($geodataResponse->borderPoint->features)) { foreach ($geodataResponse->borderPoint->features as $feature) { $coords = $feature->geometry->coordinates; //var_dump($coords);exit; $long = $coords[0]; $lat = $coords[1]; if ($hausnummer->borderpoint_lat != $lat || $hausnummer->borderpoint_long != $long) { $hausnummer->borderpoint_lat = $lat; $hausnummer->borderpoint_long = $long; $hausnummer_changed = true; //$hausnummer = new \ADBHausnummer($hausnummer_id); } } } } if (property_exists($geodataResponse, "trenches") && $geodataResponse->trenches->features) { $trenches = []; foreach ($geodataResponse->trenches->features as $feature) { $feature_coords = []; //var_dump($feature->geometry->coordinates);exit; foreach ($feature->geometry->coordinates as $coords) { $long = $coords[0]; $lat = $coords[1]; $feature_coords[] = [$lat, $long]; } $trenches[] = $feature_coords; } if (count($trenches)) { //var_dump($trenches);exit; $hausnummer->trenches = json_encode($trenches); $hausnummer_changed = true; //$hausnummer = new \ADBHausnummer($hausnummer_id); } } if($hausnummer_changed) { $hausnummer->save(); $hausnummer->resetSaveNesting(); $hausnummer_changed = false; } } } if($option_delete_units) { // Löscht in Rimo nicht (mehr) vorhandene Wohneinheiten, sofern keine Bestellung oder OAID foreach(\ADBHausnummerModel::search(["netzgebiet_id" => $adb_netzgebiet->id]) as $hausnummer) { if(!in_array($hausnummer->rimo_id, $rimo_building_list)) { // delete Homes not existing in Rimo $delete_strings = deleteHomesIfEligible([], $hausnummer); if(is_array($delete_strings) && count($delete_strings)) { foreach($delete_strings as $delete_string) { $addressErrors[] = $delete_string; } } if($disable_delete) continue; if(!\ADBWohneinheitModel::count(["hausnummer_id" => $hausnummer->id])) { // keine homes mehr übrig, lösche Hausnummer echo "[DD] Deleting Hausnummer ".$hausnummer->id.", da keine Wohneinheit mehr (extref ".$hausnummer->extref.", rimo_id: ".$hausnummer->rimo_id.")\n"; $addressErrors[] = "[DD] Deleting Hausnummer ".$hausnummer->id.", da keine Wohneinheit mehr (extref ".$hausnummer->extref.", rimo_id: ".$hausnummer->rimo_id.")"; $hausnummer->delete(); } } } } /* * get SBIDI Orders, to create Preorders later */ if($option_import_sbidi_orders) { $sbidi_orders_resp = \Sbidi_AnoApi::getOrders($cluster_rimo_id); if(!$sbidi_orders_resp) { echo "Error fetching SBIDI Orders\n"; } else { if(!property_exists($sbidi_orders_resp, "orders") || !is_array($sbidi_orders_resp->orders) || !count($sbidi_orders_resp->orders)) { echo "No Orders in SBIDI Orders response\n"; } else { foreach($sbidi_orders_resp->orders as $order) { if(!is_array($order->homes) || !count($order->homes)) continue; foreach($order->homes as $home) { if(!$home->home_id || !$home->work_order_id) continue; $sorder = \SbidiOrder::getFirst(["home_rimo_id" => $home->home_id]); if(!$sorder) { $sorder = \SbidiOrder::getFirst(["workorder_rimo_id" => $home->work_order_id]); } if(!$sorder) { // create SbidiOrder $sorder = \SbidiOrder::create([ "building_rimo_id" => $order->building_id, "home_rimo_id" => $home->home_id, "workorder_rimo_id" => $home->work_order_id, ]); } $sorder->update([ "uid" => $order->customer->uid, "firstname" => $order->customer->firstname, "lastname" => $order->customer->lastname, "address" => $order->customer->address, "zip" => $order->customer->zip, "city" => $order->customer->city, "country" => $order->customer->country, "email" => $order->customer->email, "phone" => $order->customer->phone, "order_date" => ($order->order_date) ? : null, ]); $building_type = "sd"; foreach($order->lines as $line) { if(strpos("MPH", $line->product_code) !== false) { $building_type = "md"; break; } } $sorder->building_type = $building_type; $sorder->save(); } } } } } /* * get workorders */ $params = $baseParams; $params["clusterId"] = $cluster_rimo_id; $qs = http_build_query($params); $req_url = $epGetWorkorders . "?" . $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 Workorders for building $rimo_building_id\n"; continue; } $workordersResponse = json_decode($responseText); //print_r($workordersResponse);exit; if (is_object($workordersResponse) && property_exists($workordersResponse, "item") && is_array($workordersResponse->item) && count($workordersResponse->item)) { foreach ($workordersResponse->item as $workorder) { //$wo_building_external_id = $workorder->location->id; $wo_home = false; $wo_home_external_id = false; if(is_object($workorder->home) && $workorder->home->id) { $wo_home_external_id = $workorder->home->id; } $rimo_workorder_id = $workorder->id; $rimo_workorder_name = $workorder->name; $workorder_status = $workorder->state->userLabel; $team_id = (is_array($workorder->teams->item) && count($workorder->teams->item)) ? $workorder->teams->item[0]->id : null; $team_name = (is_array($workorder->teams->item) && count($workorder->teams->item)) ? $workorder->teams->item[0]->name : null; $wo = \RimoWorkorderModel::getFirst(["rimo_id" => $rimo_workorder_id]); if($wo) { $test_wo = $wo->adb_wohneinheit; if($wo_home_external_id && $wo->adb_wohneinheit_id && (!$test_wo || !is_object($test_wo) || !$test_wo->id)) { // workorder ist nicht-exisentem Home zugewiesen // echtes home mit SDIHome finden $wo_new_home = \ADBWohneinheitModel::getFirst(['extref' => $wo_home_external_id]); if($wo_new_home) { // if home exists, update workorder to new wohneinheit_id $wo->adb_wohneinheit_id = $wo_new_home->id; $wo->save(); } } if($wo_home_external_id) { $wo_home = \ADBWohneinheitModel::getFirst(["extref" => $wo_home_external_id]); if($wo_home && $wo_home != $wo->adb_wohneinheit_id) { $addressErrors[] = "Wohneinheit für Workorder ".$wo->rimo_name." hat sich geändert von ".$wo->adb_wohneinheit_id." auf ".$wo_home->extref." (aber wurde nicht im Tool übernommen)"; } } else { $addressErrors[] = "Wohneinheit für Workorder ".$wo->rimo_name." ist jetzt leer"; } //echo "Updating Workorder $rimo_workorder_id ($workorder_home_id)\n"; if ($workorder_status != $wo->rimo_status) { //\mfValuecache::singleton()->set("adbhausnummer-save-nesting-level-".$hausnummer->id, 0); \mfValuecache::singleton()->set("adbwohneinheit-save-nesting-level-".$wo_home->id, 0); $wo->rimo_status = $workorder_status ?: ""; $wo->save(); } if ($team_id != $wo->rimo_team_id) { $wo->rimo_team_id = $team_id; $wo->save(); } if ($team_name != $wo->rimo_team_name) { $wo->rimo_team_name = $team_name; $wo->save(); } } else { if($workorder_status == "Deleted") continue; if(!$wo_home_external_id) continue; $wo_home = \ADBWohneinheitModel::getFirst(["extref" => $wo_home_external_id]); if (!$wo_home) { //echo "Home zu Workorder $rimo_workorder_id ($workorder_home_id) nicht gefunden\n"; continue; } //if(($option_wo_ignore_status == "Documented" && $workorder_status == "Documented") || $workorder_status == "Cancelled") continue; // dont import status > Executed //\mfValuecache::singleton()->set("adbhausnummer-save-nesting-level-".$hausnummer->id, 0); \mfValuecache::singleton()->set("adbwohneinheit-save-nesting-level-".$wo_home->id, 0); //echo "Creating Workorder $rimo_workorder_id ($workorder_home_id)\n"; $wo = \RimoWorkorderModel::create([ "adb_wohneinheit_id" => $wo_home->id, "rimo_id" => $rimo_workorder_id, "rimo_name" => $rimo_workorder_name, "rimo_status" => $workorder_status ?: "", "rimo_team_id" => $team_id, "rimo_team_name" => $team_name ]); if (!$wo->save()) { echo "Fehler beim Erstellen der RimoWorkorder $rimo_workorder_id ($wo_home_external_id)\n"; continue; } } // create Preorder if requested if($option_create_preorder && $wo_home) { if(\PreorderModel::getFirst(["adb_wohneinheit_id" => $wo_home->id])) { //echo "Preorder already exists for Workorder $rimo_workorder_id ($wo_home_external_id)\n"; continue; } $preorder = \PreorderModel::createFromRimoWorkorder($wo); if(!$preorder) { echo "Error creating Preorder for Workorder $rimo_workorder_id ($wo_home_external_id)\n"; } else { //var_dump($preorder); } } } } // save address errors if ($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", $addressErrors)); } echo "Cluster $cluster_rimo_id ($cluster_name): $hausnummer_count Buildings; $hausnummer_found_count Buildings verarbeitet; $homes_count Homes verarbeitet.\n"; } // get Netzgebiet FCPs echo "updating FCPs {$adb_netzgebiet->name}\n"; foreach(\ADBRimoFcp::getAll(["netzgebiet_id" => $adb_netzgebiet->id]) as $fcp) { if(!$fcp->rimo_id) continue; $fcp_external_id = $fcp->rimo_id; $params = $baseParams; $params["buildingId"] = $fcp_external_id; $qs = http_build_query($params); $req_url = $epGetBuilding . "?" . $qs; $req_ctx = stream_context_create($ctxOptsGet); $responseText = file_get_contents($req_url, false, $req_ctx); if ($responseText === false) { //echo "Error fetching FCP Building $fcp_external_id in cluster $cluster_rimo_id (" . $cluster->name . ")\n"; continue; } $buildingResponse = json_decode($responseText); $hausnummer_count = 0; $hausnummer_found_count = 0; $homes_count = 0; $rimo_building_list = []; if (!is_object($buildingResponse)) { continue; } $rimo_fcp = $buildingResponse; if(!property_exists($rimo_fcp, "executionState") || !is_object($rimo_fcp->executionState) || !property_exists($rimo_fcp->executionState, "name") || !property_exists($rimo_fcp->executionState, "userLabel") ) { continue; } if(!property_exists($rimo_fcp, "operationalState") || !is_object($rimo_fcp->operationalState) || !property_exists($rimo_fcp->operationalState, "name") || !property_exists($rimo_fcp->operationalState, "userLabel") ) { continue; } $fcp_execstate_id = $rimo_fcp->executionState->name; $fcp_execstate_label = $rimo_fcp->executionState->userLabel; $fcp_opstate_id = $rimo_fcp->operationalState->name; $fcp_opstate_label = $rimo_fcp->operationalState->userLabel; $fcp_changed = false; if($fcp->rimo_ex_state != $fcp_execstate_label) { $fcp->rimo_ex_state = $fcp_execstate_label; $fcp_changed = true; } if($fcp->rimo_op_state != $fcp_opstate_label) { $fcp->rimo_op_state = $fcp_opstate_label; $fcp_changed = true; } if($fcp_changed) { //var_dump($fcp); $fcp->update((array)$fcp); // see if buildings and preorders need to get flag 141 set if(defined("TT_PREORDER_RIMO_FCP_STATUS_MATRIX")) { foreach (TT_PREORDER_RIMO_FCP_STATUS_MATRIX as $matrix) { if (!$matrix["pf"] && !$matrix["hf"]) { continue; } //echo "checking if need to set pf {$matrix["pf"]} or hf {$matrix["hf"]}\n"; if (!is_array($matrix["fcpop"])) $matrix["fcpop"] = [$matrix["fcpop"]]; if (!is_array($matrix["fcpex"])) $matrix["fcpex"] = [$matrix["fcpex"]]; if (!in_array(strtolower($fcp->rimo_ex_state), $matrix["fcpex"])) continue; if (!in_array(strtolower($fcp->rimo_op_state), $matrix["fcpop"])) continue; foreach (\ADBHausnummerModel::search(["fcp_id" => $fcp->id]) as $hausnummer) { if (array_key_exists("hf", $matrix) && $matrix["hf"]) { //echo "setting hausnummer {$hausnummer->id} flag {$matrix["hf"]}\n"; $hausnummer->setStatusflag($matrix["hf"], 1); } if (array_key_exists("pf", $matrix) && $matrix["pf"]) { foreach (\PreorderModel::searchActive(["adb_hausnummer_id" => $hausnummer->id]) as $preorder) { //echo "setting preorder {$preorder->id} flag {$matrix["pf"]}\n"; $preorder->setStatusFlag($matrix["pf"], 1); } } } break; } } } } } //echo $response; echo "\n"; function getServiceDnValues($dn) { $dn = explode(":", $dn); $fields = []; foreach ($dn as $d) { $m = []; if (preg_match('/^([^{]+)\{([^}]+)\}$/', $d, $m)) { if (!$m[1] || !$m[2]) { continue; } $value = $m[1]; $key = $m[2]; if (array_key_exists($key, $fields)) { if (is_array($fields[$key])) { $fields[$key][] = $value; } else { $tmp = $fields[$key]; $fields[$key] = []; $fields[$key][] = $tmp; $fields[$key][] = $value; } } else { $fields[$key] = $value; } } } if (array_key_exists("Shelf", $fields) && is_array($fields['Shelf'])) { $shelf = implode(" ", $fields['Shelf']); $fields['Shelf'] = $shelf; } if (!array_key_exists("PatchPanel", $fields) && array_key_exists("Shelf", $fields)) { $m = []; if (preg_match('/^([MB]\d[^ ]*)\s+([MB]\d[^ ]*)/i', $fields['Shelf'], $m)) { foreach ([1, 2] as $i) { if (strlen($m[$i])) { $letter = substr(strtoupper($m[$i]), 0, 1); if ($letter == "B") { $fields['Shelf'] = $m[$i]; } if ($letter == "M") { $fields['PatchPanel'] = $m[$i]; } } } } } /*if(!array_key_exists("Shelf", $fields) && array_key_exists("SpliceBox", $fields)) { $fields['Shelf'] = $fields['SpliceBox']; }*/ return $fields; } function deleteHomesIfEligible(Array $rimo_home_list, Object $hausnummer) { global $disable_delete; if($disable_delete) return []; $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, $rimo_home_list)) { if($adb_unit->oaid) { $oaid = \OpenAccessIdModel::getFirstOaid($adb_unit->oaid); $oaid_units_count = \ADBWohneinheitModel::count(["oaid" => $adb_unit->oaid]); if($oaid && $oaid->origin == "ofaa" && $oaid_units_count === 1) { // last unit with this OAID $adb_unit->rimo_deleted = 1; $adb_unit->save(["no_updates" => 1]); continue; } } if(count($adb_unit->active_preorders)) { // don't delete if there is an active order $adb_unit->rimo_deleted = 1; $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.")"; \mfValuecache::singleton()->delete("mfObjectmodel-adb_wohneinheit-".$adb_unit->id); $adb_unit->delete(); } } return $return_strings; }