api = $apiClient; $this->log = mfLoghandler::singleton(); } public function findOrCreateProduct($specs = []) { if(!array_key_exists("up", $specs) || !array_key_exists("down", $specs) || !is_numeric($specs["up"]) || !is_numeric($specs["down"])) { return false; } $up = $specs["up"]; $down = $specs["down"]; $search_name = false; if(array_key_exists("name", $specs) && $specs["name"]) { $search_name = $specs["name"]; } $products = $this->api->getProducts(); foreach($products as $product) { if($up == $product->bb_up && $down == $product->bb_down) { if($search_name) { if($product->name == $search_name) { return $product->id; } } else { return $product->id; } } } // not found, so create new product $name = "xinon_tt_{$down}_{$up}"; if(array_key_exists("name", $specs)) { $name = $specs["name"]; } $product_id = $this->api->createProduct([ "name" => $name, "bb_down" => $down, "bb_up" => $up ]); if(!$product_id) { return false; } return $product_id; } /** * Creates services in Citycom API * * $data = [ * "up" = 300, * "down" = 300, * "execution_date" = [Y-m-d | false], * "services" = CITYCOM_OAN_API_SERVICES_FOR_ORDER, * "product_name" = "thetool-test OAN 300/300" * ] * * @param $preorder Preorder * @param $sublocation_id integer * @param $data Array * @return boolean */ public function orderServices($preorder, $sublocation_id, $data) { if(!is_numeric($sublocation_id) || !$sublocation_id) { return false; } if(!array_key_exists("up", $data) || !array_key_exists("down", $data) || !is_numeric($data["up"]) || !is_numeric($data["down"])) { return false; } if(!array_key_exists("services", $data) || !is_array($data["services"]) || !count($data["services"])) { return false; } $up = $data["up"]; $down = $data["down"]; $product_name = $data["product_name"]; $execution_date = $data["execution_date"]; $want_services = $data["services"]; if(!$execution_date) { $execution_date = date("Y-m-d"); } $product_data = [ "up" => $up, "down" => $down, "name" => $product_name ]; // find or craete product $product_id = $this->findOrCreateProduct($product_data); if(!$product_id) { $this->log->error(__METHOD__.": no citycom product found for ".print_r($product_data, true)); return false; } // order all services and save ctags $cc_service_types = $this->api->getServiceTypes(); $new_services = []; $ctags = $preorder->getNextFreeCtags(); if(!$ctags) { $this->log->error(__METHOD__.": No New Free Ctags (Preorder ".$preorder->id.")"); } if(count($ctags) < count($want_services)) { $this->log->error(__METHOD__.": Not enough New Free CTags for Preorder ".$preorder->id); } $preorder_ctag_data = [ "preorder_id" => $preorder->id, "network" => "citycom-oan-api", "stag" => $preorder->adb_hausnummer->vlan_stag, ]; $service_count = 0; foreach($cc_service_types as $stype) { // was this service type requested if(!in_array($stype->name, $want_services)) continue; $ctag = $ctags[$service_count]; $ctag_service_type = array_flip(CITYCOM_OAN_API_SERVICES_FOR_ORDER)[$stype->name]; if(!$ctag_service_type) { $this->log->error(__METHOD__.": Cannot create Service ".$stype->name." for preorder ".$preorder->id." because no ctag service type defined"); } $service_data = [ "sublocation" => $sublocation_id, "service_type" => $stype->id, "product" => $product_id, "billing_date" => $execution_date, "ctag" => $ctag, ]; $this->log->info(__METHOD__.": Creating Service ".$stype->name." on sublocation $sublocation_id with product_id $product_id and ctag $ctag"); //continue; // register new Service with Citycom /*$new_service = $this->api->createService($service_data); if(!$new_service) { return false; }*/ $service_return = [ "ont" => [ "serial" => "ONT123456", "fsan" => "FSAN7890", ], ]; // save ctag $ctag_data = $preorder_ctag_data; $ctag_data["ctag"] = $ctag; $ctag_data["service_type"] = $ctag_service_type; $pct = PreorderCtag::create($ctag_data); $service_count++; if(!$pct->save()) { $this->log->error(__METHOD__.": Error saving PreorderCtag (Preorder ".$preorder->id.")"); return false; } try { $pct->configureNetwork(); } catch(Exception $e) { $this->log->error(__METHOD__.": Error configuring network equipment (Preorder ".$preorder->id.")"); } } return true; } public static function citycomIdToHausnummerExtref($id) { if(!$id) return false; return "citycom-$id"; } public static function hausnummerExtrefToCitycomId($extref) { if(!$extref) return false; return str_replace("citycom-", "", $extref); } }