BemsWebApplication.ContractManagement = function (params, viewInfo) { 'use strict'; var partnerTypeDataSource = BemsWebApplication.db.createDataSource('CmPartnerType', false, true, true), partnerDataSource = BemsWebApplication.db.createDataSource('CmPartner', true, true, true), contractClassDataSource = BemsWebApplication.db.createDataSource('FmsContractClass', true, true, true), contractMethodDataSource = BemsWebApplication.db.createDataSource('FmsContractMethod', true, true, true), contractTypeDataSource = BemsWebApplication.db.createDataSource('FmsContractType', true, true, true), paymentTypeDataSource = BemsWebApplication.db.createDataSource('FmsPaymentType', true, true, true); var partnerTypes = ko.observableArray(), contractClasses = ko.observableArray(), contractMethods = ko.observableArray(), contractTypes = ko.observableArray(), paymentTypes = ko.observableArray(), allPartner = ko.observableArray(), filteredPartner = ko.observableArray(); var partnerTypesForSearch = ko.observableArray(), contractClassesForSearch = ko.observableArray(), contractMethodsForSearch = ko.observableArray(), contractTypesForSearch = ko.observableArray(), paymentTypesForSearch = ko.observableArray(); var ConstractInfo = ko.observableArray(); var FmsConstractDataSource = BemsWebApplication.db.createDataSource('FmsContract'); var RowName; var deferredForSearch = new $.Deferred(); var dataSourceOptions = { select: [ 'SiteId', 'ContractId', 'Name', 'PartnerTypeId', 'PartnerId', 'ContractDate', 'StartDate', 'EndDate', 'OwnerShipName', 'OwnerShipPhoneNo', 'CommitmentMan', 'Comment', 'ContractClassId', 'ContractTypeId', 'ContractMethodId', 'PaymentTypeId', 'CmPartner/Name', 'CmPartnerType/Name', 'FmsContractClass/Name', 'FmsContractType/Name', 'FmsContractMethod/Name', 'FmsPaymentType/Name' ], expand: [ 'CmPartner', 'CmPartnerType', 'FmsContractClass', 'FmsContractType', 'FmsContractMethod', 'FmsPaymentType' ], extendOptions: { forceOriginalField: true }, filter: [ ['SiteId', '=', BWA.UserInfo.SiteId()], ] }; var viewModel = BWA.DataGrid.createViewWithDataGrid(params, viewInfo, 'FmsContract', { dataSourceOptions: dataSourceOptions, columns: [ { dataField: 'ContractId', caption: $G('number'), width: '10%', alignment: 'center', allowFiltering: false, sortOrder: 'desc' }, { dataField: 'Name', caption: '계약명', width: '45%', alignment: 'center' }, { dataField: 'CmPartnerType/Name', caption: '업체유형', width: '35%', alignment: 'center' }, { dataField: 'StartDate', caption: '시작일', width: '15%', alignment: 'center', dataType: 'string', customizeText: function (cellInfo) { if (cellInfo.value != null) { if (cellInfo.value.getMonth() >= 9) { if (cellInfo.value.getDate() >= 10) { return cellInfo.value.getFullYear() + "-" + (cellInfo.value.getMonth() + 1) + "-" + cellInfo.value.getDate(); } else { return cellInfo.value.getFullYear() + "-" + (cellInfo.value.getMonth() + 1) + "-0" + cellInfo.value.getDate(); } } else { if (cellInfo.value.getDate() >= 10) { return cellInfo.value.getFullYear() + "-0" + (cellInfo.value.getMonth() + 1) + "-" + cellInfo.value.getDate(); } else { return cellInfo.value.getFullYear() + "-0" + (cellInfo.value.getMonth() + 1) + "-0" + cellInfo.value.getDate(); } } } } }, { dataField: 'EndDate', caption: '종료일', width: '15%', alignment: 'center', dataType: 'string', customizeText: function (cellInfo) { if (cellInfo.value != null) { if (cellInfo.value.getMonth() >= 9) { if (cellInfo.value.getDate() >= 10) { return cellInfo.value.getFullYear() + "-" + (cellInfo.value.getMonth() + 1) + "-" + cellInfo.value.getDate(); } else { return cellInfo.value.getFullYear() + "-" + (cellInfo.value.getMonth() + 1) + "-0" + cellInfo.value.getDate(); } } else { if (cellInfo.value.getDate() >= 10) { return cellInfo.value.getFullYear() + "-0" + (cellInfo.value.getMonth() + 1) + "-" + cellInfo.value.getDate(); } else { return cellInfo.value.getFullYear() + "-0" + (cellInfo.value.getMonth() + 1) + "-0" + cellInfo.value.getDate(); } } } } }, { dataField: 'CmPartner/Name', caption: '업체명', width: '25%', alignment: 'center' }, { dataField: 'OwnerShipName', caption: '담당자', width: '20%', alignment: 'center' }, { dataField: 'OwnerShipPhoneNo', caption: '전화번호', width: '25%', alignment: 'center' }, ], paging: { pageSize: 18, enabled: true }, pager: {}, "export": { enabled: true, fileName: '계약 목록', }, onExporting: function (e) { e.component.columnOption("ContractId", "visible", false); }, onExported: function (e) { e.component.columnOption("ContractId", "visible", true); }, searchViewItems: [ { id: 'Name' }, { id: 'StartDate', type: 'dateRange', isOnlyDate: true }, { id: 'EndDate', type: 'dateRange', isOnlyDate: true }, { id: 'CmPartner/Name' }, { id: 'OwnerShipName' }, { id: 'PartnerTypeId', ignoreValue: 0, defaultValue: 0, dataSource: partnerTypesForSearch }, { id: 'ContractClassId', ignoreValue: 0, defaultValue: 0, dataSource: contractClassesForSearch }, { id: 'ContractMethodId', ignoreValue: 0, defaultValue: 0, dataSource: contractMethodsForSearch }, { id: 'ContractTypeId', ignoreValue: 0, defaultValue: 0, dataSource: contractTypesForSearch }, { id: 'PaymentTypeId', ignoreValue: 0, defaultValue: 0, dataSource: paymentTypesForSearch }, ], handleViewShowing: function (dataModel) { $.when( partnerTypeDataSource.load(), contractClassDataSource.load(), contractMethodDataSource.load(), contractTypeDataSource.load(), paymentTypeDataSource.load(), partnerDataSource.load() ).done(function (partnerTypeDataResult, contractClassDataResult, contractMethodDataResult, contractTypeDataResult, paymentTypeDataResult, partnerDataResult) { partnerTypes(partnerTypeDataResult[0]); contractClasses(contractClassDataResult[0]); contractMethods(contractMethodDataResult[0]); contractTypes(contractTypeDataResult[0]); paymentTypes(paymentTypeDataResult[0]); allPartner(partnerDataResult[0]); partnerTypesForSearch($SearchView.createArrayOfSelectBox('PartnerTypeId', partnerTypeDataResult[0])); contractClassesForSearch($SearchView.createArrayOfSelectBox('ContractClassId', contractClassDataResult[0])); contractMethodsForSearch($SearchView.createArrayOfSelectBox('ContractMethodId', contractMethodDataResult[0])); contractTypesForSearch($SearchView.createArrayOfSelectBox('ContractTypeId', contractTypeDataResult[0])); paymentTypesForSearch($SearchView.createArrayOfSelectBox('PaymentTypeId', paymentTypeDataResult[0])); deferredForSearch.resolve(); }); }, handleDataGridRowClick: function (id, dataGrid, clickRow, popupVisible) { var data = clickRow.data; RowName = data.Name; popupVisible(true); }, // 이름 중복 방지 handlePopupShowing: function () { FmsConstractDataSource.filter([ ["SiteId", "=", BWA.UserInfo.SiteId()] ]); FmsConstractDataSource.load().done(function (result) { ConstractInfo(result); }); }, beforeInsertingDataViewModel: function (dataModel, dbModelId) { var dfd = $.Deferred(); var isDuplicated = false; if (dataModel.StartDate() > dataModel.EndDate()) dfd.resolve(false, '시작일은 종료일 이전으로 선택해야 합니다.', 'error'); for (var i = 0; i < ConstractInfo().length; i++) { if (dataModel.Name() == ConstractInfo()[i].Name()) { isDuplicated = true; break; } } if (!BWA.DataUtil.isValidInputValue(dataModel.Name())) return dfd.resolve(false, '저장 혹은 수정 반드시 필수 입력 정보를 모두 입력하셔야 합니다 (앞뒤공백 허용안함)'); else { if (isDuplicated) return dfd.resolve(false, '이미 동일 정보가 존재합니다.'); else return dfd.resolve(true); } }, beforeUpdateDataViewModel: function () { if (viewModel.dataModel.StartDate() > viewModel.dataModel.EndDate()) { utils.toast.show('시작일은 종료일 이전으로 선택해야 합니다.', 'error'); return 0; } var isDuplicated = false; for (var i = 0; i < ConstractInfo().length; i++) { if (viewModel.dataModel.Name() == ConstractInfo()[i].Name() && RowName() != ConstractInfo()[i].Name()) { isDuplicated = true; break; } } if (!BWA.DataUtil.isValidInputValue(viewModel.dataModel.Name())) { utils.toast.show('저장 혹은 수정 반드시 필수 입력 정보를 모두 입력하셔야 합니다 (앞뒤공백 허용안함)', 'error'); return 0; } else { if (isDuplicated) { utils.toast.show('이미 동일 정보가 존재합니다.', 'error'); return 0; } else return 2; } }, }); function processValueChange(e) { filteredPartner([]); _.each(allPartner(), function (partner) { if (e.value === partner.PartnerTypeId()) { filteredPartner.push(partner); } }); if (!_.isEmpty(filteredPartner())) { viewModel.dataModel.PartnerId(filteredPartner()[0].PartnerId()); } } viewModel.processValueChange = processValueChange; viewModel.partnerTypes = partnerTypes; viewModel.contractClasses = contractClasses; viewModel.contractMethods = contractMethods; viewModel.contractTypes = contractTypes; viewModel.paymentTypes = paymentTypes; viewModel.allPartner = allPartner; viewModel.filteredPartner = filteredPartner; return viewModel; };