From 9b3335c820d24c2d0122035b1527f01f4ed1eec6 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 4 Oct 2022 21:42:00 +0200 Subject: [PATCH] Added salescluster to addressdb and preorder --- Layout/default/Preordercampaign/Form.php | 10 +- application/ADBGemeinde/ADBGemeindeModel.php | 11 ++ .../ADBHausnummer/ADBHausnummerModel.php | 12 +- .../ADBNetzgebiet/ADBNetzgebietModel.php | 137 +++++++++++++++++ application/ADBPlz/ADBPlzModel.php | 15 +- application/ADBStrasse/ADBStrasseModel.php | 11 ++ .../ADBWohneinheit/ADBWohneinheitModel.php | 8 +- application/Api/v1/AddressdbApicontroller.php | 85 +++++++++-- .../Preordercampaign/Preordercampaign.php | 10 +- .../PreordercampaignController.php | 18 ++- .../PreordercampaignGemeinde.php | 5 - .../PreordercampaignSalescluster.php | 5 + .../PreordercampaignSalesclusterModel.php} | 26 ++-- lib/mvcfronk/mfBase/mfBaseApicontroller.php | 1 + public/docs/preorder-api.yaml | 48 +++++- scripts/addressdb_home_update.php | 96 ++++++++++++ scripts/addressdb_update.php | 141 ++++++++++++++---- 17 files changed, 552 insertions(+), 87 deletions(-) create mode 100644 application/ADBNetzgebiet/ADBNetzgebietModel.php delete mode 100644 application/PreordercampaignGemeinde/PreordercampaignGemeinde.php create mode 100644 application/PreordercampaignSalescluster/PreordercampaignSalescluster.php rename application/{PreordercampaignGemeinde/PreordercampaignGemeindeModel.php => PreordercampaignSalescluster/PreordercampaignSalesclusterModel.php} (69%) create mode 100755 scripts/addressdb_home_update.php diff --git a/Layout/default/Preordercampaign/Form.php b/Layout/default/Preordercampaign/Form.php index 4a06e7d07..01256193b 100644 --- a/Layout/default/Preordercampaign/Form.php +++ b/Layout/default/Preordercampaign/Form.php @@ -116,11 +116,11 @@
- +
- + +
@@ -204,7 +204,7 @@ closeOnSelect: false }); - $("#adb_gemeinde_ids").select2({ + $("#adb_netzgebiet_ids").select2({ allowClear: true, placeholder: "", closeOnSelect: false diff --git a/application/ADBGemeinde/ADBGemeindeModel.php b/application/ADBGemeinde/ADBGemeindeModel.php index 72c0528b7..3013c3ef8 100644 --- a/application/ADBGemeinde/ADBGemeindeModel.php +++ b/application/ADBGemeinde/ADBGemeindeModel.php @@ -92,6 +92,8 @@ class ADBGemeindeModel { $where = self::getSqlFilter($filter); $sql = "SELECT Gemeinde.* FROM Gemeinde LEFT JOIN Plz ON (Plz.gemeinde_id = Gemeinde.id) + LEFT JOIN GemeindeNetzgebiet ON (GemeindeNetzgebiet.gemeinde_id = Gemeinde.id) + LEFT JOIN Netzgebiet ON (GemeindeNetzgebiet.netzgebiet_id = Netzgebiet.id) WHERE $where GROUP BY Gemeinde.id ORDER BY name,code,kennziffer"; @@ -118,6 +120,15 @@ class ADBGemeindeModel { $where = "1=1 "; + if(array_key_exists("netzgebiet_id", $filter)) { + $netzgebiet_id = $filter['netzgebiet_id']; + if(is_numeric($netzgebiet_id)) { + $where .= " AND Netzgebiet.id=$netzgebiet_id"; + } elseif(is_array($netzgebiet_id) && count($netzgebiet_id)) { + $where .= " AND Netzgebiet.id IN (". implode(",", $netzgebiet_id).")"; + } + } + if(array_key_exists("code", $filter)) { $code = $filter['code']; if(is_numeric($code)) { diff --git a/application/ADBHausnummer/ADBHausnummerModel.php b/application/ADBHausnummer/ADBHausnummerModel.php index 89c5038b1..00783b2d3 100644 --- a/application/ADBHausnummer/ADBHausnummerModel.php +++ b/application/ADBHausnummer/ADBHausnummerModel.php @@ -46,6 +46,7 @@ class ADBHausnummerModel { $db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); $where = self::getSqlFilter($filter); + mfLoghandler::singleton()->debug($where); $res = $db->select("Hausnummer", "*", "$where ORDER BY strasse_id,hausnummer LIMIT 1"); if($db->num_rows($res)) { $data = $db->fetch_object($res); @@ -100,7 +101,7 @@ class ADBHausnummerModel { WHERE $where ORDER BY strasse_id,hausnummer"; - //mfLoghandler::singleton()->debug($sql); + mfLoghandler::singleton()->debug($sql); if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; @@ -121,6 +122,13 @@ class ADBHausnummerModel { private static function getSqlFilter($filter) { $where = "1=1 "; + if(array_key_exists("extref", $filter)) { + $extref = FronkDB::singleton()->escape($filter['extref']); + if($extref) { + $where .= " AND Hausnummer.`extref` = '$extref'"; + } + } + if(array_key_exists("plz_id", $filter)) { $plz_id = $filter['plz_id']; if(is_numeric($plz_id)) { @@ -139,6 +147,8 @@ class ADBHausnummerModel { } } + + if(array_key_exists("hausnummer", $filter)) { $hausnummer = FronkDB::singleton()->escape($filter['hausnummer']); if($hausnummer) { diff --git a/application/ADBNetzgebiet/ADBNetzgebietModel.php b/application/ADBNetzgebiet/ADBNetzgebietModel.php new file mode 100644 index 000000000..a9ac527f7 --- /dev/null +++ b/application/ADBNetzgebiet/ADBNetzgebietModel.php @@ -0,0 +1,137 @@ + $value) { + if(property_exists(get_called_class(), $field)) { + $model ->$field = $value; + } + } + + $me = mfValuecache::singleton()->get("me"); + if(!$me) { + $me = new User(); + $me->loadMe(); + mfValuecache::singleton()->set("me", $me); + } + /* + if($model->create_by === null) { + $model->create_by = $me->id; + } + if($model->edit_by === null) { + $model->edit_by = $me->id; + }*/ + + return $model; + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); + + $where = self::getSqlFilter($filter); + $res = $db->select("Netzgebiet", "*", "$where ORDER BY name LIMIT 1"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new ADBNetzgebiet($data); + if($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + + public static function getAll() { + $items = []; + + $db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); + + $res = $db->select("Netzgebiet", "*", "1=1 ORDER BY name"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new ADBNetzgebiet($data); + } + } + return $items; + + } + + + public static function count($filter) { + $db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); + + $where = self::getSqlFilter($filter); + $sql = "SELECT COUNT(*) as cnt FROM Netzgebiet + WHERE $where + "; + + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + return $data->cnt; + } + return 0; + } + + public static function search($filter, $limit = false) { + $items = []; + $db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); + + $where = self::getSqlFilter($filter); + $sql = "SELECT Netzgebiet.* FROM Netzgebiet + LEFT JOIN Plz ON (Plz.netzgebiet_id = Netzgebiet.id) + WHERE $where + GROUP BY Netzgebiet.id + ORDER BY name"; + + mfLoghandler::singleton()->debug($sql); + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($count)) { + $sql .= " LIMIT ".$limit['count']; + } + } + + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new ADBNetzgebiet($data); + } + } + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + if(array_key_exists("name", $filter)) { + $name = FronkDB::singleton()->escape($filter['name']); + if($name) { + $where .= " AND Netzgebiet.`name` = '$name'"; + } + } + + if(array_key_exists("extref", $filter)) { + $extref = FronkDB::singleton()->escape($filter['extref']); + if($extref) { + $where .= " AND Netzgebiet.`extref` = '$extref'"; + } + } + + //var_dump($filter, $where);exit; + return $where; + } + +} diff --git a/application/ADBPlz/ADBPlzModel.php b/application/ADBPlz/ADBPlzModel.php index b773e740a..db558f741 100644 --- a/application/ADBPlz/ADBPlzModel.php +++ b/application/ADBPlz/ADBPlzModel.php @@ -91,10 +91,14 @@ class ADBPlzModel { $where = self::getSqlFilter($filter); $sql = "SELECT Plz.* FROM Plz + LEFT JOIN Gemeinde ON (Gemeinde.id = Plz.gemeinde_id) + LEFT JOIN GemeindeNetzgebiet ON (GemeindeNetzgebiet.gemeinde_id = Plz.gemeinde_id) + LEFT JOIN Netzgebiet ON (GemeindeNetzgebiet.netzgebiet_id = Netzgebiet.id) WHERE $where + GROUP BY Plz.id ORDER BY plz"; - //mfLoghandler::singleton()->debug($sql); + mfLoghandler::singleton()->debug($sql); if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; @@ -116,6 +120,15 @@ class ADBPlzModel { $where = "1=1 "; + if(array_key_exists("netzgebiet_id", $filter)) { + $netzgebiet_id = $filter['netzgebiet_id']; + if(is_numeric($netzgebiet_id)) { + $where .= " AND Netzgebiet.id=$netzgebiet_id"; + } elseif(is_array($netzgebiet_id) && count($netzgebiet_id)) { + $where .= " AND Netzgebiet.id IN (". implode(",", $netzgebiet_id).")"; + } + } + if(array_key_exists("hausnummer_id", $filter)) { $hausnummer_id = $filter['hausnummer_id']; if(is_numeric($hausnummer_id)) { diff --git a/application/ADBStrasse/ADBStrasseModel.php b/application/ADBStrasse/ADBStrasseModel.php index 2e9e81627..1419ad321 100644 --- a/application/ADBStrasse/ADBStrasseModel.php +++ b/application/ADBStrasse/ADBStrasseModel.php @@ -96,6 +96,8 @@ class ADBStrasseModel { LEFT JOIN Hausnummer ON (Hausnummer.strasse_id = Strasse.id) LEFT JOIN Plz ON (Plz.id = Hausnummer.plz_id) LEFT JOIN Gemeinde ON (Gemeinde.id = Strasse.gemeinde_id) + LEFT JOIN GemeindeNetzgebiet ON (GemeindeNetzgebiet.gemeinde_id = Strasse.gemeinde_id) + LEFT JOIN Netzgebiet ON (GemeindeNetzgebiet.netzgebiet_id = Netzgebiet.id) WHERE $where GROUP BY Strasse.id ORDER BY gemeinde_id,name,kennziffer"; @@ -121,6 +123,15 @@ class ADBStrasseModel { private static function getSqlFilter($filter) { $where = "1=1 "; + + if(array_key_exists("netzgebiet_id", $filter)) { + $netzgebiet_id = $filter['netzgebiet_id']; + if(is_numeric($netzgebiet_id)) { + $where .= " AND Netzgebiet.id=$netzgebiet_id"; + } elseif(is_array($netzgebiet_id) && count($netzgebiet_id)) { + $where .= " AND Netzgebiet.id IN (". implode(",", $netzgebiet_id).")"; + } + } if(array_key_exists("gemeinde_id", $filter)) { $gemeinde_id = $filter['gemeinde_id']; diff --git a/application/ADBWohneinheit/ADBWohneinheitModel.php b/application/ADBWohneinheit/ADBWohneinheitModel.php index 3cdc74ab1..36d47e10c 100644 --- a/application/ADBWohneinheit/ADBWohneinheitModel.php +++ b/application/ADBWohneinheit/ADBWohneinheitModel.php @@ -100,7 +100,7 @@ class ADBWohneinheitModel { WHERE $where ORDER BY hausnummer_id,block,stiege,LENGTH(stock),stock,LENGTH(tuer),tuer"; - //mfLoghandler::singleton()->debug($sql); + mfLoghandler::singleton()->debug($sql); if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; @@ -121,6 +121,12 @@ class ADBWohneinheitModel { private static function getSqlFilter($filter) { $where = "1=1 "; + if(array_key_exists("extref", $filter)) { + $extref = FronkDB::singleton()->escape($filter['extref']); + if($extref) { + $where .= " AND Wohneinheit.`extref` = '$extref'"; + } + } if(array_key_exists("hausnummer_id", $filter)) { $hausnummer_id = $filter['hausnummer_id']; diff --git a/application/Api/v1/AddressdbApicontroller.php b/application/Api/v1/AddressdbApicontroller.php index 2302e87ec..ffbcd71ee 100644 --- a/application/Api/v1/AddressdbApicontroller.php +++ b/application/Api/v1/AddressdbApicontroller.php @@ -1,7 +1,11 @@ db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); @@ -19,19 +23,42 @@ class AddressdbApicontroller extends mfBaseApicontroller { } protected function authenticated() { - $campaignApiuser = PreordercampaignApiuserModel::getFirst(["worker_id" => $this->me->id]); - $campaign = new Preordercampaign($campaignApiuser->preordercampaign_id); - if($campaign) { - foreach(PreordercampaignGemeindeModel::search(['preordercampaign_id' => $campaign->id]) as $gemeinde) { - $this->filter_gemeinde_ids[] = $gemeinde->id; + $campaignApiusers = PreordercampaignApiuserModel::search(["worker_id" => $this->me->id]); + + foreach($campaignApiusers as $campaignApiuser) { + $campaign = new Preordercampaign($campaignApiuser->preordercampaign_id); + if($campaign) { + foreach(PreordercampaignSalesclusterModel::search(['preordercampaign_id' => $campaign->id]) as $campain_scluster) { + if(!in_array($campain_scluster->salescluster_id, $this->filter_salescluster_ids)) { + $this->filter_salescluster_ids[] = $campain_scluster->salescluster_id; + } + + if(!array_key_exists($campain_scluster->salescluster_id, $this->campaigns_by_scluster)) { + $this->campaigns_by_scluster[$campain_scluster->salescluster_id] = []; + } + $this->campaigns_by_scluster[$campain_scluster->salescluster_id][] = $campaign->id; + + } + $this->campaigns[$campaign->id] = $campaign; + + // get allowed preordertypes + if(is_array($campaign->types) && count($campaign->types)) { + foreach($campaign->types as $type) { + $this->allowed_preordertypes[] = $type->type; + } + } + } + + foreach(PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) { + $this->addAllowedOrigin($origin->hostname); } } - foreach(PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) { - $this->addAllowedOrigin($origin->hostname); - } + $this->allowed_preordertypes = array_unique($this->allowed_preordertypes); + //var_dump($this->allowed_preordertypes);exit; + //var_dump($this->filter_salescluster_ids, $this->campaigns_by_scluster);exit; + //var_dump($this->campaigns, $this->allowed_origins);exit; - //var_dump($campaign, $this->allowed_origins);exit; } protected function findCity() { @@ -56,6 +83,10 @@ class AddressdbApicontroller extends mfBaseApicontroller { $city_search['plz%'] = $zip; } + if(count($this->filter_salescluster_ids)) { + $city_search['netzgebiet_id'] = $this->filter_salescluster_ids; + } + $results = ADBGemeindeModel::search($city_search); @@ -84,7 +115,13 @@ class AddressdbApicontroller extends mfBaseApicontroller { $zip = $search; } - $results = ADBPlzModel::search(['plzstring%' => $zip]); + $zip_search = ['plzstring%' => $zip]; + + if(count($this->filter_salescluster_ids)) { + $zip_search['netzgebiet_id'] = $this->filter_salescluster_ids; + } + + $results = ADBPlzModel::search($zip_search); $zips = []; @@ -122,6 +159,10 @@ class AddressdbApicontroller extends mfBaseApicontroller { $street_search['plz%'] = $zip; } + if(count($this->filter_salescluster_ids)) { + $street_search['netzgebiet_id'] = $this->filter_salescluster_ids; + } + $results = ADBStrasseModel::search($street_search); @@ -131,6 +172,7 @@ class AddressdbApicontroller extends mfBaseApicontroller { } $streets = array_unique($streets); + sort($streets); return mfResponse::Ok(['streets' => array_values($streets)]); } @@ -170,6 +212,22 @@ class AddressdbApicontroller extends mfBaseApicontroller { if($this->db()->num_rows($res)) { while($data = $this->db()->fetch_object($res)) { + $ptypes = []; + switch($data->freigabe) { + case "voll": + if(in_array("order", $this->allowed_preordertypes)) { + $ptypes[] = "order"; + } + case "vorsorge": + if(in_array("provision", $this->allowed_preordertypes)) { + $ptypes[] = "provision"; + } + case "interesse": + if(in_array("interest", $this->allowed_preordertypes)) { + $ptypes[] = "interest"; + } + } + $addresses[] = [ 'zip' => $data->plz, 'city' => $data->gemeinde, @@ -179,7 +237,10 @@ class AddressdbApicontroller extends mfBaseApicontroller { 'stock' => $data->stock, 'stiege' => $data->stiege, 'tuer' => $data->tuer, - 'zusatz' => $data->zusatz + 'zusatz' => $data->zusatz, + 'gps_lat' => $data->gps_lat, + 'gps_long' => $data->gps_long, + 'preorderTypes' => $ptypes ]; } } diff --git a/application/Preordercampaign/Preordercampaign.php b/application/Preordercampaign/Preordercampaign.php index ba29eaf6e..2c421543f 100644 --- a/application/Preordercampaign/Preordercampaign.php +++ b/application/Preordercampaign/Preordercampaign.php @@ -5,7 +5,7 @@ class Preordercampaign extends mfBaseModel { private $preorders; private $types; private $setup_products; - private $gemeinden; + private $salesclusters; private $apiusers; private $corsorigins; @@ -88,12 +88,12 @@ class Preordercampaign extends mfBaseModel { return $this->setup_products; } - if($name == "gemeinden") { - $items = PreordercampaignGemeindeModel::search(["preordercampaign_id" => $this->id]); + if($name == "salesclusters") { + $items = PreordercampaignSalesclusterModel::search(["preordercampaign_id" => $this->id]); foreach($items as $pog) { - $this->gemeinden[$pog->gemeinde_id] = $pog; + $this->salesclusters[$pog->salescluster_id] = $pog; } - return $this->gemeinden; + return $this->salesclusters; } if($name == "apiusers") { diff --git a/application/Preordercampaign/PreordercampaignController.php b/application/Preordercampaign/PreordercampaignController.php index 55697fe93..eaf554dad 100644 --- a/application/Preordercampaign/PreordercampaignController.php +++ b/application/Preordercampaign/PreordercampaignController.php @@ -92,7 +92,9 @@ class PreordercampaignController extends mfBaseController { $this->layout()->set("networksections", NetworksectionModel::getAll()); - $this->layout()->set("adb_gemeinden", ADBGemeindeModel::getAll()); + $this->layout()->set("adb_netzgebiete", ADBNetzgebietModel::getAll()); + + } protected function editAction() { @@ -110,7 +112,6 @@ class PreordercampaignController extends mfBaseController { $this->layout()->set("campaign", $campaign); - return $this->addAction(); } @@ -180,16 +181,17 @@ class PreordercampaignController extends mfBaseController { $campaign->addTypes($r->types); } - //var_dump($r->adb_gemeinde_ids);exit; - foreach(PreordercampaignGemeindeModel::search(['preordercampaign_id' => $campaign->id]) as $pcg) { + //var_dump($r->adb_netzgebiet_ids);exit; + foreach(PreordercampaignSalesclusterModel::search(['preordercampaign_id' => $campaign->id]) as $pcg) { $pcg->delete(); } - if(is_array($r->adb_gemeinde_ids) && count($r->adb_gemeinde_ids)) { - foreach($r->adb_gemeinde_ids as $gemeinde_id) { - $pcg = PreordercampaignGemeindeModel::getFirst(['preordercampaign_id' => $campaign->id, 'gemeinde_id' => $gemeinde_id]); + //var_dump($r->adb_netzgebiet_ids);exit; + if(is_array($r->adb_netzgebiet_ids) && count($r->adb_netzgebiet_ids)) { + foreach($r->adb_netzgebiet_ids as $netzgebiet_id) { + $pcg = PreordercampaignSalesclusterModel::getFirst(['preordercampaign_id' => $campaign->id, 'salescluster_id' => $netzgebiet_id]); if($pcg) continue; - $pcg = PreordercampaignGemeindeModel::create(['preordercampaign_id' => $campaign->id, 'gemeinde_id' => $gemeinde_id]); + $pcg = PreordercampaignSalesclusterModel::create(['preordercampaign_id' => $campaign->id, 'salescluster_id' => $netzgebiet_id]); $pcg->save(); } } diff --git a/application/PreordercampaignGemeinde/PreordercampaignGemeinde.php b/application/PreordercampaignGemeinde/PreordercampaignGemeinde.php deleted file mode 100644 index f9b9c30a2..000000000 --- a/application/PreordercampaignGemeinde/PreordercampaignGemeinde.php +++ /dev/null @@ -1,5 +0,0 @@ - $value) { if(property_exists(get_called_class(), $field)) { @@ -40,10 +40,10 @@ class PreordercampaignGemeindeModel { $db = FronkDB::singleton(); - $res = $db->select("PreordercampaignGemeinde", "*", "ORDER BY preordercampaign_id, gemeinde_id"); + $res = $db->select("PreordercampaignSalescluster", "*", "ORDER BY preordercampaign_id, salescluster_id"); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { - $items[] = new PreordercampaignGemeinde($data); + $items[] = new PreordercampaignSalescluster($data); } } return $items; @@ -54,10 +54,10 @@ class PreordercampaignGemeindeModel { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); - $res = $db->select("PreordercampaignGemeinde", "*", "$where ORDER BY preordercampaign_id, gemeinde_id"); + $res = $db->select("PreordercampaignSalescluster", "*", "$where ORDER BY preordercampaign_id, salescluster_id"); if($db->num_rows($res)) { $data = $db->fetch_object($res); - $item = new PreordercampaignGemeinde($data); + $item = new PreordercampaignSalescluster($data); if($item->id) { return $item; } else { @@ -72,10 +72,10 @@ class PreordercampaignGemeindeModel { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); - $res = $db->select("PreordercampaignGemeinde", "*", "$where ORDER BY preordercampaign_id, gemeinde_id"); + $res = $db->select("PreordercampaignSalescluster", "*", "$where ORDER BY preordercampaign_id, salescluster_id"); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { - $items[] = new PreordercampaignGemeinde($data); + $items[] = new PreordercampaignSalescluster($data); } } return $items; @@ -92,10 +92,10 @@ class PreordercampaignGemeindeModel { } } - if(array_key_exists("gemeinde_id", $filter)) { - $gemeinde_id = $filter['gemeinde_id']; - if(is_numeric($gemeinde_id)) { - $where .= " AND gemeinde_id=$gemeinde_id"; + if(array_key_exists("salescluster_id", $filter)) { + $salescluster_id = $filter['salescluster_id']; + if(is_numeric($salescluster_id)) { + $where .= " AND salescluster_id=$salescluster_id"; } } diff --git a/lib/mvcfronk/mfBase/mfBaseApicontroller.php b/lib/mvcfronk/mfBase/mfBaseApicontroller.php index 8f9e53f9e..33f21a373 100644 --- a/lib/mvcfronk/mfBase/mfBaseApicontroller.php +++ b/lib/mvcfronk/mfBase/mfBaseApicontroller.php @@ -459,6 +459,7 @@ class mfBaseApicontroller { protected function addAllowedOrigin($origin) { $this->allowed_origins[] = trim($origin); + $this->allowed_origins = array_unique($this->allowed_origins); return true; } diff --git a/public/docs/preorder-api.yaml b/public/docs/preorder-api.yaml index c107cb70a..70e428740 100644 --- a/public/docs/preorder-api.yaml +++ b/public/docs/preorder-api.yaml @@ -45,7 +45,16 @@ paths: type: string responses: '200': - description: Successful operation + description: | + Successful operation + + Der Rückgabewert `preorderType` gibt an, welche Vorbestelltypen an dieser Adresse erlaubt sind: + + | preorderType | description | + |--------------|-------------| + | interest | Interessensbekundung | + | provision | Vorsorgeanschluss | + | order | Vollanschluss | content: application/json: schema: @@ -340,16 +349,49 @@ components: properties: street: type: string - description: Straße + description: Strasse + example: Beispielstraße housenumber: type: string description: Hausnummer + example: 13 zip: type: string description: PLZ + example: 9999 city: type: string - description: Gemeinde + description: Ort + example: Beispielhausen + 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 + preorderTypes: + type: array + example: ["interest", "provision", "order"] + description: | + Erlaubte Vorbestelltypen: + + | preorderType | description | + |--------------|-------------| + | interest | Interessensbekundung | + | provision | Vorsorgeanschluss | + | order | Vollanschluss | + items: + type: string required: - street addressResponse: diff --git a/scripts/addressdb_home_update.php b/scripts/addressdb_home_update.php new file mode 100755 index 000000000..0f69bda47 --- /dev/null +++ b/scripts/addressdb_home_update.php @@ -0,0 +1,96 @@ +#!/usr/bin/php + $current_building_extref]); + if($hausnummer) { + $units = ADBWohneinheitModel::search(['hausnummer_id' => $hausnummer->id]); + if(count($units) < $current_home_count) { + echo "Weniger Homes vorhanden, als importiert (hausnummer extref: ".$hausnummer->extref.") building_extref $building_extref\n"; + } + } + + $current_home_count = 0; + } + $current_home_count++; + $current_building_extref = $building_extref; + + $hausnummer = ADBHausnummerModel::getFirst(['extref' => $building_extref]); + if(!$hausnummer) { + echo "Hausnummer nicht gefunden: $building_extref\n"; + continue; + } + + $existing_units = ADBWohneinheitModel::search(['hausnummer_id' => $hausnummer->id]); + + $current_unit = $existing_units[$current_home_count -1]; + if(!$current_unit) { + // transmission stations may not have been created + //echo "unit for $wohneinheit_extref not found\n"; + continue; + } + $current_unit->extref = $wohneinheit_extref; + $current_unit->save(); + $updated++; + + + + /*if($current_home_count > count($existing_units)) { + echo "creating unit $wohneinheit_extref (hausnummer $building_extref)\n"; + $unit_data = [ + 'extref' => $wohneinheit_extref, + 'hausnummer_id' => $hausnummer->id, + 'num' => $current_home_count, + 'tuer' => $current_home_count, + 'nutzung' => "Wohnung" + ]; + + $unit = ADBWohneinheitModel::create($unit_data); + //var_dump($unit);exit; + //if(!$unit->save()) { + // die("Error saving new unit\n"); + //} + }*/ + + } + +} + +echo "Updated $updated units\n"; \ No newline at end of file diff --git a/scripts/addressdb_update.php b/scripts/addressdb_update.php index 107fe65fc..32e8145e4 100755 --- a/scripts/addressdb_update.php +++ b/scripts/addressdb_update.php @@ -13,7 +13,7 @@ require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php"); $me = new User(1); -$filename = BASEDIR."/scripts/import/60670-1u2__Premstaetten_FTTxLocations.csv"; +$filename = BASEDIR."/scripts/import/premstaetten_buildings.csv"; $db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); $log = mfLoghandler::singleton(); @@ -23,31 +23,34 @@ $input = fopen($filename, "r"); $h_extrefs = []; $i = 0; -while($csv = fgetcsv($input, 0, ",")) { +while($csv = fgetcsv($input, 0, ";")) { $i++; if($i == 1) continue; + $hausnummer = false; + if(!trim($csv[0])) { continue; } else { //echo implode(", ", $csv)."\n"; - $gem_kz = trim($csv[1]); - $gem_name = trim($csv[0]); + $gem_kz = trim($csv[17]); + $gem_name = trim($csv[18]); //$ort_kz = trim($csv[2]); - $plz_name = trim($csv[3]); - $ort_name = trim($csv[4]); - $strasse_hausnummer = trim($csv[5]); - $hausnummer_extref = trim($csv[35]); + $plz_name = trim($csv[5]); + $ort_name = trim($csv[6]); + $strasse_hausnummer = trim($csv[4]); + $hausnummer_extref = trim($csv[1]); if(in_array($hausnummer_extref, $h_extrefs)) { die("Hausnummer extref doppelt!\n"); } $h_extrefs[] = $hausnummer_extref; - $lat = trim($csv[17]); - $long = trim($csv[18]); + $lat = str_replace(",",".",trim($csv[7])); + $long = str_replace(",",".",trim($csv[8])); - $netzgebiet_extref = trim($csv[26]); + $netzgebiet_extref = trim($csv[14]); + if(!$netzgebiet_extref) continue; $netzgebiet = new ADBNetzgebiet(); $netzgebiet->loadByExtref($netzgebiet_extref); if(!$netzgebiet->id) { @@ -55,12 +58,15 @@ while($csv = fgetcsv($input, 0, ",")) { } //var_dump($netzgebiet);exit; - $nutzung = trim($csv[7]); - $funktion = trim($csv[9]); + $nutzung = trim($csv[9]); + $funktion = trim($csv[20]); if($nutzung != "Greenfield" && $funktion == "Greenfield") { $nutzung = "Greenfield"; } + $unit_count = $csv[10]; + if(!$unit_count) $unit_count = 1; + if(!$strasse_hausnummer) continue; //echo "strasse: $strasse\n"; @@ -75,11 +81,15 @@ while($csv = fgetcsv($input, 0, ",")) { if(preg_match('/^(\D+)\s+(\d+[a-z0-9\/&#._-]*)(?:\s+((?:gesch(?:ae|ä)ft|betrieb und wohnungen|stg|paketlogistik|cafe|pavillon|pfarrheim|[^ ]*haus|[^ ]*geb(?:ae|ä)ude|[^ ]*halle)(?:\s+[a-z0-9]+)?))?/i', $strasse_hausnummer, $m)) { $strasse_name = trim($m[1]); $hausnummer_name = trim($m[2]); - $addresszusatz = trim($m[3]); + if(array_key_exists(3, $m)) { + $addresszusatz = trim($m[3]); + } } elseif(preg_match('/^(.+)\s+(\d+[a-z0-9\/&#._-]*)(.+)?/i', $strasse_hausnummer, $m)) { $strasse_name = trim($m[1]); $hausnummer_name = trim($m[2]); - $addresszusatz = trim($m[3]); + if(array_key_exists(3, $m)) { + $addresszusatz = trim($m[3]); + } } elseif(preg_match('/^(.+)/i', $strasse_hausnummer, $m)) { // ignore GST objects continue; @@ -110,8 +120,6 @@ while($csv = fgetcsv($input, 0, ",")) { - - if(!$gem_name || !$plz_name || !$ort_name || !$strasse_name) { die("!! Konnte Adresse nicht parsen\n"); } @@ -188,10 +196,10 @@ while($csv = fgetcsv($input, 0, ",")) { $strasse_id = false; $strsql = "SELECT * FROM view_hausnummer WHERE gemeinde_id=1 AND ortschaft IN ('". implode("', '", $ort_search)."') AND strasse IN ('". implode("', '", $strasse_search)."')"; - echo "$strsql\n";exit; + //echo "$strsql\n";exit; $strres = $db->query($strsql); if(!$db->num_rows($strres)) { - echo("Strasse in adressdb nicht gefunden: $strsql\n"); + echo("Strasse in adressdb nicht gefunden, wird erstellt: $strsql\n"); // create strasse @@ -201,17 +209,17 @@ while($csv = fgetcsv($input, 0, ",")) { 'name' => $strasse_name ]; $strasse = ADBStrasseModel::create($strasse_data); - $strasse_id = $strasse->save(); - if(!$strasse_id) die("Cannot create strasse: $strasse_name\n"); + //$strasse_id = $strasse->save(); + //if(!$strasse_id) die("Cannot create strasse: $strasse_name\n"); } else { $data = $db->fetch_object($strres); $strasse_id = $data->strasse_id; } - + /* if(!$strasse_id) { die("keine strasse_id"); - } - + }*/ + /* $plz = ADBPlzModel::getFirst(['plz' => $plz_name]); if(!$plz) die("PLZ nicht gefunden: $plz_name\n"); @@ -224,7 +232,8 @@ while($csv = fgetcsv($input, 0, ",")) { 'hausnummer' => $hausnummer_name, 'zusatz' => $addresszusatz, 'gps_lat' => $lat, - 'gps_long' => $long + 'gps_long' => $long, + 'freigabe' => 'voll' ]; //var_dump($hausnummer_data);exit; @@ -232,8 +241,9 @@ while($csv = fgetcsv($input, 0, ",")) { $hausnummer_id = $hausnummer->save(); if(!$hausnummer_id) die("Error creating hausnummer"); - + */ } else { + $data = $db->fetch_object($res); $hausnummer = new ADBHausnummer($data->hausnummer_id); @@ -245,20 +255,85 @@ while($csv = fgetcsv($input, 0, ",")) { $hausnummer->extref = $hausnummer_extref; $hausnummer->save(); } - $hausnummer->gps_lat = $lat; - $hausnummer->gps_long = $long; + $hausnummer->gps_lat = (float)$lat; + $hausnummer->gps_long = (float)$long; + $hausnummer->freigabe = "voll"; $hausnummer->save(); } + /* + if($hausnummer) { + // create missing wohneinheiten + $new_unit_count = 0; + $last_unit_tuer = 0; + + $units = ADBWohneinheitModel::search(['hausnummer_id' => $hausnummer->id]); + if(count($units)) { + foreach($units as $unit) { + if($unit->tuer > $last_unit_tuer) { + $last_unit_tuer = $unit->tuer; + } + } + } + echo $hausnummer->strasse->name." ".$hausnummer->hausnummer."(".$hausnummer->id."): ".count($units)." vorhanden vs. csv ".$unit_count."\n"; + if(count($units) < $unit_count) { + $new_unit_count = $unit_count - count($units); + } + if($new_unit_count && count($units) === 1) { + $unit = $units[0]; + if(!$unit->zusatz && !$unit->tuer) { + $unit->tuer = 1; + if(!$unit->save()) { + die("Couldn't set tuer to 1 (wohneinheit ".$unit->id.")"); + } + $last_unit_tuer = 1; + } + } + + if($new_unit_count == 1 && $nutzung != "Transmitting station") { + // create single wohneinheit + echo "Creating single Wohneiheit (".$hausnummer->strasse->name." ".$hausnummer->hausnummer." - hausnummer_id: ".$hausnummer->id.")\n"; + $unit_data = [ + 'hausnummer_id' => $hausnummer->id, + 'num' => 1, + 'nutzung' => "Wohnung" + ]; + if($last_unit_tuer) { + $unit_data['tuer'] = ++$last_unit_tuer; + } + $unit = ADBWohneinheitModel::create($unit_data); + //var_dump($unit);exit; + $unit->save(); + } + + if($new_unit_count > 1) { + echo "Creating $new_unit_count Wohneiheiten (".$hausnummer->strasse->name." ".$hausnummer->hausnummer." - hausnummer_id: ".$hausnummer->id.")\n"; + for($i = 0; $i < $new_unit_count; $i++) { + if($nutzung == "Transmitting station") continue; + + $unit_data = [ + 'hausnummer_id' => $hausnummer->id, + 'num' => 1, + 'tuer' => ++$last_unit_tuer, + 'nutzung' => "Wohnung" + ]; + $unit = ADBWohneinheitModel::create($unit_data); + + $unit->save(); + } + //if($hausnummer->id != 1723 && $nutzung != "Transmitting station") { var_dump($unit);exit; } + } + }*/ - + /* $data = $db->fetch_object($res); $csv_ort = str_replace(['ß','ä','ö','ü'], ['ss','ae','oe','ue'], $ort_name); $db_ort = str_replace(['ß','ä','ö','ü'], ['ss','ae','oe','ue'], $data->ortschaft); - + */ // update ortschaft if needed + /* if($csv_ort != $db_ort && $nutzung != "Greenfield") { if($strasse_name != "Hauptstrasse" && $hausnummer_name != "133") { @@ -266,7 +341,7 @@ while($csv = fgetcsv($input, 0, ",")) { echo "Netzgebiet extref: \033[1m-------\033[0m | Hausnummer extref: \033[1m--------\033[0m | gem: \033[1m$data->gemeinde\033[0m | plz: \033[1m$data->plz\033[0m | ort: \033[1m$db_ort\033[0m | strasse: \033[1m$data->strasse\033[0m | hausnummer: \033[1m$data->hausnummer\033[0m\n"; // find ortschaft - /*$ortschaft = ADBOrtschaftModel::getFirst(['name' => $ort_name]); + $ortschaft = ADBOrtschaftModel::getFirst(['name' => $ort_name]); if(!$ortschaft) { $ort_data = []; $ort_data['gemeinde_id'] = 1; @@ -285,9 +360,9 @@ while($csv = fgetcsv($input, 0, ",")) { die("Hausnummer nicht gefunden für Ort update"); } $hausnummer->ortschaft_id = $ortschaft->id; - $hausnummer->save();*/ + $hausnummer->save(); } - } + }*/ //echo "$data->ortschaft, $data->plz, $data->strasse, $data->hausnummer\n";