|
|
@@ -97,42 +97,21 @@ module.exports = app => {
|
|
|
// --------------------------------------------
|
|
|
|
|
|
async _getAncGclUpdateData(data, ag_id, calc = true) {
|
|
|
- if (!calc) return [null, null];
|
|
|
-
|
|
|
+ if (!calc) return null;
|
|
|
try {
|
|
|
- const ancGclData = await this.ctx.service.ancillaryGcl.getDataById(ag_id);
|
|
|
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
|
|
|
- const posDatas = await this.ctx.service.pos.getAllDataByCondition({ where: { lid } });
|
|
|
- const detailDatas = await this.getAllDataByCondition({ columns: ['id', 'lid', 'pid', 'qty'], where: { pid } });
|
|
|
+ const detailDatas = await this.getAllDataByCondition({ columns: ['id', 'ag_id', 'qty'], where: { ag_id } });
|
|
|
|
|
|
- let sumPosQty = 0;
|
|
|
+ const ancGclUpdate = { id: ag_id, quantity: 0 };
|
|
|
for (const d of data) {
|
|
|
- sumPosQty = this.ctx.helper.add(d.qty, sumPosQty);
|
|
|
+ ancGclUpdate.quantity = this.ctx.helper.add(d.qty, ancGclUpdate.quantity);
|
|
|
}
|
|
|
for (const d of detailDatas) {
|
|
|
if (data.findIndex(x => { return x.id === d.id; }) >= 0) continue;
|
|
|
- sumPosQty = this.ctx.helper.add(d.qty, sumPosQty);
|
|
|
- }
|
|
|
- const op = posDatas.find(x => { return x.id === pid; });
|
|
|
- if (!op) throw '所属计量单元不存在,请刷新页面后再试';
|
|
|
- const ppd = { id: op.id, sgfh_qty: this.ctx.helper.round(sumPosQty, precision.value) };
|
|
|
- ppd.quantity = this.ctx.helper.sum([ppd.sgfh_qty, op.sjcl_qty, op.qtcl_qty]);
|
|
|
-
|
|
|
- let sumBillsQty = 0;
|
|
|
- for (const pd of posDatas) {
|
|
|
- if (pd.id === pid) {
|
|
|
- sumBillsQty = this.ctx.helper.add(sumBillsQty, ppd.sgfh_qty);
|
|
|
- } else {
|
|
|
- sumBillsQty = this.ctx.helper.add(sumBillsQty, pd.sgfh_qty);
|
|
|
- }
|
|
|
+ ancGclUpdate.quantity = this.ctx.helper.add(d.qty, ancGclUpdate.quantity);
|
|
|
}
|
|
|
- const bpd = { id: ledgerData.id, sgfh_qty: sumBillsQty };
|
|
|
- bpd.sgfh_tp = this.ctx.helper.mul(bpd.sgfh_qty, ledgerData.unit_price, this.ctx.tender.info.decimal.tp);
|
|
|
- bpd.quantity = this.ctx.helper.sum([bpd.sgfh_qty, ledgerData.sjcl_qty, ledgerData.qtcl_qty]);
|
|
|
- bpd.total_price = this.ctx.helper.mul(bpd.quantity, ledgerData.unit_price, this.ctx.tender.info.decimal.tp);
|
|
|
- return [ppd, bpd];
|
|
|
+ return ancGclUpdate;
|
|
|
} catch (err) {
|
|
|
- return [null, null];
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -140,35 +119,35 @@ module.exports = app => {
|
|
|
const user_id = this.ctx.session.sessionUser.accountId;
|
|
|
|
|
|
const datas = data instanceof Array ? data : [data];
|
|
|
- const le = await this.ctx.service.ledgerExtra.getDataById(datas[0].lid);
|
|
|
- if (!le.calc_template) throw '未定义计算模板,请先在清单处选择计算模板';
|
|
|
- const calcTemplate = await this.ctx.service.calcTmpl.getTemplate(le.calc_template);
|
|
|
+ const ancGcl = await this.ctx.service.ancillaryGcl.getDataById(datas[0].ag_id);
|
|
|
+ if (!ancGcl.calc_template) throw '未定义计算模板,请先在附属工程量选择计算模板';
|
|
|
+ const calcTemplate = await this.ctx.service.calcTmpl.getTemplate(ancGcl.calc_template);
|
|
|
if (!calcTemplate) throw '计算模板不存在';
|
|
|
- const posDetail = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, pid: datas[0].pid }, orders: [['pcd_order', 'ASC']] });
|
|
|
- const selectIndex = selectId ? posDetail.findIndex(x => { return x.id === selectId; }) : -1;
|
|
|
+ const details = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, pid: datas[0].pid }, orders: [['agd_order', 'ASC']] });
|
|
|
+ const selectIndex = selectId ? details.findIndex(x => { return x.id === selectId; }) : -1;
|
|
|
if (selectId && selectIndex < 0) throw '选择的数据不存在';
|
|
|
const startOrder = selectId
|
|
|
- ? (selectIndex > 0 ? posDetail[selectIndex - 1].pcd_order : 0)
|
|
|
- : (posDetail.length > 0 ? posDetail[posDetail.length - 1].pcd_order : 0);
|
|
|
+ ? (selectIndex > 0 ? details[selectIndex - 1].agd_order : 0)
|
|
|
+ : (details.length > 0 ? details[details.length - 1].agd_order : 0);
|
|
|
|
|
|
const insertData = [], updateData = [];
|
|
|
let isCalc = false;
|
|
|
for (const [i, d] of datas.entries()) {
|
|
|
- if (!d.lid || !d.pid || !d.pcd_order) throw '新增其他数据,提交的数据错误';
|
|
|
+ if (!d.lid || !d.pid || !d.ag_id || !d.agd_order) throw '新增其他数据,提交的数据错误';
|
|
|
const nd = {
|
|
|
id: this.uuid.v4(), tid: this.ctx.tender.id,
|
|
|
create_user_id: user_id, update_user_id: user_id,
|
|
|
- lid: d.lid, pid: d.pid, pcd_order: startOrder + i + 1,
|
|
|
+ lid: d.lid, pid: d.pid, ag_id: d.ag_id, agd_order: startOrder + i + 1,
|
|
|
};
|
|
|
if (this._loadDataAndCalc(nd, d, {}, calcTemplate)) isCalc = true;
|
|
|
insertData.push(nd);
|
|
|
}
|
|
|
- const [posUpdate, billsUpdate] = await this._getBillsPosUpdateData(insertData, insertData[0].pid, insertData[0].lid, isCalc);
|
|
|
+ const gclUpdate = await this._getAncGclUpdateData(insertData, insertData[0].ag_id, isCalc);
|
|
|
if (selectIndex >= 0) {
|
|
|
- const maxOrder = insertData[insertData.length - 1].pcd_order;
|
|
|
- for (const p of posDetail) {
|
|
|
- if (p.pcd_order > startOrder) {
|
|
|
- updateData.push({ id: p.id, pcd_order: maxOrder + updateData.length + 1 });
|
|
|
+ const maxOrder = insertData[insertData.length - 1].agd_order;
|
|
|
+ for (const p of details) {
|
|
|
+ if (p.agd_order > startOrder) {
|
|
|
+ updateData.push({ id: p.id, agd_order: maxOrder + updateData.length + 1 });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -177,8 +156,7 @@ module.exports = app => {
|
|
|
try {
|
|
|
await conn.insert(this.tableName, insertData);
|
|
|
if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
|
|
|
- if (posUpdate) await conn.update(this.ctx.service.pos.tableName, posUpdate);
|
|
|
- if (billsUpdate) await conn.update(this.ctx.service.ledger.tableName, billsUpdate);
|
|
|
+ if (gclUpdate) await conn.update(this.ctx.service.ancillaryGcl.tableName, gclUpdate);
|
|
|
await conn.commit();
|
|
|
} catch(err) {
|
|
|
await conn.rollback();
|
|
|
@@ -187,42 +165,39 @@ module.exports = app => {
|
|
|
const addData = await this.getAllDataByCondition({
|
|
|
where: { id: this.ctx.helper._.map(insertData, 'id') }
|
|
|
});
|
|
|
- const posData = isCalc ? await this.ctx.service.pos.getDataById(addData[0].pid) : null;
|
|
|
- const ledgerData = isCalc ? await this.ctx.service.ledger.getDataById(addData[0].lid) : null;
|
|
|
- return [addData, updateData, posData, ledgerData]
|
|
|
+ const gclUpdateData = isCalc ? await this.ctx.service.ancillaryGcl.getDataById(addData[0].ag_id) : null;
|
|
|
+ return [addData, updateData, gclUpdateData]
|
|
|
}
|
|
|
async _delDatas (data) {
|
|
|
if (!data || data.length === 0) throw '提交数据错误';
|
|
|
const orgDatas = await this.getAllDataByCondition({ where: { id: data } });
|
|
|
|
|
|
- if (!orgDatas || orgDatas.length === 0) throw '删除的设计量明细不存在';
|
|
|
+ if (!orgDatas || orgDatas.length === 0) throw '删除的明细不存在';
|
|
|
|
|
|
- let posDetail = await this.getAllDataByCondition({ where: { tid: orgDatas[0].tid, pid: orgDatas[0].pid } });
|
|
|
- posDetail = posDetail.filter(pa => {
|
|
|
+ let details = await this.getAllDataByCondition({ where: { tid: orgDatas[0].tid, pid: orgDatas[0].pid } });
|
|
|
+ details = details.filter(pa => {
|
|
|
return data.indexOf(pa.id) < 0;
|
|
|
});
|
|
|
- posDetail.sort((x, y) => { return x.pcd_order - y.pcd_order; });
|
|
|
+ details.sort((x, y) => { return x.agd_order - y.agd_order; });
|
|
|
|
|
|
const updateData = [];
|
|
|
- posDetail.forEach((x, i) => {
|
|
|
- if (x.pcd_order !== i + 1) updateData.push({ id: x.id, pcd_order: i + 1});
|
|
|
+ details.forEach((x, i) => {
|
|
|
+ if (x.agd_order !== i + 1) updateData.push({ id: x.id, agd_order: i + 1});
|
|
|
});
|
|
|
- const [posUpdate, billsUpdate] = await this._getBillsPosUpdateData(data.map(x => { return { id: x, qty: 0 }; }), orgDatas[0].pid, orgDatas[0].lid);
|
|
|
+ const gclUpdate = await this._getAncGclUpdateData(data.map(x => { return { id: x, qty: 0 }; }), orgDatas[0].ag_id);
|
|
|
|
|
|
const conn = await this.db.beginTransaction();
|
|
|
try {
|
|
|
await conn.delete(this.tableName, { id: data });
|
|
|
if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
|
|
|
- if (posUpdate) await conn.update(this.ctx.service.pos.tableName, posUpdate);
|
|
|
- if (billsUpdate) await conn.update(this.ctx.service.ledger.tableName, billsUpdate);
|
|
|
+ if (gclUpdate) await conn.update(this.ctx.service.ancillaryGcl.tableName, gclUpdate);
|
|
|
await conn.commit();
|
|
|
} catch (err) {
|
|
|
await conn.rollback();
|
|
|
throw err;
|
|
|
}
|
|
|
- const posData = await this.ctx.service.pos.getDataById(orgDatas[0].pid);
|
|
|
- const ledgerData = await this.ctx.service.ledger.getDataById(orgDatas[0].lid);
|
|
|
- return [data, updateData, posData, ledgerData];
|
|
|
+ const ancGclData = await this.ctx.service.ancillaryGcl.getDataById(orgDatas[0].ag_id);
|
|
|
+ return [data, updateData, ancGclData];
|
|
|
}
|
|
|
async _updateDatas (data) {
|
|
|
if (!data || data.length === 0) throw '提交数据错误';
|
|
|
@@ -232,11 +207,11 @@ module.exports = app => {
|
|
|
const orgDatas = await this.getAllDataByCondition({
|
|
|
where: { id: this.ctx.helper._.map(datas, 'id') }
|
|
|
});
|
|
|
- if (!orgDatas || orgDatas.length === 0) throw '修改的设计量明细不存在';
|
|
|
+ if (!orgDatas || orgDatas.length === 0) throw '修改的明细不存在';
|
|
|
|
|
|
- const le = await this.ctx.service.ledgerExtra.getDataById(orgDatas[0].lid);
|
|
|
- if (!le.calc_template) throw '未定义计算模板,请先在清单处选择计算模板';
|
|
|
- const calcTemplate = await this.ctx.service.calcTmpl.getTemplate(le.calc_template);
|
|
|
+ const ancGcl = await this.ctx.service.ancillaryGcl.getDataById(orgDatas[0].ag_id);
|
|
|
+ if (!ancGcl.calc_template) throw '未定义计算模板,请先在附属工程量选择计算模板';
|
|
|
+ const calcTemplate = await this.ctx.service.calcTmpl.getTemplate(ancGcl.calc_template);
|
|
|
if (!calcTemplate) throw '计算模板不存在';
|
|
|
|
|
|
const uDatas = [];
|
|
|
@@ -246,52 +221,44 @@ module.exports = app => {
|
|
|
if (!od) continue;
|
|
|
|
|
|
const nd = { id: od.id, update_user_id: user_id };
|
|
|
- if (d.pcd_order) nd.pcd_order = d.pcd_order;
|
|
|
+ if (d.agd_order) nd.agd_order = d.agd_order;
|
|
|
if (this._loadDataAndCalc(nd, d, od, calcTemplate)) isCalc = true;
|
|
|
uDatas.push(nd);
|
|
|
}
|
|
|
- const [posUpdate, billsUpdate] = await this._getBillsPosUpdateData(uDatas, orgDatas[0].pid, orgDatas[0].lid, isCalc);
|
|
|
+ const gclUpdate = await this._getAncGclUpdateData(uDatas, orgDatas[0].ag_id, isCalc);
|
|
|
|
|
|
if (uDatas.length > 0) {
|
|
|
const conn = await this.db.beginTransaction();
|
|
|
try {
|
|
|
await conn.updateRows(this.tableName, uDatas);
|
|
|
- if (posUpdate) await conn.update(this.ctx.service.pos.tableName, posUpdate);
|
|
|
- if (billsUpdate) await conn.update(this.ctx.service.ledger.tableName, billsUpdate);
|
|
|
+ if (gclUpdate) await conn.update(this.ctx.service.ancillaryGcl.tableName, gclUpdate);
|
|
|
await conn.commit();
|
|
|
} catch (err) {
|
|
|
await conn.rollback();
|
|
|
throw err;
|
|
|
}
|
|
|
- const posData = isCalc ? await this.ctx.service.pos.getDataById(orgDatas[0].pid) : null;
|
|
|
- const ledgerData = isCalc ? await this.ctx.service.ledger.getDataById(orgDatas[0].lid) : null;
|
|
|
- return [uDatas, posData, ledgerData];
|
|
|
+ return [uDatas, gclUpdate];
|
|
|
} else {
|
|
|
return [];
|
|
|
}
|
|
|
}
|
|
|
async updateDatas(data) {
|
|
|
- const result = { detail: {add: [], del: [], update: []}, pos: null, bills: null };
|
|
|
+ const result = { detail: {add: [], del: [], update: []}, ancGcl: null};
|
|
|
try {
|
|
|
if (data.add) {
|
|
|
- [result.detail.add, result.detail.update, result.pos, result.bills] = await this._addDatas(data.add, data.select);
|
|
|
+ [result.detail.add, result.detail.update, result.ancGcl] = await this._addDatas(data.add, data.select);
|
|
|
}
|
|
|
if (data.update) {
|
|
|
const orgUpdate = result.detail.update;
|
|
|
- [result.detail.update, result.pos, result.bills] = await this._updateDatas(data.update);
|
|
|
+ [result.detail.update, result.ancGcl] = await this._updateDatas(data.update);
|
|
|
if (orgUpdate.length > 0) result.detail.update.push(...orgUpdate);
|
|
|
}
|
|
|
if (data.del) {
|
|
|
- [result.detail.del, result.detail.update, result.pos, result.bills] = await this._delDatas(data.del);
|
|
|
+ [result.detail.del, result.detail.update, result.ancGcl] = await this._delDatas(data.del);
|
|
|
}
|
|
|
return result;
|
|
|
} catch (err) {
|
|
|
- if (err.stack) {
|
|
|
- throw err;
|
|
|
- } else {
|
|
|
- result.err = err.toString();
|
|
|
- return result;
|
|
|
- }
|
|
|
+ throw err;
|
|
|
}
|
|
|
}
|
|
|
|