Merge branch 'Preorder/change-unit-counting-logic' into 'master'

changed logic of unit count calculation based on internal tool type

See merge request fronk/thetool!1426
This commit is contained in:
Luca Haid
2025-06-10 07:46:48 +00:00
8 changed files with 135 additions and 678 deletions
@@ -7,10 +7,7 @@ class ADBNetzgebietModel {
public $source_id;
public $rimo_id;
public $freigabe;
public $unit_count;
public $unit_count_sd;
public $unit_count_md;
public $create = null;
public $edit = null;
@@ -175,13 +172,3 @@ class ADBNetzgebietModel {
}
}
/*
* Update unit_count in Netzgebiet:
UPDATE `Netzgebiet`
LEFT JOIN (SELECT netzgebiet_id, COUNT(*) as uc FROM Wohneinheit LEFT JOIN Hausnummer ON (Wohneinheit.hausnummer_id = Hausnummer.id) GROUP BY netzgebiet_id) AS uc_table ON (uc_table.netzgebiet_id = Netzgebiet.id)
SET unit_count = uc_table.uc
WHERE uc_table.uc IS NOT NULL;
*/
+22 -21
View File
@@ -148,28 +148,29 @@ class DashboardController extends mfBaseController {
}
private function getTotalHomes($preordercampaign_id = [], $gemeinde_id = false) {
$total_homes = 0;
return 0;
// $i_total_calculated_homes = 0;
if($gemeinde_id) {
$sql = "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 adb_strasse.gemeinde_id = $gemeinde_id
";
$res = $this->db()->query($sql);
if($this->db()->num_rows($res)) {
$data = $this->db()->fetch_object($res);
return $data->cnt;
}
} else {
foreach($preordercampaign_id as $campaign_id) {
$campaign = new Preordercampaign($campaign_id);
if($campaign->id) {
$total_homes += ($campaign->homes_total) ? $campaign->homes_total : $campaign->total_homes;
}
}
}
return $total_homes;
// if($gemeinde_id) {
// $sql = "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 adb_strasse.gemeinde_id = $gemeinde_id
// ";
// $res = $this->db()->query($sql);
// if($this->db()->num_rows($res)) {
// $data = $this->db()->fetch_object($res);
// return $data->cnt;
// }
// } else {
// foreach($preordercampaign_id as $campaign_id) {
// $campaign = new Preordercampaign($campaign_id);
// if($campaign->id) {
// $i_total_calculated_homes += ($campaign->homes_DEPRECATED_total) ? $campaign->homes_DEPRECATED_total : $campaign->homes_DEPRECATED_total;
// }
// }
// }
// return $i_total_calculated_homes;
}
private function getPartnerOrderCount($preordercampaign_id = [], $gemeinde_id = false) {
@@ -200,6 +200,9 @@ class DashboardNewController extends mfBaseController {
$efh_connection_types = ["single-dwelling", "business"];
$mph_connection_types = ["apartment-building", "apartment", "multi-dwelling"];
$efh_connection_types = [0,1];
$mph_connection_types = [2];
$countFunction = function($params, $statusFlag = null) use ($campaign_ids, $gemeinde_ids) {
$baseParams = ["preordercampaign_id" => $campaign_ids, "gemeinde_id" => $gemeinde_ids];
@@ -254,15 +257,15 @@ class DashboardNewController extends mfBaseController {
'baufortschritt_140' => $countFunction([">status_code" => "139", "<status_code" => "899"]),
// MAYBE OVERWORK
'order_efh' => $countFunction(["connection_type" => $efh_connection_types]),
'installationspaket_erhalten' => $countFunction(["connection_type" => $efh_connection_types], 145),
'lehrrohr_im_haus' => $countFunction(["connection_type" => $efh_connection_types], 200),
'order_efh' => $countFunction(["tool_building_type" => $efh_connection_types]),
'installationspaket_erhalten' => $countFunction(["tool_building_type" => $efh_connection_types], 145),
'lehrrohr_im_haus' => $countFunction(["tool_building_type" => $efh_connection_types], 200),
//END MAYBE OVERWORK
'inhouse_kabel_verlegt_efh' => $inhouse_kabel_verlegt_efh,
'inhouse_kabel_verlegt_efh_magenta' => $countFunction([">status_code" => "243", "<status_code" => "899", "partner_id" => 4803]),
'inhouse_kabel_verlegt_efh_salzburg' => $countFunction([">status_code" => "243", "<status_code" => "899", "partner_id" => 5668]),
'order_mph' => $countFunction(["connection_type" => $mph_connection_types]),
'inhouse_kabel_verlegt_mph' => $countFunction(["connection_type" => $mph_connection_types], 242),
'order_mph' => $countFunction(["tool_building_type" => $mph_connection_types]),
'inhouse_kabel_verlegt_mph' => $countFunction(["tool_building_type" => $mph_connection_types], 242),
'timeline' => $this->getTimeline('weekly-orders', $campaign_ids, $gemeinde_ids),
'timeline_inhouse_kabel_verlegt_efh' => $timeline_inhouse_kabel_verlegt_efh,
@@ -311,17 +314,17 @@ class DashboardNewController extends mfBaseController {
'type' => 'default',
'order_max_home_addrdb' => $order_max_homes,
'order_actual_order' => $countFunction([]),
'order_efh' => $countFunction(["connection_type" => $efh_connection_types]),
'order_efh_vorsorge' => $countFunction(["connection_type" => $efh_connection_types, "type" => "provision"]),
'order_efh_vollanschluss' => $countFunction(["connection_type" => $efh_connection_types, "type" => "order"]),
'order_mph' => $countFunction(["connection_type" => $mph_connection_types]),
'order_mph_vorsorge' => $countFunction(["connection_type" => $mph_connection_types, "type" => "provision"]),
'order_mph_vollanschluss' => $countFunction(["connection_type" => $mph_connection_types, "type" => "order"]),
'order_efh' => $countFunction(["tool_building_type" => $efh_connection_types]),
'order_efh_vorsorge' => $countFunction(["tool_building_type" => $efh_connection_types, "type" => "provision"]),
'order_efh_vollanschluss' => $countFunction(["tool_building_type" => $efh_connection_types, "type" => "order"]),
'order_mph' => $countFunction(["tool_building_type" => $mph_connection_types]),
'order_mph_vorsorge' => $countFunction(["tool_building_type" => $mph_connection_types, "type" => "provision"]),
'order_mph_vollanschluss' => $countFunction(["tool_building_type" => $mph_connection_types, "type" => "order"]),
'baufortschritt_140' => $baufortschritt_140,
'installationspaket_erhalten' => $countFunction(["connection_type" => $efh_connection_types], 145),
'lehrrohr_im_haus' => $countFunction(["connection_type" => $efh_connection_types], 200),
'inhouse_kabel_verlegt_efh' => $countFunction(["connection_type" => $efh_connection_types], 242),
'inhouse_kabel_verlegt_mph' => $countFunction(["connection_type" => $mph_connection_types], 242),
'installationspaket_erhalten' => $countFunction(["tool_building_type" => $efh_connection_types], 145),
'lehrrohr_im_haus' => $countFunction(["tool_building_type" => $efh_connection_types], 200),
'inhouse_kabel_verlegt_efh' => $countFunction(["tool_building_type" => $efh_connection_types], 242),
'inhouse_kabel_verlegt_mph' => $countFunction(["tool_building_type" => $mph_connection_types], 242),
'installationsfortschritt_245' => $countFunction([">status_code" => "244", "<status_code" => "899"]),
'ont_installiert_300' => $ont_installiert_300,
'vollanschluss_dokumentiert_350' => $countFunction(["status_code" => ["350","500"], "type" => "order"]),
@@ -390,17 +393,17 @@ class DashboardNewController extends mfBaseController {
'type' => 'default',
'order_max_home_addrdb' => $order_max_homes,
'order_actual_order' => $countFunction([]),
'order_efh' => $countFunction(["connection_type" => $efh_connection_types]),
'order_efh_vorsorge' => $countFunction(["connection_type" => $efh_connection_types, "type" => "provision"]),
'order_efh_vollanschluss' => $countFunction(["connection_type" => $efh_connection_types, "type" => "order"]),
'order_mph' => $countFunction(["connection_type" => $mph_connection_types]),
'order_mph_vorsorge' => $countFunction(["connection_type" => $mph_connection_types, "type" => "provision"]),
'order_mph_vollanschluss' => $countFunction(["connection_type" => $mph_connection_types, "type" => "order"]),
'order_efh' => $countFunction(["tool_building_type" => $efh_connection_types]),
'order_efh_vorsorge' => $countFunction(["tool_building_type" => $efh_connection_types, "type" => "provision"]),
'order_efh_vollanschluss' => $countFunction(["tool_building_type" => $efh_connection_types, "type" => "order"]),
'order_mph' => $countFunction(["tool_building_type" => $mph_connection_types]),
'order_mph_vorsorge' => $countFunction(["tool_building_type" => $mph_connection_types, "type" => "provision"]),
'order_mph_vollanschluss' => $countFunction(["tool_building_type" => $mph_connection_types, "type" => "order"]),
'baufortschritt_140' => $baufortschritt_140,
'installationspaket_erhalten' => $countFunction(["connection_type" => $efh_connection_types], 145),
'lehrrohr_im_haus' => $countFunction(["connection_type" => $efh_connection_types], 200),
'inhouse_kabel_verlegt_efh' => $countFunction(["connection_type" => $efh_connection_types], 242),
'inhouse_kabel_verlegt_mph' => $countFunction(["connection_type" => $mph_connection_types], 242),
'installationspaket_erhalten' => $countFunction(["tool_building_type" => $efh_connection_types], 145),
'lehrrohr_im_haus' => $countFunction(["tool_building_type" => $efh_connection_types], 200),
'inhouse_kabel_verlegt_efh' => $countFunction(["tool_building_type" => $efh_connection_types], 242),
'inhouse_kabel_verlegt_mph' => $countFunction(["tool_building_type" => $mph_connection_types], 242),
'installationsfortschritt_245' => $countFunction([">status_code" => "244", "<status_code" => "899"]),
'ont_installiert_300' => $ont_installiert_300,
'vollanschluss_dokumentiert_350' => $countFunction(["status_code" => ["350","500"], "type" => "order"]),
+83 -46
View File
@@ -495,7 +495,7 @@ class PreorderModel
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM `" . FRONKDB_DBNAME . "`.Preorder tt_preorder
LEFT JOIN `" . FRONKDB_DBNAME . "`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id)
LEFT JOIN `" . ADDRESSDB_DBNAME . "`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id)
LEFT JOIN `" . ADDRESSDB_DBNAME . "`.Hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.id)
LEFT JOIN `" . ADDRESSDB_DBNAME . "`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id)
LEFT OUTER JOIN (SELECT adb_wohneinheit_id, count(*) cnt, MAX(rimo_status) as rimo_status, MAX(rimo_name) as rimo_name, MAX(rimo_team_name) as rimo_team_name, MAX(rimo_team_id) as rimo_team_id FROM `" . FRONKDB_DBNAME . "`.RimoWorkorder WHERE rimo_status NOT IN ('deleted', 'cancelled') GROUP BY adb_wohneinheit_id) workorder ON adb_wohneinheit.id = workorder.adb_wohneinheit_id
WHERE $where
@@ -795,6 +795,18 @@ class PreorderModel
}
}
if (array_key_exists("tool_building_type", $filter)) {
$tool_building_type = $filter['tool_building_type'];
if (is_array($tool_building_type) && count($tool_building_type)) {
$where .= " AND adb_hausnummer.tool_building_type IN ('" . implode("','", $tool_building_type) . "')";
} else {
$tool_building_type = FronkDB::singleton()->escape($filter['connection_type']);
if ($tool_building_type) {
$where .= " AND adb_hausnummer.tool_building_type like '%$tool_building_type%'";
}
}
}
if (array_key_exists("connection_count", $filter)) {
$connection_count = $filter['connection_count'];
if (intval($connection_count) === 1) {
@@ -1095,7 +1107,7 @@ class PreorderModel
$sql = "SELECT COUNT(*) as cnt
FROM `" . FRONKDB_DBNAME . "`.Preorder tt_preorder
LEFT JOIN `" . FRONKDB_DBNAME . "`.Preorderstatus tt_preorderstatus ON tt_preorder.status_id = tt_preorderstatus.id
LEFT JOIN `" . ADDRESSDB_DBNAME . "`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id)
LEFT JOIN `" . ADDRESSDB_DBNAME . "`.Hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.id)
INNER JOIN `" . FRONKDB_DBNAME . "`.PreorderStatusflagValue psfv ON tt_preorder.id = psfv.preorder_id
INNER JOIN `" . FRONKDB_DBNAME . "`.PreorderStatusflag psf ON psfv.flag_id = psf.id
WHERE $where AND psf.code = " . $statusFlag . " AND psfv.value = 1";
@@ -1112,67 +1124,92 @@ class PreorderModel
}
public static function countActivePreorder($preorderCampaignId = null) {
$db = FronkDB::singleton();
$db = FronkDB::singleton();
$where = "";
if ($preorderCampaignId) {
$where .= " AND p.preordercampaign_id = $preorderCampaignId";
}
$where = "";
if ($preorderCampaignId) {
$where .= " AND p.preordercampaign_id = $preorderCampaignId";
}
$sql = "SELECT
COALESCE(SUM(CASE
WHEN LOWER(vh.gdaeigenschaft) IN ('multi dwelling', 'mehrparteienhaus', 'gebaeude mit 2 oder mehr wohnungen', 'gebäude mit 2 oder mehr wohnungen', 'wohngebaeude fuer gemeinschaften', 'wohngebäude für gemeinschaften', 'hotels und aehnliche gebaeude', 'hotels und ähnliche gebäude', 'wohngebäude mit 2 und mehr wohnungen')
OR LOWER(vh.rimo_type) IN ('multi dwelling', 'mehrparteienhaus', 'gebaeude mit 2 oder mehr wohnungen', 'gebäude mit 2 oder mehr wohnungen', 'wohngebaeude fuer gemeinschaften', 'wohngebäude für gemeinschaften', 'hotels und aehnliche gebaeude', 'hotels und ähnliche gebäude', 'wohngebäude mit 2 und mehr wohnungen')
THEN COALESCE(p.connection_count, 1)
ELSE 0
END), 0) AS md_count,
COALESCE(SUM(COALESCE(p.connection_count, 1)), 0) AS total_count
$sql = "SELECT
COALESCE(SUM(CASE
WHEN h.tool_building_type = 2 THEN COALESCE(p.connection_count, 1)
ELSE 0
END), 0) AS md_count,
COALESCE(SUM(COALESCE(p.connection_count, 1)), 0) AS total_count
FROM
`".FRONKDB_DBNAME."`.Preorder p
LEFT JOIN `".ADDRESSDB_DBNAME."`.Hausnummer h ON p.adb_hausnummer_id = h.id
LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus tt_preorderstatus ON p.status_id = tt_preorderstatus.id
WHERE p.deleted = 0 AND tt_preorderstatus.code < 899";
FROM
`".FRONKDB_DBNAME."`.Preorder p
LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer vh ON p.adb_hausnummer_id = vh.hausnummer_id
LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus tt_preorderstatus ON p.status_id = tt_preorderstatus.id
WHERE p.deleted = 0 AND tt_preorderstatus.code < 899";
$queryStart = microtime(true);
$res = $db->query($sql . $where);
mfLoghandler::singleton()->debug("[Query took: ".(microtime(true) - $queryStart)." seconds] " . $sql);
$queryStart = microtime(true);
$res = $db->query($sql . $where);
mfLoghandler::singleton()->debug("[Query took: ".(microtime(true) - $queryStart)." seconds] " . $sql);
if ($db->num_rows($res)) {
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
return ['md_count' => $data->md_count, 'sd_count' => $data->total_count - $data->md_count, 'total_count' => $data->total_count];
}
return ['md_count' => 0, 'sd_count' => 0, 'total_count' => 0];
return [
'md_count' => (int)$data->md_count,
'sd_count' => (int)($data->total_count - $data->md_count),
'total_count' => (int)$data->total_count
];
}
return [
'md_count' => 0,
'sd_count' => 0,
'total_count' => 0
];
}
public static function countTotalUnits($preorderCampaignId = null) {
$db = FronkDB::singleton();
$db = FronkDB::singleton();
$where = "1=1";
if ($preorderCampaignId) {
$where .= " AND pc.id = $preorderCampaignId";
}
$where = "1=1";
if ($preorderCampaignId) {
$where .= " AND pc.id = $preorderCampaignId";
}
$sql = "SELECT
pc.id AS campaign_id,
COALESCE(SUM(n.unit_count), 0) AS total_unit_count,
COALESCE(SUM(n.unit_count_sd), 0) AS total_unit_count_sd,
COALESCE(SUM(n.unit_count_md), 0) AS total_unit_count_md
FROM 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
WHERE $where
GROUP BY pc.id";
$sql = "SELECT
pc.id AS campaign_id,
COUNT(w.id) AS total_unit_count,
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
FROM 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."`.Wohneinheit w ON h.id = w.hausnummer_id
WHERE $where
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);
return ['total_unit_count' => $data->total_unit_count, 'total_unit_count_sd' => $data->total_unit_count_sd, 'total_unit_count_md' => $data->total_unit_count_md];
}
return ['total_unit_count' => 0, 'total_unit_count_sd' => 0, 'total_unit_count_md' => 0];
$data = $db->fetch_object($res);
return [
'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
];
}
return [
'total_unit_count' => 0,
'total_unit_count_sd' => 0,
'total_unit_count_md' => 0
];
}
public static function countHistoryStatus($filter = [], $status_code = null) {
if ($status_code === null) {
die("Please select a status code");
@@ -13,9 +13,6 @@ class Preordercampaign extends mfBaseModel {
private $salesclusters;
private $apiusers;
private $corsorigins;
private $total_homes;
private $total_homes_sd;
private $total_homes_md;
private $workorder_count;
private $required_fields = [];
private $banned_fcps;
@@ -132,41 +129,6 @@ class Preordercampaign extends mfBaseModel {
return $wo_count;
}
if($name == "total_homes_sd") {
$this->getProperty("total_homes");
return $this->total_homes_sd;
}
if($name == "total_homes_md") {
$this->getProperty("total_homes");
return $this->total_homes_md;
}
if($name == "total_homes") {
$total = 0;
$total_sd = 0;
$total_md = 0;
$netzgebiet_ids = null;
if ($this->getProperty("salesclusters")) {
foreach($this->getProperty("salesclusters") as $scluster) {
if($scluster->id) $netzgebiet_ids[] = $scluster->id;
}
}
if(is_array($netzgebiet_ids) && count($netzgebiet_ids)) {
foreach(ADBNetzgebietModel::search(["netzgebiet_id" => $netzgebiet_ids]) as $netzgebiet) {
$total += $netzgebiet->unit_count;
$total_sd += $netzgebiet->unit_count_sd;
$total_md += $netzgebiet->unit_count_md;
}
}
$this->total_homes = $total;
$this->total_homes_sd = $total_sd;
$this->total_homes_md = $total_md;
return $total;
}
if($name == "required_fields") {
if(!$this->required_preorder_fields) return [];
$this->required_fields = json_decode($this->required_preorder_fields);
@@ -53,7 +53,7 @@ class PreordercampaignController extends mfBaseController {
'id' => $c->id,
'network_id' => $c->network_id,
'network_name' => $c->network->name,
'network_owner_name' => $c->network->owner->getCompanyOrName(),
'network_owner_name' => $c->network->owner ? $c->network->owner->getCompanyOrName() : '',
'name' => $c->name,
'area' => $c->area,
'from' => $c->from,