BemsWebApplication.BudgetExecute = function (params, viewInfo) { "use strict"; var initialized = false; var eq = BWA.DataUtil.constructEqualFilter, and = BWA.DataUtil.andFilter; var isNewBudget = ko.observable(false); var currentYear = (new Date()).getFullYear(); var currentMonth = (new Date()).getMonth(); var modifiableBudgetPlanDataGridOptions = undefined; var budgetsInView = ko.observableArray(), budgets = ko.observableArray() ; var selectedBudgetYearName = ko.observable(); var budgetGridView = undefined; var disabledButtons = [ko.observable(), ko.observable(), ko.observable(), ko.observable()]; var budgetDataSource = BemsWebApplication.db.createDataSource('FmsBudget', true, false, true); var budgetCodeDataSourceForTree = BWA.DataUtil.createDataSource({ dataSourceOptions: { paginate: false, store: BemsWebApplication.odata.FmsBudgetCodeClass, select: ['SiteId', 'BudgetSeq', 'BudgetClassId', 'RootBudgetClassId', 'ParentBudgetClassId', 'RootBudgetName', 'ParentBudgetName', 'Depth', 'Name', 'Abbreviation', 'IsUse', 'YearlyBudget', 'MonthlyBudget'], } }); var newBudgetCodeDataSource = BWA.DataUtil.createDataSource({ dataSourceOptions: { paginate: false, store: BemsWebApplication.odata.FmsBudgetCodeClass, select: [ 'SiteId', 'BudgetSeq', 'BudgetClassId', 'RootBudgetClassId', 'ParentBudgetClassId', 'RootBudgetName', 'ParentBudgetName', 'Depth', 'Name', 'Abbreviation', 'IsUse', 'YearlyBudget', 'MonthlyBudget'], } }); //var existingBudgetCodeDataSource = BWA.DataUtil.createDataSource({ // dataSourceOptions: { // paginate: false, // select: ['SiteId', 'Year', 'FmsBudgetDetail/BudgetSeq', 'BudgetClassId', 'ParentBudgetClassId', 'FmsBudgetDetail/Depth', // 'FmsBudgetDetail/Name', 'FmsBudgetDetail/Abbreviation', 'FmsBudgetDetail/IsUse', // 'YearlyBudget', 'MonthlyBudget', 'MonthlyExecution', 'Month', 'YearlyExecution' // ], // //, 'MonthlyExecution' , 'YearlyExecution' // expand: ['FmsBudgetDetail'], // extendOptions: { // forceOriginalField: true // }, // } //}, 'FmsBudgetDetailExecution'); var existingBudgetCodeDataSource = BWA.DataUtil.createDataSource({ dataSourceOptions: { paginate: false, store: BemsWebApplication.odata.FmsBudgetDetailExecution2, select: [ 'SiteId', 'Year', 'BudgetClassId', 'BudgetSeq', 'RootBudgetClassId', 'ParentBudgetClassId', 'RootBudgetName', 'ParentBudgetName', 'Month', 'Depth', 'Name', 'YearlyBudget', 'MonthlyBudget', 'YearlyExecution', 'MonthlyExecution'], } }); modifiableBudgetPlanDataGridOptions = utils.datagrid.defaultOptions({ pager: { showPageSizeSelector: false, allowedPageSizes: [] }, //groupPanel: { // visible: true //}, dataSource: budgetsInView, columns: [ { dataField: 'RootBudgetName', caption: '대분류', alignment: 'left', groupIndex: 0 }, { dataField: 'ParentBudgetName', caption: '중분류', alignment: 'left', groupIndex: 1 }, { dataField: 'Name', caption: '명칭', alignment: 'left' }, { dataField: 'MonthlyBudget', caption: '월간예산', alignment: 'center' , cellTemplate: utils.datagrid.getNumberBoxDataGridTemplateFunc({ min: 0, max: Number.MAX_SAFE_INTEGER, dataField: 'MonthlyBudget' , convertValueWhenValueChanged: function (data, value) { var yearlyValue = value * 12; data.YearlyBudget(Number(yearlyValue.toFixed(1))); reCalculateSummaryValue(); return Math.max(0, value); } }) }, { dataField: 'MonthlyExecution', caption: '월간실적', alignment: 'center' , cellTemplate: utils.datagrid.getNumberBoxDataGridTemplateFunc({ min: 0, max: Number.MAX_SAFE_INTEGER, dataField: 'MonthlyExecution' , convertValueWhenValueChanged: function (data, value) { var yearlyValue = value * 12; data.YearlyExecution(Number(yearlyValue.toFixed(1))); reCalculateSummaryValue(); return Math.max(0, value); } }) }, ], sortByGroupSummaryInfo: [{ summaryItem: 'count' }], summary: { groupItems: [ { column: 'MonthlyBudget', summaryType: 'sum', valueFormat: "currency", showInGroupFooter: false, alignByColumn: true }, { column: 'MonthlyExecution', summaryType: 'sum', valueFormat: "currency", showInGroupFooter: false, alignByColumn: true } ] }, cellClick: function (e) { if (e.rowType == 'data' && e.data['Depth']() === 2) { if (e.column.dataField === 'MonthlyExecution' || e.column.dataField === 'MonthlyBudget') { utils.datagrid.cellClickEventForDataGridTemplate(e, e.column.dataField); } } }, rowPrepared: function (rowElement, rowInfo) { var data = rowInfo.data; if (rowInfo.rowType == 'data') { if (data['Depth']() === 0) { var color = 'rgba(100,100,100,0.2)'; rowElement.css('background-color', color); } else if (data['Depth']() === 1) { var color = 'rgba(150,150,150,0.2)'; rowElement.css('background-color', color); } else if (data['Depth']() === 2) { } } }, cellPrepared: function (cellElement, cellInfo) { switch (cellInfo.column.dataField) { case 'Name': if (cellInfo.rowType == 'data' && cellInfo.data['Depth']() === 1) { cellElement.css('padding-left', '30px'); } else if (cellInfo.rowType == 'data' && cellInfo.data['Depth']() === 2) { cellElement.css('padding-left', '60px'); } break; } }, contentReadyAction: function (e) { budgetGridView = e.component; }, }); var viewModel, popup = null, virtualFacilityPopup = null, //popupVisible = ko.observable(false), //rootTreeData = ko.observable(null), selectedTreeData = ko.observable(null), selectedTreeItem = ko.observable(null); var commandButtonOptions = [ { icon: 'add', id: 'create3', title: '불러오기', action: getBudgetYear }, { icon: 'edit', id: 'create2', title: '저장', action: saveBudget }, { id: 'create4', title: '초기화', action: resetBudget }, ]; function handleClickTreeItem(element, data) { selectedTreeItem(data); var filter; var depth = data.depth; var parentData = data.parentData; data = data.data; selectedTreeData(data); if (depth == 1) { filter = [ ['SiteId', '=', BWA.UserInfo.SiteId()], 'and', ['FmsBudgetDetail/Depth', '=', 0] ]; } else if (depth === 2) { // FirstCodeClass 에 해당하는 모든 항목 filter = [ ['SiteId', '=', BWA.UserInfo.SiteId()], 'and', ['FmsBudgetDetail/BudgetSeq', 'startswith', data.BudgetSeq()] ]; } else if (depth === 3) { // SecondCodeClass 에 해당하는 모든 항목 filter = [ ['SiteId', '=', BWA.UserInfo.SiteId()], 'and', [ ['BudgetClassId', '=', data.ParentBudgetClassId()], 'or', ['BudgetClassId', '=', data.BudgetClassId()], 'or', ['ParentBudgetClassId', '=', data.BudgetClassId()] ] ]; } budgetGridView.filter(filter); } function handleViewShown() { //setTreeview(); //currentYear = selectedBudgets.Year(); //currentMonth = selectedBudgets.Month(); //var a = [BWA.UserInfo.SiteId(), 2015, 12]; if (initialized == false) { selectedBudgetYearName('대상 항목'); viewModel.popup.onSelectedBudgets(null); } initialized = true; } viewModel = BWA.CommonView.create(params, viewInfo, null, null, handleViewShown); viewModel.modifiableBudgetPlanDataGridOptions = modifiableBudgetPlanDataGridOptions; viewModel.commandButtonOptions = commandButtonOptions; viewModel.budgets = budgets; viewModel.selectedBudgetYearName = selectedBudgetYearName; popup = viewModel.popup = BWA.Popup.BudgetExecute.create(viewModel, { onSelectedBudgets: function (selectedBudgets) { var now = new Date(); if (selectedBudgets == null) existingBudgetCodeDataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), and, eq('Year', now.getFullYear() ), and, eq('Month', now.getMonth() + 1), ]); else existingBudgetCodeDataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), and, eq('Year', selectedBudgets.Year()), and, eq('Month', selectedBudgets.Month()), ]); $.when(existingBudgetCodeDataSource.load()).done(function (budgetCodeResult) { budgetsInView(budgetCodeResult); }); isNewBudget(false); if (selectedBudgets == null) { currentYear = now.getFullYear(); currentMonth = now.getMonth() + 1; } else{ currentYear = selectedBudgets.Year(); currentMonth = selectedBudgets.Month(); } selectedBudgetYearName(currentYear + ' 년도 ' + currentMonth + ' 월 대상 항목'); } }); function reCalculateSummaryValue() { _.each(budgetsInView(), function (x) { if (x['Depth']() === 0) { var yearlySum = 0; var monthlySum = 0; var yearlySumBudget = 0; var monthlySumBudget = 0; _.each(budgetsInView(), function (y) { if (y['Depth']() === 2 && y['BudgetSeq']().indexOf(x['BudgetSeq']()) == 0) { yearlySum += Number(y.YearlyExecution()); monthlySum += Number(y.MonthlyExecution()); yearlySumBudget += Number(y.YearlyBudget()); monthlySumBudget += Number(y.MonthlyBudget()); } }); x.YearlyExecution(yearlySum); x.MonthlyExecution(monthlySum); x.YearlyBudget(yearlySumBudget); x.MonthlyBudget(monthlySumBudget); } else if (x['Depth']() === 1) { var yearlySum = 0; var monthlySum = 0; _.each(budgetsInView(), function (y) { if (y['Depth']() === 2 && y['BudgetSeq']().indexOf(x['BudgetSeq']()) == 0) { yearlySum += Number(y.YearlyExecution()); monthlySum += Number(y.MonthlyExecution()); yearlySumBudget += Number(y.YearlyBudget()); monthlySumBudget += Number(y.MonthlyBudget()); } }); x.YearlyExecution(yearlySum); x.MonthlyExecution(monthlySum); x.YearlyBudget(yearlySumBudget); x.MonthlyBudget(monthlySumBudget); } }); budgetGridView.refresh(); } function newBudget() { isNewBudget(true); $.when( newBudgetCodeDataSource.load(), budgetDataSource.load() ).done(function (budgetCodeResult, budgetResult) { budgetsInView(budgetCodeResult[0]); budgets(budgetResult[0]); // currentYear 설정 (최신년도 설정 으로...) if (_.isEmpty(budgets())) { currentYear = (new Date()).getFullYear(); } else { var lastBudget = _.last(budgets()); currentYear = lastBudget.Year() + 1; } selectedBudgetYearName(currentYear + ' 년도 대상 항목'); }); } function deleteBudget() { //DevExpress.ui.dialog.confirm($G('deleteConfirmMsg'), "삭제").then(function (result) { // if (result) { // var array = budgetsInView(); // if (_.isEmpty(array)) { // utils.toast.show('저장할 예산편성이 없습니다.', 'error'); // return; // } // var siteId = BWA.UserInfo.SiteId(); // var year = currentYear; // var parameters = { // SiteId: siteId, // Year: year // }; // BWA.api.post('FmsBudgetDetailExecution/Delete', null, parameters).done(function () { // utils.toast.show($G('successDatabaseDeleteMsg')); // resetBudget(); // }); // } //}); } function getBudgetYear() { popup.show(); } function saveBudget() { var array = budgetsInView(); if (_.isEmpty(array)) { utils.toast.show('저장할 예산편성이 없습니다.', 'error'); return; } var siteId = BWA.UserInfo.SiteId(); var year = currentYear; var month = currentMonth; var postData; var parameters = { SiteId: siteId, Year: year, Month: month }; postData = _.map(array, function (f) { return { SiteId: siteId, Year: year, BudgetClassId: f.BudgetClassId(), RootBudgetClassId: f.RootBudgetClassId(), ParentBudgetClassId: f.ParentBudgetClassId(), Month: month, YearlyBudget: f.YearlyBudget(), MonthlyBudget: f.MonthlyBudget(), YearlyExecution: f.YearlyExecution(), MonthlyExecution: f.MonthlyExecution(), }; }); BWA.api.post('FmsBudgetDetailExecution/Save', postData, parameters).done(function () { utils.toast.show($G('successDatabaseProcessMsg')); }); } function resetBudget() { isNewBudget(false); budgetsInView([]); budgets([]); budgetGridView.refresh(); selectedBudgetYearName('대상 항목'); //clearTreeview(); } function clearTreeview() { $('#facilityTreeView').cwTreeView({ width: '30%', height: 'auto', onClickTreeItem: handleClickTreeItem, delegateDataSource: getDataSourceDelegateForTreeView(budgetCodeDataSourceForTree) }); } function setTreeview() { $('#facilityTreeView').cwTreeView({ width: '30%', height: 'auto', onClickTreeItem: handleClickTreeItem, delegateDataSource: getDataSourceDelegateForTreeView(budgetCodeDataSourceForTree) }); } function getDataSourceDelegateForTreeView(dataSource) { return function (data, alterObj) { var id = data.id; var depth = data.depth; var promise = null; switch (depth) { case 0: var typeDeferred = new $.Deferred(); promise = typeDeferred.promise(); alterObj.IsFunction = false; typeDeferred.resolve([{ Id: 0, Name: '관리항목' }]); //rootTreeData(data); break; case 1: dataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), 'and', eq('Depth', 0) ]); promise = dataSource.load(); break; case 2: dataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), 'and', eq('ParentBudgetClassId', data.data.BudgetClassId()), 'and', eq('Depth', 1) ]); promise = dataSource.load(); break; } return promise; }; } //viewModel.popup.onSelectedBudgets(null); return viewModel; };