|
@@ -282,34 +282,48 @@ module.exports = app => {
|
|
|
if (!(data instanceof Array)) throw '提交数据错误';
|
|
|
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
- const result = { ledger: {}, pos: null }, updateLid = [];
|
|
|
+ const result = { ledger: {}, pos: null };
|
|
|
const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
|
|
|
- let bills = null, precision = null;
|
|
|
+
|
|
|
+ const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid);
|
|
|
+ const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
|
|
|
+ const updateBills = {id: bills.id};
|
|
|
+ const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
|
|
|
+ for (const bp of billsPos) {
|
|
|
+ const d = data.find(function (x) {
|
|
|
+ return bp.id ? x.id === bp.id : false;
|
|
|
+ });
|
|
|
+ if (d) continue;
|
|
|
+ updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
|
|
|
+ updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
|
|
|
+ updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
|
|
|
+ updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
for (const d of data) {
|
|
|
const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
|
|
|
if (d.sgfh_qty || d.sjcl_qty || d.qtcl_qty) {
|
|
|
- if (!bills || bills.id !== d.lid) {
|
|
|
- bills = await this.ctx.service.reviseBills.getDataById(d.lid);
|
|
|
- precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
|
|
|
- updateLid.push(d.lid);
|
|
|
- }
|
|
|
if (d.sgfh_qty !== undefined) {
|
|
|
d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
|
|
|
} else if (op) {
|
|
|
d.sgfh_qty = op.sgfh_qty;
|
|
|
}
|
|
|
+ updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
|
|
|
if (d.sjcl_qty !== undefined) {
|
|
|
d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
|
|
|
} else if (op) {
|
|
|
d.sjcl_qty = op.sjcl_qty;
|
|
|
}
|
|
|
+ updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
|
|
|
if (d.qtcl_qty) {
|
|
|
d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
|
|
|
} else if (op) {
|
|
|
d.qtcl_qty = op.qtcl_qty;
|
|
|
}
|
|
|
+ updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
|
|
|
d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
|
|
|
+ updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
|
|
|
}
|
|
|
if (d.id) {
|
|
|
await transaction.update(this.tableName, d);
|
|
@@ -317,18 +331,20 @@ module.exports = app => {
|
|
|
this._insertPosData(transaction, d, tid, rid);
|
|
|
}
|
|
|
}
|
|
|
- for (const lid of updateLid) {
|
|
|
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
|
|
|
- }
|
|
|
+ const info = this.ctx.tender.info;
|
|
|
+ updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
|
|
|
+ updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
|
|
|
+ updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
|
|
|
+ updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
|
|
|
+ await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
|
|
|
+ updateBills.ledger_id = bills.ledger_id;
|
|
|
await transaction.commit();
|
|
|
} catch (err) {
|
|
|
await transaction.rollback();
|
|
|
throw err;
|
|
|
}
|
|
|
result.pos = data;
|
|
|
- if (updateLid.length > 0) {
|
|
|
- result.ledger.update = await this.ctx.service.reviseBills.getDataById(updateLid);
|
|
|
- }
|
|
|
+ result.ledger.update = [updateBills];
|
|
|
return result;
|
|
|
}
|
|
|
}
|