updated dashboard to use preordermodel calculation
This commit is contained in:
@@ -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 = [];
|
||||
|
||||
@@ -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.
|
||||
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
|
||||
pc.id AS campaign_id,
|
||||
|
||||
-- Total unit count based on the new logic
|
||||
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')
|
||||
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))";
|
||||
|
||||
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";
|
||||
|
||||
$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) {
|
||||
|
||||
Reference in New Issue
Block a user