Added Preorder network Operators/ISPs & snopp api

This commit is contained in:
Frank Schubert
2024-04-18 14:00:25 +02:00
parent 6ce6e15b1a
commit 1819b8ee9c
16 changed files with 2090 additions and 951 deletions
@@ -0,0 +1,46 @@
<?php
namespace application\Api\v1\Modules\Operationaldata;
use application\Api\v1\Modules;
require_once(APPDIR."/Api/v1/Modules/ApiControllerModule.php");
/*
* API Endpoints for Operationaldata from SNOPP
*/
class Snopp extends Modules\ApiControllerModule
{
public function init()
{
}
public function setPreorderConnected($id) {
if(!$id || !is_numeric($id)) {
return \mfResponse::BadRequest(["message" => "id missing"]);
}
$wohneinheit = new \ADBWohneinheit($id);
if(!$wohneinheit->id) {
return \mfResponse::NotFound(["message" => "Home not found"]);
}
$preorder = \PreorderModel::getFirstActive(["adb_wohneinheit_id" => $id]);
if(!$preorder) {
return \mfResponse::NotFound(["message" => "Home not found"]);
}
$new_status = \PreorderstatusModel::getFirst(["code" => 300]);
if(!$new_status) {
return \mfResponse::InternalServerError();
}
if($preorder->status->code < $new_status->code) {
$preorder->status_id = $new_status->id;
$preorder->save();
}
return \mfResponse::Ok(["message" => "OK"]);
}
}
@@ -1,6 +1,5 @@
<?php
//namespace thetool\Api\Modules\Preorder;
namespace application\Api\v1\Modules\Preorder;
use application\Api\v1\Modules;
@@ -1,5 +1,8 @@
<?php
require_once(APPDIR."/Api/v1/Modules/Operationaldata/Snopp.php");
use application\Api\v1\Modules;
class OperationaldataApicontroller extends mfBaseApicontroller
@@ -28,21 +31,23 @@ class OperationaldataApicontroller extends mfBaseApicontroller
/*
* TODO: Load Api Modules automatically in mfBaseApiController
*/
/*$modules = [];
foreach (["Cif", "Activation"] as $moduleName) {
$classname = "application\\Api\\v1\\Modules\\Preorder\\" . $moduleName;
$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("/preorder/open", "getOpenPreorders", "GET");
$this->addRoute("/preorder/customerInstallationFeedback", [$modules["Cif"], "getCifData"], "GET");
$this->addRoute("/preorder/customerInstallationFeedback", [$modules["Cif"], "userSetCif"], "POST");
@@ -184,12 +189,112 @@ class OperationaldataApicontroller extends mfBaseApicontroller
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;
@@ -249,17 +354,19 @@ class OperationaldataApicontroller extends mfBaseApicontroller
}
/*foreach($b as $tmp_b) {
/*
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);
return mfResponse::Ok(["buildings" => $buildings]);
*/
}
}
+3 -1
View File
@@ -453,7 +453,9 @@ class PreorderModel {
if(array_key_exists("partner_id", $filter)) {
$partner_id = $filter['partner_id'];
if(is_numeric($partner_id)) {
if(is_array($partner_id)) {
$where .= " AND partner_id IN (".join(",", $partner_id).")";
} elseif(is_numeric($partner_id)) {
$where .= " AND partner_id=$partner_id";
}
}
@@ -15,6 +15,8 @@ class Preordercampaign extends mfBaseModel {
private $required_fields = [];
private $banned_fcps;
private $all_fcp_names;
private $active_operators;
private $passive_operators;
public function getAllFcpNames() {
if(!$this->id) return [];
@@ -184,7 +186,32 @@ class Preordercampaign extends mfBaseModel {
$this->banned_fcps = $banned_fcp_array;
return $this->banned_fcps;
}
if($name == "passive_operators") {
$pops = PreordercampaignOperatorModel::search(["preordercampaign_id" => $this->id, "type" => "passive"]);
if(!$pops) {
return [];
}
$this->passive_operators = [];
foreach($pops as $pop) {
$this->passive_operators[$pop->operator_id] = $pop;
}
return $this->passive_operators;
}
if($name == "active_operators") {
$aops = PreordercampaignOperatorModel::search(["preordercampaign_id" => $this->id, "type" => "active"]);
if(!$aops) {
return [];
}
$this->active_operators = [];
foreach($aops as $aop) {
$this->active_operators[$aop->operator_id] = $aop;
}
return $this->active_operators;
}
if($name == "apiusers") {
$items = PreordercampaignApiuserModel::search(["preordercampaign_id" => $this->id]);
foreach($items as $poa) {
@@ -375,13 +375,145 @@ class PreordercampaignController extends mfBaseController {
}
}
}
//var_dump($r->get());exit;
// sync active operators and ISPs
if(is_array($r->active_operators) && count($r->active_operators)) {
// delete removed operators
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "active"]) as $op) {
if(!in_array($op->operator_id, $r->active_operators)) {
$this->log->debug("deleting campaignoperator ".$op->id);
$op->delete(); // automatically deletes isps too
}
}
//var_dump($r->active_operators);exit;
// add missing operators
foreach($r->active_operators as $op_key => $op_id) {
if(!$op_id) continue;
$op = PreordercampaignOperatorModel::getFirst(["preordercampaign_id" => $campaign->id, "type" => "active", "operator_id" => $op_id]);
if(!$op) {
$op = PreordercampaignOperatorModel::create([
"preordercampaign_id" => $campaign->id,
"operator_id" => $op_id,
"type" => "active"
]);
$op->save();
}
//var_dump($r->active_isps, $op_id);exit;
// sync operators ISPs
if(is_array($r->active_isps) && array_key_exists($op_id, $r->active_isps) && count($r->active_isps[$op_id])) {
// delete removed ISPs
foreach(PreordercampaignOperatorIspModel::search(["campaignoperator_id" => $op_key]) as $isp) {
if(!array_key_exists($op_id, $r->active_isps) || !in_array($isp->isp_id, $r->active_isps[$op_id])) {
$this->log->debug("Deleting isp ".$isp->id." from operator $op_id");
$isp->delete();
}
}
// create new ISPs if not existing
//var_dump($op_key, $r->active_isps[$op_id]);exit;
foreach ($r->active_isps[$op_id] as $isp_id) {
if ($op_key == "new" || !PreordercampaignOperatorIspModel::getFirst(["campaignoperator_id" => $op_key, "isp_id" => $isp_id])) {
$new_isp = PreordercampaignOperatorIspModel::create([
"campaignoperator_id" => $op_key,
"isp_id" => $isp_id
]);
//var_dump($new_isp);exit;
if (!$new_isp->save()) {
$this->log->debug("Error saving isp " . print_r($new_isp, true));
}
$this->log->debug("created operatorIsp $op_id $isp_id");
}
}
} else {
// got empty ISP list, so delete all ISPs
if($op_id && $op_key != "new") {
foreach(PreordercampaignOperatorIspModel::search(["campaignoperator_id" => $op_key]) as $isp) {
$this->log->debug("Deleting all ISPs for $op_id");
$isp->delete();
}
}
}
}
} else {
// got empty operator list, delete all
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "active"]) as $op) {
$op->delete(); // automatically deletes isps too
}
}
if(is_array($r->passive_operators)) {
if(!count($r->passive_operators)) {
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "passive"]) as $op) {
$op->delete();
}
} else {
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "passive"]) as $op) {
if(!in_array($op->id, $r->passive_operators)) {
$op->delete();
}
}
foreach($r->passive_operators as $op_id) {
if(!$op_id) continue;
if(!PreordercampaignOperatorModel::getFirst(["preordercampaign_id" => $campaign->id, "type" => "passive", "operator_id" => $op_id])) {
$new_op = PreordercampaignOperatorModel::create([
"preordercampaign_id" => $campaign->id,
"operator_id" => $op_id,
"type" => "passive"
]);
$new_op->save();
}
}
}
} else {
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "passive"]) as $op) {
$op->delete();
}
}
$this->layout()->setFlash("Vorbestellkampagne erfolgreich gespeichert.", "success");
//$this->redirect("Preordercampaign", "Edit", ['id' => $new_id]);
$this->redirect("Preordercampaign");
if($r->return == "index") {
$this->redirect("Preordercampaign");
}
$this->redirect("Preordercampaign", "Edit", ['id' => $new_id]);
}
protected function billingAction() {
if(!$this->me->is("Admin")) {
$this->redirect("Preordercampaign");
}
$id = $this->request->id;
if(!is_numeric($id) || !$id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$campaign = new Preordercampaign($id);
if(!$campaign->id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
// Get mysqli resource from Model, so layout can output data as it's retrieved
// Works around lousy performance and horrendous memory usage
$res = PreorderModel::searchActive(["preordercampaign_id" => $id], [], true);
$this->layout()->setTemplate("Preorder/billing-export.csv");
$this->layout()->set("res", $res);
$this->layout()->set("campaign", $campaign);
}
protected function downloadAddonServices() {
$id = $this->request->preordercampaign_id;
if(!is_numeric($id) || !$id) {
@@ -0,0 +1,52 @@
<?php
class PreordercampaignOperator extends mfBaseModel
{
private $operator;
private $preordercampaign;
private $isps;
protected function beforeDelete() {
// delete all ISPs
foreach(PreordercampaignOperatorIspModel::search(["campaignoperator_id" => $this->id]) as $isp) {
$isp->delete();
}
return true;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "isps") {
$isps = PreordercampaignOperatorIspModel::search(["campaignoperator_id" => $this->id]);
if(!$isps) {
return [];
}
foreach($isps as $isp) {
$this->isps[$isp->isp_id] = $isp;
}
return $this->isps;
}
if($name == "operator") {
$operator = mfValuecache::singleton()->getMfObject("Address", $this->operator_id);
if(!$operator) {
return null;
}
$this->operator = $operator;
return $this->operator;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->getMfObject($classname, $this->$idfield);
if(!$this->$name) {
return null;
}
}
return $this->$name;
}
}
@@ -0,0 +1,169 @@
<?php
class PreordercampaignOperatorModel {
public $preordercampaign_id;
public $operator_id;
public $type;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new PreordercampaignOperator();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("PreordercampaignOperator", "*", "1=1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreordercampaignOperator($data);
}
}
return $items;
}
public static function getFirst($filter = null) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
mfLoghandler::singleton()->debug($where);
$res = $db->select("PreordercampaignOperator", "*", "$where ORDER BY id");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreordercampaignOperator($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getFirstOaid($oaid) {
$db = FronkDB::singleton();
if(!$oaid) return null;
$where = self::getSqlFilter(["oaid" => $oaid]);
//mfLoghandler::singleton()->debug($where);
$res = $db->select("PreordercampaignOperator", "*", "$where ORDER BY id");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreordercampaignOperator($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM PreordercampaignOperator
WHERE $where
";
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreordercampaignOperator
WHERE $where
ORDER BY id";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
$res = $db->query($sql);
//mfLoghandler::singleton()->debug($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreordercampaignOperator($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$db = FronkDB::singleton();
$where = "1=1 ";
if(!is_array($filter)) {
return $where;
}
if(array_key_exists("preordercampaign_id", $filter)) {
$preordercampaign_id = $filter['preordercampaign_id'];
if(is_numeric($preordercampaign_id)) {
$where .= " AND preordercampaign_id = $preordercampaign_id";
}
}
if(array_key_exists("operator_id", $filter)) {
$operator_id = $filter['operator_id'];
if(is_numeric($operator_id)) {
$where .= " AND operator_id = $operator_id";
}
}
if(array_key_exists("type", $filter)) {
$type = $db->escape($filter['type']);
if($type) {
$where .= " AND type LIKE '$type'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,30 @@
<?php
class PreordercampaignOperatorIsp extends mfBaseModel
{
private $isp;
public function getProperty($name) {
if($this->$name == null) {
if($name == "isp") {
$isp = mfValuecache::singleton()->getMfObject("Address", $this->isp_id);
if(!$isp) {
return null;
}
$this->isp = $isp;
return $this->isp;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->getMfObject($classname, $this->$idfield);
if(!$this->$name) {
return null;
}
}
return $this->$name;
}
}
@@ -0,0 +1,160 @@
<?php
class PreordercampaignOperatorIspModel {
public $campaignoperator_id;
public $isp_id;
public $type;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new PreordercampaignOperatorIsp();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("PreordercampaignOperatorIsp", "*", "1=1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreordercampaignOperatorIsp($data);
}
}
return $items;
}
public static function getFirst($filter = null) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
mfLoghandler::singleton()->debug($where);
$res = $db->select("PreordercampaignOperatorIsp", "*", "$where ORDER BY id");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreordercampaignOperatorIsp($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getFirstOaid($oaid) {
$db = FronkDB::singleton();
if(!$oaid) return null;
$where = self::getSqlFilter(["oaid" => $oaid]);
//mfLoghandler::singleton()->debug($where);
$res = $db->select("PreordercampaignOperatorIsp", "*", "$where ORDER BY id");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreordercampaignOperatorIsp($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM PreordercampaignOperatorIsp
WHERE $where
";
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreordercampaignOperatorIsp
WHERE $where
ORDER BY id";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
$res = $db->query($sql);
//mfLoghandler::singleton()->debug($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreordercampaignOperatorIsp($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$db = FronkDB::singleton();
$where = "1=1 ";
if(!is_array($filter)) {
return $where;
}
if(array_key_exists("isp_id", $filter)) {
$isp_id = $filter['isp_id'];
if(is_numeric($isp_id)) {
$where .= " AND isp_id = $isp_id";
}
}
if(array_key_exists("campaignoperator_id", $filter)) {
$campaignoperator_id = $filter['campaignoperator_id'];
if(is_numeric($campaignoperator_id)) {
$where .= " AND campaignoperator_id = $campaignoperator_id";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -105,7 +105,7 @@ class PreorderstatusModel {
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}