updated dashboard to use preordermodel calculation

This commit is contained in:
2025-12-10 09:27:26 +01:00
parent 7f01604803
commit 3fbf663656
2 changed files with 25 additions and 93 deletions
+23 -54
View File
@@ -1261,46 +1261,34 @@ class PreorderModel
];
}
public static function countTotalUnits($preorderCampaignId = null) {
public static function countTotalUnits($preorderCampaignId = null, $gemeindeId = null) {
$db = FronkDB::singleton();
$where = ["1=1"];
// The new WHERE condition is more complex and implemented directly in the main query.
$where = "1=1";
// Support both array and single campaign ID
if ($preorderCampaignId) {
$where .= " AND pc.id = " . (int)$preorderCampaignId;
$campaignIds = is_array($preorderCampaignId) ? array_map('intval', $preorderCampaignId) : [(int)$preorderCampaignId];
$where[] = "pc.id IN (" . implode(',', $campaignIds) . ")";
}
// This query now implements the conditional logic for counting units.
// A unit is counted if its building type is standard, OR if its type is special AND has an active preorder.
$sql = "SELECT
pc.id AS campaign_id,
-- Total unit count based on the new logic
if ($gemeindeId) {
$gemeindeIds = is_array($gemeindeId) ? array_map('intval', $gemeindeId) : [(int)$gemeindeId];
$where[] = "s.gemeinde_id IN (" . implode(',', $gemeindeIds) . ")";
}
$whereClause = implode(' AND ', $where);
$sql = "SELECT
COUNT(w.id) AS total_unit_count,
-- SD unit count (Single Dwelling)
SUM(CASE
WHEN h.tool_building_type IN (0, 1) THEN 1
ELSE 0
END) AS total_unit_count_sd,
-- MD unit count (Multi Dwelling)
SUM(CASE
WHEN h.tool_building_type = 2 THEN 1
ELSE 0
END) AS total_unit_count_md,
-- NEW Not2Connect unit count
SUM(CASE
WHEN h.rimo_op_state = 'Not2Connect' THEN 1
ELSE 0
END) AS total_unit_count_not2connect
SUM(CASE WHEN h.tool_building_type IN (0, 1) THEN 1 ELSE 0 END) AS total_unit_count_sd,
SUM(CASE WHEN h.tool_building_type = 2 THEN 1 ELSE 0 END) AS total_unit_count_md,
SUM(CASE WHEN h.rimo_op_state = 'Not2Connect' THEN 1 ELSE 0 END) AS total_unit_count_not2connect
FROM `".FRONKDB_DBNAME."`.Preordercampaign pc
LEFT JOIN `".FRONKDB_DBNAME."`.PreordercampaignSalescluster pcs ON pc.id = pcs.preordercampaign_id
LEFT JOIN `".ADDRESSDB_DBNAME."`.Netzgebiet n ON pcs.salescluster_id = n.id
LEFT JOIN `".ADDRESSDB_DBNAME."`.Hausnummer h ON n.id = h.netzgebiet_id
LEFT JOIN `".ADDRESSDB_DBNAME."`.Strasse s ON h.strasse_id = s.id
LEFT JOIN `".ADDRESSDB_DBNAME."`.Wohneinheit w ON h.id = w.hausnummer_id
-- Subquery to find all buildings that have at least one active preorder
LEFT JOIN (
SELECT p_sub.adb_hausnummer_id
FROM `".FRONKDB_DBNAME."`.Preorder p_sub
@@ -1308,26 +1296,12 @@ class PreorderModel
WHERE p_sub.deleted = 0 AND ps_sub.code NOT IN (20) AND ps_sub.code < 899
GROUP BY p_sub.adb_hausnummer_id
) AS active_preorders ON h.id = active_preorders.adb_hausnummer_id
WHERE
($where)
AND
(
-- Condition 1: Include unit if its building rimo_type is NOT one of the special types.
h.rimo_type NOT IN ('greenfield', 'other', 'transmitting station', 'transformer station', 'outdoor cabinet')
OR
-- Condition 2: OR if the rimo_type IS special (or NULL), include it ONLY IF an active preorder exists for the building.
(
(h.rimo_type IS NULL OR h.rimo_type IN ('greenfield', 'other', 'transmitting station', 'transformer station', 'outdoor cabinet'))
AND active_preorders.adb_hausnummer_id IS NOT NULL
)
)
GROUP BY pc.id";
WHERE ($whereClause)
AND (h.rimo_type NOT IN ('greenfield', 'other', 'transmitting station', 'transformer station', 'outdoor cabinet')
OR ((h.rimo_type IS NULL OR h.rimo_type IN ('greenfield', 'other', 'transmitting station', 'transformer station', 'outdoor cabinet'))
AND active_preorders.adb_hausnummer_id IS NOT NULL))";
$queryStart = microtime(true);
$res = $db->query($sql);
mfLoghandler::singleton()->debug("[Query took: ".(microtime(true) - $queryStart)." seconds] " . $sql);
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
@@ -1335,16 +1309,11 @@ class PreorderModel
'total_unit_count' => (int)$data->total_unit_count,
'total_unit_count_sd' => (int)$data->total_unit_count_sd,
'total_unit_count_md' => (int)$data->total_unit_count_md,
'total_unit_count_not2connect' => (int)$data->total_unit_count_not2connect // New return value
'total_unit_count_not2connect' => (int)$data->total_unit_count_not2connect
];
}
return [
'total_unit_count' => 0,
'total_unit_count_sd' => 0,
'total_unit_count_md' => 0,
'total_unit_count_not2connect' => 0
];
return ['total_unit_count' => 0, 'total_unit_count_sd' => 0, 'total_unit_count_md' => 0, 'total_unit_count_not2connect' => 0];
}
public static function countHistoryStatus($filter = [], $status_code = null) {