se_other.js 20 KB

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