se_other.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. editEnded: function (e, info) {
  265. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  266. const node = info.sheet.zh_data[info.row];
  267. const col = info.sheet.zh_setting.cols[info.col];
  268. const data = {};
  269. if (node) {
  270. data.update = {};
  271. data.update.id = node.id;
  272. const oldValue = node ? node[col.field] : null;
  273. const newValue = trimInvalidChar(info.editingText);
  274. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  275. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  276. return;
  277. }
  278. data.update[col.field] = newValue;
  279. } else {
  280. if (col.field !== 'name') {
  281. toastr.warning('请先输入名称');
  282. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  283. return;
  284. }
  285. data.add = {};
  286. data.add.order = info.row + 1;
  287. data.add.name = trimInvalidChar(info.editingText);
  288. }
  289. postData(window.location.pathname + '/update', data, function (result) {
  290. seOtherObj.loadUpdateData(result);
  291. SpreadJsObj.reLoadSheetData(info.sheet);
  292. refreshSum();
  293. }, function () {
  294. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  295. });
  296. },
  297. editStarting(e, info) {
  298. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  299. info.cancel = true;
  300. return;
  301. }
  302. const col = info.sheet.zh_setting.cols[info.col];
  303. const node = info.sheet.zh_data[info.row];
  304. if (!node) return;
  305. switch (col.field) {
  306. case 'name':
  307. case 'total_price':
  308. info.cancel = readOnly || node.pre_used;
  309. break;
  310. }
  311. },
  312. clipboardPasting(e, info) {
  313. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  314. info.cancel = true;
  315. if (!setting || !sortData) return;
  316. const pasteData = info.pasteData.html
  317. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  318. : (info.pasteData.text === ''
  319. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  320. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  321. const hint = {
  322. name: {type: 'warning', msg: '名称不可为空,已过滤'},
  323. tp: {type: 'warning', msg: '输入的 金额 非法,已过滤'},
  324. };
  325. const uDatas = [], iDatas = [];
  326. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  327. const curRow = info.cellRange.row + iRow;
  328. const node = sortData[curRow];
  329. let bPaste = false;
  330. const data = {};
  331. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  332. const curCol = info.cellRange.col + iCol;
  333. const colSetting = setting.cols[curCol];
  334. const value = trimInvalidChar(pasteData[iRow][iCol]);
  335. if (colSetting.field === 'name' && (!value || value === '')) {
  336. toastMessageUniq(hint.name);
  337. break;
  338. }
  339. if (colSetting.type === 'Number') {
  340. const num = _.toNumber(value);
  341. if (num) {
  342. data[colSetting.field] = num;
  343. bPaste = true;
  344. }
  345. } else {
  346. data[colSetting.field] = value;
  347. bPaste = true;
  348. }
  349. }
  350. if (bPaste) {
  351. if (node) {
  352. data.id = node.id;
  353. uDatas.push(data);
  354. } else {
  355. data.order = curRow + 1;
  356. iDatas.push(data);
  357. }
  358. }
  359. }
  360. const updateData = {};
  361. if (uDatas.length > 0) updateData.update = uDatas;
  362. if (iDatas.length > 0) updateData.add = iDatas;
  363. if (uDatas.length > 0 || iDatas.length > 0) {
  364. postData(window.location.pathname + '/update', updateData, function (result) {
  365. seOtherObj.loadUpdateData(result);
  366. SpreadJsObj.reLoadSheetData(info.sheet);
  367. refreshSum();
  368. });
  369. } else {
  370. SpreadJsObj.reLoadSheetData(info.sheet);
  371. }
  372. },
  373. upMove: function () {
  374. const sels = otherSheet.getSelections(), sortData = otherSheet.zh_data;
  375. const node = sortData[sels[0].row];
  376. const preNode = sortData[sels[0].row - 1];
  377. const data = [
  378. {id: node.id, order: preNode.order},
  379. {id: preNode.id, order: node.order}
  380. ];
  381. postData(window.location.pathname + '/update', {update: data}, function (result) {
  382. seOtherObj.loadUpdateData(result);
  383. SpreadJsObj.reLoadRowsData(otherSheet, [sels[0].row, sels[0].row - 1]);
  384. otherSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  385. });
  386. },
  387. downMove: function () {
  388. const sels = otherSheet.getSelections(), sortData = otherSheet.zh_data;
  389. const node = sortData[sels[0].row];
  390. const nextNode = sortData[sels[0].row + 1];
  391. const data = [
  392. {id: node.id, order: nextNode.order},
  393. {id: nextNode.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. };
  402. otherSheet.bind(spreadNS.Events.EditEnded, seOtherOprObj.editEnded);
  403. otherSheet.bind(spreadNS.Events.EditStarting, seOtherOprObj.editStarting);
  404. otherSheet.bind(spreadNS.Events.ClipboardPasting, seOtherOprObj.clipboardPasting);
  405. SpreadJsObj.addDeleteBind(otherSpread, seOtherOprObj.deletePress);
  406. $.contextMenu({
  407. selector: '#other-spread',
  408. build: function ($trigger, e) {
  409. const target = SpreadJsObj.safeRightClickSelection($trigger, e, otherSpread);
  410. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  411. },
  412. items: {
  413. del: {
  414. name: '删除',
  415. icon: 'fa-remove',
  416. callback: function (key, opt) {
  417. seOtherOprObj.delete(otherSheet);
  418. },
  419. disabled: function (key, opt) {
  420. const sels = otherSheet.getSelections();
  421. if (!sels || !sels[0]) return true;
  422. const row = sels[0].row;
  423. const node = seOtherObj.data[row];
  424. return node === undefined || node === null;
  425. },
  426. visible: function (key, opt) {
  427. return !readOnly;
  428. }
  429. },
  430. sprDel: '------------',
  431. upMove: {
  432. name: '上移',
  433. icon: 'fa-arrow-up',
  434. callback: function (key, opt) {
  435. seOtherOprObj.upMove();
  436. },
  437. disabled: function (key, opt) {
  438. const sels = otherSheet.getSelections();
  439. if (!sels || !sels[0] || sels[0].row === 0) return true;
  440. const row = sels[0].row;
  441. const node = seOtherObj.data[row];
  442. return node === undefined || node === null;
  443. },
  444. visible: function (key, opt) {
  445. return !readOnly;
  446. }
  447. },
  448. downMove: {
  449. name: '下移',
  450. icon: 'fa-arrow-down',
  451. callback: function (key, opt) {
  452. seOtherOprObj.downMove();
  453. },
  454. disabled: function (key, opt) {
  455. const sels = otherSheet.getSelections();
  456. if (!sels || !sels[0] || sels[0].row >= seOtherObj.data.length - 1) return true;
  457. const row = sels[0].row;
  458. const node = seOtherObj.data[row];
  459. return node === undefined || node === null;
  460. },
  461. visible: function (key, opt) {
  462. return !readOnly;
  463. }
  464. }
  465. },
  466. })
  467. }
  468. $('#exportExcel').click(function () {
  469. SpreadExcelObj.exportSimpleXlsxSheet(otherSpreadSetting, seOtherObj.data, $('.sidebar-title').attr('data-original-title') + "-其他.xlsx");
  470. });
  471. });