From 3fbf66365605c2a7adec9b75677c0c8e9f4bf504 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Wed, 10 Dec 2025 09:27:26 +0100 Subject: [PATCH] updated dashboard to use preordermodel calculation --- .../DashboardNew/DashboardNewController.php | 41 +--------- application/Preorder/PreorderModel.php | 77 ++++++------------- 2 files changed, 25 insertions(+), 93 deletions(-) diff --git a/application/DashboardNew/DashboardNewController.php b/application/DashboardNew/DashboardNewController.php index c3bc888c4..8fde56738 100644 --- a/application/DashboardNew/DashboardNewController.php +++ b/application/DashboardNew/DashboardNewController.php @@ -196,7 +196,7 @@ class DashboardNewController extends mfBaseController { $campaign_ids = array_map(fn($campaign) => $campaign->id, $owner_campaigns); } - $order_max_homes = $this->getTotalHomes($campaign_ids, $gemeinde_ids); + $order_max_homes = PreorderModel::countTotalUnits($campaign_ids, $gemeinde_ids)['total_unit_count']; $efh_connection_types = ["single-dwelling", "business"]; $mph_connection_types = ["apartment-building", "apartment", "multi-dwelling"]; @@ -370,7 +370,7 @@ class DashboardNewController extends mfBaseController { $campaign_ids = [$campaign->id]; $gemeinde_ids = []; // Empty array as in original - $order_max_homes = $this->getTotalHomes($campaign_ids, $gemeinde_ids); + $order_max_homes = PreorderModel::countTotalUnits($campaign_ids, $gemeinde_ids)['total_unit_count']; $efh_connection_types = [0, 1]; // Single-dwelling and business $mph_connection_types = [2]; // Apartment-building, apartment, multi-dwelling @@ -568,43 +568,6 @@ class DashboardNewController extends mfBaseController { return $timeline; } - private function getTotalHomes(array $preordercampaign_id = [], array $gemeinde_id = []) { - $baseSQL = "SELECT COUNT(adb_wohneinheit.id) as cnt FROM `" . ADDRESSDB_DBNAME . "`.Wohneinheit adb_wohneinheit - LEFT JOIN `" . ADDRESSDB_DBNAME . "`.Hausnummer adb_hausnummer ON (adb_wohneinheit.hausnummer_id = adb_hausnummer.id) - LEFT JOIN `" . ADDRESSDB_DBNAME . "`.Strasse adb_strasse ON (adb_hausnummer.strasse_id = adb_strasse.id) - WHERE 1=1"; - - $where = ""; - - if (!empty($preordercampaign_id)) { - $netzgebiet_ids = []; - foreach ($preordercampaign_id as $campaign_id) { - $campaign = new Preordercampaign($campaign_id); - if ($campaign->network_id) { - $network = new Network($campaign->network_id); - $netzgebiet_ids[] = $network->adb_netzgebiet_id; - } - } - - $where .= " AND adb_hausnummer.netzgebiet_id IN (" . implode(',', array_map('intval', $netzgebiet_ids)) . ")"; - } - - if (!empty($gemeinde_id)) { - $where .= " AND adb_strasse.gemeinde_id IN (" . implode(',', array_map('intval', $gemeinde_id)) . ")"; - } - - $sql = $baseSQL . $where; - - $res = $this->db()->query($sql); - if ($this->db()->num_rows($res)) { - $data = $this->db()->fetch_object($res); - return $data->cnt; - } - return 0; - } - - - protected function getDashboardAddressDBDataAction() { if (!$this->me->is("Admin")) self::sendError("Keine Berechtigung"); $baseFilter = []; diff --git a/application/Preorder/PreorderModel.php b/application/Preorder/PreorderModel.php index bfe320679..ac5b621b4 100644 --- a/application/Preorder/PreorderModel.php +++ b/application/Preorder/PreorderModel.php @@ -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) {