Files
thetool/application/DashboardNew/DashboardNewController.php
2025-06-05 15:24:45 +02:00

634 lines
33 KiB
PHP

<?php
//TODO: interessen
class DashboardNewController extends mfBaseController {
private User $me;
protected function init(): void {
$this->needlogin=true;
$me = new User();
$me->loadMe();
$this->layout()->set("me", $me);
$this->me = $me;
if ($this->me->address_id === '5908') $this->me->address_id = '209';
}
protected function indexAction() {
if (!$this->me->can("Statistics") || !$this->me->is(["Admin", "netowner", "salespartner"])) {
$this->redirect("Dashboard");
}
$this->layout()->set('additionalJS', ["plugins/chart.js/chart.4.4.6.js", "plugins/chart.js/chartjs-adapter-moment.min.js"]);
Helper::renderVue($this, $this->mod, "Dashboard", [
"IS_ADMIN" => $this->me->is("Admin") ? "true" : "false",
"ME_ADDRESS_ID" => $this->me->address_id,
]);
}
protected function adbAction() {
if (!$this->me->can("Statistics") || !$this->me->is(["Admin", "netowner", "salespartner"])) {
$this->redirect("Dashboard");
}
if (!$this->me->is("Admin")) {
$this->redirect("Dashboard");
}
Helper::renderVue($this, "DashboardAdb", "Dashboard ADB", ["IS_ADMIN" => $this->me->is("Admin") ? "true" : "false"]);
}
protected function getNetOwnerFilterOptionsAction() {
$allPreorderCampaigns = PreordercampaignModel::getAll();
$netowners = [];
foreach ($allPreorderCampaigns as $campaign) {
$network = new Network($campaign->network_id);
if (!$this->me->is("Admin") && $network->owner_id != $this->me->address_id) continue;
$networkOwner = new Address($network->owner_id);
$ownerName = $networkOwner->getCompanyOrName();
if (!in_array($network->owner_id, array_column($netowners, 'value'))) {
$netowners[] = ['text' => $ownerName, 'value' => $network->owner_id];
}
}
self::returnJson($netowners);
}
protected function getNetworkFilterOptionsAction() {
$netowners = [];
$allNetworks = ADBNetzgebietModel::getAll();
foreach ($allNetworks as $network) {
if (!$this->me->is("Admin") && $network->owner_id != $this->me->address_id) continue;
if (!in_array($network->id, array_column($netowners, 'value'))) {
$netowners[] = ['text' => $network->name, 'value' => $network->id];
}
}
self::returnJson($netowners);
}
protected function getCampaignFilterOptionsAction() {
$post = json_decode(file_get_contents('php://input'), true);
$netowner_ids = isset($post['netOwners']) ? [$post['netOwners']] : [];
$campaigns = [];
$all_campaigns = $this->me->is("Admin") ? PreordercampaignModel::getAll() : PreordercampaignModel::search(["owner_id" => $this->me->address_id]);
if (!empty($netowner_ids)) {
foreach ($all_campaigns as $campaign) {
$networkOwner = $campaign->network->owner_id;
if (!in_array($networkOwner, $netowner_ids)) continue;
if (!in_array($campaign->id, array_column($campaigns, 'value'))) {
$campaigns[] = ['text' => $campaign->name, 'value' => $campaign->id];
}
}
} else {
foreach ($all_campaigns as $campaign) {
if (!in_array($campaign->id, array_column($campaigns, 'value'))) {
$campaigns[] = ['text' => $campaign->name, 'value' => $campaign->id];
}
}
}
self::returnJson($campaigns);
}
protected function getCampaignGemeindeFilterOptionsAction() {
$post = json_decode(file_get_contents('php://input'), true);
$netowner_ids = isset($post['netOwners']) ? [$post['netOwners']] : [];
$campaign_ids = isset($post['campaigns']) ? [$post['campaigns']] : [];
$campaigns = [];
$all_campaigns = $this->me->is("Admin") ? PreordercampaignModel::getAll() : PreordercampaignModel::search(["owner_id" => $this->me->address_id]);
if (!empty($netowner_ids)) {
foreach ($all_campaigns as $campaign) {
$networkOwner = $campaign->network->owner_id;
if (!in_array($networkOwner, $netowner_ids)) continue;
if (in_array($campaign->id, $campaign_ids)) continue;
$campaigns[] = $campaign->id;
}
} else {
foreach ($all_campaigns as $campaign) {
if (in_array($campaign->id, $campaign_ids)) continue;
$campaigns[] = $campaign->id;
}
}
if (!empty($campaign_ids)) {
self::returnJson($this->getGemeindenFromCampaigns($campaign_ids));
return;
}
self::returnJson($this->getGemeindenFromCampaigns($campaigns));
}
private function checkParameterAuthorization($campaign_ids = []) {
$campaigns = PreordercampaignModel::search(["owner_id" => $this->me->address_id]);
foreach ($campaigns as $campaign) {
if (!in_array($campaign->id, $campaign_ids)) {
$key = array_search($campaign->id, $campaign_ids);
unset($campaign_ids[$key]);
}
}
return $campaign_ids;
}
private function getGemeindenFromCampaigns($campaignids = []) {
$gemeinden = [];
foreach ($campaignids as $campaign_id) {
$campaign = new Preordercampaign($campaign_id);
if (!$campaign || !$campaign->network_id || !$campaign->network->adb_netzgebiet_id) continue;
$gems = $campaign->network->adb_netzgebiet->gemeinden;
if (!is_array($gems)) continue;
foreach ($gems as $gem) {
if (!in_array($gem->id, array_column($gemeinden, 'value'))) $gemeinden[] = ['text' => $gem->name, 'value' => $gem->id];
}
}
ksort($gemeinden);
return $gemeinden;
}
protected function getDashboardDataAction() {
$post = json_decode(file_get_contents('php://input'), true);
$netowner_ids = $post['netOwners'] === '' ? [] : [$post['netOwners']] ?? [];
$campaign_ids = $post['campaigns'] === '' ? [] : [$post['campaigns']] ?? [];
$gemeinde_ids = $post['gemeinden'] === '' ? [] : [$post['gemeinden']] ?? [];
if (!empty($netowner_ids)) {
$all_campaigns = $this->me->is("Admin") ? PreordercampaignModel::getAll() : PreordercampaignModel::search(["owner_id" => $this->me->address_id]);
if ($all_campaigns[0] === NULL) {
http_response_code(500);
self::returnJson(["status" => 500, "message" => "Keine Kampagnen gefunden"]);
}
$campaign_ids = empty($campaign_ids) ?
array_map(fn($campaign) => $campaign->id, $all_campaigns) :
$campaign_ids;
$campaign_ids = array_filter($campaign_ids, function ($campaign_id) use ($netowner_ids) {
$campaign = new Preordercampaign($campaign_id);
return in_array($campaign->network->owner_id, $netowner_ids);
});
}
if (empty($campaign_ids) && !$this->me->is("Admin")) {
$owner_campaigns = PreordercampaignModel::search(["owner_id" => $this->me->address_id]);
if (empty($owner_campaigns)) {
http_response_code(500);
self::returnJson(["status" => 500, "message" => "Keine Kampagnen gefunden"]);
}
$campaign_ids = array_map(fn($campaign) => $campaign->id, $owner_campaigns);
}
$order_max_homes = $this->getTotalHomes($campaign_ids, $gemeinde_ids);
$efh_connection_types = ["single-dwelling", "business"];
$mph_connection_types = ["apartment-building", "apartment", "multi-dwelling"];
$countFunction = function($params, $statusFlag = null) use ($campaign_ids, $gemeinde_ids) {
$baseParams = ["preordercampaign_id" => $campaign_ids, "gemeinde_id" => $gemeinde_ids];
$params = array_merge($baseParams, $params);
return $statusFlag ?
PreorderModel::countStatusFlagsActive($params, $statusFlag) :
PreorderModel::countActive($params);
};
if ($this->me->address_id === '4807') {
$inhouse_kabel_verlegt_efh = $countFunction([">status_code" => "243", "<status_code" => "899"]);
$timeline_inhouse_kabel_verlegt_efh = $this->getTimeline('weekly-inhouse-kabel-verlegt-efh', $campaign_ids, $gemeinde_ids);
$inhouse_kabel_verlegt_efh_base_value_before_history = $inhouse_kabel_verlegt_efh - $timeline_inhouse_kabel_verlegt_efh[0][count($timeline_inhouse_kabel_verlegt_efh[0]) - 1]['value'];
foreach ($timeline_inhouse_kabel_verlegt_efh[0] as $key => $value) {
$timeline_inhouse_kabel_verlegt_efh[0][$key]['value'] += $inhouse_kabel_verlegt_efh_base_value_before_history;
}
$status_500_energie_steiermark = $countFunction(["partner_id" => [209,5033, 3151, 7685, 7754, 7764], "status_code" => "500", "<status_code" => 899]);
$timeline_status_500_energie_steiermark = $this->getTimeline('weekly-order-finished-energy-steiermark', $campaign_ids, $gemeinde_ids);
$status_500_energie_steiermark_base_value_before_history = $status_500_energie_steiermark - $timeline_status_500_energie_steiermark[0][count($timeline_status_500_energie_steiermark[0]) - 1]['value'];
// var_dump($this->getTimeline('weekly-order-finished-energy-steiermark', $campaign_ids, $gemeinde_ids));exit;
foreach ($timeline_status_500_energie_steiermark[0] as $key => $value) {
$timeline_status_500_energie_steiermark[0][$key]['value'] += $status_500_energie_steiermark_base_value_before_history;
}
$status_500_magenta = $countFunction(["partner_id" => 4803, "status_code" => "500", "<status_code" => 899]);
$timeline_status_500_magenta = $this->getTimeline('weekly-order-finished-magenta', $campaign_ids, $gemeinde_ids);
$status_500_magenta_base_value_before_history = $status_500_magenta - $timeline_status_500_magenta[0][count($timeline_status_500_magenta[0]) - 1]['value'];
foreach ($timeline_status_500_magenta[0] as $key => $value) {
$timeline_status_500_magenta[0][$key]['value'] += $status_500_magenta_base_value_before_history;
}
$status_500_salzburg_ag = $countFunction(["partner_id" => 5668, "status_code" => "500", "<status_code" => 899]);
$timeline_status_500_salzburg_ag = $this->getTimeline('weekly-order-finished-salzburg-ag', $campaign_ids, $gemeinde_ids);
$status_500_salzburg_ag_base_value_before_history = $status_500_salzburg_ag - $timeline_status_500_salzburg_ag[0][count($timeline_status_500_salzburg_ag[0]) - 1]['value'];
foreach ($timeline_status_500_salzburg_ag[0] as $key => $value) {
$timeline_status_500_salzburg_ag[0][$key]['value'] += $status_500_salzburg_ag_base_value_before_history;
}
self::returnJson([
'type' => 'rml',
'order_max_home_addrdb' => $order_max_homes,
'order_actual_order' => $countFunction([]),
'order_energie_steiermark' => $countFunction(["partner_id" => [209,5033, 3151, 7685, 7754, 7764]]),
'order_magenta' => $countFunction(["partner_id" => 4803]),
'order_salzburg_ag' => $countFunction(["partner_id" => 5668]),
'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),
//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),
'timeline' => $this->getTimeline('weekly-orders', $campaign_ids, $gemeinde_ids),
'timeline_inhouse_kabel_verlegt_efh' => $timeline_inhouse_kabel_verlegt_efh,
'timeline_status_500_energie_steiermark' => $timeline_status_500_energie_steiermark,
'timeline_status_500_magenta' => $timeline_status_500_magenta,
'timeline_status_500_salzburg_ag' => $timeline_status_500_salzburg_ag,
// status 244 245 -
'status_244_245_energie_steiermark' => $countFunction(["partner_id" => [209,5033, 3151, 7685, 7754, 7764], ">status_code" => "243", "<status_code" => "899"]),
'status_244_245_magenta' => $countFunction(["partner_id" => 4803, ">status_code" => "243", "<status_code" => "899"]),
'status_244_245_salzburg_ag' => $countFunction(["partner_id" => 5668, ">status_code" => "243", "<status_code" => "899"]),
'status_500_energy_steiermark' => $status_500_energie_steiermark,
'status_500_magenta' => $status_500_magenta,
'status_500_salzburg_ag' => $status_500_salzburg_ag,
]);
}
$baufortschritt_140 = $countFunction([">status_code" => "139", "<status_code" => "899"]);
$timeline_baufortschritt_140 = $this->getTimeline('weekly-leerrohr', $campaign_ids, $gemeinde_ids);
$baufortschritt_140_base_value_before_history = $baufortschritt_140 - $timeline_baufortschritt_140[0][count($timeline_baufortschritt_140[0]) - 1]['value'];
foreach ($timeline_baufortschritt_140[0] as $key => $value) {
$timeline_baufortschritt_140[0][$key]['value'] += $baufortschritt_140_base_value_before_history;
}
$ont_installiert_300 = $countFunction([">status_code" => "299", "<status_code" => "899"]);
$timeline_ont_installiert_300 = $this->getTimeline('weekly-ont-installed', $campaign_ids, $gemeinde_ids);
$ont_installiert_300_base_value_before_history = $ont_installiert_300 - $timeline_ont_installiert_300[0][count($timeline_ont_installiert_300[0]) - 1]['value'];
foreach ($timeline_ont_installiert_300[0] as $key => $value) {
$timeline_ont_installiert_300[0][$key]['value'] += $ont_installiert_300_base_value_before_history;
}
$provider_bestellt_500 = $countFunction([">status_code" => "499", "<status_code" => "899"]);
$timeline_provider_bestellt_500 = $this->getTimeline('weekly-provider-bestellt', $campaign_ids, $gemeinde_ids);
$provider_bestellt_500_base_value_before_history = $provider_bestellt_500 - $timeline_provider_bestellt_500[0][count($timeline_provider_bestellt_500[0]) - 1]['value'];
foreach ($timeline_provider_bestellt_500[0] as $key => $value) {
$timeline_provider_bestellt_500[0][$key]['value'] += $provider_bestellt_500_base_value_before_history;
}
self::returnJson([
'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"]),
'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),
'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"]),
'vorsorge_dokumentiert_351' => $countFunction(["status_code" => ["351","500"], "type" => "provision"]),
'provider_bestellt_500' => $provider_bestellt_500,
'timeline' => $this->getTimeline('weekly-orders', $campaign_ids, $gemeinde_ids),
'timeline_leerrohr' => $timeline_baufortschritt_140,
'timeline_ont_installed' => $timeline_ont_installiert_300,
'timeline_provider_bestellt' => $timeline_provider_bestellt_500
]);
}
protected function exportAction() {
// Filter campaigns where network owner is 209
$netowner_id = 209;
// Get all campaigns for this network owner
$all_campaigns = $this->me->is("Admin") ? PreordercampaignModel::getAll() : PreordercampaignModel::search(["owner_id" => $this->me->address_id]);
if (empty($all_campaigns) || $all_campaigns[0] === NULL) {
http_response_code(500);
self::returnJson(["status" => 500, "message" => "Keine Kampagnen gefunden"]);
return;
}
// Filter campaigns by network owner
$filtered_campaigns = array_filter($all_campaigns, function($campaign) use ($netowner_id) {
$campaign_obj = new Preordercampaign($campaign->id);
return $campaign_obj->network->owner_id == $netowner_id;
});
if (empty($filtered_campaigns)) {
http_response_code(404);
self::returnJson(["status" => 404, "message" => "Keine Kampagnen für Netowner 209 gefunden"]);
return;
}
$export_data = [];
foreach ($filtered_campaigns as $campaign) {
$campaign_ids = [$campaign->id];
$gemeinde_ids = []; // Empty array as in original
$order_max_homes = $this->getTotalHomes($campaign_ids, $gemeinde_ids);
$efh_connection_types = ["single-dwelling", "business"];
$mph_connection_types = ["apartment-building", "apartment", "multi-dwelling"];
$countFunction = function($params, $statusFlag = null) use ($campaign_ids, $gemeinde_ids) {
$baseParams = ["preordercampaign_id" => $campaign_ids, "gemeinde_id" => $gemeinde_ids];
$params = array_merge($baseParams, $params);
return $statusFlag ?
PreorderModel::countStatusFlagsActive($params, $statusFlag) :
PreorderModel::countActive($params);
};
$baufortschritt_140 = $countFunction([">status_code" => "139", "<status_code" => "899"]);
$ont_installiert_300 = $countFunction([">status_code" => "299", "<status_code" => "899"]);
$provider_bestellt_500 = $countFunction([">status_code" => "499", "<status_code" => "899"]);
$campaign_data = [
'campaign_id' => $campaign->id,
'campaign_name' => $campaign->name ?? 'Unnamed Campaign',
'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"]),
'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),
'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"]),
'vorsorge_dokumentiert_351' => $countFunction(["status_code" => ["351","500"], "type" => "provision"]),
'provider_bestellt_500' => $provider_bestellt_500
];
$export_data[] = $campaign_data;
}
self::returnJson($export_data);
}
//Installationsfortschritt Provider
//
// jasmin wegkommt
// psc raaba 0316 67 33 00 zollweg michael (
// zollweg michael
private function getTimeline($type, $campaign_ids, $gemeinde_ids) { //TODO: fix gemeinde
$timeline = [];
$baseParams = ["preordercampaign_id" => $campaign_ids, "gemeinde_id" => $gemeinde_ids];
$timeline[] = $this->getTimelineData($type, $baseParams);
return $timeline;
}
private function getTimelineData($type, $params) {
$timeline = [];
$start = strtotime('-1 year');
$end = strtotime('today');
$interval = new DateInterval('P1W');
$daterange = new DatePeriod(date_create(date('Y-m-d', $start)), $interval, date_create(date('Y-m-d', $end)));
// Generate a unique cache key based on the function parameters
$cacheKey = md5($type . serialize($params));
$cacheFile = TEMP_DIR . "/Dashboard/" . $cacheKey . ".json";
// Ensure the cache directory exists
if (!is_dir(TEMP_DIR . "/Dashboard/")) {
mkdir(TEMP_DIR . "/Dashboard/", 0755, true);
}
// Load cached data if it exists
if (file_exists($cacheFile)) {
$cachedData = json_decode(file_get_contents($cacheFile), true);
$lastCachedDate = end($cachedData)['date'];
$lastCachedTimestamp = strtotime($lastCachedDate);
// Only process the last 5 weeks
$daterange = new DatePeriod(
date_create(date('Y-m-d', max($lastCachedTimestamp, strtotime('-5 weeks')))),
$interval,
date_create(date('Y-m-d', $end))
);
$timeline = $cachedData;
}
foreach ($daterange as $date) {
$date = $date->format('Y-m-d');
$value = 0;
switch ($type) {
case 'weekly-orders':
$params['add-where'] = " AND tt_preorder.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countActive($params);
break;
case 'weekly-leerrohr':
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 140);
break;
case 'weekly-ont-installed':
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 300);
break;
case 'weekly-inhouse-kabel-verlegt-efh':
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 244) + PreorderModel::countHistoryStatus($params, 245);
break;
case 'weekly-order-finished-energy-steiermark':
$params['partner_id'] = [209,5033, 3151, 7685, 7754, 7764];
$params["<status_code"] = 899;
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 500);
break;
case 'weekly-order-finished-magenta':
$params['partner_id'] = 4803;
$params["<status_code"] = 899;
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 500);
break;
case 'weekly-order-finished-salzburg-ag':
$params['partner_id'] = 5668;
$params["<status_code"] = 899;
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 500);
break;
case 'weekly-provider-bestellt':
$params['add-where'] = " AND ph.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
$value = PreorderModel::countHistoryStatus($params, 500);
break;
}
$newData = [
'date' => date(DATE_ATOM, strtotime($date)),
'value' => $value
];
// Update existing data or add new data
$existingIndex = array_search($newData['date'], array_column($timeline, 'date'));
if ($existingIndex !== false) {
$timeline[$existingIndex] = $newData;
} else {
$timeline[] = $newData;
}
}
// Sort the timeline by date
usort($timeline, function($a, $b) {
return strtotime($a['date']) - strtotime($b['date']);
});
// Cache the updated data
file_put_contents($cacheFile, json_encode($timeline));
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 = [];
if (!empty($str = $this->request->netzgebiet_id)) $baseFilter["netzgebiet_id"] = $this->request->netzgebiet_id;
$sum_counts = [
"sum" => ADBHausnummerModel::count(array_merge($baseFilter, []), false),
"sum_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, [])),
"op_state_planned" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Planned"]), false),
"op_state_planned_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Planned"])),
"op_state_passed" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Passed"]), false),
"op_state_passed_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Passed"])),
"op_state_connected" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Connected"]), false),
"op_state_connected_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Connected"])),
];
$efh_rimo_types = ["residential", "company", "2/3 familienhaus", "public"];
$efh_counts = [
"sum" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_type" => $efh_rimo_types]), false),
"sum_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_type" => $efh_rimo_types])),
"op_state_planned" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Planned", "rimo_type" => $efh_rimo_types]), false),
"op_state_planned_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Planned", "rimo_type" => $efh_rimo_types])),
"op_state_passed" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Passed", "rimo_type" => $efh_rimo_types]), false),
"op_state_passed_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Passed", "rimo_type" => $efh_rimo_types])),
"op_state_connected" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Connected", "rimo_type" => $efh_rimo_types]), false),
"op_state_connected_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Connected", "rimo_type" => $efh_rimo_types])),
];
$mph_rimo_types = ["multiple dwellings"];
$mph_counts = [
"sum" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_type" => $mph_rimo_types]), false),
"sum_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_type" => $mph_rimo_types])),
"op_state_planned" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Planned", "rimo_type" => $mph_rimo_types]), false),
"op_state_planned_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Planned", "rimo_type" => $mph_rimo_types])),
"op_state_passed" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Passed", "rimo_type" => $mph_rimo_types]), false),
"op_state_passed_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Passed", "rimo_type" => $mph_rimo_types])),
"op_state_connected" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_op_state" => "Connected", "rimo_type" => $mph_rimo_types]), false),
"op_state_connected_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_op_state" => "Connected", "rimo_type" => $mph_rimo_types])),
];
$other_types = [
"other_type_sum" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_type" => "other"]), false),
"other_type_sum_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_type" => "other"])),
"type_greenfield" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_type" => "greenfield"]), false),
"type_greenfield_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_type" => "greenfield"])),
"type_transformer_station" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_type" => "transformer station"]), false),
"type_transformer_station_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_type" => "transformer station"])),
"type_others" => ADBHausnummerModel::count(array_merge($baseFilter, ["rimo_type" => "other"]), false),
"type_others_homes" => ADBHausnummerModel::countHomes(array_merge($baseFilter, ["rimo_type" => "other"])),
];
self::returnJson([
"sum_counts" => $sum_counts,
"efh_counts" => $efh_counts,
"mph_counts" => $mph_counts,
"other_types" => $other_types
]);
}
}