Browse Source

报表,期,提供年度数据

MaiXinRong 2 years ago
parent
commit
af725a88f5
1 changed files with 73 additions and 0 deletions
  1. 73 0
      app/service/report_memory.js

+ 73 - 0
app/service/report_memory.js

@@ -29,6 +29,7 @@ const billsFields = (function () {
     const cur = ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'qc_minus_tp', 'gather_qty', 'gather_tp', 'postil'];
     const pre = ['pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_qc_minus_qty', 'pre_qc_minus_tp', 'pre_gather_qty', 'pre_gather_tp'];
     const end = ['end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_qc_minus_qty', 'end_qc_minus_tp', 'end_gather_qty', 'end_gather_tp'];
+    const year = ['year_contract_qty', 'year_contract_tp', 'year_qc_qty', 'year_qc_tp', 'year_qc_minus_qty', 'year_qc_minus_tp', 'year_gather_qty', 'year_gather_tp'];
     const final = ['final_tp', 'final_ratio'];
     const final1 = [ 'final_1_qty', 'final_1_tp', 'final_1_ratio'];
     const stageDgn = ['deal_dgn_qty1', 'deal_dgn_qty2', 'c_dgn_qty1', 'c_dgn_qty2'];
@@ -382,6 +383,74 @@ module.exports = app => {
             }
         }
 
+        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 _gatherStageBills(tender, stages) {
+            let billsIndexData = {};
+            const helper = this.ctx.helper;
+            const sumAssignRelaData = function (index, rela) {
+                const loadFields = function (datas, fields, prefix, relaId) {
+                    for (const d of datas) {
+                        if (!index[d[relaId]]) index[d[relaId]] = {};
+                        const m = index[d[relaId]];
+                        m[relaId] = d[relaId];
+                        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);
+                }
+            };
+            for (const stage of stages) {
+                await this.ctx.service.stage.doCheckStage(stage);
+                const curStage = stage.readOnly
+                    ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
+                    : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
+                const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
+                sumAssignRelaData(billsIndexData, [
+                    { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: '', relaId: 'lid' },
+                    { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
+                ]);
+            }
+            const result = [];
+            for (const prop in billsIndexData) {
+                const yearStageBills = billsIndexData[prop];
+                yearStageBills.gather_qty = helper.add(yearStageBills.contract_qty, yearStageBills.qc_qty);
+                yearStageBills.gather_tp = helper.sum([yearStageBills.contract_tp, yearStageBills.qc_tp, yearStageBills.pc_tp]);
+                result.push(yearStageBills);
+            }
+            return result;
+        };
+
+        async _loadStageBillsZone(tender, zone){
+            const stages = await this._getTimeZoneStages(tender, zone);
+            return await this._gatherStageBills(tender, stages);
+        }
+
+        async _loadStageBillsYear(tender, year) {
+            return await this._loadStageBillsZone(tender, `${year}-01 - ${year}-12`);
+        }
+
         async getStageBillsData(tid, sid, fields) {
             try {
                 await this.ctx.service.tender.checkTender(tid);
@@ -413,11 +482,15 @@ module.exports = app => {
                 }
                 const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.ctx.stage.id } });
                 const endBpcStage = await this.ctx.service.stageBillsPc.getEndStageData(this.ctx.stage);
+                const yearStage = this._checkFieldsExistReg(fields, 'year_') && stage.s_time
+                    ? await this._loadStageBillsYear(this.ctx.service.tender, stage.s_time.split('-')[0])
+                    : [];
                 this.ctx.helper.assignRelaData(billsData, [
                     { data: curStage, fields: ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'postil'], prefix: '', relaId: 'lid' },
                     { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: 'pre_', relaId: 'lid' },
                     { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'org_price'], prefix: '', relaId: 'lid' },
                     { data: endBpcStage, fields: ['end_contract_pc_tp', 'end_qc_pc_tp', 'end_pc_tp', 'org_price_his'], prefix: '', relaId: 'lid' },
+                    { data: yearStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: 'year_', relaId: 'lid' },
                 ]);
                 billsData.forEach(x => {
                     if (x.org_price_his && x.org_price_his.length > 0) {