Pārlūkot izejas kodu

调差审批流判断

laiguoran 4 gadi atpakaļ
vecāks
revīzija
58d53e1f2e

+ 7 - 5
app/controller/material_controller.js

@@ -15,6 +15,7 @@ const tenderConst = require('../const/tender');
 const measureType = tenderConst.measureType;
 const accountGroup = require('../const/account_group').group;
 const materialConst = require('../const/material');
+const shenpiConst = require('../const/shenpi');
 const MaterialCalculator = require('../lib/material_calc');
 const sendToWormhole = require('stream-wormhole');
 const fs = require('fs');
@@ -176,6 +177,7 @@ module.exports = app => {
                 measureType,
                 preUrl: '/tender/' + ctx.tender.id + '/measure/material/' + ctx.params.order,
                 material: ctx.material,
+                shenpiConst,
             };
             if ((ctx.material.status === auditConst.status.uncheck || ctx.material.status === auditConst.status.checkNo) && ctx.session.sessionUser.accountId === ctx.material.user_id) {
                 // data.accountGroup = accountGroup;
@@ -214,7 +216,7 @@ module.exports = app => {
                 }
             }
             // 获取审批流程中左边列表
-            ctx.material.auditors2 = await ctx.service.materialAudit.getAuditorsWithOwner(ctx.material.id, times);
+            ctx.material.auditors2 = await ctx.service.materialAudit.getAuditorsWithOwner(ctx.material.id, ctx.material.times);
             if (ctx.material.status === auditConst.status.uncheck || ctx.material.status === auditConst.status.checkNo) {
                 ctx.material.auditorList = await ctx.service.materialAudit.getAuditors(ctx.material.id, ctx.material.times);
             }
