Browse Source

汇总计量单元

MaiXinRong 2 years ago
parent
commit
fe0169d7ee
4 changed files with 191 additions and 111 deletions
  1. 1 1
      app/controller/ledger_controller.js
  2. 30 10
      app/lib/ledger.js
  3. 10 5
      app/service/report.js
  4. 150 95
      app/service/rpt_gather_memory.js

+ 1 - 1
app/controller/ledger_controller.js

@@ -233,7 +233,7 @@ module.exports = app => {
          * 复制粘贴整块
          *
          * @param ctx
-         * @return {Promise<void>}
+         * @return {Promise<void>
          */
         async _pasteBlock(ctx, data) {
             if ((isNaN(data.id) || data.id <= 0) ||

+ 30 - 10
app/lib/ledger.js

@@ -448,7 +448,7 @@ class gatherTree extends baseTree {
         return this._newId++;
     }
 
-    loadGatherNode(node, parent, loadFun) {
+    loadGatherNode(node, parent, loadFun, loadPosFun) {
         const siblings = parent ? parent.children : this.children;
         let cur = siblings.find(function (x) {
             return node.b_code
@@ -471,8 +471,12 @@ class gatherTree extends baseTree {
             this.datas.push(cur);
         }
         loadFun(cur, node);
-        for (const c of node.children) {
-            this.loadGatherNode(c, cur, loadFun);
+        if (node.children && node.children.length > 0) {
+            for (const c of node.children) {
+                this.loadGatherNode(c, cur, loadFun, loadPosFun);
+            }
+        } else if (loadPosFun) {
+            loadPosFun(cur, node);
         }
     }
 
@@ -490,11 +494,10 @@ class gatherTree extends baseTree {
         }
     }
 
-    loadGatherTree(sourceTree,  loadFun) {
+    loadGatherTree(sourceTree, loadFun, loadPosFun) {
         for (const c of sourceTree.children) {
-            this.loadGatherNode(c, null, loadFun);
+            this.loadGatherNode(c, null, loadFun, loadPosFun);
         }
-        // todo load Pos Data;
     }
     resortChildrenByCustom(fun) {
         for (const n of this.datas) {
@@ -564,10 +567,6 @@ class pos {
         }
     }
 
-    getPos(id) {
-        return this.items[itemsPre + id];
-    }
-
     getLedgerPos(mid) {
         return this.ledgerPos[itemsPre + mid];
     }
@@ -597,6 +596,26 @@ class pos {
     }
 }
 
+class gatherPos extends pos {
+    loadGatherPos(ledgerId, sourcePosRange, loadFun) {
+        let posRange = this.getledgerPos(itemsPre + ledgerId);
+        if (!posRange) {
+            posRange = [];
+            this.ledgerPos[itemsPre + ledgerId] = posRange;
+        }
+        for (const spr of sourcePosRange) {
+            let gp = posRange.find(x => { return x.name === spr.name; });
+            if (!gp) {
+                gp = { name: spr.name };
+                gp[this.setting.ledgerId] = ledgerId;
+                this.datas.push(gp);
+                posRange.push(gp);
+            }
+            loadFun(gp, spr);
+        }
+    }
+}
+
 class checkData {
     constructor(ctx, measureType) {
         this.ctx = ctx;
@@ -1066,6 +1085,7 @@ module.exports = {
     filterTree,
     filterGatherTree,
     gatherTree,
+    gatherPos,
     checkData,
     reviseTree,
 };

+ 10 - 5
app/service/report.js

@@ -169,11 +169,6 @@ module.exports = app => {
                             runnableRst.push(service.reportMemory.getStageTempLand(params.tender_id, params.stage_id, memFieldKeys[filter]));
                             runnableKey.push(filter);
                             break;
-                        case 'mem_gather_stage_bills':
-                            runnableRst.push(service.rptGatherMemory.getGatherStageBills(memFieldKeys[filter],
-                                customDefine.gather_select, customSelect ? customSelect.gather_select : null));
-                            runnableKey.push(filter);
-                            break;
                         case 'mem_gather_tender_info':
                             runnableRst.push(service.rptGatherMemory.getGatherTenderInfo(memFieldKeys[filter],
                                 customDefine.gather_select, customSelect ? customSelect.gather_select : null));
@@ -374,6 +369,16 @@ module.exports = app => {
                         const jhHelper3 = new rptCustomData.jhHelper(this.ctx);
                         rst[filter] = await jhHelper3.gatherBills(memFieldKeys[filter], customDefine.gather_select, customSelect ? customSelect.gather_select : null);
                         break;
+                    case 'mem_gather_stage_bills':
+                        runnableRst.push(service.rptGatherMemory.getGatherStageBills(memFieldKeys[filter],
+                            customDefine.gather_select, customSelect ? customSelect.gather_select : null));
+                        runnableKey.push(filter);
+                        break;
+                    case 'mem_gather_stage_pos':
+                        runnableRst.push(service.rptGatherMemory.getGatherStageBills(memFieldKeys[filter],
+                            customDefine.gather_select, customSelect ? customSelect.gather_select : null));
+                        runnableKey.push(filter);
+                        break;
                     // case 'mem_material_bills':
                     //     rst[filter] = await service.rptGatherMemory.getMaterialBills(params.tender_id, params.material_order, memFieldKeys[filter]);
                     //     break;

+ 150 - 95
app/service/rpt_gather_memory.js

@@ -231,14 +231,8 @@ module.exports = app => {
          */
         constructor(ctx) {
             super(ctx);
-            this.resultTree = new Ledger.gatherTree(ctx, {
-                id: 'id',
-                pid: 'pid',
-                order: 'order',
-                level: 'level',
-                rootId: -1
-            });
-            this.resultPos = new Ledger.pos({ id: 'id', ledgerId: 'lid' });
+            this.resultTree = null;
+            this.resultPos = null;
             this.resultTenderInfo = [];
             this.resultDealPay = [];
             this.resultDealBills = [];
@@ -313,63 +307,9 @@ module.exports = app => {
         /**
          * 台账数据
          */
-        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', 'contract_pc_tp', 'qc_pc_tp', 'pc_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.sum([node.contract_tp, node.qc_tp, node.pc_tp]);
-                    node.end_contract_tp = helper.sum([node.pre_contract_tp, node.contract_tp, node.contract_pc_tp]);
-                    node.end_qc_tp = helper.sum([node.pre_qc_tp, node.qc_tp, node.qc_pc_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);
-                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 preStage = hasPre && stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];
-                const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
-                this.ctx.helper.assignRelaData(billsData, [
-                    { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
-                    { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
-                    { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', 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 resultPos = this.resultPos;
             const helper = this.ctx.helper;
             completeData.id = tender.id;
             completeData.name = tender.name;
@@ -413,17 +353,21 @@ module.exports = app => {
                 }
             });
             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);
             }
-
+            const pos = new Ledger.pos(this.ctx, { id: 'id', ledgerId: 'lid' });
+            const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tender.id} });
             let billsIndexData = {};
             for (const bd of billsData) {
                 billsIndexData[indexPre + bd.id] = bd;
             }
+            let posIndexData = {};
+            for (const p of posData) {
+                posIndexData[indexPre + p.id] = p;
+            }
 
             for (const stage of stages) {
                 await this.ctx.service.stage.doCheckStage(stage);
@@ -435,49 +379,130 @@ module.exports = app => {
                     {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'},
                     { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
                 ]);
+                const curStagePos = stage.readOnly
+                    ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
+                    : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
+                sumAssignRelaData(posIndexData, [
+                    {data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'},
+                ]);
             }
 
             billsTree.loadDatas(billsData);
             billsTree.calculateAll();
+            pos.LoadDatas(posData);
             this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
                 gatherUtils.gatherZone(tender, gatherNode, sourceNode, completeData.prefix, helper);
+            }, function (gatherNode, sourceNode) {
+                const posRange = pos.getLedgerPos(sourceNode.id);
+                if (!posRange || posRange.length === 0) return;
+                resultPos.loadGatherPos(gatherNode.id, posRange, (gatherPos, sourcePos) => {
+                    gatherUtils.gatherZonePos(gatherPos, sourcePos, completeData.prefix, helper);
+                });
             });
         }
+        async _gatherZoneData(tender, completeData, zone) {
+            const stages = await this._getTimeZoneStages(tender, zone);
+            await this._gatherStagesData(completeData, tender, stages);
+        }
 
+        async _gatherStageData(completeData, tender, stage, hasPre) {
+            const helper = this.ctx.helper;
+            const resultPos = this.resultPos;
+            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', 'contract_pc_tp', 'qc_pc_tp', 'pc_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.sum([node.contract_tp, node.qc_tp, node.pc_tp]);
+                    node.end_contract_tp = helper.sum([node.pre_contract_tp, node.contract_tp, node.contract_pc_tp]);
+                    node.end_qc_tp = helper.sum([node.pre_qc_tp, node.qc_tp, node.qc_pc_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);
+            }
+            const pos = new Ledger.pos(this.ctx, { id: 'id', ledgerId: 'lid' });
+            const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tender.id } });
+            if (stage) {
+                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 preStage = hasPre && stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];
+                const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
+                this.ctx.helper.assignRelaData(billsData, [
+                    { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
+                    { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
+                    { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
+                ]);
+
+                const curStagePos = stage.readOnly
+                    ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
+                    : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
+                const preStagePos = hasPre && stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.order - 1) : [];
+                this.ctx.helper.assignRelaData(posData, [
+                    { data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
+                    { data: preStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
+                ]);
+            }
+            billsTree.loadDatas(billsData);
+            billsTree.calculateAll();
+            pos.loadDatas(posData);
+
+            this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
+                gatherUtils.gatherStage(tender, gatherNode, sourceNode, completeData.prefix, helper);
+            }, function (gatherNode, sourceNode) {
+                const posRange = pos.getLedgerPos(sourceNode.id);
+                if (!posRange || posRange.length === 0) return;
+                resultPos.loadGatherPos(gatherNode.id, posRange, (gatherPos, sourcePos) => {
+                    gatherUtils.gatherStagePos(gatherPos, sourcePos, 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;
+        async _loadGatherLedger(tender) {
             const billsTree = new Ledger.billsTree(this.ctx, {
                 id: 'ledger_id',
                 pid: 'ledger_pid',
@@ -491,36 +516,58 @@ module.exports = app => {
             const billsData = await this.ctx.service.ledger.getData(tender.id);
             billsTree.loadDatas(billsData);
             billsTree.calculateAll();
+            return billsTree;
+        }
+        async _loadGatherPos(tender) {
+            const pos = new Ledger.pos({ id: 'id', ledgerId: 'lid' });
+            const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tender.id } });
+            pos.loadDatas(posData);
+            return pos;
+        }
+        async _gatherLedgerData(tender, completeData) {
+            const resultPos = this.resultPos;
+            const helper = this.ctx.helper;
+            const billsTree = await this._loadGatherLedger(tender);
+            const pos = await this._loadGatherPos(tender);
             this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
                 gatherUtils.gatherLedger(tender, gatherNode, sourceNode, completeData.prefix, helper);
+            }, function (gatherNode, sourceNode) {
+                const posRange = pos.getLedgerPos(sourceNode.id);
+                if (!posRange || posRange.length === 0) return;
+                resultPos.loadGatherPos(posRange, (gatherPos, sourcePos) => {
+                    gatherUtils.gatherPos(gatherPos, sourcePos, completeData.prefix, helper);
+                });
             });
         }
-
         async _gatherSpecialData(tender, sKey) {
+            const resultPos = this.resultPos;
             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();
+            const billsTree = await this._loadGatherLedger(tender);
+            const pos = await this._loadGatherPos(tender);
             this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
                 gatherUtils.gatherSpecial(gatherNode, sourceNode, 'ts_' + sKey + '_', helper);
+            }, function (gatherNode, sourceNode) {
+                const posRange = pos.getLedgerPos(sourceNode.id);
+                if (!posRange || posRange.length === 0) return;
+                resultPos.loadGatherPos(posRange, (gatherPos, sourcePos) => {
+                    gatherUtils.gatherPos(gatherPos, sourcePos, 'ts_' + sKey + '_', helper);
+                });
             });
         }
 
-        async getGatherStageBills(memFieldKeys, gsDefine, gsCustom) {
-            if (!gsDefine || !gsDefine.enable) return [];
-            if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
+        async _doGatherStageData(memFieldKeys, gsDefine, gsCustom) {
+            if (!gsDefine || !gsDefine.enable) return;
+            if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return;
+
+            this.resultTree = new Ledger.gatherTree(this.ctx, {
+                id: 'id',
+                pid: 'pid',
+                order: 'order',
+                level: 'level',
+                rootId: -1
+            });
+            this.resultPos = new Ledger.gatherPos(this.ctx, { id: 'id', ledgerId: 'lid' });
 
-            this.resultTree.clear();
             const gsSetting = JSON.parse(gsDefine.setting);
             let commonIndex = 0;
             const completeDatas = [];
@@ -563,7 +610,15 @@ module.exports = app => {
 
             this.resultTree.resortChildrenDefault();
             gatherUtils.completeGatherData(this.resultTree.nodes, completeDatas);
-            return this.resultTree.getDefaultDatas();
+        }
+
+        async getGatherStageBills(memFieldKeys, gsDefine, gsCustom) {
+            await this._doGatherStageData(memFieldKeys, gsDefine, gsCustom);
+            return this.resultTree ? this.resultTree.getDefaultDatas() : [];
+        }
+        async getGatherStagePos(memFieldKeys, gsDefine, gsCustom) {
+            await this._doGatherStageData(memFieldKeys, gsDefine, gsCustom);
+            return this.resultPos ? this.resultPos.getDatas() : [];
         }
 
         /**