payment_safe.js 42 KB

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