@@ -739,14 +741,14 @@ module.exports = app => {
                 if (exist) {
                     throw '该审核人已存在,请勿重复添加';
                 }
-
-                const result = await ctx.service.materialAudit.addAuditor(ctx.material.id, id, ctx.material.times);
+                const is_gdzs = ctx.tender.info.shenpi && JSON.parse(ctx.tender.info.shenpi).material === shenpiConst.sp_status.gdzs ? 1 : 0;
+                const result = await ctx.service.materialAudit.addAuditor(ctx.material.id, id, ctx.material.times, is_gdzs);
                 if (!result) {
                     throw '添加审核人失败';
                 }
 
-                const audit = await ctx.service.materialAudit.getAuditor(ctx.material.id, id, ctx.material.times);
-                ctx.body = { err: 0, msg: '', data: audit };
+                const auditors = await ctx.service.materialAudit.getAuditorsWithOwner(ctx.material.id, ctx.material.times);
+                ctx.body = { err: 0, msg: '', data: auditors };
             } catch (err) {
                 this.log(err);
                 ctx.body = { err: 1, msg: err.toString(), data: null };

+ 22 - 0
app/middleware/material_check.js

@@ -8,6 +8,7 @@
  */
 
 const status = require('../const/audit').material.status;
+const shenpiConst = require('../const/shenpi');
 const _ = require('lodash');
 
 module.exports = options => {
@@ -103,6 +104,27 @@ module.exports = options => {
             // 调差的readOnly 指表格和页面只能看不能改,和审批无关
             material.readOnly = !((material.status === status.uncheck || material.status === status.checkNo) && accountId === material.user_id);
             this.material = material;
+            // 根据状态判断是否需要更新审批人列表
+            if ((material.status === status.uncheck || material.status === status.checkNo) && this.tender.info.shenpi && JSON.parse(this.tender.info.shenpi).material !== shenpiConst.sp_status.sqspr) {
+                const shenpi_status = JSON.parse(this.tender.info.shenpi).material;
+                // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
+                const auditList = yield this.service.materialAudit.getAllDataByCondition({ where: { mid: material.id, times: material.times } });
+                const auditIdList = _.map(auditList, 'aid');
+                if (shenpi_status === shenpiConst.sp_status.gdspl) {
+                    const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status } });
+                    const shenpiIdList = _.map(shenpiList, 'audit_id');
+                    // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
+                    if (!_.isEqual(auditIdList, shenpiIdList) && shenpiIdList.length !== 0) {
+                        yield this.service.materialAudit.updateNewAuditList(material, shenpiIdList);
+                    }
+                } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
+                    const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status });
+                    // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
+                    if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
+                        yield this.service.materialAudit.updateLastAudit(material, auditList, shenpiInfo.audit_id);
+                    }
+                }
+            }
             yield next;
         } catch (err) {
             console.log(err);

+ 57 - 42
app/public/js/material_audit.js

@@ -79,46 +79,61 @@ $(document).ready(function () {
 
     // 添加到审批流程中
     $('dl').on('click', 'dd', function () {
-        const id = parseInt($(this).data('id'))
+        const id = parseInt($(this).data('id'));
         if (id) {
-            postData(getUrlPre() + '/audit/add', { auditorId: id }, (data) => {
+            postData(getUrlPre() + '/audit/add', { auditorId: id }, (datas) => {
                 const html = [];
-                html.push('<li class="list-group-item" auditorId="'+ data.aid +'"><a href="javascript: void(0)" class="text-danger pull-right">移除</a>');
-                html.push('<span>');
-                html.push(data.order + ' ');
-                html.push(data.name + ' ');
-                html.push('</span>');
-                html.push('<small class="text-muted">');
-                html.push(data.role);
-                html.push('</small></li>');
-                $('#auditors').append(html.join(''));
-
                 // 如果是重新上报,添加到重新上报列表中
                 const auditorshtml = [];
-                // 重新上报时。令其它的审批人流程图标转换
-                $('#auditors-list li i').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down');
-                for (let i = 0; i < $('#auditors-list li').length; i++) {
-                    $('#auditors-list li').eq(i).find('.pull-right').text(transFormToChinese(i+1) + '审');
-                    $('#auditors-list2 li').eq(i).find('.pull-right').text(transFormToChinese(i+1) + '审');
+                for (const [index,data] of datas.entries()) {
+                    if (index !== 0) {
+                        html.push('<li class="list-group-item" auditorId="'+ data.aid +'">');
+                        if (is_gdzs && index+1 !== datas.length) {
+                            html.push('<a href="javascript: void(0)" class="text-danger pull-right">移除</a>');
+                        }
+                        html.push('<span>');
+                        html.push(data.order + ' ');
+                        html.push(data.name + ' ');
+                        html.push('</span>');
+                        html.push('<small class="text-muted">');
+                        html.push(data.role);
+                        html.push('</small></li>');
+                    }
+                    // 添加新审批人流程修改
+                    auditorshtml.push('<li class="list-group-item" data-auditid="' + data.aid + '">');
+                    auditorshtml.push('<i class="fa fa-stop-circle"></i> ');
+                    auditorshtml.push(data.name + ' <small class="text-muted">' + data.role + '</small>');
+                    if (index === 0) {
+                        auditorshtml.push('<span class="pull-right">原报</span>');
+                    } else if (index+1 === datas.length) {
+                        auditorshtml.push('<span class="pull-right">终审</span>');
+                    } else {
+                        auditorshtml.push('<span class="pull-right">'+ transFormToChinese(index) +'审</span>');
+                    }
+                    auditorshtml.push('</li>');
                 }
-                // 添加新审批人
-                auditorshtml.push('<li class="list-group-item" data-auditid="' + data.aid + '">');
-                auditorshtml.push('<i class="fa fa-stop-circle"></i> ');
-                auditorshtml.push(data.name + ' <small class="text-muted">' + data.role + '</small>');
-                auditorshtml.push('<span class="pull-right">终审</span>');
-                auditorshtml.push('</li>');
-                $('#auditors-list').append(auditorshtml.join(''));
+                $('#auditors').html(html.join(''));
+
 
-                const auditorshtml2 = [];
                 // 重新上报时。令其它的审批人流程图标转换
-                $('#auditors-list2 li i').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down');
-                // 添加新审批人
-                auditorshtml2.push('<li class="list-group-item" data-auditid="' + data.aid + '">');
-                auditorshtml2.push('<h5 class="card-title"><i class="fa fa-stop-circle"></i> ');
-                auditorshtml2.push(data.name + ' <small class="text-muted">' + data.role + '</small>');
-                auditorshtml2.push('<span class="pull-right">终审</span>');
-                auditorshtml2.push('</h5></li>');
-                $('#auditors-list2').append(auditorshtml2.join(''));
+                // $('#auditors-list li i').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down');
+                // for (let i = 0; i < $('#auditors-list li').length; i++) {
+                //     $('#auditors-list li').eq(i).find('.pull-right').text(transFormToChinese(i+1) + '审');
+                //     $('#auditors-list2 li').eq(i).find('.pull-right').text(transFormToChinese(i+1) + '审');
+                // }
+
+                $('#auditors-list').html(auditorshtml.join(''));
+
+                // const auditorshtml2 = [];
+                // // 重新上报时。令其它的审批人流程图标转换
+                // $('#auditors-list2 li i').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down');
+                // // 添加新审批人
+                // auditorshtml2.push('<li class="list-group-item" data-auditid="' + data.aid + '">');
+                // auditorshtml2.push('<h5 class="card-title"><i class="fa fa-stop-circle"></i> ');
+                // auditorshtml2.push(data.name + ' <small class="text-muted">' + data.role + '</small>');
+                // auditorshtml2.push('<span class="pull-right">终审</span>');
+                // auditorshtml2.push('</h5></li>');
+                // $('#auditors-list2').append(auditorshtml2.join(''));
             });
         }
     });
@@ -137,19 +152,19 @@ $(document).ready(function () {
 
             // 如果是重新上报
             // 令最后一个图标转换
-            $('#auditors-list li[data-auditid="' + data.auditorId + '"]').remove();
+            $('#auditors-list li[data-auditorid="' + data.auditorId + '"]').remove();
             if ($('#auditors-list li').length !== 0 && !$('#auditors-list li i').hasClass('fa-stop-circle')) {
                 $('#auditors-list li').eq($('#auditors-list li').length-1).children('i')
                     .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
             }
-            $('#auditors-list2 li[data-auditid="' + data.auditorId + '"]').remove();
-            if ($('#auditors-list2 li').length !== 0 && !$('#auditors-list2 li i').hasClass('fa-stop-circle')) {
-                $('#auditors-list2 li').eq($('#auditors-list2 li').length-1).children('i')
-                    .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
-            }
-            for (let i = 0; i < $('#auditors-list').length; i++) {
-                $('#auditors-list').eq(i).find('.pull-right').text((i+1 === $('#auditors-list').length ? '终' : transFormToChinese(i+1)) + '审');
-                $('#auditors-list2').eq(i).find('.pull-right').text((i+1 === $('#auditors-list2').length ? '终' : transFormToChinese(i+1)) + '审');
+            // $('#auditors-list2 li[data-auditid="' + data.auditorId + '"]').remove();
+            // if ($('#auditors-list2 li').length !== 0 && !$('#auditors-list2 li i').hasClass('fa-stop-circle')) {
+            //     $('#auditors-list2 li').eq($('#auditors-list2 li').length-1).children('i')
+            //         .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
+            // }
+            for (let i = 0; i < $('#auditors-list li').length; i++) {
+                $('#auditors-list li').eq(i).find('.pull-right').text(i === 0 ? '原报' : (i+1 === $('#auditors-list li').length ? '终' : transFormToChinese(i+1)) + '审');
+                // $('#auditors-list2').eq(i).find('.pull-right').text((i+1 === $('#auditors-list2').length ? '终' : transFormToChinese(i+1)) + '审');
             }
         });
     });

+ 74 - 14
app/service/material_audit.js

@@ -103,18 +103,29 @@ module.exports = app => {
          * @param {Number} times - 第几次审批
          * @return {Promise<number>}
          */
-        async addAuditor(materialId, auditorId, times = 1) {
-            const newOrder = await this.getNewOrder(materialId, times);
-            const data = {
-                tid: this.ctx.tender.id,
-                mid: materialId,
-                aid: auditorId,
-                times,
-                order: newOrder,
-                status: auditConst.status.uncheck,
-            };
-            const result = await this.db.insert(this.tableName, data);
-            return result.effectRows = 1;
+        async addAuditor(materialId, auditorId, times = 1, is_gdzs = 0) {
+            const transaction = await this.db.beginTransaction();
+            try {
+                let newOrder = await this.getNewOrder(materialId, times);
+                // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
+                newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
+                if (is_gdzs) await this._syncOrderByDelete(transaction, materialId, newOrder, times, '+');
+                const data = {
+                    tid: this.ctx.tender.id,
+                    mid: materialId,
+                    aid: auditorId,
+                    times,
+                    order: newOrder,
+                    status: auditConst.status.uncheck,
+                };
+                const result = await transaction.insert(this.tableName, data);
+                await transaction.commit();
+                return result.effectRows = 1;
+            } catch (err) {
+                await transaction.rollback();
+                throw err;
+            }
+            return false;
         }
 
         /**
@@ -126,7 +137,7 @@ module.exports = app => {
          * @return {Promise<*>}
          * @private
          */
-        async _syncOrderByDelete(transaction, materialId, order, times) {
+        async _syncOrderByDelete(transaction, materialId, order, times, selfOperate = '-') {
             this.initSqlBuilder();
             this.sqlBuilder.setAndWhere('mid', {
                 value: materialId,
@@ -142,7 +153,7 @@ module.exports = app => {
             });
             this.sqlBuilder.setUpdateData('order', {
                 value: 1,
-                selfOperate: '-',
+                selfOperate: selfOperate,
             });
             const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
             const data = await transaction.query(sql, sqlParam);
@@ -846,6 +857,55 @@ module.exports = app => {
             const sqlParam = [tenderId];
             return this.db.query(sql, sqlParam);
         }
+
+        async updateNewAuditList(material, newIdList) {
+            const transaction = await this.db.beginTransaction();
+            try {
+                // 先删除旧的审批流,再添加新的
+                transaction.delete(this.tableName, { mid: material.id, times: material.times });
+                const newAuditors = [];
+                let order = 1;
+                for (const aid of newIdList) {
+                    newAuditors.push({
+                        tid: material.tid, mid: material.id, aid,
+                        times: material.times, order, status: auditConst.status.uncheck,
+                    });
+                    order++;
+                }
+                await transaction.insert(this.tableName, newAuditors);
+                await transaction.commit();
+            } catch (err) {
+                await transaction.rollback();
+                throw err;
+            }
+        }
+
+        async updateLastAudit(material, auditList, lastId) {
+            const transaction = await this.db.beginTransaction();
+            try {
+                // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
+                const idList = this._.map(auditList, 'aid');
+                let order = idList.length + 1;
+                if (idList.indexOf(lastId) !== -1) {
+                    transaction.delete(this.tableName, { mid: material.id, times: material.times, aid: lastId });
+                    const audit = this._.find(auditList, { 'aid': lastId });
+                    // 顺移之后审核人流程顺序
+                    await this._syncOrderByDelete(transaction, material.id, audit.order, material.times);
+                    order = order - 1;
+                }
+
+                // 添加终审
+                const newAuditor = {
+                    tid: material.tid, mid: material.id, aid: lastId,
+                    times: material.times, order, status: auditConst.status.uncheck,
+                };
+                await transaction.insert(this.tableName, newAuditor);
+                await transaction.commit();
+            } catch (err) {
+                await transaction.rollback();
+                throw err;
+            }
+        }
     }
 
     return MaterialAudit;

+ 10 - 2
app/view/material/audit_modal.ejs

@@ -8,6 +8,7 @@
             </div>
             <div class="modal-body">
                 <div class="dropdown text-right">
+                    <% if (!ctx.tender.info.shenpi || (ctx.tender.info.shenpi && JSON.parse(ctx.tender.info.shenpi).material !== shenpiConst.sp_status.gdspl)) { %>
                     <button class="btn btn-outline-primary btn-sm dropdown-toggle" type="button"
                         id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true"
                         aria-expanded="false">
@@ -34,6 +35,7 @@
                             <% }) %>
                         </dl>
                     </div>
+                    <% } %>
                 </div>
                 <div class="card mt-3">
                     <div class="card-header">
@@ -43,8 +45,13 @@
                     <ul class="list-group list-group-flush" id="auditors">
                         <% for (let i = 0, iLen = ctx.material.auditorList.length; i < iLen; i++) { %>
                         <li class="list-group-item" auditorId="<%- ctx.material.auditorList[i].aid %>">
+                            <% if (!ctx.tender.info.shenpi ||
+                                    (ctx.tender.info.shenpi && (JSON.parse(ctx.tender.info.shenpi).material === shenpiConst.sp_status.sqspr
+                                            || (JSON.parse(ctx.tender.info.shenpi).material === shenpiConst.sp_status.gdzs && i+1 !== iLen)
+                                    ))) { %>
                             <a href="javascript: void(0)" class="text-danger pull-right">移除</a>
-                            <%- ctx.material.auditorList[i].order %> <%- ctx.material.auditorList[i].name %>
+                            <% } %>
+                            <span><%- ctx.material.auditorList[i].order %> <%- ctx.material.auditorList[i].name %></span>
                             <small class="text-muted"><%- ctx.material.auditorList[i].role %></small>
                         </li>
                         <% } %>
@@ -76,7 +83,7 @@
                             <a class="sp-list-item" href="#sub-sp" data-toggle="modal" data-target="#sub-sp" id="hideSp">修改审批流程</a>
                         <% } %>
                         <div class="card mt-3">
-                            <ul class="list-group list-group-flush">
+                            <ul class="list-group list-group-flush" id="auditors-list">
                                 <% ctx.material.auditors2.forEach((item, idx) => { %>
                                 <% if (idx === 0) { %>
                                 <li class="list-group-item" data-auditorId="<%- item.aid %>">
@@ -679,6 +686,7 @@
     <script>
         const accountGroup = JSON.parse('<%- JSON.stringify(accountGroup) %>');
         const accountList = JSON.parse('<%- JSON.stringify(accountList) %>');
+        const is_gdzs = <% if (ctx.tender.info.shenpi && JSON.parse(ctx.tender.info.shenpi).material === shenpiConst.sp_status.gdzs) { %>1<% } else { %>0<% } %>;
     </script>
 <% } %>
 <script>const cur_uid = parseInt('<%- ctx.session.sessionUser.accountId %>');</script>

+ 1 - 0
app/view/tender/detail.ejs

@@ -100,6 +100,7 @@
                                 <a href="#bd-set-5" data-toggle="modal" data-target="#bd-set-5" class="btn btn-sm btn-outline-primary">显示设置</a>
                                 <a href="#bd-set-6" data-toggle="modal" data-target="#bd-set-6" class="btn btn-sm btn-outline-primary">章节设置</a>
                                 <a href="#bd-set-7" data-toggle="modal" data-target="#bd-set-7" class="btn btn-sm btn-outline-primary">付款账号</a>
+                                <a href="/tender/<%- tender.id %>/shenpi" class="btn btn-sm btn-outline-primary">审批流程</a>
                             </div>
                         </div>
                     </div>