Browse Source

计算计量金额方式修正

laiguoran 4 years ago
parent
commit
b6bcc3cb2c

+ 34 - 19
app/controller/schedule_controller.js

@@ -184,8 +184,7 @@ module.exports = app => {
          */
         async loadTpLedgerData(ctx) {
             try {
-                const highOrder = await ctx.service.scheduleStage.count({ tid: ctx.tender.id });
-                const ledgerData = await this._getStageLedgerData(ctx, ctx.params.order, highOrder);
+                const ledgerData = await this._getStageLedgerData(ctx, ctx.params.order);
                 const postData = { ledgerData };
                 const data = JSON.parse(ctx.request.body.data);
                 if (data.filter && data.filter === 'gcl') {
@@ -225,7 +224,7 @@ module.exports = app => {
          * @return {Promise<void>}
          */
         async _getAllStageLedgerData(ctx) {
-            const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
+            let ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
             const stageList = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['order', 'desc']] });
             if (stageList.length > 0) {
                 const dgnData = await ctx.service.stageBillsDgn.getDgnData(ctx.tender.id);
@@ -252,12 +251,13 @@ module.exports = app => {
                         // { data: preStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: s.order + '_pre_', relaId: 'lid' },
                     ]);
                 }
+                ledgerData = await this._addFinalStageData(ctx, ledgerData);
             }
             return ledgerData;
         }
 
-        async _getStageLedgerData(ctx, stageOrder, highOrder) {
-            const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
+        async _getStageLedgerData(ctx, stageOrder) {
+            let ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
             const dgnData = await ctx.service.stageBillsDgn.getDgnData(ctx.tender.id);
             for (const d of dgnData) {
                 const l = ctx.app._.find(ledgerData, { id: d.id });
@@ -267,7 +267,7 @@ module.exports = app => {
                 tid: ctx.tender.id,
                 order: stageOrder,
             });
-            let preStageData, prefinalStageData;
+            let preStageData;
             // 当前操作人查看最新数据,其他人查看历史数据
             const curStageData = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, stageInfo.id);
             // 查询截止上期数据
@@ -281,20 +281,35 @@ module.exports = app => {
                 { data: preStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: 'pre_', relaId: 'lid' },
             ]);
             // 获取进度最新期的数据
-            const finalStageInfo = await ctx.service.stage.getDataByCondition({
-                tid: ctx.tender.id,
-                order: highOrder,
-            });
-            const finalStageData = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, finalStageInfo.id);
-            if (finalStageInfo.order > 1) {
-                prefinalStageData = await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, finalStageInfo.order - 1);
-            } else {
-                prefinalStageData = [];
+            ledgerData = await this._addFinalStageData(ctx, ledgerData);
+            return ledgerData;
+        }
+
+        /**
+         * 添加最新一期的进度期下期数据到台账数据中(Ajax)
+         *
+         * @param ctx
+         * @return {Promise<void>}
+         */
+        async _addFinalStageData(ctx, ledgerData) {
+            const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['order', 'desc']] });
+            if (scheduleStage && scheduleStage.length > 0) {
+                let prefinalStageData;
+                const finalStageInfo = await ctx.service.stage.getDataByCondition({
+                    tid: ctx.tender.id,
+                    order: scheduleStage[0].order,
+                });
+                const finalStageData = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, finalStageInfo.id);
+                if (finalStageInfo.order > 1) {
+                    prefinalStageData = await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, finalStageInfo.order - 1);
+                } else {
+                    prefinalStageData = [];
+                }
+                this.ctx.helper.assignRelaData(ledgerData, [
+                    { data: finalStageData, fields: ['contract_qty', 'contract_expr', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: 'final_', relaId: 'lid' },
+                    { data: prefinalStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: 'pre_final_', relaId: 'lid' },
+                ]);
             }
-            this.ctx.helper.assignRelaData(ledgerData, [
-                { data: finalStageData, fields: ['contract_qty', 'contract_expr', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: 'final_', relaId: 'lid' },
-                { data: prefinalStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'used'], prefix: 'pre_final_', relaId: 'lid' },
-            ]);
             return ledgerData;
         }
 

+ 26 - 6
app/public/js/schedule_ledger.js

