payment_safe.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. html.push('<tr>');
  620. html.push(`<td><input type="checkbox" name="check-att" file_id="${file.id}"></td>`);
  621. 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>`);
  622. html.push(`<td>${file.u_name}</td>`);
  623. html.push('</tr>');
  624. return html.join('');
  625. }
  626. refreshCurTable() {
  627. const curNode = SpreadJsObj.getSelectObject(billsObj.sheet);
  628. const html = [];
  629. if (curNode) {
  630. const attIndex = this.billsIndexes[curNode.safe_id] || [];
  631. const beginIndex = (this.allPageIndex - 1) * this.pageCount;
  632. const endIndex = (this.allPageIndex) * this.pageCount - 1;
  633. for (const [i, ai] of attIndex.entries()) {
  634. if (i < beginIndex) continue;
  635. if (i > endIndex) continue;
  636. html.push(this.getFileHtml(ai));
  637. }
  638. }
  639. $('#cur-att-list').html(html.join(''));
  640. }
  641. refreshAllTable() {
  642. const html = [];
  643. const attIndex = this.atts;
  644. const beginIndex = (this.allPageIndex - 1) * this.pageCount;
  645. const endIndex = (this.allPageIndex) * this.pageCount - 1;
  646. for (const [i, ai] of attIndex.entries()) {
  647. if (i < beginIndex) continue;
  648. if (i > endIndex) continue;
  649. html.push(this.getFileHtml(ai));
  650. }
  651. $('#all-att-list').html(html.join(''));
  652. }
  653. prePage(){
  654. const pageIndex = this.activeTab + 'PageIndex';
  655. if (this[pageIndex] <= 1) return;
  656. this[pageIndex] = this[pageIndex] - 1;
  657. this.refreshShowTable();
  658. this.refreshShowPage();
  659. }
  660. nextPage() {
  661. const pageIndex = this.activeTab + 'PageIndex';
  662. if (this[pageIndex] >= this[this.activeTab + 'TotalPage']) return;
  663. this[pageIndex] = this[pageIndex] + 1;
  664. this.refreshShowTable();
  665. this.refreshShowPage();
  666. }
  667. refreshTable() {
  668. this.reCalcPage();
  669. this.refreshCurTable();
  670. this.refreshAllTable();
  671. }
  672. refreshShowTable() {
  673. if (this.activeTab === 'cur') {
  674. this.refreshCurTable();
  675. } else {
  676. this.refreshAllTable();
  677. }
  678. }
  679. batchDownload() {
  680. const checkes = $('[name=check-att]:checked');
  681. if (checkes.length === 0) {
  682. toastr.warning('请选择需要批量下载的文件');
  683. return;
  684. }
  685. const files = [], fileIds = [];
  686. for (const c of checkes) {
  687. const file = this.atts.find(x => { return x.id === parseInt(c.getAttribute('file_id'))});
  688. if (file && fileIds.indexOf(file.id) < 0) {
  689. fileIds.push(file.id);
  690. files.push({ filename: file.filename, fileext: file.fileext, filepath: c.viewpath || file.filepath});
  691. }
  692. }
  693. AliOss.zipFiles(files, '安全生产费-附件.zip', (fails) => {
  694. $(self).removeAttr('disabled');
  695. if (fails.length === 0) {
  696. toastr.success('下载成功');
  697. } else {
  698. toastr.warning(`下载成功(${fails.length}个文件下载失败)`);
  699. }
  700. }, () => {
  701. $(self).removeAttr('disabled');
  702. toastr.error('批量下载失败');
  703. });
  704. }
  705. uploadAtt(files, callback) {
  706. const curNode = SpreadJsObj.getSelectObject(billsObj.sheet);
  707. const formData = new FormData();
  708. if (curNode) formData.append('safe_id', curNode.safe_id);
  709. for (const file of files) {
  710. if (file === undefined) {
  711. toastr.error('未选择上传文件。');
  712. return false;
  713. }
  714. if (file.size > 30 * 1024 * 1024) {
  715. toastr.error('上传文件大小超过30MB。');
  716. return false;
  717. }
  718. const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
  719. if (whiteList.indexOf(fileext) === -1) {
  720. toastr.error('仅支持office文档、图片、压缩包格式,请勿上传' + fileext + '格式文件。');
  721. return false;
  722. }
  723. formData.append('size', file.size);
  724. formData.append('file[]', file);
  725. }
  726. postDataWithFile('file/upload', formData, function (files) {
  727. attObj.loadUpdateAtt(files);
  728. attObj.refreshTable();
  729. if (callback) callback();
  730. });
  731. }
  732. deleteAtt(file_id) {
  733. const file = this.atts.find(x => { return x.id === file_id; });
  734. if (!file) return;
  735. postData('file/delete', {id: file_id}, function() {
  736. attObj.loadDeleteAtt(file);
  737. attObj.refreshTable();
  738. })
  739. }
  740. setTab(tab) {
  741. this.activeTab = tab;
  742. this.refreshShowPage();
  743. }
  744. setSafeBills() {
  745. this.curPageIndex = 1;
  746. this.reCalcPage();
  747. this.refreshCurTable();
  748. }
  749. }
  750. const attObj = new AttObj();
  751. $('#upload-ok').click(function() {
  752. const input = $('#upload-file');
  753. attObj.uploadAtt(input[0].files, function() {
  754. $(input).val('');
  755. $('#upload').modal('hide');
  756. });
  757. });
  758. $('#batch-download-att').click(() => {
  759. attObj.batchDownload();
  760. });
  761. $('body').on('mouseenter', ".table-file", function(){
  762. $(this).children(".btn-group-table").css("display","block");
  763. });
  764. $('body').on('mouseleave', ".table-file", function(){
  765. $(this).children(".btn-group-table").css("display","none");
  766. });
  767. $('body').on('click', '[name=del-file]', function() {
  768. attObj.deleteAtt(parseInt(this.getAttribute('file_id')));
  769. });
  770. $('body').on('click', 'a[file_id]', function() {
  771. const file_id = parseInt(this.getAttribute('file_id'));
  772. const file = attObj.atts.find(x => { return x.id === file_id; });
  773. if (!file) return;
  774. if (file.viewpath) {
  775. window.open(file.viewpath, '_blank');
  776. } else {
  777. AliOss.downloadFile(file.filepath, `${file.filename}${file.fileext}`)
  778. }
  779. });
  780. $('.page-select').click(function() {
  781. const content = this.getAttribute('content');
  782. if (content === 'pre') {
  783. attObj.prePage();
  784. } else if (content === 'next') {
  785. attObj.nextPage();
  786. }
  787. });
  788. $('[fujian-content]').click(function() {
  789. const content = this.getAttribute('fujian-content');
  790. attObj.setTab(content.replace('-att', ''));
  791. });
  792. $('.check-all-file').change(function() {
  793. const checked = this.checked;
  794. const table = $(this).parent().parent().parent().parent();
  795. const checkes = $('[name=check-att]', table);
  796. checkes.each(function(){
  797. this.checked = checked;
  798. })
  799. });
  800. // 加载安全生产费数据
  801. postData('load', { filter: 'bills;att' }, function(result) {
  802. billsObj.loadData(result.bills);
  803. attObj.loadDatas(result.att);
  804. });
  805. const stdGclSetting = {
  806. selector: '#std-gcl',
  807. stdType: 'gcl',
  808. libs: stdBills,
  809. treeSetting: {
  810. id: 'bill_id',
  811. pid: 'pid',
  812. order: 'order',
  813. level: 'level',
  814. isLeaf: 'is_leaf',
  815. fullPath: 'full_path',
  816. rootId: -1,
  817. keys: ['id', 'list_id', 'bill_id']
  818. },
  819. spreadSetting: {
  820. cols: [
  821. {title: '清单编号', field: 'b_code', hAlign: 0, width: 170, formatter: '@', cellType: 'tree'},
  822. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@'},
  823. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'}
  824. ],
  825. treeCol: 0,
  826. emptyRows: 0,
  827. headRows: 1,
  828. headRowHeight: [32],
  829. defaultRowHeight: 21,
  830. headerFont: '12px 微软雅黑',
  831. font: '12px 微软雅黑',
  832. headColWidth: [30],
  833. selectedBackColor: '#fffacd',
  834. readOnly: true,
  835. },
  836. page: 'paymentSafe',
  837. tid: getTenderId(),
  838. cellDoubleClick: function (e, info) {
  839. const stdSheet = info.sheet;
  840. const stdTree = stdSheet.zh_tree;
  841. const stdNode = stdTree.nodes[info.row];
  842. if (!stdNode || !stdNode.b_code) return;
  843. const mainSheet = billsObj.sheet;
  844. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) return;
  845. const mainTree = mainSheet.zh_tree;
  846. const sel = mainSheet.getSelections()[0];
  847. const nodes = [stdNode, ...stdTree.getAllParents(stdNode)];
  848. nodes.sort((a, b) => { return a.level - b.level; });
  849. const stdData = [];
  850. let mainChildren = mainTree.children, mainCur, checkNode;
  851. for (const sd of nodes) {
  852. const field = sd.b_code ? 'b_code' : 'name';
  853. checkNode = mainChildren.find(x => { return x[field] === sd[field]; });
  854. if (!checkNode) {
  855. stdData.push({ b_code: sd.b_code, name: sd.name, unit: sd.unit });
  856. } else {
  857. mainCur = checkNode;
  858. mainChildren = mainCur ? mainCur.children : [];
  859. }
  860. }
  861. if (stdData.length > 0) {
  862. postData('update', { postType: 'add-std',
  863. postData: {
  864. id: mainCur ? mainCur.tree_id : mainTree.setting.rootId,
  865. stdData
  866. }
  867. }, function (result) {
  868. const refreshNode = mainTree.loadPostData(result);
  869. billsObj.refreshTree(refreshNode);
  870. const node = _.find(billsObj.tree.nodes, { b_code: stdNode.b_code, name: stdNode.name });
  871. if (node) {
  872. mainSheet.setSelection(billsObj.tree.nodes.indexOf(node), sel.col, 1, sel.colCount);
  873. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, billsObj.tree.nodes.indexOf(node)]);
  874. }
  875. billsObj.refreshOperationValid;
  876. billsObj.spread.focus();
  877. });
  878. } else {
  879. const node = _.find(billsObj.tree.nodes, { b_code: stdNode.b_code, name: stdNode.name });
  880. mainSheet.setSelection(billsObj.tree.nodes.indexOf(node), sel.col, 1, sel.colCount);
  881. billsObj.spread.focus();
  882. }
  883. },
  884. };
  885. // 展开收起标准清单
  886. $('a', '#side-menu').bind('click', function (e) {
  887. e.preventDefault();
  888. const tab = $(this), tabPanel = $(tab.attr('content'));
  889. // 展开工具栏、切换标签
  890. if (!tab.hasClass('active')) {
  891. // const close = $('.active', '#side-menu').length === 0;
  892. $('a', '#side-menu').removeClass('active');
  893. $('.tab-content .tab-select-show.tab-pane.active').removeClass('active');
  894. tab.addClass('active');
  895. tabPanel.addClass('active');
  896. // $('.tab-content .tab-pane').removeClass('active');
  897. showSideTools(tab.hasClass('active'));
  898. if (tab.attr('content') === '#std-gcl') {
  899. if (!stdGcl) stdGcl = $.stdLib(stdGclSetting);
  900. stdGcl.spread.refresh();
  901. }
  902. } else { // 收起工具栏
  903. tab.removeClass('active');
  904. tabPanel.removeClass('active');
  905. showSideTools(tab.hasClass('active'));
  906. }
  907. billsObj.spread.refresh();
  908. });
  909. // 工具栏spr
  910. $.divResizer({
  911. select: '#right-spr',
  912. callback: function () {
  913. billsObj.spread.refresh();
  914. if (stdGcl) stdGcl.spread.refresh();
  915. }
  916. });
  917. // 导航Menu
  918. $.subMenu({
  919. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  920. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  921. key: 'menu.1.0.0',
  922. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  923. callback: function (info) {
  924. if (info.mini) {
  925. $('.panel-title').addClass('fluid');
  926. $('#sub-menu').removeClass('panel-sidebar');
  927. } else {
  928. $('.panel-title').removeClass('fluid');
  929. $('#sub-menu').addClass('panel-sidebar');
  930. }
  931. autoFlashHeight();
  932. billsObj.spread.refresh();
  933. if (stdGcl) stdGcl.spread.refresh();
  934. }
  935. });
  936. // 显示层次
  937. (function (select, sheet) {
  938. $(select).click(function () {
  939. if (!sheet.zh_tree) return;
  940. const tag = $(this).attr('tag');
  941. const tree = sheet.zh_tree;
  942. setTimeout(() => {
  943. showWaitingView();
  944. switch (tag) {
  945. case "1":
  946. case "2":
  947. case "3":
  948. case "4":
  949. tree.expandByLevel(parseInt(tag));
  950. SpreadJsObj.refreshTreeRowVisible(sheet);
  951. break;
  952. case "last":
  953. tree.expandByCustom(() => { return true; });
  954. SpreadJsObj.refreshTreeRowVisible(sheet);
  955. break;
  956. }
  957. closeWaitingView();
  958. }, 100);
  959. });
  960. })('a[name=showLevel]', billsObj.sheet);
  961. });