|  | @@ -131,17 +131,13 @@ module.exports = app => {
 | 
	
		
			
				|  |  |           */
 | 
	
		
			
				|  |  |          async billsChange(bills, changes) {
 | 
	
		
			
				|  |  |              const self = this;
 | 
	
		
			
				|  |  | -            function getNewChange(cid, cbid, times, order, qty) {
 | 
	
		
			
				|  |  | +            function getNewChange(cid, cbid, times, order, qty, minus) {
 | 
	
		
			
				|  |  |                  return {
 | 
	
		
			
				|  |  | -                    tid: self.ctx.tender.id,
 | 
	
		
			
				|  |  | -                    sid: self.ctx.stage.id,
 | 
	
		
			
				|  |  | -                    lid: bills.id,
 | 
	
		
			
				|  |  | -                    pid: -1,
 | 
	
		
			
				|  |  | -                    cid,
 | 
	
		
			
				|  |  | -                    cbid,
 | 
	
		
			
				|  |  | -                    stimes: times,
 | 
	
		
			
				|  |  | -                    sorder: order,
 | 
	
		
			
				|  |  | -                    qty,
 | 
	
		
			
				|  |  | +                    tid: self.ctx.tender.id, sid: self.ctx.stage.id,
 | 
	
		
			
				|  |  | +                    lid: bills.id, pid: -1,
 | 
	
		
			
				|  |  | +                    cid, cbid,
 | 
	
		
			
				|  |  | +                    stimes: times, sorder: order,
 | 
	
		
			
				|  |  | +                    qty, minus
 | 
	
		
			
				|  |  |                  };
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              const ledgerBills = await this.ctx.service.ledger.getDataById(bills.id);
 | 
	
	
		
			
				|  | @@ -159,8 +155,10 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  const nc = this._.find(changes, { cid: oc.cid, cbid: oc.cbid });
 | 
	
		
			
				|  |  |                  if (!nc || nc.qty !== oc.qty) {
 | 
	
		
			
				|  |  |                      const qty = nc ? this.round(nc.qty, precision.value) : null;
 | 
	
		
			
				|  |  | -                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
 | 
	
		
			
				|  |  | -                    billsQty = this.ctx.helper.add(billsQty, change.qty);
 | 
	
		
			
				|  |  | +                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty, nc.minus);
 | 
	
		
			
				|  |  | +                    if (!change.minus) {
 | 
	
		
			
				|  |  | +                        billsQty = this.ctx.helper.add(billsQty, change.qty);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                      if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
 | 
	
		
			
				|  |  |                          change.id = oc.id;
 | 
	
		
			
				|  |  |                          updateChanges.push(change);
 | 
	
	
		
			
				|  | @@ -168,14 +166,18 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                          newChanges.push(change);
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                  } else {
 | 
	
		
			
				|  |  | -                    billsQty = this.ctx.helper.add(billsQty, oc.qty);
 | 
	
		
			
				|  |  | +                    if (!oc.minus) {
 | 
	
		
			
				|  |  | +                        billsQty = this.ctx.helper.add(billsQty, oc.qty);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              for (const c of changes) {
 | 
	
		
			
				|  |  |                  const nc = this._.find(oldChanges, { cid: c.cid, cbid: c.cbid });
 | 
	
		
			
				|  |  |                  if (!nc) {
 | 
	
		
			
				|  |  | -                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
 | 
	
		
			
				|  |  | -                    billsQty = this.ctx.helper.add(billsQty, change.qty);
 | 
	
		
			
				|  |  | +                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value), c.minus);
 | 
	
		
			
				|  |  | +                    if (!change.minus) {
 | 
	
		
			
				|  |  | +                        billsQty = this.ctx.helper.add(billsQty, change.qty);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                      newChanges.push(change);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
	
		
			
				|  | @@ -208,17 +210,13 @@ module.exports = app => {
 | 
	
		
			
				|  |  |           */
 | 
	
		
			
				|  |  |          async posChange(pos, changes) {
 | 
	
		
			
				|  |  |              const self = this;
 | 
	
		
			
				|  |  | -            function getNewChange(cid, cbid, times, order, qty) {
 | 
	
		
			
				|  |  | +            function getNewChange(cid, cbid, times, order, qty, minus) {
 | 
	
		
			
				|  |  |                  return {
 | 
	
		
			
				|  |  | -                    tid: self.ctx.tender.id,
 | 
	
		
			
				|  |  | -                    sid: self.ctx.stage.id,
 | 
	
		
			
				|  |  | -                    lid: pos.lid,
 | 
	
		
			
				|  |  | -                    pid: pos.id,
 | 
	
		
			
				|  |  | -                    cid,
 | 
	
		
			
				|  |  | -                    cbid,
 | 
	
		
			
				|  |  | -                    stimes: times,
 | 
	
		
			
				|  |  | -                    sorder: order,
 | 
	
		
			
				|  |  | -                    qty,
 | 
	
		
			
				|  |  | +                    tid: self.ctx.tender.id, sid: self.ctx.stage.id,
 | 
	
		
			
				|  |  | +                    lid: pos.lid, pid: pos.id,
 | 
	
		
			
				|  |  | +                    cid, cbid,
 | 
	
		
			
				|  |  | +                    stimes: times, sorder: order,
 | 
	
		
			
				|  |  | +                    qty, minus
 | 
	
		
			
				|  |  |                  };
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              const ledgerBills = await this.ctx.service.ledger.getDataById(pos.lid);
 | 
	
	
		
			
				|  | @@ -235,23 +233,27 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  const nc = this._.find(changes, { cid: oc.cid, cbid: oc.cbid });
 | 
	
		
			
				|  |  |                  if (!nc || nc.qty !== oc.qty) {
 | 
	
		
			
				|  |  |                      const qty = nc ? this.round(nc.qty, precision.value) : null;
 | 
	
		
			
				|  |  | -                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
 | 
	
		
			
				|  |  | -                    posQty = this.ctx.helper.add(posQty, change.qty);
 | 
	
		
			
				|  |  | +                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty, nc.minus);
 | 
	
		
			
				|  |  | +                    if (!change.minus) {
 | 
	
		
			
				|  |  | +                        posQty = this.ctx.helper.add(posQty, change.qty);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                      if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
 | 
	
		
			
				|  |  |                          change.id = oc.id;
 | 
	
		
			
				|  |  |                          updateChanges.push(change);
 | 
	
		
			
				|  |  | -                    } else {
 | 
	
		
			
				|  |  | -                        newChanges.push(change);
 | 
	
		
			
				|  |  |                      }
 | 
	
		
			
				|  |  |                  } else {
 | 
	
		
			
				|  |  | -                    posQty = this.ctx.helper.add(posQty, oc.qty);
 | 
	
		
			
				|  |  | +                    if (!oc.minus) {
 | 
	
		
			
				|  |  | +                        posQty = this.ctx.helper.add(posQty, oc.qty);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              for (const c of changes) {
 | 
	
		
			
				|  |  |                  const nc = this._.find(oldChanges, { cid: c.cid, cbid: c.cbid });
 | 
	
		
			
				|  |  |                  if (!nc) {
 | 
	
		
			
				|  |  | -                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
 | 
	
		
			
				|  |  | -                    posQty = this.ctx.helper.add(posQty, change.qty);
 | 
	
		
			
				|  |  | +                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value), c.minus);
 | 
	
		
			
				|  |  | +                    if (!change.minus) {
 | 
	
		
			
				|  |  | +                        posQty = this.ctx.helper.add(posQty, change.qty);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                      newChanges.push(change);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
	
		
			
				|  | @@ -401,7 +403,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |              data = helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
 | 
	
		
			
				|  |  |              const bqData = [];
 | 
	
		
			
				|  |  |              for (const d of data) {
 | 
	
		
			
				|  |  | -                if (!d.qty) continue;
 | 
	
		
			
				|  |  | +                if (!d.qty || d.minus) continue;
 | 
	
		
			
				|  |  |                  let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
 | 
	
		
			
				|  |  |                  if (!bd) {
 | 
	
		
			
				|  |  |                      const bills = await this.db.get(this.ctx.service.ledger.departTableName(tender.id), { id: d.lid });
 |