|
@@ -75,54 +75,50 @@ module.exports = app => {
|
|
|
if (!transaction || !tender || !stage) {
|
|
|
throw '数据错误';
|
|
|
}
|
|
|
- if (stage.order > 1) {
|
|
|
- const cur = await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
|
|
|
- const pre = await this.getFinalData(tender, stage.order - 1);
|
|
|
- if ((!cur || cur.length === 0) && (!pre || pre.length === 0)) return;
|
|
|
- for (const c of cur) {
|
|
|
- delete c.id;
|
|
|
- delete c.said;
|
|
|
- delete c.times;
|
|
|
- delete c.order;
|
|
|
- delete c.postil;
|
|
|
- c.sorder = stage.order;
|
|
|
- const p = this.ctx.helper._.find(pre, function (x) {
|
|
|
- return x.lid === c.lid;
|
|
|
- });
|
|
|
- if (p) {
|
|
|
- c.contract_qty = this.ctx.helper.add(c.contract_qty, p.contract_qty);
|
|
|
- c.contract_tp = this.ctx.helper.add(c.contract_tp, p.contract_tp);
|
|
|
- c.qc_qty = this.ctx.helper.add(c.qc_qty, p.qc_qty);
|
|
|
- c.qc_tp = this.ctx.helper.add(c.qc_tp, p.qc_tp);
|
|
|
- c.used = p.used || !this.ctx.helper.checkZero(c.contract_qty) || !this.ctx.helper.checkZero(c.qc_qty)
|
|
|
- || !this.ctx.helper.checkZero(c.contract_tp) || !this.ctx.helper.checkZero(c.qc_tp);
|
|
|
- pre.splice(pre.indexOf(p), 1);
|
|
|
+ const cur = await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
|
|
|
+ const pre = await this.getFinalData(tender, stage.order - 1);
|
|
|
+ const minusChange = await this.ctx.service.stageChange.getStageMinusChange(stage);
|
|
|
+ if ((!cur || cur.length === 0) && (!pre || pre.length === 0)) return;
|
|
|
+ for (const c of cur) {
|
|
|
+ delete c.id;
|
|
|
+ delete c.said;
|
|
|
+ delete c.times;
|
|
|
+ delete c.order;
|
|
|
+ delete c.postil;
|
|
|
+ c.sorder = stage.order;
|
|
|
+ const p = this.ctx.helper._.find(pre, function (x) {
|
|
|
+ return x.lid === c.lid;
|
|
|
+ });
|
|
|
+ if (p) {
|
|
|
+ c.contract_qty = this.ctx.helper.add(c.contract_qty, p.contract_qty);
|
|
|
+ c.contract_tp = this.ctx.helper.add(c.contract_tp, p.contract_tp);
|
|
|
+ c.qc_qty = this.ctx.helper.add(c.qc_qty, p.qc_qty);
|
|
|
+ c.qc_tp = this.ctx.helper.add(c.qc_tp, p.qc_tp);
|
|
|
+ if (p.used || !this.ctx.helper.checkZero(c.contract_qty) || !this.ctx.helper.checkZero(c.qc_qty)
|
|
|
+ || !this.ctx.helper.checkZero(c.contract_tp)) {
|
|
|
+ c.used = true;
|
|
|
} else {
|
|
|
- c.used = !this.ctx.helper.checkZero(c.contract_qty) || !this.ctx.helper.checkZero(c.qc_qty)
|
|
|
- || !this.ctx.helper.checkZero(c.contract_tp) || !this.ctx.helper.checkZero(c.qc_tp);
|
|
|
+ const mc = minusChange.find(x => { return x.lid === c.lid && x.qty < 0 });
|
|
|
+ c.used = !!mc;
|
|
|
+ }
|
|
|
+ pre.splice(pre.indexOf(p), 1);
|
|
|
+ } else {
|
|
|
+ if (!this.ctx.helper.checkZero(c.contract_qty) || !this.ctx.helper.checkZero(c.qc_qty)
|
|
|
+ || !this.ctx.helper.checkZero(c.contract_tp)) {
|
|
|
+ c.used = true;
|
|
|
+ } else {
|
|
|
+ const mc = minusChange.find(x => { return x.lid === c.lid && x.qty < 0 });
|
|
|
+ c.used = !!mc;
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- for (const p of pre) {
|
|
|
- if (p.id !== undefined) delete p.id;
|
|
|
- p.sid = stage.id;
|
|
|
- p.sorder = stage.order;
|
|
|
- }
|
|
|
- await transaction.insert(this.tableName, cur ? cur.concat(pre) : pre);
|
|
|
- } else {
|
|
|
- const sql = 'Insert Into ??(tid, sid, lid, sorder, contract_qty, contract_tp, qc_qty, qc_tp, used)' +
|
|
|
- ' SELECT b.tid, b.sid, b.lid, ? As `sorder`, b.contract_qty, b.contract_tp, b.qc_qty, b.qc_tp,' +
|
|
|
- ' IF(IFNULL(b.contract_qty, 0) <> 0 or IFNULL(b.contract_tp, 0) <> 0 or IFNULL(b.qc_qty, 0) <> 0 or IFNULL(b.qc_tp, 0) <> 0, 1, 0) As used' +
|
|
|
- ' FROM ' + this.ctx.service.stageBills.tableName + ' AS b' +
|
|
|
- ' INNER JOIN(' +
|
|
|
- ' SELECT Max(`times` * ' + timesLen + ' + `order`) As `flow`, `lid` FROM ' + this.ctx.service.stageBills.tableName +
|
|
|
- ' WHERE `sid` = ?' +
|
|
|
- ' GROUP By `lid`) As MF' +
|
|
|
- ' ON (b.times * ' + timesLen + ' + b.order) = MF.flow AND b.lid = MF.lid' +
|
|
|
- ' WHERE b.sid = ?';
|
|
|
- const sqlParam = [this.tableName, stage.order, stage.id, stage.id];
|
|
|
- await transaction.query(sql, sqlParam);
|
|
|
+ for (const p of pre) {
|
|
|
+ if (p.id !== undefined) delete p.id;
|
|
|
+ p.sid = stage.id;
|
|
|
+ p.sorder = stage.order;
|
|
|
}
|
|
|
+ await transaction.insert(this.tableName, cur ? cur.concat(pre) : pre);
|
|
|
}
|
|
|
|
|
|
/**
|