se_bonus.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.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: 'tp', hAlign: 2, width: 100, type: 'Number', readOnly: isPre, },
  19. {
  20. title: '时间', colSpan: '1', rowSpan: '1', field: 'real_time', hAlign: 2, width: 150, readOnly: true,
  21. formatter: 'yyyy-MM-dd', cellType: 'activeImageBtn', normalImg: '#ellipsis-icon', indent: 5,
  22. },
  23. {title: '编号', colSpan: '1', rowSpan: '1', field: 'code', hAlign: 0, width: 150, formatter: '@', readOnly: isPre, },
  24. {title: '依据材料证明', colSpan: '1', rowSpan: '1', field: 'proof', hAlign: 0, width: 180, formatter: '@', readOnly: isPre, },
  25. {
  26. title: '计量期', colSpan: '1', rowSpan: '1', field: 'sorder', hAlign: 1, width: 100, formatter: '@',
  27. getValue: function (data) {
  28. return '第' + data.sorder + '期';
  29. }, readOnly: true,
  30. },
  31. {title: '备注', colSpan: '1', rowSpan: '1', field: 'memo', hAlign: 0, width: 180, formatter: '@', cellType: 'ellipsisAutoTip', readOnly: isPre, }
  32. ],
  33. emptyRows: 3,
  34. headRows: 1,
  35. headRowHeight: [32],
  36. defaultRowHeight: 21,
  37. headerFont: '12px 微软雅黑',
  38. font: '12px 微软雅黑',
  39. imageClick: function (data, hitinfo) {
  40. if (!data) return;
  41. const setting = hitinfo.sheet.zh_setting;
  42. if (!setting) return;
  43. const col = setting.cols[hitinfo.col];
  44. if (!col || col.field !== 'real_time') return;
  45. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  46. if (!datepicker) {
  47. datepicker = $('.datepicker-here').datepicker({
  48. language: 'zh',
  49. dateFormat: 'yyyy-MM-dd',
  50. autoClose: true,
  51. onSelect: function (formattedDate, date, inst) {
  52. if (!inst.visible) return;
  53. const sels = hitinfo.sheet.getSelections();
  54. if (!sels || !sels[0]) return;
  55. const node = SpreadJsObj.getSelectObject(hitinfo.sheet);
  56. const uData = { update: {id: node.id, real_time: date} };
  57. postData(window.location.pathname + '/update', uData, function (result) {
  58. bonusObj.loadUpdateData(result);
  59. SpreadJsObj.reLoadRowData(hitinfo.sheet, sels[0].row);
  60. }, function () {
  61. SpreadJsObj.reLoadRowData(hitinfo.sheet, sels[0].row);
  62. });
  63. }
  64. }).data('datepicker');
  65. }
  66. const value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  67. if (value) {
  68. datepicker.selectDate(value);
  69. } else {
  70. datepicker.clear();
  71. }
  72. datepicker.show();
  73. $('#datepickers-container').css('top', hitinfo.cellRect.y + pos.y).css('left', hitinfo.cellRect.x + pos.x);
  74. }
  75. };
  76. const bonusSpread = SpreadJsObj.createNewSpread($('#bonus-spread')[0]);
  77. const bonusSheet = bonusSpread.getActiveSheet();
  78. spreadSetting.readOnly = readOnly;
  79. SpreadJsObj.initSheet(bonusSheet, spreadSetting);
  80. $.subMenu({
  81. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  82. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  83. key: 'menu.1.0.0',
  84. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  85. callback: function (info) {
  86. if (info.mini) {
  87. $('.panel-title').addClass('fluid');
  88. $('#sub-menu').removeClass('panel-sidebar');
  89. } else {
  90. $('.panel-title').removeClass('fluid');
  91. $('#sub-menu').addClass('panel-sidebar');
  92. }
  93. autoFlashHeight();
  94. bonusSpread.refresh();
  95. }
  96. });
  97. class Bonus {
  98. constructor () {
  99. this.data = [];
  100. }
  101. resortData() {
  102. this.data.sort(function (a, b) {
  103. return a.sorder !== b.sorder ? a.sorder - b.sorder : a.order - b.order;
  104. });
  105. }
  106. loadDatas(datas) {
  107. this.data = datas;
  108. this.resortData();
  109. }
  110. loadUpdateData(updateData) {
  111. if (updateData.add) {
  112. for (const a of updateData.add) {
  113. this.data.push(a);
  114. }
  115. }
  116. if (updateData.update) {
  117. for (const u of updateData.update) {
  118. const d = this.data.find(function (x) {
  119. return u.id === x.id;
  120. });
  121. if (d) {
  122. _.assign(d, u);
  123. } else {
  124. this.data.push(d);
  125. }
  126. }
  127. }
  128. if (updateData.del) {
  129. _.remove(this.data, function (d) {
  130. return updateData.del.indexOf(d.id) >= 0;
  131. });
  132. }
  133. this.resortData();
  134. }
  135. getCurStageNewOrder() {
  136. const cur = this.data.filter(function (x) {
  137. return x.sid === stageId;
  138. });
  139. return cur && cur.length > 0 ? cur.length + 1 : 1;
  140. }
  141. checkCurFirst(bonusData) {
  142. const cur = this.data.filter(function (x) {
  143. return x.sid === stageId;
  144. });
  145. return cur.indexOf(bonusData) === 0;
  146. }
  147. checkCurLast(bonusData) {
  148. const cur = this.data.filter(function (x) {
  149. return x.sid === stageId;
  150. });
  151. return cur.indexOf(bonusData) === cur.length - 1;
  152. }
  153. }
  154. const bonusObj = new Bonus();
  155. postData(window.location.pathname + '/load', null, function (result) {
  156. bonusObj.loadDatas(result);
  157. SpreadJsObj.loadSheetData(bonusSheet, SpreadJsObj.DataType.Data, bonusObj.data);
  158. });
  159. if (!readOnly) {
  160. const bonusOprObj = {
  161. /**
  162. * 删除按钮响应事件
  163. * @param sheet
  164. */
  165. deletePress: function (sheet) {
  166. if (!sheet.zh_setting || readOnly) return;
  167. const sortData = sheet.zh_data;
  168. const datas = [];
  169. const sels = sheet.getSelections();
  170. if (!sels || !sels[0]) return;
  171. const hint = {
  172. name: {type: 'warning', msg: '名称不能为空,如需删除甲供材料请使用右键删除'},
  173. };
  174. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  175. let bDel = false;
  176. const node = sortData[iRow];
  177. if (node) {
  178. const data = {id: node.id};
  179. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  180. const colSetting = sheet.zh_setting.cols[iCol];
  181. if (colSetting.field === 'name') {
  182. toastMessageUniq(hint.name);
  183. return;
  184. }
  185. const style = sheet.getStyle(iRow, iCol);
  186. if (!style.locked) {
  187. const colSetting = sheet.zh_setting.cols[iCol];
  188. data[colSetting.field] = null;
  189. bDel = true;
  190. }
  191. }
  192. if (bDel) {
  193. datas.push(data);
  194. }
  195. }
  196. }
  197. if (datas.length > 0) {
  198. postData(window.location.pathname + '/update', {updateType: 'update', updateData: datas}, function (result) {
  199. bonusObj.loadUpdateData(result);
  200. SpreadJsObj.reLoadSheetData(bonusSheet);
  201. }, function () {
  202. SpreadJsObj.reLoadSheetData(bonusSheet);
  203. });
  204. }
  205. },
  206. delete: function () {
  207. const sheet = bonusSheet;
  208. if (!sheet.zh_setting || readOnly) return;
  209. const sortData = sheet.zh_data;
  210. const datas = [];
  211. const sels = sheet.getSelections();
  212. if (!sels || !sels[0]) return;
  213. const hint = {
  214. isOld: {type: 'warning', msg: '本项为往期数据,不可删除'},
  215. invalidDel: {type: 'warning', msg: '本项不是您新增的,只有原报和新增人可删除'},
  216. };
  217. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  218. const node = sortData[iRow];
  219. if (node.sid !== stageID) {
  220. toastMessageUniq(hint.isOld);
  221. continue;
  222. } else {
  223. if (node.uid !== userID || stageUserId !== userID) {
  224. toastMessageUniq(hint.invalidDel);
  225. continue;
  226. }
  227. datas.push(node.id);
  228. }
  229. }
  230. if (datas.length > 0) {
  231. postData(window.location.pathname + '/update', {del: datas}, function (result) {
  232. bonusObj.loadUpdateData(result);
  233. SpreadJsObj.reLoadSheetData(sheet);
  234. }, function () {
  235. SpreadJsObj.reLoadSheetData(sheet);
  236. });
  237. }
  238. },
  239. editEnded: function (e, info) {
  240. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  241. const node = info.sheet.zh_data[info.row];
  242. const col = info.sheet.zh_setting.cols[info.col];
  243. const data = {};
  244. if (node) {
  245. data.update = {};
  246. data.update.id = node.id;
  247. const oldValue = node ? node[col.field] : null;
  248. const newValue = trimInvalidChar(info.editingText);
  249. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  250. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  251. return;
  252. }
  253. data.update[col.field] = newValue;
  254. } else {
  255. if (col.field !== 'name') {
  256. toastr.warning('新增奖罚金,请先输入名称');
  257. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  258. return;
  259. }
  260. data.add = {};
  261. data.add.order = bonusObj.getCurStageNewOrder();
  262. data.add.name = trimInvalidChar(info.editingText);
  263. }
  264. postData(window.location.pathname + '/update', data, function (result) {
  265. bonusObj.loadUpdateData(result);
  266. SpreadJsObj.reLoadSheetData(info.sheet);
  267. }, function () {
  268. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  269. });
  270. },
  271. clipboardPasting(e, info) {
  272. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  273. info.cancel = true;
  274. if (!setting || !sortData) return;
  275. const pasteData = info.pasteData.html
  276. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  277. : (info.pasteData.text === ''
  278. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  279. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  280. const hint = {
  281. name: {type: 'warning', msg: '奖罚金名称不可为空,已过滤'},
  282. tp: {type: 'warning', msg: '输入的 金额 非法,已过滤'},
  283. };
  284. const uDatas = [], iDatas = [], maxOrder = bonusObj.getCurStageNewOrder();
  285. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  286. const curRow = info.cellRange.row + iRow;
  287. const node = sortData[curRow];
  288. let bPaste = false;
  289. const data = {};
  290. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  291. const curCol = info.cellRange.col + iCol;
  292. const colSetting = setting.cols[curCol];
  293. const value = trimInvalidChar(pasteData[iRow][iCol]);
  294. if (colSetting.field === 'name' && (!value || value === '')) {
  295. toastMessageUniq(hint.name);
  296. break;
  297. }
  298. if (colSetting.type === 'Number') {
  299. const num = _.toNumber(value);
  300. if (num) {
  301. data[colSetting.field] = num;
  302. bPaste = true;
  303. }
  304. } else {
  305. data[colSetting.field] = value;
  306. bPaste = true;
  307. }
  308. }
  309. if (bPaste) {
  310. if (node) {
  311. data.id = node.id;
  312. uDatas.push(data);
  313. } else {
  314. data.order = maxOrder + iRow;
  315. iDatas.push(data);
  316. }
  317. }
  318. }
  319. const updateData = {};
  320. if (uDatas.length > 0) updateData.update = uDatas;
  321. if (iDatas.length > 0) updateData.add = iDatas;
  322. if (uDatas.length > 0 || iDatas.length > 0) {
  323. postData(window.location.pathname + '/update', updateData, function (result) {
  324. bonusObj.loadUpdateData(result);
  325. SpreadJsObj.reLoadSheetData(info.sheet);
  326. });
  327. } else {
  328. SpreadJsObj.reLoadSheetData(info.sheet);
  329. }
  330. },
  331. upMove: function () {
  332. const sheet = bonusSheet;
  333. const sels = sheet.getSelections(), sortData = sheet.zh_data;
  334. const node = sortData[sels[0].row];
  335. const preNode = sortData[sels[0].row - 1];
  336. const data = [
  337. {id: node.id, order: preNode.order},
  338. {id: preNode.id, order: node.order}
  339. ];
  340. postData(window.location.pathname + '/update', {update: data}, function (result) {
  341. bonusObj.loadUpdateData(result);
  342. SpreadJsObj.reLoadRowsData(sheet, [sels[0].row, sels[0].row - 1]);
  343. sheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  344. });
  345. },
  346. downMove: function () {
  347. const sheet = bonusSheet;
  348. const sels = sheet.getSelections(), sortData = sheet.zh_data;
  349. const node = sortData[sels[0].row];
  350. const nextNode = sortData[sels[0].row + 1];
  351. const data = [
  352. {id: node.id, order: nextNode.order},
  353. {id: nextNode.id, order: node.order}
  354. ];
  355. postData(window.location.pathname + '/update', {update: data}, function (result) {
  356. bonusObj.loadUpdateData(result);
  357. SpreadJsObj.reLoadRowsData(sheet, [sels[0].row, sels[0].row + 1]);
  358. sheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  359. });
  360. }
  361. };
  362. bonusSheet.bind(spreadNS.Events.EditEnded, bonusOprObj.editEnded);
  363. bonusSheet.bind(spreadNS.Events.ClipboardPasting, bonusOprObj.clipboardPasting);
  364. SpreadJsObj.addDeleteBind(bonusSpread, bonusOprObj.deletePress);
  365. $.contextMenu({
  366. selector: '#bonus-spread',
  367. build: function ($trigger, e) {
  368. const target = SpreadJsObj.safeRightClickSelection($trigger, e, bonusSpread);
  369. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  370. },
  371. items: {
  372. del: {
  373. name: '删除',
  374. icon: 'fa-remove',
  375. callback: function (key, opt) {
  376. bonusOprObj.delete();
  377. },
  378. disabled: function (key, opt) {
  379. const sels = bonusSheet.getSelections();
  380. if (!sels || !sels[0]) return true;
  381. const row = sels[0].row;
  382. const node = bonusObj.data[row];
  383. return node === undefined || node === null || node.sid !== stageId;
  384. },
  385. visible: function (key, opt) {
  386. return !readOnly;
  387. }
  388. },
  389. sprDel: '------------',
  390. upMove: {
  391. name: '上移',
  392. icon: 'fa-arrow-up',
  393. callback: function (key, opt) {
  394. bonusOprObj.upMove();
  395. },
  396. disabled: function (key, opt) {
  397. const sels = bonusSheet.getSelections();
  398. if (!sels || !sels[0] || sels[0].row === 0) return true;
  399. const row = sels[0].row;
  400. const node = bonusObj.data[row];
  401. return node === undefined || node === null || node.sid !== stageId || bonusObj.checkCurFirst(node);
  402. },
  403. visible: function (key, opt) {
  404. return !readOnly;
  405. }
  406. },
  407. downMove: {
  408. name: '下移',
  409. icon: 'fa-arrow-down',
  410. callback: function (key, opt) {
  411. bonusOprObj.downMove();
  412. },
  413. disabled: function (key, opt) {
  414. const sels = bonusSheet.getSelections();
  415. if (!sels || !sels[0] || sels[0].row >= bonusObj.data.length - 1) return true;
  416. const row = sels[0].row;
  417. const node = bonusObj.data[row];
  418. return node === undefined || node === null || node.sid !== stageId || bonusObj.checkCurLast(node);
  419. },
  420. visible: function (key, opt) {
  421. return !readOnly;
  422. }
  423. }
  424. },
  425. });
  426. }
  427. });