f3b9f5e34d257fc5db7843b95d2f0187de53ff40.svn-base 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. BemsWebApplication.Holiday = function (params, viewInfo) {
  2. "use strict";
  3. var holidayDataSource = BemsWebApplication.db.createDataSource('CmHoliday', true, true);
  4. var holidayCustomDataSource = BemsWebApplication.db.createDataSource('CmHolidayCustom', true);
  5. var holidayWeekendDataSource = BemsWebApplication.db.createDataSource('CmHolidayWeekend', true);
  6. var holidaySources;
  7. var holidayCustomSources;
  8. var popupWeekendSetupVisible = ko.observable(false),
  9. popupInsertVisible = ko.observable(false),
  10. hasnotModificationPermission = ko.observable(true);
  11. var holidayModel = {
  12. SiteId: ko.observable(),
  13. HolidayMonth: ko.observable(),
  14. HolidayDay: ko.observable(),
  15. IsLunar: ko.observable(),
  16. HolidayDate: ko.observable(),
  17. IsYearlyHoliday: ko.observable(false),
  18. Name: ko.observable(),
  19. };
  20. var backupDataViewModel = null;
  21. var cc = new CalendarConverter;
  22. var isEditModeInPopup = ko.observable(false);
  23. var disableOnlyEdit = ko.observable(true);
  24. var visibleEditButton = ko.observable(true);
  25. var visibleDeleteButton = ko.observable(true);
  26. var visibleCancelButton = ko.observable(false);
  27. var visibleSaveButton = ko.observable(false);
  28. var popupVisible = ko.observable(false);
  29. var holidayWeekendDataViewModel = new BemsWebApplication.CmHolidayWeekendViewModel();
  30. var loadedYears = [];
  31. var viewDate = null;
  32. var currentYear = null;
  33. var holidays = [];
  34. var weekends = [];
  35. var isSaturday = false;
  36. var isSunday = false;
  37. var holidayCalendar = null;
  38. var calendarOptions = {
  39. selectable: true,
  40. editable: false,
  41. weekends: true,
  42. height: 680,
  43. select: handleSelectInCalendar,
  44. eventClick: handleEventClickInCalendar,
  45. changedMonth: function (view) {
  46. var date = view.calendar.getDate();
  47. // hcLee 2016 01 22
  48. viewDate = date;
  49. if (currentYear !== null && date.year() !== currentYear) {
  50. currentYear = date.year();
  51. //refreshHolidays(date);
  52. loadHolidays(viewDate);
  53. }
  54. else {
  55. applyWeekends(date);
  56. }
  57. }
  58. };
  59. var selectedDates = {};
  60. function handleSelectInCalendar(start, end) {
  61. if (!hasnotModificationPermission()) {
  62. var date = start.startOf('hour');
  63. var isExist = holidayCalendar.isExistEvent(date, function (i, event) {
  64. utils.toast.show('이미 등록된 휴일이 있습니다.', 'error');
  65. });
  66. if (isExist) return;
  67. holidayModel.Name("");
  68. holidayModel.HolidayDate(start);
  69. holidayModel.IsYearlyHoliday(false);
  70. // 새 아이템 등록
  71. selectedDates.start = start;
  72. selectedDates.end = end;
  73. disableOnlyEdit(false);
  74. visibleEditButton(false);
  75. visibleDeleteButton(false);
  76. visibleSaveButton(true);
  77. isEditModeInPopup(true);
  78. popupInsertVisible(true);
  79. }
  80. else {
  81. utils.toast.show('등록할 권한이 없습니다.', 'error');
  82. }
  83. }
  84. function activePopupEditMode() {
  85. //visibleCancelButton( false );
  86. // alert('activePopupEditMode');
  87. visibleEditButton(false);
  88. visibleDeleteButton(false);
  89. visibleCancelButton(true);
  90. visibleSaveButton(true);
  91. //viewModeInPopup(false);
  92. isEditModeInPopup(true);
  93. }
  94. function activePopupViewMode() {
  95. // alert('activePopupViewMode');
  96. visibleEditButton(true);
  97. visibleDeleteButton(true);
  98. visibleCancelButton(false);
  99. visibleSaveButton(false);
  100. //visibleDeleteButton(false);
  101. //viewModeInPopup(true);
  102. isEditModeInPopup(false);
  103. }
  104. popupVisible.subscribe(function (visible) {
  105. if (visible == false) {
  106. $.each(selectedId, function (name) {
  107. delete selectedId[name];
  108. });
  109. isNewInPopup(false);
  110. // visibleEditButton(true);
  111. visibleDeleteButton(true);
  112. }
  113. });
  114. function handlePopupButtonEdit() {
  115. activePopupEditMode();
  116. //visibleCancelButton(true);
  117. //holidayModel
  118. //backupDataViewModel = getBackupDataViewModel(holidayWeekendDataViewModel);
  119. backupDataViewModel = getBackupDataViewModel(holidayModel);
  120. }
  121. function getBackupDataViewModel(model) {
  122. var object = {};
  123. $.each(model, function (name, value) {
  124. if (ko.isObservable(value)) {
  125. object[name] = value();
  126. }
  127. });
  128. return object;
  129. }
  130. function restoreDataViewModelFromBackup(model, backupModel) {
  131. $.each(backupModel, function (name, value) {
  132. model[name](value);
  133. });
  134. }
  135. function handlePopupButtonCancel() {
  136. activePopupViewMode();
  137. visibleCancelButton(false);
  138. restoreDataViewModelFromBackup(holidayModel, backupDataViewModel);
  139. }
  140. function handlePopupButtonDelete() {
  141. DevExpress.ui.dialog.confirm($G('deleteConfirmMsg'), "삭제").then(function (result) {
  142. if (result) {
  143. //handleConfirmDelete();
  144. var dbModelId;
  145. var selectedId = {};
  146. var date = holidayModel.HolidayDate();
  147. if (holidayModel.IsYearlyHoliday()) {
  148. dbModelId = 'CmHoliday';
  149. selectedId.HolidayMonth = date.month() + 1;
  150. selectedId.HolidayDay = date.date();
  151. selectedId.IsLunar = holidayModel.IsLunar();
  152. if (selectedId.IsLunar) {
  153. /* var solar = cc.lunar2solar(new Date(date.year() - 1, selectedId.HolidayMonth, selectedId.HolidayDay));
  154. if (solar.sYear !== date.year()) {
  155. solar = cc.lunar2solar(new Date(date.year(), selectedId.HolidayMonth, selectedId.HolidayDay));
  156. }
  157. //year = solar.sYear;
  158. selectedId.HolidayMonth = solar.sMonth - 1;
  159. selectedId.HolidayDay = solar.sDay; */
  160. var lunar = cc.solar2lunar(new Date(date.year(), selectedId.HolidayMonth - 1, selectedId.HolidayDay));
  161. selectedId.HolidayMonth = lunar.lMonth;
  162. selectedId.HolidayDay = lunar.lDay;
  163. }
  164. }
  165. else {
  166. dbModelId = 'CmHolidayCustom';
  167. selectedId.HolidayDate = date.format();
  168. }
  169. selectedId.SiteId = BWA.UserInfo.SiteId();
  170. BemsWebApplication.db[dbModelId].remove(selectedId).done(function () {
  171. utils.toast.show('데이터베이스 항목 삭제 작업이 성공하였습니다.');
  172. refreshList();
  173. });
  174. }
  175. popupInsertVisible(false);
  176. });
  177. }
  178. function handleEventClickInCalendar(event) {
  179. if (event.isWeekend === true) {
  180. utils.toast.show('주말은 "주말 설정" 버튼을 통해 설정하시기 바랍니다.', 'warning');
  181. return;
  182. }
  183. //기존 아이템 선택시
  184. disableOnlyEdit(true);
  185. holidayModel.Name(event.title);
  186. holidayModel.IsLunar(event.isLunar);
  187. if (event.isYearlyHoliday == undefined) holidayModel.IsYearlyHoliday(false);
  188. else holidayModel.IsYearlyHoliday(true);
  189. holidayModel.HolidayDate(event.start);
  190. //activePopupEditMode();
  191. activePopupViewMode();
  192. /* visibleEditButton(true);
  193. visibleDeleteButton(true);
  194. visibleCancelButton(false);
  195. visibleSaveButton(false); */
  196. popupInsertVisible(true);
  197. return;
  198. /*
  199. utils.dialog.confirm($G('deleteConfirmMsg'), event.isYearlyHoliday ? '정기휴일 삭제' : '특정휴일 삭제', function(result) {
  200. if (result) {
  201. var dbModelId;
  202. var selectedId = {};
  203. var date = event.start;
  204. if (event.isYearlyHoliday) {
  205. dbModelId = 'CmHoliday';
  206. selectedId.HolidayMonth = date.month() + 1;
  207. selectedId.HolidayDay = date.date();
  208. }
  209. else {
  210. dbModelId = 'CmHolidayCustom';
  211. selectedId.HolidayDate = date.format();
  212. }
  213. selectedId.SiteId = BWA.UserInfo.SiteId();
  214. BemsWebApplication.db[dbModelId].remove(selectedId).done(function() {
  215. utils.toast.show('데이터베이스 항목 삭제 작업이 성공하였습니다.');
  216. refreshList();
  217. });
  218. }
  219. else {
  220. // toast
  221. }
  222. });
  223. */
  224. }
  225. function refreshList() {
  226. /*
  227. // hcLee 2016 01 22
  228. var date = moment();
  229. currentYear = date.year();
  230. loadHolidays(date);
  231. hcLee 2016 03 10 */
  232. currentYear = viewDate.year();
  233. loadHolidays(viewDate);
  234. }
  235. // hcLee 2016 10 18 추가
  236. var initialized = false;
  237. function handleViewShowing() {
  238. //권한설정
  239. hasnotModificationPermission(!BWA.UserInfo.hasPermissionOfModification(viewInfo.viewName));
  240. }
  241. function handleViewShown() {
  242. if (initialized === false) { // hcLee 2016 10 18 추가
  243. holidayCalendar = utils.fullCalendar.create('holidayCalendar', calendarOptions);
  244. var date = moment();
  245. currentYear = date.year();
  246. loadHolidays(date);
  247. $SearchView.setPopupVisibleObservable(null);
  248. $SideMenu.showSideMenuIfWill(params.view);
  249. initialized = true;
  250. }
  251. else
  252. refreshList(); // hcLee 2016 10 18 추가
  253. }
  254. function refreshHolidays(date) {
  255. holidays = [];
  256. var currentYear = date.year();
  257. if (loadedYears.indexOf(currentYear) < 0) {
  258. loadedYears.push(currentYear);
  259. var year;
  260. var month;
  261. var day;
  262. var solar;
  263. $.each(holidaySources, function (i, item) {
  264. year = currentYear;
  265. month = item.HolidayMonth() - 1;
  266. day = item.HolidayDay();
  267. if (item.IsLunar()) {
  268. solar = cc.lunar2solar(new Date(year - 1, month, day));
  269. if (solar.sYear !== year) {
  270. solar = cc.lunar2solar(new Date(year, month, day));
  271. }
  272. year = solar.sYear;
  273. month = solar.sMonth - 1;
  274. day = solar.sDay;
  275. }
  276. utils.holiday.pushHolidayEventInArray(holidays, item.Name(), moment([year, month, day]), true, undefined, 0, item.IsLunar());
  277. });
  278. $.each(holidayCustomSources, function (i, item) {
  279. var itemDate = item.HolidayDate();
  280. if (itemDate.getFullYear() === currentYear) {
  281. utils.holiday.pushHolidayEventInArray(holidays, item.Name(), moment([itemDate.getFullYear(), itemDate.getMonth(), itemDate.getDate()]), false, undefined, 1, false);
  282. }
  283. });
  284. }
  285. applyWeekends(date);
  286. }
  287. function loadHolidays(date) {
  288. holidays = [];
  289. loadedYears = [];
  290. $.when(holidayDataSource.load(), holidayCustomDataSource.load(), holidayWeekendDataSource.load())
  291. .done(function (result1, result2, result3) {
  292. holidaySources = result1[0];
  293. holidayCustomSources = result2[0];
  294. if (result3.length > 0) {
  295. var weekend = result3[0][0];
  296. isSaturday = weekend.Saturday();
  297. isSunday = weekend.Sunday();
  298. holidayWeekendDataViewModel.SiteId(weekend.SiteId());
  299. holidayWeekendDataViewModel.Saturday(weekend.Saturday());
  300. holidayWeekendDataViewModel.Sunday(weekend.Sunday());
  301. }
  302. refreshHolidays(date);
  303. });
  304. }
  305. function applyWeekends(date) {
  306. if (holidayCalendar === null) return;
  307. if (isSunday == false && isSaturday == false) {
  308. holidayCalendar.updateEvents(holidays);
  309. return;
  310. }
  311. weekends = utils.holiday.getWeekendHolidays(date, isSaturday, isSunday);
  312. //var month = date.clone().startOf( 'month' );
  313. //var sunday = month.clone(),
  314. // saturday = month.clone();
  315. //weekends = [];
  316. //if( month.day() !== 0 ) {
  317. // sunday.day( -7 );
  318. //}
  319. //if( month.day() !== 6 ) {
  320. // saturday.day( -1 );
  321. //}
  322. //for( var i = 0 ; i < 7 ; i++ ) {
  323. // if( isSunday ) {
  324. // holidayCalendar.pushEventInArray( weekends, '주말 휴일', moment( [ sunday.year(), sunday.month(), sunday.date() ], null, true, 2 ));
  325. // sunday.day( 7 );
  326. // }
  327. // if( isSaturday ) {
  328. // holidayCalendar.pushEventInArray( weekends, '주말 휴일', moment( [ saturday.year(), saturday.month(), saturday.date() ], null, true, 2 ));
  329. // saturday.day( 13 );
  330. // }
  331. //}
  332. holidayCalendar.updateEvents(holidays.concat(weekends));
  333. }
  334. function handleModification() {
  335. //shouldReload = true;
  336. }
  337. function handleViewHidden() {
  338. holidayCalendar.clearEvents(true);
  339. }
  340. function handleViewDisposing() {
  341. BemsWebApplication.db.CmHolidayWeekend.modified.remove(handleModification);
  342. }
  343. BemsWebApplication.db.CmHolidayWeekend.modified.add(handleModification);
  344. function popupInsertView() {
  345. popupWeekendSetupVisible(true);
  346. }
  347. function handleWeekendSetupPopupButtonSave() {
  348. holidayWeekendDataViewModel.IsUse(true);
  349. var keys = BWA.db.extractKeysObject('CmHolidayWeekend', holidayWeekendDataViewModel);
  350. BemsWebApplication.db.CmHolidayWeekend.update(keys, holidayWeekendDataViewModel.toJS()).done(function () {
  351. utils.toast.show('데이터베이스 항목 수정 작업이 성공하였습니다.');
  352. popupWeekendSetupVisible(false);
  353. currentYear = viewDate.year();
  354. loadHolidays(viewDate);
  355. //refreshList();
  356. // hcLee 2016 01 22
  357. //applyWeekends(viewDate);
  358. //refreshHolidays(viewDate);
  359. /*
  360. if (currentYear !== null && viewDate.year() !== currentYear) {
  361. currentYear = viewDate.year();
  362. refreshHolidays(viewDate);
  363. }
  364. else {
  365. applyWeekends(viewDate);
  366. }*/
  367. });
  368. }
  369. function handleWeekendPopupButtonClose() {
  370. popupWeekendSetupVisible(false);
  371. }
  372. function handleInsertPopupButtonSave() {
  373. var dbModelId;
  374. // hcLee 2015 12 23
  375. if (visibleCancelButton() == true) {
  376. var selectedId = {};
  377. var date = holidayModel.HolidayDate();
  378. if (holidayModel.IsYearlyHoliday()) {
  379. dbModelId = 'CmHoliday';
  380. selectedId.HolidayMonth = date.month() + 1;
  381. selectedId.HolidayDay = date.date();
  382. dataViewModel = new BemsWebApplication.CmHolidayViewModel();
  383. dataViewModel.IsLunar(holidayModel.IsLunar());
  384. dataViewModel.HolidayMonth(selectedId.HolidayMonth);
  385. dataViewModel.HolidayDay(selectedId.HolidayDay);
  386. }
  387. else {
  388. dbModelId = 'CmHolidayCustom';
  389. selectedId.HolidayDate = date.format();
  390. dataViewModel = new BemsWebApplication.CmHolidayCustomViewModel();
  391. dataViewModel.HolidayDate(holidayModel.HolidayDate());
  392. }
  393. selectedId.SiteId = BWA.UserInfo.SiteId();
  394. dataViewModel.SiteId(BWA.UserInfo.SiteId());
  395. dataViewModel.Name(holidayModel.Name());
  396. dataViewModel.IsUse(true);
  397. BemsWebApplication.db[dbModelId].update(selectedId, dataViewModel.toJS()).done(function () {
  398. popupInsertVisible(false);
  399. utils.toast.show('데이터베이스 항목 삭제 작업이 성공하였습니다.');
  400. refreshList();
  401. });
  402. }
  403. else {
  404. var dataViewModel;
  405. var selectedDate = selectedDates.start;
  406. if (holidayModel.IsYearlyHoliday()) {
  407. dbModelId = 'CmHoliday';
  408. dataViewModel = new BemsWebApplication.CmHolidayViewModel();
  409. }
  410. else {
  411. dbModelId = 'CmHolidayCustom';
  412. dataViewModel = new BemsWebApplication.CmHolidayCustomViewModel();
  413. }
  414. var promised = {};
  415. for (selectedDate = selectedDates.start ; selectedDate.isSame(selectedDates.end) === false ; selectedDate.add(1, 'days')) {
  416. if (holidayModel.IsYearlyHoliday()) {
  417. dataViewModel.IsLunar(holidayModel.IsLunar());
  418. var month = selectedDate.month() + 1,
  419. date = selectedDate.date();
  420. if (holidayModel.IsLunar()) {
  421. var lunar = cc.solar2lunar(new Date(selectedDate.year(), month - 1, date));
  422. month = lunar.lMonth;
  423. date = lunar.lDay;
  424. }
  425. console.log(month + ' ' + date);
  426. dataViewModel.HolidayMonth(month);
  427. dataViewModel.HolidayDay(date);
  428. }
  429. else {
  430. dataViewModel.HolidayDate(new Date(selectedDate.year(), selectedDate.month(), selectedDate.date()));
  431. }
  432. dataViewModel.SiteId(BWA.UserInfo.SiteId());
  433. dataViewModel.Name(holidayModel.Name());
  434. dataViewModel.IsUse(true);
  435. promised = BemsWebApplication.db[dbModelId].insert(dataViewModel.toJS());
  436. }
  437. promised.done(function (values, newId) {
  438. popupInsertVisible(false);
  439. refreshList();
  440. utils.toast.show('휴일 등록 작업이 성공하였습니다.');
  441. }).fail(function () {
  442. popupInsertVisible(false);
  443. refreshList();
  444. });
  445. }
  446. }
  447. function handleInsertPopupButtonClose() {
  448. popupInsertVisible(false);
  449. }
  450. var holidayWeekendSetupToolbarItems = [
  451. { location: 'before', text: '주말 휴일 설정' },
  452. { location: 'after', widget: 'button', options: { text: $G('save'), icon: 'save', clickAction: handleWeekendSetupPopupButtonSave, disabled: hasnotModificationPermission } },
  453. { location: 'after', widget: 'button', options: { text: $G('close'), icon: 'close', clickAction: handleWeekendPopupButtonClose } }
  454. ];
  455. var insertHolidayToolbarItems = [
  456. { location: 'before', text: '주말 휴일 설정' },
  457. // 2015 12 22 hcLee
  458. { location: 'after', widget: 'button', options: { text: $G('edit'), icon: 'edit', visible: visibleEditButton, disabled: hasnotModificationPermission, clickAction: handlePopupButtonEdit } },
  459. { location: 'after', widget: 'button', options: { text: $G('delete'), type: 'danger', icon: 'remove', visible: visibleDeleteButton, disabled: hasnotModificationPermission, clickAction: handlePopupButtonDelete } },
  460. { location: 'after', widget: 'button', options: { text: $G('cancel'), icon: 'cancel', visible: visibleCancelButton, clickAction: handlePopupButtonCancel } },
  461. { location: 'after', widget: 'button', options: { text: $G('save'), icon: 'save', visible: visibleSaveButton, clickAction: handleInsertPopupButtonSave } },
  462. { location: 'after', widget: 'button', options: { text: $G('close'), icon: 'close', clickAction: handleInsertPopupButtonClose } }
  463. ];
  464. var popupOptions = {
  465. width: '480px',
  466. height: 'auto',
  467. visible: popupWeekendSetupVisible,
  468. closeOnOutsideClick: false,
  469. //showingAction: handlePopupShowing,
  470. //shownAction: handlePopupShown,
  471. //isEditModeInPopup:isEditModeInPopup,
  472. animation: utils.popup.createAnimation()
  473. };
  474. var insertPopupOptions = $.extend({}, popupOptions);
  475. insertPopupOptions.visible = popupInsertVisible;
  476. return {
  477. NoSearchView: true, // 2019.07.25 kgpark 검색버튼 삭제
  478. viewShowing: handleViewShowing,
  479. viewShown: handleViewShown,
  480. viewHidden: handleViewHidden,
  481. viewDisposing: handleViewDisposing,
  482. isEditModeInPopup: isEditModeInPopup,
  483. disableOnlyEdit: disableOnlyEdit,
  484. refreshList: refreshList,
  485. popupInsertView: popupInsertView, // 주말설정
  486. popupOptions: popupOptions,
  487. insertPopupOptions: insertPopupOptions,
  488. holidayWeekendSetupToolbarItems: holidayWeekendSetupToolbarItems,
  489. insertHolidayToolbarItems: insertHolidayToolbarItems,
  490. dataModel: holidayWeekendDataViewModel,
  491. holidayModel: holidayModel,
  492. hasnotModificationPermission: hasnotModificationPermission
  493. };
  494. };