diff --git a/application/ADBWohneinheit/ADBWohneinheitModel.php b/application/ADBWohneinheit/ADBWohneinheitModel.php index 49c3dc487..211ab60d2 100644 --- a/application/ADBWohneinheit/ADBWohneinheitModel.php +++ b/application/ADBWohneinheit/ADBWohneinheitModel.php @@ -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) { diff --git a/application/Api/v1/PreorderApicontroller.php b/application/Api/v1/PreorderApicontroller.php index cb59c0c8e..1d0adf2a2 100644 --- a/application/Api/v1/PreorderApicontroller.php +++ b/application/Api/v1/PreorderApicontroller.php @@ -270,13 +270,13 @@ class PreorderApicontroller extends mfBaseApicontroller { $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($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]; + $request_oaid_unit = "$request_oaid_hausnummer".$m[2]; } } } @@ -290,7 +290,7 @@ class PreorderApicontroller extends mfBaseApicontroller { * check address */ //var_dump($request_oaid_unit);exit; - if(!$request_oaid_unit) { + 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 || @@ -367,9 +367,16 @@ class PreorderApicontroller extends mfBaseApicontroller { * search address in AddressDB */ if($request_oaid_hausnummer) { - $res = $this->db()->select("view_hausnummer", "*", "oaid='$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 nicht gefunden"]); + return mfResponse::NotFound(["message" => "OAID not found"]); } $address = $this->db()->fetch_object($res); } else { @@ -431,13 +438,29 @@ class PreorderApicontroller extends mfBaseApicontroller { $where = "1=1 "; $unit = false; + $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) { - $unit = ADBWohneinheitModel::getFirst(["oaid" => $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(count($unit_search)) { + } 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']."'"; @@ -480,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; } } @@ -662,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)) { + + 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); } - // 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); if(!$unit->id) { return mfResponse::InternalServerError(['message' => "unit not found"]); } @@ -701,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[] = [