Bläddra i källkod

材差清单汇总,调整

MaiXinRong 2 år sedan
förälder
incheckning
07440b632b
2 ändrade filer med 75 tillägg och 45 borttagningar
  1. 74 44
      app/lib/rm/material.js
  2. 1 1
      app/service/material_list.js

+ 74 - 44
app/lib/rm/material.js

@@ -262,6 +262,59 @@ class ReportMemoryMaterial {
         }
     }
 
+    async _getMaterialStageGatherBills(tender_id, stage_id, stage_order) {
+        const decimal = this.materialGatherBase.decimal;
+        const billsData = this.ctx.helper.clone(this.materialGatherBase.billsData);
+        const curStageBills = await this.ctx.service.stageBills.getStagesData(tender_id, stage_id);
+        this.ctx.helper.assignRelaData(billsData, [
+            { data: curStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
+        ]);
+        const billsTree = this._getNewBillsTree();
+        billsTree.loadDatas(billsData);
+        billsTree.calculateAll();
+
+        const posData = this.ctx.helper.clone(this.materialGatherBase.posData);
+        const curStage = await this.ctx.service.stagePos.getStagesData(tender_id, stage_id);
+        this.ctx.helper.assignRelaData(posData, [
+            { data: curStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
+        ]);
+        const pos = this._getNewPos();
+        pos.loadDatas(posData);
+        pos.calculateAll();
+
+        const gclGatherModel = require('../gcl_gather').gclGather;
+        const gatherUtil = new gclGatherModel(this.ctx);
+        gatherUtil.gatherObj(billsTree, pos);
+        const materialGl = this.materialGatherBase.materialGl;
+        const materialNotJoin = this.materialGatherBase.materialNotJoin;
+
+        const helper = this.ctx.helper;
+        for (const g of gatherUtil.gclList) {
+            g.sid = stage_id;
+            g.sorder = stage_order;
+            g.jiacha = 0;
+            for (const x of g.leafXmjs) {
+                x.sid = stage_id;
+                x.sorder = stage_order;
+                x.jiacha = 0;
+                const mnj = materialNotJoin.find(m => {
+                    return m.gcl_id === x.org_gcl_id && m.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id  ? x.mx_id === m.mx_id : true);
+                });
+                x.is_join = !mnj;
+                if (mnj) continue;
+                const list = materialGl.filter(g => {
+                    return g.gcl_id === x.org_gcl_id && g.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id ? x.mx_id === g.mx_id : true);
+                });
+                for (const l of list) {
+                    x.jiacha = helper.add(x.jiacha, helper.mul(helper.mul(x.gather_qty, l.quantity), l.m_spread));
+                }
+                x.jiacha = helper.round(x.jiacha, decimal.tp);
+                g.jiacha = helper.add(g.jiacha, x.jiacha);
+            }
+        }
+        return [gatherUtil.gclList, gatherUtil.leafXmjs ];
+    }
+
     async getMaterialGatherBills(tender_id, material_order) {
         const materials = await this.ctx.service.material.getAllDataByCondition({
             where: { tid: tender_id },
@@ -270,55 +323,32 @@ class ReportMemoryMaterial {
         if (materials.length === 0) return {};
 
         const material = await this.ctx.service.material.getDataByCondition({ tid: tender_id, order: material_order });
-        const decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;
+        this.materialGatherBase = {};
+        this.materialGatherBase.decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;
         try {
-            const billsData = await this.ctx.service.ledger.getData(tender_id);
-            const curStageBills = await this.ctx.service.stageBills.getStagesData(tender_id, material.stage_id);
-            this.ctx.helper.assignRelaData(billsData, [
-                { data: curStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
-            ]);
-            const billsTree = this._getNewBillsTree();
-            billsTree.loadDatas(billsData);
-            billsTree.calculateAll();
-
-            const posData = await this.ctx.service.pos.getPosData({ tid: tender_id });
-            const curStage = await this.ctx.service.stagePos.getStagesData(tender_id, material.stage_id);
-            this.ctx.helper.assignRelaData(posData, [
-                { data: curStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
-            ]);
-            const pos = this._getNewPos();
-            pos.loadDatas(posData);
-            pos.calculateAll();
-
-            const gclGatherModel = require('../gcl_gather').gclGather;
-            const gatherUtil = new gclGatherModel(this.ctx);
-            gatherUtil.gatherObj(billsTree, pos);
-            const materialGl = material_order === materials[0].order
+            // 获取基础数据
+            this.materialGatherBase.billsData = await this.ctx.service.ledger.getData(tender_id);
+            this.materialGatherBase.posData = await this.ctx.service.pos.getPosData({ tid: tender_id });
+            this.materialGatherBase.materialGl = material_order === materials[0].order
                 ? await this.ctx.service.materialList.getMaterialData(tender_id, material.id)
                 : await this.ctx.service.materialList.getPreMaterialData(tender_id, material.id);
-            const materialNotJoin = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { mid: material.id } });
-
-            const helper = this.ctx.helper;
-            for (const g of gatherUtil.gclList) {
-                g.jiacha = 0;
-                for (const x of g.leafXmjs) {
-                    x.jiacha = 0;
-                    const mnj = materialNotJoin.find(m => {
-                        return m.gcl_id === x.org_gcl_id && m.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id  ? x.mx_id === m.mx_id : true);
-                    });
-                    x.is_join = !mnj;
-                    if (mnj) continue;
-                    const list = materialGl.filter(g => {
-                        return g.gcl_id === x.org_gcl_id && g.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id ? x.mx_id === g.mx_id : true);
-                    });
-                    for (const l of list) {
-                        x.jiacha = helper.add(x.jiacha, helper.mul(helper.mul(x.gather_qty, l.quantity), l.m_spread));
-                    }
-                    x.jiacha = helper.round(x.jiacha, decimal.tp);
-                    g.jiacha = helper.add(g.jiacha, x.jiacha);
+            this.materialGatherBase.materialNotJoin = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { mid: material.id } });
+
+            const mem_material_gather_bills = [], mem_material_gather_xmj = [];
+            if (material.is_stage_self) {
+                const stageIds = material.stage_id.split(',');
+                const stageOrders = material.s_order.split(',');
+                for (const [i, sid] of stageIds.entries()) {
+                    const [gclList, leafXmjs] = await this._getMaterialStageGatherBills(tender_id, sid, stageOrders[i]);
+                    mem_material_gather_bills.push(...gclList);
+                    mem_material_gather_xmj.push(...leafXmjs);
                 }
+            } else {
+                const [gclList, leafXmjs] = await this._getMaterialStageGatherBills(tender_id, material.stage_id, material.stage_order);
+                mem_material_gather_bills.push(...gclList);
+                mem_material_gather_xmj.push(...leafXmjs);
             }
-            return { mem_material_gather_bills: gatherUtil.gclList, mem_material_gather_xmj: gatherUtil.leafXmjs, mem_material_gather_gl: materialGl };
+            return {mem_material_gather_bills, mem_material_gather_xmj, mem_material_gather_gl: this.materialGatherBase.materialGl};
         } catch (err) {
             this.ctx.log(err);
             return {};

+ 1 - 1
app/service/material_list.js

@@ -310,7 +310,7 @@ module.exports = app => {
         }
 
         async getPreMaterialData(tid, mid) {
-            const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`ms_id` ml.`tid`, ml.`mid`, mbh.m_spread' +
+            const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`ms_id`, ml.`tid`, ml.`mid`, mbh.m_spread' +
                 ' FROM ' + this.tableName + ' as ml' +
                 ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
                 ' LEFT JOIN ' + this.ctx.service.materialBillsHistory.tableName + ' as mbh ON ml.`mb_id` = mbh.`mb_id` and mbh.mid = ?' +