From 1435923200d3f4209b885822c0032a6dff75b245 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Sat, 13 Dec 2025 11:10:56 +0000 Subject: [PATCH] fixed findAddresses query --- .../PreorderIFrame/PreorderIFrameModel.php | 86 ++++++++----------- 1 file changed, 37 insertions(+), 49 deletions(-) diff --git a/application/PreorderIFrame/PreorderIFrameModel.php b/application/PreorderIFrame/PreorderIFrameModel.php index 692c9a518..a0c123881 100644 --- a/application/PreorderIFrame/PreorderIFrameModel.php +++ b/application/PreorderIFrame/PreorderIFrameModel.php @@ -80,73 +80,61 @@ class PreorderIFrameModel extends mfBaseModel return array_column($rows, 'name'); } - + public function findAddresses(array $params): array { - $whereClauses = [ - "p.plzstring = " . $this->db->escape($params['zip']), - "o.name = '" . $this->db->escape($params['city']) . "'", - "s.name = '" . $this->db->escape($params['street']) . "'", - "h.hausnummer = '" . $this->db->escape($params['housenumber']) . "'", - ]; + if (empty($params['gemeinde_id']) && empty($params['cluster_id'])) return []; - if (!empty($params['gemeinde_id'])) { - $whereClauses[] = "h.gemeinde_id = " . intval($params['gemeinde_id']); - } elseif (!empty($params['cluster_id'])) { - $whereClauses[] = "h.netzgebiet_id = " . intval($params['cluster_id']); - } else { - return []; - } + $sql = "SELECT h.oaid, h.hausnummer, s.name as street, p.plzstring as zip, o.name as city, h.id as hausnummer_id, w.id as wohneinheit_id, + h.stiege, h.unit_count as building_unit_count, h.tool_building_type as building_type, + w.oaid as unit_oaid, w.stock, w.tuer, w.zusatz + FROM addressdb.Hausnummer h + JOIN addressdb.Strasse s ON h.strasse_id = s.id + JOIN addressdb.Plz p ON h.plz_id = p.id + JOIN addressdb.Ortschaft o ON s.gemeinde_id = o.gemeinde_id + LEFT JOIN addressdb.Wohneinheit w ON w.hausnummer_id = h.id + WHERE p.plzstring = " . $this->db->escape($params['zip']) . " + AND o.name = '" . $this->db->escape($params['city']) . "' + AND s.name = '" . $this->db->escape($params['street']) . "' + AND h.hausnummer = '" . $this->db->escape($params['housenumber']) . "'"; - $whereString = implode(" AND ", $whereClauses); + $cond = !empty($params['gemeinde_id']) + ? " AND h.gemeinde_id = " . intval($params['gemeinde_id']) + : " AND h.netzgebiet_id = " . intval($params['cluster_id']); - $query = " - SELECT h.oaid, h.hausnummer, s.name as street, p.plzstring as zip, o.name as city, h.id as hausnummer_id, w.id as wohneinheit_id, - h.stiege, h.unit_count as building_unit_count, h.tool_building_type as building_type, - w.oaid as unit_oaid, w.stock, w.tuer, w.zusatz - FROM addressdb.Hausnummer h - JOIN addressdb.Strasse s ON h.strasse_id = s.id - JOIN addressdb.Plz p ON h.plz_id = p.id - JOIN addressdb.Ortschaft o ON s.gemeinde_id = o.gemeinde_id - LEFT JOIN addressdb.Wohneinheit w ON w.hausnummer_id = h.id - WHERE $whereString - "; + $results = $this->db->fetch_all_assoc($this->db->query($sql . $cond)); + + if (empty($results) && empty($params['gemeinde_id'])) + $results = $this->db->fetch_all_assoc($this->db->query($sql)); - $results = $this->db->fetch_all_assoc($this->db->query($query)); if (empty($results)) return []; - $orderType = $params['orderType'] ?? 'order'; - - // For 'interest' order type, return a single entry for the whole building. - if ($orderType === 'interest') { - $representativeAddress = $this->formatAddressRow($results[0]); - $representativeAddress['wohneinheit_id'] = null; // Critical: No specific unit - $representativeAddress['oaid'] = $results[0]['oaid']; // Use building OAID - $representativeAddress['showText'] = "Gesamtes Gebäude"; - $representativeAddress['preorderTypes'] = ['interest']; - return [$representativeAddress]; // Return one item, so frontend proceeds directly. + if (($params['orderType'] ?? 'order') === 'interest') { + $addr = $this->formatAddressRow($results[0]); + $addr['wohneinheit_id'] = null; + $addr['oaid'] = $results[0]['oaid']; + $addr['showText'] = "Gesamtes Gebäude"; + $addr['preorderTypes'] = ['interest']; + return [$addr]; } - // Original logic for 'order' type $addresses = []; - $topCounter = 1; - if (count($results) > 1 && $results[0]['wohneinheit_id'] !== null) { + $i = 1; foreach ($results as $row) { - $address = $this->formatAddressRow($row); - $address['showText'] = $this->buildShowText($row, $topCounter++); - $address['preorderTypes'] = ['order']; - $addresses[] = $address; + $addr = $this->formatAddressRow($row); + $addr['showText'] = $this->buildShowText($row, $i++); + $addr['preorderTypes'] = ['order']; + $addresses[] = $addr; } } else { - // Single unit or building without units - $address = $this->formatAddressRow($results[0]); - $address['preorderTypes'] = ['order']; - $addresses[] = $address; + $addr = $this->formatAddressRow($results[0]); + $addr['preorderTypes'] = ['order']; + $addresses[] = $addr; } return $addresses; - } +} private function formatAddressRow(array $row): array {