fixed findAddresses query

This commit is contained in:
Luca Haid
2025-12-13 11:10:56 +00:00
parent 9f8bc2011d
commit 1435923200

View File

@@ -83,25 +83,9 @@ class PreorderIFrameModel extends mfBaseModel
public function findAddresses(array $params): array public function findAddresses(array $params): array
{ {
$whereClauses = [ if (empty($params['gemeinde_id']) && empty($params['cluster_id'])) return [];
"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'])) { $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,
$whereClauses[] = "h.gemeinde_id = " . intval($params['gemeinde_id']);
} elseif (!empty($params['cluster_id'])) {
$whereClauses[] = "h.netzgebiet_id = " . intval($params['cluster_id']);
} else {
return [];
}
$whereString = implode(" AND ", $whereClauses);
$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, 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 w.oaid as unit_oaid, w.stock, w.tuer, w.zusatz
FROM addressdb.Hausnummer h FROM addressdb.Hausnummer h
@@ -109,40 +93,44 @@ class PreorderIFrameModel extends mfBaseModel
JOIN addressdb.Plz p ON h.plz_id = p.id JOIN addressdb.Plz p ON h.plz_id = p.id
JOIN addressdb.Ortschaft o ON s.gemeinde_id = o.gemeinde_id JOIN addressdb.Ortschaft o ON s.gemeinde_id = o.gemeinde_id
LEFT JOIN addressdb.Wohneinheit w ON w.hausnummer_id = h.id LEFT JOIN addressdb.Wohneinheit w ON w.hausnummer_id = h.id
WHERE $whereString 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']) . "'";
$cond = !empty($params['gemeinde_id'])
? " AND h.gemeinde_id = " . intval($params['gemeinde_id'])
: " AND h.netzgebiet_id = " . intval($params['cluster_id']);
$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 []; if (empty($results)) return [];
$orderType = $params['orderType'] ?? 'order'; if (($params['orderType'] ?? 'order') === 'interest') {
$addr = $this->formatAddressRow($results[0]);
// For 'interest' order type, return a single entry for the whole building. $addr['wohneinheit_id'] = null;
if ($orderType === 'interest') { $addr['oaid'] = $results[0]['oaid'];
$representativeAddress = $this->formatAddressRow($results[0]); $addr['showText'] = "Gesamtes Gebäude";
$representativeAddress['wohneinheit_id'] = null; // Critical: No specific unit $addr['preorderTypes'] = ['interest'];
$representativeAddress['oaid'] = $results[0]['oaid']; // Use building OAID return [$addr];
$representativeAddress['showText'] = "Gesamtes Gebäude";
$representativeAddress['preorderTypes'] = ['interest'];
return [$representativeAddress]; // Return one item, so frontend proceeds directly.
} }
// Original logic for 'order' type
$addresses = []; $addresses = [];
$topCounter = 1;
if (count($results) > 1 && $results[0]['wohneinheit_id'] !== null) { if (count($results) > 1 && $results[0]['wohneinheit_id'] !== null) {
$i = 1;
foreach ($results as $row) { foreach ($results as $row) {
$address = $this->formatAddressRow($row); $addr = $this->formatAddressRow($row);
$address['showText'] = $this->buildShowText($row, $topCounter++); $addr['showText'] = $this->buildShowText($row, $i++);
$address['preorderTypes'] = ['order']; $addr['preorderTypes'] = ['order'];
$addresses[] = $address; $addresses[] = $addr;
} }
} else { } else {
// Single unit or building without units $addr = $this->formatAddressRow($results[0]);
$address = $this->formatAddressRow($results[0]); $addr['preorderTypes'] = ['order'];
$address['preorderTypes'] = ['order']; $addresses[] = $addr;
$addresses[] = $address;
} }
return $addresses; return $addresses;