'use strict'; /** * * * @author Mai * @date * @version */ const math = require('mathjs'); const standard = require('../const/standard'); const changeSort = { name: '变更令排序', hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' + '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' + '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' + '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"', /** * * @param ctx - context常量 * @param data - 全部数据源{Array} * @param fieldsKey - 计算字段 * @param options - 计算设置 */ fun: function(ctx, data, fieldsKey, options) { if (!data.change) return; // 变更令排序 data.change.sort(function (a, b) { return a.code.localeCompare(b.code); }); if (data.change_audit_list) { data.change_audit_list.sort(function (a, b) { const aCIndex = data.change.findIndex(function (c) { return c.cid === a.cid; }); const bCIndex = data.change.findIndex(function (c) { return c.cid === b.cid; }); return aCIndex === bCIndex ? ctx.helper.compareCode(a.code, b.code) : aCIndex - bCIndex; }); } }, }; const gatherGcl = { name: '汇总工程量清单', hint: '请使用mem_stage_bills下指标,注意事项:\n' + '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' + ' 清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' + '2. 汇总后,以下字段,均会失效, 请勿使用:\n' + ' 台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' + ' 树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' + ' 图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' + '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n', fun: function (ctx, data, fieldsKey, options) { const gatherFields = function(gcl, data, fields) { for (const f of fields) { if (data[f]) { gcl[f] = ctx.helper.add(gcl[f], data[f]); } } }; if (!data.mem_stage_bills) return; const fields = ctx.helper._.map(fieldsKey, 'field'); const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf']; for (const nf of needFields) { if (fields.indexOf(nf) === -1) return; } const gatherOther = fields.indexOf('chapter') >= 0; ctx.helper._.pull(data.mem_stage_bills, 'is_leaf'); const gclBills = [], other = {name: '未计入清单章节项', chapter: '10000'}; for (const b of data.mem_stage_bills) { if (b.b_code && b.b_code !== '') { let gcl = gclBills.find(function (g) { return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price)); }); if (!gcl) { gcl = { b_code: b.b_code, name: b.name, unit: b.unit, unit_price: b.unit_price, qc_bgl_code: [], chapter: b.chapter, deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp, }; gclBills.push(gcl); } gatherFields(gcl, b, [ 'deal_qty', 'deal_tp', 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price', 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp', 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp', 'final_tp' ]); if (b.qc_bgl_code && b.qc_bgl_code !== '') { gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';')); } } else if (gatherOther) { gatherFields(other, b, [ 'deal_tp', 'sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp', 'end_contract_tp', 'end_qc_tp', 'end_gather_tp', 'final_tp' ]); } } if (gatherOther) gclBills.push(other); for (const g of gclBills) { if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';'); g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_ratio), 100); } data.mem_stage_bills = gclBills; } }; const sortGcl = { name: '工程量清单排序', hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' + 'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' + 'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' + '特别的,如有"未计入清单章节项": \n' + ' 1. 默认"未计入清单章节项"排列在最后\n' + ' 2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n', fun: function (ctx, data, fieldsKey, options) { if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return; const code = fieldsKey[0].field; const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : ''; const name = fieldsKey.length > 2 ? fieldsKey[2].field : ''; const sortData = data[fieldsKey[0].table]; if (!sortData) return; sortData.sort(function (a, b) { if (chapter !== '') { if (a[name] === '未计入清单章节项') { return b[chapter] === '100' ? 1 : -1; } else if (b[name] === '未计入清单章节项') { return a[chapter] === '100' ? -1 : 1 } else { return ctx.helper.compareCode(a[code], b[code]); } } else { return ctx.helper.compareCode(a[code], b[code]); } }); } }; const gatherChapter = { name: '汇总章级数据', hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n'+ '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' + '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' + '注意事项:\n' + '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' + '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' + '示例:\n' + 'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' + 'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' + '结果:\n' + '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据', defaultSetting: { count: 9, unChapter: { name: '未计入清单章节合计', order: 1, }, gclSum: { name: '清单小计', order: 2, }, unGcl: { name: '非清单项费用', order: 3, }, sum: { name: '合计', order: 4, } }, customSetting1: { count: 7, gclSum: { name: '第100章至700章清单合计', order: 1, }, custom: [ {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2}, {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3}, {name: '计日工合计', node_type: '计日工', order: 4}, {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', node_type: '暂列金额', order: 5}, {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6}, ], }, _getCalcChapter: function (chapter, options) { const gclChapter = [], otherChapter = [], customChapter = []; let serialNo = 1; for (const c of chapter) { const cc = { code: c.code, name: c.name, cType: 1 }; cc.serialNo = serialNo++; cc.filter = '^' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}-'; gclChapter.push(cc); if (serialNo > options.count) break; } if (options.unChapter) { gclChapter.push({ name: options.unChapter.name, cType: 21, serialNo: serialNo + options.unChapter.order, order: options.unChapter.order }); } if (options.gclSum) { otherChapter.push({ name: options.gclSum.name, cType: 11, serialNo: serialNo + options.gclSum.order, order: options.gclSum.order }); } if (options.unGcl) { otherChapter.push({ name: options.unGcl.name, cType: 31, serialNo: serialNo + options.unGcl.order, order: options.unGcl.order }); } if (options.sum) { otherChapter.push({ name: options.sum.name , cType: 41, serialNo: serialNo + options.sum.order, order: options.sum.order }); } if (options.custom && options.custom instanceof Array) { for (const c of options.custom) { const cc = { name: c.name, serialNo: serialNo + c.order, order_calc: c.order_calc, cType: 5, order: c.order, }; if (c.node_type && c.node_type !== '') { const nodeType = standard.nodeType.find(function (x) {return x.text === c.node_type}); cc.node_type = nodeType.value; } else { cc.node_type = -1; } customChapter.push(cc); } } return [gclChapter, otherChapter, customChapter]; }, _getGclChapter: function (chapter, data) { for (const c of chapter) { if (c.filter) { const reg = new RegExp(c.filter); if (reg.test(data.b_code)) { return c; } } else { return c; } } }, _checkFilter: function (fullPath, filter) { for (const f of filter) { if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true; } return false; }, _orderCalc: function (ctx, chapter, fields) { const orderMatch = new RegExp('o[0-9]+', 'igm'); for (const c of chapter) { if (c.order_calc && c.order_calc !== '') { console.log(c.order_calc); const matchs = c.order_calc.match(orderMatch); const calcMatch = []; for (const m of matchs) { const order = m.substring(1, m.length); console.log(order); const orderChapter = chapter.find(function (x) {return x.order == order}); console.log(orderChapter); if (orderChapter) { calcMatch.push({match: m, value: orderChapter}) } } console.log(calcMatch); for (const f of fields) { let expr = c.order_calc; for (const m of calcMatch) { expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0); } try { c[f] = ctx.helper.round(math.eval(expr), 6); } catch(err) { } } } } }, fun: function (ctx, data, fieldsKey, options) { if (!data.tender_info || !data.tender_info.chapter) return; if (!fieldsKey && fieldsKey.length < 0) return; const calcFields = []; const gatherData = function (chapter, data) { if (!chapter) return; for (const f in data) { if (data[f] && (f.indexOf('tp') >= 0 || f === 'total_price')) { chapter[f] = ctx.helper.add(chapter[f], data[f]); if (calcFields.indexOf(f) === -1) calcFields.push(f); } } }; const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting); const fields = ctx.helper._.map(fieldsKey, 'field'); const needFields = ['b_code', 'is_leaf']; for (const nf of needFields) { if (fields.indexOf(nf) === -1) return; } const sourceData = data[fieldsKey[0].table]; if (!sourceData) return; const filter = []; for (const d of sourceData) { for (const c of customChapter) { if (c.node_type && c.node_type > 0 && c.node_type === d.node_type) { gatherData(c, d); filter.push(d.full_path); } } if (!d.is_leaf || this._checkFilter(d.full_path, filter)) continue; for (const c of otherChapter) { if (c.cType === 41) { gatherData(c, d); } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) { gatherData(c, d); } else if (c.cType === 11 && (d.b_code)) { gatherData(c, d); } } if (d.b_code) { const c = this._getGclChapter(gclChapter, d); gatherData(c, d); } } const chapter = gclChapter.concat(otherChapter).concat(customChapter); this._orderCalc(ctx, chapter, calcFields); chapter.sort(function (a, b) {return a.serialNo - b.serialNo}); data[fieldsKey[0].table] = chapter; }, }; const analysisObj = { changeSort, gatherGcl, sortGcl, gatherChapter, }; const analysisDefine = (function (obj) { const result = []; for (const o in obj) { const analysis = obj[o]; if (analysis.name && analysis.hint && analysis.fun) { result.push({ name: analysis.name, key: o, hint: analysis.hint, }) } } return result; })(analysisObj); module.exports = { analysisObj, analysisDefine };