payment_detail_check.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. const rptAudits = yield this.service.paymentRptAudit.getAllDataByCondition({ where: { td_id: detail.id } });
  38. const accountId = this.session.sessionUser.accountId,
  39. auditorIds = _.map(detail.auditors, 'aid'),
  40. rptAuditIds = _.map(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. } else if (this.tender.isTourist) {
  51. detail.curTimes = detail.times;
  52. if (detail.status === status.uncheck || detail.status === status.checkNo) {
  53. detail.curOrder = 0;
  54. } else if (detail.status === status.checked) {
  55. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  56. } else {
  57. detail.curOrder = detail.curAuditor.order;
  58. }
  59. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  60. if (detail.status === status.uncheck) {
  61. throw '您无权查看该数据';
  62. }
  63. // detail.readOnly = detail.status !== status.checking || accountId !== detail.curAuditor.aid;
  64. detail.curTimes = detail.status === status.checkNo ? detail.times - 1 : detail.times;
  65. if (detail.status === status.checked) {
  66. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  67. } else if (detail.status === status.checkNo) {
  68. const audit = this.service.paymentDetailAudit.getDataByCondition({
  69. td_id: detail.id, times: detail.times, status: status.checkNo,
  70. });
  71. detail.curOrder = audit.order;
  72. } else {
  73. detail.curOrder = accountId === detail.curAuditor.aid ? detail.curAuditor.order : detail.curAuditor.order - 1;
  74. }
  75. } else if (rptAuditIds.indexOf(accountId) !== -1 || this.payment.auditPermission.view_all) {
  76. if (detail.status === status.uncheck || detail.status === status.checkNo) {
  77. throw '您无权查看该数据';
  78. }
  79. } else { // 其他不可见
  80. throw '您无权查看该数据';
  81. }
  82. // 获取最新的期
  83. detail.highOrder = yield this.service.paymentDetail.count({
  84. tr_id: detail.tr_id,
  85. });
  86. detail.readOnly = !((detail.status === status.uncheck || detail.status === status.checkNo) && accountId === detail.uid);
  87. this.detail = detail;
  88. this.trInfo = trInfo;
  89. // 根据状态判断是否需要更新审批人列表
  90. if ((detail.status === status.uncheck || detail.status === status.checkNo) && trInfo.sp_status !== shenpiConst.sp_status.sqspr) {
  91. const shenpi_status = trInfo.sp_status;
  92. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  93. const auditList = yield this.service.paymentDetailAudit.getAllDataByCondition({ where: { td_id: detail.id, times: detail.times }, orders: [['order', 'asc']] });
  94. const auditIdList = _.map(auditList, 'aid');
  95. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  96. const shenpiList = yield this.service.paymentShenpiAudit.getAllDataByCondition({ where: { tr_id: trInfo.id, sp_status: shenpi_status } });
  97. const shenpiIdList = _.map(shenpiList, 'audit_id');
  98. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  99. if (!_.isEqual(auditIdList, shenpiIdList)) {
  100. yield this.service.paymentDetailAudit.updateNewAuditList(detail, shenpiIdList);
  101. }
  102. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  103. const shenpiInfo = yield this.service.paymentShenpiAudit.getDataByCondition({ tr_id: trInfo.id, sp_status: shenpi_status });
  104. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  105. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  106. yield this.service.paymentDetailAudit.updateLastAudit(detail, auditList, shenpiInfo.audit_id);
  107. } else if (!shenpiInfo) {
  108. // 不存在终审人的状态下这里恢复为授权审批人
  109. this.detail.sp_status = shenpiConst.sp_status.sqspr;
  110. }
  111. }
  112. }
  113. yield next;
  114. } catch (err) {
  115. // 输出错误到日志
  116. if (err.stack) {
  117. this.logger.error(err);
  118. } else {
  119. this.session.message = {
  120. type: messageType.ERROR,
  121. icon: 'exclamation-circle',
  122. message: err,
  123. };
  124. this.getLogger('fail').info(JSON.stringify({
  125. error: err,
  126. project: this.session.sessionProject,
  127. user: this.session.sessionUser,
  128. body: this.session.body,
  129. }));
  130. }
  131. if (this.helper.isAjax(this.request)) {
  132. if (err.stack) {
  133. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  134. } else {
  135. this.body = {err: 3, msg: err.toString(), data: null};
  136. }
  137. } else {
  138. if (this.helper.isWap(this.request)) {
  139. this.redirect('/wap/list');
  140. } else {
  141. err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/payment');
  142. }
  143. }
  144. }
  145. };
  146. };