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
{
$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 [];
}
$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,
$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
@@ -109,44 +93,48 @@ class PreorderIFrameModel extends mfBaseModel
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
";
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 [];
$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
{