瀏覽代碼

删除合同支付项,更新相关应用

MaiXinRong 2 年之前
父節點
當前提交
776d7dc8a3
共有 3 個文件被更改,包括 82 次插入5 次删除
  1. 1 1
      app/controller/stage_controller.js
  2. 3 0
      app/lib/pay_calc.js
  3. 78 4
      app/service/stage_pay.js

+ 1 - 1
app/controller/stage_controller.js

@@ -1026,7 +1026,7 @@ module.exports = app => {
                         responseData.data = await ctx.service.stagePay.getStagePay(ctx.stage, responseData.data.pid);
                         break;
                     case 'del':
-                        await ctx.service.pay.del(data.id);
+                        await ctx.service.stagePay.del(data.id);
                         responseData.data = await ctx.service.stagePay.getStagePays(ctx.stage);
                         await payCalculator.calculateAll(responseData.data);
                         await this._updateStageCache(ctx, payCalculator);

+ 3 - 0
app/lib/pay_calc.js

@@ -54,14 +54,17 @@ class PayCalculate {
 
     _calculateTpExpr(pay, pays) {
         let formula = pay.expr;
+        const bPrint = (pay.expr === 'f18+f12');
         const orderParam = pay.expr.match(this.orderReg);
         if (orderParam) {
             for (const op of orderParam) {
                 const order = parseInt(op.substring(1, op.length));
                 const orderPay = pays.find(x => { return x.order === order });
+                if (bPrint) console.log(order, orderPay);
                 formula = formula.replace(op, orderPay && orderPay.tp || 0);
             }
         }
+        if (bPrint) console.log(formula);
         for (const b of this.bases) {
             if ((b.code === 'bqwc' || b.code === 'bqht') && (!pay.pre_used && pay.sprice)) {
                 switch (b.code) {

+ 78 - 4
app/service/stage_pay.js

@@ -343,13 +343,11 @@ module.exports = app => {
             return await transaction.query(sql, sqlParam);
         }
 
-        _newExprAfterChangeOrder(expr, orderPart1, orderPart2) {
-            if (!expr) return [false, expr];
-
+        _splitExprByOrder(expr) {
             const orderParam = [...expr.matchAll(/f\d+/ig)];
             if (orderParam.length === 0) return [false, expr];
 
-            const exprPart = [], newExprPart = [];
+            const exprPart = [];
             for (const [i, op] of orderParam.entries()) {
                 if (i === 0) {
                     exprPart.push(expr.substring(0, op.index));
@@ -360,6 +358,16 @@ module.exports = app => {
                 exprPart.push(op[0]);
                 if (i === orderParam.length - 1) exprPart.push(expr.substring(op.index + op[0].length, expr.length));
             }
+            return [true, exprPart];
+        }
+
+        _newExprAfterChangeOrder(expr, orderPart1, orderPart2) {
+            if (!expr) return [false, expr];
+            const [hasOrder, exprPart] = this._splitExprByOrder(expr);
+            if (!hasOrder) return [false, expr];
+
+            const newExprPart = [];
+
             let change = false;
             for (const ep of exprPart) {
                 if (ep === orderPart1) {
@@ -411,6 +419,72 @@ module.exports = app => {
                 throw err;
             }
         }
+
+        _newExprAfterDelete(expr, orderParts) {
+            if (!expr) return [false, expr];
+            const [hasOrder, exprPart] = this._splitExprByOrder(expr);
+            if (!hasOrder) return [false, expr];
+
+            const newExprPart = [];
+
+            let change = false;
+            for (const ep of exprPart) {
+                const orderPart = orderParts.find(x => {return x.orgOrder === ep; });
+                if (orderPart) {
+                    change = true;
+                    newExprPart.push(orderPart.newOrder);
+                } else {
+                    newExprPart.push(ep);
+                }
+            }
+            return [change, newExprPart.join('')];
+        }
+
+        async del(id) {
+            if (!this.ctx.tender || !this.ctx.stage) throw '数据错误';
+            // 检查是否可以删除
+            const pay = await this.ctx.service.pay.getDataByCondition({id: id});
+            if (!pay) {
+                throw '合同支付项不存在';
+            } else if (pay.ptype !== payConst.payType.normal) {
+                throw '该合同支付项不可删除';
+            }
+
+            const stagePays = await this.getStagePays(this.ctx.stage);
+            const orderParts = [], updateStageData = [], updateData = [];
+            for (const sp of stagePays) {
+                if (sp.order === pay.order) {
+                    orderParts.push({ orgOrder: `f${sp.order}`, newOrder: '#ref!'});
+                } else if (sp.order > pay.order) {
+                    orderParts.push({ orgOrder: `f${sp.order}`, newOrder: `f${sp.order-1}`});
+                } else {
+                    updateData.push({id: sp.pid, order: sp.order -1 });
+                }
+            }
+            for (const sp of stagePays) {
+                const [change, newExpr] = this._newExprAfterDelete(sp.expr, orderParts);
+                if (change) {
+                    updateStageData.push({ condition: { where: { sid: this.ctx.stage.id, pid: sp.pid } }, update: { expr: newExpr } });
+                }
+            }
+
+            // 删除合同支付
+            const transaction = await this.db.beginTransaction();
+            try {
+                // 假删除
+                const result = await transaction.update(this.ctx.service.pay.tableName, { id: id, valid: false });
+                if (result.affectedRows !== 1) throw '删除合同支付项失败';
+                await transaction.updateRows(this.ctx.service.pay.tableName, updateData);
+                for (const usd of updateStageData) {
+                    await transaction.update(this.tableName, usd.update, usd.condition);
+                }
+                await transaction.commit();
+                return true;
+            } catch(err) {
+                await transaction.rollback();
+                throw err;
+            }
+        }
     }
 
     return StagePay;