Browse Source

汇总表,多标段多期汇总调整

MaiXinRong 2 years ago
parent
commit
f4d7ba3da4
1 changed files with 63 additions and 21 deletions
  1. 63 21
      app/service/rpt_gather_memory.js

+ 63 - 21
app/service/rpt_gather_memory.js

@@ -238,10 +238,10 @@ module.exports = app => {
             this.resultDealBills = [];
         }
 
-        async _getValidStages(tenderId) {
+        async _getValidStages(tenderId, sort = 'desc') {
             const stages = await this.db.select(this.ctx.service.stage.tableName, {
                 where: { tid: tenderId },
-                orders: [['order', 'desc']],
+                orders: [['order', sort]],
             });
             if (stages.length !== 0) {
                 const lastStage = stages[0];
@@ -281,53 +281,71 @@ module.exports = app => {
             const beginTime = moment(times[0], 'YYYY-MM');
             const endTime = moment(times[1], 'YYYY-MM');
 
-            const stages = await this._getValidStages(tender.id), validStages = [];
+            const stages = await this._getValidStages(tender.id, 'asc'), validStages = [];
+            let preStage, endStage;
             for (const stage of stages) {
                 const sTime = moment(stage.s_time, 'YYYY-MM');
                 if (sTime.isBetween(beginTime, endTime, null, '[]')) {
                     validStages.push(stage);
+                } else if (sTime.isBefore(beginTime)) {
+                    if (!preStage || moment(preStage.s_time, 'YYYY-MM').isBefore(sTime)) preStage = stage;
+                } else if (sTime.isAfter(endTime)) {
+                    if (!endStage || moment(endStage.s_time, 'YYYY-MM').isAfter(sTime)) endStage = stage;
                 }
             }
-            return validStages;
+            return [validStages, preStage, endStage];
         }
         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 = [];
+            const stages = await this._getValidStages(tender.id, 'asc'), validStages = [];
+            let preStage, endStage;
             for (const stage of stages) {
-                if (stage.order < iBegin || stage.order > iEnd) continue;
-
-                validStages.push(stage);
+                if (stage.order < iBegin) {
+                    if (!preStage || preStage.order < stage.order) preStage = stage;
+                } else if (stage.order > iEnd) {
+                    if (!endStage || endStage.order > stage.order) endStage = stage;
+                } else {
+                    validStages.push(stage);
+                }
             }
-            return validStages;
+            return [validStages, preStage, endStage];
         }
         async _getCheckedZoneStages(tender, zone) {
-            console.log(zone);
             const times = zone.split(' - ');
             if (times.length !== 2) throw '选择的汇总周期无效';
             const beginTime = moment(times[0], 'YYYY-MM-DD');
             const endTime = moment(times[1], 'YYYY-MM-DD');
 
-            const stages = await this._getValidStages(tender.id), validStages = [];
+            const stages = await this._getValidStages(tender.id, 'asc'), validStages = [];
+            let preStage, endStage;
             for (const stage of stages) {
                 if (stage.status !== auditConst.stage.status.checked) continue;
-                const finalAudit = this.ctx.service.stageAudit.getLastestAuditor(stage.id, stage.times, stage.status);
+
+                const finalAudit = await this.ctx.service.stageAudit.getLastestAuditor(stage.id, stage.times, stage.status);
                 if (!finalAudit) continue;
 
                 const sTime = moment(finalAudit.end_time);
+                // console.log(stage.order, finalAudit.end_time);
+                stage.checked_time = sTime;
                 if (sTime.isBetween(beginTime, endTime, null, '[]')) {
                     validStages.push(stage);
+                } else if (sTime.isBefore(beginTime)) {
+                    if (!preStage || preStage.checked_time.isBefore(sTime)) preStage = stage;
+                } else if (sTime.isAfter(endTime)) {
+                    if (!endStage || endStage.checked_time.isAfter(sTime)) endStage = stage;
                 }
             }
-            return validStages;
+            // console.log(validStages.map(x => { return x.order }), preStage ? preStage.order : -1, endStage ? endStage.order : -1);
+            return [validStages, preStage, endStage];
         }
 
         /**
          * 台账数据
          */
 
-        async _gatherStagesData(completeData, tender, stages) {
+        async _gatherStagesData(completeData, tender, stages, preStage) {
             const resultPos = this.resultPos;
             const helper = this.ctx.helper;
             completeData.id = tender.id;
@@ -366,9 +384,17 @@ module.exports = app => {
                 calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', '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);
@@ -380,7 +406,11 @@ module.exports = app => {
             const pos = new Ledger.pos({
                 id: 'id', ledgerId: 'lid',
                 calc: function (node) {
+                    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);
                 },
             });
             const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tender.id} });
@@ -393,6 +423,18 @@ module.exports = app => {
                 posIndexData[indexPre + p.id] = p;
             }
 
+            if (preStage) {
+                await this.ctx.service.stage.doCheckStage(preStage);
+                const endStage = await this.ctx.service.stageBillsFinal.getFinalData(tender, preStage.order);
+                sumAssignRelaData(billsIndexData, [
+                    {data: endStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'},
+                ]);
+                const endStagePos = await this.ctx.service.stagePosFinal.getFinalData(tender, preStage.order);
+                sumAssignRelaData(posIndexData, [
+                    {data: endStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'},
+                ]);
+            }
+
             for (const stage of stages) {
                 await this.ctx.service.stage.doCheckStage(stage);
                 const curStage = stage.readOnly
@@ -421,21 +463,21 @@ module.exports = app => {
                 const posRange = pos.getLedgerPos(sourceNode.id);
                 if (!posRange || posRange.length === 0) return;
                 resultPos.loadGatherPos(gatherNode.id, posRange, (gatherPos, sourcePos) => {
-                    gatherUtils.gatherZonePos(tender, gatherPos, sourcePos, completeData.prefix, helper);
+                    gatherUtils.gatherStagePos(tender, gatherPos, sourcePos, completeData.prefix, helper);
                 });
             });
         }
         async _gatherZoneData(tender, completeData, zone) {
-            const stages = await this._getTimeZoneStages(tender, zone);
-            await this._gatherStagesData(completeData, tender, stages);
+            const [stages, preStage, endStage] = await this._getTimeZoneStages(tender, zone);
+            await this._gatherStagesData(completeData, tender, stages, preStage);
         }
         async _gatherIndexZoneData(tender, completeData, stageZone) {
-            const stages = await this._getOrderZoneStages(tender, stageZone);
-            await this._gatherStagesData(completeData, tender, stages);
+            const [stages, preStage, endStage] = await this._getOrderZoneStages(tender, stageZone);
+            await this._gatherStagesData(completeData, tender, stages, preStage);
         }
         async _gatherCheckedZoneData(tender, completeData, zone) {
-            const stages = await this._getCheckedZoneStages(tender, zone);
-            await this._gatherStagesData(completeData, tender, stages);
+            const [stages, preStage, endStage] = await this._getCheckedZoneStages(tender, zone);
+            await this._gatherStagesData(completeData, tender, stages, preStage);
         }
 
         async _gatherStageData(completeData, tender, stage, hasPre) {