se_bonus.js 24 KB

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