|  | @@ -249,78 +249,81 @@ module.exports = app => {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          async _reCalcStageBills(tenderId, newDecimal, oldDecimal) {
 | 
	
		
			
				|  |  | -            let updateStageBills = [], insertStageBills = [];
 | 
	
		
			
				|  |  | -            const stage = await this.ctx.service.stage.getLastestStage(tenderId, true);
 | 
	
		
			
				|  |  | -            if (!stage || stage.status === auditConst.stage.status.checking ||
 | 
	
		
			
				|  |  | -                stage.status === auditConst.stage.status.checked || newDecimal.tp === oldDecimal.tp)
 | 
	
		
			
				|  |  | -                return [updateStageBills, insertStageBills];
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
 | 
	
		
			
				|  |  | -            const bills = await this.ctx.service.ledger.getAllDataByCondition({
 | 
	
		
			
				|  |  | -                columns: ['id', 'unit_price'],
 | 
	
		
			
				|  |  | -                where: { tender_id: tenderId, is_leaf: true },
 | 
	
		
			
				|  |  | -            });
 | 
	
		
			
				|  |  | -            for (const sb of stageBills) {
 | 
	
		
			
				|  |  | -                const b = bills.find(x => {return x.id === sb.lid});
 | 
	
		
			
				|  |  | -                const contract_tp = this.ctx.helper.mul(b.unit_price, sb.contract_qty, newDecimal.tp);
 | 
	
		
			
				|  |  | -                const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, newDecimal.tp);
 | 
	
		
			
				|  |  | -                if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp) continue;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                //if (sb.)
 | 
	
		
			
				|  |  | -                if (sb.times === stage.times && sb.order === 0) {
 | 
	
		
			
				|  |  | -                    updateStageBills.push({
 | 
	
		
			
				|  |  | -                        id: sb.id, contract_tp, qc_tp
 | 
	
		
			
				|  |  | -                    });
 | 
	
		
			
				|  |  | -                } else {
 | 
	
		
			
				|  |  | -                    insertStageBills.push({
 | 
	
		
			
				|  |  | -                        tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
 | 
	
		
			
				|  |  | -                        times: stage.times, order: 0,
 | 
	
		
			
				|  |  | -                        contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
 | 
	
		
			
				|  |  | -                        qc_qty: sb.qc_qty, qc_tp,
 | 
	
		
			
				|  |  | -                        postil: sb.postil,
 | 
	
		
			
				|  |  | -                    });
 | 
	
		
			
				|  |  | +            const updateStageBills = [], insertStageBills = [];
 | 
	
		
			
				|  |  | +            const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
 | 
	
		
			
				|  |  | +            if (stages.length === 0 || newDecimal.tp === oldDecimal.tp) return [updateStageBills, insertStageBills];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            for (const stage of stages) {
 | 
	
		
			
				|  |  | +                const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
 | 
	
		
			
				|  |  | +                const bills = await this.ctx.service.ledger.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                    columns: ['id', 'unit_price'],
 | 
	
		
			
				|  |  | +                    where: { tender_id: tenderId, is_leaf: true },
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                for (const sb of stageBills) {
 | 
	
		
			
				|  |  | +                    const b = bills.find(x => {return x.id === sb.lid});
 | 
	
		
			
				|  |  | +                    const contract_tp = this.ctx.helper.mul(b.unit_price, sb.contract_qty, newDecimal.tp);
 | 
	
		
			
				|  |  | +                    const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, newDecimal.tp);
 | 
	
		
			
				|  |  | +                    if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp) continue;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    if (sb.times === stage.times && sb.order === 0) {
 | 
	
		
			
				|  |  | +                        updateStageBills.push({
 | 
	
		
			
				|  |  | +                            id: sb.id, contract_tp, qc_tp
 | 
	
		
			
				|  |  | +                        });
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        insertStageBills.push({
 | 
	
		
			
				|  |  | +                            tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
 | 
	
		
			
				|  |  | +                            times: stage.times, order: 0,
 | 
	
		
			
				|  |  | +                            contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
 | 
	
		
			
				|  |  | +                            qc_qty: sb.qc_qty, qc_tp,
 | 
	
		
			
				|  |  | +                            postil: sb.postil,
 | 
	
		
			
				|  |  | +                        });
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              return [updateStageBills, insertStageBills];
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
 | 
	
		
			
				|  |  | -            let changeSj = [], changeSb = [], changeSo = [];
 | 
	
		
			
				|  |  | -            const stage = await this.ctx.service.stage.getLastestStage(tenderId, true);
 | 
	
		
			
				|  |  | -            if (!stage) return [changeSj, changeSb, changeSo];
 | 
	
		
			
				|  |  | +            const changeSj = [], changeSb = [], changeSo = [];
 | 
	
		
			
				|  |  | +            const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
 | 
	
		
			
				|  |  | +            if (stages.length === 0) return [changeSj, changeSb, changeSo];
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
 | 
	
		
			
				|  |  |              const calcUp = upDecimal < oldDecimal.up,
 | 
	
		
			
				|  |  |                  calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            if (calcUp || calcTp) {
 | 
	
		
			
				|  |  | -                const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
 | 
	
		
			
				|  |  | -                    columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
 | 
	
		
			
				|  |  | -                    where: { sid: stage.id }
 | 
	
		
			
				|  |  | -                });
 | 
	
		
			
				|  |  | -                for (const sj of stageJgcl) {
 | 
	
		
			
				|  |  | -                    const cj = { id: sj.id };
 | 
	
		
			
				|  |  | -                    cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
 | 
	
		
			
				|  |  | -                    cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
 | 
	
		
			
				|  |  | -                    cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
 | 
	
		
			
				|  |  | -                    changeSj.push(cj);
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            if (calcTp) {
 | 
	
		
			
				|  |  | -                changeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
 | 
	
		
			
				|  |  | -                    columns: ['id', 'tp'],
 | 
	
		
			
				|  |  | -                    where: { sid: stage.id }
 | 
	
		
			
				|  |  | -                });
 | 
	
		
			
				|  |  | -                for (const cb of changeSb) {
 | 
	
		
			
				|  |  | -                    cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
 | 
	
		
			
				|  |  | +            for (const stage of stages) {
 | 
	
		
			
				|  |  | +                if (calcUp || calcTp) {
 | 
	
		
			
				|  |  | +                    const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                        columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
 | 
	
		
			
				|  |  | +                        where: { sid: stage.id }
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    for (const sj of stageJgcl) {
 | 
	
		
			
				|  |  | +                        const cj = { id: sj.id };
 | 
	
		
			
				|  |  | +                        cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
 | 
	
		
			
				|  |  | +                        cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
 | 
	
		
			
				|  |  | +                        cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
 | 
	
		
			
				|  |  | +                        changeSj.push(cj);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -                changeSo = await this.ctx.service.stageOther.getAllDataByCondition({
 | 
	
		
			
				|  |  | -                    columns: ['id', 'total_price', 'tp'],
 | 
	
		
			
				|  |  | -                    where: { sid: stage.id }
 | 
	
		
			
				|  |  | -                });
 | 
	
		
			
				|  |  | -                for (const co of changeSo) {
 | 
	
		
			
				|  |  | -                    if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
 | 
	
		
			
				|  |  | -                    co.tp = this.ctx.helper.round(co.tp, tpDecimal);
 | 
	
		
			
				|  |  | +                if (calcTp) {
 | 
	
		
			
				|  |  | +                    const stageChangeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                        columns: ['id', 'tp'],
 | 
	
		
			
				|  |  | +                        where: { sid: stage.id }
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    for (const cb of stageChangeSb) {
 | 
	
		
			
				|  |  | +                        cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    changeSb.push(...stageChangeSb);
 | 
	
		
			
				|  |  | +                    const stageChangeSo = await this.ctx.service.stageOther.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                        columns: ['id', 'total_price', 'tp'],
 | 
	
		
			
				|  |  | +                        where: { sid: stage.id }
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    for (const co of stageChangeSo) {
 | 
	
		
			
				|  |  | +                        if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
 | 
	
		
			
				|  |  | +                        co.tp = this.ctx.helper.round(co.tp, tpDecimal);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    changeSo.push(...stageChangeSo);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              return [changeSj, changeSb, changeSo];
 |