payment_safe.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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: '@'},
  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 = 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.allPageIndex - 1) * this.pageCount;
  633. const endIndex = (this.allPageIndex) * 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. }
  747. setTab(tab) {
  748. this.activeTab = tab;
  749. this.refreshShowPage();
  750. }
  751. setSafeBills() {
  752. this.curPageIndex = 1;
  753. this.reCalcPage();
  754. this.refreshCurTable();
  755. }
  756. }
  757. const attObj = new AttObj();
  758. $('#upload-ok').click(function() {
  759. const input = $('#upload-file');
  760. attObj.uploadAtt(input[0].files, function() {
  761. $(input).val('');
  762. $('#upload').modal('hide');
  763. });
  764. });
  765. $('#batch-download-att').click(() => {
  766. attObj.batchDownload();
  767. });
  768. $('body').on('mouseenter', ".table-file", function(){
  769. $(this).children(".btn-group-table").css("display","block");
  770. });
  771. $('body').on('mouseleave', ".table-file", function(){
  772. $(this).children(".btn-group-table").css("display","none");
  773. });
  774. $('body').on('click', '[name=del-file]', function() {
  775. attObj.deleteAtt(parseInt(this.getAttribute('file_id')));
  776. });
  777. $('body').on('click', 'a[name=view-file]', function() {
  778. const file_id = parseInt(this.getAttribute('file_id'));
  779. const file = attObj.atts.find(x => { return x.id === file_id; });
  780. if (!file) return;
  781. if (file.viewpath) {
  782. window.open(file.viewpath, '_blank');
  783. } else {
  784. AliOss.downloadFile(file.filepath, `${file.filename}${file.fileext}`)
  785. }
  786. });
  787. $('body').on('click', 'a[name=locate-bills]', function() {
  788. attObj.locateBills(parseInt(this.getAttribute('file_id')));
  789. });
  790. $('.page-select').click(function() {
  791. const content = this.getAttribute('content');
  792. if (content === 'pre') {
  793. attObj.prePage();
  794. } else if (content === 'next') {
  795. attObj.nextPage();
  796. }
  797. });
  798. $('[fujian-content]').click(function() {
  799. const content = this.getAttribute('fujian-content');
  800. attObj.setTab(content.replace('-att', ''));
  801. });
  802. $('.check-all-file').change(function() {
  803. const checked = this.checked;
  804. const table = $(this).parent().parent().parent().parent();
  805. const checkes = $('[name=check-att]', table);
  806. checkes.each(function(){
  807. this.checked = checked;
  808. })
  809. });
  810. // 加载安全生产费数据
  811. postData('load', { filter: 'bills;att' }, function(result) {
  812. billsObj.loadData(result.bills);
  813. attObj.loadDatas(result.att);
  814. });
  815. const stdGclSetting = {
  816. selector: '#std-gcl',
  817. stdType: 'gcl',
  818. libs: stdBills,
  819. treeSetting: {
  820. id: 'bill_id',
  821. pid: 'pid',
  822. order: 'order',
  823. level: 'level',
  824. isLeaf: 'is_leaf',
  825. fullPath: 'full_path',
  826. rootId: -1,
  827. keys: ['id', 'list_id', 'bill_id']
  828. },
  829. spreadSetting: {
  830. cols: [
  831. {title: '清单编号', field: 'b_code', hAlign: 0, width: 170, formatter: '@', cellType: 'tree'},
  832. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@'},
  833. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'}
  834. ],
  835. treeCol: 0,
  836. emptyRows: 0,
  837. headRows: 1,
  838. headRowHeight: [32],
  839. defaultRowHeight: 21,
  840. headerFont: '12px 微软雅黑',
  841. font: '12px 微软雅黑',
  842. headColWidth: [30],
  843. selectedBackColor: '#fffacd',
  844. readOnly: true,
  845. },
  846. page: 'paymentSafe',
  847. tid: getTenderId(),
  848. cellDoubleClick: function (e, info) {
  849. const stdSheet = info.sheet;
  850. const stdTree = stdSheet.zh_tree;
  851. const stdNode = stdTree.nodes[info.row];
  852. if (!stdNode || !stdNode.b_code) return;
  853. const mainSheet = billsObj.sheet;
  854. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) return;
  855. const mainTree = mainSheet.zh_tree;
  856. const sel = mainSheet.getSelections()[0];
  857. const nodes = [stdNode, ...stdTree.getAllParents(stdNode)];
  858. nodes.sort((a, b) => { return a.level - b.level; });
  859. const stdData = [];
  860. let mainChildren = mainTree.children, mainCur, checkNode;
  861. for (const sd of nodes) {
  862. const field = sd.b_code ? 'b_code' : 'name';
  863. checkNode = mainChildren.find(x => { return x[field] === sd[field]; });
  864. if (!checkNode) {
  865. stdData.push({ b_code: sd.b_code, name: sd.name, unit: sd.unit });
  866. } else {
  867. mainCur = checkNode;
  868. mainChildren = mainCur ? mainCur.children : [];
  869. }
  870. }
  871. if (stdData.length > 0) {
  872. postData('update', { postType: 'add-std',
  873. postData: {
  874. id: mainCur ? mainCur.tree_id : mainTree.setting.rootId,
  875. stdData
  876. }
  877. }, function (result) {
  878. const refreshNode = mainTree.loadPostData(result);
  879. billsObj.refreshTree(refreshNode);
  880. const node = _.find(billsObj.tree.nodes, { b_code: stdNode.b_code, name: stdNode.name });
  881. if (node) {
  882. mainSheet.setSelection(billsObj.tree.nodes.indexOf(node), sel.col, 1, sel.colCount);
  883. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, billsObj.tree.nodes.indexOf(node)]);
  884. }
  885. billsObj.refreshOperationValid;
  886. billsObj.spread.focus();
  887. });
  888. } else {
  889. const node = _.find(billsObj.tree.nodes, { b_code: stdNode.b_code, name: stdNode.name });
  890. mainSheet.setSelection(billsObj.tree.nodes.indexOf(node), sel.col, 1, sel.colCount);
  891. billsObj.spread.focus();
  892. }
  893. },
  894. };
  895. // 展开收起标准清单
  896. $('a', '#side-menu').bind('click', function (e) {
  897. e.preventDefault();
  898. const tab = $(this), tabPanel = $(tab.attr('content'));
  899. // 展开工具栏、切换标签
  900. if (!tab.hasClass('active')) {
  901. // const close = $('.active', '#side-menu').length === 0;
  902. $('a', '#side-menu').removeClass('active');
  903. $('.tab-content .tab-select-show.tab-pane.active').removeClass('active');
  904. tab.addClass('active');
  905. tabPanel.addClass('active');
  906. // $('.tab-content .tab-pane').removeClass('active');
  907. showSideTools(tab.hasClass('active'));
  908. if (tab.attr('content') === '#std-gcl') {
  909. if (!stdGcl) stdGcl = $.stdLib(stdGclSetting);
  910. stdGcl.spread.refresh();
  911. }
  912. } else { // 收起工具栏
  913. tab.removeClass('active');
  914. tabPanel.removeClass('active');
  915. showSideTools(tab.hasClass('active'));
  916. }
  917. billsObj.spread.refresh();
  918. });
  919. // 工具栏spr
  920. $.divResizer({
  921. select: '#right-spr',
  922. callback: function () {
  923. billsObj.spread.refresh();
  924. if (stdGcl) stdGcl.spread.refresh();
  925. }
  926. });
  927. // 导航Menu
  928. $.subMenu({
  929. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  930. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  931. key: 'menu.1.0.0',
  932. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  933. callback: function (info) {
  934. if (info.mini) {
  935. $('.panel-title').addClass('fluid');
  936. $('#sub-menu').removeClass('panel-sidebar');
  937. } else {
  938. $('.panel-title').removeClass('fluid');
  939. $('#sub-menu').addClass('panel-sidebar');
  940. }
  941. autoFlashHeight();
  942. billsObj.spread.refresh();
  943. if (stdGcl) stdGcl.spread.refresh();
  944. }
  945. });
  946. // 显示层次
  947. (function (select, sheet) {
  948. $(select).click(function () {
  949. if (!sheet.zh_tree) return;
  950. const tag = $(this).attr('tag');
  951. const tree = sheet.zh_tree;
  952. setTimeout(() => {
  953. showWaitingView();
  954. switch (tag) {
  955. case "1":
  956. case "2":
  957. case "3":
  958. case "4":
  959. tree.expandByLevel(parseInt(tag));
  960. SpreadJsObj.refreshTreeRowVisible(sheet);
  961. break;
  962. case "last":
  963. tree.expandByCustom(() => { return true; });
  964. SpreadJsObj.refreshTreeRowVisible(sheet);
  965. break;
  966. }
  967. closeWaitingView();
  968. }, 100);
  969. });
  970. })('a[name=showLevel]', billsObj.sheet);
  971. });