255 lines
11 KiB
PHP
255 lines
11 KiB
PHP
<?php
|
|
|
|
//TODO: interessen
|
|
class DashboardNewController extends mfBaseController {
|
|
private User $me;
|
|
|
|
protected function init(): void {
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->layout()->set("me", $me);
|
|
$this->me = $me;
|
|
}
|
|
|
|
protected function indexAction() {
|
|
$this->layout()->set('additionalJS', ["plugins/chart.js/chart.4.4.6.js", "plugins/chart.js/chartjs-adapter-moment.min.js"]);
|
|
Helper::renderVue($this, "DashboardNew", $this->mod, []);
|
|
}
|
|
|
|
|
|
protected function getNetOwnerFilterOptionsAction() {
|
|
if (!$this->me->is("Admin")) return; // TODO: enable for RML and Energie Steiermark
|
|
|
|
$allPreorderCampaigns = PreordercampaignModel::getAll();
|
|
$netowners = [];
|
|
|
|
foreach ($allPreorderCampaigns as $campaign) {
|
|
$network = new Network($campaign->network_id);
|
|
$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 getCampaignFilterOptionsAction() {
|
|
if (!$this->me->is("Admin")) return; // TODO: enable for RML and Energie Steiermark
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
$netowner_ids = isset($post['netOwners']) ? [$post['netOwners']] : [];
|
|
$campaigns = [];
|
|
|
|
$all_campaigns = PreordercampaignModel::getAll();
|
|
|
|
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() {
|
|
if (!$this->me->is("Admin")) return; // TODO: enable for RML and Energie Steiermark
|
|
$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 = PreordercampaignModel::getAll();
|
|
|
|
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 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)) {
|
|
$campaign_ids = empty($campaign_ids) ?
|
|
array_map(fn($campaign) => $campaign->id, PreordercampaignModel::getAll()) :
|
|
$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);
|
|
});
|
|
}
|
|
|
|
$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];
|
|
if (!empty($gemeinde_ids)) {
|
|
// as the count only supports 1 gemeinde id as filter we need to use a foreach to get the count for multiple gemeinde ids
|
|
|
|
$count = 0;
|
|
foreach ($gemeinde_ids as $gemeinde_id) {
|
|
$baseParams["gemeinde_id"] = $gemeinde_id;
|
|
$count += $statusFlag ?
|
|
PreorderModel::countStatusFlagsActive($baseParams, $statusFlag) :
|
|
PreorderModel::countActive($baseParams);
|
|
}
|
|
return $count;
|
|
} else {
|
|
$params = array_merge($baseParams, $params);
|
|
return $statusFlag ?
|
|
PreorderModel::countStatusFlagsActive($params, $statusFlag) :
|
|
PreorderModel::countActive($params);
|
|
}
|
|
};
|
|
|
|
self::returnJson([
|
|
'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' => $countFunction([">status_code" => "139", "<status_code" => "899"]),
|
|
'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' => $countFunction([">status_code" => "299", "<status_code" => "899"]),
|
|
'vollanschluss_dokumentiert_350' => $countFunction(["status_code" => ["350","500"], "type" => "order"]),
|
|
'vorsorge_dokumentiert_351' => $countFunction(["status_code" => ["351","500"], "type" => "provision"]),
|
|
'provider_bestellt_500' => $countFunction(["status_code" => "500"]),
|
|
'timeline' => $this->getTimeline('weekly-orders', $campaign_ids, $gemeinde_ids)
|
|
]);
|
|
}
|
|
|
|
private function getTimeline($type, $campaign_ids, $gemeinde_ids) {
|
|
$timeline = [];
|
|
$baseParams = ["preordercampaign_id" => $campaign_ids];
|
|
if (!empty($gemeinde_ids)) {
|
|
foreach ($gemeinde_ids as $gemeinde_id) {
|
|
$baseParams["gemeinde_id"] = $gemeinde_id;
|
|
$timeline[] = $this->getTimelineData($type, $baseParams);
|
|
}
|
|
} else {
|
|
$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)));
|
|
|
|
foreach ($daterange as $date) {
|
|
$date = $date->format('Y-m-d');
|
|
$params['add-where'] = " AND tt_preorder.`create` >= UNIX_TIMESTAMP('" . $date . " 00:00:00') AND tt_preorder.`create` <= UNIX_TIMESTAMP('" . $date . " 23:59:59')";
|
|
$timeline[] = [
|
|
'date' => date(DATE_ATOM, strtotime($date)),
|
|
'value' => PreorderModel::countActive($params)
|
|
];
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
} |