payment_detail_check.js 7.6 KB

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