BemsWebApplication.BudgetPlan2 = function (params, viewInfo) { "use strict"; var eq = BWA.DataUtil.constructEqualFilter, and = BWA.DataUtil.andFilter; var initialized = false; var isNewBudget = ko.observable(false); var currentYear = (new Date()).getFullYear(); 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, store: BemsWebApplication.odata.FmsBudgetDetail, select: ['SiteId', 'Year', 'BudgetSeq', 'BudgetClassId', 'RootBudgetClassId', 'ParentBudgetClassId', 'RootBudgetName', 'ParentBudgetName', 'Depth', 'Name', 'Abbreviation', 'IsUse', 'YearlyBudget', 'MonthlyBudget'], //, 'MonthlyExecution' } }); // }, 'FmsBudgetDetail'); modifiableBudgetPlanDataGridOptions = utils.datagrid.defaultOptions({ pager: { showPageSizeSelector: false, allowedPageSizes: [] }, //groupPanel: { // visible: true //}, dataSource: budgetsInView, columns: [ // { dataField: 'BudgetSeq', caption: 'Seq', width: '15%', alignment: 'left'}, , //sortOrder: 'asc' //{ dataField: 'BudgetClassId', caption: 'BudgetClassId', width: '10%', alignment: 'center' }, // { dataField: 'RootBudgetClassId', caption: 'RootBudgetClassId', width: '10%', alignment: 'center' }, { dataField: 'RootBudgetName', caption: '대분류', alignment: 'left', groupIndex: 0 }, { dataField: 'ParentBudgetName', caption: '중분류', alignment: 'left', groupIndex: 1 }, // { dataField: 'ParentBudgetClassId', caption: 'ParentBudgetClassId', width: '10%', alignment: 'center'}, { dataField: 'Name', caption: '명칭', alignment: 'left' }, { dataField: 'YearlyBudget', caption: '연간예산', alignment: 'center' , customizeText: function (cellInfo) { return 0; } , cellTemplate: utils.datagrid.getNumberBoxDataGridTemplateFunc({ min: 0, max: Number.MAX_SAFE_INTEGER, dataField: 'YearlyBudget' , convertValueWhenValueChanged: function (data, value) { var monthlyValue = value / 12; data.MonthlyBudget(Number(monthlyValue.toFixed(1))); reCalculateSummaryValue(); return Math.max(0, value); } }) }, { 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); } }) }, ], sortByGroupSummaryInfo: [{ summaryItem: 'count' }], summary: { groupItems: [ { column: 'YearlyBudget', summaryType: 'sum', valueFormat: "currency", showInGroupFooter: false, alignByColumn: true }, { column: 'MonthlyBudget', summaryType: 'sum', valueFormat: "currency", showInGroupFooter: false, alignByColumn: true } ] }, cellClick: function (e) { if (e.rowType == 'data' && e.data.Depth() === 2) { if (e.column.dataField === 'YearlyBudget' || e.column.dataField === 'MonthlyBudget') { utils.datagrid.cellClickEventForDataGridTemplate(e, e.column.dataField); } } }, rowPrepared: function (rowElement, rowInfo) { if (rowInfo.rowType == 'data') { var data = rowInfo.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) { if (cellInfo.rowType == 'data') { switch (cellInfo.column.dataField) { case 'Name': //if (cellInfo.data.Depth() === 1) { // cellElement.css('padding-left', '30px'); //} //else if (cellInfo.data.Depth() === 2) { cellElement.css('padding-left', '40px'); } break; } } }, contentReadyAction: function (e) { budgetGridView = e.component; }, }); var viewModel, popup = null, virtualFacilityPopup = null, selectedTreeData = ko.observable(null), selectedTreeItem = ko.observable(null); var commandButtonOptions = [{ icon: 'add', id: 'create1', title: '신규', //disabled: disabledButtons[BID_INSERT_VIRTUAL_FACILITY], action: newBudget }, { icon: 'add', id: 'create3', title: '불러오기', //disabled: disabledButtons[BID_GENERATE_POINT], //action: generatePoints action: getBudgetYear }, { icon: 'edit', id: 'create2', title: '저장', //disabled: disabledButtons[BID_EDIT_VIRTUAL_FACILITY], action: saveBudget }, { id: 'create4', title: '초기화', //disabled: disabledButtons[BID_GENERATE_POINT], //action: generatePoints action: resetBudget }, { icon: 'remove', id: 'delete', title: '삭제', //disabled: disabledButtons[BID_DELETE_POINT], //action: handleRemoveSelectedPoints action: deleteBudget }]; 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', ['Depth', '=', 0] ]; } else if (depth === 2) { // FirstCodeClass 에 해당하는 모든 항목 filter = [ ['SiteId', '=', BWA.UserInfo.SiteId()], 'and', ['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(); 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.BudgetPlan2.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('Depth', 2), ]); else existingBudgetCodeDataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), and, eq('Year', selectedBudgets.Year()), and, eq('Depth', 2), ]); $.when(existingBudgetCodeDataSource.load()).done(function (budgetCodeResult) { budgetsInView(budgetCodeResult); }); isNewBudget(false); if (selectedBudgets == null) currentYear = now.getFullYear(); else currentYear = selectedBudgets.Year(); selectedBudgetYearName(currentYear + ' 년도 대상 항목'); } }); function reCalculateSummaryValue() { _.each(budgetsInView(), function (x) { if (x.Depth() === 0) { var yearlySum = 0; var monthlySum = 0; _.each(budgetsInView(), function (y) { if (y.Depth() === 2 && y.BudgetSeq().indexOf(x.BudgetSeq()) == 0) { yearlySum += Number(y.YearlyBudget()); monthlySum += Number(y.MonthlyBudget()); } }); x.YearlyBudget(yearlySum); x.MonthlyBudget(monthlySum); } 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.YearlyBudget()); monthlySum += Number(y.MonthlyBudget()); } }); x.YearlyBudget(yearlySum); x.MonthlyBudget(monthlySum); } }); budgetGridView.refresh(); } function newBudget() { isNewBudget(true); newBudgetCodeDataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), and, eq('Depth', 2), ]); $.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('FmsBudgetDetail/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 postData; var parameters = { SiteId: siteId, Year: year }; postData = _.map(array, function (f) { return { SiteId: siteId, Year: year, BudgetClassId: f.BudgetClassId(), BudgetSeq: f.BudgetSeq(), RootBudgetClassId: f.RootBudgetClassId(), ParentBudgetClassId: f.ParentBudgetClassId(), Depth: f.Depth(), Name: f.Name(), Abbreviation: f.Abbreviation(), IsUse: f.IsUse(), YearlyBudget: f.YearlyBudget(), MonthlyBudget: f.MonthlyBudget(), //MonthlyExecution: 0, //YearlyExecution: 0, }; }); if (isNewBudget()) { BWA.api.post('FmsBudgetDetail/Save', postData, parameters).done(function () { utils.toast.show($G('successDatabaseProcessMsg')); isNewBudget(false); }); } else { BWA.api.post('FmsBudgetDetail/Update', 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; }; } return viewModel; };