payment_detail_check.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const messageType = require('../const/message_type');
  10. const status = require('../const/audit').stage.status;
  11. const shenpiConst = require('../const/shenpi');
  12. const _ = require('lodash');
  13. module.exports = options => {
  14. /**
  15. * 标段校验 中间件
  16. * 1. 读取标段数据(包括属性)
  17. * 2. 检验用户是否可见标段(不校验具体权限)
  18. *
  19. * @param {function} next - 中间件继续执行的方法
  20. * @return {void}
  21. */
  22. return function* paymentDetailCheck(next) {
  23. try {
  24. const id = parseInt(this.params.did);
  25. if (!id) throw '参数错误';
  26. const detail = yield this.service.paymentDetail.getDataById(id);
  27. if (!detail) {
  28. throw '支付审批表单不存在';
  29. }
  30. const trInfo = yield this.service.paymentTenderRpt.getDataById(detail.tr_id);
  31. if (!trInfo) {
  32. throw '支付审批报表不存在';
  33. }
  34. // 读取原报、审核人数据
  35. detail.auditors = yield this.service.paymentDetailAudit.getAuditors(detail.id, detail.times);
  36. detail.curAuditor = yield this.service.paymentDetailAudit.getCurAuditor(detail.id, detail.times);
  37. detail.rptAudits = yield this.service.paymentRptAudit.getListByDetail(detail.id);
  38. const accountId = this.session.sessionUser.accountId,
  39. auditorIds = _.map(detail.auditors, 'aid'),
  40. rptAuditIds = _.map(detail.rptAudits, 'uid');
  41. if (accountId === detail.uid) { // 原报
  42. detail.curTimes = detail.times;
  43. if (detail.status === status.uncheck || detail.status === status.checkNo) {
  44. detail.curOrder = 0;
  45. } else if (detail.status === status.checked) {
  46. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  47. } else {
  48. detail.curOrder = detail.curAuditor.aid === accountId ? detail.curAuditor.order : detail.curAuditor.order - 1;
  49. }
  50. detail.filePermission = true;
  51. } else if (auditorIds.indexOf(accountId) !== -1 || rptAuditIds.indexOf(accountId) !== -1 || this.payment.auditPermission.view_all) { // 审批人及签署人及查看所有权人
  52. if (detail.status === status.uncheck) {
  53. throw '您无权查看该数据';
  54. }
  55. // detail.readOnly = detail.status !== status.checking || accountId !== detail.curAuditor.aid;
  56. detail.curTimes = detail.status === status.checkNo ? detail.times - 1 : detail.times;
  57. if (detail.status === status.checked) {
  58. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  59. } else if (detail.status === status.checkNo) {
  60. const audit = this.service.paymentDetailAudit.getDataByCondition({
  61. td_id: detail.id, times: detail.times, status: status.checkNo,
  62. });
  63. detail.curOrder = audit.order;
  64. } else {
  65. detail.curOrder = accountId === detail.curAuditor.aid ? detail.curAuditor.order : detail.curAuditor.order - 1;
  66. }
  67. detail.filePermission = auditorIds.indexOf(accountId) !== -1 || rptAuditIds.indexOf(accountId) !== -1;
  68. } else { // 其他不可见
  69. throw '您无权查看该数据';
  70. }
  71. // 获取最新的期
  72. detail.highOrder = yield this.service.paymentDetail.count({
  73. tr_id: detail.tr_id,
  74. });
  75. detail.readOnly = !((detail.status === status.uncheck || detail.status === status.checkNo) && accountId === detail.uid);
  76. this.detail = detail;
  77. this.trInfo = trInfo;
  78. // 根据状态判断是否需要更新审批人列表
  79. if ((detail.status === status.uncheck || detail.status === status.checkNo) && trInfo.sp_status !== shenpiConst.sp_status.sqspr) {
  80. const shenpi_status = trInfo.sp_status;
  81. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  82. const auditList = yield this.service.paymentDetailAudit.getAllDataByCondition({ where: { td_id: detail.id, times: detail.times }, orders: [['order', 'asc']] });
  83. const auditIdList = _.map(auditList, 'aid');
  84. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  85. const shenpiList = yield this.service.paymentShenpiAudit.getAllDataByCondition({ where: { tr_id: trInfo.id, sp_status: shenpi_status } });
  86. const shenpiIdList = _.map(shenpiList, 'audit_id');
  87. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  88. if (!_.isEqual(auditIdList, shenpiIdList)) {
  89. yield this.service.paymentDetailAudit.updateNewAuditList(detail, shenpiIdList);
  90. }
  91. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  92. const shenpiInfo = yield this.service.paymentShenpiAudit.getDataByCondition({ tr_id: trInfo.id, sp_status: shenpi_status });
  93. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  94. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  95. yield this.service.paymentDetailAudit.updateLastAudit(detail, auditList, shenpiInfo.audit_id);
  96. } else if (!shenpiInfo) {
  97. // 不存在终审人的状态下这里恢复为授权审批人
  98. this.detail.sp_status = shenpiConst.sp_status.sqspr;
  99. }
  100. }
  101. }
  102. yield next;
  103. } catch (err) {
  104. // 输出错误到日志
  105. if (err.stack) {
  106. this.logger.error(err);
  107. } else {
  108. this.session.message = {
  109. type: messageType.ERROR,
  110. icon: 'exclamation-circle',
  111. message: err,
  112. };
  113. this.getLogger('fail').info(JSON.stringify({
  114. error: err,
  115. project: this.session.sessionProject,
  116. user: this.session.sessionUser,
  117. body: this.session.body,
  118. }));
  119. }
  120. if (this.helper.isAjax(this.request)) {
  121. if (err.stack) {
  122. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  123. } else {
  124. this.body = {err: 3, msg: err.toString(), data: null};
  125. }
  126. } else {
  127. if (this.helper.isWap(this.request)) {
  128. this.redirect('/wap/list');
  129. } else {
  130. err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/payment');
  131. }
  132. }
  133. }
  134. };
  135. };