'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_' + "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); }, 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 _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.getAuditorStageData(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.getLastestStageData(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 _gatherMonthData(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._gatherStageData(completeData, tender, stage, hasPre); } async _gatherZoneData(sTender, completeData, zone) { const helper = this.ctx.helper; /** * 汇总并合并 相关数据 * @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 tender = await this.ctx.service.tender.getCheckTender(sTender.tid); completeData.id = tender.id; completeData.name = tender.name; 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; } 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); for (const stage of stages) { const sTime = moment(stage.s_time, 'YYYY-MM'); if (sTime.isBetween(beginTime, endTime, null, '[]')) { await this.ctx.service.stage.doCheckStage(stage); if (stage.readOnly) { const curStage = await this.ctx.service.stageBills.getAuditorStageData(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.getLastestStageData(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(gatherNode, sourceNode, completeData.prefix, helper); }); } async _gatherFinalData(sTender, completeData, hasPre) { const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); const stages = await this._getValidStages(tender.id); await this._gatherStageData(completeData, tender, stages[0], hasPre); } async _gatherCheckedFinalData(sTender, completeData, hasPre) { const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); const stages = await this._getCheckedStages(tender.id); await this._gatherStageData(completeData, tender, stages[0], hasPre); } async _gatherLedgerData(sTender, 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 tender = await this.ctx.service.tender.getCheckTender(sTender.tid); 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(sTender, 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 tender = await this.ctx.service.tender.getCheckTender(sTender.tid); 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 []; 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._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; } 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); 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 _gatherMonthTenderInfo(sTender, index, month, hasPre) { const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); 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 _gatherZoneTenderInfo(sTender, index, zone) { const helper = this.ctx.helper; const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); const info = await this._getBaseTenderInfo(tender); 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, true); for (const stage of stages) { const sTime = moment(stage.s_time, 'YYYY-MM'); if (sTime.isBetween(beginTime, endTime, null, '[]')) { 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); this.resultTenderInfo.push(info); } async _gatherFinalTenderInfo(sTender, index, hasPre) { const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); 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(sTender, index, hasPre) { const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); 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(sTender, index) { const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); const info = await this._getBaseTenderInfo(tender); this.resultTenderInfo.push(info); } async _gatherSpecialTenderInfo(sTender, sKey) { const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); 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 tender of gsCustom.tenders) { const specialKey = this._checkSpecialTender(tender, gsSetting.special); 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; } 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 _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 _gatherZoneStagePay(sTender, completeData, zone) { const helper = this.ctx.helper; const tender = await this.ctx.service.tender.getCheckTender(sTender.tid); completeData.id = tender.id; completeData.name = tender.name; const stages = await this._getValidStages(tender.id); const times = zone.split(' - '); if (times.length !== 2) throw '选择的汇总周期无效'; const beginTime = moment(times[0], 'YYYY-MM'); const endTime = moment(times[1], 'YYYY-MM'); for (const stage of stages) { const sTime = moment(stage.s_time, 'YYYY-MM'); if (sTime.isBetween(beginTime, endTime, null, '[]')) { 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 _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; } 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; };