| 
					
				 | 
			
			
				@@ -24,14 +24,30 @@ const stageImVersion = '1.0'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const Ledger = require('../lib/ledger'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-const curFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-const preFields = ['pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-const endFields = ['end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-const finalFields = ['final_tp', 'final_ratio']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-const stageFields = curFields.concat(preFields, endFields, finalFields); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-const stageEndFields = preFields.concat(endFields, finalFields); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-const bglFields = ['qc_bgl_code']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const billsFields = (function () { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const cur = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const pre = ['pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const end = ['end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const final = ['final_tp', 'final_ratio']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const stage = cur.concat(pre, end, final); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const stageEnd = pre.concat(end, final); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const bgl = ['qc_bgl_code']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return {cur, pre, end, final, stage, stageEnd, bgl}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+})(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const posFields = (function () { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const cur = ['contract_qty', 'qc_qty', 'gather_qty', 'postil']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const pre = ['pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const end = ['end_contract_qty', 'end_qc_qty', 'end_gather_qty']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const final = ['final_ratio']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const stage = cur.concat(pre, end, final); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const stageEnd = pre.concat(end, final); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const bgl = ['qc_bgl_code']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return {cur, pre, end, final, stage, stageEnd, bgl}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+})(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     class ReportMemory extends app.BaseService { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -89,6 +105,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             // 需要缓存的数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             this.stageImData = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             this.changeData = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.stageValidRole = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         _checkFieldsExist(source, check) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -175,30 +192,6 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        async getStageImTzNoReturn(tid, sid) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // 备注:单独拎出以下几行代码一个是为了提高效率(跟getStageImTzDataDirectlyByKey方法协作使用) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            //      二是如果出现并行查询(台账及台账清单)情况下,会出现干扰(已验证过),导致数据丢失 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!this.stageImData) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                this.stageImData = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                await this._generateStageIm(tid, sid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } catch (err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                this.stageImData.main = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                this.stageImData.bills = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        getStageImTzDataDirectlyByKey(key) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let rst = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (key === 'mem_stage_im_tz') { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                rst = this.stageImData.main; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                rst = this.stageImData.bills; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return rst; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         async getStageImTzData(tid, sid, fields) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this.ctx.service.tender.checkTender(tid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this.ctx.service.stage.checkStage(sid); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -342,7 +335,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (this._checkFieldsExist(fields, stageFields)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this._checkFieldsExist(fields, billsFields.stage)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 if (this.ctx.stage.readOnly) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     const curStage = await this.ctx.service.stageBills.getAuditorStageData(this.ctx.tender.id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -356,7 +349,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (this._checkFieldsExist(fields, preFields.concat(endFields, finalFields))) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this._checkFieldsExist(fields, billsFields.stageEnd)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 this.ctx.helper.assignRelaData(billsData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     {data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'} 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -365,7 +358,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             this.billsTree.loadDatas(billsData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             this.billsTree.calculateAll(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (this._checkFieldsExist(fields, bglFields)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this._checkFieldsExist(fields, billsFields.bgl)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 await this._calcBillsBgl(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -385,35 +378,67 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        async _calcPosBgl() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (!this.ctx.stage) return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const helper = this.ctx.helper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const tender = this.ctx.tender; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const stage = this.ctx.stage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const bglData = this.ctx.stage.readOnly 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                ? await this.ctx.service.stageChange.getAuditorAllStageData(tender.id, stage.id, stage.curTimes, stage.curOrder) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                : await this.ctx.service.stageChange.getLastestAllStageData(tender.id, stage.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (const p of this.pos.datas) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                p.qc_bgl_code = ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const pBgl = helper._.filter(bglData, {lid: p.lid, pid: p.id}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (pBgl.length === 0) continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                helper._.pullAll(bglData, pBgl); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const validBgl = helper._.filter(pBgl, function (x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return !helper.checkZero(x.qty); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                p.qc_bgl_code = helper._.uniq(helper._.map(validBgl, 'c_code')).join(';'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         async getStagePosData(tid, sid, fields) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this.ctx.service.tender.checkTender(tid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this.ctx.service.stage.checkStage(sid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (this.ctx.stage.readOnly) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                const curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                this.ctx.helper.assignRelaData(posData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                const curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this._checkFieldsExist(fields, posFields.stage)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (this.ctx.stage.readOnly) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    this.ctx.helper.assignRelaData(posData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    this.ctx.helper.assignRelaData(posData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this._checkFieldsExist(fields, posFields.stageEnd)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 this.ctx.helper.assignRelaData(posData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    {data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    {data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid'} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            this.ctx.helper.assignRelaData(posData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                {data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid'} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             this.pos.loadDatas(posData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             this.pos.calculateAll(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this._checkFieldsExist(fields, posFields.bgl)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                await this._calcPosBgl(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return this.pos.getDatas(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         _getStageValidRole () { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             if (!this.ctx.stage) throw '期数据错误,请重试'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this.stageValidRole && this.stageValidRole.length > 0) return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const result = [{dataOrder: 0, flowOrder: 0, uid: this.ctx.stage.user_id}]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             for (const auditor of this.ctx.stage.auditors) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -433,15 +458,16 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.stageValidRole = result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         async getStageBillsCompareData(tid, sid, fields) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this.ctx.service.tender.checkTender(tid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this.ctx.service.stage.checkStage(sid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            await this._getStageValidRole(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const stage = this.ctx.stage, helper = this.ctx.helper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const validRole = this._getStageValidRole(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const validRole = this.stageValidRole; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const allStageBills = await this.ctx.service.stageBills.getAllDataByCondition({where: {sid: sid}}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -469,7 +495,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (this._checkFieldsExist(fields, preFields.concat(endFields, finalFields))) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this._checkFieldsExist(fields, billsFields.stageEnd)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 this.ctx.helper.assignRelaData(billsData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     {data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid'} 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -521,6 +547,56 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             // ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        async getStagePosCompareData(tid, sid, fields) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            await this.ctx.service.tender.checkTender(tid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            await this.ctx.service.stage.checkStage(sid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            await this._getStageValidRole(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const stage = this.ctx.stage, helper = this.ctx.helper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const validRole = this.stageValidRole; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const allStagePos = await this.ctx.service.stagePos.getAllDataByCondition({where: {sid: sid}}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.ctx.helper.assignRelaData(posData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                {data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid'} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const stagePosIndex = {}, timesLen = 100; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (const role of validRole) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const stagePos = this.ctx.helper._.filter(allStagePos, function (x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return x.times < stage.curTimes || (x.times === stage.curTimes && x.order <= role.dataOrder); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                this.ctx.helper._.pullAll(allStagePos, stagePos); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                for (const sp of stagePos) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const key = 'sp-' + sp.pid; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const spi = stagePosIndex[key]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if (spi) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if ((spi.times * timesLen + spi.order) < (sp.times * timesLen + sp.order)) stagePosIndex[key] = sp; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        stagePosIndex[key] = sp; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const filterStagePos = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                for (const prop in stagePosIndex) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    filterStagePos.push(stagePosIndex[prop]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                this.ctx.helper.assignRelaData(posData, [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    {data: filterStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'r' + role.flowOrder + '_', relaId: 'pid'} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                ]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.pos.loadDatas(posData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.pos.calculateAll(function (p) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                p.pre_gather_qty = helper.add(p.pre_contract_qty, p.pre_qc_qty); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                for (const role of validRole) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const prefix = 'r' + role.flowOrder + '_'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    p[prefix + 'gather_qty'] = helper.add(p[prefix + 'contract_qty'], p[prefix + 'qc_qty']); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return this.pos.getDatas(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         async getStagePayData(tid, sid, fields) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this.ctx.service.tender.checkTender(tid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             await this.ctx.service.stage.checkStage(sid); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -534,6 +610,13 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const payCalculator = new PayCalculator(this.ctx, this.ctx.stage, this.ctx.tender.info); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 await payCalculator.calculateAll(dealPay); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this._checkFieldsExist(fields, ['start_stage_order']) && this.ctx.stage.status !== audit.stage.status.checked) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                for (const dp of dealPay) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if (!dp.start_stage_order || dp.start_stage_order === this.ctx.stage.order) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        dp.start_stage_order = this.ctx.helper.checkZero(dp.tp) ? null : this.ctx.stage.order; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             if (this._checkFieldsExistReg(fields, 'r[0-9]+_tp')) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const validRole = this._getStageValidRole(); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -578,7 +661,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const self = this; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const decimal = this.ctx.tender.info.decimal, ctx = this.ctx; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                const change = await this.ctx.service.change.getListByStatus(tid, 3); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const change = await this.ctx.service.change.getAllCheckedChanges(tid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 for (const c of change) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     const types = ctx.helper._.map(c.type.split(','), function (t) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         return self._getChangeConstName(changeConst.type, ctx.helper._.toInteger(t)); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -595,7 +678,6 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const changeBills = await this.ctx.service.changeAuditList.getChangeAuditBills(tid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 for (const d of changeBills) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    //const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, d.unit); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     d.o_qty = d.oamount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     d.o_tp = this.ctx.helper.mul(d.o_qty, d.unit_price, decimal.tp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     d.c_qty = d.camount; 
			 |