payment_safe.js 44 KB

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