se_bonus.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. 'use strict';
  2. /**
  3. * 奖罚金
  4. *
  5. * @author Mai
  6. * @date 2020/2/12
  7. * @version
  8. */
  9. const isPre = function (data) {
  10. return data && data.sid !== stageId;
  11. };
  12. $(document).ready(() => {
  13. autoFlashHeight();
  14. let datepicker;
  15. const spreadSetting = {
  16. cols: [
  17. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 235, formatter: '@', readOnly: isPre, },
  18. {title: '类型', colSpan: '1', rowSpan: '1', field: 'b_type', hAlign: 0, width: 40, formatter: '@', readOnly: isPre, },
  19. {title: '金额', colSpan: '1', rowSpan: '1', field: 'tp', hAlign: 2, width: 100, type: 'Number', readOnly: isPre, },
  20. {
  21. title: '时间', colSpan: '1', rowSpan: '1', field: 'real_time', hAlign: 2, width: 150, readOnly: true,
  22. formatter: 'yyyy-MM-dd', cellType: 'activeImageBtn', normalImg: '#ellipsis-icon', indent: 5,
  23. showImage: function (data) {
  24. return data !== undefined && data !== null;
  25. },
  26. },
  27. {title: '编号', colSpan: '1', rowSpan: '1', field: 'code', hAlign: 0, width: 150, formatter: '@', readOnly: isPre, },
  28. {title: '发文单位', colSpan: '1', rowSpan: '1', field: 'doc_co', hAlign: 0, width: 150, formatter: '@', readOnly: isPre},
  29. {
  30. title: '依据材料证明', colSpan: '1', rowSpan: '1', field: 'proof_file', hAlign: 1, width: 80, formatter: '@',
  31. readOnly: true, cellType: 'imageBtn', normalImg: '#rela-file-icon', hoverImg: '#rela-file-hover',
  32. getValue: function (data) {
  33. return data.proof_file ? data.proof_file.length : 0;
  34. },
  35. showImage: function (data) {
  36. return data !== undefined && data !== null;
  37. },
  38. },
  39. // {title: '依据材料证明', colSpan: '1', rowSpan: '1', field: 'proof', hAlign: 0, width: 150, formatter: '@', readOnly: isPre},
  40. {
  41. title: '计量期', colSpan: '1', rowSpan: '1', field: 'sorder', hAlign: 1, width: 100, formatter: '@',
  42. getValue: function (data) {
  43. return '第' + data.sorder + '期';
  44. }, readOnly: true,
  45. },
  46. {title: '备注', colSpan: '1', rowSpan: '1', field: 'memo', hAlign: 0, width: 180, formatter: '@', cellType: 'ellipsisAutoTip', readOnly: isPre, }
  47. ],
  48. emptyRows: readOnly ? 0 : 3,
  49. headRows: 1,
  50. headRowHeight: [32],
  51. defaultRowHeight: 21,
  52. headerFont: '12px 微软雅黑',
  53. font: '12px 微软雅黑',
  54. readOnly: readOnly,
  55. imageClick: function (data, hitinfo) {
  56. if (!data) return;
  57. const setting = hitinfo.sheet.zh_setting;
  58. if (!setting) return;
  59. const col = setting.cols[hitinfo.col];
  60. if (!col) return;
  61. if (col.field === 'proof_file') {
  62. fileObj.generateFilesHtml(data);
  63. $('#file').modal('show');
  64. }
  65. if (col.field === 'real_time' && !readOnly) {
  66. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  67. if (!datepicker) {
  68. datepicker = $('.datepicker-here').datepicker({
  69. language: 'zh',
  70. dateFormat: 'yyyy-MM-dd',
  71. autoClose: true,
  72. onSelect: function (formattedDate, date, inst) {
  73. if (!inst.visible) return;
  74. const sels = hitinfo.sheet.getSelections();
  75. if (!sels || !sels[0]) return;
  76. const node = SpreadJsObj.getSelectObject(hitinfo.sheet);
  77. const uData = { update: {id: node.id, real_time: date} };
  78. postData(window.location.pathname + '/update', uData, function (result) {
  79. bonusObj.loadUpdateData(result);
  80. SpreadJsObj.reLoadRowData(hitinfo.sheet, sels[0].row);
  81. }, function () {
  82. SpreadJsObj.reLoadRowData(hitinfo.sheet, sels[0].row);
  83. });
  84. }
  85. }).data('datepicker');
  86. }
  87. const value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  88. if (value) {
  89. datepicker.selectDate(value);
  90. } else {
  91. datepicker.clear();
  92. }
  93. datepicker.show();
  94. $('#datepickers-container').css('top', hitinfo.cellRect.y + pos.y).css('left', hitinfo.cellRect.x + pos.x);
  95. }
  96. }
  97. };
  98. const bonusSpread = SpreadJsObj.createNewSpread($('#bonus-spread')[0]);
  99. const bonusSheet = bonusSpread.getActiveSheet();
  100. spreadSetting.readOnly = readOnly;
  101. SpreadJsObj.initSheet(bonusSheet, spreadSetting);
  102. $.subMenu({
  103. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  104. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  105. key: 'menu.1.0.0',
  106. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  107. callback: function (info) {
  108. if (info.mini) {
  109. $('.panel-title').addClass('fluid');
  110. $('#sub-menu').removeClass('panel-sidebar');
  111. } else {
  112. $('.panel-title').removeClass('fluid');
  113. $('#sub-menu').addClass('panel-sidebar');
  114. }
  115. autoFlashHeight();
  116. bonusSpread.refresh();
  117. }
  118. });
  119. const fileObj = {
  120. generateFilesHtml(data) {
  121. const id = data.id, files = data.proof_file;
  122. let html = [];
  123. if (files !== null && files !== undefined) {
  124. for (const [i, f] of files.entries()) {
  125. html.push('<tr>');
  126. html.push('<td style="width: 200px">', f.filename + f.fileext, '</td>');
  127. html.push('<td>', f.username, '</td>');
  128. html.push('<td>', f.in_time, '</td>');
  129. html.push('<td>');
  130. // 下载
  131. html.push('<a href="download/file?b_id=' + id + '&index=' + i + '" title="下载"><i class="fa fa-download "></i></a>');
  132. // 删除
  133. if (!readOnly && !isPre(data)) {
  134. html.push('<a class="delete-att text-danger ml-1" href="javascript:void(0);" data-id ="' + id + '"file-index="' + i + '" title="删除"><i class="fa fa-remove "></i></a>');
  135. }
  136. html.push('</td>');
  137. html.push('</tr>');
  138. }
  139. }
  140. $('#file-list').html(html.join(''));
  141. },
  142. uploadFile() {
  143. const files = this.files;
  144. const select = SpreadJsObj.getSelectObject(bonusSheet);
  145. const formData = new FormData();
  146. formData.append('bonus_id', select.id);
  147. for (const file of files) {
  148. if (file === undefined) {
  149. toastr.error('未选择上传文件。');
  150. return false;
  151. }
  152. if (file.size > 30 * 1024 * 1024) {
  153. toastr.error('上传文件大小超过30MB。');
  154. return false;
  155. }
  156. const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
  157. if (whiteList.indexOf(fileext) === -1) {
  158. toastr.error('仅支持office文档、图片、压缩包格式,请勿上传' + fileext + '格式文件。');
  159. return false;
  160. }
  161. formData.append('size[]', file.size);
  162. formData.append('file[]', file);
  163. }
  164. postDataWithFile('upload/file', formData, function (data) {
  165. select.proof_file = data;
  166. fileObj.generateFilesHtml(select);
  167. SpreadJsObj.reLoadNodesData(bonusSheet, [select]);
  168. });
  169. $(this).val('');
  170. },
  171. deleteFile() {
  172. const data = {
  173. b_id: $(this).attr('data-id'),
  174. index: parseInt($(this).attr('file-index')),
  175. };
  176. postData('delete/file', data, function (data) {
  177. const select = SpreadJsObj.getSelectObject(bonusSheet);
  178. select.proof_file = data;
  179. fileObj.generateFilesHtml(select);
  180. SpreadJsObj.reLoadNodesData(bonusSheet, [select]);
  181. });
  182. }
  183. };
  184. class Bonus {
  185. constructor () {
  186. this.data = [];
  187. }
  188. resortData() {
  189. this.data.sort(function (a, b) {
  190. return a.sorder !== b.sorder ? a.sorder - b.sorder : a.order - b.order;
  191. });
  192. }
  193. loadDatas(datas) {
  194. this.data = datas;
  195. this.resortData();
  196. }
  197. loadUpdateData(updateData) {
  198. if (updateData.add) {
  199. for (const a of updateData.add) {
  200. this.data.push(a);
  201. }
  202. }
  203. if (updateData.update) {
  204. for (const u of updateData.update) {
  205. const d = this.data.find(function (x) {
  206. return u.id === x.id;
  207. });
  208. if (d) {
  209. _.assign(d, u);
  210. } else {
  211. this.data.push(d);
  212. }
  213. }
  214. }
  215. if (updateData.del) {
  216. _.remove(this.data, function (d) {
  217. return updateData.del.indexOf(d.id) >= 0;
  218. });
  219. }
  220. this.resortData();
  221. }
  222. getCurStageNewOrder() {
  223. const cur = this.data.filter(function (x) {
  224. return x.sid === stageId;
  225. });
  226. return cur && cur.length > 0 ? cur.length + 1 : 1;
  227. }
  228. checkCurFirst(bonusData) {
  229. const cur = this.data.filter(function (x) {
  230. return x.sid === stageId;
  231. });
  232. return cur.indexOf(bonusData) === 0;
  233. }
  234. checkCurLast(bonusData) {
  235. const cur = this.data.filter(function (x) {
  236. return x.sid === stageId;
  237. });
  238. return cur.indexOf(bonusData) === cur.length - 1;
  239. }
  240. }
  241. const bonusObj = new Bonus();
  242. postData(window.location.pathname + '/load', null, function (result) {
  243. bonusObj.loadDatas(result);
  244. SpreadJsObj.loadSheetData(bonusSheet, SpreadJsObj.DataType.Data, bonusObj.data);
  245. });
  246. if (!readOnly) {
  247. const bonusOprObj = {
  248. /**
  249. * 删除按钮响应事件
  250. * @param sheet
  251. */
  252. deletePress: function (sheet) {
  253. if (!sheet.zh_setting || readOnly) return;
  254. const sortData = sheet.zh_data;
  255. const datas = [];
  256. const sels = sheet.getSelections();
  257. if (!sels || !sels[0]) return;
  258. const hint = {
  259. name: {type: 'warning', msg: '名称不能为空,如需删除甲供材料请使用右键删除'},
  260. };
  261. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  262. let bDel = false;
  263. const node = sortData[iRow];
  264. if (node) {
  265. const data = {id: node.id};
  266. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  267. const colSetting = sheet.zh_setting.cols[iCol];
  268. if (colSetting.field === 'name') {
  269. toastMessageUniq(hint.name);
  270. return;
  271. }
  272. const style = sheet.getStyle(iRow, iCol);
  273. if (!style.locked) {
  274. const colSetting = sheet.zh_setting.cols[iCol];
  275. data[colSetting.field] = null;
  276. bDel = true;
  277. }
  278. }
  279. if (bDel) {
  280. datas.push(data);
  281. }
  282. }
  283. }
  284. if (datas.length > 0) {
  285. postData(window.location.pathname + '/update', {update: datas}, function (result) {
  286. bonusObj.loadUpdateData(result);
  287. SpreadJsObj.reLoadSheetData(bonusSheet);
  288. }, function () {
  289. SpreadJsObj.reLoadSheetData(bonusSheet);
  290. });
  291. }
  292. },
  293. delete: function () {
  294. const sheet = bonusSheet;
  295. if (!sheet.zh_setting || readOnly) return;
  296. const sortData = sheet.zh_data;
  297. const datas = [];
  298. const sels = sheet.getSelections();
  299. if (!sels || !sels[0]) return;
  300. const hint = {
  301. isOld: {type: 'warning', msg: '本项为往期数据,不可删除'},
  302. invalidDel: {type: 'warning', msg: '本项不是您新增的,只有原报和新增人可删除'},
  303. };
  304. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  305. const node = sortData[iRow];
  306. if (node.sid !== stageId) {
  307. toastMessageUniq(hint.isOld);
  308. continue;
  309. } else {
  310. if (node.uid !== userID && stageUserId !== userID) {
  311. toastMessageUniq(hint.invalidDel);
  312. continue;
  313. }
  314. datas.push(node.id);
  315. }
  316. }
  317. if (datas.length > 0) {
  318. postData(window.location.pathname + '/update', {del: datas}, function (result) {
  319. bonusObj.loadUpdateData(result);
  320. SpreadJsObj.reLoadSheetData(sheet);
  321. }, function () {
  322. SpreadJsObj.reLoadSheetData(sheet);
  323. });
  324. }
  325. },
  326. editEnded: function (e, info) {
  327. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  328. const node = info.sheet.zh_data[info.row];
  329. const col = info.sheet.zh_setting.cols[info.col];
  330. const data = {};
  331. if (node) {
  332. data.update = {};
  333. data.update.id = node.id;
  334. const oldValue = node ? node[col.field] : null;
  335. const newValue = trimInvalidChar(info.editingText);
  336. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  337. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  338. return;
  339. }
  340. data.update[col.field] = newValue;
  341. } else {
  342. if (col.field !== 'name') {
  343. toastr.warning('新增奖罚金,请先输入名称');
  344. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  345. return;
  346. }
  347. data.add = {};
  348. data.add.order = bonusObj.getCurStageNewOrder();
  349. data.add.name = trimInvalidChar(info.editingText);
  350. }
  351. postData(window.location.pathname + '/update', data, function (result) {
  352. bonusObj.loadUpdateData(result);
  353. SpreadJsObj.reLoadSheetData(info.sheet);
  354. }, function () {
  355. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  356. });
  357. },
  358. clipboardPasting(e, info) {
  359. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  360. info.cancel = true;
  361. if (!setting || !sortData) return;
  362. const pasteData = info.pasteData.html
  363. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  364. : (info.pasteData.text === ''
  365. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  366. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  367. const hint = {
  368. name: {type: 'warning', msg: '奖罚金名称不可为空,已过滤'},
  369. tp: {type: 'warning', msg: '输入的 金额 非法,已过滤'},
  370. };
  371. const uDatas = [], iDatas = [], maxOrder = bonusObj.getCurStageNewOrder();
  372. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  373. const curRow = info.cellRange.row + iRow;
  374. const node = sortData[curRow];
  375. let bPaste = false;
  376. const data = {};
  377. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  378. const curCol = info.cellRange.col + iCol;
  379. const colSetting = setting.cols[curCol];
  380. const value = trimInvalidChar(pasteData[iRow][iCol]);
  381. if (colSetting.field === 'name' && (!value || value === '')) {
  382. toastMessageUniq(hint.name);
  383. break;
  384. }
  385. if (colSetting.type === 'Number') {
  386. const num = _.toNumber(value);
  387. if (num) {
  388. data[colSetting.field] = num;
  389. bPaste = true;
  390. }
  391. } else {
  392. data[colSetting.field] = value;
  393. bPaste = true;
  394. }
  395. }
  396. if (bPaste) {
  397. if (node) {
  398. data.id = node.id;
  399. uDatas.push(data);
  400. } else {
  401. data.order = maxOrder + iRow;
  402. iDatas.push(data);
  403. }
  404. }
  405. }
  406. const updateData = {};
  407. if (uDatas.length > 0) updateData.update = uDatas;
  408. if (iDatas.length > 0) updateData.add = iDatas;
  409. if (uDatas.length > 0 || iDatas.length > 0) {
  410. postData(window.location.pathname + '/update', updateData, function (result) {
  411. bonusObj.loadUpdateData(result);
  412. SpreadJsObj.reLoadSheetData(info.sheet);
  413. });
  414. } else {
  415. SpreadJsObj.reLoadSheetData(info.sheet);
  416. }
  417. },
  418. upMove: function () {
  419. const sheet = bonusSheet;
  420. const sels = sheet.getSelections(), sortData = sheet.zh_data;
  421. const node = sortData[sels[0].row];
  422. const preNode = sortData[sels[0].row - 1];
  423. const data = [
  424. {id: node.id, order: preNode.order},
  425. {id: preNode.id, order: node.order}
  426. ];
  427. postData(window.location.pathname + '/update', {update: data}, function (result) {
  428. bonusObj.loadUpdateData(result);
  429. SpreadJsObj.reLoadRowsData(sheet, [sels[0].row, sels[0].row - 1]);
  430. sheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  431. });
  432. },
  433. downMove: function () {
  434. const sheet = bonusSheet;
  435. const sels = sheet.getSelections(), sortData = sheet.zh_data;
  436. const node = sortData[sels[0].row];
  437. const nextNode = sortData[sels[0].row + 1];
  438. const data = [
  439. {id: node.id, order: nextNode.order},
  440. {id: nextNode.id, order: node.order}
  441. ];
  442. postData(window.location.pathname + '/update', {update: data}, function (result) {
  443. bonusObj.loadUpdateData(result);
  444. SpreadJsObj.reLoadRowsData(sheet, [sels[0].row, sels[0].row + 1]);
  445. sheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  446. });
  447. }
  448. };
  449. bonusSheet.bind(spreadNS.Events.EditEnded, bonusOprObj.editEnded);
  450. bonusSheet.bind(spreadNS.Events.ClipboardPasting, bonusOprObj.clipboardPasting);
  451. SpreadJsObj.addDeleteBind(bonusSpread, bonusOprObj.deletePress);
  452. $.contextMenu({
  453. selector: '#bonus-spread',
  454. build: function ($trigger, e) {
  455. const target = SpreadJsObj.safeRightClickSelection($trigger, e, bonusSpread);
  456. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  457. },
  458. items: {
  459. del: {
  460. name: '删除',
  461. icon: 'fa-remove',
  462. callback: function (key, opt) {
  463. bonusOprObj.delete();
  464. },
  465. disabled: function (key, opt) {
  466. const sels = bonusSheet.getSelections();
  467. if (!sels || !sels[0]) return true;
  468. const row = sels[0].row;
  469. const node = bonusObj.data[row];
  470. return node === undefined || node === null || node.sid !== stageId;
  471. },
  472. visible: function (key, opt) {
  473. return !readOnly;
  474. }
  475. },
  476. sprDel: '------------',
  477. upMove: {
  478. name: '上移',
  479. icon: 'fa-arrow-up',
  480. callback: function (key, opt) {
  481. bonusOprObj.upMove();
  482. },
  483. disabled: function (key, opt) {
  484. const sels = bonusSheet.getSelections();
  485. if (!sels || !sels[0] || sels[0].row === 0) return true;
  486. const row = sels[0].row;
  487. const node = bonusObj.data[row];
  488. return node === undefined || node === null || node.sid !== stageId || bonusObj.checkCurFirst(node);
  489. },
  490. visible: function (key, opt) {
  491. return !readOnly;
  492. }
  493. },
  494. downMove: {
  495. name: '下移',
  496. icon: 'fa-arrow-down',
  497. callback: function (key, opt) {
  498. bonusOprObj.downMove();
  499. },
  500. disabled: function (key, opt) {
  501. const sels = bonusSheet.getSelections();
  502. if (!sels || !sels[0] || sels[0].row >= bonusObj.data.length - 1) return true;
  503. const row = sels[0].row;
  504. const node = bonusObj.data[row];
  505. return node === undefined || node === null || node.sid !== stageId || bonusObj.checkCurLast(node);
  506. },
  507. visible: function (key, opt) {
  508. return !readOnly;
  509. }
  510. }
  511. },
  512. });
  513. }
  514. $('#exportExcel').click(function () {
  515. SpreadExcelObj.exportSimpleXlsxSheet(spreadSetting, bonusObj.data, $('h2')[0].innerHTML + "-奖罚金.xlsx");
  516. });
  517. $('#upload-file').change(fileObj.uploadFile);
  518. $('body').on('click', '.delete-att', fileObj.deleteFile);
  519. });