se_other.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. if (thousandth) sjsSettingObj.setTpThousandthFormat(otherSpreadSetting);
  92. SpreadJsObj.initSheet(otherSheet, otherSpreadSetting);
  93. $.subMenu({
  94. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  95. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  96. key: 'menu.1.0.0',
  97. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  98. callback: function (info) {
  99. if (info.mini) {
  100. $('.panel-title').addClass('fluid');
  101. $('#sub-menu').removeClass('panel-sidebar');
  102. } else {
  103. $('.panel-title').removeClass('fluid');
  104. $('#sub-menu').addClass('panel-sidebar');
  105. }
  106. autoFlashHeight();
  107. otherSpread.refresh();
  108. }
  109. });
  110. class SeOther {
  111. constructor () {
  112. this.data = [];
  113. }
  114. resortData() {
  115. this.data.sort(function (a, b) {
  116. return a.order - b.order;
  117. });
  118. }
  119. calculateAll() {
  120. for (const d of this.data) {
  121. d.end_tp = ZhCalc.add(d.pre_tp, d.tp);
  122. }
  123. }
  124. loadDatas(datas) {
  125. this.data = datas;
  126. this.calculateAll();
  127. this.resortData();
  128. }
  129. loadUpdateData(updateData) {
  130. if (updateData.add) {
  131. for (const a of updateData.add) {
  132. this.data.push(a);
  133. }
  134. }
  135. if (updateData.update) {
  136. for (const u of updateData.update) {
  137. const d = this.data.find(function (x) {
  138. return u.id === x.id;
  139. });
  140. if (d) {
  141. _.assign(d, u);
  142. } else {
  143. this.data.push(d);
  144. }
  145. }
  146. }
  147. if (updateData.del) {
  148. _.remove(this.data, function (d) {
  149. return updateData.del.indexOf(d.id) >= 0;
  150. });
  151. }
  152. this.calculateAll();
  153. this.resortData();
  154. }
  155. sum () {
  156. const result = {
  157. total_price: 0,
  158. tp: 0,
  159. end_tp: 0,
  160. };
  161. for (const d of this.data) {
  162. result.total_price = ZhCalc.add(result.total_price, d.total_price);
  163. result.tp = ZhCalc.add(result.tp, d.tp);
  164. result.end_tp = ZhCalc.add(result.end_tp, d.end_tp);
  165. }
  166. return result;
  167. }
  168. }
  169. const seOtherObj = new SeOther();
  170. const refreshSum = function () {
  171. const sum = seOtherObj.sum();
  172. const html = [];
  173. const getTrHtml = function (name, value) {
  174. return '<tr><td>' + name + '</td><td class="text-right">' + (!checkZero(value) ? value : '') + ' </td></tr>';
  175. };
  176. html.push(getTrHtml('金额', sum.total_price));
  177. html.push(getTrHtml('本期金额', sum.tp));
  178. html.push(getTrHtml('截止本期金额', sum.end_tp));
  179. $('#sum').html(html.join(' '));
  180. };
  181. postData(window.location.pathname + '/load', null, function (result) {
  182. seOtherObj.loadDatas(result);
  183. SpreadJsObj.loadSheetData(otherSheet, SpreadJsObj.DataType.Data, seOtherObj.data);
  184. refreshSum();
  185. });
  186. if (!readOnly) {
  187. const seOtherOprObj = {
  188. /**
  189. * 删除按钮响应事件
  190. * @param sheet
  191. */
  192. deletePress: function (sheet) {
  193. if (!sheet.zh_setting || readOnly) return;
  194. const sortData = sheet.zh_data;
  195. const datas = [];
  196. const sels = sheet.getSelections();
  197. if (!sels || !sels[0]) return;
  198. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  199. let bDel = false;
  200. const node = sortData[iRow];
  201. if (node) {
  202. const data = {id: node.id};
  203. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  204. const colSetting = sheet.zh_setting.cols[iCol];
  205. if (colSetting.field === 'name') {
  206. toastr.error('名称不能为空,如需删除请使用右键删除');
  207. return;
  208. }
  209. const style = sheet.getStyle(iRow, iCol);
  210. if (!style.locked) {
  211. const colSetting = sheet.zh_setting.cols[iCol];
  212. data[colSetting.field] = null;
  213. bDel = true;
  214. }
  215. }
  216. if (bDel) {
  217. datas.push(data);
  218. }
  219. }
  220. }
  221. if (datas.length > 0) {
  222. postData(window.location.pathname + '/update', {update: datas}, function (result) {
  223. seOtherObj.loadUpdateData(result);
  224. SpreadJsObj.reLoadSheetData(otherSheet);
  225. refreshSum();
  226. }, function () {
  227. SpreadJsObj.reLoadSheetData(otherSheet);
  228. });
  229. }
  230. },
  231. delete: function (sheet) {
  232. if (!sheet.zh_setting || readOnly) return;
  233. const sortData = sheet.zh_data;
  234. const datas = [];
  235. const sels = sheet.getSelections();
  236. if (!sels || !sels[0]) return;
  237. const hint = {
  238. isOld: {type: 'warning', msg: '该数据已计量,不可删除'},
  239. invalidDel: {type: 'warning', msg: '该数据不是您新增的,只有原报和新增人可删除'},
  240. };
  241. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  242. const node = sortData[iRow];
  243. if (node.pre_used || !checkZero(node.end_tp)) {
  244. toastMessageUniq(hint.isOld);
  245. continue;
  246. } else {
  247. if (node.add_uid !== userID && stageUserId !== userID) {
  248. toastMessageUniq(hint.invalidDel);
  249. continue;
  250. }
  251. datas.push(node.id);
  252. }
  253. }
  254. if (datas.length > 0) {
  255. postData(window.location.pathname + '/update', {del: datas}, function (result) {
  256. seOtherObj.loadUpdateData(result);
  257. SpreadJsObj.reLoadSheetData(otherSheet);
  258. refreshSum();
  259. }, function () {
  260. SpreadJsObj.reLoadSheetData(otherSheet);
  261. });
  262. }
  263. },
  264. insert: function(sheet) {
  265. if (!sheet.zh_setting || !sheet.zh_data) return;
  266. const node = SpreadJsObj.getSelectObject(sheet);
  267. postData(window.location.pathname + '/update', {insert: { select: node.id, count: 1 }}, function (result) {
  268. seOtherObj.loadUpdateData(result);
  269. SpreadJsObj.reLoadSheetData(sheet);
  270. refreshSum();
  271. }, function () {
  272. SpreadJsObj.reLoadSheetData(sheet);
  273. });
  274. },
  275. editEnded: function (e, info) {
  276. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  277. const node = info.sheet.zh_data[info.row];
  278. const col = info.sheet.zh_setting.cols[info.col];
  279. const data = {};
  280. if (node) {
  281. data.update = {};
  282. data.update.id = node.id;
  283. const oldValue = node ? node[col.field] : null;
  284. const newValue = trimInvalidChar(info.editingText);
  285. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  286. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  287. return;
  288. }
  289. data.update[col.field] = newValue;
  290. } else {
  291. if (col.field !== 'name') {
  292. toastr.warning('请先输入名称');
  293. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  294. return;
  295. }
  296. data.add = {};
  297. data.add.order = info.row + 1;
  298. data.add.name = trimInvalidChar(info.editingText);
  299. }
  300. postData(window.location.pathname + '/update', data, function (result) {
  301. seOtherObj.loadUpdateData(result);
  302. SpreadJsObj.reLoadSheetData(info.sheet);
  303. refreshSum();
  304. }, function () {
  305. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  306. });
  307. },
  308. editStarting(e, info) {
  309. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  310. info.cancel = true;
  311. return;
  312. }
  313. const col = info.sheet.zh_setting.cols[info.col];
  314. const node = info.sheet.zh_data[info.row];
  315. if (!node) return;
  316. switch (col.field) {
  317. case 'name':
  318. case 'total_price':
  319. info.cancel = readOnly || node.pre_used;
  320. break;
  321. }
  322. },
  323. clipboardPasting(e, info) {
  324. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  325. info.cancel = true;
  326. if (!setting || !sortData) return;
  327. const pasteData = info.pasteData.html
  328. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  329. : (info.pasteData.text === ''
  330. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  331. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  332. const hint = {
  333. name: {type: 'warning', msg: '名称不可为空,已过滤'},
  334. tp: {type: 'warning', msg: '输入的 金额 非法,已过滤'},
  335. };
  336. const uDatas = [], iDatas = [];
  337. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  338. const curRow = info.cellRange.row + iRow;
  339. const node = sortData[curRow];
  340. let bPaste = false;
  341. const data = {};
  342. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  343. const curCol = info.cellRange.col + iCol;
  344. const colSetting = setting.cols[curCol];
  345. const value = trimInvalidChar(pasteData[iRow][iCol]);
  346. if (colSetting.field === 'name' && (!value || value === '')) {
  347. toastMessageUniq(hint.name);
  348. break;
  349. }
  350. if (colSetting.type === 'Number') {
  351. const num = _.toNumber(value);
  352. if (num) {
  353. data[colSetting.field] = num;
  354. bPaste = true;
  355. }
  356. } else {
  357. data[colSetting.field] = value;
  358. bPaste = true;
  359. }
  360. }
  361. if (bPaste) {
  362. if (node) {
  363. data.id = node.id;
  364. uDatas.push(data);
  365. } else {
  366. data.order = curRow + 1;
  367. iDatas.push(data);
  368. }
  369. }
  370. }
  371. const updateData = {};
  372. if (uDatas.length > 0) updateData.update = uDatas;
  373. if (iDatas.length > 0) updateData.add = iDatas;
  374. if (uDatas.length > 0 || iDatas.length > 0) {
  375. postData(window.location.pathname + '/update', updateData, function (result) {
  376. seOtherObj.loadUpdateData(result);
  377. SpreadJsObj.reLoadSheetData(info.sheet);
  378. refreshSum();
  379. });
  380. } else {
  381. SpreadJsObj.reLoadSheetData(info.sheet);
  382. }
  383. },
  384. upMove: function () {
  385. const sels = otherSheet.getSelections(), sortData = otherSheet.zh_data;
  386. const node = sortData[sels[0].row];
  387. const preNode = sortData[sels[0].row - 1];
  388. const data = [
  389. {id: node.id, order: preNode.order},
  390. {id: preNode.id, order: node.order}
  391. ];
  392. postData(window.location.pathname + '/update', {update: data}, function (result) {
  393. seOtherObj.loadUpdateData(result);
  394. SpreadJsObj.reLoadRowsData(otherSheet, [sels[0].row, sels[0].row - 1]);
  395. otherSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  396. });
  397. },
  398. downMove: function () {
  399. const sels = otherSheet.getSelections(), sortData = otherSheet.zh_data;
  400. const node = sortData[sels[0].row];
  401. const nextNode = sortData[sels[0].row + 1];
  402. const data = [
  403. {id: node.id, order: nextNode.order},
  404. {id: nextNode.id, order: node.order}
  405. ];
  406. postData(window.location.pathname + '/update', {update: data}, function (result) {
  407. seOtherObj.loadUpdateData(result);
  408. SpreadJsObj.reLoadRowsData(otherSheet, [sels[0].row, sels[0].row + 1]);
  409. otherSheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  410. });
  411. }
  412. };
  413. otherSheet.bind(spreadNS.Events.EditEnded, seOtherOprObj.editEnded);
  414. otherSheet.bind(spreadNS.Events.EditStarting, seOtherOprObj.editStarting);
  415. otherSheet.bind(spreadNS.Events.ClipboardPasting, seOtherOprObj.clipboardPasting);
  416. SpreadJsObj.addDeleteBind(otherSpread, seOtherOprObj.deletePress);
  417. $.contextMenu({
  418. selector: '#other-spread',
  419. build: function ($trigger, e) {
  420. const target = SpreadJsObj.safeRightClickSelection($trigger, e, otherSpread);
  421. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  422. },
  423. items: {
  424. del: {
  425. name: '删除',
  426. icon: 'fa-remove',
  427. callback: function (key, opt) {
  428. seOtherOprObj.delete(otherSheet);
  429. },
  430. disabled: function (key, opt) {
  431. const sels = otherSheet.getSelections();
  432. if (!sels || !sels[0]) return true;
  433. const row = sels[0].row;
  434. const node = seOtherObj.data[row];
  435. return node === undefined || node === null;
  436. },
  437. visible: function (key, opt) {
  438. return !readOnly;
  439. }
  440. },
  441. sprDel: '------------',
  442. insert: {
  443. name: '插入',
  444. icon: 'fa-plus',
  445. callback: function (key, opt) {
  446. seOtherOprObj.insert(otherSheet);
  447. },
  448. disabled: function (key, opt) {
  449. const node = SpreadJsObj.getSelectObject(otherSheet);
  450. return !node;
  451. },
  452. visible: function (key, opt) {
  453. return !readOnly;
  454. }
  455. },
  456. upMove: {
  457. name: '上移',
  458. icon: 'fa-arrow-up',
  459. callback: function (key, opt) {
  460. seOtherOprObj.upMove();
  461. },
  462. disabled: function (key, opt) {
  463. const sels = otherSheet.getSelections();
  464. if (!sels || !sels[0] || sels[0].row === 0) return true;
  465. const row = sels[0].row;
  466. const node = seOtherObj.data[row];
  467. return node === undefined || node === null;
  468. },
  469. visible: function (key, opt) {
  470. return !readOnly;
  471. }
  472. },
  473. downMove: {
  474. name: '下移',
  475. icon: 'fa-arrow-down',
  476. callback: function (key, opt) {
  477. seOtherOprObj.downMove();
  478. },
  479. disabled: function (key, opt) {
  480. const sels = otherSheet.getSelections();
  481. if (!sels || !sels[0] || sels[0].row >= seOtherObj.data.length - 1) return true;
  482. const row = sels[0].row;
  483. const node = seOtherObj.data[row];
  484. return node === undefined || node === null;
  485. },
  486. visible: function (key, opt) {
  487. return !readOnly;
  488. }
  489. }
  490. },
  491. });
  492. }
  493. $('#exportExcel').click(function () {
  494. SpreadExcelObj.exportSimpleXlsxSheet(otherSpreadSetting, seOtherObj.data, $('.sidebar-title').attr('data-original-title') + "-其他.xlsx");
  495. });
  496. });