343 lines
10 KiB
PHP
343 lines
10 KiB
PHP
<?php
|
|
|
|
require_once APPDIR."/OpenAccessId/helper/rimo.php";
|
|
|
|
class OpenAccessId extends mfBaseModel {
|
|
private $owner;
|
|
private $adb_wohneinheit;
|
|
private $adb_wohneinheit_count;
|
|
private $termination;
|
|
|
|
public function loadRandomUnassigned($attributes = []) {
|
|
$where = "active = 1 AND assigned = 0 AND adb_wohneinheit_id IS NULL AND termination_id IS NULL AND exported = 0 AND exported_to IS NULL";
|
|
|
|
if(array_key_exists("owner_id", $attributes) && $attributes['owner_id']) {
|
|
$owner_id = (int)$attributes['owner_id'];
|
|
if(!$owner_id) {
|
|
throw new Exception("Besitzer muss eine Zahl sein");
|
|
}
|
|
$where .= " AND owner_id = $owner_id";
|
|
}
|
|
|
|
if(array_key_exists("origin", $attributes) && $attributes['origin']) {
|
|
$origin = $attributes['origin'];
|
|
if(!OpenAccessIdModel::getFirst(["origin" => $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() {
|
|
$rimo = new OpenAccessId_Helper_Rimo($this->oaid);
|
|
|
|
// 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) {
|
|
$json_data = json_decode($wohneinheit->external_data);
|
|
if(is_object($json_data)) {
|
|
$extdata = $json_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) {
|
|
$resp_data = $rimo->getFtuData($wohneinheit->extref);
|
|
|
|
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) || !$existing_rimo_export_data->oaid_id) || (!isset($existing_rimo_export_data->name) || !$existing_rimo_export_data->name)) {
|
|
|
|
$oaid_data = $rimo->getOaid();
|
|
// try to create, if it fails, it already exists
|
|
if(!$oaid_data) {
|
|
$this->createInRimo();
|
|
}
|
|
|
|
//$this->log->debug(__METHOD__.": OAID ".$this->oaid.": Already created in RIMO");
|
|
|
|
/*************************************************************************
|
|
* get OAID from rimo and check if it's the same FTU as our Wohneinheit
|
|
*/
|
|
|
|
$ftu_data = $wohneinheit->ftu_data;
|
|
if($ftu_data['id'] && $ftu_data['name']) {
|
|
|
|
$resp_data = $rimo->getOaid();
|
|
|
|
if(!$resp_data) {
|
|
// oaid was not found in rimo, try creating it
|
|
$this->createInRimo();
|
|
$resp_data = $rimo->getOaid();
|
|
}
|
|
|
|
$assign_oaid = false;
|
|
|
|
if($resp_data && $resp_data->terminiationUnit && $resp_data->terminiationUnit->id) {
|
|
if($ftu_data['id'] != $resp_data->terminiationUnit->id) {
|
|
$old_ftu_id = $resp_data->terminiationUnit->id;
|
|
|
|
// unassign oaid from FTU
|
|
$resp_data = $rimo->unassignOaid($old_ftu_id);
|
|
$assign_oaid = true;
|
|
|
|
}
|
|
} else {
|
|
$assign_oaid = true;
|
|
}
|
|
|
|
if ($assign_oaid) {
|
|
// assign oaid to Wohneinheit FTU
|
|
$resp_data = $rimo->assignOaid($ftu_data['id']);
|
|
|
|
// update OAID export data
|
|
$exp_data_update = json_decode($this->export_data);
|
|
$exp_data_update->rimo->ftu_id = $ftu_data['id'];
|
|
$exp_data_update->rimo->ftu_name = $ftu_data['name'];
|
|
$exp_data_update->rimo->ftu_assigned_date = date("U");
|
|
$this->export_data = json_encode($exp_data_update);
|
|
$this->save();
|
|
}
|
|
}
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
* 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)) {
|
|
$resp_data = $rimo->assignOaid($unit_extdata->rimo->ftu->id);
|
|
|
|
// add FTU id to external_data
|
|
$oaid_export_data = new StdClass();
|
|
if($this->export_data) {
|
|
$json_data = json_decode($this->export_data);
|
|
if(is_object($json_data)) {
|
|
$oaid_export_data = $json_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 createInRimo() {
|
|
$rimo = new OpenAccessId_Helper_Rimo($this->oaid);
|
|
|
|
$resp_data = $rimo->createOaid();
|
|
if(!$resp_data) {
|
|
return false;
|
|
}
|
|
|
|
// mark OAID as exported
|
|
$oaid_export_data = new StdClass();
|
|
if($this->export_data) {
|
|
$json_data = json_decode($this->export_data);
|
|
if(is_object($json_data)) {
|
|
$oaid_export_data = $json_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();
|
|
|
|
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;
|
|
}
|
|
|
|
if($name == "adb_wohneinheit_count") {
|
|
$this->adb_wohneinheit_count = ADBWohneinheitModel::count(["oaid" => $this->oaid]);
|
|
return $this->adb_wohneinheit_count;
|
|
}
|
|
|
|
$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;
|
|
}
|
|
} |