function getTenderId() { return window.location.pathname.split('/')[2]; } const invalidFields = { parent: ['cur_qty', 'cur_tp', 'unit_price'], }; $(document).ready(function() { let stdGcl; autoFlashHeight(); class BillsObj { constructor() { this.spread = SpreadJsObj.createNewSpread($('#bills-spread')[0]); this.sheet = this.spread.getActiveSheet(); this.treeSetting = { id: 'tree_id', pid: 'tree_pid', order: 'tree_order', level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path', rootId: -1, calcFields: ['cur_tp', 'pre_tp', 'end_tp'], keys: ['id', 'detail_id', 'tree_id'], }; this.tree = createNewPathTree('ledger', this.treeSetting); this.spreadSetting = { cols: [ {title: '编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 230, formatter: '@', cellType: 'tree'}, {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 185, formatter: '@', cellType: 'autoTip'}, {title: '规格', colSpan: '1', rowSpan: '2', field: 'spec', hAlign: 0, width: 150, formatter: '@'}, {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', cellType: 'unit'}, {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 80, type: 'Number'}, {title: '本期|数量', colSpan: '2|1', rowSpan: '1|1', field: 'cur_qty', hAlign: 2, width: 80, type: 'Number'}, {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'cur_tp', hAlign: 2, width: 80, type: 'Number', readOnly: true}, {title: '截止本期|数量', colSpan: '2|1', rowSpan: '1|1', field: 'end_qty', hAlign: 2, width: 80, type: 'Number'}, {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'end_tp', hAlign: 2, width: 80, type: 'Number', readOnly: true}, {title: '发票号', colSpan: '1', rowSpan: '2', field: 'invoice_code', hAlign: 0, width: 100, formatter: '@'}, {title: '备注', colSpan: '1', rowSpan: '2', field: 'memo', hAlign: 0, width: 120, formatter: '@', cellType: 'ellipsisAutoTip'}, ], emptyRows: 3, headRows: 2, headRowHeight: [25, 25], defaultRowHeight: 21, headerFont: '12px 微软雅黑', font: '12px 微软雅黑', readOnly: readOnly, localCache: { key: 'payment-safe-bills', colWidth: true, }, }; this.ckBillsSpread = window.location.pathname + '-billsSelect'; this.initSpread(); this.initOtherEvent(); } initSpread() { SpreadJsObj.initSheet(this.sheet, this.spreadSetting); this.spread.bind(spreadNS.Events.SelectionChanged, this.selectionChanged); this.spread.bind(spreadNS.Events.topRowChanged, this.topRowChanged); this.spread.bind(spreadNS.Events.ClipboardChanging, function (e, info) { const copyText = SpreadJsObj.getFilterCopyText(info.sheet); SpreadJsObj.Clipboard.setCopyData(copyText); }); if (readOnly) return; this.spread.bind(spreadNS.Events.EditEnded, this.editEnded); this.spread.bind(spreadNS.Events.EditStarting, this.editStarting); this.spread.bind(spreadNS.Events.ClipboardPasting, this.clipboardPasting); SpreadJsObj.addDeleteBind(this.spread, this.deletePress); } initOtherEvent() { const self = this; // 增删上下移升降级 $('a[name="base-opr"]').click(function () { self.baseOpr(this.getAttribute('type')); }); } refreshOperationValid() { const setObjEnable = function (obj, enable) { if (enable) { obj.removeClass('disabled'); } else { obj.addClass('disabled'); } }; const invalidAll = function () { setObjEnable($('a[name=base-opr][type=add]'), false); setObjEnable($('a[name=base-opr][type=delete]'), false); setObjEnable($('a[name=base-opr][type=up-move]'), false); setObjEnable($('a[name=base-opr][type=down-move]'), false); setObjEnable($('a[name=base-opr][type=up-level]'), false); setObjEnable($('a[name=base-opr][type=down-level]'), false); }; const sel = this.sheet.getSelections()[0]; const row = sel ? sel.row : -1; const tree = this.sheet.zh_tree; if (!tree) { invalidAll(); return; } const first = tree.nodes[row]; if (!first) { invalidAll(); return; } let last = first, sameParent = true, nodeUsed = this.checkNodeUsed(tree, first); if (sel.rowCount > 1 && first) { for (let r = 1; r < sel.rowCount; r++) { const rNode = tree.nodes[sel.row + r]; if (!rNode) { sameParent = false; break; } nodeUsed = nodeUsed || this.checkNodeUsed(tree, rNode); if (rNode.tree_level > first.tree_level) continue; if ((rNode.tree_level < first.tree_level) || (rNode.tree_level === first.tree_level && rNode.tree_pid !== first.tree_pid)) { sameParent = false; break; } last = rNode; } } const preNode = tree.getPreSiblingNode(first); const valid = !this.sheet.zh_setting.readOnly; setObjEnable($('a[name=base-opr][type=add]'), valid && first && first.tree_level > 1); setObjEnable($('a[name=base-opr][type=delete]'), valid && first && sameParent && first.tree_level > 1 && !nodeUsed); setObjEnable($('a[name=base-opr][type=up-move]'), valid && first && sameParent && first.tree_level > 1 && preNode); setObjEnable($('a[name=base-opr][type=down-move]'), valid && first && sameParent && first.tree_level > 1 && !tree.isLastSibling(last)); setObjEnable($('a[name=base-opr][type=up-level]'), valid && first && sameParent && tree.getParent(first) && !nodeUsed && first.tree_level > 2); setObjEnable($('a[name=base-opr][type=down-level]'), valid && first && sameParent && first.tree_level > 1 && preNode && !this.checkNodeUsed(tree, preNode)); } loadRelaData() { this.refreshOperationValid(); SpreadJsObj.saveTopAndSelect(this.sheet, this.ckBillsSpread); attObj.setSafeBills(); } refreshTree(data) { const sheet = this.sheet; SpreadJsObj.massOperationSheet(sheet, function () { const tree = sheet.zh_tree; // 处理删除 if (data.delete) { data.delete.sort(function (a, b) { return b.deleteIndex - a.deleteIndex; }); for (const d of data.delete) { sheet.deleteRows(d.deleteIndex, 1); } } // 处理新增 if (data.create) { const newNodes = data.create; if (newNodes) { newNodes.sort(function (a, b) { return a.index - b.index; }); for (const node of newNodes) { sheet.addRows(node.index, 1); SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1); } } } // 处理更新 if (data.update) { const rows = []; for (const u of data.update) { rows.push(tree.nodes.indexOf(u)); } SpreadJsObj.reLoadRowsData(sheet, rows); } // 处理展开 if (data.expand) { const expanded = []; for (const e of data.expand) { if (expanded.indexOf(e) === -1) { const posterity = tree.getPosterity(e); for (const p of posterity) { sheet.setRowVisible(tree.nodes.indexOf(p), p.visible); expanded.push(p); } } } } }); } loadData(datas) { this.tree.loadDatas(datas); treeCalc.calculateAll(this.tree); SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Tree, this.tree); SpreadJsObj.loadTopAndSelect(this.sheet, this.ckBillsSpread); this.refreshOperationValid(); } getDefaultSelectInfo() { if (!this.tree) return; const sel = this.sheet.getSelections()[0]; const node = this.sheet.zh_tree.nodes[sel.row]; if (!node) return; let count = 1; if (sel.rowCount > 1) { for (let r = 1; r < sel.rowCount; r++) { const rNode = this.sheet.zh_tree.nodes[sel.row + r]; if (rNode.tree_level > node.tree_level) continue; if ((rNode.tree_level < node.tree_level) || (rNode.tree_level === node.tree_level && rNode.tree_pid !== node.tree_pid)) { toastr.warning('请选择同一节点下的节点,进行该操作'); return; } count += 1; } } return [this.tree, node, count]; } checkNodeUsed(tree, node) { if (node.pre_qty || node.pre_tp) return true; const posterity = tree.getPosterity(node); for (const p of posterity) { if (p.pre_qty || p.pre_tp) return true; } return false; } baseOpr(type, addCount = 1) { const self = this; const sheet = self.sheet; const sel = sheet.getSelections()[0]; const [tree, node, count] = this.getDefaultSelectInfo(); if (!tree || !node || !count) return; if (type === 'delete') { const parent = tree.getParent(node); const children = parent ? parent.children : tree.children; const index = children.indexOf(node); for (let i = 0; i < count; i++) { const child = children[i+index]; if (this.checkNodeUsed(tree, child)) { toastr.warning('选中的节点已计量,不可删除'); return; } } } else if (type === 'up-level') { const parent = tree.getParent(node); const children = parent ? parent.children : tree.children; const index = children.indexOf(node); for (let i = index; i < children.length; i++) { const child = children[index]; if (this.checkNodeUsed(tree, child)) { if (i >= index + count) { toastr.warning('其后节点已计量,选中的节点不可升级'); } else { toastr.warning('选中的节点已计量,不可升级'); } return; } } } else if (type === 'down-level') { const parent = tree.getParent(node); const children = parent ? parent.children : tree.children; const index = children.indexOf(node); if (index > 0 && this.checkNodeUsed(tree, children[index-1])) { toastr.warning('其前节点已计量,选中的节点不可降级'); return; } for (let i = index; i < count; i++) { const child = children[i+index]; if (this.checkNodeUsed(tree, child)) { toastr.warning('选中的节点已计量,不可降级'); return; } } } const updateData = { postType: type, postData: { id: node.tree_id, count: type === 'add' ? addCount : count, } }; if (type === 'delete') { deleteAfterHint(function () { postData('update', updateData, function (result) { const refreshData = tree.loadPostData(result); self.refreshTree(refreshData); if (sel) { sheet.setSelection(sel.row, sel.col, 1, sel.colCount); } self.refreshOperationValid(); }); }); } else { postData('update', updateData, function (result) { const refreshData = tree.loadPostData(result); self.refreshTree(refreshData); if (['up-move', 'down-move'].indexOf(type) > -1) { if (sel) { sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount); // SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(node)]); } } else if (type === 'add') { const sel = sheet.getSelections()[0]; if (sel) { sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount); // SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(refreshData.create[0])]); } } self.refreshOperationValid(); }); } } // 事件 selectionChanged(e, info) { if (info.newSelections) { if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) { billsObj.loadRelaData(); } } } topRowChanged(e, info) { SpreadJsObj.saveTopAndSelect(info.sheet, billsObj.ckBillsSpread); } editEnded(e, info) { if (!info.sheet.zh_setting) return; const tree = info.sheet.zh_tree; const node = SpreadJsObj.getSelectObject(info.sheet); const data = { id: node.id, detail_id: node.detail_id, tree_id: node.tree_id }; // 未改变值则不提交 const col = info.sheet.zh_setting.cols[info.col]; const orgValue = node[col.field]; const newValue = trimInvalidChar(info.editingText); if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) return; if (info.editingText) { const text = newValue; if (billsObj.checkNodeUsed(tree, node) && col.field ==='b_code' && orgValue !== '' && text === '') { toastr.error('节点已计量,请勿删除编号'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } if (col.type === 'Number') { const num = _.toNumber(text); if (_.isFinite(num)) { data[col.field] = num; } else { try { data[col.field] = math.evaluate(transExpr(text)); } catch(err) { toastr.error('输入的表达式非法'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } } } else { data[col.field] = text; } } else { if (billsObj.checkNodeUsed(tree, node) && (col.field ==='b_code') && orgValue !== '') { toastr.error('节点已计量,请勿删除编号'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } data[col.field] = col.type === 'Number' ? 0 : ''; } // 更新至服务器 postData('update', {postType: 'update', postData: data}, function (result) { const refreshNode = billsObj.tree.loadPostData(result); billsObj.refreshTree(refreshNode); }, function () { SpreadJsObj.reLoadRowData(info.sheet, info.row, 1); }); } editStarting(e, info) { if (!info.sheet.zh_setting || !info.sheet.zh_tree) return; const tree = info.sheet.zh_tree; const col = info.sheet.zh_setting.cols[info.col]; const node = info.sheet.zh_tree.nodes[info.row]; if (!node) { info.cancel = true; return; } switch (col.field) { case 'b_code': info.cancel = readOnly || billsObj.checkNodeUsed(tree, node); break; case 'unit_price': info.cancel = readOnly || (node.children && node.children.length > 0); break; case 'cur_qty': case 'cur_tp': info.cancel = (node.children && node.children.length > 0); break; } } deletePress (sheet) { if (!sheet.zh_setting) return; const sel = sheet.getSelections()[0], datas = []; for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) { let bDel = false; const node = sheet.zh_tree.nodes[iRow]; const data = sheet.zh_tree.getNodeKeyData(node); for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) { const col = sheet.zh_setting.cols[iCol]; const style = sheet.getStyle(iRow, iCol); if (style.locked) continue; if (col.field === 'b_code' && sheet.zh_tree.checkNodeUsed(node, pos)) { toastr.warning(`"${node.b_code || ''} ${node.name}"已计量,请勿修改`); return; } data[col.field] = col.type === 'Number' ? 0 : ''; bDel = true; } if (bDel) datas.push(data); } if (datas.length > 0) { postData('update', {postType: 'update', postData: datas}, function (result) { const refreshNode = sheet.zh_tree.loadPostData(result); billsObj.refreshTree(refreshNode); }, function () { SpreadJsObj.reLoadRowData(info.sheet, sel.row, sel.rowCount); }); } } clipboardPasting(e, info) { info.cancel = true; const tree = info.sheet.zh_tree, setting = info.sheet.zh_setting; if (!setting || !tree) return; const pasteData = info.pasteData.html ? SpreadJsObj.analysisPasteHtml(info.pasteData.html) : (info.pasteData.text === '' ? SpreadJsObj.Clipboard.getAnalysisPasteText() : SpreadJsObj.analysisPasteText(info.pasteData.text)); const hint = { usedUp: {type: 'warning', msg: '节点已计量,不可修改单价'}, usedCode: {type: 'warning', msg: '节点已计量,编号不可修改'}, invalidExpr: {type: 'warning', msg: '粘贴的表达式非法'}, parent: {type: 'warning', msg: '含有子项的清单,不可粘贴数量、单价、金额'}, }; const datas = [], filterNodes = []; let level, filterRow = 0; for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) { const curRow = info.cellRange.row + iRow; const node = tree.nodes[curRow]; if (!node) continue; if (!level) level = node.level; if (node.level < level) break; let bPaste = false; const data = info.sheet.zh_tree.getNodeKeyData(node); for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) { const curCol = info.cellRange.col + iCol; const colSetting = info.sheet.zh_setting.cols[curCol]; const value = trimInvalidChar(pasteData[iRow-filterRow][iCol]); if (node.children && node.children.length > 0 && invalidFields.parent.indexOf(colSetting.field) >= 0) { toastMessageUniq(hint.parent); continue; } if (billsObj.checkNodeUsed(tree, node) && colSetting.field === 'unit_price') { toastMessageUniq (hint.usedUp); continue; } if (colSetting.type === 'Number') { const num = _.toNumber(value); if (num) { data[colSetting.field] = num; } else { try { data[colSetting.field] = math.evaluate(transExpr(value)); bPaste = true; } catch (err) { toastMessageUniq(hint.invalidExpr); continue; } } } else { if (node.used && (colSetting.field ==='b_code') && data[colSetting.field] !== '' && value === '') { toastMessageUniq(hint.usedCode); continue; } data[colSetting.field] = value; } bPaste = true; } if (bPaste) { datas.push(data); } else { filterNodes.push(node); } } if (datas.length > 0) { postData('update', {postType: 'update', postData: datas}, function (result) { const refreshNode = tree.loadPostData(result); if (refreshNode.update) refreshNode.update = refreshNode.update.concat(filterNodes); billsObj.refreshTree(refreshNode); }, function () { SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount); }); } else { SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount); } } } const billsObj = new BillsObj(); // 清单右键菜单 const billsContextMenuOptions = { selector: '#bills-spread', build: function ($trigger, e) { const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread); billsObj.loadRelaData(); return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader; }, items: {} }; if (!readOnly) { billsContextMenuOptions.items.create = { name: '新增', icon: 'fa-sign-in', callback: function (key, opt) { billsObj.baseOpr('add'); }, disabled: function (key, opt) { const sheet = billsObj.sheet; const selection = sheet.getSelections(); const sel = selection ? selection[0] : sheet.getSelections()[0]; const row = sel ? sel.row : -1; const tree = sheet.zh_tree; if (!tree) return true; const first = sheet.zh_tree.nodes[row]; const valid = !sheet.zh_setting.readOnly; return !(valid && first && first.tree_level > 1); } }; billsContextMenuOptions.items.delete = { name: '删除', icon: 'fa-remove', callback: function (key, opt) { billsObj.baseOpr('delete'); }, disabled: function (key, opt) { const sheet = billsObj.sheet; const selection = sheet.getSelections(); const sel = selection ? selection[0] : sheet.getSelections()[0]; const row = sel ? sel.row : -1; const tree = sheet.zh_tree; if (!tree) return true; const first = sheet.zh_tree.nodes[row]; let last = first, sameParent = true, nodeUsed = billsObj.checkNodeUsed(tree, first); if (sel.rowCount > 1 && first) { for (let r = 1; r < sel.rowCount; r++) { const rNode = tree.nodes[sel.row + r]; if (!rNode) { sameParent = false; break; } nodeUsed = nodeUsed || billsObj.checkNodeUsed(tree, rNode); if (rNode.tree_level > first.tree_level) continue; if ((rNode.tree_level < first.tree_level) || (rNode.tree_level === first.tree_level && rNode.tree_pid !== first.tree_pid)) { sameParent = false; break; } last = rNode; } } const valid = !sheet.zh_setting.readOnly; return !(valid && first && sameParent && !(first.tree_level === 1) && !nodeUsed); } }; $('#psb-decimal').on('show.bs.modal', () => { const decimalObjs = [$('#psb-qty'), $('#psb-up'), $('#psb-tp')]; for (const obj of decimalObjs) { obj.val(obj.attr('org')); } }); $('#psb-decimal-ok').click(() => { const decimal = { qty: parseInt($('#psb-qty').val()), up: parseInt($('#psb-up').val()), tp: parseInt($('#psb-tp').val()) }; postData('decimal', { decimal }, function(result) { if (result.calc) { const refreshNode = billsObj.tree.loadPostData(result); billsObj.refreshTree(refreshNode); } $('#psb-qty').attr('org', decimal.qty); $('#psb-up').attr('org', decimal.up); $('#psb-tp').attr('org', decimal.tp); $('#psb-decimal').modal('hide'); }); }); } class AttObj { constructor() { this.atts = []; this.billsIndexes = {}; this.pageCount = 15; this.activeTab = 'cur'; this.curPageIndex = 1; this.curTotalPage = 1; this.allPageIndex = 1; this.allTotalPage = 1; } refreshShowPage() { const prefix = this.activeTab; $('#totalPage').html(this[prefix + 'TotalPage']); $('#currentPage').html(this[prefix + 'PageIndex']); if (this[prefix + 'TotalPage'] > 1) { $('#showPage').show(); } else { $('#showPage').hide(); } } reCalcPage() { this.allTotalPage = Math.ceil(this.atts.length / this.pageCount); const curNode = SpreadJsObj.getSelectObject(billsObj.sheet); const curAttIndex = curNode ? this.billsIndexes[curNode.safe_id] || [] : []; this.curTotalPage = Math.ceil(curAttIndex.length / this.pageCount); this.refreshShowPage(); } loadDatas(datas) { for (const d of datas) { this.atts.push(d); if (!this.billsIndexes[d.safe_id]) { this.billsIndexes[d.safe_id] = []; } this.billsIndexes[d.safe_id].push(d); } this.refreshTable(); } loadUpdateAtt(files) { for (const d of files) { this.atts.unshift(d); if (!this.billsIndexes[d.safe_id]) { this.billsIndexes[d.safe_id] = []; } this.billsIndexes[d.safe_id].unshift(d); } } loadDeleteAtt(file) { let fileIndex = this.atts.findIndex(x => { return x.id === file.id; }); this.atts.splice(fileIndex, 1); if (this.billsIndexes[file.safe_id]) { fileIndex = this.billsIndexes[file.safe_id].findIndex(x => { return x.id === file.id; }); this.billsIndexes[file.safe_id].splice(fileIndex, 1); } } getFileHtml(file) { const html = []; const downHtml = ``; const delHtml = file.uid === userID ? `` : ''; const locateHtml = ``; html.push('