BemsWebApplication.Holiday = function (params, viewInfo) { "use strict"; var holidayDataSource = BemsWebApplication.db.createDataSource('CmHoliday', true, true); var holidayCustomDataSource = BemsWebApplication.db.createDataSource('CmHolidayCustom', true); var holidayWeekendDataSource = BemsWebApplication.db.createDataSource('CmHolidayWeekend', true); var holidaySources; var holidayCustomSources; var popupWeekendSetupVisible = ko.observable(false), popupInsertVisible = ko.observable(false), hasnotModificationPermission = ko.observable(true); var holidayModel = { SiteId: ko.observable(), HolidayMonth: ko.observable(), HolidayDay: ko.observable(), IsLunar: ko.observable(), HolidayDate: ko.observable(), IsYearlyHoliday: ko.observable(false), Name: ko.observable(), }; var backupDataViewModel = null; var cc = new CalendarConverter; var isEditModeInPopup = ko.observable(false); var disableOnlyEdit = ko.observable(true); var visibleEditButton = ko.observable(true); var visibleDeleteButton = ko.observable(true); var visibleCancelButton = ko.observable(false); var visibleSaveButton = ko.observable(false); var popupVisible = ko.observable(false); var holidayWeekendDataViewModel = new BemsWebApplication.CmHolidayWeekendViewModel(); var loadedYears = []; var viewDate = null; var currentYear = null; var holidays = []; var weekends = []; var isSaturday = false; var isSunday = false; var holidayCalendar = null; var calendarOptions = { selectable: true, editable: false, weekends: true, height: 680, select: handleSelectInCalendar, eventClick: handleEventClickInCalendar, changedMonth: function (view) { var date = view.calendar.getDate(); // hcLee 2016 01 22 viewDate = date; if (currentYear !== null && date.year() !== currentYear) { currentYear = date.year(); //refreshHolidays(date); loadHolidays(viewDate); } else { applyWeekends(date); } } }; var selectedDates = {}; function handleSelectInCalendar(start, end) { if (!hasnotModificationPermission()) { var date = start.startOf('hour'); var isExist = holidayCalendar.isExistEvent(date, function (i, event) { utils.toast.show('이미 등록된 휴일이 있습니다.', 'error'); }); if (isExist) return; holidayModel.Name(""); holidayModel.HolidayDate(start); holidayModel.IsYearlyHoliday(false); // 새 아이템 등록 selectedDates.start = start; selectedDates.end = end; disableOnlyEdit(false); visibleEditButton(false); visibleDeleteButton(false); visibleSaveButton(true); isEditModeInPopup(true); popupInsertVisible(true); } else { utils.toast.show('등록할 권한이 없습니다.', 'error'); } } function activePopupEditMode() { //visibleCancelButton( false ); // alert('activePopupEditMode'); visibleEditButton(false); visibleDeleteButton(false); visibleCancelButton(true); visibleSaveButton(true); //viewModeInPopup(false); isEditModeInPopup(true); } function activePopupViewMode() { // alert('activePopupViewMode'); visibleEditButton(true); visibleDeleteButton(true); visibleCancelButton(false); visibleSaveButton(false); //visibleDeleteButton(false); //viewModeInPopup(true); isEditModeInPopup(false); } popupVisible.subscribe(function (visible) { if (visible == false) { $.each(selectedId, function (name) { delete selectedId[name]; }); isNewInPopup(false); // visibleEditButton(true); visibleDeleteButton(true); } }); function handlePopupButtonEdit() { activePopupEditMode(); //visibleCancelButton(true); //holidayModel //backupDataViewModel = getBackupDataViewModel(holidayWeekendDataViewModel); backupDataViewModel = getBackupDataViewModel(holidayModel); } function getBackupDataViewModel(model) { var object = {}; $.each(model, function (name, value) { if (ko.isObservable(value)) { object[name] = value(); } }); return object; } function restoreDataViewModelFromBackup(model, backupModel) { $.each(backupModel, function (name, value) { model[name](value); }); } function handlePopupButtonCancel() { activePopupViewMode(); visibleCancelButton(false); restoreDataViewModelFromBackup(holidayModel, backupDataViewModel); } function handlePopupButtonDelete() { DevExpress.ui.dialog.confirm($G('deleteConfirmMsg'), "삭제").then(function (result) { if (result) { //handleConfirmDelete(); var dbModelId; var selectedId = {}; var date = holidayModel.HolidayDate(); if (holidayModel.IsYearlyHoliday()) { dbModelId = 'CmHoliday'; selectedId.HolidayMonth = date.month() + 1; selectedId.HolidayDay = date.date(); selectedId.IsLunar = holidayModel.IsLunar(); if (selectedId.IsLunar) { /* var solar = cc.lunar2solar(new Date(date.year() - 1, selectedId.HolidayMonth, selectedId.HolidayDay)); if (solar.sYear !== date.year()) { solar = cc.lunar2solar(new Date(date.year(), selectedId.HolidayMonth, selectedId.HolidayDay)); } //year = solar.sYear; selectedId.HolidayMonth = solar.sMonth - 1; selectedId.HolidayDay = solar.sDay; */ var lunar = cc.solar2lunar(new Date(date.year(), selectedId.HolidayMonth - 1, selectedId.HolidayDay)); selectedId.HolidayMonth = lunar.lMonth; selectedId.HolidayDay = lunar.lDay; } } else { dbModelId = 'CmHolidayCustom'; selectedId.HolidayDate = date.format(); } selectedId.SiteId = BWA.UserInfo.SiteId(); BemsWebApplication.db[dbModelId].remove(selectedId).done(function () { utils.toast.show('데이터베이스 항목 삭제 작업이 성공하였습니다.'); refreshList(); }); } popupInsertVisible(false); }); } function handleEventClickInCalendar(event) { if (event.isWeekend === true) { utils.toast.show('주말은 "주말 설정" 버튼을 통해 설정하시기 바랍니다.', 'warning'); return; } //기존 아이템 선택시 disableOnlyEdit(true); holidayModel.Name(event.title); holidayModel.IsLunar(event.isLunar); if (event.isYearlyHoliday == undefined) holidayModel.IsYearlyHoliday(false); else holidayModel.IsYearlyHoliday(true); holidayModel.HolidayDate(event.start); //activePopupEditMode(); activePopupViewMode(); /* visibleEditButton(true); visibleDeleteButton(true); visibleCancelButton(false); visibleSaveButton(false); */ popupInsertVisible(true); return; /* utils.dialog.confirm($G('deleteConfirmMsg'), event.isYearlyHoliday ? '정기휴일 삭제' : '특정휴일 삭제', function(result) { if (result) { var dbModelId; var selectedId = {}; var date = event.start; if (event.isYearlyHoliday) { dbModelId = 'CmHoliday'; selectedId.HolidayMonth = date.month() + 1; selectedId.HolidayDay = date.date(); } else { dbModelId = 'CmHolidayCustom'; selectedId.HolidayDate = date.format(); } selectedId.SiteId = BWA.UserInfo.SiteId(); BemsWebApplication.db[dbModelId].remove(selectedId).done(function() { utils.toast.show('데이터베이스 항목 삭제 작업이 성공하였습니다.'); refreshList(); }); } else { // toast } }); */ } function refreshList() { /* // hcLee 2016 01 22 var date = moment(); currentYear = date.year(); loadHolidays(date); hcLee 2016 03 10 */ currentYear = viewDate.year(); loadHolidays(viewDate); } // hcLee 2016 10 18 추가 var initialized = false; function handleViewShowing() { //권한설정 hasnotModificationPermission(!BWA.UserInfo.hasPermissionOfModification(viewInfo.viewName)); } function handleViewShown() { if (initialized === false) { // hcLee 2016 10 18 추가 holidayCalendar = utils.fullCalendar.create('holidayCalendar', calendarOptions); var date = moment(); currentYear = date.year(); loadHolidays(date); $SearchView.setPopupVisibleObservable(null); $SideMenu.showSideMenuIfWill(params.view); initialized = true; } else refreshList(); // hcLee 2016 10 18 추가 } function refreshHolidays(date) { holidays = []; var currentYear = date.year(); if (loadedYears.indexOf(currentYear) < 0) { loadedYears.push(currentYear); var year; var month; var day; var solar; $.each(holidaySources, function (i, item) { year = currentYear; month = item.HolidayMonth() - 1; day = item.HolidayDay(); if (item.IsLunar()) { solar = cc.lunar2solar(new Date(year - 1, month, day)); if (solar.sYear !== year) { solar = cc.lunar2solar(new Date(year, month, day)); } year = solar.sYear; month = solar.sMonth - 1; day = solar.sDay; } utils.holiday.pushHolidayEventInArray(holidays, item.Name(), moment([year, month, day]), true, undefined, 0, item.IsLunar()); }); $.each(holidayCustomSources, function (i, item) { var itemDate = item.HolidayDate(); if (itemDate.getFullYear() === currentYear) { utils.holiday.pushHolidayEventInArray(holidays, item.Name(), moment([itemDate.getFullYear(), itemDate.getMonth(), itemDate.getDate()]), false, undefined, 1, false); } }); } applyWeekends(date); } function loadHolidays(date) { holidays = []; loadedYears = []; $.when(holidayDataSource.load(), holidayCustomDataSource.load(), holidayWeekendDataSource.load()) .done(function (result1, result2, result3) { holidaySources = result1[0]; holidayCustomSources = result2[0]; if (result3.length > 0) { var weekend = result3[0][0]; isSaturday = weekend.Saturday(); isSunday = weekend.Sunday(); holidayWeekendDataViewModel.SiteId(weekend.SiteId()); holidayWeekendDataViewModel.Saturday(weekend.Saturday()); holidayWeekendDataViewModel.Sunday(weekend.Sunday()); } refreshHolidays(date); }); } function applyWeekends(date) { if (holidayCalendar === null) return; if (isSunday == false && isSaturday == false) { holidayCalendar.updateEvents(holidays); return; } weekends = utils.holiday.getWeekendHolidays(date, isSaturday, isSunday); //var month = date.clone().startOf( 'month' ); //var sunday = month.clone(), // saturday = month.clone(); //weekends = []; //if( month.day() !== 0 ) { // sunday.day( -7 ); //} //if( month.day() !== 6 ) { // saturday.day( -1 ); //} //for( var i = 0 ; i < 7 ; i++ ) { // if( isSunday ) { // holidayCalendar.pushEventInArray( weekends, '주말 휴일', moment( [ sunday.year(), sunday.month(), sunday.date() ], null, true, 2 )); // sunday.day( 7 ); // } // if( isSaturday ) { // holidayCalendar.pushEventInArray( weekends, '주말 휴일', moment( [ saturday.year(), saturday.month(), saturday.date() ], null, true, 2 )); // saturday.day( 13 ); // } //} holidayCalendar.updateEvents(holidays.concat(weekends)); } function handleModification() { //shouldReload = true; } function handleViewHidden() { holidayCalendar.clearEvents(true); } function handleViewDisposing() { BemsWebApplication.db.CmHolidayWeekend.modified.remove(handleModification); } BemsWebApplication.db.CmHolidayWeekend.modified.add(handleModification); function popupInsertView() { popupWeekendSetupVisible(true); } function handleWeekendSetupPopupButtonSave() { holidayWeekendDataViewModel.IsUse(true); var keys = BWA.db.extractKeysObject('CmHolidayWeekend', holidayWeekendDataViewModel); BemsWebApplication.db.CmHolidayWeekend.update(keys, holidayWeekendDataViewModel.toJS()).done(function () { utils.toast.show('데이터베이스 항목 수정 작업이 성공하였습니다.'); popupWeekendSetupVisible(false); currentYear = viewDate.year(); loadHolidays(viewDate); //refreshList(); // hcLee 2016 01 22 //applyWeekends(viewDate); //refreshHolidays(viewDate); /* if (currentYear !== null && viewDate.year() !== currentYear) { currentYear = viewDate.year(); refreshHolidays(viewDate); } else { applyWeekends(viewDate); }*/ }); } function handleWeekendPopupButtonClose() { popupWeekendSetupVisible(false); } function handleInsertPopupButtonSave() { var dbModelId; // hcLee 2015 12 23 if (visibleCancelButton() == true) { var selectedId = {}; var date = holidayModel.HolidayDate(); if (holidayModel.IsYearlyHoliday()) { dbModelId = 'CmHoliday'; selectedId.HolidayMonth = date.month() + 1; selectedId.HolidayDay = date.date(); dataViewModel = new BemsWebApplication.CmHolidayViewModel(); dataViewModel.IsLunar(holidayModel.IsLunar()); dataViewModel.HolidayMonth(selectedId.HolidayMonth); dataViewModel.HolidayDay(selectedId.HolidayDay); } else { dbModelId = 'CmHolidayCustom'; selectedId.HolidayDate = date.format(); dataViewModel = new BemsWebApplication.CmHolidayCustomViewModel(); dataViewModel.HolidayDate(holidayModel.HolidayDate()); } selectedId.SiteId = BWA.UserInfo.SiteId(); dataViewModel.SiteId(BWA.UserInfo.SiteId()); dataViewModel.Name(holidayModel.Name()); dataViewModel.IsUse(true); BemsWebApplication.db[dbModelId].update(selectedId, dataViewModel.toJS()).done(function () { popupInsertVisible(false); utils.toast.show('데이터베이스 항목 삭제 작업이 성공하였습니다.'); refreshList(); }); } else { var dataViewModel; var selectedDate = selectedDates.start; if (holidayModel.IsYearlyHoliday()) { dbModelId = 'CmHoliday'; dataViewModel = new BemsWebApplication.CmHolidayViewModel(); } else { dbModelId = 'CmHolidayCustom'; dataViewModel = new BemsWebApplication.CmHolidayCustomViewModel(); } var promised = {}; for (selectedDate = selectedDates.start ; selectedDate.isSame(selectedDates.end) === false ; selectedDate.add(1, 'days')) { if (holidayModel.IsYearlyHoliday()) { dataViewModel.IsLunar(holidayModel.IsLunar()); var month = selectedDate.month() + 1, date = selectedDate.date(); if (holidayModel.IsLunar()) { var lunar = cc.solar2lunar(new Date(selectedDate.year(), month - 1, date)); month = lunar.lMonth; date = lunar.lDay; } console.log(month + ' ' + date); dataViewModel.HolidayMonth(month); dataViewModel.HolidayDay(date); } else { dataViewModel.HolidayDate(new Date(selectedDate.year(), selectedDate.month(), selectedDate.date())); } dataViewModel.SiteId(BWA.UserInfo.SiteId()); dataViewModel.Name(holidayModel.Name()); dataViewModel.IsUse(true); promised = BemsWebApplication.db[dbModelId].insert(dataViewModel.toJS()); } promised.done(function (values, newId) { popupInsertVisible(false); refreshList(); utils.toast.show('휴일 등록 작업이 성공하였습니다.'); }).fail(function () { popupInsertVisible(false); refreshList(); }); } } function handleInsertPopupButtonClose() { popupInsertVisible(false); } var holidayWeekendSetupToolbarItems = [ { location: 'before', text: '주말 휴일 설정' }, { location: 'after', widget: 'button', options: { text: $G('save'), icon: 'save', clickAction: handleWeekendSetupPopupButtonSave, disabled: hasnotModificationPermission } }, { location: 'after', widget: 'button', options: { text: $G('close'), icon: 'close', clickAction: handleWeekendPopupButtonClose } } ]; var insertHolidayToolbarItems = [ { location: 'before', text: '주말 휴일 설정' }, // 2015 12 22 hcLee { location: 'after', widget: 'button', options: { text: $G('edit'), icon: 'edit', visible: visibleEditButton, disabled: hasnotModificationPermission, clickAction: handlePopupButtonEdit } }, { location: 'after', widget: 'button', options: { text: $G('delete'), type: 'danger', icon: 'remove', visible: visibleDeleteButton, disabled: hasnotModificationPermission, clickAction: handlePopupButtonDelete } }, { location: 'after', widget: 'button', options: { text: $G('cancel'), icon: 'cancel', visible: visibleCancelButton, clickAction: handlePopupButtonCancel } }, { location: 'after', widget: 'button', options: { text: $G('save'), icon: 'save', visible: visibleSaveButton, clickAction: handleInsertPopupButtonSave } }, { location: 'after', widget: 'button', options: { text: $G('close'), icon: 'close', clickAction: handleInsertPopupButtonClose } } ]; var popupOptions = { width: '480px', height: 'auto', visible: popupWeekendSetupVisible, closeOnOutsideClick: false, //showingAction: handlePopupShowing, //shownAction: handlePopupShown, //isEditModeInPopup:isEditModeInPopup, animation: utils.popup.createAnimation() }; var insertPopupOptions = $.extend({}, popupOptions); insertPopupOptions.visible = popupInsertVisible; return { NoSearchView: true, // 2019.07.25 kgpark 검색버튼 삭제 viewShowing: handleViewShowing, viewShown: handleViewShown, viewHidden: handleViewHidden, viewDisposing: handleViewDisposing, isEditModeInPopup: isEditModeInPopup, disableOnlyEdit: disableOnlyEdit, refreshList: refreshList, popupInsertView: popupInsertView, // 주말설정 popupOptions: popupOptions, insertPopupOptions: insertPopupOptions, holidayWeekendSetupToolbarItems: holidayWeekendSetupToolbarItems, insertHolidayToolbarItems: insertHolidayToolbarItems, dataModel: holidayWeekendDataViewModel, holidayModel: holidayModel, hasnotModificationPermission: hasnotModificationPermission }; };