se_other.js 20 KB

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