|
|
@@ -36,6 +36,7 @@ module.exports = app => {
|
|
|
const order = await this._getMaxOrder(this.ctx.tender.id);
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
|
+ const resultData = {};
|
|
|
const newBills = {
|
|
|
tid: this.ctx.tender.id,
|
|
|
mid: this.ctx.material.id,
|
|
|
@@ -47,6 +48,10 @@ module.exports = app => {
|
|
|
if (result.affectedRows !== 1) {
|
|
|
throw '新增工料数据失败';
|
|
|
}
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ await this.ctx.service.materialStageBills.add(transaction, result.insertId);
|
|
|
+ resultData.pushStageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: result.insertId } });
|
|
|
+ }
|
|
|
const insertArray = [];
|
|
|
const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
|
|
|
for (const ym of material_month) {
|
|
|
@@ -61,7 +66,8 @@ module.exports = app => {
|
|
|
}
|
|
|
if (insertArray.length !== 0) await transaction.insert(this.ctx.service.materialMonth.tableName, insertArray);
|
|
|
await transaction.commit();
|
|
|
- return await this.getDataById(result.insertId);
|
|
|
+ resultData.info = await this.getDataById(result.insertId);
|
|
|
+ return resultData;
|
|
|
} catch (error) {
|
|
|
console.log(error);
|
|
|
await transaction.rollback();
|
|
|
@@ -69,6 +75,93 @@ module.exports = app => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 添加工料
|
|
|
+ * @return {void}
|
|
|
+ */
|
|
|
+ async addByGlj(data, order = null) {
|
|
|
+ if (!this.ctx.tender || !this.ctx.material) {
|
|
|
+ throw '数据错误';
|
|
|
+ }
|
|
|
+ const newOrder = this._.isNumber(order) ? parseInt(order) + 1 : await this._getMaxOrder(this.ctx.tender.id);
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ // order以下的工料+1
|
|
|
+ await this._syncOrder(transaction, this.ctx.tender.id, newOrder, '+');
|
|
|
+ const resultData = {};
|
|
|
+ const newBills = {
|
|
|
+ tid: this.ctx.tender.id,
|
|
|
+ mid: this.ctx.material.id,
|
|
|
+ code: data.code,
|
|
|
+ name: data.name,
|
|
|
+ unit: data.unit,
|
|
|
+ m_up_risk: data.rise_range,
|
|
|
+ m_down_risk: data.fall_range,
|
|
|
+ spec: data.spec,
|
|
|
+ m_type: data.type,
|
|
|
+ remark: data.memo,
|
|
|
+ order: newOrder,
|
|
|
+ in_time: new Date(),
|
|
|
+ };
|
|
|
+ // 新增工料
|
|
|
+ const result = await transaction.insert(this.tableName, newBills);
|
|
|
+ if (result.affectedRows !== 1) {
|
|
|
+ throw '新增工料数据失败';
|
|
|
+ }
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ await this.ctx.service.materialStageBills.add(transaction, result.insertId, data.memo);
|
|
|
+ resultData.pushStageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: result.insertId } });
|
|
|
+ }
|
|
|
+ const insertArray = [];
|
|
|
+ const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
|
|
|
+ for (const ym of material_month) {
|
|
|
+ const one_month = {
|
|
|
+ tid: this.ctx.tender.id,
|
|
|
+ mid: this.ctx.material.id,
|
|
|
+ mb_id: result.insertId,
|
|
|
+ msg_tp: null,
|
|
|
+ yearmonth: ym,
|
|
|
+ };
|
|
|
+ insertArray.push(one_month);
|
|
|
+ }
|
|
|
+ if (insertArray.length !== 0) await transaction.insert(this.ctx.service.materialMonth.tableName, insertArray);
|
|
|
+ await transaction.commit();
|
|
|
+ resultData.info = await this.getDataById(result.insertId);
|
|
|
+ return resultData;
|
|
|
+ } catch (error) {
|
|
|
+ console.log(error);
|
|
|
+ await transaction.rollback();
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移除清单时,同步其后清单order
|
|
|
+ * @param transaction - 事务
|
|
|
+ * @param {Number} cid - 变更cid
|
|
|
+ * @param {Number} order - order之后的
|
|
|
+ * @return {Promise<*>}
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+ async _syncOrder(transaction, tid, order, selfOperate = '-', num = 1) {
|
|
|
+ this.initSqlBuilder();
|
|
|
+ this.sqlBuilder.setAndWhere('tid', {
|
|
|
+ value: this.db.escape(tid),
|
|
|
+ operate: '=',
|
|
|
+ });
|
|
|
+ this.sqlBuilder.setAndWhere('order', {
|
|
|
+ value: order,
|
|
|
+ operate: '>=',
|
|
|
+ });
|
|
|
+ this.sqlBuilder.setUpdateData('order', {
|
|
|
+ value: num,
|
|
|
+ selfOperate,
|
|
|
+ });
|
|
|
+ const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
|
|
|
+ const data = await transaction.query(sql, sqlParam);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
async _getMaxOrder(tenderId) {
|
|
|
const sql = 'SELECT Max(??) As value FROM ?? Where tid = ' + tenderId;
|
|
|
const sqlParam = ['order', this.tableName];
|
|
|
@@ -88,19 +181,37 @@ module.exports = app => {
|
|
|
// 判断t_type是否为费用,且存在quantity,m_spread值
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
|
+ // 防止多页面操作时,清单工料含量存在时工料可删
|
|
|
+ const materialListNum = await this.ctx.service.materialList.count({ tid: this.ctx.tender.id, mb_id: id });
|
|
|
+ if (materialListNum > 0) {
|
|
|
+ throw '该工料已存在对应的清单工料含量,删除失败';
|
|
|
+ }
|
|
|
const mbInfo = await this.getDataById(id);
|
|
|
await transaction.delete(this.tableName, { id });
|
|
|
- let m_tp = this.ctx.material.m_tp;
|
|
|
- if (mbInfo.t_type === materialConst.t_type[1].value && mbInfo.quantity !== null && mbInfo.m_spread !== null) {
|
|
|
+ const m_tp = this.ctx.material.m_tp;
|
|
|
+ const result = { m_tp };
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ await transaction.delete(this.ctx.service.materialStageBills.tableName, { mb_id: id });
|
|
|
// 金额发生变化,则重新计算本期金额
|
|
|
- m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ for (const sid of this.ctx.material.stage_id.split(',')) {
|
|
|
+ const msInfo = await transaction.get(this.ctx.service.materialStage.tableName, { tid: this.ctx.tender.id, sid });
|
|
|
+ await this.ctx.service.materialStage.updateMtp(transaction, msInfo.id);
|
|
|
+ }
|
|
|
+ result.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id } });
|
|
|
+ result.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
|
|
|
+ result.m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ } else if (mbInfo.t_type === materialConst.t_type[1].value && mbInfo.quantity !== null && mbInfo.m_spread !== null) {
|
|
|
+ // 金额发生变化,则重新计算本期金额
|
|
|
+ result.m_tp = await this.calcMaterialMTp(transaction);
|
|
|
}
|
|
|
const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
|
|
|
if (material_month.length > 0) {
|
|
|
await transaction.delete(this.ctx.service.materialMonth.tableName, { mb_id: id });
|
|
|
}
|
|
|
+ // order以下的清单-1
|
|
|
+ await this._syncOrder(transaction, this.ctx.tender.id, mbInfo.order, '-');
|
|
|
await transaction.commit();
|
|
|
- return m_tp;
|
|
|
+ return result;
|
|
|
} catch (err) {
|
|
|
await transaction.rollback();
|
|
|
throw err;
|
|
|
@@ -113,23 +224,24 @@ module.exports = app => {
|
|
|
* @param {Number} id2 - 工料2的id
|
|
|
* @returns {Promise<void>}
|
|
|
*/
|
|
|
- async changeOrder(id1, id2) {
|
|
|
+ async changeOrder(datas) {
|
|
|
if (!this.ctx.tender || !this.ctx.material) {
|
|
|
throw '数据错误';
|
|
|
}
|
|
|
- const bill1 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id1 });
|
|
|
- const bill2 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id2 });
|
|
|
- if (!bill1 || !bill2) {
|
|
|
- throw '数据错误';
|
|
|
- }
|
|
|
+ // const bill1 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id1 });
|
|
|
+ // const bill2 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id2 });
|
|
|
+ // if (!bill1 || !bill2) {
|
|
|
+ // throw '数据错误';
|
|
|
+ // }
|
|
|
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
|
- const order = bill1.order;
|
|
|
- bill1.order = bill2.order;
|
|
|
- bill2.order = order;
|
|
|
- await transaction.update(this.tableName, { id: bill1.id, order: bill1.order });
|
|
|
- await transaction.update(this.tableName, { id: bill2.id, order: bill2.order });
|
|
|
+ // const order = bill1.order;
|
|
|
+ // bill1.order = bill2.order;
|
|
|
+ // bill2.order = order;
|
|
|
+ // await transaction.update(this.tableName, { id: bill1.id, order: bill1.order });
|
|
|
+ // await transaction.update(this.tableName, { id: bill2.id, order: bill2.order });
|
|
|
+ await transaction.updateRows(this.tableName, datas);
|
|
|
await transaction.commit();
|
|
|
return true;
|
|
|
} catch (err) {
|
|
|
@@ -143,7 +255,7 @@ module.exports = app => {
|
|
|
* @param {Object} data 工料内容
|
|
|
* @return {void}
|
|
|
*/
|
|
|
- async save(data) {
|
|
|
+ async save(data, ms_id = null) {
|
|
|
if (!this.ctx.tender || !this.ctx.material) {
|
|
|
throw '数据错误';
|
|
|
}
|
|
|
@@ -153,16 +265,90 @@ module.exports = app => {
|
|
|
// 判断t_type是否为费用
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
|
+ const result = {};
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ if (!ms_id) {
|
|
|
+ throw '期参数有误';
|
|
|
+ }
|
|
|
+ const needUp = await this.updateOneBillsData(transaction, data, ms_id);
|
|
|
+ const [one_m_tp, one_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, data, ms_id);
|
|
|
+ // 更新materialStage 值
|
|
|
+ data.m_tp = this.ctx.helper.round(one_m_tp, this.ctx.material.decimal.tp);
|
|
|
+ data.m_tax_tp = this.ctx.helper.round(one_tax_tp, this.ctx.material.decimal.tp);
|
|
|
+ data.quantity = null;
|
|
|
+ data.msg_tp = null;
|
|
|
+ data.msg_times = null;
|
|
|
+ data.msg_spread = null;
|
|
|
+ data.m_spread = null;
|
|
|
+ delete data.ms_id;
|
|
|
+
|
|
|
+ // 更新materialStage 值
|
|
|
+ await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
|
|
|
+ result.stageBillsData = this.ctx.material.is_stage_self && needUp ?
|
|
|
+ await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: data.id } }) :
|
|
|
+ await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, ms_id, mb_id: data.id } });
|
|
|
+ result.stageData = this.ctx.material.is_stage_self && needUp ?
|
|
|
+ await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) :
|
|
|
+ await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id, id: ms_id } });
|
|
|
+ }
|
|
|
await transaction.update(this.tableName, data);
|
|
|
- const m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ result.m_tp = await this.calcMaterialMTp(transaction);
|
|
|
await transaction.commit();
|
|
|
- return m_tp;
|
|
|
+ return result;
|
|
|
} catch (err) {
|
|
|
await transaction.rollback();
|
|
|
throw err;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async updateOneBillsData(transaction, data, ms_id) {
|
|
|
+ // 当以下值和bills值不相同时,需要同步更新最新的计算值到stageBills表里
|
|
|
+ const updateColsArray = ['t_type', 'm_tax', 'basic_price', 'm_up_risk', 'm_down_risk', 'is_summary'];
|
|
|
+ let needUp = null;
|
|
|
+ const mbInfo = await this.getDataById(data.id);
|
|
|
+ for (const uc of updateColsArray) {
|
|
|
+ if (data[uc] !== undefined && data[uc] !== mbInfo[uc]) {
|
|
|
+ needUp = uc;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (needUp) {
|
|
|
+ const msList = await this.ctx.service.materialStage.getAllDataByCondition({ where: { mid: this.ctx.material.id } });
|
|
|
+ for (const ms of msList) {
|
|
|
+ if (ms.id !== parseInt(ms_id)) {
|
|
|
+ const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({
|
|
|
+ mid: this.ctx.material.id,
|
|
|
+ ms_id: ms.id,
|
|
|
+ mb_id: data.id,
|
|
|
+ });
|
|
|
+ const updateData = {
|
|
|
+ id: msbInfo.id,
|
|
|
+ };
|
|
|
+ if (needUp === 'm_tax') {
|
|
|
+ updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(msbInfo.m_tp, (1 + this.ctx.helper.div(data.m_tax, 100))), this.ctx.material.decimal.tp);
|
|
|
+ } else if (needUp === 'm_up_risk' || needUp === 'm_down_risk' || needUp === 'basic_price') {
|
|
|
+ const basic_price = needUp === 'basic_price' ? data.basic_price : mbInfo.basic_price;
|
|
|
+ const [msg_spread, m_spread] = await this.getSpread(mbInfo, msbInfo.msg_tp, this.ctx.material.decimal.up, basic_price);
|
|
|
+ updateData.msg_spread = msg_spread;
|
|
|
+ updateData.m_spread = m_spread;
|
|
|
+ const newTp = this.ctx.helper.round(this.ctx.helper.mul(msbInfo.quantity, m_spread), this.ctx.material.decimal.tp);
|
|
|
+ updateData.m_tp = newTp;
|
|
|
+ updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp);
|
|
|
+ } else if (needUp === 't_type') {
|
|
|
+ updateData.quantity = null;
|
|
|
+ updateData.m_tp = null;
|
|
|
+ updateData.m_tax_tp = null;
|
|
|
+ } else if (needUp === 'is_summary') {
|
|
|
+ updateData.is_summary = data.is_summary;
|
|
|
+ }
|
|
|
+ await transaction.update(this.ctx.service.materialStageBills.tableName, updateData);
|
|
|
+ await this.ctx.service.materialStage.updateMtp(transaction, ms.id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return needUp;
|
|
|
+ }
|
|
|
+
|
|
|
async saveOrigin(data) {
|
|
|
if (!this.ctx.tender || !this.ctx.material) {
|
|
|
throw '数据错误';
|
|
|
@@ -190,7 +376,7 @@ module.exports = app => {
|
|
|
* @param {Object} data 工料内容
|
|
|
* @return {void}
|
|
|
*/
|
|
|
- async saveDatas(datas) {
|
|
|
+ async saveDatas(datas, ms_id = null) {
|
|
|
if (!this.ctx.tender || !this.ctx.material) {
|
|
|
throw '数据错误';
|
|
|
}
|
|
|
@@ -200,15 +386,34 @@ module.exports = app => {
|
|
|
try {
|
|
|
for (const data of datas) {
|
|
|
delete data.in_time;
|
|
|
+ let needUp = null;
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ needUp = await this.updateOneBillsData(transaction, data, ms_id);
|
|
|
+ const [one_m_tp, one_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, data, ms_id);
|
|
|
+ // 更新materialStage 值
|
|
|
+ data.m_tp = this.ctx.helper.round(one_m_tp, this.ctx.material.decimal.tp);
|
|
|
+ data.m_tax_tp = this.ctx.helper.round(one_tax_tp, this.ctx.material.decimal.tp);
|
|
|
+ data.quantity = null;
|
|
|
+ data.msg_tp = null;
|
|
|
+ data.msg_times = null;
|
|
|
+ data.msg_spread = null;
|
|
|
+ data.msg_spread = null;
|
|
|
+ data.m_spread = null;
|
|
|
+ }
|
|
|
// delete data.m_tp;
|
|
|
// console.log(data);
|
|
|
await transaction.update(this.tableName, data);
|
|
|
}
|
|
|
- // console.log(datas);
|
|
|
- // await transaction.update(this.tableName, datas);
|
|
|
- const m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ // 更新materialStage 值
|
|
|
+ const result = {};
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
|
|
|
+ result.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id } });
|
|
|
+ result.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
|
|
|
+ }
|
|
|
+ result.m_tp = await this.calcMaterialMTp(transaction);
|
|
|
await transaction.commit();
|
|
|
- return m_tp;
|
|
|
+ return result;
|
|
|
} catch (err) {
|
|
|
await transaction.rollback();
|
|
|
throw err;
|
|
|
@@ -297,10 +502,11 @@ module.exports = app => {
|
|
|
* @param data
|
|
|
* @returns {Promise<void>}
|
|
|
*/
|
|
|
- async getSpread(data, msg_tp, newDecimalUp = this.ctx.material.decimal.up) {
|
|
|
+ async getSpread(data, msg_tp, newDecimalUp = this.ctx.material.decimal.up, basic_price = null) {
|
|
|
data.msg_tp = msg_tp;
|
|
|
- const msg_spread = this.ctx.helper.round(this.ctx.helper.sub(data.msg_tp, data.basic_price), newDecimalUp);
|
|
|
- const cor = msg_spread >= 0 ? this.ctx.helper.mul(data.basic_price, this.ctx.helper.div(data.m_up_risk, 100)) : this.ctx.helper.mul(data.basic_price, this.ctx.helper.div(data.m_down_risk, 100));
|
|
|
+ const newBp = basic_price ? basic_price : data.basic_price;
|
|
|
+ const msg_spread = this.ctx.helper.round(this.ctx.helper.sub(data.msg_tp, newBp), newDecimalUp);
|
|
|
+ const cor = msg_spread >= 0 ? this.ctx.helper.mul(newBp, this.ctx.helper.div(data.m_up_risk, 100)) : this.ctx.helper.mul(newBp, this.ctx.helper.div(data.m_down_risk, 100));
|
|
|
const m_spread = Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? this.ctx.helper.round(this.ctx.helper.sub(msg_spread, cor), newDecimalUp) : this.ctx.helper.round(this.ctx.helper.add(msg_spread, cor), newDecimalUp)) : 0;
|
|
|
return [msg_spread, m_spread];
|
|
|
}
|
|
|
@@ -316,19 +522,60 @@ module.exports = app => {
|
|
|
}
|
|
|
const transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
|
- const mbInfo = await this.getDataById(data.id);
|
|
|
- data.m_tp = this.ctx.helper.round(this.ctx.helper.mul(data.quantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
|
|
|
- data.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(data.m_tp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp);
|
|
|
- await transaction.update(this.tableName, data);
|
|
|
- let m_tp = this.ctx.material.m_tp;
|
|
|
- if (mbInfo.quantity !== data.quantity) {
|
|
|
- m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ const returnData = {};
|
|
|
+ returnData.m_tp = this.ctx.material.m_tp;
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ const mbInfo = await this.getDataById(data.id);
|
|
|
+ let all_m_tp = 0;
|
|
|
+ let all_tax_tp = 0;
|
|
|
+ for (const sid of this.ctx.material.stage_id.split(',')) {
|
|
|
+ const materialCalculator = new MaterialCalculator(this.ctx, sid, this.ctx.tender.info);
|
|
|
+ const quantity = await materialCalculator.calculateExpr(data.expr);
|
|
|
+ const msInfo = await this.ctx.service.materialStage.getDataByCondition({ mid: this.ctx.material.id, sid });
|
|
|
+ const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({ mid: this.ctx.material.id, mb_id: data.id, ms_id: msInfo.id });
|
|
|
+ const newQuantity = quantity !== 0 ? this.ctx.helper.round(quantity, this.ctx.material.decimal.qty) : null;
|
|
|
+ const m_tp = newQuantity ? this.ctx.helper.round(this.ctx.helper.mul(newQuantity, msbInfo.m_spread), this.ctx.material.decimal.tp) : null;
|
|
|
+ const updateData = {
|
|
|
+ id: data.id,
|
|
|
+ quantity: newQuantity,
|
|
|
+ m_tp,
|
|
|
+ m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(m_tp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp),
|
|
|
+ };
|
|
|
+ const [one_bill_m_tp, one_bill_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, updateData, msInfo.id);
|
|
|
+ await this.ctx.service.materialStage.updateMtp(transaction, msInfo.id);
|
|
|
+ all_m_tp = this.ctx.helper.round(one_bill_m_tp, this.ctx.material.decimal.tp);
|
|
|
+ all_tax_tp = this.ctx.helper.round(one_bill_tax_tp, this.ctx.material.decimal.tp);
|
|
|
+ }
|
|
|
+ const updateBillsData = {
|
|
|
+ id: data.id,
|
|
|
+ expr: data.expr,
|
|
|
+ m_tp: all_m_tp ? all_m_tp : null,
|
|
|
+ m_tax_tp: all_tax_tp ? all_tax_tp : null,
|
|
|
+ };
|
|
|
+ console.log(all_m_tp);
|
|
|
+ await transaction.update(this.tableName, updateBillsData);
|
|
|
+ returnData.m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ returnData.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: data.id } });
|
|
|
+ returnData.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
|
|
|
+ } else {
|
|
|
+ const materialCalculator = new MaterialCalculator(this.ctx, this.ctx.material.stage_id, this.ctx.tender.info);
|
|
|
+ const quantity = await materialCalculator.calculateExpr(data.expr);
|
|
|
+ // 更新quantity值并重新返回计算本期金额,截止本期金额
|
|
|
+ const updateData = {
|
|
|
+ id: data.id,
|
|
|
+ quantity: quantity !== 0 ? this.ctx.helper.round(quantity, this.ctx.material.decimal.qty) : null,
|
|
|
+ expr: data.expr,
|
|
|
+ };
|
|
|
+ const mbInfo = await this.getDataById(updateData.id);
|
|
|
+ updateData.m_tp = this.ctx.helper.round(this.ctx.helper.mul(updateData.quantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
|
|
|
+ updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(updateData.m_tp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp);
|
|
|
+ await transaction.update(this.tableName, updateData);
|
|
|
+ if (mbInfo.quantity !== updateData.quantity) {
|
|
|
+ returnData.m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ }
|
|
|
}
|
|
|
await transaction.commit();
|
|
|
- const returnData = {
|
|
|
- m_tp,
|
|
|
- info: await this.getDataById(data.id),
|
|
|
- }
|
|
|
+ returnData.info = await this.getDataById(data.id);
|
|
|
return returnData;
|
|
|
} catch (err) {
|
|
|
await transaction.rollback();
|
|
|
@@ -356,52 +603,115 @@ module.exports = app => {
|
|
|
}
|
|
|
|
|
|
// 小数位变化更新单价和金额
|
|
|
- async resetDecimal(transaction, newDecimalUp, newDecimalTp) {
|
|
|
+ async resetDecimal(transaction, newDecimalUp, newDecimalTp, newDecimalQty) {
|
|
|
const mbList = await transaction.select(this.tableName, { where: { tid: this.ctx.tender.id }, orders: [['order', 'asc']] });
|
|
|
const updateList = [];
|
|
|
const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
|
|
|
const updateMonthList = [];
|
|
|
+ const materialStageList = this.ctx.material.is_stage_self ? await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) : [];
|
|
|
for (const mb of mbList) {
|
|
|
const updateData = {
|
|
|
id: mb.id,
|
|
|
};
|
|
|
- if (newDecimalUp !== this.ctx.material.decimal.up) {
|
|
|
- let newmsg_tp = this.ctx.helper.round(mb.msg_tp, newDecimalUp);
|
|
|
- mb.msg_tp = newmsg_tp;
|
|
|
- // 判断是否有月信息价,如果有则msg_tp值由月信息价的平均单价获得,并更新月信息价单价
|
|
|
- if (material_month.length > 0) {
|
|
|
- const monthList = await transaction.select(this.ctx.service.materialMonth.tableName, { where: { mb_id: mb.id, mid: this.ctx.material.id } });
|
|
|
- if (monthList.length !== 0) {
|
|
|
- for (const m of monthList) {
|
|
|
- // 更新月信息单价小数位
|
|
|
- const newMonthMsgTP = this.ctx.helper.round(m.msg_tp, newDecimalUp);
|
|
|
- if (m.msg_tp && newMonthMsgTP !== m.msg_tp) {
|
|
|
- m.msg_tp = newMonthMsgTP;
|
|
|
- updateMonthList.push({ id: m.id, msg_tp: m.msg_tp });
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ const updateStageBillsList = [];
|
|
|
+ for (const ms of materialStageList) {
|
|
|
+ const msb = await transaction.get(this.ctx.service.materialStageBills.tableName, { mid: this.ctx.material.id, mb_id: mb.id, ms_id: ms.id });
|
|
|
+ msb.m_up_risk = mb.m_up_risk;
|
|
|
+ msb.m_down_risk = mb.m_down_risk;
|
|
|
+ const updateStageBillData = {
|
|
|
+ id: msb.id,
|
|
|
+ };
|
|
|
+ if (newDecimalQty !== this.ctx.material.decimal.qty) {
|
|
|
+ // 通过管理重新算出quantity并保留小数位
|
|
|
+ const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `ms_id`=? AND `is_join`=1';
|
|
|
+ const sqlParam = [this.ctx.material.id, mb.id, ms.id];
|
|
|
+ const mb_quantity = await transaction.queryOne(sql, sqlParam);
|
|
|
+ const newQuantity = this.ctx.helper.round(mb_quantity.quantity, newDecimalQty);
|
|
|
+ if (newQuantity !== msb.quantity) {
|
|
|
+ updateStageBillData.quantity = newQuantity;
|
|
|
+ msb.quantity = newQuantity;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (newDecimalUp !== this.ctx.material.decimal.up) {
|
|
|
+ const newmsg_tp = this.ctx.helper.round(msb.msg_tp, newDecimalUp);
|
|
|
+ msb.msg_tp = newmsg_tp;
|
|
|
+ const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
|
|
|
+ const [newmsg_spread, newm_spread] = await this.getSpread(msb, msb.msg_tp, newDecimalUp, newbasic_price);
|
|
|
+ msb.m_spread = newm_spread;
|
|
|
+ updateStageBillData.msg_tp = newmsg_tp;
|
|
|
+ updateStageBillData.msg_spread = newmsg_spread;
|
|
|
+ updateStageBillData.m_spread = newm_spread;
|
|
|
+ }
|
|
|
+ const newTp = this.ctx.helper.round(this.ctx.helper.mul(msb.quantity, msb.m_spread), newDecimalTp);
|
|
|
+ updateStageBillData.m_tp = newTp;
|
|
|
+ updateStageBillData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), newDecimalTp);
|
|
|
+ updateStageBillsList.push(updateStageBillData);
|
|
|
+ }
|
|
|
+ if (newDecimalUp !== this.ctx.material.decimal.up) {
|
|
|
+ const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
|
|
|
+ updateData.basic_price = newbasic_price;
|
|
|
+ }
|
|
|
+ if (updateStageBillsList.length > 0) await transaction.updateRows(this.ctx.service.materialStageBills.tableName, updateStageBillsList);
|
|
|
+ const sql = 'SELECT SUM(`m_tp`) as total_price, SUM(IF(`m_tax_tp` is null, `m_tp`, `m_tax_tp`)) as tax_total_price FROM ' + this.ctx.service.materialStageBills.tableName + ' WHERE `tid` = ? AND `mid` = ? AND `mb_id` = ? AND `is_summary` = 1';
|
|
|
+ const sqlParam = [this.ctx.tender.id, this.ctx.material.id, mb.id];
|
|
|
+ const tp = await transaction.queryOne(sql, sqlParam);
|
|
|
+ updateData.m_tp = this.ctx.helper.round(tp.total_price, newDecimalTp);
|
|
|
+ updateData.m_tax_tp = this.ctx.helper.round(tp.tax_total_price, newDecimalTp);
|
|
|
+ updateList.push(updateData);
|
|
|
+ } else {
|
|
|
+ if (newDecimalUp !== this.ctx.material.decimal.up) {
|
|
|
+ let newmsg_tp = this.ctx.helper.round(mb.msg_tp, newDecimalUp);
|
|
|
+ mb.msg_tp = newmsg_tp;
|
|
|
+ // 判断是否有月信息价,如果有则msg_tp值由月信息价的平均单价获得,并更新月信息价单价
|
|
|
+ if (material_month.length > 0) {
|
|
|
+ const monthList = await transaction.select(this.ctx.service.materialMonth.tableName, { where: { mb_id: mb.id, mid: this.ctx.material.id } });
|
|
|
+ if (monthList.length !== 0) {
|
|
|
+ for (const m of monthList) {
|
|
|
+ // 更新月信息单价小数位
|
|
|
+ const newMonthMsgTP = this.ctx.helper.round(m.msg_tp, newDecimalUp);
|
|
|
+ if (m.msg_tp && newMonthMsgTP !== m.msg_tp) {
|
|
|
+ m.msg_tp = newMonthMsgTP;
|
|
|
+ updateMonthList.push({ id: m.id, msg_tp: m.msg_tp });
|
|
|
+ }
|
|
|
}
|
|
|
+ const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
|
|
|
+ const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), [null, '', 0]);
|
|
|
+ newmsg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), newDecimalUp) : null;
|
|
|
+ mb.msg_tp = newmsg_tp;
|
|
|
}
|
|
|
- const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
|
|
|
- const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), [null, '', 0]);
|
|
|
- newmsg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), newDecimalUp) : null;
|
|
|
- mb.msg_tp = newmsg_tp;
|
|
|
}
|
|
|
+ const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
|
|
|
+ mb.basic_price = newbasic_price;
|
|
|
+ const [newmsg_spread, newm_spread] = await this.getSpread(mb, mb.msg_tp, newDecimalUp);
|
|
|
+ mb.m_spread = newm_spread;
|
|
|
+ updateData.basic_price = newbasic_price;
|
|
|
+ updateData.msg_tp = newmsg_tp;
|
|
|
+ updateData.msg_spread = newmsg_spread;
|
|
|
+ updateData.m_spread = newm_spread;
|
|
|
+ }
|
|
|
+ if (newDecimalQty !== this.ctx.material.decimal.qty) {
|
|
|
+ // 通过管理重新算出quantity并保留小数位
|
|
|
+ const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
|
|
|
+ const sqlParam = [this.ctx.material.id, mb.id];
|
|
|
+ const mb_quantity = await transaction.queryOne(sql, sqlParam);
|
|
|
+ const newQuantity = this.ctx.helper.round(mb_quantity.quantity, newDecimalQty);
|
|
|
+ mb.quantity = newQuantity;
|
|
|
+ updateData.quantity = newQuantity;
|
|
|
}
|
|
|
- const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
|
|
|
- mb.basic_price = newbasic_price;
|
|
|
- const [newmsg_spread, newm_spread] = await this.getSpread(mb, mb.msg_tp, newDecimalUp);
|
|
|
- mb.m_spread = newm_spread;
|
|
|
- updateData.basic_price = newbasic_price;
|
|
|
- updateData.msg_tp = newmsg_tp;
|
|
|
- updateData.msg_spread = newmsg_spread;
|
|
|
- updateData.m_spread = newm_spread;
|
|
|
+ const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, mb.m_spread), newDecimalTp);
|
|
|
+ updateData.m_tp = newTp;
|
|
|
+ updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), newDecimalTp);
|
|
|
+ updateList.push(updateData);
|
|
|
}
|
|
|
- const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, mb.m_spread), newDecimalTp);
|
|
|
- updateData.m_tp = newTp;
|
|
|
- updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), newDecimalTp);
|
|
|
- updateList.push(updateData);
|
|
|
}
|
|
|
if (updateMonthList.length > 0) await transaction.updateRows(this.ctx.service.materialMonth.tableName, updateMonthList);
|
|
|
if (updateList.length > 0) await transaction.updateRows(this.tableName, updateList);
|
|
|
+ if (this.ctx.material.is_stage_self) {
|
|
|
+ for (const ms of materialStageList) {
|
|
|
+ await this.ctx.service.materialStage.updateMtp(transaction, ms.id);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|