BemsWebApplication.MonthMoney = function(params, viewInfo) { "use strict"; var initialized = false, shouldReload = false, dataSource; var CODE = $Code, FuelType = CODE.FuelType, FacilityType = CODE.FacilityType, FacilityCode = CODE.FacilityCode, F = CODE.Formula, TIT = CODE.TimeIntervalType; var visiblePopup = ko.observable(false); var factorToeDataSource = BemsWebApplication.db.createDataSource('BemsFactorToe'), chartPieDay = BWA.Chart.Instance('chartPieDayMM', 'dxPieChart'), chartBarDay = BWA.Chart.Instance('chartBarDayMM', 'dxChart'), chartPieMonth = BWA.Chart.Instance('chartPieMonthMM', 'dxPieChart'), chartBarMonth = BWA.Chart.Instance('chartBarMonthMM', 'dxChart'); var MonthMoneyGet = new BWA.Chart.PointHistory_MonthMoneyGet(BWA.UserInfo.SiteId()); var timeBoxForSearch = BWA.SearchView.createDateTimeBox($G.TimeTypesForMonthOnly); var factorToe = null; var Toe = $Code.FuelType.WATER + 1; var fuelTypesDataSource = BemsWebApplication.db.createDataSource('BemsFuelType'), fuelTypesForSearch = ko.observableArray(), timeTypesForSearch = ko.observableArray(), timeTypeIdForSearch = ko.observable(0), fuelTypeIdForSearch = ko.observable(), deferredForSearch = new $.Deferred(); var dataArray = ko.observableArray(), dataDayForPie = ko.observableArray(), dataMonthForPie = ko.observableArray(), dataDayForBar = ko.observableArray(), dataMonthForBar = ko.observableArray() ; var formulaGet = new BWA.Chart.FormulaGet(BWA.UserInfo.SiteId()); timeTypesForSearch($G.TimeTypes); var m = moment(); /*dataDayForBar([ { DateTime: new Date(m.year(), m.month(), 1), PowerToe: 40, GasToe: 40, WaterToe: 40 }, { DateTime: new Date(m.year(), m.month(), 2), Value: 50, PowerToe: 40, GasToe: 40, WaterToe: 40 }, { DateTime: new Date(m.year(), m.month(), 3), Value: 60, PowerToe: 40, GasToe: 40, WaterToe: 40 }, { DateTime: new Date(m.year(), m.month(), 4), Value: 80, PowerToe: 40, GasToe: 40, WaterToe: 40 }, { DateTime: new Date(m.year(), m.month(), 5), Value: 100, PowerToe: 40, GasToe: 40, WaterToe: 40 }, { DateTime: new Date(m.year(), m.month(), 6), Value: 40, PowerToe: 40, GasToe: 40, WaterToe: 40 }, { DateTime: new Date(m.year(), m.month(), 7), Value: 120, PowerToe: 40, GasToe: 40, WaterToe: 40 }, { DateTime: new Date(m.year(), m.month(), 31), Value: 120, PowerToe: 40, GasToe: 40, WaterToe: 40 } ]); dataMonthForBar(dataDayForBar()); dataDayForPie([ { FuelTypeName: '전기', Value: 320 }, { FuelTypeName: '가스', Value: 350 }, { FuelTypeName: '수도', Value: 360 }, ]); dataMonthForPie(dataDayForPie()); */ function handleMonthBarPointClick(clickedPoint) { var month = clickedPoint.originalArgument; //loadDailyEnergyUsage(month); //console.log(month); } var searchViewOptions = { searchViewItems: [ { id: 'TimeTypeId', defaultValue: 0, value: timeTypeIdForSearch, dataSource: timeTypesForSearch }, { id: 'FuelTypeId', ignoreValue: 0, defaultValue: 1, value: fuelTypeIdForSearch, dataSource: fuelTypesForSearch }, { id: 'DateForHour', type: 'dateRange', isOnlyDate: true }, { id: 'TimeForHour', type: 'dateRange', isOnlyDate: true }, ], promiseDataInSearchView: deferredForSearch.promise() }; function handleViewShown() { if (initialized === false) { var chartOption = BWA.ChartOptions.MonthMoney; chartPieDay = chartPieDay(chartOption.getPieChartDayOptions()); chartBarDay = chartBarDay(chartOption.getBarChartDayOptions()); chartPieMonth = chartPieMonth(chartOption.getPieChartMonthOptions()); chartBarMonth = chartBarMonth(chartOption.getBarChartMonthOptions(handleMonthBarPointClick)); //$('#chartPieDay').dxPieChart(chartOption.getPieChartDayOptions(dataDayForPie)); //$('#chartBarDay').dxChart(chartOption.getBarChartDayOptions(dataDayForBar)); //$('#chartPieMonth').dxPieChart(chartOption.getPieChartMonthOptions(dataMonthForPie)); //$('#chartBarMonth').dxChart(chartOption.getBarChartMonthOptions(dataMonthForBar)); initialized = true; } factorToeDataSource.load().done(function(dbFactorToe) { factorToe = BWA.Bems.refineFactorToeDataArray(dbFactorToe); //loadMonthlyEnergyUsage(); //loadDailyEnergyUsage(); handleSearchInSearchView(); }); } function handleSearchInSearchView() { visiblePopup(true); $("#large-indicator").dxLoadIndicator({ height: 60, width: 60 }); //fuelTypeId = fuelTypeIdForSearch(); loadMonthlyEnergyUsage(); //loadDailyEnergyUsage(); } function getChartDataFromDataSources(loadedDataSources, barDataArray, pieDataArray, timeFormat) { var pieFields = [$G('electricity'), $G('water'), $G('gas'), ]; var toeFields = ['Elec', 'Water', 'Gas']; //var pieFields = [$G('electricity'), $G('gas')]; //var toeFields = ['ElectricityToe', 'GasToe',]; _.each(loadedDataSources, function(array, i) { var field = toeFields[i]; var sum = 0; _.each(array[0], function(x) { var data = { DateTime: moment(x.DateTime).format(timeFormat), }; sum += data[field] = x.Value; barDataArray.push(data); }); pieDataArray.push({ FuelTypeName: pieFields[i], Value: sum }); }); //_.each(pieDataArray, function (x) { // if (x.Value == 0) x.Value = 1; //}); } var fuelTypes = [FuelType.ELECTRICITY, FuelType.GAS, FuelType.WATER]; function loadMonthlyEnergyUsage() { visiblePopup(true); $("#large-indicator").dxLoadIndicator({ height: 60, width: 60 }); var date = timeBoxForSearch.getDate(), timeType = timeBoxForSearch.type(); var promises = []; var param = { SiteId: BWA.UserInfo.SiteId(), TimeIntervalType: timeType, StartDate : date.startDate, EndDate : date.endDate, }; //promises.push(BWA.api.post('BemsMonitoringPointHistory/MonthMoney', null, param)); /* _.each(fuelTypes, function(x) { promises.push(energyUsageStatusGet.requestGetForUsgaeStatus(x, TIT.MONTH) (moment().startOf('year'), moment())); });*/ promises.push(MonthMoneyGet.apiGet(_.extend(param, {Type:10}))); promises.push(MonthMoneyGet.apiGet(_.extend(param, {Type: 11 }))); promises.push(MonthMoneyGet.apiGet(_.extend(param, {Type: 12 }))); $.when.apply(this, promises).done(function(e,w,g) { var Datas = []; /* _.each(e[0], function (x) { Datas.push(e[0]); }); _.each(w[0], function (x) { Datas.push(x); }); _.each(g[0], function (x) { Datas.push(x); });*/ Datas.push(e[0]); Datas.push(w[0]); Datas.push(g[0]); var barDataArray = []; var pieDataArray = []; getChartDataFromDataSources(Datas, barDataArray, pieDataArray, 'M'); //var eData = data[0]; //var wData = data[1]; //var gData = data[2]; //console.log(barDataArray); chartBarMonth().option('dataSource', barDataArray); chartPieMonth().option('dataSource', pieDataArray); visiblePopup(false); $('#st4').text(new Date().getFullYear() + '년 에너지 비용 현황'); $('#st5').text(new Date().getFullYear() + '년 에너지 비용 비율'); $('#st6').text(new Date().getFullYear() + '년 에너지 비용'); }).fail(function (error) { //alert(error); visiblePopup(false); }); } function handleViewHidden() { visiblePopup(false); $SearchView.visibleObservable(false); // hcLee 2015 03 23 //hideOverlay(); // var dataGrid = $('#gridContainer').dxDataGrid('instance'); // dataGrid.clearSelection(); } function handleViewShowing() { } function refreshList() { //dataSource.pageIndex(0); //dataSource.load(); } var viewModel = $.extend(BWA.CommonView.create(params, viewInfo, searchViewOptions, ko.observable(null), handleViewShown, null, handleSearchInSearchView), { refreshList: refreshList, viewHidden: handleViewHidden, // hcLee 2015 03 23 viewShowing: handleViewShowing, }); viewModel.timeBoxForSearch = timeBoxForSearch; viewModel.popupOptions = { width: 300, height: 100, contentTemplate: "info", showTitle: false, title: "메세지", visible: visiblePopup, dragEnabled: false, shading: false, closeOnOutsideClick: false }; return viewModel; };