| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866 | 'use strict';/** * * * @author Mai * @date * @version */const math = require('mathjs');const standard = require('../const/standard');// 通用方法const rdaUtils = {    orderCalc: function (ctx, data, fields) {        const orderMatch = new RegExp('o[0-9]+', 'igm');        for (const c of data) {            if (c.order_calc && c.order_calc !== '') {                const matchs = c.order_calc.match(orderMatch);                const calcMatch = [];                for (const m of matchs) {                    const order = m.substring(1, m.length);                    const orderData = data.find(function (x) {return (x.order + '') === order});                    if (orderData) {                        calcMatch.push({match: m, value: orderData})                    }                }                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) {                    }                }            }        }    }};// 可提供的数据处理方法const changeSort = {    name: '变更令排序',    hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' +        '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' +        '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' +        '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"',    intro: '报表对于主从表的排序有要求,解决变更令数据中的变更令、变更清单排序问题',    /**     *     * @param ctx - context常量     * @param data - 全部数据源{Array}     * @param fieldsKey - 计算字段     * @param options - 计算设置     */    fun: function(ctx, data, fieldsKey, options) {        const change = options && options.change ? data[options.change] : data.change;        if (!change) return;        // 变更令排序        change.sort(function (a, b) {            return a.code.localeCompare(b.code);        });        const changeBills = options && options.changeBills ? data[options.changeBills] : data.change_audit_list;        if (changeBills) {            changeBills.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',    intro: '根据三级清单树结构,汇总平面工程量清单,目前仅支持mem_stage_bills表',    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',    intro: '根据选择列,对数据进行工程量清单排序,兼容1000章后清单,以及403-1-a类的非纯数字编号排序',    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章数据',    intro: '用于得到类似“清单汇总--章级合计”的数据,可通过options自定义数据',    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×暂列金额比列)', order: 5, match: [{node_type: '暂列金额'}, {field: 'name', part: '暂列金额'}, {field: 'name', part: '暂定金额'}]},            {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},        ],        rela: [            {                table: 'deal_bills', key: 'code',                fields: {source: 'total_price', target: 'ex_value1'},            },            {                table: 'mem_change_bills', key: 'code',                fields: {source: '', target: 'ex_value2'},            }        ]    },    _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 (options.activeFields) {                cc.active = cc.serialNo > options.count;            } else {                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.match) {                    cc.match = JSON.parse(JSON.stringify(c.match));                    for (const m of cc.match) {                        if (m.node_type && m.node_type !== '') {                            const nodeType = standard.nodeType.find(function (x) {return x.text === m.node_type});                            m.node_type = nodeType.value;                        }                    }                }                customChapter.push(cc);            }        }        return [gclChapter, otherChapter, customChapter];    },    _getGclChapter: function (chapter, data, field) {        for (const c of chapter) {            if (c.filter) {                const reg = new RegExp(c.filter);                if (reg.test(data[field])) {                    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;    },    _gatherRela: function (ctx, data, rela, gclChapter, otherChapter) {        if (!rela) return;        const gatherRelaFields = function (chapter, source, field) {            const fields = field instanceof Array ? field : [field];            for (const f of fields) {                chapter[f.target] = ctx.helper.add(chapter[f.target], source[f.source]);            }        };        const relaBills = rela instanceof Array ? rela : [rela];        for (const rb of relaBills) {            if (!rb.table) continue;            const relaData = data[rb.table];            if (!relaData) continue;            for (const rd of relaData) {                for (const c of otherChapter) {                    if (c.cType === 41) {                        gatherRelaFields(c, rd, rb.fields);                    } else if (c.cType === 31 && (!rd[rb.key] || rd[rb.key] === '')) {                        gatherRelaFields(c, rd, rb.fields);                    } else if (c.cType === 11 && (rd[rb.key])) {                        gatherRelaFields(c, rd, rb.fields);                    }                }                if (rd[rb.key]) {                    const c = this._getGclChapter(gclChapter, rd, rb.key);                    gatherRelaFields(c, rd, rb.fields);                }            }        }    },    _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 !== '') {                const matchs = c.order_calc.match(orderMatch);                const calcMatch = [];                for (const m of matchs) {                    const order = m.substring(1, m.length);                    const orderChapter = chapter.find(function (x) {return x.order == order});                    if (orderChapter) {                        calcMatch.push({match: m, value: orderChapter})                    }                }                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) {                    }                }            }        }    },    _checkMatch: function (match, d) {        for (const m of match) {            if (m.node_type) {                if (m.node_type === d.node_type) return true;            } else if (m.field) {                const value = d[m.field];                if (m.part && value) {                    if (value.indexOf(m.part) >= 0) return true;                }            }        }        return false;    },    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.match && this._checkMatch(c.match, d)) {                    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, 'b_code');                if (c) {                    gatherData(c, d);                }            }        }        this._gatherRela(ctx, data, options.rela, gclChapter, otherChapter);        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.filter(function (x) {            if (x.active) {                for (const f of options.activeFields) {                    if (!ctx.helper.checkZero(x[f])) return true;                }                return false;            } else {                return true;            }        });    },};const join = {    name: "连接两张数据表",    hint: "用于处理类似于关联签约清单的情况,会改变主表的数据",    intro: '用于处理类似于关联签约清单的情况,会根据关联表(sub)改变主表(main)的数据',    defaultSetting: {        main: 'mem_stage_bills',        sub: 'deal_bills',        keyFields: [            {main: 'b_code', sub: 'code', type: 'string'},            {main: 'name', sub: 'name',type: 'string'},            {main: 'unit', sub: 'unit',type: 'string'},            {main: 'unit_price', sub: 'unit_price',type: 'number'},        ],        importFields: [            {main: 'ex_value1', sub: 'quantity', type: 'sum'},            {main: 'ex_value2', sub: 'total_price', type: 'sum'}        ],        joinType: 'outer', //'outer', 'main', 'sub', 'inner',    },    fun: function (ctx, data, fields, options) {        if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;        const main = data[options.main];        const sub = data[options.sub];        if (!main || !sub) return;        const _ = ctx.helper._, result = _.cloneDeep(main);        for (const r of result) {            r._join_tag = 'main';            for (const i of options.importFields) {                r[i.main] = null;            }        }        for (const s of sub) {            let r = result.find(function (x) {                for (const k of options.keyFields) {                    switch (k.type) {                        case 'string':                            if (x[k.main] !== s[k.sub] && (!_.isNil(x[k.main]) || !_.isNil(s[k.sub]))) return false;                            break;                        case 'number':                            if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main] - s[k.sub]))) return false;                            break;                    }                }                return true;            });            if (r && r._join_tag === 'main') {                r._join_tag = 'both';            }            if (!r) {                r = {_join_tag: 'sub'};                for (const k of options.keyFields) {                    r[k.main] = s[k.sub];                }                result.push(r);            }            for (const i of options.importFields) {                //r[i.main] = s[i.sub];                if (i.type === 'sum') {                    r[i.main] = ctx.helper.add(r[i.main], s[i.sub]);                } else {                    r[i.main] =  s[i.sub];                }            }        }        switch (options.joinType) {            case 'main':                data[options.main] = _.filter(result, function (r) {return ['main', 'both'].indexOf(r._join_tag) >= 0});                break;            case 'sub':                data[options.main] = _.filter(result, function (r) {return ['sub', 'both'].indexOf(r._join_tag) >= 0});                break;            case 'inner':                data[options.main] = _.filter(result, function (r) {return r._join_tag === 'both'});                break;            case 'outer':                data[options.main] = result;                break;        }    }};const getChapterCode = {    name: '获取章级编号',    intro: '根据清单编号,计算章级编号,并写入指定字段,适用于未提供chapter字段的数据,例如签约清单',    hint: '',    defaultSetting: {        table: 'mem_stage_bills',        b_code: 'b_code',        chapter: 'chapter',    },    fun: function (ctx, data, fields, options) {        if (!options || !options.table || !options.b_code || !options.chapter) return;        const cData = data[options.table];        if (!cData) return;        for (const d of cData) {            d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);        }    }};const filter = {    name: '数据过滤',    intro: '根据设置过滤数据,会直接修改数据表',    defaultSetting: {        "table": "pay",        "condition": [            {"field": "minus", "type": "bool", "value": "false"},            {"field": "ptype", "type": "num", "operate": "=", "value": 1},            {"field": "name", "type": "str", "operate": "=", "value": "扣回"}        ],    },    _typeFun: {        bool: '_checkBoolean',        str: '_checkString',        num: '_checkNumber',    },    _checkBoolean: function (ctx, value, condition) {        switch (condition.value) {            case 'true':                return value ? true : false;            case 'false':                return value ? false : true;            default:                return true;        }    },    _checkNumber: function (ctx, value, condition) {        //if (ctx.helper._.isNil(value)) value = 0;        switch (condition.operate) {            case 'non-zero':                return !ctx.helper.checkZero(value);            case '=':                return !ctx.helper._.isNil(value) ? value === condition.value : false;            case '>':                return !ctx.helper._.isNil(value) ? value > condition.value : false;            case '<':                return !ctx.helper._.isNil(value) ? value < condition.value : false;            case '>=':                return !ctx.helper._.isNil(value) ? value >= condition.value : false;            case '<=':                return !ctx.helper._.isNil(value) ? value <= condition.value : false;            default:                return true;        }    },    _checkString: function (ctx, value, condition) {        switch (condition.operate) {            case '=':                return value === condition.value;            case 'part':                return !ctx.helper._.isNil(value) ? value.indexOf(condition.value) >= 0 : false;            case 'enum':                return (!ctx.helper._.isNil(value) && condition.value instanceof Array) ? condition.value.indexOf(value) >= 0 : false;            default:                return true;        }    },    fun: function (ctx, data, fields, options) {        if (!options || !options.table || !options.condition) return;        const fData = data[options.table], self = this;        if (!fData) return;        for (const c of options.condition) {            c.fun = this._typeFun[c.type];        }        const result = fData.filter(function (d) {            for (const c of options.condition) {                if (!self[c.fun](ctx, d[c.field], c)) return false;            }            return true;        });        data[options.table] = result;    }};const gatherStagePay = {    name: '汇总合同支付数据',    intro: '根据付扣款项等,分类汇总合同支付的数据',    defaultSetting: {        table: 'mem_stage_pay',        custom: [            {name: '本期完成计量', ptype: 4, order: 1, visible: false},            {name: '业主违约罚金', match: '业主违约罚金', order: 2},            {name: '迟付款利息', match: '迟付款利息', order: 3},            {name: '其他付款', minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},            {name: '合计', order: 5, order_calc: 'o1+o2+o3+o4', },            {name: '动员预付款', order: 6},            {name: '扣动员预付款', match: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款'], order: 7},            {name: '扣材料预付款', match: '扣材料预付款', order: 8},            {name: '承包人违约罚金', match: '承包人违约罚金', order: 9},            {name: '保留金', match: '保留金', order: 10},            {name: '税金', match: '税金', order: 11},            {name: '质量保证金', match: '质量保证金', order: 12},            {name: '其他扣款', minus: 1, rid: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款', '扣材料预付款', '承包人违约罚金', '保留金', '税金', '质量保证金'], order: 13},            {name: '扣款合计', minus: 1, rid: [], order: 14},            {name: '支付', ptype: 2, order: 15},        ]    },    _gatherFields: function (ctx, d, source, calcFields) {        let filterData = source;        if (d.ptype) {            filterData = filterData.filter(function (x) {                return x.ptype === d.ptype;            });        }        if (d.match) {            filterData = filterData.filter(function (x) {                if (d.match instanceof Array) {                    for (const m of d.match) {                        if (x.name.indexOf(m) >= 0) return true;                    }                } else {                    return x.name.indexOf(d.match) >= 0;                }            });        }        if (!ctx.helper._.isNil(d.minus)) {            filterData = filterData.filter(function (x) {                return d.minus === 1 ? x.minus : !x.minus;            });        }        if (d.rid) {            filterData = filterData.filter(function (x) {                for (const r of d.rid) {                    if (x.name.indexOf(r) >= 0) return false;                }                return true;            });        }        for (const fd of filterData) {            for (const prop in fd) {                if (prop.indexOf('tp') >= 0) {                    d[prop] = ctx.helper.add(d[prop], fd[prop]);                    if (calcFields.indexOf(prop) === -1) calcFields.push(prop);                }            }        }    },    fun: function (ctx, data, fields, options) {        if (!options || !options.table || !options.custom) return;        const gatherData = data[options.table];        const result = [], calcFields = [];        for (const c of options.custom) {            const cData = JSON.parse(JSON.stringify(c));            if (!cData.order_calc && cData.empty !== 1) {                this._gatherFields(ctx, cData, gatherData, calcFields);            }            result.push(cData);        }        rdaUtils.orderCalc(ctx, result, calcFields);        data[options.table] = result.filter(function (x) {            return x.visible === undefined || x.visible;        });    },};const union = {    name: '合并数据',    intro: '类似sql的union,可合并任意数据,合并结果写到"预留扩展数据-合并(mem_union_data)"中',    defaultSetting: {        union: [            {                table: 'mem_stage_bills',                fields: [                    {target: 'str1', source: 'chapter'},                    {target: 'str2', source: 'name'},                    {target: 'tp1', source: 'total_price'},                    {target: 'tp2', source: 'gather_tp'},                    {target: 'tp3', source: 'pre_gather_tp'},                    {target: 'tp4', source: 'end_gather_tp'},                    {target: 'str3', data: '章级合计哟'},                ]            }, {                table: 'mem_stage_pay',                fields: [                    {target: 'str2', source: 'name'},                    {target: 'tp2', source: 'tp'},                    {target: 'tp3', source: 'pre_tp'},                    {target: 'tp4', source: 'end_tp'},                    {target: 'str3', data: '合同支付哟'},                ]            }        ]    },    fun: function(ctx, data, fields, options) {        if (!options || !options.union) return;        const result = [];        for (const u of options.union) {            const unionData = data[u.table];            for (const d of unionData) {                const nd = {};                for (const f of u.fields) {                    if (f.data) {                        nd[f.target] = f.data;                    } else if (f.source) {                        nd[f.target] = d[f.source];                    }                }                result.push(nd);            }        }        data.mem_union_data = result;    }};const addSumChapter = {    name: '添加章级合计',    intro: '在排序好的工程量清单列表中,根据清单所属章级,添加章级标题行、合计行',    defaultSetting: {        table: 'mem_stage_bills',        code: 'b_code',        chapter: 'chapter',        stdChapter: { title: '%s章', sum: '%s章合计'},        otherChapter: { title: '其他章', sum: '其他章合计'},        sum: { name: '总合计' },        fields: ['ex_value1', 'ex_value2', 'end_gather_tp', 'pre_gather_tp', 'gather_tp'],    },    fun: function (ctx, data, fields, options) {        if (!options || !options.table || !options.code || !options.chapter || !options.stdChapter) return;        const gclData = data[options.table];        if (!gclData || !data.tender_info) return;        const chapters = ctx.helper._.uniq(ctx.helper._.map(gclData, options.chapter));        const finalSum = options.sum ? {name: options.sum.name, chapterNum: 100000, chapterPart: 1} : null;        for (const chapter of chapters) {            const chapterInfo = data.tender_info.chapter.find(function (x) { return x.code === chapter});            const chapterOptions = chapterInfo ? options.stdChapter : options.otherChapter;            // sum            const chapterGcl = gclData.filter(function (x) {                return x.chapter === chapter;            });            const sum = { chapter: chapter, chapterPart: 3, chapterNum: parseInt(chapter) };            for (const cg of chapterGcl) {                cg.chapterPart = 2;                cg.chapterNum = parseInt(cg[options.chapter]);                for (const f of options.fields) {                    sum[f] = ctx.helper.add(sum[f], cg[f]);                    if (finalSum) {                        finalSum[f] = ctx.helper.add(finalSum[f], cg[f]);                    }                }            }            if (chapterOptions.sum !== undefined) {                sum.name = chapterOptions.sum.replace('%s', chapter);                gclData.push(sum);            }            // title            if (chapterOptions.title !== undefined) {                const title = { chapter: chapter, chapterPart: 1, chapterNum: parseInt(chapter) };                if (chapterInfo) {                    title[options.code] = options.stdChapter.title.replace('%s', chapter);                    title.name = chapterInfo.name;                } else {                    title.name = chapterOptions.title;                }                gclData.push(title);            }        }        if (finalSum) {            gclData.push(finalSum);        }        gclData.sort(function (a, b) {            if (a.chapter !== b.chapter) {                return a.chapterNum - b.chapterNum;            } else if (a.chapterPart !== b.chapterPart) {                return a.chapterPart - b.chapterPart;            } else {                return ctx.helper.compareCode(a[options.code], b[options.code]);            }        });    }};const analysisObj = {    changeSort,    gatherGcl,    sortGcl,    gatherChapter,    join,    getChapterCode,    filter,    union,    gatherStagePay,    addSumChapter,};const analysisDefine = (function (obj) {    const result = [];    for (const o in obj) {        const analysis = obj[o];        if (analysis.name && analysis.fun) {            result.push({                name: analysis.name,                key: o,                hint: analysis.hint,                intro: analysis.intro,                url: '/help?m=report&s=analysis&d=' + o,            })        }    }    return result;})(analysisObj);module.exports = {    analysisObj,    analysisDefine};
 |