weapp_tender_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 'use strict';
  2. const auditConst = require('../const/audit');
  3. const measureType = require('../const/tender').measureType;
  4. const advanceConst = require('../const/advance');
  5. const status = require('../const/audit').advance.status;
  6. const shenpiConst = require('../const/shenpi');
  7. const _ = require('lodash');
  8. module.exports = app => {
  9. class WeappTenderController extends app.BaseController {
  10. async listManage(ctx) {
  11. try {
  12. const projectId = ctx.query.projectId;
  13. if (!projectId) {
  14. ctx.body = { err: 1, msg: '缺少projectId参数', data: null };
  15. return;
  16. }
  17. const accountInfo = ctx.session.sessionUser;
  18. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  19. if (userPermission !== null && userPermission.tender !== undefined && userPermission.tender.indexOf('1') !== -1) {
  20. const subProject = await this.ctx.service.subProject.getDataById(projectId);
  21. const tenderList = await this.ctx.service.tender.getList('', userPermission, ctx.session.sessionUser.is_admin, '', subProject);
  22. for (const tender of tenderList) {
  23. // 查询当前审批期以及状态
  24. const stage = await this.ctx.service.stage.getLastestStage(tender.id, true);
  25. tender.stage_order = stage && stage.order ? stage.order : null;
  26. tender.stage_status = stage && stage.status ? stage.status : null;
  27. }
  28. const categoryData = await this.ctx.service.category.getAllCategory(subProject);
  29. const renderData = {
  30. categoryData,
  31. tenderList,
  32. };
  33. ctx.body = { err: 0, msg: '', data: renderData };
  34. } else {
  35. ctx.body = { err: 1, msg: '您没有管理权限', data: null };
  36. }
  37. } catch (err) {
  38. this.log(err);
  39. ctx.body = { err: 1, msg: err.toString(), data: null };
  40. }
  41. }
  42. async detail(ctx) {
  43. try {
  44. const tender = ctx.tender.data;
  45. let bCalcTp = ctx.tender.data.user_id === ctx.session.sessionUser.accountId && (
  46. ctx.tender.ledger_status === auditConst.ledger.status.checkNo || ctx.tender.ledger_status === auditConst.ledger.status.uncheck);
  47. const stages = await ctx.service.stage.getValidStages(ctx.tender.id);
  48. const lastStage = stages.length > 0 ? stages[0] : null; // await ctx.service.stage.getLastestStage(ctx.tender.id);
  49. const [change_tp, change_p_tp, change_n_tp, change_valuation_tp, change_unvaluation_tp] = await ctx.service.change.getChangeTp(ctx.tender.id);
  50. tender.change_tp = change_tp;
  51. tender.change_p_tp = change_p_tp;
  52. tender.change_n_tp = change_n_tp;
  53. tender.change_valuation_tp = change_valuation_tp;
  54. tender.change_unvaluation_tp = change_unvaluation_tp;
  55. if (lastStage) {
  56. await this.ctx.service.stage.checkStageGatherData(lastStage, ctx.session.sessionUser.is_admin);
  57. if ((!bCalcTp) && tender.measure_type === measureType.gcl.value) {
  58. bCalcTp = lastStage.status !== auditConst.stage.status.checked && !lastStage.readOnly;
  59. }
  60. if (bCalcTp) {
  61. const sum = await this.ctx.service.ledger.addUp({ tender_id: ctx.tender.id/* , is_leaf: true*/ });
  62. tender.total_price = sum.total_price;
  63. tender.deal_tp = sum.deal_tp;
  64. }
  65. tender.sum = ctx.tender.info.calc_type === 'tp' ? ctx.helper.add(tender.total_price, tender.change_valuation_tp) : ctx.helper.add(tender.total_price, tender.change_tp);
  66. tender.gather_tp = ctx.helper.sum([lastStage.contract_tp, lastStage.qc_tp, lastStage.pc_tp]);
  67. tender.end_contract_tp = ctx.helper.sum([lastStage.contract_tp, lastStage.pre_contract_tp, lastStage.contract_pc_tp]);
  68. tender.end_qc_tp = ctx.helper.sum([lastStage.qc_tp, lastStage.pre_qc_tp, lastStage.qc_pc_tp]);
  69. tender.end_positive_qc_tp = ctx.helper.sum([lastStage.positive_qc_tp, lastStage.pre_positive_qc_tp, lastStage.positive_qc_pc_tp]);
  70. tender.end_negative_qc_tp = ctx.helper.sum([lastStage.negative_qc_tp, lastStage.pre_negative_qc_tp, lastStage.negative_qc_pc_tp]);
  71. tender.end_gather_tp = ctx.helper.add(tender.end_contract_tp, tender.end_qc_tp);
  72. tender.pre_gather_tp = ctx.helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
  73. tender.yf_tp = lastStage.yf_tp;
  74. tender.sf_tp = lastStage.sf_tp;
  75. tender.qc_ratio = ctx.helper.mul(ctx.helper.div(tender.end_qc_tp, ctx.tender.info.deal_param.contractPrice, 2), 100);
  76. tender.pre_ratio = ctx.helper.mul(ctx.helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
  77. tender.cur_ratio = ctx.helper.mul(ctx.helper.div(tender.gather_tp, tender.sum, 2), 100);
  78. tender.other_tp = ctx.helper.sub(ctx.helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
  79. tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
  80. tender.end_yf_tp = ctx.helper.add(lastStage.yf_tp, lastStage.pre_yf_tp);
  81. tender.end_sf_tp = ctx.helper.add(lastStage.sf_tp, lastStage.pre_sf_tp);
  82. tender.undone_tp = ctx.helper.sub(ctx.helper.sub(ctx.helper.add(tender.total_price, change_tp), tender.end_contract_tp), tender.end_qc_tp);
  83. } else {
  84. if (bCalcTp) {
  85. const sum = await this.ctx.service.ledger.addUp({ tender_id: ctx.tender.id/* , is_leaf: true*/ });
  86. tender.total_price = sum.total_price;
  87. tender.deal_tp = sum.deal_tp;
  88. }
  89. tender.sum = ctx.helper.add(tender.total_price, tender.change_tp);
  90. }
  91. // const monthProgress = [];
  92. // for (const s of stages) {
  93. // if (s.s_time) {
  94. // let progress = monthProgress.find(function(x) {
  95. // return x.month === s.s_time;
  96. // });
  97. // if (!progress) {
  98. // progress = { month: s.s_time };
  99. // monthProgress.push(progress);
  100. // }
  101. // progress.tp = ctx.helper.add(ctx.helper.add(progress.tp, s.contract_tp), s.qc_tp);
  102. // }
  103. // }
  104. // monthProgress.sort(function(x, y) {
  105. // return Date.parse(x.month) - Date.parse(y.month);
  106. // });
  107. // let sum = 0;
  108. // for (const p of monthProgress) {
  109. // p.ratio = ctx.helper.mul(ctx.helper.div(p.tp, tender.sum, 4), 100);
  110. // sum = ctx.helper.add(sum, p.tp);
  111. // p.end_tp = sum;
  112. // p.end_ratio = ctx.helper.mul(ctx.helper.div(p.end_tp, tender.sum, 4), 100);
  113. // }
  114. ctx.body = {
  115. code: 0, msg: '', data: {
  116. tender,
  117. // monthProgress,
  118. // stagesEcharts: JSON.parse(JSON.stringify(stages)).reverse(),
  119. },
  120. };
  121. } catch (error) {
  122. this.log(error);
  123. ctx.body = { code: -1, msg: error.toString(), data: null };
  124. }
  125. }
  126. async advanceList(ctx) {
  127. try {
  128. const { advanceType } = ctx.query;
  129. const typeCol = advanceConst.typeCol.find(x => x.type === Number(advanceType));
  130. if (!typeCol) {
  131. ctx.body = { code: -1, msg: '预付款类型错误', data: null };
  132. return;
  133. }
  134. const { decimal } = ctx.tender.info;
  135. const advancePayTotal = ctx.tender.info.deal_param[typeCol.key + 'Advance'];
  136. const advances = await ctx.service.advance.getAdvanceList(ctx.tender.id, typeCol.type, decimal.pay ? decimal.payTp : decimal.tp, advancePayTotal);
  137. ctx.body = { code: 0, msg: '', data: advances };
  138. } catch (error) {
  139. this.log(error);
  140. ctx.body = { code: -1, msg: error.toString(), data: null };
  141. }
  142. }
  143. async advanceDetail(ctx) {
  144. try {
  145. const { id } = ctx.query;
  146. const advance = await this.service.advance.getDataById(id);
  147. if (!advance) {
  148. throw '预付款数据错误';
  149. }
  150. advance.user = await this.service.projectAccount.getAccountInfoById(advance.uid);
  151. // 读取审核人列表数据
  152. advance.auditors = await this.service.advanceAudit.getAuditors(advance.id, advance.times);
  153. advance.curAuditor = await this.service.advanceAudit.getCurAuditor(advance.id, advance.times);
  154. // 根据状态判断是否需要更新审批人列表
  155. if ((advance.status === status.uncheck || advance.status === status.checkNo) && ctx.tender.info.shenpi.advance !== shenpiConst.sp_status.sqspr) {
  156. const shenpi_status = this.ctx.tender.info.shenpi.advance;
  157. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  158. const auditList = await this.service.advanceAudit.getAllDataByCondition({ where: { vid: advance.id, times: advance.times }, orders: [['order', 'asc']] });
  159. const auditIdList = _.map(auditList, 'audit_id');
  160. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  161. const shenpiList = await this.service.shenpiAudit.getAllDataByCondition({ where: { tid: advance.tid, sp_type: shenpiConst.sp_type.advance, sp_status: shenpi_status } });
  162. const shenpiIdList = _.map(shenpiList, 'audit_id');
  163. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  164. if (!_.isEqual(auditIdList, shenpiIdList)) {
  165. await this.service.advanceAudit.updateNewAuditList(advance, shenpiIdList);
  166. }
  167. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  168. const shenpiInfo = await this.service.shenpiAudit.getDataByCondition({ tid: advance.tid, sp_type: shenpiConst.sp_type.advance, sp_status: shenpi_status });
  169. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  170. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  171. await this.service.advanceAudit.updateLastAudit(advance, auditList, shenpiInfo.audit_id);
  172. } else if (!shenpiInfo) {
  173. // 不存在终审人的状态下这里恢复为授权审批人
  174. this.ctx.tender.info.shenpi.advance = shenpiConst.sp_status.sqspr;
  175. }
  176. }
  177. }
  178. advance.advancePayTotal = ctx.tender.info.deal_param[ advanceConst.typeCol[advance.type].key + 'Advance'];
  179. const times = advance.status === auditConst.advance.status.checkNo ? advance.times - 1 : advance.times;
  180. if (advance.status === auditConst.advance.status.checkNo) {
  181. advance.curAuditor = await ctx.service.advanceAudit.getAuditorByStatus(advance.id, advance.status, times);
  182. advance.auditors = await ctx.service.advanceAudit.getAuditors(advance.id, times);
  183. }
  184. // 因为上报人不在审核列表里面单独添加进去
  185. // advance.auditors.unshirt({
  186. // audit_id: advance.uid,
  187. // name: advance.user.name,
  188. // company: advance.user.company,
  189. // create_time: advance.pay_time,
  190. // end_time: advance.auditors.length ? advance.auditors[0].create_time : null,
  191. // mobile: '',
  192. // })
  193. ctx.body = { code: 0, msg: '', data: { advance } };
  194. } catch (error) {
  195. this.log(error);
  196. ctx.body = { code: -1, msg: error.toString(), data: null };
  197. }
  198. }
  199. }
  200. return WeappTenderController;
  201. };