se_other.js 19 KB

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