BemsWebApplication.RegularCheckSchedule = function (params, viewInfo, legalCheckOptions) { "use strict"; var cycleUnitDataSource = BemsWebApplication.db.createDataSource('FmsWorkCodeCycleUnit'), holidayWorkTypeDataSource = BemsWebApplication.db.createDataSource('FmsWorkCodeHolidayWorkType', true, true), businessFieldDataSource = BemsWebApplication.db.createDataSource('CmBusinessField', true, true, true), agencyDataSource = undefined; //var partnerTypeDataSource = BemsWebApplication.db.createDataSource('CmPartnerType', false, false, true); //var partnerTypes = ko.observableArray(); //BemsWebApplication.db.createDataSource('CmPartner', true, false, true), var popup; var IsUseStatusForSerach = ko.observable(); //추가 var searchIsuseStatus = $SearchView.createObservableCodeNamesOfSelectBoxForSearch($G.IsUseName);// 추가 var eq = BWA.DataUtil.constructEqualFilter, and = BWA.DataUtil.andFilter; var FmsWorkScheduleDataSource = BemsWebApplication.db.createDataSource('FmsWorkSchedule'); var workschedule = ko.observableArray(); var RowName, RowBusinessFieldId; var legalCheckViewOptions = undefined, legalCheckPopupOptions = undefined; var anyCheckViewOptions = undefined; if (legalCheckOptions) { legalCheckViewOptions = legalCheckOptions.viewOptions; legalCheckPopupOptions = legalCheckOptions.popupOptions; } var cycleUnits = ko.observableArray(), holidayWorkTypes = ko.observableArray(), businessFields = ko.observableArray(), agencies = undefined, businessFieldsForSearch = ko.observableArray(), agenciesForSearch = undefined; var deferredForSearch = new $.Deferred(); var selectedScheduleId; var selectedScheduleName = ko.observable(); var workTypeId = legalCheckViewOptions ? $Code.WorkType.LEGAL_CHECK : (anyCheckViewOptions ? $Code.WorkType.INNER_CHECK : $Code.WorkType.REGULAR_CHECK), dataSourceOptions = { select: [ 'SiteId', 'WorkScheduleId', 'WorkTypeId', 'BusinessFieldId', 'Name', 'CycleSize', 'IsUse', 'IsAutoOrder', 'WorkDueDate', 'CmUser/Name', 'CmBusinessField/Name', 'FmsWorkCodeCycleUnit/Name', 'FmsWorkCodeHolidayWorkType/Name', 'CmPartner/Name' ], filter: [ eq('SiteId', BWA.UserInfo.SiteId()), and, eq('WorkTypeId', workTypeId) ], expand: ['CmUser', 'CmBusinessField', 'FmsWorkCodeCycleUnit', 'FmsWorkCodeHolidayWorkType', 'CmPartner'], extendOptions: { forceOriginalField: true } }; if (!_.isUndefined(legalCheckViewOptions)) { //dataSourceOptions.select.push('FmsWorkCodeLegalInspectionAgency/Name'); //dataSourceOptions.expand.push('FmsWorkCodeLegalInspectionAgency'); dataSourceOptions.select.push('CmPartner/Name'); dataSourceOptions.expand.push('CmPartner'); //agencyDataSource = BemsWebApplication.db.createDataSource('FmsWorkCodeLegalInspectionAgency', true, true); agencyDataSource = BemsWebApplication.db.createDataSource('CmPartner', true, false, true), agencyDataSource.filter([ eq('PartnerTypeId', 3), // 검사기관 and, eq('SiteId', BWA.UserInfo.SiteId()) ]) agencies = ko.observableArray(); agenciesForSearch = ko.observableArray(); } var viewModel = BWA.DataGrid.createViewWithDataGrid(params, viewInfo, 'FmsWorkSchedule', { popupWidth: 1200, dataSourceOptions: dataSourceOptions, columns: _.compact([ { dataField: 'WorkScheduleId', caption: $G('number'), width: '8%', alignment: 'center', sortOrder: 'desc' }, { dataField: 'CmBusinessField/Name', caption: $G('businessFieldName'), width: '10%', alignment: 'center' }, { dataField: 'Name', caption: $G('facilityCheckScheduleName'), width: '34%', alignment: 'center' }, { dataField: 'WorkDueDate', caption: $G('workDueDate'), width: '12%', alignment: 'center', customizeText: function (cellInfo) { return $G.date(cellInfo.value); } }, { dataField: 'FmsWorkCodeHolidayWorkType/Name', caption: $G('facilityCheckScheduleHolidayName'), width: '10%', alignment: 'center' }, { dataField: 'FmsWorkCodeCycleUnit/Name', caption: $G('facilityCheckScheduleCycleUnitName'), width: '10%', alignment: 'center' }, legalCheckViewOptions ? { dataField: 'CmPartner/Name', caption: $G('legalInspectionAgency'), width: '15%', alignment: 'center' } : undefined, { dataField: 'IsAutoOrder', caption: $G("autoOrder"), alignment: 'center', width: '8%', customizeText: function (cellInfo) { return $G.isUse(cellInfo.value); } }, utils.datagrid.columnIsUse('8%') ]), promiseDataInSearchView: deferredForSearch.promise(), searchViewItems: _.compact([ { id: 'BusinessFieldId', ignoreValue: 0, defaultValue: 0, dataSource: businessFieldsForSearch }, legalCheckViewOptions ? { id: 'InspectionAgencyId', ignoreValue: 0, defaultValue: 0, dataSource: agenciesForSearch } : undefined, { id: 'Name' }, { id: 'WorkDueDate', type: 'dateRange', isOnlyDate: true}, { id: 'IsUse', ignoreValue: 0, defaultValue: true, value: IsUseStatusForSerach, dataSource: searchIsuseStatus, isChecked: true } //추가 //$SearchView.handleSearchInSearchView(); ]),//IsUseStatusForSerach wordWrapEnabled: true, cellPrepared: function (cellElement, cellInfo) { if (cellInfo.rowType == 'data') { switch (cellInfo.column.dataField) { case 'FmsWorkCodeCycleUnit/Name': $(cellElement).text(cellInfo.data.CycleSize() + ' ' + cellInfo.value); break; } } }, handleInitializeDataModelValue: function (dataModel) { dataModel.IsAutoOrder(false); dataModel.WorkTypeId(workTypeId); dataModel.BusinessFieldId(BWA.UserInfo.BusinessFieldId()); }, // 이름 중복 방지 handlePopupShowing: function () { if (popup && popup.handlePopupShowing) { popup.handlePopupShowing.apply(this, arguments); } FmsWorkScheduleDataSource.filter([ ["SiteId", "=", BWA.UserInfo.SiteId()], 'and', ["WorkTypeId", "=", workTypeId] ]); FmsWorkScheduleDataSource.load().done(function (result) { workschedule(result); }); }, beforeInsertingDataViewModel: function (dataModel, dbModelId) { var dfd = $.Deferred(); var isDuplicated = false; for (var i = 0; i < workschedule().length; i++) { if (dataModel.Name() == workschedule()[i].Name() && dataModel.BusinessFieldId() == workschedule()[i].BusinessFieldId()) { isDuplicated = true; } } if (!BWA.DataUtil.isValidInputValue(dataModel.Name())) return dfd.resolve(false, '저장 혹은 수정 반드시 필수 입력 정보를 모두 입력하셔야 합니다 (앞뒤공백 허용안함)'); else { if (isDuplicated) return dfd.resolve(false, '이미 동일 정보가 존재합니다.'); else return dfd.resolve(true); } }, beforeUpdateDataViewModel: function () { var isDuplicated = false; for (var i = 0; i < workschedule().length; i++) { if (viewModel.dataModel.Name() == workschedule()[i].Name() && viewModel.dataModel.BusinessFieldId() == workschedule()[i].BusinessFieldId() && (RowName() != workschedule()[i].Name() || RowBusinessFieldId() != workschedule()[i].BusinessFieldId())) { isDuplicated = true; } } 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; } }, handleViewShowing: function () { var useBusinessFiltering = BWA.UserInfo.isDependBusinessField(viewInfo.viewName); if (useBusinessFiltering) { dataSourceOptions.filter.push('and'); dataSourceOptions.filter.push([eq('BusinessFieldId', BWA.UserInfo.BusinessFieldId()), 'or', eq('BusinessFieldId', 1)]); businessFieldDataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), and, [eq('BusinessFieldId', BWA.UserInfo.BusinessFieldId()), 'or', eq('BusinessFieldId', 1)] ]); } var loadFromDataSource = BWA.DataUtil.loadFromDataSource; //loadFromDataSource(holidayWorkTypeDataSource, holidayWorkTypes); $.when.apply($, _.compact([ cycleUnitDataSource.load(), holidayWorkTypeDataSource.load(), businessFieldDataSource.load(), agencyDataSource ? agencyDataSource.load() : undefined ])).done(function (dbCycleUnits, dbHolidayWorkTypes, dbBusinessFields, dbAgencies) { cycleUnits(dbCycleUnits[0]); holidayWorkTypes(dbHolidayWorkTypes[0]); businessFields(dbBusinessFields[0]); businessFieldsForSearch($SearchView.createArrayOfSelectBox('BusinessFieldId', businessFields())); if (!_.isUndefined(dbAgencies)) { agencies(dbAgencies[0]); agenciesForSearch($SearchView.createArrayOfSelectBox('PartnerId', agencies())); } deferredForSearch.resolve(); }); }, handleAfterLoadingModelByKey: function (data, dataViewModel) { var dataModel = viewModel.dataModel; dataModel.CycleSizeText(data.CycleSize + ' ' + dataModel.CycleUnitName()); if (popup && popup.handleAfterLoadingModelByKey) { popup.handleAfterLoadingModelByKey(data, dataViewModel); } }, handleViewShown: function () { // 추가 setTimeout(delayplay, 100); }, handleDataGridRowClick: function (id, dataGrid, clickRow, popupVisible) { dataGrid.clearSelection(); var data = clickRow.data; var dataModel = viewModel.dataModel; if (popup && popup.handleBeforeShowingPopupForEdit) { popup.handleBeforeShowingPopupForEdit(data, dataModel); } RowName = data.Name; RowBusinessFieldId = data.BusinessFieldId; //selectedScheduleId = data.ScheduleId(); //selectedScheduleName = $KoSet(selectedScheduleName, data.Name()); //selectedCheckGroupDataSource.filter([ // ['SiteId', '=', BWA.UserInfo.SiteId()], // 'and', // ['WorkScheduleId', '=', selectedScheduleId] //]); popupVisible(true); }, handleSaveButtonInPopup: function (isInsert) { if (isInsert === false) { if (popup && popup.handleAfterSave) { popup.handleAfterSave(undefined); } } }, handleAfterSaveInPopup: function (responseKey, response) { if (popup && popup.handleAfterSave) { popup.handleAfterSave(responseKey, response); } }, getAddedToolbarItemsInPopup: function (e) { var isEditModeInPopup = e.isEditModeInPopup; return [{ location: 'after', widget: 'button', options: { text: $G('facilityInsertion'), icon: 'search', visible: isEditModeInPopup, clickAction: function () { popup.facilitySearchPopup.show(); } } }, { location: 'after', widget: 'button', options: { text: $G('facilityDeletion'), icon: 'remove', type: 'danger', visible: isEditModeInPopup, clickAction: function () { if (popup && popup.handleDeleteSelectedFacilities) { popup.handleDeleteSelectedFacilities(); } } } }]; }, //handlePopupShown: function (editMode, isNewInPopup, dataModel) { // if (popup && popup.handlePopupShowing) { // popup.handlePopupShown.apply(this, arguments); // } //} }); //function processValueChange(e) { // if ($G.date(viewModel.dataModel.WorkDueDate()) < $G.date(new Date())) // { // viewModel.dataModel.WorkDueDate(new Date()); // utils.toast.show('작업예정일은 오늘 혹은 오늘 이후로 설정하시기 바랍니다.', 'error'); // } //} function delayplay() { BemsWebApplication.app.currentViewInfo = viewInfo; BemsWebApplication.app.currentViewInfo.model.handleSearchInSearchView(); } viewModel.dataModel.CycleSizeText = ko.observable(); viewModel.cycleUnits = cycleUnits; viewModel.holidayWorkTypes = holidayWorkTypes; viewModel.businessFields = businessFields; viewModel.agencies = agencies; viewModel.legalCheckOptions = legalCheckOptions; popup = viewModel.popup = BWA.Popup.RegularCheckSchedule.create(viewInfo, viewModel, legalCheckPopupOptions); viewModel.popupVisible.subscribe(function (visible) { if (!visible) { popup.facilitiesInView([]); popup.facilities([]); } }); //viewModel.processValueChange = processValueChange; return viewModel; };