|  | @@ -90,50 +90,59 @@ module.exports = app => {
 | 
	
		
			
				|  |  |              return await this.db.query(sql, [sid, importLid]);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        async getChangeBillsWithUsedInfo(stage) {
 | 
	
		
			
				|  |  | +            const sql = 'SELECT sic.* ' +
 | 
	
		
			
				|  |  | +                '  FROM ' + this.tableName + ' sic ' +
 | 
	
		
			
				|  |  | +                '  LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON sic.sid = s.id' +
 | 
	
		
			
				|  |  | +                '  WHERE sic.tid = ? And s.order <= ?';
 | 
	
		
			
				|  |  | +            const result = await this.db.query(sql, [stage.tid, stage.order]);
 | 
	
		
			
				|  |  | +            return result;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          async getChangeWithUsedInfo(stage, importLid) {
 | 
	
		
			
				|  |  |              const change = importLid
 | 
	
		
			
				|  |  |                  ? await this.getLeafXmjImportChange(stage.id, importLid)
 | 
	
		
			
				|  |  |                  : await this.getImportChange(stage.id);
 | 
	
		
			
				|  |  |              if (change.length === 0) return [];
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            // 引入变更,不计算已执行等????
 | 
	
		
			
				|  |  | -            // const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({
 | 
	
		
			
				|  |  | -            //     where: { cid: change.map(x => { return x.cid; }) }
 | 
	
		
			
				|  |  | -            // });
 | 
	
		
			
				|  |  | -            // const changeBillsIndex = {}, changeBillsPart = {};
 | 
	
		
			
				|  |  | -            // for (const cb of changeBills) {
 | 
	
		
			
				|  |  | -            //     changeBillsIndex[cb.id] = cb;
 | 
	
		
			
				|  |  | -            //     if (!changeBillsPart[cb.cid]) changeBillsPart[cb.cid] = [];
 | 
	
		
			
				|  |  | -            //     changeBillsPart[cb.cid].push(cb);
 | 
	
		
			
				|  |  | -            // }
 | 
	
		
			
				|  |  | -            // const stageChangeBills = await this.getEndStageImportChange(stage);
 | 
	
		
			
				|  |  | -            // for (const scb of stageChangeBills) {
 | 
	
		
			
				|  |  | -            //     if (!scb.rela_qty) continue;
 | 
	
		
			
				|  |  | -            //
 | 
	
		
			
				|  |  | -            //     const cb = changeBillsIndex[scb.rela_cbid];
 | 
	
		
			
				|  |  | -            //     if (cb) cb.used_qty = this.ctx.helper.add(cb.used_qty, scb.rela_qty);
 | 
	
		
			
				|  |  | -            // }
 | 
	
		
			
				|  |  | -            // for (const cid in changeBillsPart) {
 | 
	
		
			
				|  |  | -            //     const c = change.find(x => { return x.cid === cid });
 | 
	
		
			
				|  |  | -            //     if (!c) continue;
 | 
	
		
			
				|  |  | -            //
 | 
	
		
			
				|  |  | -            //     for (const cb of changeBillsPart[cid]) {
 | 
	
		
			
				|  |  | -            //         cb.tp = this.ctx.helper.mul(cb.spamount, cb.unit_price, c.tp_decimal || this.ctx.tender.info.decimal.tp);
 | 
	
		
			
				|  |  | -            //         cb.used_tp = this.ctx.helper.mul(cb.used_qty, cb.unit_price, this.ctx.tender.info.decimal.tp);
 | 
	
		
			
				|  |  | -            //
 | 
	
		
			
				|  |  | -            //         c.used_tp = this.ctx.helper.add(c.used_tp, cb.used_tp);
 | 
	
		
			
				|  |  | -            //         if (cb.spamount > 0) {
 | 
	
		
			
				|  |  | -            //             c.p_tp = this.ctx.helper.add(c.p_tp, cb.tp);
 | 
	
		
			
				|  |  | -            //             c.p_used_tp = this.ctx.helper.add(c.p_used_tp, cb.used_tp);
 | 
	
		
			
				|  |  | -            //         } else if (cb.spamount < 0){
 | 
	
		
			
				|  |  | -            //             c.n_tp = this.ctx.helper.add(c.n_tp, cb.tp);
 | 
	
		
			
				|  |  | -            //             c.n_used_tp = this.ctx.helper.add(c.n_used_tp, cb.used_tp);
 | 
	
		
			
				|  |  | -            //         }
 | 
	
		
			
				|  |  | -            //     }
 | 
	
		
			
				|  |  | -            //     c.used_pt = c.total_price ? this.ctx.helper.mul(this.ctx.helper.div(c.used_tp, c.total_price, 4), 100) : 0;
 | 
	
		
			
				|  |  | -            //     c.p_used_pt = c.p_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.p_used_tp, c.p_tp, 4), 100) : 0;
 | 
	
		
			
				|  |  | -            //     c.n_used_pt = c.n_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.n_used_tp, c.n_tp, 4), 100) : 0;
 | 
	
		
			
				|  |  | -            // }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                where: { cid: change.map(x => { return x.cid; }) }
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            const changeBillsIndex = {}, changeBillsPart = {};
 | 
	
		
			
				|  |  | +            for (const cb of changeBills) {
 | 
	
		
			
				|  |  | +                changeBillsIndex[cb.id] = cb;
 | 
	
		
			
				|  |  | +                if (!changeBillsPart[cb.cid]) changeBillsPart[cb.cid] = [];
 | 
	
		
			
				|  |  | +                changeBillsPart[cb.cid].push(cb);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            const stageChangeBills = await this.getChangeBillsWithUsedInfo(stage);
 | 
	
		
			
				|  |  | +            for (const scb of stageChangeBills) {
 | 
	
		
			
				|  |  | +                if (!scb.rela_qty) continue;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                const cb = changeBillsIndex[scb.rela_cbid];
 | 
	
		
			
				|  |  | +                if (cb) cb.used_qty = this.ctx.helper.add(cb.used_qty, scb.rela_qty);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            for (const cid in changeBillsPart) {
 | 
	
		
			
				|  |  | +                const c = change.find(x => { return x.cid === cid });
 | 
	
		
			
				|  |  | +                if (!c) continue;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                for (const cb of changeBillsPart[cid]) {
 | 
	
		
			
				|  |  | +                    cb.tp = this.ctx.helper.mul(cb.spamount, cb.unit_price, c.tp_decimal || this.ctx.tender.info.decimal.tp);
 | 
	
		
			
				|  |  | +                    cb.used_tp = this.ctx.helper.mul(cb.used_qty, cb.unit_price, this.ctx.tender.info.decimal.tp);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    c.used_tp = this.ctx.helper.add(c.used_tp, cb.used_tp);
 | 
	
		
			
				|  |  | +                    if (cb.spamount > 0) {
 | 
	
		
			
				|  |  | +                        c.p_tp = this.ctx.helper.add(c.p_tp, cb.tp);
 | 
	
		
			
				|  |  | +                        c.p_used_tp = this.ctx.helper.add(c.p_used_tp, cb.used_tp);
 | 
	
		
			
				|  |  | +                    } else if (cb.spamount < 0){
 | 
	
		
			
				|  |  | +                        c.n_tp = this.ctx.helper.add(c.n_tp, cb.tp);
 | 
	
		
			
				|  |  | +                        c.n_used_tp = this.ctx.helper.add(c.n_used_tp, cb.used_tp);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                c.used_pt = c.total_price ? this.ctx.helper.mul(this.ctx.helper.div(c.used_tp, c.total_price, 4), 100) : 0;
 | 
	
		
			
				|  |  | +                c.p_used_pt = c.p_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.p_used_tp, c.p_tp, 4), 100) : 0;
 | 
	
		
			
				|  |  | +                c.n_used_pt = c.n_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.n_used_tp, c.n_tp, 4), 100) : 0;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |              return change;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 |