(function () { 'use strict'; angular .module('hyperion') .controller('system.UtilitiesCostController.view', ['$rootScope', '$scope', '$sessionStorage', '$timeout', '$location', 'sysClock', 'timerange', 'modalDialog', 'authService', '$http', 'CONFIG', function($rootScope, $scope, $sessionStorage, $timeout, $location, sysClock, timerange, modalDialog, authService, $http, CONFIG) { $scope.isLoading = true; // show ajax loader $scope.ismanager = $sessionStorage.currentUser.ismanager; // set #content div margin if($sessionStorage.currentUser !== undefined) { if($sessionStorage.currentUser.smallMenu) { $("#content").css('margin-left', 50); } } var scopeIsActive = true; $scope.$on('$destroy', function() { scopeIsActive = false; }); $scope.chart_cost_title = 'LTM Utility Costs: Electric'; // default $scope.cost_bill_type = 'electric'; // default $scope.chart_cost_electric = true; // default $scope.chart_cost_water = false; $scope.chart_cost_ngas = false; $scope.bills = []; function load_data() { return new Promise((resolve, reject) => { $scope.cost_explorer_enabled = false; // check if ENABLED for project $scope.baseline_complete = true; // check if ALL LOCATION has coplete baseline data authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'costprojectconfig(project_id: "' + $rootScope.project + '"){' + 'project { ' + 'id ' + 'name ' + '} ' + 'enabled ' + 'baseline ' + '}' + '}' }; console.log(request); $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve console.log(response); $scope.cost_explorer_enabled = Boolean(response.data.data.costprojectconfig.enabled); $scope.baseline_complete = Boolean(response.data.data.costprojectconfig.baseline); if($scope.cost_explorer_enabled) { const bills = bills_load_data(); const averages = averages_load_data(); const chart = create_chart_objects(); Promise.all([ bills, averages, chart ]); resolve(null); } else { // if NOT ENABLED redirect to CONFIGURATION $location.path('/system/cost/configuration'); } },function(error) { // failure console.error(error); reject(); }); }); }); }; $timeout(function() { load_data().then(() => { console.log('------------------------ $timeout'); $scope.isLoading = false; }).catch(error => { console.warn(error); }); }, 100); $scope.showCostChart = function(type) { $scope.isLoading = true; type = type?type:'electric'; switch(type) { case 'electric': $scope.chart_cost_title = 'LTM Utility Costs: Electric'; $scope.cost_bill_type = 'electric'; $scope.chart_cost_electric = true; $scope.chart_cost_water = false; $scope.chart_cost_ngas = false; break; case 'water': $scope.chart_cost_title = 'LTM Utility Costs: Water'; $scope.cost_bill_type = 'water'; $scope.chart_cost_electric = false; $scope.chart_cost_water = true; $scope.chart_cost_ngas = false; break; case 'ngas': $scope.chart_cost_title = 'LTM Utility Costs: Natural Gas'; $scope.cost_bill_type = 'naturalgas'; $scope.chart_cost_electric = false; $scope.chart_cost_water = false; $scope.chart_cost_ngas = true; break; } const bills = bills_load_data(); const averages = averages_load_data(); const chart = chart_load_data(); Promise.all([ bills, averages, chart ]); $scope.isLoading = false; }; $scope.configuration = function() { $location.path('/system/cost/configuration'); }; $scope.newBill = function() { modalDialog.showDialog({ 'template':'modal.costbillnew.html', 'controller':'modalCostBillNew' }).then( function(bill){ console.log(bill); let mutation = null; switch(bill.type.id) { case 0: mutation = 'costbilladdelectric'; break; case 1: mutation = 'costbilladdwater'; break; case 2: mutation = 'costbilladdngas'; break; }; authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { ' + mutation + '( ' + 'location_id: "' + bill.location.id + '", ' + 'period_start: "' + moment(bill.beginDate).format('YYYY-MM-DD') + '", ' + 'period_end: "' + moment(bill.endDate).format('YYYY-MM-DD') + '", ' + 'amount: ' + bill.due + ', ' + 'used: ' + bill.used + ') { ' + 'type ' + '}' + '}' }; console.log(request); $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.isLoading = true; console.log(response); load_data().then(() => { $scope.isLoading = false; }); },function(error) { // failure console.error(error); } ); }); } ); }; //////////////////////////////////////////////////////////////////////////////////////////////////////////// // Bills const bills_load_data = () => { console.log('bills_load_data'); return new Promise((resolve) => { authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'costbillsproject(project_id:"' + $rootScope.project + '", bill_type:"' + $scope.cost_bill_type + '") { ' + 'type ' + 'period_start ' + 'period_end ' + 'amount ' + 'used ' + '}' + '}' }; console.log(request); $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve console.log(response); $scope.bills = response.data.data.costbillsproject; $scope.bills.forEach(bill => { bill.period = moment(bill.period_start).format('MMM Do, YYYY') + ' - ' + moment(bill.period_end).format('MMM Do, YYYY') // 'Aug 17 - Sep 18, 2024', }); resolve(); },function(error) { // failure console.error(error); }); }); }); }; //////////////////////////////////////////////////////////////////////////////////////////////////////////// // Averages const averages_load_data = () => { console.log('averages_load_data'); return new Promise((resolve) => { authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'costaggregatesproject(project_id: "' + $rootScope.project + '", bill_type: "' + $scope.cost_bill_type + '"){' + 'type ' + 'avgmocostbase ' + 'avgmocostcurr ' + 'avgmosavings ' + 'avgmosavingsprc ' + 'lastmodate ' + 'lastmocostbase ' + 'lastmocostcurr ' + 'lastmosavings ' + 'lastmosavingsprc ' + '}' + '}' }; console.log(request); $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve console.log(response); $scope.summary = response.data.data.costaggregatesproject; $scope.summary.lastmodate = moment($scope.summary.lastmodate).format('MMM YYYY') },function(error) { // failure console.error(error); }); }); }); }; //////////////////////////////////////////////////////////////////////////////////////////////////////////// // HIGHCHARTS CONFIG Highcharts.setOptions({ lang: { thousandsSep: ',' } }); /* Highcharts.dateFormats = { 'Z': () => { return moment.tz(sysClock.getTimeZone()).format('MMM YYYY'); } }; */ //////////////////////////////////////////////////////////////////////////////////////////////////////////// // CHART: COST $scope.loading_charts = true; const chart_clean = (chart) => { // remove data servies (empty graph) while(chart.series.length) { chart.series[0].remove(); } } const chart_load_data = () => { // prepare loading 1m aggregation data to graph console.log('chart_load_data'); return new Promise((resolve) => { chart_clean(chart_cost); // remove data servies (empty graph) chart_load_monthly_data().then(() => { resolve(null); }); }); } const chart_load_monthly_data = () => { return new Promise((resolve) => { let series_baseline = { name: "Baseline Cost", type: "column", marker: { symbol: 'circle' }, yAxis: 0, zIndex: 1, tooltip: { valuePrefix: '$' }, color: '#ced4da', data: [] }; let series_current = { name: "Current Cost", type: "column", marker: { symbol: 'circle' }, yAxis: 0, zIndex: 2, tooltip: { valuePrefix: '$' }, color: '#0197fe', data: [] }; authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'costprojectmonthly(project_id:"' + $rootScope.project + '", bill_type:"' + $scope.cost_bill_type + '") { ' + 'epoch ' + 'isbase ' + 'base ' + 'current ' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( (response) => { // resolve response.data.data.costprojectmonthly.forEach(month => { if(month.isbase) { // baseline month series_baseline.data.push({ x: parseInt(month.epoch, 10), //y: (month.base?month.base:0) y: month.base }); } else { series_baseline.data.push({ x: parseInt(month.epoch, 10), //y: (month.base?month.base:0) y: month.base }); series_current.data.push({ x: parseInt(month.epoch, 10), //y: month.current?month.current:0) y: month.current }); } }); chart_cost.addSeries(series_baseline); chart_cost.addSeries(series_current); resolve(null); },(error) => { // failure console.error(error); } ); }); }); }; //////////////////////////////////////////////////////////////////////////////////////////////////////////// // CHART OBJECT: Electric Cost let chart_cost = null; // chart object const create_chart_objects = () => { const obj_chart_monthly = new Promise((resolve, reject) => { new Highcharts.Chart({ chart: { renderTo: 'chart_monthly', height: 200, minHeight: 200, borderWidth: 0, marginTop: 0, marginRight: 0, marginLeft: 0, marginBottom: 40, spacingLeft: 0, spacingRight: 0, spacingBottom: 0, spacingTop: 0, backgroundColor: 'transparent', plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, }, time: { useUTC: true }, title: { text: null }, subtitle: { text: null }, exporting: { enabled: false }, credits: { enabled: false }, legend: { enabled: false }, accessibility: { announceNewData: { enabled: true } }, xAxis: { type: 'datetime', tickLength: 0, lineWidth: 0, dateTimeLabelFormats: { month: '%b %Y', // Display month and year on the x-axis labels year: '%Y' } }, yAxis: [{ // Power Cost Actial title: { text: 'Cost', style: { color: Highcharts.getOptions().colors[0] } }, gridLineWidth: 0, min: 0, opposite: false }], tooltip: { xDateFormat: '%b %Y', // Format tooltip to show month and year shared: true, outside: true, useHTML: true, backgroundColor: '#fff', borderWidth: 0 }, plotOptions: { column: { stacking: null, pointPadding: 0.05, borderWidth: 0.05, dataLabels: { enabled: false } }, series: { cursor: 'pointer', pointPadding: 0.05, borderWidth: 0.05, borderWidth: 0.05, minPointLength: 3, shadow: false, dataLabels: { enabled: false }, marker: { enabled: false }, } }, series: [] }, (chart) => { chart_cost = chart; resolve(); }); }); Promise.all([ obj_chart_monthly ]) .then(() => { console.log('create_chart_objects: Promise.all'); chart_load_data() .then(() => { $scope.loading_charts = false; }); }); }; }]) .controller('system.UtilitiesCostController.config', ['$rootScope', '$scope', '$sessionStorage', '$timeout', '$location', 'authService', '$http', 'CONFIG', function($rootScope, $scope, $sessionStorage, $timeout, $location, authService, $http, CONFIG) { $scope.isLoading = true; // show ajax loader $scope.ismanager = $sessionStorage.currentUser.ismanager; // set #content div margin if($sessionStorage.currentUser !== undefined) { if($sessionStorage.currentUser.smallMenu) { $("#content").css('margin-left', 50); } } var scopeIsActive = true; $scope.$on('$destroy', function() { scopeIsActive = false; }); $scope.cost_explorer_enabled = false; // check if ENABLED for project $scope.locations = []; function load_data() { return new Promise((resolve, reject) => { authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'costprojectconfig(project_id: "' + $rootScope.project + '"){' + 'project { ' + 'id ' + 'name ' + '} ' + 'enabled ' + 'locations { ' + 'location { ' + 'id ' + 'name ' + '} ' + 'enabled ' + 'electric { ' + 'enabled ' + 'baseline_start ' + '} ' + 'water { ' + 'enabled ' + 'baseline_start ' + '} ' + 'ngas { ' + 'enabled ' + 'baseline_start ' + '} ' + '} ' + '}' + '}' }; console.log(request); $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve console.log(response); $scope.cost_explorer_enabled = Boolean(response.data.data.costprojectconfig.enabled); if($scope.cost_explorer_enabled) { $scope.locations = response.data.data.costprojectconfig.locations; let default_baseline_start = moment().subtract(1, 'year').startOf('year'); $scope.locations.forEach(location => { location.electric.baseline_start = location.electric.baseline_start?location.electric.baseline_start:default_baseline_start; location.water.baseline_start = location.water.baseline_start?location.water.baseline_start:default_baseline_start; location.ngas.baseline_start = location.ngas.baseline_start?location.ngas.baseline_start:default_baseline_start; }); } resolve(); },function(error) { // failure console.error(error); reject(); }); }); resolve(); }); }; $timeout(function() { load_data().then(() => { $scope.isLoading = false; }).catch(error => { console.warn(error); }); }, 100); $scope.toggleCostProject = function() { $scope.isLoading = true; authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { costprojectconfig(project_id: "' + $rootScope.project + '", enabled: ' + !$scope.cost_explorer_enabled + '){enabled}}'}; console.log(request); $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.cost_explorer_enabled = response.data.data.costprojectconfig.enabled; if($scope.cost_explorer_enabled) { load_data().then(() => { $scope.isLoading = false; }).catch(error => { console.warn(error); }); } else { $scope.isLoading = false; } },function(error) { // failure console.error(error); } ); }); }; $scope.toggleCostLocation = function(location_id) { $scope.isLoading = true; let location = $scope.locations.find(item => item.location && item.location.id === location_id); authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { costlocationconfig(location_id: "' + location_id + '", enabled: ' + location.enabled + '){enabled}}'}; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve location.enabled = response.data.data.costlocationconfig.enabled; $scope.isLoading = false; },function(error) { // failure console.error(error); } ); }); }; $scope.toggleLocationElectric = function(location_id) { $scope.isLoading = true; let location = $scope.locations.find(item => item.location && item.location.id === location_id); console.log(location); authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { costlocationelectric(location_id: "' + location_id + '", enabled: ' + location.electric.enabled + ', baseline_start: "' + moment(location.electric.baseline_start).format('YYYY-MM-DD') + '"){enabled, baseline_start}}'}; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve console.log(response); location.electric.enabled = response.data.data.costlocationelectric.enabled; location.electric.baseline_start = response.data.data.costlocationelectric.baseline_start; $scope.isLoading = false; },function(error) { // failure console.error(error); } ); }); }; $scope.toggleLocationWater = function(location_id) { $scope.isLoading = true; let location = $scope.locations.find(item => item.location && item.location.id === location_id); console.log(location); authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { costlocationwater(location_id: "' + location_id + '", enabled: ' + location.water.enabled + ', baseline_start: "' + moment(location.water.baseline_start).format('YYYY-MM-DD') + '"){enabled, baseline_start}}'}; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve console.log(response); location.water.enabled = response.data.data.costlocationwater.enabled; location.water.baseline_start = response.data.data.costlocationwater.baseline_start; $scope.isLoading = false; },function(error) { // failure console.error(error); } ); }); }; $scope.toggleLocationNGas = function(location_id) { $scope.isLoading = true; let location = $scope.locations.find(item => item.location && item.location.id === location_id); console.log(location); authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { costlocationngas(location_id: "' + location_id + '", enabled: ' + location.ngas.enabled + ', baseline_start: "' + moment(location.ngas.baseline_start).format('YYYY-MM-DD') + '"){enabled, baseline_start}}'}; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve console.log(response); location.ngas.enabled = response.data.data.costlocationngas.enabled; location.ngas.baseline_start = response.data.data.costlocationngas.baseline_start; $scope.isLoading = false; },function(error) { // failure console.error(error); } ); }); }; }]); })();