240 lines
6.2 KiB
PHP
240 lines
6.2 KiB
PHP
<?php
|
|
|
|
class ADBWohneinheit extends mfBaseModel {
|
|
private $hausnummer;
|
|
private $rimo_workorder;
|
|
private $ftu_data;
|
|
private $status;
|
|
|
|
protected function init() {
|
|
$this->db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
$this->table = "Wohneinheit";
|
|
}
|
|
|
|
protected function afterSave() {
|
|
if(!$this->id) return true;
|
|
if(!$this->hausnummer_id) return true;
|
|
|
|
// ADBWohneinheit_onSave_noAutoUnitCount can be defined if doing bulk
|
|
// operations where unit count is calculated seperately
|
|
if(!defined("ADBWohneinheit_onSave_noAutoUnitCount") || !ADBWohneinheit_onSave_noAutoUnitCount) {
|
|
$unit_count = ADBWohneinheitModel::count(['hausnummer_id' => $this->hausnummer_id]);
|
|
$hausnummer = $this->getProperty("hausnummer");
|
|
if($hausnummer->id) {
|
|
$hausnummer->unit_count = $unit_count;
|
|
$hausnummer->save();
|
|
}
|
|
|
|
if($hausnummer->netzgebiet_id) {
|
|
$netzgebiet = new ADBNetzgebiet($hausnummer->netzgebiet_id);
|
|
if($netzgebiet->id) {
|
|
$netzgebiet->unit_count = ADBWohneinheitModel::count(['netzgebiet_id' => $hausnummer->netzgebiet_id]);
|
|
$netzgebiet->save();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static function parseHausnummerZusatz($text) {
|
|
|
|
$data = [];
|
|
$data['block'] = "";
|
|
$data['stock'] = "";
|
|
$data['stiege'] = "";
|
|
$data['tuer'] = "";
|
|
$data['zusatz'] = "";
|
|
|
|
$text = trim($text);
|
|
if(!$text) return $data;
|
|
$text = " ".$text;
|
|
|
|
$m = [];
|
|
if(preg_match('/(?:t(?:ü|ue)r|topp|door?)\s+(\d\d?|\w\w?)/i', $text, $m)) {
|
|
$data['tuer'] = $m[1];
|
|
$text = str_replace($m[0], "", $text);
|
|
}
|
|
|
|
if(preg_match('/(\d+)\.\s+(?:stock|geschoss|etage|level|floor)/i', $text, $m)) {
|
|
$data['stock'] = $m[1];
|
|
$text = str_replace($m[0], "", $text);
|
|
}
|
|
|
|
if(preg_match('/(?: stock(?:werk)?| geschoss| etage| level | floor)\s+(\d\d?|parterre|erdgescho(?:ss|ß))\.?/i', $text, $m)) {
|
|
$data['stock'] = $m[1];
|
|
$text = str_replace($m[0], "", $text);
|
|
}
|
|
|
|
if(preg_match('/(parterre|erdgescho(?:ss|ß))/i', $text, $m)) {
|
|
$data['stock'] = $m[1];
|
|
$text = str_replace($m[0], "", $text);
|
|
}
|
|
|
|
|
|
$text = trim(preg_replace('/\s{2,}/', "", $text));
|
|
|
|
if($text) {
|
|
$data['zusatz'] = $text;
|
|
}
|
|
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function getNewOAID() {
|
|
if(!$this->hausnummer_id) {
|
|
return false;
|
|
}
|
|
|
|
$bcode = $this->getProperty("hausnummer")->oaid;
|
|
//var_dump($bcode);
|
|
if(!$bcode) {
|
|
$hausnummer = $this->getProperty("hausnummer");
|
|
$hausnummer->oaid = $hausnummer->getNewOAID();
|
|
if(!$hausnummer->oaid) {
|
|
return false;
|
|
}
|
|
if(!$hausnummer->save()) {
|
|
return false;
|
|
}
|
|
$bcode = $hausnummer->oaid;
|
|
}
|
|
|
|
$codes = [];
|
|
|
|
// get existing codes
|
|
$res = $this->db->select("Wohneinheit", "oaid", "oaid like '$bcode.%'");
|
|
if($this->db->num_rows($res)) {
|
|
while($data = $this->db->fetch_object($res)) {
|
|
$codes[] = $data->oaid;
|
|
}
|
|
} else {
|
|
return $bcode.".001";
|
|
}
|
|
|
|
if(count($codes)) {
|
|
sort($codes);
|
|
}
|
|
|
|
$last_code = end($codes);
|
|
|
|
$m = [];
|
|
if(preg_match('/\.(\d+)$/', $last_code, $m)) {
|
|
if($m[1]) {
|
|
$last_num = $m[1];
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
$new_num = ++$last_num;
|
|
$code = $bcode.".".sprintf("%03d", $new_num);
|
|
return $code;
|
|
}
|
|
|
|
public function getExternalData() {
|
|
if(!$this->external_data) return [];
|
|
|
|
$extdata = json_decode($this->external_data, true);
|
|
if(is_array($extdata) && count($extdata)) {
|
|
return $extdata;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function getExternalRimoData() {
|
|
$extdata = $this->getExternalData();
|
|
if(!is_array($extdata) || !count($extdata)) {
|
|
return false;
|
|
}
|
|
//var_dump($extdata);exit;
|
|
if(array_key_exists("rimo", $extdata) && count($extdata['rimo'])) {
|
|
$rimo_data = $extdata['rimo'];
|
|
if(is_array($extdata['rimo']) && count($extdata['rimo'])) {
|
|
return $rimo_data;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function getProperty($name) {
|
|
if($this->$name == null) {
|
|
|
|
if($name == "hausnummer") {
|
|
$this->hausnummer = new ADBHausnummer($this->hausnummer_id);
|
|
return $this->hausnummer;
|
|
}
|
|
|
|
if($name == "rimo_workorder") {
|
|
$this->rimo_workorder = new RimoWorkorder();
|
|
if($this->id) {
|
|
$this->rimo_workorder = RimoWorkorderModel::getFirst(['adb_wohneinheit_id' => $this->id]);
|
|
}
|
|
return $this->rimo_workorder;
|
|
}
|
|
|
|
if($name == "ftu_data") {
|
|
$rimo_data = $this->getExternalRimoData();
|
|
|
|
if(!is_array($rimo_data) || !count($rimo_data)) {
|
|
return ['id' => "", 'name' => ""];
|
|
}
|
|
|
|
if(array_key_exists("ftu", $rimo_data)) {
|
|
$ftu_data = $rimo_data['ftu'];
|
|
if($ftu_data['id'] || $ftu_data['name']) {
|
|
$this->ftu_data = $ftu_data;
|
|
return $this->ftu_data;
|
|
}
|
|
}
|
|
return ['id' => "", 'name' => ""];
|
|
}
|
|
|
|
if($name == "status") {
|
|
$this->status = new ADBStatus($this->status_id);
|
|
return $this->status;
|
|
}
|
|
|
|
$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;
|
|
}
|
|
|
|
public function __toString() {
|
|
$parts = [];
|
|
if($this->block) {
|
|
$parts[] = "Block ".$this->block;
|
|
}
|
|
if($this->stiege) {
|
|
$parts[] = "Stiege ".$this->stiege;
|
|
}
|
|
if($this->stock) {
|
|
if(is_numeric($this->stock)) {
|
|
$parts[] = $this->stock.". Stock";
|
|
} elseif(preg_match('/^(erdgescho(ss|ß)|parterre)$/i', $this->stock)) {
|
|
$parts[] = "Erdgeschoss";
|
|
} else {
|
|
$parts[] = "Stock ".$this->stock;
|
|
}
|
|
}
|
|
if($this->tuer) {
|
|
$parts[] = "Tür ".$this->tuer;
|
|
}
|
|
if($this->zusatz) {
|
|
$parts[] = $this->zusatz;
|
|
}
|
|
return implode(" ", $parts);
|
|
}
|
|
}
|