BemsWebApplication.setprice2 = function (params, viewInfo) { //탭 표시 요금제 Array 데이터 초기화 var tabsData = [ { id: "0", text: "test1" }, { id: "1", text: "test2" }, { id: "2", text: "test3" } ]; //권한설정 var hasnotModificationPermission = ko.observable(true); var old = false; //요금제 선택 SelectBox Array데이터 초기화 var priceTypeDataSource = [{ FuelTypeId: 1, PriceTypeId: 1, PriceTypeIdDesc: "" }]; //요금제 항목 데이터 소스 변수 선언 var priceCodeDataSource; //요금제 업데이트 시 변경전 조건과 변경후 변경값이 다르기 때문에 변경전 요금제를 저장할 용도로 변수 선언 var beforeSelectPriceTypeId; //에너지원 선택 전역 변수 초기화 var energySelectButton = "power"; var selectFacilityTypeId; var selectFacilityCode; var selectFormulaId; var init = false; var endpointSelector = ""; if (BemsWebApplication.config.mode == "production") { endpointSelector = new DevExpress.EndpointSelector(BemsWebApplication.config.endpoints).config.db.production; } else { endpointSelector = new DevExpress.EndpointSelector(BemsWebApplication.config.endpoints).config.db.local; } function getSitePrice(SiteID, FuelTypeId, PriceCode) { //현재 요금제 선택된 항목 var dataSource = new DevExpress.data.DataSource({ store: new DevExpress.data.ODataStore({ url: endpointSelector + "/BemsSitePrice" }), expand: 'BemsPriceType', requireTotalCount: true, pageSize: 100 }); dataSource.filter([ ["SiteID", "=", SiteID], "and", ["FuelTypeId", "=", FuelTypeId], "and", ["BemsPriceType.PriceTypeIdDesc", "=", $("#tabOptions").dxTabs("instance").option('selectedItem').text] ]); dataSource.load() .done(function (result) { $("#priceSelect").dxSelectBox("instance").option('value', result[0].BemsPriceType.PriceTypeId); beforeSelectPriceTypeId = result[0].BemsPriceType.PriceTypeId; }) .fail(function (error) { alert(error); }); } function getPriceType(FuelTypeId) {//요금제 종류 var dataSource = new DevExpress.data.DataSource({ store: { type: "odata", url: endpointSelector + "/BemsPriceType" }, //map: function (itemData) { // return { // PriceTypeId: itemData.PriceTypeId, // PriceTypeIdDesc: itemData.PriceTypeIdDesc, // FuelTypeId: itemData.FuelTypeId // } //}, requireTotalCount: true, pageSize: 100 }); dataSource.filter(["FuelTypeId", "=", FuelTypeId]); dataSource.sort({ getter: "PriceTypeId", asc: true }); dataSource.load() .done(function (result) { priceTypeDataSource = result; $("#priceSelect").dxSelectBox("instance").option('dataSource', priceTypeDataSource); $("#priceSelect").dxSelectBox("instance").option('displayExpr', 'PriceTypeIdDesc'); $("#priceSelect").dxSelectBox("instance").option('valueExpr', "PriceTypeId"); getSitePrice(BWA.UserInfo.SiteId(), result[0].FuelTypeId);//PriceCode P038 청구요금 }) .fail(function (error) { alert(error); }); } function PriceTypeDataSourceToArray(dataSource) { var resultArray = new Array(dataSource._items.length); for (var i = 0; i < dataSource._items.length; i++) { resultArray[i] = dataSource._items[i].PriceTypeIdDesc; } return resultArray; } function getPriceCode(FuelTypeId) { //요금제 항목 종류 var dataSource = new DevExpress.data.DataSource({ store: { type: "odata", url: endpointSelector + "/BemsPriceCode" }, map: function (itemData) { return { PriceCode: itemData.PriceCode, PriceCodeDesc: itemData.PriceCodeDesc, FuelTypeId: itemData.FuelTypeId, Unit: itemData.Unit } }, requireTotalCount: true, pageSize: 100 }); dataSource.filter(["FuelTypeId", "=", FuelTypeId]); dataSource.sort({ getter: "PriceCode", asc: true }); dataSource.load() .done(function (result) { priceCodeDataSource = result; var $PriceCodeLayOut = $('#PriceCodeLayOut'); // finds the element $('#PriceCodeLayOut').empty(); for (var i = 0; i < result.length; i++) { $('') // creates a new HTML element .appendTo($PriceCodeLayOut) // appends a new HTML element to the element $('
') .appendTo($PriceCodeLayOut) .dxSwitch(); if (i % 5 == 4) { $('
').appendTo($PriceCodeLayOut) $('
').appendTo($PriceCodeLayOut) $('
').appendTo($PriceCodeLayOut) } } var setInputIds = document.querySelectorAll('.setInputId'); var setSwitchIds = document.querySelectorAll('.setSwitchId'); for (var i = 0; i < result.length; i++) { setInputIds[i].id = 'input' + i; setInputIds[i].value = result[i].PriceCodeDesc; setSwitchIds[i].id = 'switch' + i; $('#switch' + i).dxSwitch("instance").option('onText', 'On'); $('#switch' + i).dxSwitch("instance").option('offText', 'Off'); $('#switch' + i).dxSwitch("instance").option('value', true); } getPriceCodeYN(BWA.UserInfo.SiteId(), FuelTypeId); }) .fail(function (error) { alert(error); }); } function getPriceCodeYN(SiteID, FuelTypeId) { //현재 요금제 선택된 항목 var dataSource = new DevExpress.data.DataSource({ store: new DevExpress.data.ODataStore({ url: endpointSelector + "/BemsSitePrice" }), expand: 'BemsPriceType', requireTotalCount: true, pageSize: 100 }); dataSource.filter([ ["SiteID", "=", SiteID], "and", ["FuelTypeId", "=", FuelTypeId], "and", ["BemsPriceType.PriceTypeIdDesc", "=", $("#tabOptions").dxTabs("instance").option('selectedItem').text] ]); dataSource.sort({ getter: "PriceCode", asc: true }); dataSource.load() .done(function (result) { for (var i = 0; i < result.length; i++) { var tmpYN; if (result[i].UseYN == "Y") { tmpYN = true; } else { tmpYN = false; } $("#switch" + i).dxSwitch("instance").option('value', tmpYN); } }) .fail(function (error) { alert(error); }); } function saveSettings() { var newmenus = []; var FuelTypeId; var priceTypeId = parseInt($("#priceSelect").dxSelectBox("instance").option('value')); if (energySelectButton == "power") { FuelTypeId = 1; } else if (energySelectButton == "gas") { FuelTypeId = 2; } else { FuelTypeId = 3; } var tmp = parseInt($("#tabOptions").dxTabs("instance").option('selectedIndex')); var seletctedTabItem = $("#tabOptions").dxTabs("instance").option('selectedItem'); var selectPriceTypeId = parseInt($("#priceSelect").dxSelectBox("instance").option('value')); var selectPriceTypeIdDesc = $("#priceSelect").dxSelectBox("instance").option('text'); if (selectPriceTypeId == 0) { utils.toast.show("요금제를 선택해 주세요."); return; } var dataSource = new DevExpress.data.DataSource({ store: new DevExpress.data.ODataStore({ url: endpointSelector + "/BemsSitePrice" }), expand: 'BemsPriceType', requireTotalCount: true, pageSize: 100 }); dataSource.filter([ ["SiteID", "=", BWA.UserInfo.SiteId()], "and", ["FuelTypeId", "=", FuelTypeId] ]); dataSource.sort({ getter: "PriceCode", asc: true }); dataSource.load() .done(function (result) { for (var i = 0; i < result.length; i++) { if (result[i].PriceTypeId == priceTypeId) { old = true; break; } } if (old)// 기존에 있는 요금표 변경 -> 기존꺼 N { BWA.api.post('BemsPriceFormula/Update', { SiteId: BWA.UserInfo.SiteId(), PriceTypeId: selectPriceTypeId, FacilityTypeId: selectFacilityTypeId, FormulaId: selectFormulaId, FacilityCode: selectFacilityCode, UseYN: 'N' }).done(function () { saving_PriceFormula = true; if (saving_PriceFormula) { BWA.api.post('BemsPriceFormula/Update', { SiteId: BWA.UserInfo.SiteId(), PriceTypeId: priceTypeId, FacilityTypeId: selectFacilityTypeId, FormulaId: selectFormulaId, FacilityCode: selectFacilityCode, UseYN: 'Y' }).done(function () { saving_PriceFormula = false; }).fail(function (error) { utils.toast.show(error); }); } }); old = false; } else {// 새로운 요금표 생성 -> 기존꺼 Y BWA.api.post('BemsPriceFormula/Update', { SiteId: BWA.UserInfo.SiteId(), PriceTypeId: priceTypeId, FacilityTypeId: selectFacilityTypeId, FormulaId: selectFormulaId, FacilityCode: selectFacilityCode, UseYN: 'Y' }).done(function () { }); } }) .fail(function (error) { alert(error); }); for (var i = 0; i < priceCodeDataSource.length; i++) { //삭제후 저장 var UseYN = $("#switch" + i).dxSwitch("instance").option('value'); if (UseYN == true) { UseYN = 'Y'; } else { UseYN = 'N'; } newmenus.push({ SiteId: BWA.UserInfo.SiteId(), FuelTypeId: FuelTypeId, PriceTypeId: selectPriceTypeId, PriceCode: priceCodeDataSource[i].PriceCode, UseYN: UseYN, }); } for (var i = 0; i < tabsData.length; i++) { if (selectPriceTypeId == tabsData[i].id) { if (seletctedTabItem.id != selectPriceTypeId) { utils.toast.show("존재하고 있는 요금제입니다."); return; } } } if (seletctedTabItem.id != 0) { BWA.api.post('BemsSitePrice/DeleteAll', { SiteID: BWA.UserInfo.SiteId(), PriceTypeId: seletctedTabItem.id }).done(function () { BWA.api.post('BemsSitePrice/InsertAll', newmenus).done(function () { newmenus = []; }).fail(function (error) { utils.toast.show(error); }); }); } else { BWA.api.post('BemsSitePrice/InsertAll', newmenus).done(function () { }).fail(function (error) { utils.toast.show(error); }); } tabsData[tmp].id = selectPriceTypeId; tabsData[tmp].text = selectPriceTypeIdDesc; $("#tabOptions").dxTabs("instance").option('dataSource', tabsData); setTimeout(function () { $("#tabOptions").dxTabs("instance").option('selectedIndex', 0) }, 2000); $("#tabOptions").dxTabs("instance").option('selectedIndex', 0); utils.toast.show("요금제가 저장 되었습니다."); } function getSitePriceData(SiteID, FuelTypeId) { var dataSource = new DevExpress.data.DataSource({ store: new DevExpress.data.ODataStore({ url: endpointSelector + "/BemsSitePrice" }), expand: 'BemsPriceType', requireTotalCount: true, pageSize: 1000 }); dataSource.filter([ ["SiteID", "=", SiteID], "and", ["FuelTypeId", "=", FuelTypeId] ]); dataSource.sort({ getter: "PriceTypeId", asc: true }); dataSource.load() .done(function (result) { if (result.length != 0) { var tmp = []; var tmpCnt = 0; tmp.push({ id: result[0].BemsPriceType.PriceTypeId, text: result[0].BemsPriceType.PriceTypeIdDesc }); for (var i = 0; i < result.length; i++) { if (tmp[tmpCnt].text != result[i].BemsPriceType.PriceTypeIdDesc) { tmp.push({ id: result[i].BemsPriceType.PriceTypeId, text: result[i].BemsPriceType.PriceTypeIdDesc }); tmpCnt++; } } } else { utils.toast.show("작성된 요금제가 없습니다."); } tabsData = tmp; $("#tabOptions").dxTabs("instance").option('dataSource', tabsData); $("#tabOptions").dxTabs("instance").option('selectedIndex', 0); getPriceType(FuelTypeId); getPriceCode(FuelTypeId); }) .fail(function (error) { utils.toast.show(error); }); } function deletePrice() { var FuelTypeId; if (energySelectButton == "power") { FuelTypeId = 1; } else if (energySelectButton == "gas") { FuelTypeId = 2; } else if (energySelectButton == "water") { FuelTypeId = 3; } else { utils.toast.show("에너지원이 선택되지 않았습니다."); } if (tabsData.length == 1) { utils.toast.show("요금제가 최소 하나 이상이어야 합니다."); return; } var selectPriceTypeId = parseInt($("#priceSelect").dxSelectBox("instance").option('value')); BWA.api.post('BemsPriceFormula/Update', { SiteId: BWA.UserInfo.SiteId(), PriceTypeId: selectPriceTypeId, FacilityTypeId: selectFacilityTypeId, FormulaId: selectFormulaId, FacilityCode: selectFacilityCode, UseYN: 'N' }).done(function () { }); BWA.api.post('BemsSitePrice/DeleteAll', { SiteID: BWA.UserInfo.SiteId(), PriceTypeId: selectPriceTypeId }).done(function () { }).fail(function (error) { utils.toast.show(error); }); utils.toast.show("요금제가 삭제 되었습니다."); tabsData.splice(parseInt($("#tabOptions").dxTabs("instance").option('selectedIndex')), 1); $("#tabOptions").dxTabs("instance").option('dataSource', tabsData); $("#tabOptions").dxTabs("instance").option('selectedIndex', 0); } function SelectBemsPriceFormula() { var dataSource = new DevExpress.data.DataSource({ store: new DevExpress.data.ODataStore({ url: endpointSelector + "/BemsPriceFormula" }), requireTotalCount: true, pageSize: 100 }); var priceTypeId = $("#priceSelect").dxSelectBox("instance").option('value'); if (priceTypeId == null) return; dataSource.filter([ ["SiteId", "=", BWA.UserInfo.SiteId()], "and", ["PriceTypeId", "=", priceTypeId] ]); dataSource.load() .done(function (result) { selectFacilityTypeId = result[0].FacilityTypeId selectFacilityCode = result[0].FacilityCode selectFormulaId = result[0].FormulaId }) .fail(function (error) { utils.toast.show(error); }); } function handleViewShown() { getSitePriceData(BWA.UserInfo.SiteId(), 1); $("#powerBtn").css("background-image", "linear-gradient(rgb(155,171,195) 0%,rgb(65,97,140) 100%)"); $("#gasBtn").css("background-image", "linear-gradient(rgb(56,88,130) 0%, rgb(44,68,101)100%)"); $("#waterBtn").css("background-image", "linear-gradient(rgb(56,88,130) 0%, rgb(44,68,101)100%)"); init = true; } var searchViewOptions = { //templete function }; function handleSearchInSearchView() { //templete function } function handleViewShowing() { //권한설정 hasnotModificationPermission(!BWA.UserInfo.hasPermissionOfModification(viewInfo.viewName)); } var viewModel = $.extend(BWA.CommonView.create(params, viewInfo, searchViewOptions, ko.observable(null), handleViewShown, null, handleSearchInSearchView), { viewShowing: handleViewShowing, powerButtonClicked: function () { energySelectButton = "power"; $("#powerBtn").css("background-image", "linear-gradient(rgb(155,171,195) 0%,rgb(65,97,140) 100%)");//(180deg,#9babc3 0%,#41618c 100%) $("#gasBtn").css("background-image", "linear-gradient(rgb(56,88,130) 0%, rgb(44,68,101)100%)"); $("#waterBtn").css("background-image", "linear-gradient(rgb(56,88,130) 0%, rgb(44,68,101)100%)"); getSitePriceData(BWA.UserInfo.SiteId(), 1); }, gasButtonClicked: function () { energySelectButton = "gas"; $("#gasBtn").css("background-image", "linear-gradient(rgb(155,171,195) 0%,rgb(65,97,140) 100%)"); $("#powerBtn").css("background-image", "linear-gradient(rgb(56,88,130) 0%, rgb(44,68,101)100%)"); $("#waterBtn").css("background-image", "linear-gradient(rgb(56,88,130) 0%, rgb(44,68,101)100%)"); getSitePriceData(BWA.UserInfo.SiteId(), 2); }, waterButtonClicked: function () { energySelectButton = "water"; $("#waterBtn").css("background-image", "linear-gradient(rgb(155,171,195) 0%,rgb(65,97,140) 100%)"); $("#gasBtn").css("background-image", "linear-gradient(rgb(56,88,130) 0%, rgb(44,68,101)100%)"); $("#powerBtn").css("background-image", "linear-gradient(rgb(56,88,130) 0%, rgb(44,68,101)100%)"); getSitePriceData(BWA.UserInfo.SiteId(), 3); }, saveButtonClicked: function () { DevExpress.ui.dialog.confirm("요금제를 저장 하시겠습니까?", "저장").done(function (dialogResult) { if (dialogResult) saveSettings(); }); }, tabOptions: { dataSource: tabsData, height: 20, selectedIndex: 0, onSelectionChanged: function (e) { $("#priceSelect").dxSelectBox("instance").option('value', e.addedItems[0].id); if (e.addedItems[0].id != 0) SelectBemsPriceFormula(); var FuelTypeId; if (energySelectButton == "power") { FuelTypeId = 1; } else if (energySelectButton == "gas") { FuelTypeId = 2; } else if (energySelectButton == "water") { FuelTypeId = 3; } else { utils.toast.show("에너지원이 선택되지 않았습니다."); } getPriceCode(FuelTypeId); } }, addButtonClicked: function () { if ((tabsData.length == 5) && (tabsData == null)) { utils.toast.show("요금제는 추가는 최대 5개까지만 가능합니다."); } else { tabsData.push({ id: 0, text: "신규 요금제" }); $("#tabOptions").dxTabs("instance").option('dataSource', tabsData); $("#tabOptions").dxTabs("instance").option('selectedIndex', tabsData.length - 1); setTimeout(function () { $("#tabOptions").dxTabs("instance").option('selectedIndex', tabsData.length - 1); }, 1000); } }, removeButtonClicked: function () { if (tabsData[tabsData.length - 1].id != 0) { utils.toast.show("기존 요금제는 요금제 세부설정에서 삭제 가능합니다."); } else { tabsData.splice(tabsData.length - 1, 1); $("#tabOptions").dxTabs("instance").option('dataSource', tabsData); $("#tabOptions").dxTabs("instance").option('selectedIndex', 0); } }, priceSelect: { dataSource: priceTypeDataSource, onSelectionChanged: function () { if (init) if ($("#priceSelect").dxSelectBox("instance").option('value') != 0) SelectBemsPriceFormula(); } }, delButtonClicked: function () { DevExpress.ui.dialog.confirm("요금제를 삭제 하시겠습니까?", "삭제").done(function (dialogResult) { if (dialogResult) { deletePrice(); } }); }, hasnotModificationPermission: hasnotModificationPermission //권한설정 }); return viewModel; };