se_bonus.js 19 KB

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