Explorar o código

期、审批流程数据相关

MaiXinRong %!s(int64=6) %!d(string=hai) anos
pai
achega
3d480eb377

+ 52 - 95
app/controller/stage_controller.js

@@ -109,24 +109,13 @@ module.exports = app => {
         }
 
         /**
-         * 获取期数据
+         * 获取审批界面所需的 原报、审批人数据等
          * @param ctx
          * @returns {Promise<void>}
          * @private
          */
-        async _getStage(ctx) {
-            const stageOrder = parseInt(ctx.params.order);
-            ctx.stage = await ctx.service.stage.getDataByCondition({
-                tid: ctx.tender.id,
-                order: stageOrder,
-            });
-            if (ctx.stage.status === auditConst.status.uncheck && ctx.stage.user_id !== ctx.session.sessionUser.accountId) {
-                throw '数据错误';
-            }
+        async _getStageAuditViewData (ctx) {
             const times = ctx.stage.status === auditConst.status.checkNo ? ctx.stage.times - 1 : ctx.stage.times;
-            ctx.stage.auditors = await ctx.service.stageAudit.getAuditors(ctx.stage.id, times);
-            ctx.stage.curAuditor = await ctx.service.stageAudit.getCurAuditor(ctx.stage.id, times);
-            ctx.stage.flowOrder = ctx.stage.curAuditor ? ctx.stage.curAuditor.order : 0;
             ctx.stage.user = await ctx.service.projectAccount.getAccountInfoById(ctx.stage.user_id);
             ctx.stage.auditHistory = [];
             if (ctx.stage.times > 1) {
@@ -148,12 +137,17 @@ module.exports = app => {
          */
         async index(ctx) {
             try {
-                await this._getStage(ctx);
+                await this._getStageAuditViewData(ctx);
                 const renderData = await this._getDefaultRenderData(ctx);
                 [renderData.ledgerSpread, renderData.posSpread] = this._getSpreadSetting();
                 renderData.changeConst = changeConst;
                 renderData.ledgerData = await ctx.service.ledger.getDataByTenderId(ctx.tender.id, -1);
-                renderData.curStageData = await ctx.service.stageBills.getAuditorStageData(ctx.tender.id, ctx.stage.id, ctx.stage.times, 0);
+                // 当前操作人查看最新数据,其他人查看历史数据
+                if (ctx.stage.readOnly) {
+                    renderData.curStageData = await ctx.service.stageBills.getAuditorStageData(ctx.tender.id, ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder);
+                } else {
+                    renderData.curStageData = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, ctx.stage.id);
+                }
                 renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.stage.index);
                 // 查询截止上期数据
                 if (ctx.stage.order > 1) {
@@ -175,15 +169,18 @@ module.exports = app => {
          */
         async getStagePosData(ctx) {
             try {
-                await this._getStage(ctx);
                 const condition = JSON.parse(ctx.request.body.data) || {};
                 condition.tid = ctx.tender.id;
                 const responseData = {err: 0, msg: '', data: {}};
                 responseData.data.pos = await ctx.service.pos.getPosData(condition);
                 // 根据当前人,或指定对象查询数据
-                responseData.data.curStageData = await ctx.service.stagePos.getAuditorStageData(ctx.tender.id,
-                    ctx.stage.id, ctx.stage.times, ctx.stage.curAuditor ? ctx.stage.curAuditor.order : 0,
-                    this.app._.map(responseData.data.pos, 'id'));
+                if (ctx.stage.readOnly) {
+                    responseData.data.curStageData = await ctx.service.stagePos.getAuditorStageData(ctx.tender.id,
+                        ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder, this.app._.map(responseData.data.pos, 'id'));
+                } else {
+                    responseData.data.curStageData = await ctx.service.stagePos.getLastestStageData(ctx.tender.id,
+                        ctx.stage.id, this.app._.map(responseData.data.pos, 'id'));
+                }
                 // 查询截止上期数据
                 if (ctx.stage.order > 1) {
                     responseData.data.preStageData = await ctx.service.stagePosFinal.getFinalData(ctx.tender.data, ctx.stage);
@@ -204,15 +201,9 @@ module.exports = app => {
          */
         async updateStageData(ctx) {
             try {
-                await this._getStage(ctx);
                 // 检查登录用户,是否可操作
-                if (ctx.session.sessionUser.accountId === ctx.stage.user_id) {
-                    if (ctx.stage.status === auditConst.status.checking || ctx.stage.status === auditConst.status.checked) {
-                        throw '该计量期当前您无权操作';
-                    }
-                } else {
-                    // 检查是否可修改
-                    //const curAuditor = await ctx.service.stageAudit.get
+                if (ctx.stage.readOnly) {
+                    throw '该计量期当前您无权操作';
                 }
 
                 const data = JSON.parse(ctx.request.body.data);
@@ -236,7 +227,7 @@ module.exports = app => {
          */
         async detail(ctx) {
             try {
-                await this._getStage(ctx);
+                await this._getStageAuditViewData(ctx);
                 const renderData = await this._getDefaultRenderData(ctx);
                 renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.stage.detail);
                 renderData.imType = tenderConst.imType;
@@ -254,12 +245,9 @@ module.exports = app => {
          */
         async buildDetailData (ctx) {
             try {
-                await this._getStage(ctx);
                 // 检查登录用户,是否可操作
-                if (ctx.session.sessionUser.accountId === ctx.stage.user_id) {
-                    if (ctx.stage.status === auditConst.status.checking || ctx.stage.status === auditConst.status.checked) {
-                        throw '该计量期当前您无权操作';
-                    }
+                if (ctx.stage.readOnly) {
+                    throw '该计量期当前您无权操作';
                 }
 
                 const data = JSON.parse(ctx.request.body.data);
@@ -279,29 +267,26 @@ module.exports = app => {
          */
         async loadDetailRelaData(ctx) {
             try {
-                await this._getStage(ctx);
                 const data = JSON.parse(ctx.request.body.data);
                 // 加载台账数据
                 if (data.loadType === 'ledger') {
                     const ledgerData = await ctx.service.ledger.getDataByTenderId(ctx.tender.id, -1);
                     ctx.body = {err: 0, msg: '', data: ledgerData };
                 } else if (data.loadType === 'all') {
-                    const ledger = await ctx.service.ledger.getDataByTenderId(ctx.tender.id, -1);
-                    const curStage = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, ctx.stage.id);
-                    const stageDetail = await ctx.service.stageDetail.getLastestStageData(ctx.tender.id, ctx.stage.id);
-                    ctx.body = {
-                        err: 0,
-                        msg: '',
-                        data: {
-                            ledger,
-                            curStage,
-                            stageDetail,
-                        }
-                    };
+                    const result = {};
+                    result.ledger = await ctx.service.ledger.getDataByTenderId(ctx.tender.id, -1);
+                    if (ctx.stage.readOnly) {
+                        result.curStage = await ctx.service.stageBills.getAuditorStageData(ctx.tender.id, ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder);
+                        result.stageDetail = await ctx.service.stageDetail.getAuditorStageData(ctx.tender.id, ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder);
+                    } else {
+                        result.curStage = await ctx.service.stageBills.getLastestStageData(ctx.tender.id, ctx.stage.id);
+                        result.stageDetail = await ctx.service.stageDetail.getLastestStageData(ctx.tender.id, ctx.stage.id);
+                    }
+                    ctx.body = { err: 0, msg: '', data: result };
                 }
             } catch (err) {
                 this.log(err);
-                ctx.body = {err: 1, msg: err.toString(), data: null};
+                ctx.body = { err: 1, msg: err.toString(), data: null };
             }
         }
 
@@ -312,12 +297,9 @@ module.exports = app => {
          */
         async setAdvancedConfig(ctx) {
             try {
-                await this._getStage(ctx);
                 // 检查登录用户,是否可操作
-                if (ctx.session.sessionUser.accountId === ctx.stage.user_id) {
-                    if (ctx.stage.status === auditConst.status.checking || ctx.stage.status === auditConst.status.checked) {
-                        throw '该计量期当前您无权操作';
-                    }
+                if (ctx.stage.readOnly) {
+                    throw '该计量期当前您无权操作';
                 }
 
                 const data = JSON.parse(ctx.request.body.data);
@@ -337,15 +319,9 @@ module.exports = app => {
          */
         async saveDetailData(ctx) {
             try {
-                await this._getStage(ctx);
                 // 检查登录用户,是否可操作
-                if (ctx.session.sessionUser.accountId === ctx.stage.user_id) {
-                    if (ctx.stage.status === auditConst.status.checking || ctx.stage.status === auditConst.status.checked) {
-                        throw '该计量期当前您无权操作';
-                    }
-                } else {
-                    // 检查是否可修改
-                    //const curAuditor = await ctx.service.stageAudit.get
+                if (ctx.stage.readOnly) {
+                    throw '该计量期当前您无权操作';
                 }
 
                 const data = JSON.parse(ctx.request.body.data);
@@ -365,6 +341,9 @@ module.exports = app => {
          */
         async addCalcImage(ctx) {
             try {
+                if (ctx.stage.readOnly) {
+                    throw '该计量期当前您无权操作';
+                }
                 const stream = await ctx.getFileStream();
                 const create_time = Date.parse(new Date()) / 1000;
                 const fileInfo = path.parse(stream.filename);
@@ -384,15 +363,9 @@ module.exports = app => {
          */
         async mergeCalcImage(ctx) {
             try {
-                await this._getStage(ctx);
                 // 检查登录用户,是否可操作
-                if (ctx.session.sessionUser.accountId === ctx.stage.user_id) {
-                    if (ctx.stage.status === auditConst.status.checking || ctx.stage.status === auditConst.status.checked) {
-                        throw '该计量期当前您无权操作';
-                    }
-                } else {
-                    // 检查是否可修改
-                    //const curAuditor = await ctx.service.stageAudit.get
+                if (ctx.stage.readOnly) {
+                    throw '该计量期当前您无权操作';
                 }
                 const data = JSON.parse(ctx.request.body.data);
 
@@ -429,7 +402,7 @@ module.exports = app => {
          */
         async pay(ctx) {
             try {
-                await this._getStage(ctx);
+                await this._getStageAuditViewData(ctx);
                 const renderData = await this._getDefaultRenderData(ctx);
                 const dealPay = await ctx.service.stagePay.getStagePays(ctx.stage);
                 if (dealPay && dealPay.length > 0) {
@@ -459,16 +432,9 @@ module.exports = app => {
          */
         async savePayData(ctx) {
             try {
-                await this._getStage(ctx);
                 // 检查登录用户,是否可操作
-                if (ctx.session.sessionUser.accountId === ctx.stage.user_id) {
-                    if (ctx.stage.status === auditConst.status.checking || ctx.stage.status === auditConst.status.checked) {
-                        throw '该计量期当前您无权操作';
-                    }
-                } else {
-                    if (!ctx.stage.curAuditor || ctx.stage.curAuditor.id !== ctx.session.sessionUser.accountId) {
-                        throw '该计量期当前您无权操作';
-                    }
+                if (ctx.stage.readOnly) {
+                    throw '该计量期当前您无权操作';
                 }
 
                 const data = JSON.parse(ctx.request.body.data);
@@ -541,7 +507,6 @@ module.exports = app => {
             }
 
             try {
-                await this._getStage(ctx);
                 const chapterDetail = JSON.parse(JSON.stringify(payConst.chapterDetail));
                 const calcDetail = _.sortBy(chapterDetail, ['cType']);
                 for (const cd of calcDetail) {
@@ -602,11 +567,7 @@ module.exports = app => {
          */
         async change(ctx) {
             try {
-                await this._getStage(ctx);
                 const renderData = await this._getDefaultRenderData(ctx);
-                // renderData.changes = await ctx.service.change.getAllDataByCondition({
-                //     where: {tid: ctx.tender.id, status: changeAudit.status.checked},
-                // });
                 renderData.ledger = await ctx.service.ledger.getDataByTenderId(ctx.tender.id, -1);
                 renderData.changes = await ctx.service.change.getChangeAndUsedInfo(ctx.tender.id);
                 if (renderData.changes.length > 0) {
@@ -629,7 +590,10 @@ module.exports = app => {
          */
         async searchValidChange(ctx) {
             try {
-                await this._getStage(ctx);
+                // if (ctx.stage.readOnly) {
+                //     throw '您无权调用变更令';
+                // }
+
                 const data = JSON.parse(ctx.request.body.data);
                 if (!data.bills && !data.pos) {
                     throw '数据错误';
@@ -651,7 +615,10 @@ module.exports = app => {
          */
         async useChange(ctx) {
             try {
-                await this._getStage(ctx);
+                if (ctx.stage.readOnly) {
+                    throw '您无权使用变更令';
+                }
+
                 const data = JSON.parse(ctx.request.body.data);
                 if (!data.target || (!data.target.bills && !data.target.pos) || !data.change) {
                     throw '调用变更令数据错误'
@@ -675,7 +642,6 @@ module.exports = app => {
          */
         async changeDetail(ctx) {
             try {
-                await this._getStage(ctx);
                 const data = JSON.parse(ctx.request.body.data);
                 if (!data.cid) {
                     throw '查询数据错误';
@@ -697,7 +663,6 @@ module.exports = app => {
          */
         async addAudit(ctx) {
             try {
-                await this._getStage(ctx);
                 const data = JSON.parse(ctx.request.body.data);
                 const id = this.app._.toInteger(data.auditorId);
                 if (isNaN(id) || id <= 0) {
@@ -735,7 +700,6 @@ module.exports = app => {
          */
         async deleteAudit(ctx) {
             try {
-                await this._getStage(ctx);
                 const data = JSON.parse(ctx.request.body.data);
                 const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
                 if (isNaN(id) || id <= 0) {
@@ -760,8 +724,6 @@ module.exports = app => {
          */
         async startAudit(ctx) {
             try {
-                await this._getStage(ctx);
-
                 // 检查权限等
                 if (!ctx.stage) {
                     throw '数据错误';
@@ -789,7 +751,6 @@ module.exports = app => {
          */
         async checkAudit(ctx) {
             try {
-                await this._getStage(ctx);
                 if (!this.ctx.stage || (this.ctx.stage.status !== auditConst.status.checking && this.ctx.stage.status !== auditConst.status.checkNoPre)) {
                     throw '当前期数据有误';
                 }
@@ -858,7 +819,6 @@ module.exports = app => {
          */
         async gather(ctx) {
             try {
-                await this._getStage(ctx);
                 const renderData = await this._getDefaultRenderData(ctx);
                 [renderData.gclSpread, renderData.leafXmjSpread] = this._getGatherSpreadSetting();
                 renderData.ledger = await ctx.service.ledger.getDataByTenderId(ctx.tender.id, -1);
@@ -904,7 +864,6 @@ module.exports = app => {
                 return [ledger, pos];
             };
             try {
-                await this._getStage(ctx);
                 const renderData = await this._getDefaultRenderData(ctx);
                 [renderData.ledgerSpread, renderData.posSpread] = getCompareSpreadSetting();
                 renderData.ledger = await ctx.service.ledger.getDataByTenderId(ctx.tender.id, -1);
@@ -922,7 +881,6 @@ module.exports = app => {
 
         async compareAuditor(ctx) {
             try {
-                await this._getStage(ctx);
                 const data = JSON.parse(ctx.request.body.data);
                 const result = [];
                 for (const order of data.auditors) {
@@ -945,7 +903,6 @@ module.exports = app => {
          */
         async report(ctx) {
             try {
-                await this._getStage(ctx);
                 const renderData = await this._getDefaultRenderData(ctx);
                 await this.layout('stage/report.ejs', renderData, 'stage/report_modal.ejs');
             } catch (err) {

+ 48 - 6
app/middleware/stage_check.js

@@ -2,13 +2,13 @@
 
 /**
  *
- *
  * @author Mai
  * @date
  * @version
  */
 
-const auditConst = require('../const/audit').flow;
+const status = require('../const/audit').stage.status;
+const _ = require('lodash');
 
 module.exports = options => {
     /**
@@ -16,29 +16,71 @@ module.exports = options => {
      * 1. 读取期数据
      * 2. 检验用户是否参与期(不校验具体权限)
      *
+     * 写入ctx.stage数据
+     * 其中:
+     * stage.auditors: 审批人列表(退回原报时,加载上一流程)
+     * stage.curAuditor: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)
+     * stage.readonly: 登录人,是否可操作
+     * stage.curTimes: 当前登录人,操作、查阅数据times
+     * stage.curOrder: 当前登录人,操作、查阅数据order
+     *
+     * 该方法为通用方法,如需stage其他数据,请在controller中查询
+     *
      * @param {function} next - 中间件继续执行的方法
      * @return {void}
      */
     return function* stageCheck(next) {
         try {
             // 读取标段数据
-            const stageOrder = parseInt(this.params.id);
+            const stageOrder = parseInt(this.params.order);
             if (stageOrder <= 0) {
                 throw '您访问的期不存在';
             }
-            console.log(this.tender);
-            console.log(stageOrder);
             const stage = yield this.service.stage.getDataByCondition({
                 tid: this.tender.id,
                 order: stageOrder,
             });
-            console.log(stage);
             if (!stage) {
                 throw '期数据错误';
             }
+
+            // 读取原报、审核人数据
             stage.auditors = yield this.service.stageAudit.getAuditors(stage.id, stage.times);
             stage.curAuditor = yield this.service.stageAudit.getCurAuditor(stage.id, stage.times);
+
+            // 权限相关
             // todo 校验权限 (标段参与人、分享)
+            const accountId = this.session.sessionUser.accountId, auditorIds = _.map(stage.auditors, 'aid'), shareIds = [];
+            if (accountId === stage.user_id) { // 原报
+                stage.readOnly = stage.status !== status.uncheck && stage.status !== status.checkNo;
+                stage.curTimes = stage.status === status.checkNo ? stage.times + 1 : stage.times;
+                stage.curOrder = stage.status === status.checked ? _.max(_.map(stage.auditors, 'order')) : (stage.status !== status.checkNo ? stage.curAuditor.order - 1 : 0);
+            } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
+                if (stage.status === status.uncheck) {
+                    throw '您无权查看该数据';
+                }
+                stage.readOnly = (stage.status === status.checking || stage.status === status.checkNoPre) && accountId !== stage.curAuditor.aid;
+                stage.curTimes = stage.times;
+                if (stage.status === status.checked) {
+                    stage.curOrder = _.max(_.map(stage.auditors, 'order'));
+                } else if (stage.status === status.checkNo) {
+                    const audit = this.service.stageAudit.getDataByCondition({
+                        sid: stage.id, times: stage.times, status: status.checkNo
+                    });
+                    stage.curOrder = audit.order;
+                } else {
+                    stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
+                }
+            } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
+                if (stage.status === status.uncheck) {
+                    throw '您无权查看该数据';
+                }
+                stage.readOnly = true;
+                stage.curTimes = stage.times;
+                stage.curOrder = stage.status === status.checked ? _.max(_.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
+            } else { // 其他不可见
+                throw '您无权查看该数据';
+            }
             this.stage = stage;
             yield next;
         } catch (err) {

+ 1 - 1
app/public/js/stage_change.js

@@ -89,7 +89,7 @@ $(document).ready(() => {
             {title: '清单编号', colSpan: '1', rowSpan: '1', field: 'code', hAlign: 0, width: 80, formatter: '@', readOnly: true},
             {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 150, type: 'Number', readOnly: true},
             {title: '单位', colSpan: '1', rowSpan: '1', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
-            {title: '单价', colSpan: '1', rowSpan: '1', field: 'up', hAlign: 2, width: 60, type: 'Number', readOnly: true},
+            {title: '单价', colSpan: '1', rowSpan: '1', field: 'unit_price', hAlign: 2, width: 60, type: 'Number', readOnly: true},
             {title: '数量', colSpan: '1', rowSpan: '1', field: 'qty', hAlign: 2, width: 60, formatter: '@', readOnly: true},
             {title: '金额', colSpan: '1', rowSpan: '1', field: 'tp', hAlign: 2, width: 60, formatter: '@', readOnly: true},
             {title: '已变更', colSpan: '1', rowSpan: '1', field: 'used_qty', hAlign: 2, width: 60, type: 'Number', readOnly: true},

+ 24 - 24
app/router.js

@@ -124,36 +124,36 @@ module.exports = app => {
 
     // 期计量详细
     // 本期计量台账
-    app.get('/tender/:id/measure/stage/:order', sessionAuth, tenderCheck, 'stageController.index');
-    app.post('/tender/:id/measure/stage/:order/pos', sessionAuth, tenderCheck, 'stageController.getStagePosData');
-    app.post('/tender/:id/measure/stage/:order/update', sessionAuth, tenderCheck, 'stageController.updateStageData');
-    app.post('/tender/:id/measure/stage/:order/valid-change', sessionAuth, tenderCheck, 'stageController.searchValidChange');
-    app.post('/tender/:id/measure/stage/:order/use-change',sessionAuth, tenderCheck, 'stageController.useChange');
+    app.get('/tender/:id/measure/stage/:order', sessionAuth, tenderCheck, stageCheck, 'stageController.index');
+    app.post('/tender/:id/measure/stage/:order/pos', sessionAuth, tenderCheck, stageCheck, 'stageController.getStagePosData');
+    app.post('/tender/:id/measure/stage/:order/update', sessionAuth, tenderCheck, stageCheck, 'stageController.updateStageData');
+    app.post('/tender/:id/measure/stage/:order/valid-change', sessionAuth, tenderCheck, stageCheck, 'stageController.searchValidChange');
+    app.post('/tender/:id/measure/stage/:order/use-change',sessionAuth, tenderCheck, stageCheck, 'stageController.useChange');
     // 中间计量
-    app.get('/tender/:id/measure/stage/:order/detail', sessionAuth, tenderCheck, 'stageController.detail');
-    app.post('/tender/:id/measure/stage/:order/detail/build', sessionAuth, tenderCheck, 'stageController.buildDetailData');
-    app.post('/tender/:id/measure/stage/:order/detail/adv', sessionAuth, tenderCheck, 'stageController.setAdvancedConfig');
-    app.post('/tender/:id/measure/stage/:order/detail/load', sessionAuth, tenderCheck, 'stageController.loadDetailRelaData');
-    app.post('/tender/:id/measure/stage/:order/detail/save', sessionAuth, tenderCheck, 'stageController.saveDetailData');
-    app.post('/tender/:id/measure/stage/:order/detail/add-img', sessionAuth, tenderCheck, 'stageController.addCalcImage');
-    app.post('/tender/:id/measure/stage/:order/detail/merge-img', sessionAuth, tenderCheck, 'stageController.mergeCalcImage');
+    app.get('/tender/:id/measure/stage/:order/detail', sessionAuth, tenderCheck, stageCheck, 'stageController.detail');
+    app.post('/tender/:id/measure/stage/:order/detail/build', sessionAuth, tenderCheck, stageCheck, 'stageController.buildDetailData');
+    app.post('/tender/:id/measure/stage/:order/detail/adv', sessionAuth, tenderCheck, stageCheck, 'stageController.setAdvancedConfig');
+    app.post('/tender/:id/measure/stage/:order/detail/load', sessionAuth, tenderCheck, stageCheck, 'stageController.loadDetailRelaData');
+    app.post('/tender/:id/measure/stage/:order/detail/save', sessionAuth, tenderCheck, stageCheck, 'stageController.saveDetailData');
+    app.post('/tender/:id/measure/stage/:order/detail/add-img', sessionAuth, tenderCheck, stageCheck, 'stageController.addCalcImage');
+    app.post('/tender/:id/measure/stage/:order/detail/merge-img', sessionAuth, tenderCheck, stageCheck, 'stageController.mergeCalcImage');
     // 合同支付
-    app.get('/tender/:id/measure/stage/:order/pay', sessionAuth, tenderCheck, 'stageController.pay');
-    app.post('/tender/:id/measure/stage/:order/pay/detail', sessionAuth, tenderCheck, 'stageController.chapterDetail');
-    app.post('/tender/:id/measure/stage/:order/pay/save', sessionAuth, tenderCheck, 'stageController.savePayData');
+    app.get('/tender/:id/measure/stage/:order/pay', sessionAuth, tenderCheck, stageCheck, 'stageController.pay');
+    app.post('/tender/:id/measure/stage/:order/pay/detail', sessionAuth, tenderCheck, stageCheck, 'stageController.chapterDetail');
+    app.post('/tender/:id/measure/stage/:order/pay/save', sessionAuth, tenderCheck, stageCheck, 'stageController.savePayData');
     // 变更令
-    app.get('/tender/:id/measure/stage/:order/change', sessionAuth, tenderCheck, 'stageController.change');
-    app.post('/tender/:id/measure/stage/:order/change/detail', sessionAuth, tenderCheck, 'stageController.changeDetail');
+    app.get('/tender/:id/measure/stage/:order/change', sessionAuth, tenderCheck, stageCheck, 'stageController.change');
+    app.post('/tender/:id/measure/stage/:order/change/detail', sessionAuth, tenderCheck, stageCheck, 'stageController.changeDetail');
     // 审批
-    app.post('/tender/:id/measure/stage/:order/audit/add', sessionAuth, tenderCheck, 'stageController.addAudit');
-    app.post('/tender/:id/measure/stage/:order/audit/delete', sessionAuth, tenderCheck, 'stageController.deleteAudit');
-    app.post('/tender/:id/measure/stage/:order/audit/start', sessionAuth, tenderCheck, 'stageController.startAudit');
-    app.post('/tender/:id/measure/stage/:order/audit/check', sessionAuth, tenderCheck, 'stageController.checkAudit');
+    app.post('/tender/:id/measure/stage/:order/audit/add', sessionAuth, tenderCheck, stageCheck, 'stageController.addAudit');
+    app.post('/tender/:id/measure/stage/:order/audit/delete', sessionAuth, tenderCheck, stageCheck, 'stageController.deleteAudit');
+    app.post('/tender/:id/measure/stage/:order/audit/start', sessionAuth, tenderCheck, stageCheck, 'stageController.startAudit');
+    app.post('/tender/:id/measure/stage/:order/audit/check', sessionAuth, tenderCheck, stageCheck, 'stageController.checkAudit');
     // 清单汇总
-    app.get('/tender/:id/measure/stage/:order/gather', sessionAuth, tenderCheck, 'stageController.gather');
+    app.get('/tender/:id/measure/stage/:order/gather', sessionAuth, tenderCheck, stageCheck, 'stageController.gather');
     // 审核比较
-    app.get('/tender/:id/measure/stage/:order/compare', sessionAuth, tenderCheck, 'stageController.compare');
-    app.post('/tender/:id/measure/stage/:order/compare/load', sessionAuth, tenderCheck, 'stageController.compareAuditor');
+    app.get('/tender/:id/measure/stage/:order/compare', sessionAuth, tenderCheck, stageCheck, 'stageController.compare');
+    app.post('/tender/:id/measure/stage/:order/compare/load', sessionAuth, tenderCheck, stageCheck, 'stageController.compareAuditor');
     // 报表
     app.get('/tender/:id/measure/stage/:order/report', sessionAuth, tenderCheck, 'stageController.report');
     // 变更管理

+ 25 - 3
app/service/change.js

@@ -720,14 +720,36 @@ module.exports = app => {
          * @returns {Promise<void>}
          */
         async getChangeAndUsedInfo(tid) {
+            const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
+            let filter;
+            if (lastStage.id === this.ctx.stage.id) {
+                filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
+                    [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
+            } else {
+                if (lastStage.status === audit.stage.status.uncheck) {
+                    filter = 'And s.order < ' + lastStage.order;
+                } else if (lastStage.status === audit.stage.status.checked) {
+                    filter = '';
+                } else if (lastStage.status === audit.stage.status.checkNo) {
+                    filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And sChange.`stimes` <= ?))',
+                        [lastStage.order, lastStage.order, lastStage.times])
+                } else {
+                    const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
+                    filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
+                        [lastStage.order, lastStage.order, lastStage.times, lastStage.times, curAuditor.order - 1]);
+                }
+            }
+            console.log(filter);
             const sql = 'SELECT C.*, Sum(U.utp) As used_tp, Round(Sum(U.utp) / C.total_price * 100, 2) As used_pt' +
                 '  FROM ' + this.tableName + ' As C' +
                 '  LEFT JOIN (SELECT sc.tid, sc.cid, sc.cbid, Round(SUM(sc.qty) * cb.unit_price, ?) As utp' +
                 '    FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
                 '    INNER JOIN (' +
-                '      SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, `sid` ' +
-                '        FROM ' + this.ctx.service.stageChange.tableName +
-                '        WHERE tid = ?' +
+                '      SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid` ' +
+                '        FROM ' + this.ctx.service.stageChange.tableName + ' As sChange ' +
+                '        LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s' +
+                '        ON sChange.sid = s.id' +
+                '        WHERE sChange.tid = ?' + filter +
                 '        GROUP By `lid`, `pid`, `cbid`, `sid`' +
                 '    ) As m' +
                 '    ON sc.stimes = m.stimes And sc.sorder = m.sorder And sc.`cbid` = m.`cbid` AND sc.`sid` = m.`sid`' +

+ 18 - 12
app/service/stage_bills.js

@@ -40,7 +40,6 @@ module.exports = app => {
                         '  ) As MaxFilter ' +
                         '  ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`';
             const sqlParam = [tid, sid];
-            console.log(this.db.format(sql, sqlParam));
             if (!lid) {
                 return await this.db.query(sql, sqlParam);
             } else if (lid instanceof Array) {
@@ -52,17 +51,26 @@ module.exports = app => {
             }
         }
 
+        /**
+         * 查询 某期 某轮审批 某人数据
+         * @param {Number} tid - 标段id
+         * @param {Number} sid - 期id
+         * @param {Number} times - 第几轮
+         * @param {Number} order - 流程
+         * @param {Number|Array} lid - 台账节点id(可以为空)
+         * @returns {Promise<*>}
+         */
         async getAuditorStageData(tid, sid, times, order, lid) {
             const lidSql = lid ? ' And Bills.lid in (?)' : '';
             const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
                 '  INNER JOIN ( ' +
                 '    SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid` From ' + this.tableName +
-                '      WHERE `times` <= ? AND `order` <= ?' +
+                '      WHERE `times` < ? OR (`times` = ? AND `order` <= ?)' +
                 '      GROUP BY `lid`' +
                 '  ) As MaxFilter ' +
                 '  ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid' +
                 '  WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
-            const sqlParam = [times, order, tid, sid];
+            const sqlParam = [times, times, order, tid, sid];
             if (!lid) {
                 return await this.db.query(sql, sqlParam);
             } else if (lid instanceof Array) {
@@ -90,8 +98,8 @@ module.exports = app => {
                 tid: this.ctx.tender.id,
                 lid: ledgerData.id,
                 sid: this.ctx.stage.id,
-                times: this.ctx.stage.times,
-                order: this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0,
+                times: this.ctx.stage.curTimes,
+                order: this.ctx.stage.curOrder,
                 said: this.ctx.session.sessionUser.accountId,
             };
 
@@ -135,8 +143,8 @@ module.exports = app => {
                         d.tid = this.ctx.tender.id;
                         d.sid = this.ctx.stage.id;
                         d.said = this.ctx.session.sessionUser.accountId;
-                        d.times = this.ctx.stage.times;
-                        d.order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
+                        d.times = this.ctx.stage.curTimes;
+                        d.order = this.ctx.stage.curOrder;
                         await transaction.insert(this.tableName, d);
                     } else {
                         d.id = stageBills.id;
@@ -174,8 +182,7 @@ module.exports = app => {
             if (stageBills) {
                 if ((updateData.contract_qty === undefined || stageBills.contract_qty !== updateData.contract_qty) ||
                     (updateData.qc_qty === undefined || stageBills.qc_qty !== updateData.qc_qty)) {
-                    const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
-                    if (stageBills.times === this.ctx.stage.times && stageBills.order === curOrder) {
+                    if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
                         updateData.id = stageBills.id;
                         await transaction.update(this.tableName, updateData);
                     } else {
@@ -218,7 +225,7 @@ module.exports = app => {
                     return;
                 } else {
                     const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
-                    if (stageBills.times === this.ctx.stage.times && stageBills.order === curOrder) {
+                    if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
                         posGather.id = stageBills.id;
                         await transaction.update(this.tableName, posGather);
                     } else {
@@ -239,10 +246,9 @@ module.exports = app => {
                 '  ) As MaxFilter ' +
                 '  ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid' +
                 '  WHERE Bills.sid = ?';
-            const sqlParam = [stage.times, stage.curAuditor ? stage.curAuditor.order : 0, stage.id];
+            const sqlParam = [stage.curTimes, stage.curOrder, stage.id];
             const result = await this.db.queryOne(sql, sqlParam);
             return result;
-            //return this._.add(result.contract_tp, result.qc_tp);
         }
 
         async getSumTotalPriceFilter(stage, operate, filter) {

+ 59 - 17
app/service/stage_change.js

@@ -9,6 +9,7 @@
  */
 
 const defaultPid = -1; // 非pid
+const audit = require('../const/audit');
 
 module.exports = app => {
 
@@ -26,7 +27,7 @@ module.exports = app => {
         }
 
         /**
-         * 查询 调用的变更令
+         * 查询 调用的变更令 最新数据
          * @param {Number} tid - 标段id
          * @param {Number} sid - 期id
          * @param {Number} lid - 台账节点id
@@ -36,16 +37,38 @@ module.exports = app => {
         async getLastestStageData(tid, sid, lid, pid) {
             const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
                 '  INNER JOIN ( ' +
-                '    SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid` From ' + this.tableName +
+                '    SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
                 '      WHERE tid = ? And sid = ? And lid = ? And pid = ?' +
                 '      GROUP By `lid`, `pid`' +
                 '  ) As m ' +
-                '  ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid';
+                '  ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid`';
             const sqlParam = [tid, sid, lid, pid ? pid : -1];
             return await this.db.query(sql, sqlParam);
         }
 
         /**
+         * 查询 调用的变更令 某轮 某人的数据
+         * @param {Number} tid - 标段id
+         * @param {Number} sid - 期id
+         * @param {Number} times - 第几轮
+         * @param {Number} order - 第几人
+         * @param {Number} lid - 台账节点id
+         * @param {Number} pid - 部位明细id
+         * @returns {Promise<*>}
+         */
+        async getAuditorStageData(tid, sid, times, order, lid, pid) {
+            const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
+                '  INNER JOIN ( ' +
+                '    SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `sid` From ' + this.tableName +
+                '      WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?)) And lid = ? And pid = ?' +
+                '      GROUP By `lid`, `pid`' +
+                '  ) As m ' +
+                '  ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid`';
+            const sqlParam = [tid, sid, times, times, order, lid, pid ? pid : -1];
+            return await this.db.query(sql, sqlParam);
+        }
+
+        /**
          * 台账,调用变更令
          *
          * @param {Object} bills - 台账节点数据
@@ -74,7 +97,6 @@ module.exports = app => {
             const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
             // 获取原变更令
             const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1);
-            const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
             // 获取更新数据
             const updateChanges = [], newChanges = [];
             let billsQty = 0;
@@ -82,9 +104,9 @@ module.exports = app => {
                 const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
                 if (!nc || nc.qty !== oc.qty) {
                     const qty = nc ? this.round(nc.qty, precision.value) : null;
-                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, order, qty);
+                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
                     billsQty = this.ctx.helper.plus(billsQty, change.qty);
-                    if (oc.stimes === this.ctx.stage.times && oc.sorder === order) {
+                    if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
                         change.id = oc.id;
                         updateChanges.push(change);
                     } else {
@@ -97,7 +119,7 @@ module.exports = app => {
             for (const c of changes) {
                 const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
                 if (!nc) {
-                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, order, this.round(c.qty, precision.value));
+                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
                     billsQty = this.ctx.helper.plus(billsQty, change.qty);
                     newChanges.push(change);
                 }
@@ -157,9 +179,9 @@ module.exports = app => {
                 const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
                 if (!nc || nc.qty !== oc.qty) {
                     const qty = nc ? this.round(nc.qty, precision.value) : null;
-                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, qty);
+                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty);
                     posQty = this.ctx.helper.plus(posQty, change.qty);
-                    if (oc.stimes === this.ctx.stage.times && oc.sorder === this.ctx.stage.flowOrder) {
+                    if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
                         change.id = oc.id;
                         updateChanges.push(change);
                     } else {
@@ -172,7 +194,7 @@ module.exports = app => {
             for (const c of changes) {
                 const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
                 if (!nc) {
-                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, this.round(c.qty, precision.value));
+                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value));
                     posQty = this.ctx.helper.plus(posQty, change.qty);
                     newChanges.push(change);
                 }
@@ -196,9 +218,7 @@ module.exports = app => {
             // 获取返回数据
             try {
                 const data = {};
-                console.log(pos);
                 data.bills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
-                console.log(data.bills);
                 data.pos = await this.ctx.service.stagePos.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.id]);
                 return data;
             } catch(err) {
@@ -213,20 +233,42 @@ module.exports = app => {
          * @returns {Promise<void>}
          */
         async getUsedData(tid, cid) {
+            const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
+            let filter;
+            if (lastStage.id === this.ctx.stage.id) {
+                filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))',
+                    [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
+            } else {
+                if (lastStage.status === audit.stage.status.uncheck) {
+                    filter = 'And s.order < ' + lastStage.order;
+                } else if (lastStage.status === audit.stage.status.checked) {
+                    filter = '';
+                } else if (lastStage.status === audit.stage.status.checkNo) {
+                    filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ?))',
+                        [lastStage.order, lastStage.order, lastStage.times])
+                } else {
+                    const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
+                    filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))',
+                        [lastStage.order, lastStage.order, lastStage.times, curAuditor.order - 1]);
+                }
+            }
+
             const sql = 'SELECT c.lid, c.pid, SUM(c.qty) as used_qty,' +
                         '    cb.tid, cb.cid, cb.id, cb.code, cb.name, cb.unit, cb.unit_price, cb.detail, cb.samount' +
                         '  FROM ' + this.ctx.service.changeAuditList.tableName + ' As cb' +
                         '  LEFT JOIN ' + this.tableName + ' As c ON cb.id = c.cbid ' +
-                        '  LEFT JOIN (' +
-                        '    SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, `sid`, `cid` ' +
-                        '      FROM ' + this.tableName +
-                        '      WHERE tid = ? AND cid = ?' +
-                        '      GROUP By `lid`, `pid`, `cbid`, `sid`' +
+                        '  INNER JOIN (' +
+                        '    SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid`, `cid` ' +
+                        '      FROM ' + this.tableName + ' As sChange' +
+                        '      LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s ON sChange.sid = s.id' +
+                        '      WHERE sChange.tid = ? AND cid = ?' + filter +
+                        '      GROUP By `lid`, `pid`, `cbid`, sChange.`sid`' +
                         '  ) As m' +
                         '  ON c.stimes = m.stimes And c.sorder = m.sorder And c.`cbid` = m.`cbid` AND c.`sid` = m.`sid` And c.`cid` = m.`cid`' +
                         '  WHERE cb.cid = ?' +
                         '  GROUP By c.`cbid`';
             const sqlParam = [tid, cid, cid];
+
             return await this.db.query(sql, sqlParam);
         }
 

+ 31 - 0
app/service/stage_detail.js

@@ -50,6 +50,37 @@ module.exports = app => {
         }
 
         /**
+         * 查询 某期 某轮审批 某人数据
+         * @param {Number} tid - 标段id
+         * @param {Number} sid - 期id
+         * @param {Number} times - 第几轮
+         * @param {Number} order - 流程
+         * @param {Number|Array} lid - 台账节点id(可以为空)
+         * @returns {Promise<*>}
+         */
+        async getAuditorStageData(tid, sid, times, order, lid) {
+            const lidSql = lid ? ' And Bills.lid in (?)' : '';
+            const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
+                '  INNER JOIN ( ' +
+                '    SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid`, `uuid` From ' + this.tableName +
+                '      WHERE `tid` = ? And `sid` = ? And (`times` < ? OR (`times` = ? And `order` <= ?))' + lidSql +
+                '      GROUP BY `lid`, `uuid`' +
+                '  ) As MaxFilter ' +
+                '  ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
+                '  WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
+            const sqlParam = [tid, sid, times, times, order, tid, sid];
+            if (!lid) {
+                return await this.db.query(sql, sqlParam);
+            } else if (lid instanceof Array) {
+                sqlParam.push(lid.join(', '));
+                return await this.db.query(sql, sqlParam);
+            } else {
+                sqlParam.push(lid);
+                return await this.db.queryOne(sql, sqlParam);
+            }
+        }
+
+        /**
          * 获取 中间计量 用户最新输入数据
          * @param {Number} tid - 标段id
          * @param {Number} sid - 期id

+ 3 - 9
app/service/stage_pay.js

@@ -105,7 +105,7 @@ module.exports = app => {
         }
 
         async getStagePay(stage, pid) {
-            return await this.getAuditorStagePay(pid, stage.id, stage.times, stage.curAuditor ? stage.curAuditor.order : 0);
+            return await this.getAuditorStagePay(pid, stage.id, stage.curTimes, stage.curOrder);
         }
 
         /**
@@ -115,13 +115,7 @@ module.exports = app => {
          * @returns {Promise<*>}
          */
         async getStagePays(stage) {
-            return await this.getAuditorStageData(stage.id, stage.times, stage.curAuditor ? stage.curAuditor.order : 0);
-            // const sql = 'SELECT SP.*, P.`order`, P.uid, P.name, P.minus, P.ptype, P.sprice, P.sexpr, P.rprice, P.rexpr, P.is_yf, P.dl_type, P.dl_count, P.dl_tp_type, P.dl_tp ' +
-            //     '  FROM ?? As SP, ?? As P ' +
-            //     '  WHERE SP.`sid` = ? AND SP.`stimes` = ? AND SP.`sorder` = ? AND SP.`pid` = P.`id` ' +
-            //     '  ORDER BY P.`order`';
-            // const sqlParam = [this.tableName, this.ctx.service.pay.tableName, stage.id, stage.times, stage.curAuditor ? stage.curAuditor.order : 0];
-            // return await this.db.query(sql, sqlParam);
+            return await this.getAuditorStageData(stage.id, stage.curTimes, stage.curOrder);
         }
 
         /**
@@ -240,7 +234,7 @@ module.exports = app => {
                         '  FROM ?? As SP, ?? As P ' +
                         '  WHERE SP.`sid` = ? AND SP.`stimes` = ? AND SP.`sorder` = ? And SP.`pid` = P.`id` And P.`valid`';
             const sqlParam = [this.tableName, times, order, this.tableName, this.ctx.service.pay.tableName,
-                stage.id, stage.times, stage.curAuditor ? stage.curAuditor.order : 0];
+                stage.id, stage.curTimes, stage.curOrder];
             return await transaction.query(sql, sqlParam);
         }
     }

+ 16 - 19
app/service/stage_pos.js

@@ -32,14 +32,15 @@ module.exports = app => {
          * @returns {Promise<*>}
          */
         async getLastestStageData(tid, sid, pid) {
-            let pidSql = pid ? (' And Pos.pid in (' + (pid instanceof Array ? pid.join(', ') : pid) + ')') : '';
+            let pidSql = pid ? (' And pid in (' + (pid instanceof Array ? pid.join(', ') : pid) + ')') : '';
             const sql = 'SELECT * FROM ' + this.tableName + ' As Pos ' +
                 '  INNER JOIN ( ' +
-                '    SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `pid` From ' + this.tableName +
+                '    SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `tid`, `sid`, `pid` From ' + this.tableName +
+                '      WHERE `tid` = ? And sid = ?' + pidSql +
                 '      GROUP BY `pid`' +
                 '  ) As MaxFilter ' +
                 '  ON Pos.times = MaxFilter.times And Pos.order = MaxFilter.order And Pos.pid = MaxFilter.pid' +
-                '  WHERE Pos.tid = ? And Pos.sid = ?' + pidSql;
+                '    And Pos.`tid` = MaxFilter.`tid` And Pos.`sid` = MaxFilter.`sid`';
             const sqlParam = [tid, sid];
             if (!pid) {
                 return await this.db.query(sql, sqlParam);
@@ -68,7 +69,7 @@ module.exports = app => {
             const sql = 'SELECT * FROM ' + this.tableName + ' As Pos ' +
                 '  INNER JOIN ( ' +
                 '    SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `pid` From ' + this.tableName +
-                '      WHERE `times` <= ? AND `order` <= ?' +
+                '      WHERE `times` < ? OR (`times` = ? AND `order` <= ?)' +
                 '      GROUP BY `pid`' +
                 '  ) As MaxFilter ' +
                 '  ON Pos.times = MaxFilter.times And Pos.order = MaxFilter.order And Pos.pid = MaxFilter.pid' +
@@ -99,7 +100,7 @@ module.exports = app => {
             const p = JSON.parse(JSON.stringify(data.updateData));
             p.tid = this.ctx.tender.id;
             p.add_stage = this.ctx.stage.id;
-            p.add_times = this.ctx.stage.times;
+            p.add_times = this.ctx.stage.curTimes;
             p.add_user = this.ctx.session.sessionUser.accountId;
             if (p.contract_qty) { delete p.contract_qty; }
             if (p.qc_qty) { delete p.qc_qty; }
@@ -123,8 +124,8 @@ module.exports = app => {
                     tid: this.ctx.tender.id,
                     sid: this.ctx.stage.id,
                     said: this.ctx.session.sessionUser.accountId,
-                    times: this.ctx.stage.times,
-                    order: 0,
+                    times: this.ctx.stage.curTimes,
+                    order: this.ctx.stage.curOrder,
                 };
                 if (data.contract_qty) { ps.contract_qty = this.round(data.contract_qty, precision.value); }
                 if (data.qc_qty) { ps.qc_qty = this.round(data.qc_qty, precision.value); }
@@ -149,7 +150,6 @@ module.exports = app => {
             const result = {ledger: [], pos: [], stageUpdate: true};
             const datas = data instanceof Array ? data : [data];
             const orgStagePos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, this._.map(datas, 'pid'));
-            const userOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
             for (const d of datas) {
                 const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
                 if (d.contract_qty || d.qc_qty) {
@@ -159,14 +159,14 @@ module.exports = app => {
                     }
                     this.ctx.helper.checkFieldPrecision(d, this.qtyFields, precision.value);
                 }
-                if (osp && osp.times === this.ctx.stage.times && osp.order === userOrder) {
+                if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
                     await transaction.update(this.tableName, d, {where: {id: osp.id}});
                 } else {
                     d.tid = this.ctx.tender.id;
                     d.sid = this.ctx.stage.id;
                     d.said = this.ctx.session.sessionUser.accountId;
-                    d.times = this.ctx.stage.times;
-                    d.order = userOrder;
+                    d.times = this.ctx.stage.curTimes;
+                    d.order = this.ctx.stage.curOrder;
                     await transaction.insert(this.tableName, d);
                 }
                 result.pos.push(d.pid);
@@ -193,11 +193,11 @@ module.exports = app => {
             const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
             if (pos instanceof Array) {
                 for (const p of pos) {
-                    if (p.add_stage !== this.ctx.stage.id || p.add_times !== this.ctx.stage.times || p.add_user !== this.ctx.session.sessionUser.accountId) {
+                    if (p.add_stage !== this.ctx.stage.id || p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId) {
                         throw '您无权删除该数据';
                     }
                 }
-            } else if (pos.add_stage !== this.ctx.stage.id || pos.add_times !== this.ctx.stage.times || pos.add_user !== this.ctx.session.sessionUser.accountId) {
+            } else if (pos.add_stage !== this.ctx.stage.id || pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId) {
                 throw '您无权删除该数据';
             }
             const ledgerIds = this._.map(pos, 'lid');
@@ -248,7 +248,6 @@ module.exports = app => {
                 throw err;
             }
 
-            console.log(refreshData);
             try {
                 const result = {ledger: {}, pos: {}};
                 if (refreshData.ledger && refreshData.ledger.length > 0) {
@@ -257,7 +256,6 @@ module.exports = app => {
                         result.ledger.curStageData = await await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
                     }
                 }
-                console.log(result.ledger.curStageData);
                 if (refreshData.pos && refreshData.pos.length > 0) {
                     result.pos.pos = await this.ctx.service.pos.getPosData({id: refreshData.pos});
                     if (refreshData.stageUpdate) {
@@ -272,8 +270,7 @@ module.exports = app => {
 
         async updateChangeQuantity(transaction, pos, qty) {
             const orgPos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.id);
-            const userOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
-            if (orgPos && orgPos.times === this.ctx.stage.times && orgPos.order === userOrder) {
+            if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
                 await transaction.update(this.tableName, {qc_qty: qty}, {where: {id: orgPos.id}});
             } else {
                 await transaction.insert(this.tableName, {
@@ -282,8 +279,8 @@ module.exports = app => {
                     lid: pos.lid,
                     pid: pos.id,
                     said: this.ctx.session.sessionUser.accountId,
-                    times: this.ctx.stage.times,
-                    order: userOrder,
+                    times: this.ctx.stage.curTimes,
+                    order: this.ctx.stage.curOrder,
                     qc_qty: qty,
                 });
             }