Files
thetool/application/Api/v1/OperationaldataApicontroller.php
2024-09-26 17:31:07 +02:00

373 lines
14 KiB
PHP

<?php
require_once(APPDIR."/Api/v1/Modules/Operationaldata/Snopp.php");
use application\Api\v1\Modules;
class OperationaldataApicontroller extends mfBaseApicontroller
{
//private $filter_gemeinde_ids = [];
//private $campaign;
private $campaign;
private $campaigns = [];
private $filter_salescluster_ids = [];
private $campaigns_by_scluster = [];
private $allowed_preordertypes = [];
private $district_is_city = false;
private $hausnummer_add_zusatz = false;
private $exist_is_error = false;
protected function init()
{
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
}
protected function registerRoutes()
{
/*
* TODO: Load Api Modules automatically in mfBaseApiController
*/
$modules = [];
foreach (["Snopp"] as $moduleName) {
$classname = "application\\Api\\v1\\Modules\\Operationaldata\\" . $moduleName;
$modules[$moduleName] = new $classname([
"get" => $this->get,
"post" => $this->post,
"db" => $this->db(),
"me" => $this->me
]);
}
$this->addRoute("/operationaldata/networks", "getNetworks", "GET");
$this->addRoute("/operationaldata/preordercampaigns", "getPreorderCampaigns", "GET");
$this->addRoute("/operationaldata/buildings", "getBuildingsByCampaign", "GET");
$this->addRoute("/operationaldata/home/:id/connected", [$modules["Snopp"], "setPreorderConnected"], "POST");
$this->addRoute("/operationaldata/home/:id/active", [$modules["Snopp"], "setPreorderActive"], "POST");
/*$this->addRoute("/preorder/open", "getOpenPreorders", "GET");
$this->addRoute("/preorder/customerInstallationFeedback", [$modules["Cif"], "getCifData"], "GET");
$this->addRoute("/preorder/customerInstallationFeedback", [$modules["Cif"], "userSetCif"], "POST");
$this->addRoute("/preorder/:code/clientInstallationFinished", [$modules["Cif"], "providerSetCif"], "POST");
$this->addRoute("/preorder/:code/serviceActivated", [$modules["Activation"], "setServiceActive"], "POST");
$this->addRoute("/preorder/:code", "getPreorder", "GET");
$this->addRoute("/preorder/:code", "cancelPreorder", "DELETE");
*/
}
/*
* is called after User is authenticated by API Key
*/
protected function authenticated()
{
$this->registerRoutes();
if ($this->me->is("preorderaddressreporting")) {
return mfResponse::Forbidden();
}
$campaignApiusers = PreordercampaignApiuserModel::search(["worker_id" => $this->me->id]);
foreach ($campaignApiusers as $campaignApiuser) {
$campaign = new Preordercampaign($campaignApiuser->preordercampaign_id);
if ($campaign->id) {
foreach (PreordercampaignSalesclusterModel::search(['preordercampaign_id' => $campaign->id]) as $campain_scluster) {
if (!in_array($campain_scluster->salescluster_id, $this->filter_salescluster_ids)) {
$this->filter_salescluster_ids[] = $campain_scluster->salescluster_id;
}
$this->campaigns_by_scluster[$campain_scluster->salescluster_id] = $campaign->id;
}
$this->campaigns[$campaign->id] = $campaign;
// get allowed preordertypes
if (is_array($campaign->types) && count($campaign->types)) {
foreach ($campaign->types as $type) {
$this->allowed_preordertypes[] = $type->type;
}
}
if ($campaign->district_is_city == 1) {
$this->district_is_city = true;
}
if ($campaign->hausnummer_add_zusatz == 1) {
$this->hausnummer_add_zusatz = true;
}
if ($campaign->exist_is_error == 1) {
$this->exist_is_error = true;
}
if ($campaign->require_connectiontype == 1) {
$this->require_connectiontype = true;
}
if ($campaign->allow_unit_update == 1) {
$this->allow_unit_update = true;
}
} else {
$this->log->debug(__METHOD__ . ": campaign not found (PreordercampaignApiuser::preordercampaign_id " . $campaignApiuser->preordercampaign_id . ")");
$this->log->debug(print_r($campaignApiuser, true));
}
foreach (PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) {
$this->addAllowedOrigin($origin->hostname);
}
}
$this->allowed_preordertypes = array_unique($this->allowed_preordertypes);
}
protected function getNetworks() {
$return = [];
$my_network_ids = [];
foreach($this->me->my_networks as $network) {
if($network->id && !in_array($network->id, $my_network_ids)) {
$my_network_ids[] = $network->id;
}
}
$networks = NetworkModel::search(["opsystem" => "snopp"]);
foreach($networks as $network) {
if(!in_array($network->id, $my_network_ids)) continue;
$name = $network->name;
if($network->adb_netzgebiet_id) {
$cityname = preg_replace('/^Liezen\s+-\s+/', '', $network->adb_netzgebiet->name);
$name = "ROC_".$network->adb_netzgebiet->extref."_".$cityname;
}
$net = [];
$net["id"] = $network->id;
$net["name"] = $name;
$net["created"] = $network->create;
$net["updated"] = $network->edit;
$return[] = $net;
}
return mfResponse::Ok(["networks" => $return]);
}
protected function getPreorderCampaigns() {
$return = [];
foreach($this->campaigns as $campaign) {
$c = [];
$c["id"] = $campaign->id;
$c["name"] = $campaign->name;
$c["created"] = $campaign->create;
$c["updated"] = $campaign->edit;
$return[] = $c;
}
return mfResponse::Ok(["preordercampaigns" => $return]);
}
protected function getBuildingsByCampaign() {
$buildings = [];
if(!array_key_exists("campaign_id", $this->get) || !$this->get["campaign_id"]) {
return mfResponse::BadRequest(["message" => "mandatory parameter campaign_id is missing."]);
}
$campaign_id = $this->get["campaign_id"];
if(!is_numeric($campaign_id) || $campaign_id < 1) {
return mfResponse::BadRequest(["message" => "Invalid value for campaign_id"]);
}
$campaign = new Preordercampaign($campaign_id);
if(!$campaign) {
return mfResponse::NotFound(["message" => "Network not found"]);
}
if($campaign->from > date('U') || $campaign->to < date('U')) {
return mfResponse::Ok(["buildings" => []]);
}
$adb_netzgebiet_id = $campaign->adb_netzgebiet->id;
if(!$adb_netzgebiet_id) {
return mfResponse::NotFound(["message" => "Network not found"]);
}
// nur Bestellungen von Aktivnetzbetreiber und seine ISPs
if(!count($campaign->active_operators)) {
return mfResponse::Ok(["buildings" => []]);
}
$isp_ids = [];
foreach($campaign->active_operators as $aop) {
foreach($aop->isps as $op_isp) {
if(!in_array($op_isp->id, $isp_ids)) {
$isp_ids[] = $op_isp->isp_id;
}
}
}
//var_dump($isp_ids);exit;
$b = [];
$woStateNameToId = [];
$woStateNameToId["Clarify"] = 5;
$woStateNameToId["Review"] = 7;
$woStateNameToId["Accepted"] = 8;
$woStateNameToId["Plan Released"] = 10;
$woStateNameToId["Assigned"] = 20;
$woStateNameToId["Executed"] = 30;
$woStateNameToId["Documented"] = 40;
$woStateNameToId["Cancelled"] = 99;
foreach(PreorderModel::searchActive(["preordercampaign_id" => $campaign_id, "partner_id" => $isp_ids]) as $preorder) {
$hausnummer_id = $preorder->adb_hausnummer_id;
$wohneinheit_id = $preorder->adb_wohneinheit_id;
if(!$hausnummer_id || !$wohneinheit_id) continue;
$hausnummer = $preorder->adb_hausnummer;
$wohneinheit = $preorder->adb_wohneinheit;
if(!array_key_exists($hausnummer_id, $b)) {
$b[$hausnummer_id] = [
'id' => $preorder->adb_hausnummer_id,
'status' => [
'code' => $preorder->status->code,
'text' => $preorder->status->name
],
'oaid' => $hausnummer->oaid,
'salescluster_id' => $preorder->preordercampaign_id,
'network_id' => $preorder->campaign->network_id,
'zip' => $hausnummer->plz->plz,
'city' => $hausnummer->strasse->gemeinde->name,
"municipality" => "",
'district' => $hausnummer->ortschaft->name,
'street' => $hausnummer->strasse->name,
'housenumber' => $hausnummer->hausnummer,
'gps_lat' => ($hausnummer->gps_lat) ? (float)$hausnummer->gps_lat : null,
'gps_long' => ($hausnummer->gps_long) ? (float)$hausnummer->gps_long : null,
"homes" => []
];
if($this->district_is_city) {
$b[$hausnummer_id]['city'] = $hausnummer->ortschaft->name;
$b[$hausnummer_id]['municipality'] = $hausnummer->strasse->gemeinde->name;
} else {
unset($b[$hausnummer_id]['municipality']);
}
}
if(!array_key_exists($wohneinheit_id, $b[$hausnummer_id]["homes"])) {
$b[$hausnummer_id]["homes"][] = [
'id' => $wohneinheit_id,
'oaid' => $wohneinheit->oaid,
'num' => (int)$wohneinheit->num,
'block' => $wohneinheit->block,
'stiege' => $wohneinheit->stiege,
'stock' => $wohneinheit->stock,
'tuer' => $wohneinheit->tuer,
'zusatz' => $wohneinheit->zusatz,
'patch_cluster' => $wohneinheit->patch_cluster,
'patch_shelf' => $wohneinheit->patch_shelf,
'patch_module' => $wohneinheit->patch_module,
'patch_port' => $wohneinheit->patch_port,
'workorder_id' => ($wohneinheit->rimo_workorder) ? $wohneinheit->rimo_workorder->id : null,
'workorder_name' => ($wohneinheit->rimo_workorder) ? $wohneinheit->rimo_workorder->rimo_name : null,
'workorder_state_id' => ($wohneinheit->rimo_workorder) ? $woStateNameToId[$wohneinheit->rimo_workorder->rimo_status] : null,
'workorder_state_label' => ($wohneinheit->rimo_workorder) ? $wohneinheit->rimo_workorder->rimo_status : null,
];
}
}
$buildings = array_values($b);
return mfResponse::Ok(["buildings" => $buildings]);
/*
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$sql = AddressDB::$wohneinheit_query;
$sql .= " WHERE Hausnummer.netzgebiet_id=$adb_netzgebiet_id AND Hausnummer.visibility = 'public'";
$res = $db->query($sql);
if(!$db->num_rows($res)) {
return mfResponse::Ok(["buildings" => []]);
}
$b = [];
while($data = $db->fetch_object($res)) {
if(!array_key_exists($data->hausnummer_id, $b)) {
$housenumber = $data->hausnummer;
if($this->hausnummer_add_zusatz) {
if($data->zusatz) {
$housenumber .= " (".$data->zusatz.")";
}
}
$b[$data->hausnummer_id] = [
'id' => $data->hausnummer_id,
'oaid' => $data->hausnummer_oaid,
'cluster_id' => $data->netzgebiet_extref,
'zip' => $data->plz,
'city' => $data->gemeinde,
"municipality" => "",
'district' => $data->ortschaft,
'street' => $data->strasse,
'housenumber' => $housenumber,
'gps_lat' => ($data->gps_lat) ? (float)$data->gps_lat : null,
'gps_long' => ($data->gps_long) ? (float)$data->gps_long : null,
"homes" => []
];
if($this->district_is_city) {
$b[$data->hausnummer_id]['city'] = $data->ortschaft;
$b[$data->hausnummer_id]['municipality'] = $data->gemeinde;
} else {
unset($b[$data->hausnummer_id]['municipality']);
}
}
$b[$data->hausnummer_id]["homes"][] = [
'id' => $data->wohneinheit_id,
'oaid' => $data->wohneinheit_oaid,
'num' => (int)$data->num,
'block' => $data->block,
'stiege' => $data->stiege,
'stock' => $data->stock,
'tuer' => $data->tuer,
'zusatz' => $data->zusatz,
];
}
/*
foreach($b as $tmp_b) {
$homes = array_values($tmp_b["homes"]);
return mfResponse::Ok(["Homes" => $homes]);
unset($tmp_b["homes"]);
$tmp_b["homes"] = array_values($homes);
$buildings[] = $tmp_b;
}
$buildings = array_values($b);
*/
}
}