浏览代码

合同支付,在审批通过前,强制计算合同支付数据并更新数据库

MaiXinRong 1 年之前
父节点
当前提交
b77cea3939
共有 2 个文件被更改,包括 16 次插入9 次删除
  1. 3 3
      app/service/stage_audit.js
  2. 13 6
      app/service/stage_pay.js

+ 3 - 3
app/service/stage_audit.js

@@ -424,8 +424,10 @@ module.exports = app => {
             const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
             const stageInfo = await this.ctx.service.stage.getDataById(stageId);
 
-            const transaction = await this.db.beginTransaction();
+            // 计算并合同支付最终数据
+            const [yfPay, sfPay, stagePays] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 
+            const transaction = await this.db.beginTransaction();
             try {
                 const ledgerTp = await this._updateTender(transaction);
                 // 添加通知
@@ -460,8 +462,6 @@ module.exports = app => {
                     end_time: time,
                 });
                 await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
-                // 计算并合同支付最终数据
-                const [yfPay, sfPay, stagePays] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
                 const stageTp = {
                     contract_tp: tpData.contract_tp,
                     qc_tp: tpData.qc_tp,

+ 13 - 6
app/service/stage_pay.js

@@ -144,6 +144,7 @@ module.exports = app => {
             const stagePays = [];
             const prePays = await this.getStageLastestPays(preStage);
             for (const pp of prePays) {
+                console.log(pp);
                 const p = this._.find(pays, {id: pp.pid});
                 if (!p.valid) continue;
                 stagePays.push({
@@ -263,9 +264,8 @@ module.exports = app => {
          * @return {Promise<Array>} 计算结果(应付和实付数据)
          */
         async calcAllStagePays(stage, transaction) {
-            if (!stage || !transaction) {
-                throw '计算数据错误';
-            }
+            if (!stage) throw '计算数据错误';
+
             const stagePays = await this.getStagePays(stage);
             const PayCalculator = require('../lib/pay_calc');
             const payCalculator = new PayCalculator(this.ctx, stage, this.ctx.tender.info);
@@ -286,9 +286,16 @@ module.exports = app => {
                     rprice: sp.rprice,
                 });
             }
-            await transaction.updateRows(this.tableName, update);
-            if (srUpdate.length > 0) {
-                await transaction.updateRows(this.ctx.service.pay.tableName, srUpdate);
+            const conn = transaction || await this.db.beginTransaction();
+            try {
+                await conn.updateRows(this.tableName, update);
+                if (srUpdate.length > 0) {
+                    await conn.updateRows(this.ctx.service.pay.tableName, srUpdate);
+                }
+                if (!transaction) await conn.commit();
+            } catch (err) {
+                if (!transaction) await conn.rollback();
+                throw '计算合同支付数据错误';
             }
             const yf = this._.find(stagePays, {ptype: payConst.payType.yf});
             const sf = this._.find(stagePays, {ptype: payConst.payType.sf});