Browse Source

多期同时计量

MaiXinRong 1 year ago
parent
commit
6e361de286

+ 24 - 21
app/controller/revise_controller.js

@@ -410,29 +410,32 @@ module.exports = app => {
                 // 已计量
                 if (revise.uid === ctx.session.sessionUser.accountId &&
                     (revise.status === audit.revise.status.uncheck || revise.status === audit.revise.status.checkNo)) {
-                    const lastStage = await ctx.service.stage.getLastestStage(ctx.tender.id, true);
-                    if (lastStage) {
-                        if (lastStage.status === audit.stage.status.checked) {
-                            const usedPreBills = await ctx.service.stageBillsFinal.getUsedBills(ctx.tender.id, lastStage.order);
-                            for (const b of reviseBills) {
-                                b.used = usedPreBills.indexOf(b.id) >= 0;
-                            }
-                            const usedPrePos = await ctx.service.stagePosFinal.getUsedPos(ctx.tender.id, lastStage.order);
-                            for (const p of revisePos) {
-                                p.used = usedPrePos.indexOf(p.id) >= 0;
-                            }
-                        } else {
-                            const usedPreBills = lastStage.order > 1 ? await ctx.service.stageBillsFinal.getUsedBills(ctx.tender.id, lastStage.order - 1) : [];
-                            const usedCurBills = await ctx.service.stageBills.getStageUsedBills(ctx.tender.id, lastStage.id);
-                            for (const b of reviseBills) {
-                                b.used = usedPreBills.indexOf(b.id) >= 0 || usedCurBills.indexOf(b.id) >= 0;
-                            }
-                            const usedPrePos = lastStage.order > 1 ? await ctx.service.stagePosFinal.getUsedPos(ctx.tender.id, lastStage.order - 1) : [];
-                            const usedCurPos = await ctx.service.stagePos.getStageUsedPos(ctx.tender.id, lastStage.id);
-                            for (const p of revisePos) {
-                                p.used = usedPrePos.indexOf(p.id) >= 0 || usedCurPos.indexOf(p.id) >= 0;
+                    const stages = await ctx.service.stage.getAllDataByCondition({
+                        where: { tid: ctx.tender.id },
+                        orders: [['order', 'desc']],
+                    });
+                    if (stages.length > 0) {
+                        const usedBills = [], usedPos = [];
+                        for (const s of stages) {
+                            if (s.status === audit.stage.status.checked) {
+                                const usedPreBills = await ctx.service.stageBillsFinal.getUsedBills(ctx.tender.id, s.order);
+                                usedBills.push(...usedPreBills);
+                                const usedPrePos = await ctx.service.stagePosFinal.getUsedPos(ctx.tender.id, s.order);
+                                usedPos.push(...usedPrePos);
+                                break;
+                            } else {
+                                const usedCurBills = await ctx.service.stageBills.getStageUsedBills(ctx.tender.id, s.id);
+                                usedBills.push(...usedCurBills);
+                                const usedCurPos = await ctx.service.stagePos.getStageUsedPos(ctx.tender.id, s.id);
+                                usedPos.push(...usedCurPos);
                             }
                         }
+                        for (const b of reviseBills) {
+                            b.used = usedBills.indexOf(b.id) >= 0;
+                        }
+                        for (const p of revisePos) {
+                            p.used = usedPos.indexOf(p.id) >= 0;
+                        }
                     }
                 }
                 // 结算状态

+ 8 - 17
app/controller/stage_controller.js

@@ -272,7 +272,7 @@ module.exports = app => {
             // 结算状态
             const settleStatus = ctx.stage.readySettle ? await ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: ctx.stage.readySettle.id }}) : [];
             // 查询截止上期数据
-            const preStageData = ctx.stage.order > 1 ? await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, ctx.stage.order - 1) : [];
+            const preStageData = ctx.stage.preCheckedStage ? await ctx.service.stageBillsFinal.getFinalData(ctx.tender.data, ctx.stage.preCheckedStage.order) : [];
             this.ctx.helper.assignRelaData(ledgerData, [
                 { data: dgnData, fields: ['deal_dgn_qty1', 'deal_dgn_qty2', 'c_dgn_qty1', 'c_dgn_qty2'], prefix: '', relaId: 'id' },
                 { data: memoData, fields: this.ledgerMemoColumn, prefix: '', relaId: 'id' },
@@ -308,7 +308,7 @@ module.exports = app => {
             // 结算状态
             const settleStatus = ctx.stage.readySettle ? await ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: ctx.stage.readySettle.id }}) : [];
             // 查询截止上期数据
-            const preStageData = ctx.stage.order > 1 ? await ctx.service.stagePosFinal.getFinalData(ctx.tender.data, ctx.stage.order - 1) : [];
+            const preStageData = ctx.stage.preCheckedStage ? await ctx.service.stagePosFinal.getFinalData(ctx.tender.data, ctx.stage.preCheckedStage.order) : [];
             this.ctx.helper.assignRelaData(posData, [
                 { data: memoData, fields: this.posMemoColumn, prefix: '', relaId: 'id'},
                 { data: extraData, fields: this.posExtraColumn, prefix: '', relaId: 'id'},
@@ -487,23 +487,14 @@ module.exports = app => {
                 condition.tid = ctx.tender.id;
                 const responseData = { err: 0, msg: '', data: {} };
 
-                let curStageData,
-                    preStageData;
                 responseData.data = await ctx.service.pos.getPosDataWithAddStageOrder(condition);
                 // 根据当前人,或指定对象查询数据
                 const curWhere = JSON.parse(ctx.request.body.data);
-                if (ctx.stage.readOnly) {
-                    curStageData = await ctx.service.stagePos.getAuditorStageData2(ctx.tender.id,
-                        ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder, curWhere);
-                } else {
-                    curStageData = await ctx.service.stagePos.getLastestStageData2(ctx.tender.id, ctx.stage.id, curWhere);
-                }
+                const curStageData = ctx.stage.readOnly
+                    ? await ctx.service.stagePos.getAuditorStageData2(ctx.tender.id, ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder, curWhere)
+                    : await ctx.service.stagePos.getLastestStageData2(ctx.tender.id, ctx.stage.id, curWhere);
                 // 查询截止上期数据
-                if (ctx.stage.order > 1) {
-                    preStageData = await ctx.service.stagePosFinal.getFinalData(ctx.tender.data, ctx.stage.order - 1);
-                } else {
-                    preStageData = [];
-                }
+                const preStageData = ctx.stage.preCheckedStage ? await ctx.service.stagePosFinal.getFinalData(ctx.tender.data, ctx.stage.preCheckedStage.order) : [];
                 this.ctx.helper.assignRelaData(responseData.data, [
                     { data: curStageData, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty', 'postil'], prefix: '', relaId: 'pid' },
                     { data: preStageData, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'pre_', relaId: 'pid' },
@@ -974,8 +965,8 @@ module.exports = app => {
                 renderData.calcBase = await ctx.service.stage.getStagePayCalcBase(ctx.stage, ctx.tender.info);
                 renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.stage.pay);
                 renderData.whiteList = this.ctx.app.config.multipart.whitelist;
-                if (ctx.stage.order > 1) {
-                    renderData.pre = await ctx.service.stageBillsFinal.getSumTotalPrice(ctx.stage.tid, ctx.stage.order - 1);
+                if (ctx.stage.preCheckedStage) {
+                    renderData.pre = await ctx.service.stageBillsFinal.getSumTotalPrice(ctx.stage.tid, ctx.stage.preCheckedStage.order);
                     renderData.pre.gather_tp = ctx.helper.add(renderData.pre.contract_tp, renderData.pre.qc_tp);
                 } else {
                     renderData.pre = { contract_tp: null, qc_tp: null, gather_tp: null };

+ 1 - 1
app/lib/budget_final.js

@@ -175,7 +175,7 @@ class BudgetFinal {
             const curBills = stage.readOnly
                 ? await this.ctx.service.stageBills.getAuditorStageData2(id, stage.id, stage.curTimes, stage.curOrder)
                 : await this.ctx.service.stageBills.getLastestStageData2(id, stage.id);
-            const preBills = stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData({id}, stage.order - 1) : [];
+            const preBills = stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData({id}, stage.preCheckedStage.order) : [];
             const bpcData = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
             helper.assignRelaData(bills, [
                 { data: curBills, fields: ['contract_tp', 'qc_tp'], prefix: '', relaId: 'lid' },

+ 1 - 1
app/lib/pay_calc.js

@@ -157,7 +157,7 @@ class PayCalculate {
     async _getAddCalcRela() {
         if (this.cur && this.add) return;
 
-        this.pre = this.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getSumTotalPrice(this.stage.tid, this.stage.order - 1) : {};
+        this.pre = this.stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getSumTotalPrice(this.stage.tid, this.stage.preCheckedStage.order) : {};
         this.cur = await this.ctx.service.stageBills.getSumTotalPrice(this.stage);
         this.add = {};
         if (this.pre) {

+ 71 - 2
app/lib/revise_price.js

@@ -335,6 +335,63 @@ class revisePriceCalc {
             { id: stage.id, ...pcTp, check_calc: true, cache_time_l: new Date() });
         return pcTp;
     }
+    async _calcStageWithoutPc(stage, bills, transaction) {
+        // 无单价变更不执行
+        if (this.price.length === 0) return;
+
+        const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
+
+        // 加载树结构
+        this.ctx.helper.assignRelaData(bills, [
+            { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'positive_qc_qty', 'negative_qc_qty', 'times', 'order', 'postil'], prefix: 'cur_', relaId: 'lid' },
+        ]);
+        const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
+        billsTree.loadDatas(bills);
+        const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
+
+        // 计算 insertBills/updateBills StageChange
+        const result = { ibData: [], ubData: [], scData: [] };
+        const helper = this.ctx.helper;
+        const decimal = this.ctx.tender.info.decimal;
+        const said = this.ctx.session.sessionUser.accountId;
+        billsTree.calculateAll(node => {
+            if (node.children && node.children.length > 0) return;
+            // const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
+            // if (!priceDiff) return;
+            if (node.cur_id && (node.cur_contract_qty || node.cur_qc_qty)) {
+                const cur_contract_tp = helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp);
+                const cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
+                const cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
+                const cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
+                if (cur_contract_tp !== node.cur_contract_tp || cur_qc_tp !== node.cur_qc_tp || cur_positive_qc_tp !== node.cur_positive_qc_tp || cur_negative_qc_tp !== node.cur_positive_qc_tp) {
+                    if (node.cur_times === stage.times && node.cur_order === 0) {
+                        result.ubData.push({
+                            id: node.cur_id,
+                            contract_tp: cur_contract_tp, qc_tp: cur_qc_tp,
+                            positive_qc_tp: cur_positive_qc_tp, negative_qc_tp: cur_negative_qc_tp,
+                        });
+                    } else {
+                        result.ibData.push({
+                            tid: stage.tid, sid: stage.id, said,
+                            lid: node.id, times: stage.times, order: 0,
+                            contract_qty: node.cur_contract_qty, contract_tp: cur_contract_tp,
+                            qc_qty: node.cur_qc_qty, qc_tp: cur_qc_tp,
+                            positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: cur_positive_qc_tp,
+                            negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: cur_negative_qc_tp,
+                            postil: node.cur_postil,
+                        });
+                    }
+                }
+            }
+            const scDetail = stageChange.filter(x => { return x.lid === node.id; });
+            for (const scd of scDetail) {
+                result.scData.push({ id: scd.id, unit_price: node.unit_price });
+            }
+        });
+        if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
+        if (result.ubData.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, result.ubData);
+        if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
+    }
     /**
      * 计算修订台账
      * @param {Object}revise - 最新一次台账修订(此处不检查)
@@ -371,8 +428,20 @@ class revisePriceCalc {
         if (pos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, pos);
 
         // 应用到未审完成期
-        const latestStage = await this.ctx.service.stage.getLastestStage(revise.tid, true);
-        if (latestStage && latestStage.status !== audit.stage.status.checked) return await this._calcStage(latestStage, xmj, transaction);
+        const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: revise.tid }, order: ['order', 'asc'] });
+        const latestStage = stages[stages.length - 1];
+        let pcTp;
+        if (latestStage && latestStage.status !== audit.stage.status.checked) {
+            for (const s of stages) {
+                if (s.status === audit.stage.status.checked) continue;
+                if (!pcTp) {
+                    pcTp = await this._calcStage(latestStage, xmj, transaction);
+                } else {
+                    await this._calcStageWithoutPc(latestStage, xmj, transaction);
+                }
+            }
+        }
+        return pcTp;
     }
     async calcRevise(revise, transaction) {
         if (revise.tid !== this.ctx.tender.id) throw '数据错误';

+ 3 - 3
app/lib/rptCustomData.js

@@ -235,7 +235,7 @@ class jhHelper {
         let billsData = await this.ctx.service.ledger.getData(tender.id);
         if (filterGcl) billsData = billsData.filter(x => { return x.b_code && x.is_leaf });
         const curStage = await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
-        const preStage = stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];
+        const preStage = stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
         const loadData = [
             { data: curStage, fields: this.billsQueryField, prefix: '', relaId: 'lid' },
             { data: preStage, fields: this.billsQueryField, prefix: 'pre_', relaId: 'lid' }
@@ -254,7 +254,7 @@ class jhHelper {
         const helper = this.ctx.helper;
         const posData = await this.ctx.service.pos.getPosData({tid: tender.id});
         const curStage = await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
-        const preStage = stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.order - 1) : [];
+        const preStage = stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
         const loadData = [
             { data: curStage, fields: ['qc_qty'], prefix: '', relaId: 'pid' },
             { data: preStage, fields: ['qc_qty'], prefix: 'pre_', relaId: 'pid' }
@@ -526,7 +526,7 @@ class fjHelper {
             ? await this.ctx.helper.loadLedgerDataFromOss(this.ctx.stage.ledgerHis.bills_file)
             : await this.ctx.service.ledger.getData(this.ctx.tender.id);
 
-        const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
+        const preStage = this.ctx.stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.preCheckedStage.order) : [];
         const curStage = this.ctx.stage.readOnly
             ? await this.ctx.service.stageBills.getAuditorStageData2(tid, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder)
             : await this.ctx.service.stageBills.getLastestStageData2(tid, this.ctx.stage.id);

+ 2 - 2
app/lib/stage_im.js

@@ -77,7 +77,7 @@ class StageIm {
             ? await this.ctx.service.stageBills.getAuditorStageData2(this.ctx.tender.id,
                 this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder)
             : await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
-        const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
+        const preStage = this.ctx.stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.preCheckedStage.order) : [];
         const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.ctx.stage.id } });
 
         this.ctx.helper.assignRelaData(billsData, [
@@ -95,7 +95,7 @@ class StageIm {
             ? await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id,
                 this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder)
             : await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
-        const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
+        const prePosStage = this.ctx.stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.preCheckedStage.order) : [];
         this.ctx.helper.assignRelaData(posData, [
             { data: curPosStage, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: '', relaId: 'pid' },
             { data: prePosStage, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'pre_', relaId: 'pid' },

+ 1 - 1
app/lib/tender_info.js

@@ -45,7 +45,7 @@ class TenderInfo {
                 ? await this.ctx.service.stageBills.getAuditorStageData2(this.tender.id, this.stage.id, this.stage.curTimes, this.stage.curOrder)
                 : await this.ctx.service.stageBills.getLastestStageData2(this.tender.id, this.stage.id);
 
-            const preStage = this.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.tender, this.stage.order - 1) : [];
+            const preStage = this.stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(this.tender, this.stage.preCheckedStage.order) : [];
             const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.stage.id } });
             this.ctx.helper.assignRelaData(billsData, [
                 { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },

+ 1 - 0
app/middleware/stage_check.js

@@ -48,6 +48,7 @@ module.exports = options => {
 
             // 读取原报、审核人数据
             yield this.service.stage.loadStageUser(stage);
+            yield this.service.stage.loadPreCheckedStage(stage);
 
             // 历史台账
             if (stage.status === status.checked) {

+ 1 - 0
app/public/js/revise.js

@@ -915,6 +915,7 @@ $(document).ready(() => {
                     info.cancel = posRange && posRange.length > 0;
                     break;
                 case 'unit_price':
+                    console.log(node);
                     info.cancel = (node.children && node.children.length > 0) || node.used || node.settle_status === settleStatus.finish;
                     break;
                 case 'sgfh_qty':

+ 6 - 6
app/service/report_memory.js

@@ -393,7 +393,7 @@ module.exports = app => {
             const helper = this.ctx.helper;
             const tender = this.ctx.tender;
             const stage = this.ctx.stage;
-            const preBglData = stage.order > 1 ? await this.ctx.service.stageChangeFinal.getFinalChangeBills(tender.id, stage.order - 1) : [];
+            const preBglData = stage.preCheckedStage ? await this.ctx.service.stageChangeFinal.getFinalChangeBills(tender.id, stage.preCheckedStage.order) : [];
             for (const node of billsTree.nodes) {
                 node.pre_qc_bgl_code = '';
                 node.pre_qc_bgl_arr = [];
@@ -535,7 +535,7 @@ module.exports = app => {
                     }
                 }
                 if (this._checkFieldsExist(fields, billsFields.stageEnd)) {
-                    preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
+                    preStage = this.ctx.stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.preCheckedStage.order) : [];
                 }
                 const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.ctx.stage.id } });
                 const endBpcStage = await this.ctx.service.stageBillsPc.getEndStageData(this.ctx.stage);
@@ -625,7 +625,7 @@ module.exports = app => {
                     }
                 }
                 if (this._checkFieldsExist(fields, posFields.stageEnd)) {
-                    prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
+                    prePosStage = this.ctx.stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.preCheckedStage.order) : [];
                 }
                 const changeData = this._checkFieldsExistReg(fields, 'due_')
                     ? await this.ctx.service.changeAuditList.getPosSum(this.ctx.tender.id)
@@ -715,8 +715,8 @@ module.exports = app => {
 
                 const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.ctx.stage.id } });
                 const endBpcStage = await this.ctx.service.stageBillsPc.getEndStageData(this.ctx.stage);
-                const preStage = this._checkFieldsExist(fields, billsFields.stageEnd) && this.ctx.stage.order > 1
-                    ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1)
+                const preStage = this._checkFieldsExist(fields, billsFields.stageEnd) && this.ctx.stage.preCheckedStage
+                    ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.preCheckedStage.order)
                     : [];
 
                 this.ctx.helper.assignRelaData(billsData, [
@@ -779,7 +779,7 @@ module.exports = app => {
 
                 const allStagePos = await this.ctx.service.stagePos.getAllDataByCondition({where: {sid: sid}});
                 const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }});
-                const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
+                const prePosStage = this.ctx.stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.preCheckedStage.order) : [];
                 this.ctx.helper.assignRelaData(posData, [
                     {data: prePosStage, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'pre_', relaId: 'pid'}
                 ]);

+ 6 - 6
app/service/rpt_gather_memory.js

@@ -611,7 +611,7 @@ module.exports = app => {
                 const curStage = stage.readOnly
                     ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
                     : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
-                const preStage = hasPre && stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.order - 1) : [];
+                const preStage = hasPre && stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
                 const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
                 this.ctx.helper.assignRelaData(billsData, [
                     { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
@@ -622,7 +622,7 @@ module.exports = app => {
                 const curStagePos = stage.readOnly
                     ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
                     : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
-                const preStagePos = hasPre && stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.order - 1) : [];
+                const preStagePos = hasPre && stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
                 this.ctx.helper.assignRelaData(posData, [
                     { data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
                     { data: preStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
@@ -1354,7 +1354,7 @@ module.exports = app => {
 
         async _gatherStageJgcl(tender, stage) {
             const data = await this.ctx.service.stageJgcl.getStageData(stage);
-            const preData = stage.order > 1 ? await this.ctx.service.stageJgcl.getPreStageData(stage.tid, stage.order) : 0;
+            const preData = stage.preCheckedStage ? await this.ctx.service.stageJgcl.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
             for (const d of data) {
                 const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
                 if (pd) {
@@ -1479,7 +1479,7 @@ module.exports = app => {
 
         async _gatherStageOther(tender, stage) {
             const data = await this.ctx.service.stageOther.getStageData(stage);
-            const preData = stage.order > 1 ? await this.ctx.service.stageOther.getPreStageData(stage.tid, stage.order) : [];
+            const preData = stage.preCheckedStage ? await this.ctx.service.stageOther.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
             for (const d of data) {
                 const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
                 if (pd) {
@@ -1539,7 +1539,7 @@ module.exports = app => {
 
         async _gatherStageSafeProd(tender, stage) {
             const data = await this.ctx.service.stageSafeProd.getStageData(stage);
-            const preData = stage.order > 1 ? await this.ctx.service.stageSafeProd.getPreStageData(stage.tid, stage.order) : [];
+            const preData = stage.preCheckedStage ? await this.ctx.service.stageSafeProd.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
             for (const d of data) {
                 const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
                 if (pd) {
@@ -1618,7 +1618,7 @@ module.exports = app => {
 
         async _gatherStageTempLand(tender, stage) {
             const data = await this.ctx.service.stageTempLand.getStageData(stage);
-            const preData = stage.order > 1 ? await this.ctx.service.stageTempLand.getPreStageData(stage.tid, stage.order) : [];
+            const preData = stage.preCheckedStage ? await this.ctx.service.stageTempLand.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
             for (const d of data) {
                 const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
                 if (pd) {

+ 1 - 1
app/service/settle.js

@@ -104,7 +104,7 @@ module.exports = app => {
         async addSettle(tenderId, date, period) {
             const settles = await this.getAllDataByCondition({
                 where: { tid: tenderId },
-                order: ['settle_order', 'asc'],
+                orders: [['settle_order', 'asc']],
             });
             const pre = settles[settles.length - 1];
             if (settles.length > 0 && pre.audit_status !== auditConst.settle.status.checked) {

+ 43 - 20
app/service/stage.js

@@ -80,6 +80,8 @@ module.exports = app => {
             stage.curAuditorIds = this._.map(stage.curAuditors, 'aid');
             stage.flowAuditors = stage.curAuditors.length > 0 ? stage.auditors.filter(x => { return x.order === stage.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
             stage.flowAuditorIds = this._.map(stage.flowAuditors, 'aid');
+            stage.nextAuditors = stage.curAuditors.length > 0 ? stage.auditors.filter(x => { return x.order === stage.curAuditors[0].order + 1; }) : [];
+            stage.nextAuditorIds = this._.map(stage.nextAuditors, 'aid');
             stage.auditorGroups = this.ctx.helper.groupAuditors(stage.auditors);
             stage.userGroups = this.ctx.helper.groupAuditorsUniq(stage.auditorGroups);
             stage.userGroups.unshift([{
@@ -124,9 +126,23 @@ module.exports = app => {
             }
         }
 
+        async loadPreCheckedStage(stage) {
+            if (stage.order > 1) {
+                const preCheckedStages = await this.ctx.service.stage.getAllDataByCondition({
+                    where: { tid: stage.tid, status: auditConst.stage.status.checked },
+                    orders: [['order', 'desc']],
+                });
+                stage.preCheckedStage = preCheckedStages[0];
+            } else {
+                stage.preCheckedStage = undefined;
+            }
+            stage.isCheckFirst = stage.order > 1 ? (stage.preCheckedStage ? stage.preCheckedStage.order === stage.order - 1 : false) : true;
+        }
+
         async doCheckStage(stage, force = false) {
             const status = auditConst.stage.status;
             await this.loadStageUser(stage);
+            await this.loadPreCheckedStage(stage);
 
             const accountId = this.ctx.session.sessionUser.accountId, shareIds = [];
             const isTenderTourist = await this.service.tenderTourist.getDataByCondition({ tid: stage.tid, user_id: accountId });
@@ -408,12 +424,17 @@ module.exports = app => {
                     if (sf && s.readOnly) {
                         await this.ctx.service.stage.update({ sf_tp: sf.tp, pre_sf_tp: sf.pre_tp }, { id: s.id });
                     }
-                    s.sf_tp = sf.tp;
+                    s.sf_tp = sf ? sf.tp : 0;
                 }
             }
             return stages;
         }
 
+        async getNextStages(tenderId, order) {
+            const sql = 'SELECT * FROM ?? WHERE tid = ? AND `order` > ?';
+            return await this.db.query(sql, [this.tableName, tenderId, order]);
+        }
+
         async _getSumTp(condition, ...field) {
             const fieldSql = [];
             for (const f of field) {
@@ -434,12 +455,10 @@ module.exports = app => {
         async addStage(tenderId, date, period) {
             const stages = await this.getAllDataByCondition({
                 where: { tid: tenderId },
-                order: ['order'],
+                order: [['order', 'asc']],
             });
             const preStage = stages[stages.length - 1];
-            if (stages.length > 0 && stages[stages.length - 1].status !== auditConst.stage.status.checked) {
-                throw '上一期未审批通过,请等待上一期审批通过后,再新增数据';
-            }
+            const preCheckedStage = stages.find(x => { return x.status === auditConst.stage.status.checked; });
             const order = stages.length + 1;
             const newStage = {
                 sid: this.uuid.v4(),
@@ -460,16 +479,18 @@ module.exports = app => {
                 newStage.im_pre = preStage.im_pre;
                 newStage.im_gather = preStage.im_gather;
                 newStage.im_gather_node = preStage.im_gather_node;
-                newStage.pre_contract_tp = this.ctx.helper.sum([preStage.pre_contract_tp, preStage.contract_tp, preStage.contract_pc_tp]);
-                newStage.pre_qc_tp = this.ctx.helper.sum([preStage.pre_qc_tp, preStage.qc_tp, preStage.qc_pc_tp]);
-                newStage.pre_positive_qc_tp = this.ctx.helper.sum([preStage.pre_positive_qc_tp, preStage.positive_qc_tp, preStage.positive_qc_pc_tp]);
-                newStage.pre_negative_qc_tp = this.ctx.helper.sum([preStage.pre_negative_qc_tp, preStage.negative_qc_tp, preStage.negative_qc_pc_tp]);
-                newStage.pre_yf_tp = this.ctx.helper.add(preStage.pre_yf_tp, preStage.yf_tp);
-                if (preStage.order === 1 || preStage.pre_sf_tp) {
-                    newStage.pre_sf_tp = this.ctx.helper.add(preStage.pre_sf_tp, preStage.sf_tp);
-                } else {
-                    const sumTp = await this._getSumTp({tid: preStage.tid}, 'sf_tp');
-                    newStage.pre_sf_tp = sumTp.sf_tp || 0;
+                if (preCheckedStage) {
+                    newStage.pre_contract_tp = this.ctx.helper.sum([preCheckedStage.pre_contract_tp, preCheckedStage.contract_tp, preCheckedStage.contract_pc_tp]);
+                    newStage.pre_qc_tp = this.ctx.helper.sum([preCheckedStage.pre_qc_tp, preStage.qc_tp, preCheckedStage.qc_pc_tp]);
+                    newStage.pre_positive_qc_tp = this.ctx.helper.sum([preCheckedStage.pre_positive_qc_tp, preCheckedStage.positive_qc_tp, preCheckedStage.positive_qc_pc_tp]);
+                    newStage.pre_negative_qc_tp = this.ctx.helper.sum([preCheckedStage.pre_negative_qc_tp, preCheckedStage.negative_qc_tp, preCheckedStage.negative_qc_pc_tp]);
+                    newStage.pre_yf_tp = this.ctx.helper.add(preCheckedStage.pre_yf_tp, preStage.yf_tp);
+                    if (preCheckedStage.order === 1 || preCheckedStage.pre_sf_tp) {
+                        newStage.pre_sf_tp = this.ctx.helper.add(preCheckedStage.pre_sf_tp, preCheckedStage.sf_tp);
+                    } else {
+                        const sumTp = await this._getSumTp({tid: preCheckedStage.tid}, 'sf_tp');
+                        newStage.pre_sf_tp = sumTp.sf_tp || 0;
+                    }
                 }
             } else {
                 const projFunRela = await this.ctx.service.project.getFunRela(this.ctx.session.sessionProject.id);
@@ -498,16 +519,18 @@ module.exports = app => {
                 }
                 // 新增期其他台账数据
                 let pcTp = { contract_pc_tp: 0, qc_pc_tp: 0, pc_tp: 0, positive_qc_pc_tp: 0, negative_qc_pc_tp: 0 };
-                if (preStage) {
-                    const jgclResult = await this.ctx.service.stageJgcl.addInitialStageData(newStage, preStage, transaction);
+                if (preCheckedStage) {
+                    const jgclResult = await this.ctx.service.stageJgcl.addInitialStageData(newStage, preCheckedStage, transaction);
                     if (!jgclResult) throw '初始化甲供材料数据失败';
-                    const otherResult = await this.ctx.service.stageOther.addInitialStageData(newStage, preStage, transaction);
+                    const otherResult = await this.ctx.service.stageOther.addInitialStageData(newStage, preCheckedStage, transaction);
                     if (!otherResult) throw '初始化其他台账数据失败';
-                    const safeResult = await this.ctx.service.stageSafeProd.addInitialStageData(newStage, preStage, transaction);
+                    const safeResult = await this.ctx.service.stageSafeProd.addInitialStageData(newStage, preCheckedStage, transaction);
                     if (!safeResult) throw '初始化其他台账数据失败';
-                    const tempResult = await this.ctx.service.stageTempLand.addInitialStageData(newStage, preStage, transaction);
+                    const tempResult = await this.ctx.service.stageTempLand.addInitialStageData(newStage, preCheckedStage, transaction);
                     if (!tempResult) throw '初始化其他台账数据失败';
+                }
 
+                if (preStage && preCheckedStage && preStage.order === preCheckedStage.order) {
                     const priceCalc = new RevisePrice(this.ctx);
                     pcTp = await priceCalc.newStagePriceChange(newStage, preStage, transaction);
                 }

+ 21 - 3
app/service/stage_audit.js

@@ -461,7 +461,7 @@ module.exports = app => {
                 });
                 await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
                 // 计算并合同支付最终数据
-                const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
+                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,
@@ -563,6 +563,20 @@ module.exports = app => {
 
                         await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, nextAudits, flowAudits, ledgerTp, stageTp);
                         await this.ctx.service.stagePay.cacheOrder(this.ctx.stage, transaction);
+                        // 当前期不是最新一起时
+                        if (this.ctx.stage.highOrder !== this.ctx.stage.order) {
+                            const nextStages = await this.ctx.service.stage.getNextStages(this.ctx.stage.tid, this.ctx.stage.order);
+                            await this.ctx.service.stagePay.refreshNextStagePreTp(transaction, nextStages, stagePays);
+                            const preStageTp = {
+                                pre_contract_tp: this.ctx.helper.sum([this.ctx.stage.pre_contract_tp, stageTp.contract_tp, stageTp.contract_pc_tp]),
+                                pre_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_qc_tp, stageTp.qc_tp, stageTp.qc_pc_tp]),
+                                pre_positive_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_positive_qc_tp, stageTp.positive_qc_tp, stageTp.positive_qc_pc_tp]),
+                                pre_negative_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_negative_qc_tp, stageTp.negative_qc_tp, stageTp.negative_qc_pc_tp]),
+                                pre_yf_tp: this.ctx.helper.add(this.ctx.stage.pre_yf_tp, stageTp.yf_tp),
+                                pre_sf_tp: this.ctx.helper.add(this.ctx.stage.pre_sf_tp, stageTp.sf_tp),
+                            };
+                            await transaction.update(this.ctx.service.stage.tableName, nextStages.map(x => { return { id: x.id, ...preStageTp }; }));
+                        }
 
                         // 添加短信通知-审批通过提醒功能
                         const auditList = await this.getAuditors(stageId, stageInfo.times);
@@ -1679,8 +1693,12 @@ module.exports = app => {
                 };
                 newAuditors.push(na);
             }
-            const result = await transaction.insert(this.tableName, newAuditors);
-            return (result.effectRows = auditors.length);
+            if (newAuditors.length > 0) {
+                const result = await transaction.insert(this.tableName, newAuditors);
+                return (result.effectRows = auditors.length);
+            } else {
+                return true;
+            }
         }
 
         /**

+ 23 - 4
app/service/stage_pay.js

@@ -95,8 +95,8 @@ module.exports = app => {
             }
             const stagePays = [];
             // 获取截止上期数据
-            if (stage.order > 1) {
-                const preStage = await this.ctx.service.stage.getDataByCondition({tid: stage.tid, order: stage.order - 1});
+            if (stage.preCheckedStage) {
+                const preStage = await this.ctx.service.stage.getDataByCondition({tid: stage.tid, order: stage.preCheckedStage.order});
                 if (!preStage) {
                     throw '标段数据有误';
                 }
@@ -263,7 +263,7 @@ module.exports = app => {
             }
             const yf = this._.find(stagePays, {ptype: payConst.payType.yf});
             const sf = this._.find(stagePays, {ptype: payConst.payType.sf});
-            return [yf, sf];
+            return [yf, sf, stagePays];
         }
 
         async getSpecialTotalPrice(stage) {
@@ -283,7 +283,7 @@ module.exports = app => {
             const stagePays = stage.status === auditConst.stage.status.checked
                 ? await this.getStageLastestPays(stage)
                 : await this.getStagePays(stage);
-            const sf = this._.find(stagePays, {ptype: payConst.payType.sf});
+            const sf = this._.find(stagePays, { ptype: payConst.payType.sf });
             return sf;
         }
 
@@ -505,6 +505,25 @@ module.exports = app => {
         async clearCacheOrder(stage, transaction) {
             await transaction.update(this.tableName, { porder: 0 }, { where: { sid: stage.id } });
         }
+
+        async refreshNextStagePreTp(transaction, nextStages, prePays) {
+            const updateData = [];
+            for (const s of nextStages) {
+                const curPays = await this.getStageLastestPays(s);
+                for (const cp of curPays) {
+                    const pp = prePays.find(x => { return x.pid === cp.pid });
+                    if (pp) {
+                        updateData.push({
+                            id: cp.id, pre_tp: pp.end_tp,
+                            pre_used: pp.pre_used || !this.ctx.helper.checkZero(pp.tp),
+                            pre_finish: pp.pre_finish || (pp.rprice ? pp.end_tp === pp.rprice : false),
+                            start_stage_order: pp.start_stage_order,
+                        });
+                    }
+                }
+            }
+            if (updateData.length > 0) await transaction.update(this.tableName, updateData);
+        }
     }
 
     return StagePay;

+ 4 - 0
app/service/tender_cache.js

@@ -227,6 +227,8 @@ module.exports = app => {
         }
 
         async updateStageCache4Start(transaction, stage, status, auditors, ledgerTp, stageTp) {
+            if (!stage.isCheckFirst) return;
+
             const orgCache = await this.getDataById(stage.tid);
             const data = { id: stage.tid, stage_status: status };
             if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);
@@ -248,6 +250,8 @@ module.exports = app => {
         }
 
         async updateStageCache4Flow(transaction, stage, status, auditors, preAuditors, ledgerTp, stageTp, pcTp) {
+            if (!stage.isCheckFirst) return;
+
             const orgCache = await this.getDataById(stage.tid);
             const data = { id: stage.tid, stage_status: status };
             if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);

+ 1 - 2
app/view/measure/stage.ejs

@@ -10,8 +10,7 @@
                 <% if (ctx.session.sessionProject.page_show.openSettle) { %>
                 <a href="/tender/<%= ctx.tender.id %>/settle" class="btn btn-primary btn-sm">计量结算</a>
                 <% } %>
-                <% if (ctx.session.sessionUser.accountId === ctx.tender.data.user_id && ctx.tender.data.ledger_status === auditConst.status.checked &&
-                        (stages.length === 0 || stages[0].status === auditConst.status.checked)) { %>
+                <% if (ctx.session.sessionUser.accountId === ctx.tender.data.user_id && ctx.tender.data.ledger_status === auditConst.status.checked) { %>
                     <% if (!ctx.session.sessionProject.page_show.close1stStageCheckDealParam && ctx.helper.checkZero(ctx.tender.info.deal_param.contractPrice) && stages.length === 0) { %>
                         <a href="#add-qi" data-toggle="modal" data-target="#tips" class="btn btn-primary btn-sm">开始新一期</a>
                     <% } else { %>

+ 1 - 2
app/view/measure/stage_modal.ejs

@@ -1,5 +1,4 @@
-<% if (ctx.session.sessionUser.accountId === ctx.tender.data.user_id && ctx.tender.data.ledger_status === auditConst.status.checked &&
-        (stages.length === 0 || stages[stages.length- 1].status === auditConst.status.checked)) { %>
+<% if (ctx.session.sessionUser.accountId === ctx.tender.data.user_id && ctx.tender.data.ledger_status === auditConst.status.checked) { %>
 <!--弹出填写合同参数-->
 <div class="modal fade" id="tips" data-backdrop="static">
     <div class="modal-dialog" role="document">

+ 5 - 0
app/view/stage/audit_btn.ejs

@@ -1,15 +1,20 @@
 <div class="contarl-box">
     <% if (ctx.stage.status === auditConst.status.uncheck) { %>
     <% if (ctx.session.sessionUser.accountId === ctx.stage.user_id) { %>
+
+    <% if (ctx.stage.order > 1 && ctx.stage.preCheckedStage && ctx.stage.preCheckedStage.order === ctx.stage.order - 1) { %>
     <% if (!ctx.session.sessionProject.page_show.openStageStart || (ctx.session.sessionProject.page_show.openStageStart && ctx.stage.startPermission)) { %>
     <a id="sub-sp-btn" href="javascript: void(0);" data-toggle="modal" data-target="#sub-sp" class="btn btn-primary btn-sm btn-block">上报审批</a>
     <% } else { %>
     <a href="javascript:void(0);" data-toggle="tooltip" data-placement="right" class="btn btn-secondary btn-sm btn-block" data-original-title="不在上报时间范围内">上报审批</a>
     <% } %>
+    <% } %>
+
     <% if (ctx.session.sessionProject.page_show.openStageStart && ctx.stage.startPermission) { %><p class="text-warning text-center">截止:<%- ctx.stage.startEndDay %></p><% } %>
     <% } else { %>
     <a id="sub-sp-btn" href="javascript: void(0);" data-toggle="modal" data-target="#sub-sp" class="btn btn-outline-secondary btn-sm btn-block">上报中</a>
     <% } %>
+
     <% } else if (ctx.stage.status === auditConst.status.checking) { %>
         <% if (ctx.stage.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) >= 0) { %>
             <a id="sp-done-btn" href="javascript: void(0);" data-toggle="modal" data-target="#sp-done" class="btn btn-success btn-sm btn-block">审批通过</a>

+ 1 - 1
app/view/stage/pay.ejs

@@ -58,7 +58,7 @@
 <script>
     const tender = JSON.parse(unescape('<%- escape(JSON.stringify(tender)) %>'));
     const stage = JSON.parse(unescape('<%- escape(JSON.stringify(ctx.stage)) %>'));
-    const readOnly = <%- stage.readOnly || stage.revising %>;
+    const readOnly = <%- stage.readOnly || stage.revising || (stage.order > 1 && (!stage.preCheckedStage || stage.preCheckedStage.order < stage.order - 1)) %>;
     const dealPay = JSON.parse(unescape('<%- escape(JSON.stringify(dealPay)) %>'));
     const calcBase = JSON.parse('<%- JSON.stringify(calcBase) %>');
     const decimal = JSON.parse('<%- JSON.stringify(ctx.tender.info.decimal) %>');