|
@@ -81,7 +81,127 @@ module.exports = app => {
|
|
|
const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
|
|
|
data.quantity = this.round(data.quantity, precision.value);
|
|
|
}
|
|
|
- const addRst = await transaction.insert(this.tableName, data);
|
|
|
+ if (transaction) {
|
|
|
+ const addRst = await transaction.insert(this.tableName, data);
|
|
|
+ } else {
|
|
|
+ const addRst = await this.db.insert(this.tableName, data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async _completeInsertPosData(tid, data) {
|
|
|
+ data.id = this.uuid.v4();
|
|
|
+ data.tid = tid;
|
|
|
+ // todo 新增期
|
|
|
+ data.add_stage = 0;
|
|
|
+ data.add_times = 0;
|
|
|
+ data.in_time = new Date();
|
|
|
+ data.add_user = this.ctx.session.sessionUser.accountId;
|
|
|
+ }
|
|
|
+
|
|
|
+ async _addPosData(tid, data) {
|
|
|
+ if (data.updateData instanceof Array) {
|
|
|
+ for (const d of data.updateData) {
|
|
|
+ this._completeInsertPosData(tid, d);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this._completeInsertPosData(tid, data);
|
|
|
+ }
|
|
|
+ console.log(data);
|
|
|
+ await this.db.insert(this.tableName, data);
|
|
|
+ return { pos: data }
|
|
|
+ }
|
|
|
+
|
|
|
+ async _updatePosData(tid, data) {
|
|
|
+ if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
|
|
|
+ const op = await this.getDataById(data.id);
|
|
|
+ const bills = await this.ctx.service.ledger.getDataById(op.lid);
|
|
|
+ const billsPos = await this.getAllDataByCondition({where: {lid: op.lid} });
|
|
|
+
|
|
|
+ const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
|
|
|
+ if (data.sgfh_qty !== undefined) {
|
|
|
+ data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
|
|
|
+ } else if (op) {
|
|
|
+ data.sgfh_qty = op.sgfh_qty;
|
|
|
+ }
|
|
|
+ if (data.sjcl_qty !== undefined) {
|
|
|
+ data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
|
|
|
+ } else if (op) {
|
|
|
+ data.sjcl_qty = op.sjcl_qty;
|
|
|
+ }
|
|
|
+ if (data.qtcl_qty !== undefined) {
|
|
|
+ data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
|
|
|
+ } else if (op) {
|
|
|
+ data.qtcl_qty = op.qtcl_qty;
|
|
|
+ }
|
|
|
+ data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
|
|
|
+
|
|
|
+ const updateBills = {id: bills.id};
|
|
|
+ for (const bp of billsPos) {
|
|
|
+ const calcData = bp.id === data.id ? data : bp;
|
|
|
+ updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
|
|
|
+ updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
|
|
|
+ updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
|
|
|
+ updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.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);
|
|
|
+
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ transaction.update(this.tableName, data);
|
|
|
+ transaction.update(this.ctx.service.ledger.tableName, updateBills);
|
|
|
+ await transaction.commit();
|
|
|
+ updateBills.ledger_id = bills.ledger_id;
|
|
|
+ return {
|
|
|
+ ledger: { update: [updateBills] },
|
|
|
+ pos: data,
|
|
|
+ }
|
|
|
+ } catch(err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ await this.db.update(this.tableName, d, {tid: tid, id: d.id});
|
|
|
+ return {pos: data};
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async _deletePosData(tid, data) {
|
|
|
+ if (!data || data.length === 0) {
|
|
|
+ throw '提交数据错误';
|
|
|
+ }
|
|
|
+
|
|
|
+ const pos = await this.getPosData({tid: tid, id: data});
|
|
|
+ const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
|
|
|
+ const billsPos = await this.getAllDataByCondition({ where: {lid: bills.id} });
|
|
|
+ const updateBills = {id: bills.id};
|
|
|
+ for (const bp of billsPos) {
|
|
|
+ if (data.indexOf(bp.id) >= 0) 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);
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ await transaction.delete(this.tableName, {tid: tid, id: data});
|
|
|
+ await transaction.update(this.ctx.service.ledger.tableName, updateBills);
|
|
|
+ await transaction.commit();
|
|
|
+ updateBills.ledger_id = bills.ledger_id;
|
|
|
+ return { ledger: { update: [updateBills] }, pos: data };
|
|
|
+ } catch(err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -91,77 +211,14 @@ module.exports = app => {
|
|
|
* @returns {Promise<{ledger: {}, pos: null}>}
|
|
|
*/
|
|
|
async savePosData(data, tid) {
|
|
|
- const result = { ledger: {}, pos: null };
|
|
|
- const transaction = await this.db.beginTransaction();
|
|
|
- try {
|
|
|
- if (data.updateType === 'add') {
|
|
|
- if (data.updateData instanceof Array) {
|
|
|
- for (const d of data.updateData) {
|
|
|
- this._insertPosData(transaction, d, tid);
|
|
|
- }
|
|
|
- } else {
|
|
|
- this._insertPosData(transaction, data.updateData, tid);
|
|
|
- }
|
|
|
- } else if (data.updateType === 'update') {
|
|
|
- const datas = data.updateData instanceof Array ? data.updateData : [data.updateData];
|
|
|
- result.ledger.update = [];
|
|
|
- const orgPos = await this.getAllDataByCondition({
|
|
|
- where: {tid: tid, id: this._.map(datas, 'id')},
|
|
|
- columns: ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'in_time', 'porder', 'add_stage'],
|
|
|
- order: [['porder', 'ASC']],
|
|
|
- });
|
|
|
- for (const d of datas) {
|
|
|
- const op = this._.find(orgPos, function (p) { return p.id = d.id; });
|
|
|
- if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
|
|
|
- const bills = await this.ctx.service.ledger.getDataById(op.lid);
|
|
|
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
|
|
|
- if (d.sgfh_qty !== undefined) {
|
|
|
- d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
|
|
|
- } else if (op) {
|
|
|
- d.sgfh_qty = op.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;
|
|
|
- }
|
|
|
- if (d.qtcl_qty !== undefined) {
|
|
|
- d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
|
|
|
- } else if (op) {
|
|
|
- d.qtcl_qty = op.qtcl_qty;
|
|
|
- }
|
|
|
- d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
|
|
|
- }
|
|
|
- await transaction.update(this.tableName, d, {tid: tid, id: d.id});
|
|
|
- if (d.quantity !== undefined && op && (result.ledger.update.indexOf(op.lid) === -1)) {
|
|
|
- result.ledger.update.push(op.lid);
|
|
|
- }
|
|
|
- }
|
|
|
- for (const lid of result.ledger.update) {
|
|
|
- await this.ctx.service.ledger.calc(tid, lid, transaction);
|
|
|
- }
|
|
|
- } else if (data.updateType === 'delete') {
|
|
|
- if (!data.updateData || data.updateData.length === 0) {
|
|
|
- throw '提交数据错误';
|
|
|
- }
|
|
|
- const pos = await this.getPosData({tid: tid, id: data.updateData});
|
|
|
- const ledgerIds = this._.map(pos, 'lid');
|
|
|
- await transaction.delete(this.tableName, {tid: tid, id: data.updateData});
|
|
|
- for (const lid of ledgerIds) {
|
|
|
- await this.ctx.service.ledger.calc(tid, lid, transaction);
|
|
|
- }
|
|
|
- result.ledger.update = ledgerIds;
|
|
|
- } else {
|
|
|
- throw '提交数据错误';
|
|
|
- }
|
|
|
- await transaction.commit();
|
|
|
- } catch (err) {
|
|
|
- await transaction.rollback();
|
|
|
- throw err;
|
|
|
+ switch (data.updateType) {
|
|
|
+ case 'add':
|
|
|
+ return await this._addPosData(tid, data.updateData);
|
|
|
+ case 'update':
|
|
|
+ return await this._updatePosData(tid, data.updateData);
|
|
|
+ case 'delete':
|
|
|
+ return await this._deletePosData(tid, data.updateData);
|
|
|
}
|
|
|
- result.pos = data.updateData;
|
|
|
- result.ledger.update = await this.ctx.service.ledger.getDataByIds(result.ledger.update);
|
|
|
- return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -176,7 +233,7 @@ module.exports = app => {
|
|
|
}
|
|
|
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
- const result = { ledger: {}, pos: null }, updateLid = [];
|
|
|
+ const result = { ledger: {}, pos: null }, updateBills = [];
|
|
|
const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
|
|
|
let bills = null, precision = null;
|
|
|
try {
|
|
@@ -186,7 +243,7 @@ module.exports = app => {
|
|
|
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);
|
|
|
- updateLid.push(d.lid);
|
|
|
+ updateBills.push(bills);
|
|
|
}
|
|
|
if (d.sgfh_qty !== undefined) {
|
|
|
d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
|
|
@@ -211,8 +268,8 @@ module.exports = app => {
|
|
|
this._insertPosData(transaction, d, tid);
|
|
|
}
|
|
|
}
|
|
|
- for (const lid of updateLid) {
|
|
|
- await this.ctx.service.ledger.calc(tid, lid, transaction);
|
|
|
+ for (const ub of updateBills) {
|
|
|
+ await this.ctx.service.ledger.calcNode(ub, transaction);
|
|
|
}
|
|
|
await transaction.commit();
|
|
|
} catch (err) {
|