$origin])) { throw new Exception("Origin '$origin' not found!"); } $where .= " AND origin = '$origin'"; } if(array_key_exists("origin_id", $attributes) && $attributes['origin_id']) { $origin_id = $attributes['origin_id']; if(!OpenAccessIdModel::getFirst(["origin_id" => $origin_id])) { throw new Exception("OAID Set '$origin_id' not found!"); } $where .= " AND origin_id = '$origin_id'"; } //var_dump($where);exit; $res = $this->db->select("OpenAccessId", "*", "$where ORDER BY RAND() LIMIT 1"); if(!$this->db->num_rows($res)) { throw new Exception("Konnte keine zufällige freie OAID laden!"); } $data = $this->db->fetch_object($res); $this->load($data); return true; } public function exportToRimoAndAssignFtu() { // XXX for now only support ADB Addresses if(!$this->adb_wohneinheit_id) { return true; } if(!$this->id) { return false; } /*********************************************************************** * check Wohneinheit external data for FTU id (SDMMaterial_), or fetch it from RIMO */ $wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); if(!$wohneinheit->id || !$wohneinheit->extref) { return false; } $fetch_ftu = true; $unit_extdata = new StdClass(); if($wohneinheit->external_data) { $extdata = json_decode($wohneinheit->external_data); if(is_object($extdata)) { $unit_extdata = $extdata; } if(is_object($extdata) && isset($extdata->rimo) && isset($extdata->rimo->ftu->id) && $extdata->rimo->ftu->id) { //var_dump($extdata);exit; $fetch_ftu = false; } } if($fetch_ftu) { // query Home to get FTU data from RIMO - GET /queryHomeWithId $params['apiKey'] = RIMO_API_JSON_APIKEY; $params["homeId"] = $wohneinheit->extref; $ctx_opts = [ 'http' => [ 'method' => 'GET', 'header' => 'accept: application/json' ] ]; $qs = http_build_query($params); //echo $qs."\n"; $createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_QUERY_HOME; $get_url = $createOrderEp."?".$qs; $ctx = stream_context_create($ctx_opts); $this->log->debug(__METHOD__.": Getting Home to fetch FTU: $get_url"); //exit; $response = file_get_contents($get_url, false, $ctx); if($response === false) { $this->log->error("Fehler beim auslesen der FTU ".$this->oaid."\n"); return false; } //$preorder->workorder_export_date = date('U'); //$preorder->workorder_export_data = $response; //$preorder->save(); $resp_data = json_decode($response); if(!is_object($resp_data)) { $this->log->error(__METHOD__.": OAID ".$this->oaid.": Cannot fetch Home from RIMO! Invalid Response!"); return false; } if(!is_array($resp_data->ftus->item) || !count($resp_data->ftus->item)) { $this->log->warning(__METHOD__.": Homes ftus object has no items ".$this->oaid); return false; } $ftu_data = new StdClass(); $ftu_data->id = $resp_data->ftus->item[0]->id; $ftu_data->name = $resp_data->ftus->item[0]->name; if(!isset($unit_extdata->rimo)) { $unit_extdata->rimo = new StdClass(); } $unit_extdata->rimo->ftu = $ftu_data; $wohneinheit->external_data = json_encode($unit_extdata); $wohneinheit->save(); } else { // $fetch_ftu == true $this->log->debug(__METHOD__.": OAID ".$this->oaid.": Home FTU id known already"); } /*************************************************************************** * check if we have RIMO OAID data or create OAID in RIMO - POST /v1/oaid-management/oaids */ $existing_rimo_export_data = $this->getExportData("rimo"); if(!is_object($existing_rimo_export_data) || !isset($existing_rimo_export_data->oaid_id) || !isset($existing_rimo_export_data->name)) { $params = []; $params['apiKey'] = RIMO_API_JSON_APIKEY; $params['oaidName'] = $this->oaid; $ctx_opts = [ 'http' => [ 'method' => 'POST', 'header' => 'accept: application/json' ] ]; $qs = http_build_query($params); //echo $qs."\n"; $createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_CREATE_OAID; $post_url = $createOrderEp."?".$qs; $ctx = stream_context_create($ctx_opts); $this->log->debug(__METHOD__.": Creating OAID in Rimo: $post_url"); $response = file_get_contents($post_url, false, $ctx); //var_dump($response);exit; if($response === false) { $this->log->error("Fehler beim Erstellen der OAID in RIMO ".$this->oaid."\n"); $workorders_failed++; return false; } $resp_data = json_decode($response); if(!$resp_data->id || !$resp_data->name) { $this->log->warning(__METHOD__.": Create OAID returned no ID or oaid name ".$this->oaid); return false; } // mark OAID as exported $oaid_export_data = new StdClass(); if($this->export_data) { $oaid_export_data = json_decode($this->export_data); } if(!isset($oaid_export_data->rimo)) { $oaid_export_data->rimo = new StdClass(); } $oaid_export_data->rimo->oaid_id = $resp_data->id; $oaid_export_data->rimo->name = $resp_data->name; $oaid_export_data->rimo->ftu_id = null; $this->exported_to = "rimo"; $this->exported = date('U'); $this->export_data = json_encode($oaid_export_data); $this->save(); } else { $this->log->debug(__METHOD__.": OAID ".$this->oaid.": Already created in RIMO"); } /************************************************************************* * assign OAID to FTU - POST /v1/oaid-management/oaids/{oaidName}/ftu/{ftuExternalId} */ // check if we assigned the OAID to a RIMO FTU already $existing_rimo_export_data = $this->getExportData("rimo"); if(!is_object($existing_rimo_export_data) || !isset($existing_rimo_export_data->oaid_id) || !isset($existing_rimo_export_data->ftu_id)) { $params = []; $params['apiKey'] = RIMO_API_JSON_APIKEY; $ctx_opts = [ 'http' => [ 'method' => 'POST', 'header' => 'accept: application/json' ] ]; $qs = http_build_query($params); //echo $qs."\n"; $createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_ASSIGN_OAID_TO_FTU; $createOrderEp = str_replace("{oaidName}", $this->oaid, $createOrderEp); $createOrderEp = str_replace("{ftuExternalId}", $unit_extdata->rimo->ftu->id, $createOrderEp); $post_url = $createOrderEp."?".$qs; $ctx = stream_context_create($ctx_opts); $this->log->debug(__METHOD__.": Assigning OAID to FTU in Rimo: $post_url"); $response = file_get_contents($post_url, false, $ctx); if($response === false) { $this->log->error("Fehler beim Zuweisen der OAID '".$this->oaid."' zu RIMO FTU '".$unit_extdata->rimo->ftu->id."'\n"); $workorders_failed++; return false; } // add FTU id to external_data $oaid_export_data = new StdClass(); if($this->export_data) { $oaid_export_data = json_decode($this->export_data); } if(!isset($oaid_export_data->rimo)) { $oaid_export_data->rimo = new StdClass(); } $oaid_export_data->rimo->ftu_id = $unit_extdata->rimo->ftu->id; $oaid_export_data->rimo->ftu_name = $unit_extdata->rimo->ftu->name; $oaid_export_data->rimo->ftu_assigned_date = date('U'); $this->export_data = json_encode($oaid_export_data); $this->save(); } else { $this->log->debug(__METHOD__.": OAID ".$this->oaid.": Already assigned to FTU in Rimo"); } return true; } public function importFromCSV(File $file, $attributes = []) { $active = (int)$attributes['active']; $origin = $attributes['origin']; $origin_id = $attributes['origin_id']; $owner_id = (int)$attributes['owner_id']; try { $import_count = 0; $i = 0; $filename = $file->getFullPath(); $input = fopen($filename, "r"); while($csv = fgetcsv($input, 0, ";")) { $i++; if($i == 1) continue; if(!trim($csv[0])) { continue; } $name = trim($csv[0]); if(!$name) { $this->log->warning(__FILE__."::importFromCSV(): Name missing"); continue; } $oaid = OpenAccessIdModel::getFirst(["oaid" => $name]); if($oaid) continue; $oaid = OpenAccessIdModel::create([ "oaid" => $name, "active" => $active, "origin" => $origin, "origin_id" => ($origin_id) ? $origin_id : null, "owner_id" => $owner_id ]); if(!$oaid->save()) { $this->log->error(__FILE__."::importFromCSV(): Fehler beim Speichern OAID $name"); continue; } $import_count++; } return $import_count; } catch(Exception $e) { echo $e->getCode().": ".$e->getMessage();exit; return false; } } public function getExportData($key) { if(!$this->export_data) { return []; } else { $exdata = json_decode($this->export_data); if(!is_object($exdata)) { return []; } if(isset($exdata->$key)) { return $exdata->$key; } } } public function getProperty($name) { if($this->$name == null) { if(!$this->id) { return null; } if($name == "owner") { $this->owner = new Address($this->owner_id); return $this->owner; } if($name == "adb_wohneinheit") { $this->adb_wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); return $this->adb_wohneinheit; } $classname = ucfirst($name); $idfield = $name."_id"; $this->$name = new $classname($this->$idfield); if($this->$name->id) { return $this->$name; } else { return null; } } return $this->$name; } }