se_bonus.js 17 KB

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