| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940 | 'use strict';/** * * * @author Mai * @date * @version */const Ledger = require('../lib/ledger');const PayCalculator = require('../lib/pay_calc');const auditConst = require('../const/audit');const payConst = require('../const/deal_pay');const moment = require('moment');const indexPre = 'id_';const normalPrefix = 't_', sumPrefix = 's_', specPrefix = 'ts_';const gatherUtils = {    completeGatherData: function (datas, completeDatas) {        for (const data of datas) {            for (const cd of completeDatas) {                data[cd.prefix + 'id'] = cd.id;                data[cd.prefix + 'name'] = cd.name;            }        }    },    gatherStage: function (tender, gatherNode, sourceNode, prefix, helper) {        gatherNode[prefix + 'id'] = tender.id;        gatherNode[prefix + 'name'] = tender.name;        gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);        gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);        gatherNode[prefix + "dgn_qty1"] = helper.add(gatherNode[prefix + "dgn_qty1"], sourceNode.dgn_qty1);        gatherNode[prefix + "dgn_qty2"] = helper.add(gatherNode[prefix + "dgn_qty2"], sourceNode.dgn_qty2);        gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);        gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);        gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);        gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);        gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);        gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);        gatherNode[prefix + "pre_contract_qty"] = helper.add(gatherNode[prefix + "pre_contract_qty"], sourceNode.pre_contract_qty);        gatherNode[prefix + "pre_contract_tp"] = helper.add(gatherNode[prefix + "pre_contract_tp"], sourceNode.pre_contract_tp);        gatherNode[prefix + "pre_qc_qty"] = helper.add(gatherNode[prefix + "pre_qc_qty"], sourceNode.pre_qc_qty);        gatherNode[prefix + "pre_qc_tp"] = helper.add(gatherNode[prefix + "pre_qc_tp"], sourceNode.pre_qc_tp);        gatherNode[prefix + "pre_gather_qty"] = helper.add(gatherNode[prefix + "pre_gather_qty"], sourceNode.pre_gather_qty);        gatherNode[prefix + "pre_gather_tp"] = helper.add(gatherNode[prefix + "pre_gather_tp"], sourceNode.pre_gather_tp);        gatherNode[prefix + "end_contract_qty"] = helper.add(gatherNode[prefix + "end_contract_qty"], sourceNode.end_contract_qty);        gatherNode[prefix + "end_contract_tp"] = helper.add(gatherNode[prefix + "end_contract_tp"], sourceNode.end_contract_tp);        gatherNode[prefix + "end_qc_qty"] = helper.add(gatherNode[prefix + "end_qc_qty"], sourceNode.end_qc_qty);        gatherNode[prefix + "end_qc_tp"] = helper.add(gatherNode[prefix + "end_qc_tp"], sourceNode.end_qc_tp);        gatherNode[prefix + "end_gather_qty"] = helper.add(gatherNode[prefix + "end_gather_qty"], sourceNode.end_gather_qty);        gatherNode[prefix + "end_gather_tp"] = helper.add(gatherNode[prefix + "end_gather_tp"], sourceNode.end_gather_tp);        gatherNode[prefix + "deal_dgn_qty1"] = helper.add(gatherNode[prefix + "deal_dgn_qty1"], sourceNode.deal_dgn_qty1);        gatherNode[prefix + "deal_dgn_qty2"] = helper.add(gatherNode[prefix + "deal_dgn_qty2"], sourceNode.deal_dgn_qty2);        gatherNode[prefix + "c_dgn_qty1"] = helper.add(gatherNode[prefix + "c_dgn_qty1"], sourceNode.c_dgn_qty1);        gatherNode[prefix + "c_dgn_qty2"] = helper.add(gatherNode[prefix + "c_dgn_qty2"], sourceNode.c_dgn_qty2);        gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);        gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);        gatherNode['s_' + "dgn_qty1"] = helper.add(gatherNode['s_' + "dgn_qty1"], sourceNode.dgn_qty1);        gatherNode['s_' + "dgn_qty2"] = helper.add(gatherNode['s_' + "dgn_qty2"], sourceNode.dgn_qty2);        gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['s_' + "contract_qty"], sourceNode.contract_qty);        gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['s_' + "contract_tp"], sourceNode.contract_tp);        gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['s_' + "qc_qty"], sourceNode.qc_qty);        gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['s_' + "qc_tp"], sourceNode.qc_tp);        gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['s_' + "gather_qty"], sourceNode.gather_qty);        gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['s_' + "gather_tp"], sourceNode.gather_tp);        gatherNode['s_' + "pre_contract_qty"] = helper.add(gatherNode['s_' + "pre_contract_qty"], sourceNode.pre_contract_qty);        gatherNode['s_' + "pre_contract_tp"] = helper.add(gatherNode['s_' + "pre_contract_tp"], sourceNode.pre_contract_tp);        gatherNode['s_' + "pre_qc_qty"] = helper.add(gatherNode['s_' + "pre_qc_qty"], sourceNode.pre_qc_qty);        gatherNode['s_' + "pre_qc_tp"] = helper.add(gatherNode['s_' + "pre_qc_tp"], sourceNode.pre_qc_tp);        gatherNode['s_' + "pre_gather_qty"] = helper.add(gatherNode['s_' + "pre_gather_qty"], sourceNode.pre_gather_qty);        gatherNode['s_' + "pre_gather_tp"] = helper.add(gatherNode['s_' + "pre_gather_tp"], sourceNode.pre_gather_tp);        gatherNode['s_' + "end_contract_qty"] = helper.add(gatherNode['s_' + "end_contract_qty"], sourceNode.end_contract_qty);        gatherNode['s_' + "end_contract_tp"] = helper.add(gatherNode['s_' + "end_contract_tp"], sourceNode.end_contract_tp);        gatherNode['s_' + "end_qc_qty"] = helper.add(gatherNode['s_' + "end_qc_qty"], sourceNode.end_qc_qty);        gatherNode['s_' + "end_qc_tp"] = helper.add(gatherNode['s_' + "end_qc_tp"], sourceNode.end_qc_tp);        gatherNode['s_' + "end_gather_qty"] = helper.add(gatherNode['s_' + "end_gather_qty"], sourceNode.end_gather_qty);        gatherNode['s_' + "end_gather_tp"] = helper.add(gatherNode['s_' + "end_gather_tp"], sourceNode.end_gather_tp);        gatherNode['s_' + "deal_dgn_qty1"] = helper.add(gatherNode['s_' + "deal_dgn_qty1"], sourceNode.deal_dgn_qty1);        gatherNode['s_' + "deal_dgn_qty2"] = helper.add(gatherNode['s_' + "deal_dgn_qty2"], sourceNode.deal_dgn_qty2);        gatherNode['s_' + "c_dgn_qty1"] = helper.add(gatherNode['s_' + "c_dgn_qty1"], sourceNode.c_dgn_qty1);        gatherNode['s_' + "c_dgn_qty2"] = helper.add(gatherNode['s_' + "c_dgn_qty2"], sourceNode.c_dgn_qty2);    },    gatherZone: function (tender, gatherNode, sourceNode, prefix, helper) {        gatherNode[prefix + 'id'] = tender.id;        gatherNode[prefix + 'name'] = tender.name;        gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);        gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);        gatherNode[prefix + "dgn_qty1"] = helper.add(gatherNode[prefix + "dgn_qty1"], sourceNode.dgn_qty1);        gatherNode[prefix + "dgn_qty2"] = helper.add(gatherNode[prefix + "dgn_qty2"], sourceNode.dgn_qty2);        gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);        gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);        gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);        gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);        gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);        gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);        gatherNode[prefix + "deal_dgn_qty1"] = helper.add(gatherNode[prefix + "deal_dgn_qty1"], sourceNode.deal_dgn_qty1);        gatherNode[prefix + "deal_dgn_qty2"] = helper.add(gatherNode[prefix + "deal_dgn_qty2"], sourceNode.deal_dgn_qty2);        gatherNode[prefix + "c_dgn_qty1"] = helper.add(gatherNode[prefix + "c_dgn_qty1"], sourceNode.c_dgn_qty1);        gatherNode[prefix + "c_dgn_qty2"] = helper.add(gatherNode[prefix + "c_dgn_qty2"], sourceNode.c_dgn_qty2);        gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);        gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);        gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['s_' + "contract_qty"], sourceNode.contract_qty);        gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['s_' + "contract_tp"], sourceNode.contract_tp);        gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['s_' + "qc_qty"], sourceNode.qc_qty);        gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['s_' + "qc_tp"], sourceNode.qc_tp);        gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['s_' + "gather_qty"], sourceNode.gather_qty);        gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['s_' + "gather_tp"], sourceNode.gather_tp);    },    gatherLedger: function (tender, gatherNode, sourceNode, prefix, helper) {        gatherNode[prefix + 'id'] = tender.id;        gatherNode[prefix + 'name'] = tender.name;        gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);        gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);        gatherNode[prefix + "dgn_qty1"] = helper.add(gatherNode[prefix + "dgn_qty1"], sourceNode.dgn_qty1);        gatherNode[prefix + "dgn_qty2"] = helper.add(gatherNode[prefix + "dgn_qty2"], sourceNode.dgn_qty2);        gatherNode['s_' + "qty"] = helper.add(gatherNode['s_' + "qty"], sourceNode.quantity);        gatherNode['s_' + "tp"] = helper.add(gatherNode['s_' + "tp"], sourceNode.total_price);    },    gatherSpecial: function (gatherNode, sourceNode, prefix, helper) {        gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);        gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);        gatherNode[prefix + "dgn_qty1"] = helper.add(gatherNode[prefix + "dgn_qty1"], sourceNode.dgn_qty1);        gatherNode[prefix + "dgn_qty2"] = helper.add(gatherNode[prefix + "dgn_qty2"], sourceNode.dgn_qty2);    },};module.exports = app => {    class RptGatherMemory extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局context         * @return {void}         */        constructor(ctx) {            super(ctx);            this.resultTree = new Ledger.gatherTree(ctx, {                id: 'id',                pid: 'pid',                order: 'order',                level: 'level',                rootId: -1            });            this.resultTenderInfo = [];            this.resultDealPay = [];            this.resultDealBills = [];        }        async _getValidStages(tenderId) {            const stages = await this.db.select(this.ctx.service.stage.tableName, {                where: { tid: tenderId },                orders: [['order', 'desc']],            });            if (stages.length !== 0) {                const lastStage = stages[0];                if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {                    stages.splice(0, 1);                }            }            return stages;        }        async _getCheckedStages(tenderId) {            const stages = await this.db.select(this.ctx.service.stage.tableName, {                where: { tid: tenderId },                orders: [['order', 'desc']],            });            if (stages.length !== 0) {                const lastStage = stages[0];                if (lastStage.status !== auditConst.stage.status.checked) {                    stages.splice(0, 1);                }            }            return stages;        }        _checkSpecialTender(tender, special) {            if (special) {                for (const spec of special) {                    if (tender[spec.key] === true) return spec.key;                }            }            return '';        }        async _getTimeZoneStages(tender, zone) {            const times = zone.split(' - ');            if (times.length !== 2) throw '选择的汇总周期无效';            const beginTime = moment(times[0], 'YYYY-MM');            const endTime = moment(times[1], 'YYYY-MM');            const stages = await this._getValidStages(tender.id), validStages = [];            for (const stage of stages) {                const sTime = moment(stage.s_time, 'YYYY-MM');                if (sTime.isBetween(beginTime, endTime, null, '[]')) {                    validStages.push(stage);                }            }            return validStages;        }        async _getOrderZoneStages (tender, zone) {            let [iBegin, iEnd] = zone.split(':');            iBegin = this.ctx.helper._.toInteger(iBegin) || 0;            iEnd = this.ctx.helper._.toInteger(iEnd) || 0;            const stages = await this._getValidStages(tender.id), validStages = [];            for (const stage of stages) {                if (stage.order < iBegin || stage.order > iEnd) continue;                validStages.push(stage);            }            return validStages;        }        /**         * 台账数据         */        async _gatherStageData(completeData, tender, stage, hasPre) {            const helper = this.ctx.helper;            completeData.id = tender.id;            completeData.name = tender.name;            const billsTree = new Ledger.billsTree(this.ctx, {                id: 'ledger_id',                pid: 'ledger_pid',                order: 'order',                level: 'level',                rootId: -1,                keys: ['id', 'tender_id', 'ledger_id'],                stageId: 'id',                calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],                calc: function (node) {                    if (node.children && node.children.length === 0) {                        node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);                        node.gather_qty = helper.add(node.contract_qty, node.qc_qty);                        node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);                        node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);                        node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);                    }                    node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);                    node.gather_tp = helper.add(node.contract_tp, node.qc_tp);                    node.end_contract_tp = helper.add(node.pre_contract_tp, node.contract_tp);                    node.end_qc_tp = helper.add(node.pre_qc_tp, node.qc_tp);                    node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);                }            });            const billsData = await this.ctx.service.ledger.getData(tender.id);            const dgnData = await this.ctx.service.stageBillsDgn.getDgnData(tender.id);            for (const d of dgnData) {                const l = this.ctx.helper._.find(billsData, {id: d.id});                this.ctx.helper._.assignIn(l, d);            }            if (stage) {                await this.ctx.service.stage.doCheckStage(stage);                if (stage.readOnly) {                    const curStage = await this.ctx.service.stageBills.getAuditorStageData2(tender.id,                        stage.id, stage.curTimes, stage.curOrder);                    this.ctx.helper.assignRelaData(billsData, [                        {                            data: curStage,                            fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'],                            prefix: '',                            relaId: 'lid'                        }                    ]);                } else {                    const curStage = await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);                    this.ctx.helper.assignRelaData(billsData, [                        {                            data: curStage,                            fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'],                            prefix: '',                            relaId: 'lid'                        }                    ]);                }                if (hasPre) {                    const preStage = stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];                    this.ctx.helper.assignRelaData(billsData, [                        {                            data: preStage,                            fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'],                            prefix: 'pre_',                            relaId: 'lid'                        }                    ]);                }            }            billsTree.loadDatas(billsData);            billsTree.calculateAll();            this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {                gatherUtils.gatherStage(tender, gatherNode, sourceNode, completeData.prefix, helper);            });        }        async _gatherStagesData(completeData, tender, stages) {            const helper = this.ctx.helper;            completeData.id = tender.id;            completeData.name = tender.name;            /**             * 汇总并合并 相关数据             * @param {Array} index - 主数据             * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}             */            const sumAssignRelaData = function (index, rela) {                const loadFields = function (datas, fields, prefix, relaId) {                    for (const d of datas) {                        const key = indexPre + d[relaId];                        const m = index[key];                        if (m) {                            for (const f of fields) {                                if (d[f] !== undefined) {                                    m[prefix + f] = helper.add(m[prefix + f], d[f]);                                }                            }                        }                    }                };                for (const r of rela) {                    loadFields(r.data, r.fields, r.prefix, r.relaId);                }            };            const billsTree = new Ledger.billsTree(this.ctx, {                id: 'ledger_id',                pid: 'ledger_pid',                order: 'order',                level: 'level',                rootId: -1,                keys: ['id', 'tender_id', 'ledger_id'],                stageId: 'id',                calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],                calc: function (node) {                    if (node.children && node.children.length === 0) {                        node.gather_qty = helper.add(node.contract_qty, node.qc_qty);                    }                    node.gather_tp = helper.add(node.contract_tp, node.qc_tp);                }            });            const billsData = await this.ctx.service.ledger.getData(tender.id);            const dgnData = await this.ctx.service.stageBillsDgn.getDgnData(tender.id);            for (const d of dgnData) {                const l = this.ctx.helper._.find(billsData, {id: d.id});                this.ctx.helper._.assignIn(l, d);            }            let billsIndexData = {};            for (const bd of billsData) {                billsIndexData[indexPre + bd.id] = bd;            }            for (const stage of stages) {                await this.ctx.service.stage.doCheckStage(stage);                if (stage.readOnly) {                    const curStage = await this.ctx.service.stageBills.getAuditorStageData2(tender.id,                        stage.id, stage.curTimes, stage.curOrder);                    sumAssignRelaData(billsIndexData, [{                        data: curStage,                        fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'],                        prefix: '',                        relaId: 'lid'                    }]);                } else {                    const curStage = await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);                    sumAssignRelaData(billsIndexData, [{                        data: curStage,                        fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'],                        prefix: '',                        relaId: 'lid'                    }]);                }            }            billsTree.loadDatas(billsData);            billsTree.calculateAll();            this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {                gatherUtils.gatherZone(tender, gatherNode, sourceNode, completeData.prefix, helper);            });        }        async _gatherMonthData(tender, completeData, month, hasPre) {            const stages = await this._getValidStages(tender.id);            const stage = this.ctx.helper._.find(stages, {s_time: month});            await this._gatherStageData(completeData, tender, stage, hasPre);        }        async _gatherIndexData(tender, completeData, index, hasPre) {            const stages = await this._getValidStages(tender.id);            const stage = this.ctx.helper._.find(stages, {order: index});            await this._gatherStageData(completeData, tender, stage, hasPre);        }        async _gatherZoneData(tender, completeData, zone) {            const stages = await this._getTimeZoneStages(tender, zone);            await this._gatherStagesData(completeData, tender, stages);        }        async _gatherIndexZoneData(tender, completeData, stageZone) {            const stages = await this._getOrderZoneStages(tender, stageZone);            await this._gatherStagesData(completeData, tender, stages);        }        async _gatherFinalData(tender, completeData, hasPre) {            const stages = await this._getValidStages(tender.id);            await this._gatherStageData(completeData, tender, stages[0], hasPre);        }        async _gatherCheckedFinalData(tender, completeData, hasPre) {            const stages = await this._getCheckedStages(tender.id);            await this._gatherStageData(completeData, tender, stages[0], hasPre);        }        async _gatherLedgerData(tender, completeData) {            const helper = this.ctx.helper;            const billsTree = new Ledger.billsTree(this.ctx, {                id: 'ledger_id',                pid: 'ledger_pid',                order: 'order',                level: 'level',                rootId: -1,                keys: ['id', 'tender_id', 'ledger_id'],                stageId: 'id',                calcFields: ['deal_tp', 'total_price'],            });            const billsData = await this.ctx.service.ledger.getData(tender.id);            billsTree.loadDatas(billsData);            billsTree.calculateAll();            this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {                gatherUtils.gatherLedger(tender, gatherNode, sourceNode, completeData.prefix, helper);            });        }        async _gatherSpecialData(tender, sKey) {            const helper = this.ctx.helper;            const billsTree = new Ledger.billsTree(this.ctx, {                id: 'ledger_id',                pid: 'ledger_pid',                order: 'order',                level: 'level',                rootId: -1,                keys: ['id', 'tender_id', 'ledger_id'],                stageId: 'id',                calcFields: ['deal_tp', 'total_price'],            });            const billsData = await this.ctx.service.ledger.getData(tender.id);            billsTree.loadDatas(billsData);            billsTree.calculateAll();            this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {                gatherUtils.gatherSpecial(gatherNode, sourceNode, 'ts_' + sKey + '_', helper);            });        }        async getGatherStageBills(memFieldKeys, gsDefine, gsCustom) {            if (!gsDefine || !gsDefine.enable) return [];            if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];            this.resultTree.clear();            const gsSetting = JSON.parse(gsDefine.setting);            let commonIndex = 0;            const completeDatas = [];            for (const t of gsCustom.tenders) {                const specialKey = this._checkSpecialTender(t, gsSetting.special);                const tender = await this.ctx.service.tender.getCheckTender(t.tid);                if (specialKey === '') {                    const completeData = {                        prefix: 't_' + commonIndex + '_',                    };                    completeDatas.push(completeData);                    switch (gsSetting.type) {                        case 'month':                            await this._gatherMonthData(tender, completeData, gsCustom.month, gsSetting.hasPre);                            break;                        case 'zone':                            await this._gatherZoneData(tender, completeData, gsCustom.zone);                            break;                        case 'final':                            await this._gatherFinalData(tender, completeData, gsSetting.hasPre);                            break;                        case 'checked-final':                            await this._gatherCheckedFinalData(tender, completeData, gsSetting.hasPre);                            break;                        case 'ledger':                            await this._gatherLedgerData(tender, completeData);                            break;                        case 'stage':                            await this._gatherIndexData(tender, completeData, gsCustom.stage, gsSetting.hasPre);                            break;                        case 'stage-zone':                            await this._gatherIndexZoneData(tender, completeData, gsCustom.stage_zone);                            break;                    }                    commonIndex++;                } else {                    await this._gatherSpecialData(tender, specialKey);                }            }            this.resultTree.generateSortNodes();            gatherUtils.completeGatherData(this.resultTree.nodes, completeDatas);            return this.resultTree.getDefaultDatas();        }        /**         * 标段信息         */        async _getBaseTenderInfo(tender) {            const info = {                id: tender.id,                name: tender.name,                deal_tp: tender.deal_tp,                tp: tender.total_price            };            const deal_bills_sum = await this.ctx.service.dealBills.getSum(tender.id);            info.deal_bills_tp = deal_bills_sum ? deal_bills_sum.total_price : 0;            if (tender.ledger_status === auditConst.ledger.status.uncheck || tender.ledger_status === auditConst.ledger.status.checkNo) {                const sum = await this.ctx.service.ledger.addUp({tender_id: tender.id/*, is_leaf: true*/});                info.tp = sum.total_price;                info.deal_tp = sum.deal_tp;            }            info.deal_info = tender.info.deal_info;            info.construction_unit = tender.info.construction_unit;            info.tech_param = tender.info.tech_param;            info.deal_param = tender.info.deal_param;            info.pay_account = tender.info.pay_account;            info.deal_param.contractPriceWithoutZL = this.ctx.helper.sub(info.deal_param.contractPrice,                info.deal_param.zanLiePrice);            info.bid_info = tender.info.bid_info;            return info;        }        async _getStageTenderInfo(stage, info) {            if (stage) {                const helper = this.ctx.helper;                await this.ctx.service.stage.doCheckStage(stage);                await this.ctx.service.stage.checkStageGatherData(stage);                info.pre_contract_tp = stage.pre_contract_tp;                info.pre_qc_tp = stage.pre_qc_tp;                info.pre_gather_tp = helper.add(info.pre_contract_tp, info.pre_qc_tp);                info.contract_tp = stage.contract_tp;                info.qc_tp = stage.qc_tp;                info.gather_tp = helper.add(info.contract_tp, info.qc_tp);                info.end_contract_tp = helper.add(info.pre_contract_tp, info.contract_tp);                info.end_qc_tp = helper.add(info.pre_qc_tp, info.qc_tp);                info.end_gather_tp = helper.add(info.pre_gather_tp, info.gather_tp);                info.yf_tp = stage.yf_tp;                info.pre_yf_tp = stage.pre_yf_tp;                info.end_yf_tp = helper.add(stage.yf_tp, stage.pre_yf_tp);            }        }        async _getStagesTenderInfo(stages, info) {            const helper = this.ctx.helper;            for (const stage of stages) {                await this.ctx.service.stage.doCheckStage(stage);                await this.ctx.service.stage.checkStageGatherData(stage);                info.contract_tp = helper.add(info.contract_tp, stage.contract_tp);                info.qc_tp = helper.add(info.qc_tp, stage.qc_tp);                info.yf_tp = helper.add(info.yf_tp, stage.yf_tp);            }            info.gather_tp = helper.add(info.contract_tp, info.qc_tp);        }        async _gatherMonthTenderInfo(tender, index, month, hasPre) {            const info = await this._getBaseTenderInfo(tender);            const stages = await this._getValidStages(tender.id);            const stage = this.ctx.helper._.find(stages, {s_time: month});            await this._getStageTenderInfo(stage, info);            this.resultTenderInfo.push(info);        }        async _gatherOrderTenderInfo(tender, index, order, hasPre) {            const info = await this._getBaseTenderInfo(tender);            const stages = await this._getValidStages(tender.id);            const stage = this.ctx.helper._.find(stages, {order: order});            await this._getStageTenderInfo(stage, info);            this.resultTenderInfo.push(info);        }        async _gatherZoneTenderInfo(tender, index, zone) {            const info = await this._getBaseTenderInfo(tender);            const stages = await this._getTimeZoneStages(tender, zone);            await this._getStagesTenderInfo(stages, info);            this.resultTenderInfo.push(info);        }        async _gatherOrderZoneTenderInfo(tender, index, stageZone) {            const info = await this._getBaseTenderInfo(tender);            const stages = await this._getOrderZoneStages(tender, stageZone);            await this._getStagesTenderInfo(stages, info);            this.resultTenderInfo.push(info);        }        async _gatherFinalTenderInfo(tender, index, hasPre) {            const info = await this._getBaseTenderInfo(tender);            const stages = await this._getValidStages(tender.id);            await this._getStageTenderInfo(stages[0], info);            this.resultTenderInfo.push(info);        }        async _gatherCheckedFinalTenderInfo(tender, index, hasPre) {            const info = await this._getBaseTenderInfo(tender);            const stages = await this._getCheckedStages(tender.id);            await this._getStageTenderInfo(stages[0], info);            this.resultTenderInfo.push(info);        }        async _gatherLedgerTenderInfo(tender, index) {            const info = await this._getBaseTenderInfo(tender);            this.resultTenderInfo.push(info);        }        async _gatherSpecialTenderInfo(tender, sKey) {            const info = await this._getBaseTenderInfo(tender);            info.spec = sKey;            this.resultTenderInfo.push(info);        }        async getGatherTenderInfo(memFieldKeys, gsDefine, gsCustom) {            if (!gsDefine || !gsDefine.enable) return [];            if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];            this.resultTenderInfo = [];            const gsSetting = JSON.parse(gsDefine.setting);            let commonIndex = 0;            for (const t of gsCustom.tenders) {                const specialKey = this._checkSpecialTender(t, gsSetting.special);                const tender = await this.ctx.service.tender.getCheckTender(t.tid);                if (specialKey === '') {                    switch (gsSetting.type) {                        case 'month':                            await this._gatherMonthTenderInfo(tender, commonIndex, gsCustom.month, gsSetting.hasPre);                            break;                        case 'zone':                            await this._gatherZoneTenderInfo(tender, commonIndex, gsCustom.zone);                            break;                        case 'final':                            await this._gatherFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);                            break;                        case 'checked-final':                            await this._gatherCheckedFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);                            break;                        case 'ledger':                            await this._gatherLedgerTenderInfo(tender, commonIndex);                            break;                        case 'stage':                            await this._gatherOrderTenderInfo(tender, commonIndex, gsCustom.stage, gsSetting.hasPre);                            break;                        case 'stage-zone':                            await this._gatherOrderZoneTenderInfo(tender, commonIndex, gsCustom.stage_zone);                            break;                    }                    commonIndex++;                } else {                    await this._gatherSpecialTenderInfo(tender, specialKey);                }            }            return this.resultTenderInfo;        }        /**         * 合同支付         */        _gatherPayRecord(dealPay, fun) {            let rdp;            if (dealPay.ptype !== payConst.payType.normal) {                rdp = this.ctx.helper._.find(this.resultDealPay, {ptype: dealPay.ptype});            } else {                rdp = this.ctx.helper._.find(this.resultDealPay, {                    name: dealPay.name,                    minus: dealPay.minus ? true : false,                    is_yf: dealPay.is_yf ? true : false,                });            }            if (!rdp) {                rdp = {                    ptype: dealPay.ptype,                    name: dealPay.name,                    minus: dealPay.minus ? true : false,                    is_yf: dealPay.is_yf ? true : false,                };                this.resultDealPay.push(rdp);            }            if (fun) fun(rdp, dealPay);        }        async _checkStagePayCalc(tender, stage, dealPay) {            if (!stage.readOnly) {                // 计算 本期金额                const payCalculator = new PayCalculator(this.ctx, stage, tender.info);                await payCalculator.calculateAll(dealPay);            }        }        async _gatherStagePay(completeData, tender, stage, hasPre) {            if (stage) {                completeData.id = tender.id;                completeData.name = tender.name;                const helper = this.ctx.helper;                await this.ctx.service.stage.doCheckStage(stage);                const dealPay = await this.ctx.service.stagePay.getStagePays(stage);                await this._checkStagePayCalc(tender, stage, dealPay);                for (const dp of dealPay) {                    dp.end_tp = helper.add(dp.pre_tp, dp.tp);                    this._gatherPayRecord(dp, function (gatherData, sourceData) {                        gatherData[completeData.prefix + 'id'] = tender.id;                        gatherData[completeData.prefix + 'name'] = tender.name;                        gatherData[completeData.prefix + 'tp'] = helper.add(gatherData[completeData.prefix + 'tp'], sourceData.tp);                        gatherData[completeData.prefix + 'pre_tp'] = helper.add(gatherData[completeData.prefix + 'pre_tp'], sourceData.pre_tp);                        gatherData[completeData.prefix + 'end_tp'] = helper.add(gatherData[completeData.prefix + 'end_tp'], sourceData.end_tp);                        gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);                        gatherData['s_' + 'pre_tp'] = helper.add(gatherData['s_' + 'pre_tp'], sourceData.pre_tp);                        gatherData['s_' + 'end_tp'] = helper.add(gatherData['s_' + 'end_tp'], sourceData.end_tp);                    });                }            }        }        async _gatherStagesPay(completeData, tender, stages) {            const helper = this.ctx.helper;            completeData.id = tender.id;            completeData.name = tender.name;            for (const stage of stages) {                await this.ctx.service.stage.doCheckStage(stage);                const dealPay = await this.ctx.service.stagePay.getStagePays(stage);                await this._checkStagePayCalc(tender, stage, dealPay);                for (const dp of dealPay) {                    dp.end_tp = helper.add(dp.pre_tp, dp.tp);                    this._gatherPayRecord(dp, function (gatherData, sourceData) {                        gatherData[completeData.prefix + 'id'] = tender.id;                        gatherData[completeData.prefix + 'name'] = tender.name;                        gatherData[completeData.prefix + 'tp'] = helper.add(gatherData[completeData.prefix + 'tp'], sourceData.tp);                        gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);                    });                }            }        }        async _gatherMonthStagePay(sTender, completeData, month, hasPre) {            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);            const stages = await this._getValidStages(tender.id);            const stage = this.ctx.helper._.find(stages, {s_time: month});            await this._gatherStagePay(completeData, tender, stage, hasPre);        }        async _gatherOrderStagePay(sTender, completeData, order, hasPre) {            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);            const stages = await this._getValidStages(tender.id);            const stage = this.ctx.helper._.find(stages, {order: order});            await this._gatherStagePay(completeData, tender, stage, hasPre);        }        async _gatherZoneStagePay(sTender, completeData, zone) {            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);            const stages = await this._getTimeZoneStages(tender, zone);            await this._gatherStagesPay(completeData, tender, stages);        }        async _gatherOrderZoneStagePay(sTender, completeData, stageZone) {            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);            const stages = await this._getOrderZoneStages(tender, stageZone)            await this._gatherStagesPay(completeData, tender, stages);        }        async _gatherFinalStagePay(sTender, completeData, hasPre) {            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);            const stages = await this._getValidStages(tender.id);            await this._gatherStagePay(completeData, tender, stages[0], hasPre);        }        async _gatherCheckedFinalStagePay(sTender, completeData, hasPre) {            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);            const stages = await this._getCheckedStages(tender.id);            await this._gatherStagePay(completeData, tender, stages[0], hasPre);        }        async getGatherStagePay(memFieldKeys, gsDefine, gsCustom) {            if (!gsDefine || !gsDefine.enable) return [];            if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];            this.resultTenderInfo = [];            const gsSetting = JSON.parse(gsDefine.setting);            let commonIndex = 0;            const completeDatas = [];            for (const tender of gsCustom.tenders) {                const specialKey = this._checkSpecialTender(tender, gsSetting.special);                if (specialKey === '') {                    const completeData = {                        prefix: 't_' + commonIndex + '_',                    };                    completeDatas.push(completeData);                    switch (gsSetting.type) {                        case 'month':                            await this._gatherMonthStagePay(tender, completeData, gsCustom.month, gsSetting.hasPre);                            break;                        case 'zone':                            await this._gatherZoneStagePay(tender, completeData, gsCustom.zone);                            break;                        case 'final':                            await this._gatherFinalStagePay(tender, completeData, gsSetting.hasPre);                            break;                        case 'checked-final':                            await this._gatherCheckedFinalStagePay(tender, completeData, gsSetting.hasPre);                            break;                        case 'stage':                            await this._gatherOrderStagePay(tender, completeData, gsCustom.stage, gsSetting.hasPre);                            break;                        case 'stage-zone':                            await this._gatherOrderZoneStagePay(tender, completeData, gsCustom.stage_zone);                            break;                    }                    commonIndex++;                }                // 合同支付,只有在每一期中有数据,故特殊标段直接不汇总合同支付            }            gatherUtils.completeGatherData(this.resultDealPay, completeDatas);            return this.resultDealPay;        }        /**         * 签约清单         */        async _gatherDealBills(tender, fun) {            const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({                where: { tender_id: tender.id }            });            for (const db of dealBills) {                let rdb = this.ctx.helper._.find(this.resultDealBills, {                    code: db.code,                    name: db.name,                    unit: db.unit,                    unit_price: db.unit_price ? db.unit_price: 0,                });                if (!rdb) {                    rdb = {                        code: db.code,                        name: db.name,                        unit: db.unit,                        unit_price: db.unit_price ? db.unit_price: 0,                    };                    this.resultDealBills.push(rdb);                }                if (fun) fun(rdb, db);            }        }        async _gatherCommonDealBills(sTender, completeData) {            const self = this;            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);            completeData.id = tender.id;            completeData.name = tender.name;            await this._gatherDealBills(tender, function (gatherData, sourceData) {                gatherData[completeData.prefix + 'id'] = tender.id;                gatherData[completeData.prefix + 'name'] = tender.name;                gatherData[completeData.prefix + 'qty'] = sourceData.quantity;                gatherData[completeData.prefix + 'tp'] = sourceData.total_price;                gatherData['s_' + 'qty'] = self.ctx.helper.add(gatherData['s_' + 'qty'], sourceData.quantity);                gatherData['s_' + 'tp'] = self.ctx.helper.add(gatherData['s_' + 'tp'], sourceData.total_price);            });        }        async _gatherSpecialDealBills(sTender, sKey) {            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);            await this._gatherDealBills(tender, function (gatherData, sourceData) {                const prefix = 'ts_' + sKey + '_';                gatherData[prefix + 'qty'] = sourceData.quantity;                gatherData[prefix + 'tp'] = sourceData.total_price;            });        }        async getGatherDealBills(memFieldKeys, gsDefine, gsCustom) {            if (!gsDefine || !gsDefine.enable) return [];            if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];            this.resultTenderInfo = [];            const gsSetting = JSON.parse(gsDefine.setting);            let commonIndex = 0;            const completeDatas = [];            for (const tender of gsCustom.tenders) {                const specialKey = this._checkSpecialTender(tender, gsSetting.special);                if (specialKey === '') {                    const completeData = {                        prefix: 't_' + commonIndex + '_',                    };                    completeDatas.push(completeData);                    await this._gatherCommonDealBills(tender, completeData);                    commonIndex++;                } else {                    await this._gatherSpecialDealBills(tender, specialKey);                }            }            gatherUtils.completeGatherData(this.resultDealBills, completeDatas);            return this.resultDealBills;        }    }    return RptGatherMemory;};
 |