|
@@ -9,6 +9,7 @@
|
|
|
*/
|
|
|
|
|
|
const auditConst = require('../const/audit').material;
|
|
|
+const materialConst = require('../const/material');
|
|
|
|
|
|
module.exports = app => {
|
|
|
class MaterialBills extends app.BaseService {
|
|
@@ -54,8 +55,22 @@ module.exports = app => {
|
|
|
if (!this.ctx.tender || !this.ctx.material) {
|
|
|
throw '数据错误';
|
|
|
}
|
|
|
- // 判断是否可删
|
|
|
- return await this.deleteById(id);
|
|
|
+ // 判断t_type是否为费用,且存在quantity,m_spread值
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ 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) {
|
|
|
+ // 金额发生变化,则重新计算本期金额
|
|
|
+ m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ }
|
|
|
+ await transaction.commit();
|
|
|
+ return m_tp;
|
|
|
+ } catch (err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -70,9 +85,29 @@ module.exports = app => {
|
|
|
delete data.in_time;
|
|
|
delete data.m_tp;
|
|
|
// 判断是否可修改
|
|
|
- return await this.db.update(this.tableName, data);
|
|
|
+ // 判断t_type是否为费用
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ await transaction.update(this.tableName, data);
|
|
|
+ let m_tp = this.ctx.material.m_tp;
|
|
|
+ if (data.t_type === materialConst.t_type[1].value) {
|
|
|
+ m_tp = await this.calcMaterialMTp(transaction);
|
|
|
+ }
|
|
|
+ await transaction.commit();
|
|
|
+ return m_tp;
|
|
|
+ } catch (err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新新一期的quantity和截止上期金额并返回本期总金额
|
|
|
+ * @param transaction
|
|
|
+ * @param tid
|
|
|
+ * @param mid
|
|
|
+ * @returns {Promise<number>}
|
|
|
+ */
|
|
|
async updateNewMaterial(transaction, tid, mid) {
|
|
|
const materialBillsData = await this.getAllDataByCondition({ where: { tid } });
|
|
|
let m_tp = 0;
|
|
@@ -112,6 +147,50 @@ module.exports = app => {
|
|
|
await transaction.update(this.tableName, updateData);
|
|
|
return await this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, mb.m_spread), 2);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改 expr和quantity值,返回本期金额和单条数据
|
|
|
+ * @param data
|
|
|
+ * @returns {Promise<void>}
|
|
|
+ */
|
|
|
+ async updateFYQuantity(data) {
|
|
|
+ if (!this.ctx.tender || !this.ctx.material) {
|
|
|
+ throw '数据错误';
|
|
|
+ }
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ const mbInfo = await this.getDataById(data.id);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ await transaction.commit();
|
|
|
+ const returnData = {
|
|
|
+ m_tp,
|
|
|
+ info: await this.getDataById(data.id),
|
|
|
+ }
|
|
|
+ return returnData;
|
|
|
+ } catch (err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更改计算总金额并返回值
|
|
|
+ async calcMaterialMTp(transaction) {
|
|
|
+ // 金额发生变化,则重新计算本期金额
|
|
|
+ const sql = 'SELECT SUM(`m_spread`*`quantity`) as total_price FROM ' + this.tableName + ' WHERE `tid` = ?';
|
|
|
+ const sqlParam = [this.ctx.tender.id];
|
|
|
+ const tp = await transaction.queryOne(sql, sqlParam);
|
|
|
+ console.log(tp);
|
|
|
+ const updateData2 = {
|
|
|
+ id: this.ctx.material.id,
|
|
|
+ m_tp: tp.total_price,
|
|
|
+ };
|
|
|
+ await transaction.update(this.ctx.service.material.tableName, updateData2);
|
|
|
+ return tp.total_price;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return MaterialBills;
|