|
@@ -98,16 +98,75 @@ module.exports = app => {
|
|
|
data.add_user = this.ctx.session.sessionUser.accountId;
|
|
|
}
|
|
|
|
|
|
+ async _getUpdateBills(data) {
|
|
|
+ const datas = data instanceof Array ? data : [data];
|
|
|
+ const bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
|
|
|
+ const billsPos = await this.getAllDataByCondition({where: {lid: bills.id} });
|
|
|
+ const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
|
|
|
+ const updateBills = {id: bills.id};
|
|
|
+ for (const bp of billsPos) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ for (const d of datas) {
|
|
|
+ if (d.sgfh_qty) {
|
|
|
+ d.sgfh_qty = this.ctx.helper.round(d.sgfh_qty, precision.value);
|
|
|
+ updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
|
|
|
+ }
|
|
|
+ if (d.sjcl_qty) {
|
|
|
+ d.sjcl_qty = this.ctx.helper.round(d.sjcl_qty, precision.value);
|
|
|
+ updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
|
|
|
+ }
|
|
|
+ if (d.qtcl_qty) {
|
|
|
+ d.qtcl_qty = this.ctx.helper.round(d.qtcl_qty, precision.value);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ return updateBills;
|
|
|
+ }
|
|
|
+
|
|
|
async _addPosData(tid, data) {
|
|
|
- if (data.updateData instanceof Array) {
|
|
|
- for (const d of data.updateData) {
|
|
|
+ let updateBills = null;
|
|
|
+ if (data instanceof Array) {
|
|
|
+ for (const d of data) {
|
|
|
this._completeInsertPosData(tid, d);
|
|
|
}
|
|
|
+ if (data[0].sgfh_qty !== undefined || data[0].sjcl_qty !== undefined || data[0].qtcl_qty !== undefined) {
|
|
|
+ updateBills = await this._getUpdateBills(data);
|
|
|
+ }
|
|
|
} else {
|
|
|
this._completeInsertPosData(tid, data);
|
|
|
+ if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
|
|
|
+ updateBills = await this._getUpdateBills(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (updateBills) {
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ await transaction.update(this.ctx.service.ledger.tableName, updateBills);
|
|
|
+ updateBills.ledger_id = bills.ledger_id;
|
|
|
+ await transaction.commit();
|
|
|
+ } catch (err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ ledger: { update: [updateBills] },
|
|
|
+ pos: data,
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ await this.db.insert(this.tableName, data);
|
|
|
+ return { pos: data }
|
|
|
}
|
|
|
- await this.db.insert(this.tableName, data);
|
|
|
- return { pos: data }
|
|
|
}
|
|
|
|
|
|
async _updatePosData(tid, data) {
|
|
@@ -150,9 +209,7 @@ module.exports = app => {
|
|
|
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
|
- console.log(data);
|
|
|
transaction.update(this.tableName, data);
|
|
|
- console.log(updateBills);
|
|
|
transaction.update(this.ctx.service.ledger.tableName, updateBills);
|
|
|
await transaction.commit();
|
|
|
updateBills.ledger_id = bills.ledger_id;
|
|
@@ -216,7 +273,7 @@ module.exports = app => {
|
|
|
for (const d of data) {
|
|
|
transaction.update(this.tableName, d);
|
|
|
}
|
|
|
- transaction.update(this.ctx.service.ledger.tableName, updateBills);
|
|
|
+ await transaction.update(this.ctx.service.ledger.tableName, updateBills);
|
|
|
await transaction.commit();
|
|
|
updateBills.ledger_id = bills.ledger_id;
|
|
|
return {
|
|
@@ -297,53 +354,65 @@ module.exports = app => {
|
|
|
}
|
|
|
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
- const result = { ledger: {}, pos: null }, updateBills = [];
|
|
|
+ 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.ledger.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: {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.ledger.getDataById(d.lid);
|
|
|
- precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
|
|
|
- updateBills.push(bills);
|
|
|
- }
|
|
|
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);
|
|
|
} else {
|
|
|
- this._insertPosData(transaction, d, tid);
|
|
|
+ this._completeInsertPosData(tid, d);
|
|
|
+ transaction.insert(this.tableName, d);
|
|
|
}
|
|
|
}
|
|
|
- for (const ub of updateBills) {
|
|
|
- await this.ctx.service.ledger.calcNode(ub, transaction);
|
|
|
- }
|
|
|
+ await transaction.update(this.ctx.service.ledger.tableName, updateBills);
|
|
|
+ updateBills.ledger_id = bills.ledger_id;
|
|
|
await transaction.commit();
|
|
|
} catch (err) {
|
|
|
await transaction.rollback();
|
|
|
throw err;
|
|
|
}
|
|
|
result.pos = data;
|
|
|
- if (updateBills.length > 0) {
|
|
|
- result.ledger.update = await this.ctx.service.ledger.getDataByIds(this._.map(updateBills, 'id'));
|
|
|
- }
|
|
|
+ result.ledger.update = [updateBills];
|
|
|
return result;
|
|
|
}
|
|
|
|