se_bonus.js 24 KB

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