@@ -64,9 +64,13 @@ $(function () {
 
     postData(window.location.pathname + '/load', {}, function (data) {
         const showList = ['is_select', 'can_select', 'ledger_id', 'ledger_pid', 'order', 'level', 'tender_id', 'full_path',
-            'code', 'name', 'unit', 'dgn_qty1', 'dgn_qty2', 'dgn_price', 'quantity', 'total_price'];
-        const calcList = ['total_price'];
-        const fileds = [];
+            'code', 'name', 'unit', 'dgn_qty1', 'dgn_qty2', 'dgn_price', 'quantity', 'total_price',
+            'final_contract_tp', 'final_qc_tp', 'pre_final_contract_tp', 'pre_final_qc_tp', 'pre_final_gather_tp', 'final_gather_tp', 'end_final_gather_tp'
+        ];
+        const calcList = ['total_price',
+            'final_contract_tp', 'final_qc_tp', 'pre_final_contract_tp', 'pre_final_qc_tp', 'pre_final_gather_tp', 'final_gather_tp', 'end_final_gather_tp'
+        ];
+        const fileds = ['final_contract_tp', 'final_qc_tp'];
         if (scheduleStage.length > 0) {
             for (const ss of scheduleStage) {
                 // const pushTp = [ss.order + '_gather_tp', ss.order + '_contract_tp', ss.order + '_qc_tp'];
@@ -96,6 +100,9 @@ $(function () {
                         node[ss.order + '_gather_tp'] = ZhCalc.add(node[ss.order + '_contract_tp'], node[ss.order + '_qc_tp']);
                     }
                 }
+                node.pre_final_gather_tp = ZhCalc.add(node.pre_final_contract_tp, node.pre_final_qc_tp);
+                node.final_gather_tp = ZhCalc.add(node.final_contract_tp, node.final_qc_tp);
+                node.end_final_gather_tp = ZhCalc.add(node.pre_final_gather_tp, node.final_gather_tp);
             }
         });
         slh = data.slh;
@@ -222,7 +229,9 @@ $(function () {
         const [under_ledger, parent_ledger] = getPAndULedger(select_ledger);
         // 重新计算每一期计量进度金额并提交
         const stageTpList = calcStageTp(under_ledger);
-        postData(window.location.pathname + '/save',  { select_ledger: _.map(select_ledger, 'ledger_id'), under_ledger, parent_ledger, stageTpList, type: 'sz' } , function (result) {
+        const stageSjTp = calcStageSjTp(under_ledger);
+        console.log(under_ledger, stageSjTp);
+        postData(window.location.pathname + '/save',  { select_ledger: _.map(select_ledger, 'ledger_id'), under_ledger, parent_ledger, stageTpList, stageSjTp, type: 'sz' } , function (result) {
             toastr.success('设置成功');
             setTimeout(function () {
                 window.location.reload();
@@ -254,7 +263,9 @@ $(function () {
         const [under_ledger, parent_ledger] = getPAndULedger(select_ledger);
         // 重新计算每一期计量进度金额并提交
         const stageTpList = calcStageTp(under_ledger);
-        postData(window.location.pathname + '/save',  { select_ledger: _.map(select_ledger, 'ledger_id'), under_ledger, parent_ledger, stageTpList, type: 'xz' } , function (result) {
+        const stageSjTp = calcStageSjTp(under_ledger);
+        console.log(stageSjTp);
+        postData(window.location.pathname + '/save',  { select_ledger: _.map(select_ledger, 'ledger_id'), under_ledger, parent_ledger, stageTpList, stageSjTp, type: 'xz' } , function (result) {
             toastr.success('进度台账数据修正成功');
             setTimeout(function () {
                 window.location.reload();
@@ -268,7 +279,7 @@ $(function () {
         for (const sl of select_ledger) {
             const hadChild = _.find(select_ledger, { ledger_pid: sl.ledger_id });
             if (!hadChild) {
-                const pushArray = { ledger_id: sl.ledger_id, ledger_pid: sl.ledger_pid, gcl: sl.dgn_qty1, tp: sl.total_price };
+                const pushArray = { ledger_id: sl.ledger_id, ledger_pid: sl.ledger_pid, gcl: sl.dgn_qty1, tp: sl.total_price, end_final_gather_tp: sl.end_final_gather_tp };
                 if (scheduleStage.length > 0) {
                     for (const ss of scheduleStage) {
                         pushArray[ss.order + '_gather_tp'] = sl[ss.order + '_gather_tp'];
@@ -357,6 +368,15 @@ function calcStageTp(list) {
     }
     return false;
 }
+
+function calcStageSjTp(list) {
+    let stageSjTp = 0;
+    for (const l of list) {
+        stageSjTp = ZhCalc.add(stageSjTp, l.end_final_gather_tp);
+    }
+    return stageSjTp;
+}
+
 function addIsSelect(datas) {
     // const newDatas = [];
     for (const d of datas) {

+ 3 - 1
app/public/js/schedule_stage_tp.js

@@ -233,6 +233,8 @@ function calcGatherTpAndUpdate(tree) {
         for (const t of tree) {
             if (t.is_leaf && t.gather_tp) {
                 total_tp = ZhCalc.add(total_tp, t.gather_tp);
+            }
+            if (t.is_leaf && t.end_final_gather_tp) {
                 final_total_tp = ZhCalc.add(final_total_tp, t.end_final_gather_tp);
             }
         }
@@ -240,7 +242,7 @@ function calcGatherTpAndUpdate(tree) {
         console.log(schedule.stage_sj_tp, final_total_tp);
         if (curScheduleStage.tp !== total_tp || schedule.stage_sj_tp !== final_total_tp) {
             // 更新计量总金额并更新金额模式总金额
-            postData('/tender/' + getTenderId() + '/schedule/stage/save', { type: 'update_tp', postData: { tp: total_tp, stage_sj_tpL: final_total_tp, order: curScheduleStage.order } }, function (data) {
+            postData('/tender/' + getTenderId() + '/schedule/stage/save', { type: 'update_tp', postData: { tp: total_tp, stage_sj_tp: final_total_tp, order: curScheduleStage.order } }, function (data) {
                 console.log('总金额已更新');
             })
         }

+ 2 - 1
app/service/schedule_ledger.js

@@ -121,8 +121,9 @@ module.exports = app => {
                 // 判断是否已存在计量进度,并更新计量统计数据,再更新总的统计数据
                 if (datas.stageTpList) {
                     await transaction.updateRows(this.ctx.service.scheduleStage.tableName, datas.stageTpList);
-                    await this.ctx.service.scheduleStage.calcStageSjTp(transaction, this.ctx.tender.id);
+                    // await this.ctx.service.scheduleStage.calcStageSjTp(transaction, this.ctx.tender.id);
                 }
+                await this.ctx.service.scheduleStage.updateStageSjTp(transaction, this.ctx.tender.id, datas.stageSjTp);
                 await transaction.commit();
                 return true;
             } catch (err) {

+ 14 - 11
app/service/schedule_stage.js

@@ -96,17 +96,7 @@ module.exports = app => {
                     },
                 };
                 await transaction.update(this.tableName, updateData, option);
-                // 更新计量总金额
-                const stageData = {
-                    stage_sj_tp: data.stage_sj_tp,
-                };
-                const stageOption = {
-                    where: {
-                        tid: this.ctx.tender.id,
-                    },
-                };
-                await transaction.update(this.ctx.service.schedule.tableName, stageData, stageOption);
-                // await this.calcStageSjTp(transaction, this.ctx.tender.id);
+                await this.updateStageSjTp(transaction, this.ctx.tender.id, data.stage_sj_tp);
                 await transaction.commit();
                 return true;
             } catch (err) {
@@ -129,6 +119,19 @@ module.exports = app => {
             };
             return await transaction.update(this.ctx.service.schedule.tableName, updateData, option);
         }
+
+        async updateStageSjTp(transaction, tid, sj_tp) {
+            // 更新计量总金额
+            const stageData = {
+                stage_sj_tp: sj_tp,
+            };
+            const stageOption = {
+                where: {
+                    tid,
+                },
+            };
+            await transaction.update(this.ctx.service.schedule.tableName, stageData, stageOption);
+        }
     }
     return ScheduleStage;
 };