Merge branch 'fronkdev' into 'master'

New Preorders can now be ordered with OAID instead of address

See merge request fronk/thetool!36
This commit is contained in:
Frank Schubert
2023-06-21 15:23:26 +00:00
2 changed files with 150 additions and 69 deletions

View File

@@ -54,6 +54,7 @@ class ADBWohneinheitModel {
ORDER BY hausnummer_id,block,stiege,LENGTH(stock),stock,LENGTH(tuer),tuer,num
LIMIT 1";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
@@ -165,6 +166,15 @@ class ADBWohneinheitModel {
}
}
if(array_key_exists("oaid", $filter)) {
$oaid = FronkDB::singleton()->escape($filter['oaid']);
if(strlen($oaid)) {
$where .= " AND Wohneinheit.`oaid` = '$oaid'";
} else {
$where .= " AND (Wohneinheit.`oaid` IS NULL OR Wohneinheit.`oaid` = '')";
}
}
if(array_key_exists("num", $filter)) {
$num = $filter['num'];
if($num === false || $num === null) {

View File

@@ -255,7 +255,6 @@ class PreorderApicontroller extends mfBaseApicontroller {
}
}
if(!array_key_exists("address", $this->post)) {
return mfResponse::BadRequest(['message' => "address missing"]);
}
@@ -264,6 +263,24 @@ class PreorderApicontroller extends mfBaseApicontroller {
return mfResponse::BadRequest(['message' => "customer data missing"]);
}
$request_oaid = false;
$request_oaid_hausnummer = false;
$request_oaid_unit = false;
if(property_exists($this->post['address'],"oaid")) {
$request_oaid = trim($this->post['address']->oaid);
$m = [];
if($request_oaid && preg_match('/^([a-z]{2}-\d+-[0-9a-f]+)(:?\.(\d+))?/i', $request_oaid, $m)) {
if(array_key_exists(1, $m)) {
$request_oaid_hausnummer = $m[1];
}
if(array_key_exists(2, $m)) {
$request_oaid_unit = "$request_oaid_hausnummer".$m[2];
}
}
}
$is_additional_order = false;
if(array_key_exists("isAdditionalOrder", $this->post) && $this->post['isAdditionalOrder']) {
$is_additional_order = true;
@@ -272,12 +289,15 @@ class PreorderApicontroller extends mfBaseApicontroller {
/************************************************************
* check address
*/
if(!property_exists($this->post['address'],"street") || !$this->post['address']->street ||
!property_exists($this->post['address'],"housenumber") || !$this->post['address']->housenumber ||
!property_exists($this->post['address'],"zip") || !$this->post['address']->zip ||
!property_exists($this->post['address'],"city") || !$this->post['address']->city
) {
return mfResponse::BadRequest(['message' => "Mandatory address fields missing"]);
//var_dump($request_oaid_unit);exit;
if(!$request_oaid_hausnummer) {
if(!property_exists($this->post['address'],"street") || !$this->post['address']->street ||
!property_exists($this->post['address'],"housenumber") || !$this->post['address']->housenumber ||
!property_exists($this->post['address'],"zip") || !$this->post['address']->zip ||
!property_exists($this->post['address'],"city") || !$this->post['address']->city
) {
return mfResponse::BadRequest(['message' => "Mandatory address fields missing"]);
}
}
$shipping_address = "customer";
@@ -346,53 +366,68 @@ class PreorderApicontroller extends mfBaseApicontroller {
/**************************************************************
* search address in AddressDB
*/
$where = "1=1 ";
foreach($address_search as $field => $value) {
if($field == "ortschaft" || $field == "gemeinde") continue;
$where .= " AND `$field` = '$value'";
}
// filter salesclusters
if(count($this->filter_salescluster_ids)) {
$where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")";
}
$sql = "SELECT * FROM view_hausnummer WHERE $where";
$this->log->debug($sql);
$res = $this->db()->query($sql);
if(!$this->db()->num_rows($res)) {
// try with Ortschaft in front of strasse (liezen gwr issue)
if($request_oaid_hausnummer) {
$where = "oaid='$request_oaid_hausnummer'";
if(count($this->filter_salescluster_ids)) {
$where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")";
}
$res = $this->db()->select("view_hausnummer", "*", $where);
if(!$this->db()->num_rows($res)) {
return mfResponse::NotFound(["message" => "OAID not found"]);
}
$address = $this->db()->fetch_object($res);
} else {
$where = "1=1 ";
foreach($address_search as $field => $value) {
if($field == "ortschaft" || $field == "gemeinde") continue;
if($field == "strasse") {
$prefix = ($address_search['ortschaft']) ? $address_search['ortschaft'] : $address_search['gemeinde'];
if(strpos($value, "$prefix ") === 0) {
$str = substr($value, strlen($prefix)+1);
} else {
$str = $value;
}
$where .= " AND `strasse` = '$str'";
} else {
$where .= " AND `$field` = '$value'";
}
$where .= " AND `$field` = '$value'";
}
// filter salesclusters
if(count($this->filter_salescluster_ids)) {
$where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")";
}
$sql = "SELECT * FROM view_hausnummer WHERE $where";
$this->log->debug($sql);
$res = $this->db()->query($sql);
if(!$this->db()->num_rows($res)) {
return mfResponse::NotFound(['message' => "Adresse nicht gefunden"]);
// try with Ortschaft in front of strasse (liezen gwr issue)
$where = "1=1 ";
foreach($address_search as $field => $value) {
if($field == "ortschaft" || $field == "gemeinde") continue;
if($field == "strasse") {
$prefix = ($address_search['ortschaft']) ? $address_search['ortschaft'] : $address_search['gemeinde'];
if(strpos($value, "$prefix ") === 0) {
$str = substr($value, strlen($prefix)+1);
} else {
$str = $value;
}
$where .= " AND `strasse` = '$str'";
} else {
$where .= " AND `$field` = '$value'";
}
}
// filter salesclusters
if(count($this->filter_salescluster_ids)) {
$where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")";
}
$sql = "SELECT * FROM view_hausnummer WHERE $where";
$this->log->debug($sql);
$res = $this->db()->query($sql);
if(!$this->db()->num_rows($res)) {
return mfResponse::NotFound(['message' => "Adresse nicht gefunden"]);
}
}
$address = $this->db()->fetch_object($res);
}
$address = $this->db()->fetch_object($res);
//var_dump($address);exit;
/* **************************************************************
@@ -402,7 +437,30 @@ class PreorderApicontroller extends mfBaseApicontroller {
$assign_unit = false;
$where = "1=1 ";
$unit = false;
if(count($unit_search)) {
$unit_search_count_without_stock = count($unit_search);
if(array_key_exists("stock", $unit_search) && $unit_search['stock']) {
$unit_search_count_without_stock--;
}
if($request_oaid_unit) {
$where = "wohneinheit_oaid='$request_oaid_unit'";
if(count($this->filter_salescluster_ids)) {
$where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")";
}
//$unit = ADBWohneinheitModel::getFirst(["oaid" => $request_oaid_unit]);
$res = $this->db()->select("view_wohneinheit", "*", $where);
if($this->db()->num_rows($res)) {
$unit = $this->db()->fetch_object($res);
}
if(!$unit) {
return mfResponse::NotFound(["message" => "Wohneinheit nicht gefunden"]);
}
if(count($unit_search)) {
$update_unit = true;
}
} elseif($unit_search_count_without_stock > 0) {
//var_dump($unit_search);exit;
if(array_key_exists("unit_string", $unit_search)) {
$where .= " AND bezeichner LIKE '".$unit_search['unit_string']."'";
@@ -445,7 +503,7 @@ class PreorderApicontroller extends mfBaseApicontroller {
if($this->db()->num_rows($res)) {
$unit = $this->db()->fetch_object($res);
} else {
// XXX
// XXX - If there are no units without unit data maybe try finding a fitting unit without an order
//$assign_unit = true;
}
}
@@ -627,38 +685,51 @@ class PreorderApicontroller extends mfBaseApicontroller {
//var_dump($unit);
//exit;
// get available units
$exclude_wohneinheit_ids = [];
$preorders_in_hausnummer = PreorderModel::search(['adb_hausnummer_id' => $address->hausnummer_id]);
if(count($preorders_in_hausnummer)) {
foreach($preorders_in_hausnummer as $pih) {
if($pih->adb_wohneinheit_id) {
$exclude_wohneinheit_ids[] = $pih->adb_wohneinheit_id;
if($request_oaid_unit) {
// if unit oaid is set, we know our unit already
$unit = new ADBWohneinheit($unit->wohneinheit_id);
if($unit->block || $unit->stiege || $unit->stock || $unit->tuer) {
foreach(["block", "stiege", "stock", "tuer"] as $type) {
if($unit->$type && $unit->$type != $unit_search[$type]) {
return mfResponse::Forbidden(['message' => "Überschreiben von Wohneinheitsdaten nicht erlaubt"]);
}
}
}
}
} else {
// else get available units with no unit data, so we can update it
$exclude_wohneinheit_ids = [];
$preorders_in_hausnummer = PreorderModel::search(['adb_hausnummer_id' => $address->hausnummer_id]);
if(count($preorders_in_hausnummer)) {
// get units excluding unavailable units
$where = "hausnummer_id=".$address->hausnummer_id;
$where .= " AND ( block = '' OR block IS NULL)";
$where .= " AND ( stiege = '' OR stiege IS NULL)";
$where .= " AND ( stock = '' OR stock IS NULL)";
$where .= " AND ( tuer = '' OR tuer IS NULL)";
$where .= " AND ( bezeichner = '' OR bezeichner IS NULL)";
if(count($exclude_wohneinheit_ids)) {
$where .= " AND wohneinheit_id NOT IN (". implode(",",$exclude_wohneinheit_ids).")";
}
$sql = "SELECT * FROM view_wohneinheit WHERE $where";
$this->log->debug($sql);
$res = $this->db()->query($sql);
if(!$this->db()->num_rows($res)) {
return mfResponse::Forbidden(['message' => "Keine Wohneinheiten verfügbar"]);
foreach($preorders_in_hausnummer as $pih) {
if($pih->adb_wohneinheit_id) {
$exclude_wohneinheit_ids[] = $pih->adb_wohneinheit_id;
}
}
}
// get units excluding unavailable units
$where = "hausnummer_id=".$address->hausnummer_id;
$where .= " AND ( block = '' OR block IS NULL)";
$where .= " AND ( stiege = '' OR stiege IS NULL)";
$where .= " AND ( stock = '' OR stock IS NULL)";
$where .= " AND ( tuer = '' OR tuer IS NULL)";
$where .= " AND ( bezeichner = '' OR bezeichner IS NULL)";
if(count($exclude_wohneinheit_ids)) {
$where .= " AND wohneinheit_id NOT IN (". implode(",",$exclude_wohneinheit_ids).")";
}
$sql = "SELECT * FROM view_wohneinheit WHERE $where";
$this->log->debug($sql);
$res = $this->db()->query($sql);
if(!$this->db()->num_rows($res)) {
return mfResponse::Forbidden(['message' => "Keine Wohneinheiten verfügbar"]);
}
$unit_row = $this->db()->fetch_object($res);
$unit = new ADBWohneinheit($unit_row->wohneinheit_id);
}
$unit_row = $this->db()->fetch_object($res);
$unit = new ADBWohneinheit($unit_row->wohneinheit_id);
if(!$unit->id) {
return mfResponse::InternalServerError(['message' => "unit not found"]);
}
@@ -666,7 +737,7 @@ class PreorderApicontroller extends mfBaseApicontroller {
$change_note_data = [];
foreach(['block','stiege','stock','tuer','bezeichner'] as $unit_address_part) {
if($unit_search[$unit_address_part]) {
if($unit_search[$unit_address_part] && $unit_search[$unit_address_part] != $unit->$unit_address_part) {
$unit->$unit_address_part = $unit_search[$unit_address_part];
$unit_changed = true;
$change_note_data[] = [