se_other.js 20 KB

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