|  | @@ -510,6 +510,82 @@ class jhHelper {
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * 奉建项目 定制报表
 | 
	
		
			
				|  |  | + * 变更过程管理表
 | 
	
		
			
				|  |  | + * 汇总工程量清单(区分正负变更)
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +class fjHelper {
 | 
	
		
			
				|  |  | +    constructor (ctx) {
 | 
	
		
			
				|  |  | +        this.ctx = ctx;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    async _getStageImportData(tid, sid) {
 | 
	
		
			
				|  |  | +        const sql = 'SELECT sic.*, l.b_code, l.name. l.unit, l.unit_price, l.quantity, l.total_price' +
 | 
	
		
			
				|  |  | +            '    oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
 | 
	
		
			
				|  |  | +            '  FROM ' + this.ctx.service.stageImportChange.tableName + ' sic ' +
 | 
	
		
			
				|  |  | +            '  LEFT JOIN (SELECT * FROM ' + this.ctx.service.ledger.tableName + ' WHERE tender_id = ? AND is_leaf) AS l ON sic.lid = l.id' +
 | 
	
		
			
				|  |  | +            '  WHERE sic.tid = ? AND sic.sid < ?';
 | 
	
		
			
				|  |  | +        return await this.ctx.app.mysql.query(sql, [tid, tid, sid]);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    convertChangeProgress(data, sid) {
 | 
	
		
			
				|  |  | +        const helper = this.ctx.helper, tpDecimal = this.ctx.tender.info.decimal.tp;
 | 
	
		
			
				|  |  | +        const bills = [];
 | 
	
		
			
				|  |  | +        for (const d of data) {
 | 
	
		
			
				|  |  | +            let b = bills.find(x => {
 | 
	
		
			
				|  |  | +                return x.b_code === d.b_code && x.name === d.name && x.unit === d.unit && x.unit_price === d.unit_price;
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            if (!b) {
 | 
	
		
			
				|  |  | +                b = { b_code: d.b_code, name: d.name, unit: d.unit, unit_price: d.unit_price, ledgerSource: [] };
 | 
	
		
			
				|  |  | +                bills.push(b);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            const ls = b.ledgerSource.find(x => { return x.lid === d.lid; });
 | 
	
		
			
				|  |  | +            if (!ls) b.ledgerSource.push({lid: d.lid, quantity: d.quantity, total_price: d.total_price });
 | 
	
		
			
				|  |  | +            const field = (d.sid < sid ? 'pre_' : '') + (d.qty > 0 ? 'positive_' : 'negative_') + 'qc_qty';
 | 
	
		
			
				|  |  | +            b[field] = this.ctx.helper.add(b[field], d.qty);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        bills.sort((a, b) => { return helper.compareCode(a.b_code, b.b_code); });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        const result = [];
 | 
	
		
			
				|  |  | +        for (const b of bills) {
 | 
	
		
			
				|  |  | +            b.pre_positive_qc_tp = helper.mul(b.unit_price, b.pre_positive_qc_qty, tpDecimal);
 | 
	
		
			
				|  |  | +            b.pre_negative_qc_tp = helper.mul(b.unit_price, b.pre_negative_qc_qty, tpDecimal);
 | 
	
		
			
				|  |  | +            b.positive_qc_tp = helper.mul(b.unit_price, b.positive_qc_qty, tpDecimal);
 | 
	
		
			
				|  |  | +            b.negative_qc_tp = helper.mul(b.unit_price, b.negative_qc_qty, tpDecimal);
 | 
	
		
			
				|  |  | +            b.end_positive_qc_qty = helper.add(b.pre_positive_qc_qty, b.positive_qc_qty);
 | 
	
		
			
				|  |  | +            b.end_positive_qc_tp = helper.add(b.pre_positive_qc_tp, b.positive_qc_tp);
 | 
	
		
			
				|  |  | +            b.end_negative_qc_qty = helper.add(b.pre_negative_qc_qty, b.negative_qc_qty);
 | 
	
		
			
				|  |  | +            b.end_negative_qc_tp = helper.add(b.pre_negative_qc_tp, b.negative_qc_tp);
 | 
	
		
			
				|  |  | +            b.final_qty = helper.add(b.quantity, helper.add(b.end_positive_qc_qty, b.end_negative_qc_qty));
 | 
	
		
			
				|  |  | +            b.final_qty = helper.add(b.total_price, helper.add(b.end_positive_qc_tp, b.end_negative_qc_tp));
 | 
	
		
			
				|  |  | +            result.push({
 | 
	
		
			
				|  |  | +                b_code: b.b_code, name: b.name, unit: b.unit, unit_price: b.unit_price,
 | 
	
		
			
				|  |  | +                quantity: b.quantity, total_price: b.total_price,
 | 
	
		
			
				|  |  | +                pre_qc_qty: b.pre_positive_qc_qty, pre_qc_tp: b.pre_positive_qc_tp,
 | 
	
		
			
				|  |  | +                qc_qty: b.positive_qc_qty, qc_tp: b.positive_qc_tp,
 | 
	
		
			
				|  |  | +                end_qc_qty: b.end_positive_qc_qty, end_qc_tp: b.end_positive_qc_tp,
 | 
	
		
			
				|  |  | +                final_qty: b.final_qty, final_tp: b.final_tp,
 | 
	
		
			
				|  |  | +                minus: 0,
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            result.push({
 | 
	
		
			
				|  |  | +                b_code: b.b_code, name: b.name, unit: b.unit, unit_price: b.unit_price,
 | 
	
		
			
				|  |  | +                quantity: b.quantity, total_price: b.total_price,
 | 
	
		
			
				|  |  | +                pre_qc_qty: b.pre_negative_qc_tp, pre_qc_tp: b.pre_negative_qc_tp,
 | 
	
		
			
				|  |  | +                qc_qty: b.negative_qc_qty, qc_tp: b.negative_qc_tp,
 | 
	
		
			
				|  |  | +                end_qc_qty: b.end_negative_qc_qty, end_qc_tp: b.end_negative_qc_tp,
 | 
	
		
			
				|  |  | +                final_qty: b.final_qty, final_tp: b.final_tp,
 | 
	
		
			
				|  |  | +                minus: 1,
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return result;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    async getChangeProgressData(tid, sid) {
 | 
	
		
			
				|  |  | +        const data = await this._getStageImportData(tid, sid);
 | 
	
		
			
				|  |  | +        return this.convertChangeProgress(data, sid);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  module.exports = {
 | 
	
		
			
				|  |  |      jhHelper,
 | 
	
		
			
				|  |  | +    fjHelper,
 | 
	
		
			
				|  |  |  };
 |