payment_safe.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. function getTenderId() {
  2. return window.location.pathname.split('/')[2];
  3. }
  4. const invalidFields = {
  5. parent: ['cur_qty', 'cur_tp', 'unit_price'],
  6. };
  7. $(document).ready(function() {
  8. let stdGcl;
  9. autoFlashHeight();
  10. class BillsObj {
  11. constructor() {
  12. this.spread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
  13. this.sheet = this.spread.getActiveSheet();
  14. this.treeSetting = {
  15. id: 'tree_id',
  16. pid: 'tree_pid',
  17. order: 'tree_order',
  18. level: 'tree_level',
  19. isLeaf: 'tree_is_leaf',
  20. fullPath: 'tree_full_path',
  21. rootId: -1,
  22. calcFields: ['cur_tp', 'pre_tp', 'end_tp'],
  23. keys: ['id', 'detail_id', 'tree_id'],
  24. };
  25. this.tree = createNewPathTree('ledger', this.treeSetting);
  26. this.spreadSetting = {
  27. cols: [
  28. {title: '编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 230, formatter: '@', cellType: 'tree'},
  29. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 185, formatter: '@', cellType: 'autoTip'},
  30. {title: '规格', colSpan: '1', rowSpan: '2', field: 'spec', hAlign: 0, width: 150, formatter: '@'},
  31. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', cellType: 'unit'},
  32. {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 80, type: 'Number'},
  33. {title: '本期|数量', colSpan: '2|1', rowSpan: '1|1', field: 'cur_qty', hAlign: 2, width: 80, type: 'Number'},
  34. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'cur_tp', hAlign: 2, width: 80, type: 'Number', readOnly: true},
  35. {title: '截止本期|数量', colSpan: '2|1', rowSpan: '1|1', field: 'end_qty', hAlign: 2, width: 80, type: 'Number'},
  36. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'end_tp', hAlign: 2, width: 80, type: 'Number', readOnly: true},
  37. {title: '发票号', colSpan: '1', rowSpan: '2', field: 'invoice_code', hAlign: 0, width: 100, formatter: '@'},
  38. {title: '备注', colSpan: '1', rowSpan: '2', field: 'memo', hAlign: 0, width: 120, formatter: '@', cellType: 'ellipsisAutoTip'},
  39. ],
  40. emptyRows: 3,
  41. headRows: 2,
  42. headRowHeight: [25, 25],
  43. defaultRowHeight: 21,
  44. headerFont: '12px 微软雅黑',
  45. font: '12px 微软雅黑',
  46. readOnly: readOnly,
  47. localCache: {
  48. key: 'payment-safe-bills',
  49. colWidth: true,
  50. },
  51. };
  52. this.ckBillsSpread = window.location.pathname + '-billsSelect';
  53. this.initSpread();
  54. this.initOtherEvent();
  55. }
  56. initSpread() {
  57. SpreadJsObj.initSheet(this.sheet, this.spreadSetting);
  58. this.spread.bind(spreadNS.Events.SelectionChanged, this.selectionChanged);
  59. this.spread.bind(spreadNS.Events.topRowChanged, this.topRowChanged);
  60. this.spread.bind(spreadNS.Events.ClipboardChanging, function (e, info) {
  61. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  62. SpreadJsObj.Clipboard.setCopyData(copyText);
  63. });
  64. if (readOnly) return;
  65. this.spread.bind(spreadNS.Events.EditEnded, this.editEnded);
  66. this.spread.bind(spreadNS.Events.EditStarting, this.editStarting);
  67. this.spread.bind(spreadNS.Events.ClipboardPasting, this.clipboardPasting);
  68. SpreadJsObj.addDeleteBind(this.spread, this.deletePress);
  69. }
  70. initOtherEvent() {
  71. const self = this;
  72. // 增删上下移升降级
  73. $('a[name="base-opr"]').click(function () {
  74. self.baseOpr(this.getAttribute('type'));
  75. });
  76. }
  77. refreshOperationValid() {
  78. const setObjEnable = function (obj, enable) {
  79. if (enable) {
  80. obj.removeClass('disabled');
  81. } else {
  82. obj.addClass('disabled');
  83. }
  84. };
  85. const invalidAll = function () {
  86. setObjEnable($('a[name=base-opr][type=add]'), false);
  87. setObjEnable($('a[name=base-opr][type=delete]'), false);
  88. setObjEnable($('a[name=base-opr][type=up-move]'), false);
  89. setObjEnable($('a[name=base-opr][type=down-move]'), false);
  90. setObjEnable($('a[name=base-opr][type=up-level]'), false);
  91. setObjEnable($('a[name=base-opr][type=down-level]'), false);
  92. };
  93. const sel = this.sheet.getSelections()[0];
  94. const row = sel ? sel.row : -1;
  95. const tree = this.sheet.zh_tree;
  96. if (!tree) {
  97. invalidAll();
  98. return;
  99. }
  100. const first = tree.nodes[row];
  101. if (!first) {
  102. invalidAll();
  103. return;
  104. }
  105. let last = first, sameParent = true, nodeUsed = this.checkNodeUsed(tree, first);
  106. if (sel.rowCount > 1 && first) {
  107. for (let r = 1; r < sel.rowCount; r++) {
  108. const rNode = tree.nodes[sel.row + r];
  109. if (!rNode) {
  110. sameParent = false;
  111. break;
  112. }
  113. nodeUsed = nodeUsed || this.checkNodeUsed(tree, rNode);
  114. if (rNode.tree_level > first.tree_level) continue;
  115. if ((rNode.tree_level < first.tree_level) || (rNode.tree_level === first.tree_level && rNode.tree_pid !== first.tree_pid)) {
  116. sameParent = false;
  117. break;
  118. }
  119. last = rNode;
  120. }
  121. }
  122. const preNode = tree.getPreSiblingNode(first);
  123. const valid = !this.sheet.zh_setting.readOnly;
  124. setObjEnable($('a[name=base-opr][type=add]'), valid && first && first.tree_level > 1);
  125. setObjEnable($('a[name=base-opr][type=delete]'), valid && first && sameParent && first.tree_level > 1 && !nodeUsed);
  126. setObjEnable($('a[name=base-opr][type=up-move]'), valid && first && sameParent && first.tree_level > 1 && preNode);
  127. setObjEnable($('a[name=base-opr][type=down-move]'), valid && first && sameParent && first.tree_level > 1 && !tree.isLastSibling(last));
  128. setObjEnable($('a[name=base-opr][type=up-level]'), valid && first && sameParent && tree.getParent(first) && !nodeUsed && first.tree_level > 2);
  129. setObjEnable($('a[name=base-opr][type=down-level]'), valid && first && sameParent && first.tree_level > 1 && preNode && !this.checkNodeUsed(tree, preNode));
  130. }
  131. loadRelaData() {
  132. this.refreshOperationValid();
  133. SpreadJsObj.saveTopAndSelect(this.sheet, this.ckBillsSpread);
  134. attObj.setSafeBills();
  135. }
  136. refreshTree(data) {
  137. const sheet = this.sheet;
  138. SpreadJsObj.massOperationSheet(sheet, function () {
  139. const tree = sheet.zh_tree;
  140. // 处理删除
  141. if (data.delete) {
  142. data.delete.sort(function (a, b) {
  143. return b.deleteIndex - a.deleteIndex;
  144. });
  145. for (const d of data.delete) {
  146. sheet.deleteRows(d.deleteIndex, 1);
  147. }
  148. }
  149. // 处理新增
  150. if (data.create) {
  151. const newNodes = data.create;
  152. if (newNodes) {
  153. newNodes.sort(function (a, b) {
  154. return a.index - b.index;
  155. });
  156. for (const node of newNodes) {
  157. sheet.addRows(node.index, 1);
  158. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  159. }
  160. }
  161. }
  162. // 处理更新
  163. if (data.update) {
  164. const rows = [];
  165. for (const u of data.update) {
  166. rows.push(tree.nodes.indexOf(u));
  167. }
  168. SpreadJsObj.reLoadRowsData(sheet, rows);
  169. }
  170. // 处理展开
  171. if (data.expand) {
  172. const expanded = [];
  173. for (const e of data.expand) {
  174. if (expanded.indexOf(e) === -1) {
  175. const posterity = tree.getPosterity(e);
  176. for (const p of posterity) {
  177. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  178. expanded.push(p);
  179. }
  180. }
  181. }
  182. }
  183. });
  184. }
  185. loadData(datas) {
  186. this.tree.loadDatas(datas);
  187. treeCalc.calculateAll(this.tree);
  188. SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Tree, this.tree);
  189. SpreadJsObj.loadTopAndSelect(this.sheet, this.ckBillsSpread);
  190. this.refreshOperationValid();
  191. }
  192. getDefaultSelectInfo() {
  193. if (!this.tree) return;
  194. const sel = this.sheet.getSelections()[0];
  195. const node = this.sheet.zh_tree.nodes[sel.row];
  196. if (!node) return;
  197. let count = 1;
  198. if (sel.rowCount > 1) {
  199. for (let r = 1; r < sel.rowCount; r++) {
  200. const rNode = this.sheet.zh_tree.nodes[sel.row + r];
  201. if (rNode.tree_level > node.tree_level) continue;
  202. if ((rNode.tree_level < node.tree_level) || (rNode.tree_level === node.tree_level && rNode.tree_pid !== node.tree_pid)) {
  203. toastr.warning('请选择同一节点下的节点,进行该操作');
  204. return;
  205. }
  206. count += 1;
  207. }
  208. }
  209. return [this.tree, node, count];
  210. }
  211. checkNodeUsed(tree, node) {
  212. if (node.pre_qty || node.pre_tp) return true;
  213. const posterity = tree.getPosterity(node);
  214. for (const p of posterity) {
  215. if (p.pre_qty || p.pre_tp) return true;
  216. }
  217. return false;
  218. }
  219. baseOpr(type, addCount = 1) {
  220. const self = this;
  221. const sheet = self.sheet;
  222. const sel = sheet.getSelections()[0];
  223. const [tree, node, count] = this.getDefaultSelectInfo();
  224. if (!tree || !node || !count) return;
  225. if (type === 'delete') {
  226. const parent = tree.getParent(node);
  227. const children = parent ? parent.children : tree.children;
  228. const index = children.indexOf(node);
  229. for (let i = 0; i < count; i++) {
  230. const child = children[i+index];
  231. if (this.checkNodeUsed(tree, child)) {
  232. toastr.warning('选中的节点已计量,不可删除');
  233. return;
  234. }
  235. }
  236. } else if (type === 'up-level') {
  237. const parent = tree.getParent(node);
  238. const children = parent ? parent.children : tree.children;
  239. const index = children.indexOf(node);
  240. for (let i = index; i < children.length; i++) {
  241. const child = children[index];
  242. if (this.checkNodeUsed(tree, child)) {
  243. if (i >= index + count) {
  244. toastr.warning('其后节点已计量,选中的节点不可升级');
  245. } else {
  246. toastr.warning('选中的节点已计量,不可升级');
  247. }
  248. return;
  249. }
  250. }
  251. } else if (type === 'down-level') {
  252. const parent = tree.getParent(node);
  253. const children = parent ? parent.children : tree.children;
  254. const index = children.indexOf(node);
  255. if (index > 0 && this.checkNodeUsed(tree, children[index-1])) {
  256. toastr.warning('其前节点已计量,选中的节点不可降级');
  257. return;
  258. }
  259. for (let i = index; i < count; i++) {
  260. const child = children[i+index];
  261. if (this.checkNodeUsed(tree, child)) {
  262. toastr.warning('选中的节点已计量,不可降级');
  263. return;
  264. }
  265. }
  266. }
  267. const updateData = {
  268. postType: type,
  269. postData: {
  270. id: node.tree_id,
  271. count: type === 'add' ? addCount : count,
  272. }
  273. };
  274. if (type === 'delete') {
  275. deleteAfterHint(function () {
  276. postData('update', updateData, function (result) {
  277. const refreshData = tree.loadPostData(result);
  278. self.refreshTree(refreshData);
  279. if (sel) {
  280. sheet.setSelection(sel.row, sel.col, 1, sel.colCount);
  281. }
  282. self.refreshOperationValid();
  283. });
  284. });
  285. } else {
  286. postData('update', updateData, function (result) {
  287. const refreshData = tree.loadPostData(result);
  288. self.refreshTree(refreshData);
  289. if (['up-move', 'down-move'].indexOf(type) > -1) {
  290. if (sel) {
  291. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  292. // SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(node)]);
  293. }
  294. } else if (type === 'add') {
  295. const sel = sheet.getSelections()[0];
  296. if (sel) {
  297. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  298. // SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(refreshData.create[0])]);
  299. }
  300. }
  301. self.refreshOperationValid();
  302. });
  303. }
  304. }
  305. // 事件
  306. selectionChanged(e, info) {
  307. if (info.newSelections) {
  308. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) {
  309. billsObj.loadRelaData();
  310. }
  311. }
  312. }
  313. topRowChanged(e, info) {
  314. SpreadJsObj.saveTopAndSelect(info.sheet, billsObj.ckBillsSpread);
  315. }
  316. editEnded(e, info) {
  317. if (!info.sheet.zh_setting) return;
  318. const tree = info.sheet.zh_tree;
  319. const node = SpreadJsObj.getSelectObject(info.sheet);
  320. const data = { id: node.id, detail_id: node.detail_id, tree_id: node.tree_id };
  321. // 未改变值则不提交
  322. const col = info.sheet.zh_setting.cols[info.col];
  323. const orgValue = node[col.field];
  324. const newValue = trimInvalidChar(info.editingText);
  325. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) return;
  326. if (info.editingText) {
  327. const text = newValue;
  328. if (billsObj.checkNodeUsed(tree, node) && col.field ==='b_code' && orgValue !== '' && text === '') {
  329. toastr.error('节点已计量,请勿删除编号');
  330. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  331. return;
  332. }
  333. if (col.type === 'Number') {
  334. const num = _.toNumber(text);
  335. if (_.isFinite(num)) {
  336. data[col.field] = num;
  337. } else {
  338. try {
  339. data[col.field] = math.evaluate(transExpr(text));
  340. } catch(err) {
  341. toastr.error('输入的表达式非法');
  342. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  343. return;
  344. }
  345. }
  346. } else {
  347. data[col.field] = text;
  348. }
  349. } else {
  350. if (billsObj.checkNodeUsed(tree, node) && (col.field ==='b_code') && orgValue !== '') {
  351. toastr.error('节点已计量,请勿删除编号');
  352. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  353. return;
  354. }
  355. data[col.field] = col.type === 'Number' ? 0 : '';
  356. }
  357. // 更新至服务器
  358. postData('update', {postType: 'update', postData: data}, function (result) {
  359. const refreshNode = billsObj.tree.loadPostData(result);
  360. billsObj.refreshTree(refreshNode);
  361. }, function () {
  362. SpreadJsObj.reLoadRowData(info.sheet, info.row, 1);
  363. });
  364. }
  365. editStarting(e, info) {
  366. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  367. const tree = info.sheet.zh_tree;
  368. const col = info.sheet.zh_setting.cols[info.col];
  369. const node = info.sheet.zh_tree.nodes[info.row];
  370. if (!node) {
  371. info.cancel = true;
  372. return;
  373. }
  374. switch (col.field) {
  375. case 'b_code':
  376. info.cancel = readOnly || billsObj.checkNodeUsed(tree, node);
  377. break;
  378. case 'unit_price':
  379. info.cancel = readOnly || (node.children && node.children.length > 0);
  380. break;
  381. case 'cur_qty':
  382. case 'cur_tp':
  383. info.cancel = (node.children && node.children.length > 0);
  384. break;
  385. }
  386. }
  387. deletePress (sheet) {
  388. if (!sheet.zh_setting) return;
  389. const sel = sheet.getSelections()[0], datas = [];
  390. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  391. let bDel = false;
  392. const node = sheet.zh_tree.nodes[iRow];
  393. const data = sheet.zh_tree.getNodeKeyData(node);
  394. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  395. const col = sheet.zh_setting.cols[iCol];
  396. const style = sheet.getStyle(iRow, iCol);
  397. if (style.locked) continue;
  398. if (col.field === 'b_code' && sheet.zh_tree.checkNodeUsed(node, pos)) {
  399. toastr.warning(`"${node.b_code || ''} ${node.name}"已计量,请勿修改`);
  400. return;
  401. }
  402. data[col.field] = col.type === 'Number' ? 0 : '';
  403. bDel = true;
  404. }
  405. if (bDel) datas.push(data);
  406. }
  407. if (datas.length > 0) {
  408. postData('update', {postType: 'update', postData: datas}, function (result) {
  409. const refreshNode = sheet.zh_tree.loadPostData(result);
  410. billsObj.refreshTree(refreshNode);
  411. }, function () {
  412. SpreadJsObj.reLoadRowData(info.sheet, sel.row, sel.rowCount);
  413. });
  414. }
  415. }
  416. clipboardPasting(e, info) {
  417. info.cancel = true;
  418. const tree = info.sheet.zh_tree, setting = info.sheet.zh_setting;
  419. if (!setting || !tree) return;
  420. const pasteData = info.pasteData.html
  421. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  422. : (info.pasteData.text === ''
  423. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  424. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  425. const hint = {
  426. usedUp: {type: 'warning', msg: '节点已计量,不可修改单价'},
  427. usedCode: {type: 'warning', msg: '节点已计量,编号不可修改'},
  428. invalidExpr: {type: 'warning', msg: '粘贴的表达式非法'},
  429. parent: {type: 'warning', msg: '含有子项的清单,不可粘贴数量、单价、金额'},
  430. };
  431. const datas = [], filterNodes = [];
  432. let level, filterRow = 0;
  433. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  434. const curRow = info.cellRange.row + iRow;
  435. const node = tree.nodes[curRow];
  436. if (!node) continue;
  437. if (!level) level = node.level;
  438. if (node.level < level) break;
  439. let bPaste = false;
  440. const data = info.sheet.zh_tree.getNodeKeyData(node);
  441. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  442. const curCol = info.cellRange.col + iCol;
  443. const colSetting = info.sheet.zh_setting.cols[curCol];
  444. const value = trimInvalidChar(pasteData[iRow-filterRow][iCol]);
  445. if (node.children && node.children.length > 0 && invalidFields.parent.indexOf(colSetting.field) >= 0) {
  446. toastMessageUniq(hint.parent);
  447. continue;
  448. }
  449. if (billsObj.checkNodeUsed(tree, node) && colSetting.field === 'unit_price') {
  450. toastMessageUniq (hint.usedUp);
  451. continue;
  452. }
  453. if (colSetting.type === 'Number') {
  454. const num = _.toNumber(value);
  455. if (num) {
  456. data[colSetting.field] = num;
  457. } else {
  458. try {
  459. data[colSetting.field] = math.evaluate(transExpr(value));
  460. bPaste = true;
  461. } catch (err) {
  462. toastMessageUniq(hint.invalidExpr);
  463. continue;
  464. }
  465. }
  466. } else {
  467. if (node.used && (colSetting.field ==='b_code') && data[colSetting.field] !== '' && value === '') {
  468. toastMessageUniq(hint.usedCode);
  469. continue;
  470. }
  471. data[colSetting.field] = value;
  472. }
  473. bPaste = true;
  474. }
  475. if (bPaste) {
  476. datas.push(data);
  477. } else {
  478. filterNodes.push(node);
  479. }
  480. }
  481. if (datas.length > 0) {
  482. postData('update', {postType: 'update', postData: datas}, function (result) {
  483. const refreshNode = tree.loadPostData(result);
  484. if (refreshNode.update) refreshNode.update = refreshNode.update.concat(filterNodes);
  485. billsObj.refreshTree(refreshNode);
  486. }, function () {
  487. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  488. });
  489. } else {
  490. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  491. }
  492. }
  493. }
  494. const billsObj = new BillsObj();
  495. // 清单右键菜单
  496. const billsContextMenuOptions = {
  497. selector: '#bills-spread',
  498. build: function ($trigger, e) {
  499. const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread);
  500. billsObj.loadRelaData();
  501. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  502. },
  503. items: {}
  504. };
  505. if (!readOnly) {
  506. billsContextMenuOptions.items.create = {
  507. name: '新增',
  508. icon: 'fa-sign-in',
  509. callback: function (key, opt) {
  510. billsObj.baseOpr('add');
  511. },
  512. disabled: function (key, opt) {
  513. const sheet = billsObj.sheet;
  514. const selection = sheet.getSelections();
  515. const sel = selection ? selection[0] : sheet.getSelections()[0];
  516. const row = sel ? sel.row : -1;
  517. const tree = sheet.zh_tree;
  518. if (!tree) return true;
  519. const first = sheet.zh_tree.nodes[row];
  520. const valid = !sheet.zh_setting.readOnly;
  521. return !(valid && first && first.tree_level > 1);
  522. }
  523. };
  524. billsContextMenuOptions.items.delete = {
  525. name: '删除',
  526. icon: 'fa-remove',
  527. callback: function (key, opt) {
  528. billsObj.baseOpr('delete');
  529. },
  530. disabled: function (key, opt) {
  531. const sheet = billsObj.sheet;
  532. const selection = sheet.getSelections();
  533. const sel = selection ? selection[0] : sheet.getSelections()[0];
  534. const row = sel ? sel.row : -1;
  535. const tree = sheet.zh_tree;
  536. if (!tree) return true;
  537. const first = sheet.zh_tree.nodes[row];
  538. let last = first, sameParent = true, nodeUsed = billsObj.checkNodeUsed(tree, first);
  539. if (sel.rowCount > 1 && first) {
  540. for (let r = 1; r < sel.rowCount; r++) {
  541. const rNode = tree.nodes[sel.row + r];
  542. if (!rNode) {
  543. sameParent = false;
  544. break;
  545. }
  546. nodeUsed = nodeUsed || billsObj.checkNodeUsed(tree, rNode);
  547. if (rNode.tree_level > first.tree_level) continue;
  548. if ((rNode.tree_level < first.tree_level) || (rNode.tree_level === first.tree_level && rNode.tree_pid !== first.tree_pid)) {
  549. sameParent = false;
  550. break;
  551. }
  552. last = rNode;
  553. }
  554. }
  555. const valid = !sheet.zh_setting.readOnly;
  556. return !(valid && first && sameParent && !(first.tree_level === 1) && !nodeUsed);
  557. }
  558. };
  559. }
  560. class AttObj {
  561. constructor() {
  562. this.atts = [];
  563. this.billsIndexes = {};
  564. this.pageCount = 15;
  565. this.activeTab = 'cur';
  566. this.curPageIndex = 1;
  567. this.curTotalPage = 1;
  568. this.allPageIndex = 1;
  569. this.allTotalPage = 1;
  570. }
  571. refreshShowPage() {
  572. const prefix = this.activeTab;
  573. $('#totalPage').html(this[prefix + 'TotalPage']);
  574. $('#currentPage').html(this[prefix + 'PageIndex']);
  575. if (this[prefix + 'TotalPage'] > 1) {
  576. $('#showPage').show();
  577. } else {
  578. $('#showPage').hide();
  579. }
  580. }
  581. reCalcPage() {
  582. this.allTotalPage = Math.ceil(this.atts.length / this.pageCount);
  583. const curNode = SpreadJsObj.getSelectObject(billsObj.sheet);
  584. const curAttIndex = curNode ? this.billsIndexes[curNode.safe_id] || [] : [];
  585. this.curTotalPage = Math.ceil(curAttIndex.length / this.pageCount);
  586. this.refreshShowPage();
  587. }
  588. loadDatas(datas) {
  589. for (const d of datas) {
  590. this.atts.push(d);
  591. if (!this.billsIndexes[d.safe_id]) {
  592. this.billsIndexes[d.safe_id] = [];
  593. }
  594. this.billsIndexes[d.safe_id].push(d);
  595. }
  596. this.refreshTable();
  597. }
  598. loadUpdateAtt(files) {
  599. for (const d of files) {
  600. this.atts.unshift(d);
  601. if (!this.billsIndexes[d.safe_id]) {
  602. this.billsIndexes[d.safe_id] = [];
  603. }
  604. this.billsIndexes[d.safe_id].unshift(d);
  605. }
  606. }
  607. loadDeleteAtt(file) {
  608. let fileIndex = this.atts.findIndex(x => { return x.id === file.id; });
  609. this.atts.splice(fileIndex, 1);
  610. if (this.billsIndexes[file.safe_id]) {
  611. fileIndex = this.billsIndexes[file.safe_id].findIndex(x => { return x.id === file.id; });
  612. this.billsIndexes[file.safe_id].splice(fileIndex, 1);
  613. }
  614. }
  615. getFileHtml(file) {
  616. const html = [];
  617. const downHtml = `<a href="javascript: void(0);" onclick="AliOss.downloadFile('${file.filepath}', '${file.filename + file.fileext}')" class="mr-1"><i class="fa fa-download fa-fw"></i></a>`;
  618. const delHtml = file.uid === userID ? `<a href="javascript: void(0);" class="mr-1 text-danger" name="del-file" file_id="${file.id}"><i class="fa fa-trash-o fa-fw"></i></a>` : '';
  619. const locateHtml = `<a href="javascript: void(0);" name="locate-bills" file_id="${file.id}" class="mr-1"><i data-toggle="tooltip" data-placement="left" data-original-title="定位" class="fa fa-crosshairs"></i></a>`;
  620. html.push('<tr>');
  621. html.push(`<td><input type="checkbox" name="check-att" file_id="${file.id}"></td>`);
  622. html.push(`<td><div class="d-flex justify-content-between align-items-center table-file"><a href="javascript: void(0);" file_id="${file.id}" name="view-file">${file.filename}${file.fileext}</a><div class="btn-group-table" style="display: none;">${locateHtml}${downHtml}${delHtml}</div></div></td>`);
  623. html.push(`<td>${file.u_name}</td>`);
  624. html.push('</tr>');
  625. return html.join('');
  626. }
  627. refreshCurTable() {
  628. const curNode = SpreadJsObj.getSelectObject(billsObj.sheet);
  629. const html = [];
  630. if (curNode) {
  631. const attIndex = this.billsIndexes[curNode.safe_id] || [];
  632. const beginIndex = (this.curPageIndex - 1) * this.pageCount;
  633. const endIndex = (this.curPageIndex) * this.pageCount - 1;
  634. for (const [i, ai] of attIndex.entries()) {
  635. if (i < beginIndex) continue;
  636. if (i > endIndex) continue;
  637. html.push(this.getFileHtml(ai));
  638. }
  639. }
  640. $('#cur-att-list').html(html.join(''));
  641. }
  642. refreshAllTable() {
  643. const html = [];
  644. const attIndex = this.atts;
  645. const beginIndex = (this.allPageIndex - 1) * this.pageCount;
  646. const endIndex = (this.allPageIndex) * this.pageCount - 1;
  647. for (const [i, ai] of attIndex.entries()) {
  648. if (i < beginIndex) continue;
  649. if (i > endIndex) continue;
  650. html.push(this.getFileHtml(ai));
  651. }
  652. $('#all-att-list').html(html.join(''));
  653. }
  654. prePage(){
  655. const pageIndex = this.activeTab + 'PageIndex';
  656. if (this[pageIndex] <= 1) return;
  657. this[pageIndex] = this[pageIndex] - 1;
  658. this.refreshShowTable();
  659. this.refreshShowPage();
  660. }
  661. nextPage() {
  662. const pageIndex = this.activeTab + 'PageIndex';
  663. if (this[pageIndex] >= this[this.activeTab + 'TotalPage']) return;
  664. this[pageIndex] = this[pageIndex] + 1;
  665. this.refreshShowTable();
  666. this.refreshShowPage();
  667. }
  668. refreshTable() {
  669. this.reCalcPage();
  670. this.refreshCurTable();
  671. this.refreshAllTable();
  672. }
  673. refreshShowTable() {
  674. if (this.activeTab === 'cur') {
  675. this.refreshCurTable();
  676. } else {
  677. this.refreshAllTable();
  678. }
  679. }
  680. batchDownload() {
  681. const checkes = $('[name=check-att]:checked');
  682. if (checkes.length === 0) {
  683. toastr.warning('请选择需要批量下载的文件');
  684. return;
  685. }
  686. const files = [], fileIds = [];
  687. for (const c of checkes) {
  688. const file = this.atts.find(x => { return x.id === parseInt(c.getAttribute('file_id'))});
  689. if (file && fileIds.indexOf(file.id) < 0) {
  690. fileIds.push(file.id);
  691. files.push({ filename: file.filename, fileext: file.fileext, filepath: c.viewpath || file.filepath});
  692. }
  693. }
  694. AliOss.zipFiles(files, '安全生产费-附件.zip', (fails) => {
  695. $(self).removeAttr('disabled');
  696. if (fails.length === 0) {
  697. toastr.success('下载成功');
  698. } else {
  699. toastr.warning(`下载成功(${fails.length}个文件下载失败)`);
  700. }
  701. }, () => {
  702. $(self).removeAttr('disabled');
  703. toastr.error('批量下载失败');
  704. });
  705. }
  706. uploadAtt(files, callback) {
  707. const curNode = SpreadJsObj.getSelectObject(billsObj.sheet);
  708. const formData = new FormData();
  709. if (curNode) formData.append('safe_id', curNode.safe_id);
  710. for (const file of files) {
  711. if (file === undefined) {
  712. toastr.error('未选择上传文件。');
  713. return false;
  714. }
  715. if (file.size > 30 * 1024 * 1024) {
  716. toastr.error('上传文件大小超过30MB。');
  717. return false;
  718. }
  719. const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
  720. if (whiteList.indexOf(fileext) === -1) {
  721. toastr.error('仅支持office文档、图片、压缩包格式,请勿上传' + fileext + '格式文件。');
  722. return false;
  723. }
  724. formData.append('size', file.size);
  725. formData.append('file[]', file);
  726. }
  727. postDataWithFile('file/upload', formData, function (files) {
  728. attObj.loadUpdateAtt(files);
  729. attObj.refreshTable();
  730. if (callback) callback();
  731. });
  732. }
  733. deleteAtt(file_id) {
  734. const file = this.atts.find(x => { return x.id === file_id; });
  735. if (!file) return;
  736. postData('file/delete', {id: file_id}, function() {
  737. attObj.loadDeleteAtt(file);
  738. attObj.refreshTable();
  739. })
  740. }
  741. locateBills(file_id) {
  742. const file = this.atts.find(x => { return x.id === file_id; });
  743. if (!file || !file.safe_id) return;
  744. const safeBills = billsObj.tree.nodes.find(x => { return x.safe_id === file.safe_id; });
  745. SpreadJsObj.locateTreeNode(billsObj.sheet, safeBills.tree_id, true);
  746. this.refreshCurTable();
  747. }
  748. setTab(tab) {
  749. this.activeTab = tab;
  750. this.refreshShowPage();
  751. }
  752. setSafeBills() {
  753. this.curPageIndex = 1;
  754. this.reCalcPage();
  755. this.refreshCurTable();
  756. }
  757. }
  758. const attObj = new AttObj();
  759. $('#upload-ok').click(function() {
  760. const input = $('#upload-file');
  761. attObj.uploadAtt(input[0].files, function() {
  762. $(input).val('');
  763. $('#upload').modal('hide');
  764. });
  765. });
  766. $('#batch-download-att').click(() => {
  767. attObj.batchDownload();
  768. });
  769. $('body').on('mouseenter', ".table-file", function(){
  770. $(this).children(".btn-group-table").css("display","block");
  771. });
  772. $('body').on('mouseleave', ".table-file", function(){
  773. $(this).children(".btn-group-table").css("display","none");
  774. });
  775. $('body').on('click', '[name=del-file]', function() {
  776. attObj.deleteAtt(parseInt(this.getAttribute('file_id')));
  777. });
  778. $('body').on('click', 'a[name=view-file]', function() {
  779. const file_id = parseInt(this.getAttribute('file_id'));
  780. const file = attObj.atts.find(x => { return x.id === file_id; });
  781. if (!file) return;
  782. if (file.viewpath) {
  783. window.open(file.viewpath, '_blank');
  784. } else {
  785. AliOss.downloadFile(file.filepath, `${file.filename}${file.fileext}`)
  786. }
  787. });
  788. $('body').on('click', 'a[name=locate-bills]', function() {
  789. attObj.locateBills(parseInt(this.getAttribute('file_id')));
  790. });
  791. $('.page-select').click(function() {
  792. const content = this.getAttribute('content');
  793. if (content === 'pre') {
  794. attObj.prePage();
  795. } else if (content === 'next') {
  796. attObj.nextPage();
  797. }
  798. });
  799. $('[fujian-content]').click(function() {
  800. const content = this.getAttribute('fujian-content');
  801. attObj.setTab(content.replace('-att', ''));
  802. });
  803. $('.check-all-file').change(function() {
  804. const checked = this.checked;
  805. const table = $(this).parent().parent().parent().parent();
  806. const checkes = $('[name=check-att]', table);
  807. checkes.each(function(){
  808. this.checked = checked;
  809. })
  810. });
  811. // 加载安全生产费数据
  812. postData('load', { filter: 'bills;att' }, function(result) {
  813. billsObj.loadData(result.bills);
  814. attObj.loadDatas(result.att);
  815. });
  816. const stdGclSetting = {
  817. selector: '#std-gcl',
  818. stdType: 'gcl',
  819. libs: stdBills,
  820. treeSetting: {
  821. id: 'bill_id',
  822. pid: 'pid',
  823. order: 'order',
  824. level: 'level',
  825. isLeaf: 'is_leaf',
  826. fullPath: 'full_path',
  827. rootId: -1,
  828. keys: ['id', 'list_id', 'bill_id']
  829. },
  830. spreadSetting: {
  831. cols: [
  832. {title: '清单编号', field: 'b_code', hAlign: 0, width: 170, formatter: '@', cellType: 'tree'},
  833. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@'},
  834. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'}
  835. ],
  836. treeCol: 0,
  837. emptyRows: 0,
  838. headRows: 1,
  839. headRowHeight: [32],
  840. defaultRowHeight: 21,
  841. headerFont: '12px 微软雅黑',
  842. font: '12px 微软雅黑',
  843. headColWidth: [30],
  844. selectedBackColor: '#fffacd',
  845. readOnly: true,
  846. },
  847. page: 'paymentSafe',
  848. tid: getTenderId(),
  849. cellDoubleClick: function (e, info) {
  850. const stdSheet = info.sheet;
  851. const stdTree = stdSheet.zh_tree;
  852. const stdNode = stdTree.nodes[info.row];
  853. if (!stdNode || !stdNode.b_code) return;
  854. const mainSheet = billsObj.sheet;
  855. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) return;
  856. const mainTree = mainSheet.zh_tree;
  857. const sel = mainSheet.getSelections()[0];
  858. const nodes = [stdNode, ...stdTree.getAllParents(stdNode)];
  859. nodes.sort((a, b) => { return a.level - b.level; });
  860. const stdData = [];
  861. let mainChildren = mainTree.children, mainCur, checkNode;
  862. for (const sd of nodes) {
  863. const field = sd.b_code ? 'b_code' : 'name';
  864. checkNode = mainChildren.find(x => { return x[field] === sd[field]; });
  865. if (!checkNode) {
  866. stdData.push({ b_code: sd.b_code, name: sd.name, unit: sd.unit });
  867. } else {
  868. mainCur = checkNode;
  869. mainChildren = mainCur ? mainCur.children : [];
  870. }
  871. }
  872. if (stdData.length > 0) {
  873. postData('update', { postType: 'add-std',
  874. postData: {
  875. id: mainCur ? mainCur.tree_id : mainTree.setting.rootId,
  876. stdData
  877. }
  878. }, function (result) {
  879. const refreshNode = mainTree.loadPostData(result);
  880. billsObj.refreshTree(refreshNode);
  881. const node = _.find(billsObj.tree.nodes, { b_code: stdNode.b_code, name: stdNode.name });
  882. if (node) {
  883. mainSheet.setSelection(billsObj.tree.nodes.indexOf(node), sel.col, 1, sel.colCount);
  884. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, billsObj.tree.nodes.indexOf(node)]);
  885. }
  886. billsObj.refreshOperationValid();
  887. billsObj.spread.focus();
  888. });
  889. } else {
  890. const node = _.find(billsObj.tree.nodes, { b_code: stdNode.b_code, name: stdNode.name });
  891. mainSheet.setSelection(billsObj.tree.nodes.indexOf(node), sel.col, 1, sel.colCount);
  892. billsObj.spread.focus();
  893. }
  894. },
  895. };
  896. // 展开收起标准清单
  897. $('a', '#side-menu').bind('click', function (e) {
  898. e.preventDefault();
  899. const tab = $(this), tabPanel = $(tab.attr('content'));
  900. // 展开工具栏、切换标签
  901. if (!tab.hasClass('active')) {
  902. // const close = $('.active', '#side-menu').length === 0;
  903. $('a', '#side-menu').removeClass('active');
  904. $('.tab-content .tab-select-show.tab-pane.active').removeClass('active');
  905. tab.addClass('active');
  906. tabPanel.addClass('active');
  907. // $('.tab-content .tab-pane').removeClass('active');
  908. showSideTools(tab.hasClass('active'));
  909. if (tab.attr('content') === '#std-gcl') {
  910. if (!stdGcl) stdGcl = $.stdLib(stdGclSetting);
  911. stdGcl.spread.refresh();
  912. }
  913. } else { // 收起工具栏
  914. tab.removeClass('active');
  915. tabPanel.removeClass('active');
  916. showSideTools(tab.hasClass('active'));
  917. }
  918. billsObj.spread.refresh();
  919. });
  920. // 工具栏spr
  921. $.divResizer({
  922. select: '#right-spr',
  923. callback: function () {
  924. billsObj.spread.refresh();
  925. if (stdGcl) stdGcl.spread.refresh();
  926. }
  927. });
  928. // 导航Menu
  929. $.subMenu({
  930. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  931. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  932. key: 'menu.1.0.0',
  933. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  934. callback: function (info) {
  935. if (info.mini) {
  936. $('.panel-title').addClass('fluid');
  937. $('#sub-menu').removeClass('panel-sidebar');
  938. } else {
  939. $('.panel-title').removeClass('fluid');
  940. $('#sub-menu').addClass('panel-sidebar');
  941. }
  942. autoFlashHeight();
  943. billsObj.spread.refresh();
  944. if (stdGcl) stdGcl.spread.refresh();
  945. }
  946. });
  947. // 显示层次
  948. (function (select, sheet) {
  949. $(select).click(function () {
  950. if (!sheet.zh_tree) return;
  951. const tag = $(this).attr('tag');
  952. const tree = sheet.zh_tree;
  953. setTimeout(() => {
  954. showWaitingView();
  955. switch (tag) {
  956. case "1":
  957. case "2":
  958. case "3":
  959. case "4":
  960. tree.expandByLevel(parseInt(tag));
  961. SpreadJsObj.refreshTreeRowVisible(sheet);
  962. break;
  963. case "last":
  964. tree.expandByCustom(() => { return true; });
  965. SpreadJsObj.refreshTreeRowVisible(sheet);
  966. break;
  967. }
  968. closeWaitingView();
  969. }, 100);
  970. });
  971. })('a[name=showLevel]', billsObj.sheet);
  972. });