f296847db2e5ca8d2e64c691d610125570a9f741.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. BemsWebApplication.EquipRentInfo = function (params, viewInfo) {
  2. "use strict";
  3. var codeTypeDataSource = BemsWebApplication.db.createDataSource('FmsEquipmentCodeType', true, false, true),
  4. codeStateTypeDataSource = BemsWebApplication.db.createDataSource('FmsEquipmentCodeStateType', false, false, true),
  5. warehouseDataSource = BemsWebApplication.db.createDataSource('FmsMaterialWarehouse', true, false, true);
  6. var equipmentFactory = BWA.Factory.Equipment;
  7. var equipmentsDataSource = equipmentFactory.getBasicDataSourceRef({
  8. dbModeIId: 'FmsEquipmentHistory',
  9. primaryKeyDataField: 'EquipmentHistoryId'
  10. });
  11. var codeTypes = ko.observableArray(),
  12. codeStateTypes = ko.observableArray(),
  13. warehouses = ko.observableArray();
  14. var equipments = ko.observableArray(),
  15. equipmentsInView = ko.observableArray();
  16. var equipmentGridView;
  17. var equipmentDataGridOptions,
  18. modifiableEquipmentDataGridOptions = undefined;
  19. var selectedUserData = ko.observable();
  20. var codeTypesForSearch = ko.observableArray(),
  21. codeStateTypesForSearch = ko.observableArray(),
  22. warehousesForSearch = ko.observableArray();
  23. var deferredForSearch = new $.Deferred();
  24. var eq = BWA.DataUtil.constructEqualFilter,
  25. and = BWA.DataUtil.andFilter;
  26. selectedUserData.subscribe(function (value) {
  27. viewModel.dataModel.RentUser(value.Name);
  28. });
  29. var viewModel;
  30. var viewOptions = {
  31. dataSourceOptions: {
  32. select: [
  33. 'SiteId',
  34. 'EquipmentRentId',
  35. 'RegisterUserId',
  36. 'RentUser',
  37. 'Title',
  38. 'RentDate',
  39. 'ReturnDueDate',
  40. 'ReturnFixDate',
  41. 'EquipmentStateTypeId',
  42. 'Comment',
  43. 'CmUser/Name', // 등록자
  44. 'FmsEquipmentCodeStateType/Name'
  45. ],
  46. expand: [
  47. 'CmUser', 'CmUser1',
  48. 'FmsEquipmentCodeStateType',
  49. 'FmsEquipmentHistory'
  50. ],
  51. extendOptions: {
  52. forceOriginalField: true,
  53. },
  54. filter: [
  55. ['SiteId', '=', BWA.UserInfo.SiteId()],
  56. 'and',
  57. ['EquipmentStateTypeId', '=', 2]
  58. ],
  59. },
  60. columns: [
  61. { dataField: 'EquipmentRentId', caption: $G('number'), width: '10%', alignment: 'center', sortOrder: 'desc' },
  62. { dataField: 'Title', caption: '대여신청명', width: '40%', alignment: 'center' },
  63. { dataField: 'Comment', caption: '대여사유', width: '40%', alignment: 'center' },
  64. { dataField: 'RentUser', caption: '대여자', width: '20%', alignment: 'center' },
  65. {
  66. dataField: 'RentDate', caption: '대여일', width: '20%', alignment: 'center', dataType: "shortDate", format: "yyyy-MM-dd",
  67. customizeText: function (cellInfo) {
  68. return cellInfo.valueText;
  69. }
  70. },
  71. {
  72. dataField: 'ReturnDueDate', caption: '반납예정일', width: '20%', alignment: 'center', dataType: "shortDate", format: "yyyy-MM-dd",
  73. customizeText: function (cellInfo) {
  74. return cellInfo.valueText;
  75. }
  76. },
  77. { dataField: 'CmUser/Name', caption: '관리자', width: '20%', alignment: 'center', visible: false },
  78. ],
  79. paging: {
  80. pageSize: 18,
  81. enabled: true
  82. },
  83. pager: {},
  84. "export": {
  85. enabled: true,
  86. fileName: '공기구 대여 목록',
  87. },
  88. onExporting: function (e) {
  89. e.component.columnOption("EquipmentRentId", "visible", false);
  90. },
  91. onExported: function (e) {
  92. e.component.columnOption("EquipmentRentId", "visible", true);
  93. },
  94. sort: { field: "EquipmentRentId", desc: true },
  95. searchViewItems: [
  96. { id: 'Title' },
  97. { id: 'RentUser' },
  98. { id: 'RentDate', type: 'dateRange', isOnlyDate: true },
  99. { id: 'ReturnDueDate', type: 'dateRange', isOnlyDate: true }
  100. ],
  101. popupWidth: 1200,
  102. handleViewShowing: function (dataModel) {
  103. $.when(
  104. codeStateTypeDataSource.load(),
  105. codeTypeDataSource.load(),
  106. warehouseDataSource.load()
  107. ).done(function (codeStateTypeResult, codeTypeResult, warehouseResult) {
  108. codeStateTypes(codeStateTypeResult[0]);
  109. codeTypes(codeTypeResult[0]);
  110. warehouses(warehouseResult[0]);
  111. codeStateTypesForSearch($SearchView.createArrayOfSelectBox('EquipmentStateTypeId', codeStateTypes()));
  112. codeTypesForSearch($SearchView.createArrayOfSelectBox('EquipmentTypeId', codeTypes()));
  113. warehousesForSearch($SearchView.createArrayOfSelectBox('WarehouseId', warehouses()));
  114. deferredForSearch.resolve();
  115. });
  116. },
  117. handleViewShown: function (dataModel) {
  118. },
  119. handleDataGridRowClick: function (id, dataGrid, clickRow, popupVisible) {
  120. dataGrid.clearSelection();
  121. var data = clickRow.data;
  122. var dataModel = viewModel.dataModel;
  123. if (data['RentUser'] !== undefined) {
  124. dataModel.RentUser = $KoSet(dataModel.RentUser, data['RentUser']());
  125. }
  126. popupVisible(true);
  127. },
  128. handleAfterLoadingModelByKey: function (data, dataViewModel) {
  129. equipmentsDataSource.filter([
  130. eq('SiteId', dataViewModel.SiteId()),
  131. and,
  132. eq('EquipmentRentId', dataViewModel.EquipmentRentId()),
  133. and,
  134. ['IsReturned', '=', false]
  135. ]);
  136. equipmentsDataSource.load().done(function (dbEquipments) {
  137. equipmentsInView(dbEquipments);
  138. });
  139. },
  140. handlePopupShowing: function (isNewInPopup, dataViewModel) {
  141. $("#gridContainer2").dxDataGrid({
  142. paging: {
  143. pageSize: 9,
  144. enabled: true
  145. }
  146. });
  147. $("#gridContainer3").dxDataGrid({
  148. paging: {
  149. pageSize: 9,
  150. enabled: true
  151. }
  152. });
  153. // 저장 버튼
  154. var userInfo = BWA.UserInfo;
  155. if (isNewInPopup()) { // 신규등록인 경우
  156. dataViewModel.SiteId(userInfo.SiteId());
  157. dataViewModel.EquipmentStateTypeId(2);
  158. dataViewModel.RentDate($G.date(new Date()));
  159. dataViewModel.RegisterUserId(userInfo.UserId());
  160. dataViewModel.Comment('');
  161. }
  162. },
  163. handlePopupShown: function (isEditMode, isNew, dataViewModel) {
  164. }
  165. };
  166. viewModel = BWA.DataGrid.createViewWithDataGrid(params, viewInfo, 'FmsEquipmentRentInfo', viewOptions);
  167. var equipPopupToolbarItems = [
  168. { location: 'before', text: '공기구 대여' },
  169. {
  170. location: 'after', widget: 'button', options: {
  171. text: '공기구추가', icon: 'search', visible: viewModel.isEditModeInPopup,
  172. clickAction: function () { viewModel.equipmentSearchPopup.show(); }
  173. }
  174. },
  175. {
  176. location: 'after', widget: 'button', options: {
  177. text: '공기구삭제', icon: 'remove', type: 'danger', visible: viewModel.isEditModeInPopup,
  178. clickAction: function () { handleDeleteSelectedEquipments(); }
  179. }
  180. },
  181. {
  182. location: 'after', widget: 'button', options: {
  183. text: $G('save'), icon: 'save', visible: viewModel.isEditModeInPopup,
  184. clickAction: handlePopupButtonSave
  185. }
  186. },
  187. {
  188. location: 'after', widget: 'button', options: {
  189. text: $G('close'), icon: 'close',
  190. clickAction: equipPopupButtonClose
  191. }
  192. }
  193. ];
  194. equipmentDataGridOptions = utils.datagrid.defaultOptions({
  195. width: 700,
  196. pager: {
  197. showPageSizeSelector: false,
  198. allowedPageSizes: []
  199. },
  200. dataSource: equipmentsInView,
  201. columns: [
  202. { dataField: 'Name', caption: '공기구명', width: '35%', alignment: 'center' },
  203. { dataField: 'FmsEquipment/Standard', caption: '규격', width: '35%', alignment: 'center' },
  204. { dataField: 'FmsEquipment/FmsMaterialWarehouse/Name', caption: '창고', width: '15%', alignment: 'center' },
  205. // Detail 에서는 보유수량을 표시하지 않음. 사용자의 혼란 방지
  206. { dataField: 'CurrentRentCount', caption: '대여수량', width: '15%', alignment: 'center', format: 'currency' }
  207. ],
  208. });
  209. modifiableEquipmentDataGridOptions = utils.datagrid.defaultOptions({
  210. width: 700,
  211. pager: {
  212. showPageSizeSelector: false,
  213. allowedPageSizes: []
  214. },
  215. dataSource: equipments,
  216. selection: { mode: 'multiple', allowSelectAll: true },
  217. columns: [
  218. { dataField: 'Name', caption: '공기구명', width: '30%', alignment: 'center' },
  219. { dataField: 'Standard', caption: '규격', width: '30%', alignment: 'center' },
  220. { dataField: 'WarehouseName', caption: '창고', width: '13%', alignment: 'center' },
  221. { dataField: 'CurrentStockCount', caption: '보유수량', width: '13%', alignment: 'center', format: 'currency' },
  222. {
  223. dataField: 'CurrentRentCount', caption: '대여수량', width: '13%', alignment: 'center', format: 'currency',
  224. cellTemplate: utils.datagrid.getNumberBoxDataGridTemplateFunc({
  225. min: 1,
  226. max: 1000000,
  227. dataField: 'CurrentRentCount',
  228. convertValueWhenValueChanged: function (value) {
  229. return Math.max(1, value);
  230. }
  231. })
  232. },
  233. ],
  234. cellClick: function (e) {
  235. utils.datagrid.cellClickEventForDataGridTemplate(e, 'CurrentRentCount');
  236. },
  237. contentReadyAction: function (e) {
  238. equipmentGridView = e.component;
  239. },
  240. });
  241. function handleDeleteSelectedEquipments() {
  242. var rows = equipmentGridView.getSelectedRowsData();
  243. if (_.isEmpty(rows)) {
  244. utils.toast.show('선택된 자재가 없습니다.', 'error');
  245. return;
  246. }
  247. _.each(rows, function (row) {
  248. var array = equipments();
  249. var length = array.length;
  250. var id = row.EquipmentId();
  251. for (var i = 0 ; i < length ; i++) {
  252. if (id === array[i].EquipmentId()) {
  253. equipments().splice(i, 1);
  254. break;
  255. }
  256. }
  257. });
  258. equipmentGridView.refresh();
  259. }
  260. function equipPopupButtonClose() {
  261. viewModel.popupVisible(false);
  262. }
  263. // 편집모드로 오픈시 데이터 복사
  264. viewModel.isEditModeInPopup.subscribe(function (isEditMode) {
  265. if (isEditMode) { equipments(equipmentsInView.slice(0)); }
  266. });
  267. viewModel.popupVisible.subscribe(function (visible) {
  268. if (!visible) {
  269. // 데이터 초기화
  270. equipmentsInView([]);
  271. equipments([]);
  272. }
  273. });
  274. viewModel.userSearchPopup = $UserSearchPopup(viewModel, selectedUserData);
  275. viewModel.equipmentSearchPopup = BWA.Popup.Equipment.create(viewModel, {
  276. isMultipleSelect: true,
  277. onSelectedEquipments: function (selectedEquipments) {
  278. var equipmentRentId = viewModel.dataModel.EquipmentRentId();
  279. _.each(selectedEquipments, function (sm) {
  280. // JD : 팝업에서 전체선택시 추가로 하면 오류 발생함. 이에 대한 회피 코드로 try-catch
  281. // JD : 근본적인 원인은 시간 관계상 차후로 넘김
  282. var found = _.some(equipments(), function (m) {
  283. try {
  284. return m.EquipmentId() === sm.EquipmentId();
  285. } catch (e) {
  286. return m.EquipmentId() === sm.EquipmentId;
  287. }
  288. });
  289. if (found) return;
  290. // TODO : 여기서 부가적인 값들을 추가해주어야 함
  291. try {
  292. equipments.push({
  293. SiteId: ko.observable(sm.SiteId()),
  294. EquipmentId: ko.observable(sm.EquipmentId()),
  295. Name: ko.observable(sm.Name()),
  296. WarehouseName: ko.observable(sm.WarehouseName()),
  297. //Name2: ko.observable(sm.),
  298. CurrentStockCount: ko.observable(sm.CurrentStockCount()),
  299. CurrentRentCount: ko.observable(1),
  300. Standard: ko.observable(sm.Standard())
  301. });
  302. } catch (e) {
  303. equipments.push({
  304. SiteId: ko.observable(sm.SiteId),
  305. EquipmentId: ko.observable(sm.EquipmentId),
  306. Name: ko.observable(sm.Name),
  307. WarehouseName: ko.observable(sm.WarehouseName),
  308. CurrentStockCount: ko.observable(sm.CurrentStockCount),
  309. CurrentRentCount: ko.observable(1),
  310. Standard: ko.observable(sm.Standard)
  311. });
  312. }
  313. });
  314. equipmentGridView.refresh();
  315. }
  316. });
  317. viewModel.buttonSearchMainContactorUser = function (e) {
  318. viewModel.userSearchPopup.show();
  319. }
  320. function handlePopupButtonSave() {
  321. // 더블클릭문제로 인한 방어코드
  322. if (!viewModel.popupVisible()) { return; }
  323. var dataViewModel = viewModel.dataModel;
  324. // 저장버튼을 오버라이딩 하였으므로 mandatory 필드가 적용되지 않음. SelfValidation
  325. if (_.isEmpty(dataViewModel.Title())) {
  326. utils.toast.show('값을 입력해 주십시오: 제목 ', 'error');
  327. return;
  328. }
  329. if (dataViewModel.ReturnDueDate() == null) {
  330. utils.toast.show('값을 입력해 주십시오: 반납예정일 ', 'error');
  331. return;
  332. }
  333. if (_.isEmpty(dataViewModel.RentUser())) {
  334. utils.toast.show('값을 입력해 주십시오: 대여자 ', 'error');
  335. return;
  336. }
  337. if (_.isEmpty(equipments())) {
  338. utils.toast.show('불출요청한 공기구가 없습니다.', 'error');
  339. return;
  340. }
  341. if ($G.date(dataViewModel.ReturnDueDate()) < dataViewModel.RentDate()) {
  342. utils.toast.show('반납예정일을 대여일 이후로 설정하시기 바랍니다.', 'error');
  343. return;
  344. }
  345. var tempEquipmentName;
  346. // 여기서 대여수량과 불출수량 카운트를 각각 확인하여 정상인 경우만...
  347. var foundExcess = _.some(equipments(), function (m) {
  348. tempEquipmentName = m.Name();
  349. return m.CurrentStockCount() < m.CurrentRentCount();
  350. });
  351. if (foundExcess) {
  352. utils.toast.show(tempEquipmentName + '의 대여가능 수량이 초과되었습니다.', 'error');
  353. return;
  354. }
  355. var dataViewModel = viewModel.dataModel;
  356. if (viewModel.isNewInPopup()) { // insert 인 경우
  357. BWA.db.FmsEquipmentRentInfo.insert(dataViewModel.toJS()).done(function (values, newId) {
  358. var array = equipments();
  359. var postData;
  360. var parameters = {
  361. SiteId: newId.SiteId,
  362. EquipmentRentId: newId.EquipmentRentId
  363. };
  364. postData = _.map(array, function (m) {
  365. return {
  366. SiteId: newId.SiteId,
  367. EquipmentId: m.EquipmentId(),
  368. EquipmentRentId: newId.EquipmentRentId,
  369. CurrentRentCount: m.CurrentRentCount(),
  370. EquipmentStateTypeId: 2 // 출고
  371. // TODO : 필요하다면 아래에 데이터 추가 필요
  372. };
  373. });
  374. BWA.api.post('FmsEquipmentRentInfo/InsertEquipments', postData, parameters).done(
  375. function () {
  376. viewModel.popupVisible(false);
  377. viewModel.gridView().refresh();
  378. utils.toast.show($G('successDatabaseInsertionMsg'));
  379. });
  380. });
  381. }
  382. else { // update 경우
  383. var keys = BWA.db.extractKeysObject('FmsEquipmentRentInfo', dataViewModel);
  384. BWA.db.FmsEquipmentRentInfo.update(keys, dataViewModel.toJS()).done(function (res) {
  385. var array = equipments();
  386. var postData;
  387. var parameters = {
  388. SiteId: res.SiteId,
  389. EquipmentRentId: res.EquipmentRentId
  390. };
  391. postData = _.map(array, function (m) {
  392. return {
  393. SiteId: res.SiteId,
  394. EquipmentId: m.EquipmentId(),
  395. EquipmentRentId: res.EquipmentRentId,
  396. CurrentRentCount: m.CurrentRentCount(),
  397. EquipmentStateTypeId: 2 // 출고
  398. // TODO : 필요하다면 아래에 데이터 추가 필요
  399. };
  400. });
  401. BWA.api.post('FmsEquipmentRentInfo/UpdateEquipments', postData, parameters).done(
  402. function () {
  403. viewModel.popupVisible(false);
  404. viewModel.gridView().refresh();
  405. utils.toast.show($G('successDatabaseUpdateMsg'));
  406. });
  407. });
  408. }
  409. };
  410. viewModel.equipPopupToolbarItems = equipPopupToolbarItems;
  411. viewModel.dataModel.RentUser = ko.observable();
  412. viewModel.equipmentDataGridOptions = equipmentDataGridOptions;
  413. viewModel.modifiableEquipmentDataGridOptions = modifiableEquipmentDataGridOptions;
  414. viewModel.codeTypes = codeTypes;
  415. viewModel.codeStateTypes = codeStateTypes;
  416. viewModel.warehouses = warehouses;
  417. return viewModel;
  418. };