Changes for Preorder:

- Added OAID to Hausnummer and Wohneinheit
- Added format parameter to /addressdb/findAddress
- Added additional fields to /addressdb/findAddress
- Updated api doc yaml
- Updated addressdb import scripts for Liezen
This commit is contained in:
Frank Schubert
2022-12-06 22:47:58 +01:00
parent d65e2c5f88
commit 5dbf05d55f
10 changed files with 8218 additions and 63 deletions

View File

@@ -37,6 +37,40 @@ class ADBHausnummer extends mfBaseModel {
return $data;
}*/
public function getNewOAID() {
if(!$this->plz_id) {
return false;
}
$cc = "AT";
$zip = $this->getProperty("plz")->plz;
for($try = 16; $try > 0; $try--) {
$rnd[0] = random_int(0, 255);
$rnd[1] = random_int(0, 255);
$rnd[2] = random_int(0, 255);
$rnd[3] = random_int(0, 255);
$code = "$cc-$zip-";
foreach($rnd as $r) {
$code .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
}
if(ADBHausnummerModel::search(['oaid' => $code])) {
$this->log->warn(__FILE__."::getNewObjectCode: New Code already in use. Trying again for a maximum of $try times.");
} else {
// code is unique
break;
}
}
if($try == 0) {
return null;
}
return $code;
}
public function getProperty($name) {
if($this->$name == null) {

View File

@@ -133,6 +133,13 @@ class ADBHausnummerModel {
}
}
if(array_key_exists("oaid", $filter)) {
$oaid = FronkDB::singleton()->escape($filter['oaid']);
if($oaid) {
$where .= " AND Hausnummer.`oaid` = '$oaid'";
}
}
if(array_key_exists("extref", $filter)) {
$extref = FronkDB::singleton()->escape($filter['extref']);
if($extref) {

View File

@@ -53,6 +53,49 @@ class ADBWohneinheit extends mfBaseModel {
return $data;
}
public function getNewOAID() {
if(!$this->hausnummer_id) {
return false;
}
$bcode = $this->getProperty("hausnummer")->oaid;
//var_dump($bcode);
if(!$bcode) {
return false;
}
$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 afterLoad() {
//$this->hausnummer = new ADBHausnummer($this->hausnummer_id);
//$this->loadStatus();

View File

@@ -105,7 +105,7 @@ class ADBWohneinheitModel {
LEFT JOIN Hausnummer ON (Hausnummer.id = Wohneinheit.hausnummer_id)
WHERE $where
GROUP BY Wohneinheit.id
ORDER BY hausnummer_id,block,stiege,LENGTH(stock),stock,LENGTH(tuer),tuer";
ORDER BY hausnummer_id,block,stiege,LENGTH(stock),stock,LENGTH(tuer),tuer,num";
mfLoghandler::singleton()->debug($sql);
if(is_array($limit) && count($limit)) {

View File

@@ -298,6 +298,10 @@ class AddressdbApicontroller extends mfBaseApicontroller {
$search_city = $this->db()->escape(trim($get['city']));
$search_housenumber = $this->db()->escape(trim($get['housenumber']));
$format = "flat";
if($get['format'] == "tree") {
$format = "tree";
}
if(!$search_street) {
return mfResponse::BadRequest(['message' => "Searchstring cannot be empty!"]);
@@ -332,51 +336,121 @@ class AddressdbApicontroller extends mfBaseApicontroller {
}
//echo $where;
//var_dump($this->filter_salescluster_ids);exit;
$sql = "SELECT * FROM view_wohneinheit WHERE $where ORDER BY plz, gemeinde, ortschaft, strasse, LENGTH(hausnummer), hausnummer, block, stiege, stock, LENGTH(tuer), tuer";
$sql = "SELECT * FROM view_wohneinheit WHERE $where ORDER BY plz, gemeinde, ortschaft, strasse, LENGTH(hausnummer), hausnummer, block, stiege, stock, LENGTH(num), num, LENGTH(tuer), tuer";
$this->log->debug($sql);
$res = $this->db()->query($sql);
if($this->db()->num_rows($res)) {
while($data = $this->db()->fetch_object($res)) {
// get allowed preorderTypes
$ptypes = [];
if($data->freigabe) {
$freigaben = json_decode($data->freigabe);
if(is_array($freigaben) && count($freigaben)) {
foreach($freigaben as $freigabe) {
if(in_array($freigabe, $this->allowed_preordertypes)) {
$ptypes[] = $freigabe;
if($format == "flat") {
while($data = $this->db()->fetch_object($res)) {
// get unit count
$unit_count = ADBWohneinheitModel::count(['hausnummer_id' => $data->hausnummer_id]);
// get allowed preorderTypes
$ptypes = [];
if($data->freigabe) {
$freigaben = json_decode($data->freigabe);
if(is_array($freigaben) && count($freigaben)) {
foreach($freigaben as $freigabe) {
if(in_array($freigabe, $this->allowed_preordertypes)) {
$ptypes[] = $freigabe;
}
}
}
}
}
$housenumber = $data->hausnummer;
if($this->hausnummer_add_zusatz) {
if($data->zusatz) {
$housenumber .= " (".$data->zusatz.")";
$housenumber = $data->hausnummer;
if($this->hausnummer_add_zusatz) {
if($data->zusatz) {
$housenumber .= " (".$data->zusatz.")";
}
}
$addresses[] = [
'oaid' => $data->wohneinheit_oaid,
'building_oaid' => $data->hausnummer_oaid,
'zip' => $data->plz,
'city' => ($this->district_is_city) ? $data->ortschaft : $data->gemeinde,
'district' => $data->ortschaft,
'street' => $data->strasse,
'housenumber' => $housenumber,
'lot_number' => $data->grund_nr,
'building_unit_count' => (int)$unit_count,
'num' => (int)$data->num,
'block' => $data->block,
'stock' => $data->stock,
'stiege' => $data->stiege,
'tuer' => $data->tuer,
'zusatz' => $data->zusatz,
'gps_lat' => (float)$data->gps_lat,
'gps_long' => (float)$data->gps_long,
'rollout_year' => ($data->rollout) ? (int)$data->rollout : null,
'rollout_info' => $data->rollout_info,
'preorderTypes' => $ptypes
];
}
} else {
$tmp_addresses = [];
while($data = $this->db()->fetch_object($res)) {
$address_key = $data->hausnummer_id."-".$data->zusatz;
if(!array_key_exists($address_key, $tmp_addresses)) {
// get allowed preorderTypes
$ptypes = [];
if($data->freigabe) {
$freigaben = json_decode($data->freigabe);
if(is_array($freigaben) && count($freigaben)) {
foreach($freigaben as $freigabe) {
if(in_array($freigabe, $this->allowed_preordertypes)) {
$ptypes[] = $freigabe;
}
}
}
}
$housenumber = $data->hausnummer;
if($this->hausnummer_add_zusatz) {
if($data->zusatz) {
$housenumber .= " (".$data->zusatz.")";
}
}
$tmp_addresses[$address_key] = [
'oaid' => $data->hausnummer_oaid,
'zip' => $data->plz,
'city' => ($this->district_is_city) ? $data->ortschaft : $data->gemeinde,
'district' => $data->ortschaft,
'street' => $data->strasse,
'housenumber' => $housenumber,
'lot_number' => $data->grund_nr,
'building_unit_count' => 0,
'gps_lat' => (float)$data->gps_lat,
'gps_long' => (float)$data->gps_long,
'rollout_year' => ($data->rollout) ? (int)$data->rollout : null,
'rollout_info' => $data->rollout_info,
'preorderTypes' => $ptypes,
'units' => []
];
}
$tmp_addresses[$address_key]['units'][] = [
'oaid' => $data->wohneinheit_oaid,
'num' => (int)$data->num,
'block' => $data->block,
'stiege' => $data->stiege,
'stock' => $data->stock,
'tuer' => $data->tuer,
'zusatz' => $data->zusatz,
];
$tmp_addresses[$address_key]['building_unit_count']++;
}
foreach($tmp_addresses as $ta) {
$addresses[] = $ta;
}
$addresses[] = [
'zip' => $data->plz,
'city' => ($this->district_is_city) ? $data->ortschaft : $data->gemeinde,
'district' => $data->ortschaft,
'street' => $data->strasse,
'housenumber' => $housenumber,
'block' => $data->block,
'stock' => $data->stock,
'stiege' => $data->stiege,
'tuer' => $data->tuer,
'zusatz' => $data->zusatz,
'gps_lat' => $data->gps_lat,
'gps_long' => $data->gps_long,
'rollout_year' => ($data->rollout) ? (int)$data->rollout : null,
'rollout_info' => $data->rollout_info,
'preorderTypes' => $ptypes
];
}
}

View File

@@ -13,6 +13,7 @@ class PreorderApicontroller extends mfBaseApicontroller {
private $district_is_city = false;
private $hausnummer_add_zusatz = false;
private $exist_is_error = false;
private $require_connectiontype = false;
protected function init() {
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
@@ -55,6 +56,9 @@ class PreorderApicontroller extends mfBaseApicontroller {
if($campaign->exist_is_error == 1) {
$this->exist_is_error = true;
}
if($campaign->require_connectiontype == 1) {
$this->require_connectiontype = true;
}
}
@@ -107,7 +111,7 @@ class PreorderApicontroller extends mfBaseApicontroller {
return mfResponse::BadRequest(["message" => "Invalid preorderType"]);
}
$connection_type = false;
$connection_type = null;
switch($this->post['connectionType']) {
case "single-dwelling":
$connection_type = "single-dwelling";
@@ -125,7 +129,9 @@ class PreorderApicontroller extends mfBaseApicontroller {
$connection_type = "business";
break;
default:
return mfResponse::BadRequest(["message" => "Invalid connectionType"]);
if($this->require_connectiontype) {
return mfResponse::BadRequest(["message" => "Invalid connectionType"]);
}
}
@@ -148,6 +154,11 @@ class PreorderApicontroller extends mfBaseApicontroller {
return mfResponse::BadRequest(['message' => "Mandatory address fields missing"]);
}
$shipping_address = "customer";
if(array_key_exists("is_shipping", $this->post['address']) && $this->post['address']['is_shipping']) {
$shipping_address = "address";
}
$address_search = [];
$address_search_fields = [
'street' => 'strasse',

View File

@@ -18,7 +18,7 @@ class DashboardController extends mfBaseController {
protected function testAction() {
var_dump(ADBHausnummerModel::search(["hausnummer" => 1]));
var_dump(ADBWohneinheitModel::count(["hausnummer_id" => 1772369]));
exit;
}

View File

@@ -23,6 +23,11 @@ paths:
description: Sucht nach Adressen. Retourniert Adressen mit Wohneinheiten
operationId: findAddresses
parameters:
- name: format
description: "Ausgabeformat: `flat` (Ein Array-Element pro Wohneinheit mit Gebäudedaten; **default**) oder `tree` (Ein Array-Element pro Gebäude mit Wohneinheiten-Array; **empfohlen**)"
in: query
schema:
type: string
- name: street
description: Straße Suchbegriff
in: query
@@ -63,7 +68,20 @@ paths:
| order | Vollanschluss |
| reorder | Nachbestellung (nach Bauabschluss) |
content:
application/json:
"application/json (format: tree)":
schema:
type: object
properties:
status:
type: string
description: Status string
example: OK
result:
type: object
properties:
addresses:
$ref: '#/components/schemas/AddressesTree'
"application/json (format: flat)":
schema:
type: object
properties:
@@ -341,6 +359,19 @@ paths:
code:
type: string
description: Code für Statusabfrage
example: A1B2C3D4
additionalData:
type: object
description: Benutzerdefiniertes Objekt. Wird unverändert gespeichert und in `GET /preorder` wieder ausgegeben
example:
myData: Hallo
moreData: Welt
lotsMoreData:
- key: name
value: test
- key: another Key
value: more value
'400':
description: |
Bad Request
@@ -426,13 +457,21 @@ components:
type: array
items:
type: string
AddressesTree:
type: array
items:
$ref: '#/components/schemas/AddressTree'
Addresses:
type: array
items:
$ref: '#/components/schemas/Address'
Address:
AddressTree:
type: object
properties:
oaid:
type: string
description: OAID des Gebäudes
example: AT-9999-abcdef01
street:
type: string
description: Strasse
@@ -453,6 +492,122 @@ components:
type: string
description: Ortsteil
example: Ortsteil
lot_number:
type: string
description: Grundstücksnummer
example: 123/7
building_unit_count:
type: int64
description: Anzahl Wohneinheiten im Gebäude
example: 6
gps_lat:
typee: float
description: Breitengrad der GPS-Koordinate (Dezimal)
example: 48.517560
gps_long:
typee: float
description: Längengrad der GPS-Koordinate (Dezimal)
example: 13.950665
rollout_year:
type: integer
description: Jahr des geplanten Ausbaus
example: 2024
rollout_info:
type: string
description: Infotext begzl. Bauplanung
example: Bauplan 2024
preorderTypes:
type: array
example: ["interest", "provision", "order", "reorder"]
description: |
Erlaubte Vorbestelltypen:
| preorderType | description |
|--------------|-------------|
| interest | Interessensbekundung |
| provision | Vorsorgeanschluss |
| order | Vollanschluss |
| reorder | Nachbestellung (nach Bauabschluss) |
items:
type: string
units:
type: array
items:
type: object
properties:
oaid:
type: string
description: OAID der Wohneinheit
example: AT-9999-abcdef01.001
num:
type: int64
description: Fortlaufende Nummer der Wohneinheit
example: 2
block:
type: string
description: Block
example: ""
stock:
type: string
description: Stock
example: 42
stiege:
type: string
description: Stiege
example: ""
tuer:
type: string
description: Tür
example: 1337
zusatz:
type: string
description: Addresszusatz
example: im Carport
required:
- street
Address:
type: object
properties:
oaid:
type: string
description: OAID der Wohneinheit
example: AT-9999-abcdef01.001
building_oaid:
type: string
description: OAID des Gebäudes
example: AT-9999-abcdef01
street:
type: string
description: Strasse
example: Beispielstraße
housenumber:
type: string
description: Hausnummer
example: 13
zip:
type: string
description: PLZ
example: 9999
city:
type: string
description: Ort
example: Beispielhausen
district:
type: string
description: Ortsteil
example: Ortsteil
lot_number:
type: string
description: Grundstücksnummer
example: 123/7
building_unit_count:
type: int64
description: Anzahl Wohneinheiten im Gebäude
example: 6
num:
type: int64
description: Fortlaufende Nummer der Wohneinheit
example: 2
block:
type: string
description: Block
@@ -469,6 +624,10 @@ components:
type: string
description: Tür
example: 1337
zusatz:
type: string
description: Addresszusatz
example: im Carport
gps_lat:
typee: float
description: Breitengrad der GPS-Koordinate (Dezimal)
@@ -585,9 +744,9 @@ components:
| preorderType | Description |
|--------------|-------------|
| interest | Interessensbekundung|
| provision | Vorsorgeanschluss|
| order | Vollanschluss|
| interest | Interessensbekundung |
| provision | Vorsorgeanschluss |
| order | Vollanschluss |
| reorder | Nachbestellung (nach Bauabschluss) |
acceptMarketing:
type: boolean
@@ -678,8 +837,12 @@ components:
example: Nachname
street:
type: string
description: Straße und Hausnummer Kunde
example: "Beispielstraße 42"
description: Straße Kunde
example: Beispielstraße
housenumber:
type: string
description: Hausnummer Kunde
example: 42
zip:
type: string
description: PLZ Kunde
@@ -688,6 +851,22 @@ components:
type: string
description: Ort Kunde
example: Beispielhausen
block:
type: string
description: Adresszusatz
example: null
stiege:
type: string
description: Adresszusatz
example: null
stock:
type: string
description: Adresszusatz
example: null
tuer:
type: string
description: Adresszusatz
example: null
phone:
type: string
description: Telefonnummer Kunde

View File

@@ -19,7 +19,7 @@ $freigabe_default = json_encode(["order", "reorder"]);
$me = new User(1);
$folder = __DIR__."/import/";
$csvname = "Adressendatensatz_Draft01_20221118_UTF8.csv";
$csvname = "Adressendatensatz_BP2023_20221201.csv";
$filename = $folder.$csvname;
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
@@ -29,20 +29,20 @@ $input = fopen($filename, "r");
$h_extrefs = [];
$i = 0;
$l = 0;
$c = 0;
$u = 0;
$w = 0;
while($csv = fgetcsv($input, 0, ";")) {
$i++;
if($i == 1) continue;
$l++;
if($l == 1) continue;
$hausnummer = false;
if(!trim($csv[0])) {
continue;
}
$adrcd = trim($csv[0]);
$gem_kz = trim($csv[2]);
$gem_name = trim($csv[3]);
@@ -62,18 +62,28 @@ while($csv = fgetcsv($input, 0, ";")) {
//$long = str_replace(",",".",trim($csv[8]));
$rollout_time = trim($csv[12]);
$grundstueck_name = trim($csv[14]);
if(!$grundstueck_name) $grundstueck_name = null;
$unit_count = trim($csv[15]);
if(!$unit_count) $unit_count = 1;
$netzgebiet_extref = trim($csv[11]);
if(!$netzgebiet_extref) continue;
// get code and name
$netzgebiet_name_parts = explode(", ",$netzgebiet_extref);
if(count($netzgebiet_name_parts) < 2) {
die("Netzgebiet Code und Subname können nicht extrahiert werden.\n");
}
if($netzgebiet_extref == "Subcluster-Ardning") {
$netzgebiet_code = "Subcluster-Ardning";
$netzgebiet_subname = "Subcluster-Ardning";
} else {
// get code and name
$netzgebiet_name_parts = explode(", ",$netzgebiet_extref);
if(count($netzgebiet_name_parts) < 2) {
die("Netzgebiet Code und Subname können nicht extrahiert werden.\n");
}
$netzgebiet_code = $netzgebiet_name_parts[0];
$netzgebiet_subname = $netzgebiet_name_parts[1];
$netzgebiet_code = $netzgebiet_name_parts[0];
$netzgebiet_subname = $netzgebiet_name_parts[1];
}
// find netzgebiet
$netzgebiet = new ADBNetzgebiet();
@@ -92,6 +102,7 @@ while($csv = fgetcsv($input, 0, ";")) {
die("Error creating Netzgebiet!\n");
}
}
//var_dump($netzgebiet);exit;
$nutzung = trim($csv[10]);
@@ -124,7 +135,7 @@ while($csv = fgetcsv($input, 0, ";")) {
// check for GemeindeNetzgebiet
checkGemeindeNetzgebiet($gemeinde, $netzgebiet);
continue;
//continue;
$strasse = ADBStrasseModel::getFirst(['kennziffer' => $skz]);
if(!$strasse) {
die("Strasse ($skz, $strasse_name) nicht in addressdb gefunden\n");
@@ -161,18 +172,17 @@ while($csv = fgetcsv($input, 0, ";")) {
$freigabe = $freigabe_default;
} elseif($rollout_time == "2024/2025") {
$rollout = null;
$rollout_info = "Bauprogramm 2024/2025";
$rollout_info = "2024/2025";
$freigabe = json_encode(['reorder']);
} else {
$rollout = null;
$rollout_info = $rollout_time;
$rollout_info = "unscheduled";
$freigabe = json_encode([]);
}
//$hausnummer = ADBHausnummerModel::getFirst(['gemeind_id' => $gemeinde->id, 'strasse_id' => $strasse->id, 'hausnummer' => $hausnummer_string]);
$hausnummer = ADBHausnummerModel::getFirst(['adrcd' => $adrcd]);
if(!$hausnummer) {
$hausnummer_data = [
'adrcd' => $adrcd,
'netzgebiet_id' => $netzgebiet->id,
@@ -181,32 +191,93 @@ while($csv = fgetcsv($input, 0, ";")) {
'plz_id' => $plz->id,
'strasse_id' => $strasse->id,
'hausnummer' => $hausnummer_string,
'grund_nr' => $grundstueck_name,
'rollout' => $rollout,
'rollout_info' => $rollout_info,
'freigabe' => $freigabe
];
$hausnummer = ADBHausnummerModel::create($hausnummer_data);
var_dump($hausnummer);exit;
if(!$hausnummer->save()) {
var_dump($hausnummer_data);
die("Konnte Hausnummer nicht anlegen\n");
}
if(!$hausnummer->oaid) {
$hausnummer->oaid = $hausnummer->getNewOAID();
if(!$hausnummer->oaid) {
die("Error generating OAID for hausnummer ".$hausnummer->id);
}
$hausnummer->save();
}
$c++;
} else {
$hausnummer->netzgebiet_id = $netzgebiet->id;
$hausnummer->extref = $adrcd;
$hausnummer->grund_nr = $grundstueck_name;
$hausnummer->rollout = $rollout;
$hausnummer->rollout_info = $rollout_info;
$hausnummer->freigabe = $freigabe;
if(!$hausnummer->oaid) {
$hausnummer->oaid = $hausnummer->getNewOAID();
if(!$hausnummer->oaid) {
die("Error generating OAID for hausnummer ".$hausnummer->id);
}
}
if(!$hausnummer->save()) {
var_dump($hausnummer);
die("Konnte Hausnummer nicht speichern\n");
}
$u++;
}
// get count of ccurnt wohneinheiten
$existing_units_count = ADBWohneinheitModel::count(['hausnummer_id' => $hausnummer->id]);
$new_units_count = $unit_count - $existing_units_count;
$last_unit_num = 0;
//if($new_units_count) {
foreach(ADBWohneinheitModel::search(['hausnummer_id' => $hausnummer->id]) as $tmp_unit) {
if(!$tmp_unit->oaid) {
// generate missing oaid
$tmp_unit->oaid = $tmp_unit->getNewOAID();
if(!$tmp_unit->oaid) {
die("Error generating OAID for wohneinheit ".$tmp_unit->id);
}
$tmp_unit->save();
}
if($tmp_unit->num > $last_unit_num) {
$last_unit_num = $tmp_unit->num;
}
}
//}
// create wohneinheiten
for($i = 1; $i <= $new_units_count; $i++) {
$num = $last_unit_num + $i;
//echo "$existing_units_count create wohneinheit $num\n";
$unit_data = [
'hausnummer_id' => $hausnummer->id,
'num' => $num,
];
$wohneinheit = ADBWohneinheitModel::create($unit_data);
$wohneinheit_id = $wohneinheit->save();
if(!$wohneinheit_id) {
die("Cannot save Wohneinheit\n");
}
$wohneinheit->oaid = $wohneinheit->getNewOAID();
if(!$wohneinheit->oaid) {
die("Error generating OAID for wohneinheit ".$wohneinheit->id);
}
$wohneinheit->save();
$w++;
}
}
echo "$i lines processed, $c created, $u updated\n";
echo "$l lines processed, $c created, $u updated\n";
echo "$w Wohneinheiten erstellt\n";
function checkGemeindeNetzgebiet($gemeinde, $netzgebiet) {

File diff suppressed because it is too large Load Diff