se_other.js 22 KB

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