|
@@ -96,163 +96,120 @@ module.exports = app => {
|
|
|
const addRst = await transaction.insert(this.tableName, data);
|
|
|
}
|
|
|
|
|
|
+ async _completeInsertPosData(tid, rid, 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;
|
|
|
+ data.crid = rid;
|
|
|
+ }
|
|
|
+
|
|
|
async addPos(tid, rid, data) {
|
|
|
- const transaction = await this.db.beginTransaction();
|
|
|
- try {
|
|
|
- if (data instanceof Array) {
|
|
|
- for (const d of data) {
|
|
|
- this._insertPosData(transaction, d, tid, rid);
|
|
|
- }
|
|
|
- } else {
|
|
|
- this._insertPosData(transaction, data, tid, rid);
|
|
|
+ if (data instanceof Array) {
|
|
|
+ for (const d of data) {
|
|
|
+ this._completeInsertPosData(tid, rid, d);
|
|
|
}
|
|
|
- await transaction.commit();
|
|
|
- } catch (err) {
|
|
|
- await transaction.rollback();
|
|
|
- throw err;
|
|
|
+ } else {
|
|
|
+ this._completeInsertPosData(tid, rid, data);
|
|
|
}
|
|
|
+ this.db.insert(this.tableName, data);
|
|
|
return {pos: data};
|
|
|
}
|
|
|
|
|
|
async updatePos(tid, data) {
|
|
|
- const billsIds = [];
|
|
|
- const datas = data instanceof Array ? data : [data];
|
|
|
- const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
|
|
|
- const transaction = await this.db.beginTransaction();
|
|
|
- try {
|
|
|
- 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.reviseBills.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 && (billsIds.indexOf(op.lid) === -1)) {
|
|
|
- billsIds.push(op.lid);
|
|
|
- }
|
|
|
+ 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;
|
|
|
}
|
|
|
- for (const lid of billsIds) {
|
|
|
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
|
|
|
+ if (data.sjcl_qty !== undefined) {
|
|
|
+ data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
|
|
|
+ } else if (op) {
|
|
|
+ data.sjcl_qty = op.sjcl_qty;
|
|
|
}
|
|
|
- await transaction.commit();
|
|
|
- } catch (err) {
|
|
|
- await transaction.rollback();
|
|
|
- throw err;
|
|
|
+ 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, data, {tid: tid, id: data.id});
|
|
|
+ return {pos: data};
|
|
|
}
|
|
|
- const bills = await this.ctx.service.reviseBills.getDataById(billsIds);
|
|
|
- return {pos: data, ledger: {update: bills}};
|
|
|
}
|
|
|
|
|
|
async deletePos(tid, data) {
|
|
|
if (!data || data.length === 0) {
|
|
|
throw '提交数据错误';
|
|
|
}
|
|
|
+
|
|
|
const pos = await this.getPosData({tid: tid, id: data});
|
|
|
- const ledgerIds = this._.map(pos, 'lid');
|
|
|
- const transaction = await this.db.beginTransaction();
|
|
|
- try {
|
|
|
- await transaction.delete(this.tableName, {tid: tid, id: data});
|
|
|
- for (const lid of ledgerIds) {
|
|
|
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
|
|
|
- }
|
|
|
- await transaction.commit();
|
|
|
- } catch (err) {
|
|
|
- await transaction.rollback();
|
|
|
- throw err;
|
|
|
+ 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 bills = await this.ctx.service.reviseBills.getDataById(ledgerIds);
|
|
|
- return {ledger: {update: bills}, pos: data};
|
|
|
- }
|
|
|
+ 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);
|
|
|
|
|
|
- /**
|
|
|
- * 保存部位明细数据
|
|
|
- * @param data
|
|
|
- * @param {Number} tid - 标段id
|
|
|
- * @returns {Promise<{ledger: {}, pos: null}>}
|
|
|
- */
|
|
|
- async savePosData(tid, rid, data) {
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
|
- if (data.posPostType === 'add') {
|
|
|
- if (data.updateData instanceof Array) {
|
|
|
- for (const d of data.postData) {
|
|
|
- this._insertPosData(transaction, d, tid, rid);
|
|
|
- }
|
|
|
- } else {
|
|
|
- this._insertPosData(transaction, data.postData, tid, rid);
|
|
|
- }
|
|
|
- } else if (data.posPostType === 'update') {
|
|
|
- const datas = data.postData instanceof Array ? data.postData : [data.postData];
|
|
|
- result.ledger.update = [];
|
|
|
- const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
|
|
|
- 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.reviseBills.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.reviseBills.calc(tid, lid, transaction);
|
|
|
- }
|
|
|
- } else if (data.posPostType === 'delete') {
|
|
|
- if (!data.postData || data.postData.length === 0) {
|
|
|
- throw '提交数据错误';
|
|
|
- }
|
|
|
- const pos = await this.getPosData({tid: tid, id: data.postData});
|
|
|
- const ledgerIds = this._.map(pos, 'lid');
|
|
|
- await transaction.delete(this.tableName, {tid: tid, id: data.postData});
|
|
|
- for (const lid of ledgerIds) {
|
|
|
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
|
|
|
- }
|
|
|
- result.ledger.update = ledgerIds;
|
|
|
- } else {
|
|
|
- throw '提交数据错误';
|
|
|
- }
|
|
|
+ await transaction.delete(this.tableName, {tid: tid, id: data});
|
|
|
+ await transaction.update(this.ctx.service.ledger.tableName, updateBills);
|
|
|
await transaction.commit();
|
|
|
- } catch (err) {
|
|
|
+ updateBills.ledger_id = bills.ledger_id;
|
|
|
+ return { ledger: { update: [updateBills] }, pos: data };
|
|
|
+ } catch(err) {
|
|
|
await transaction.rollback();
|
|
|
throw err;
|
|
|
}
|
|
|
- result.pos = data.postData;
|
|
|
- result.ledger.update = await this.ctx.service.reviseBills.getDataById(result.ledger.update);
|
|
|
- return result;
|
|
|
}
|
|
|
|
|
|
/**
|