weapp_tender_controller.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. const auditConst = require('../const/audit');
  3. const measureType = require('../const/tender').measureType;
  4. module.exports = app => {
  5. class WeappTenderController extends app.BaseController {
  6. async listManage(ctx) {
  7. try {
  8. const projectId = ctx.query.projectId;
  9. if (!projectId) {
  10. ctx.body = { err: 1, msg: '缺少projectId参数', data: null };
  11. return;
  12. }
  13. const accountInfo = ctx.session.sessionUser;
  14. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  15. if (userPermission !== null && userPermission.tender !== undefined && userPermission.tender.indexOf('1') !== -1) {
  16. const subProject = await this.ctx.service.subProject.getDataById(projectId);
  17. const tenderList = await this.ctx.service.tender.getList('manage', userPermission, ctx.session.sessionUser.is_admin, '', subProject);
  18. const categoryData = await this.ctx.service.category.getAllCategory(subProject);
  19. const renderData = {
  20. categoryData,
  21. tenderList,
  22. };
  23. ctx.body = { err: 0, msg: '', data: renderData };
  24. } else {
  25. ctx.body = { err: 1, msg: '您没有管理权限', data: null };
  26. }
  27. } catch (err) {
  28. this.log(err);
  29. ctx.body = { err: 1, msg: err.toString(), data: null };
  30. }
  31. }
  32. async detail(ctx) {
  33. try {
  34. const { id: tenderId } = ctx.query;
  35. const tender = await this.service.tender.getTender(tenderId);
  36. const tenderInfo = await this.service.tenderInfo.getTenderInfo(tenderId);
  37. if (!tender || !tenderInfo) {
  38. throw '标段信息不存在';
  39. }
  40. let bCalcTp = tender.user_id === ctx.session.sessionUser.accountId && (
  41. tender.ledger_status === auditConst.ledger.status.checkNo || tender.ledger_status === auditConst.ledger.status.uncheck);
  42. const stages = await ctx.service.stage.getValidStages(tenderId);
  43. const lastStage = stages.length > 0 ? stages[0] : null; // await ctx.service.stage.getLastestStage(ctx.tender.id);
  44. const [change_tp, change_p_tp, change_n_tp, change_valuation_tp, change_unvaluation_tp] = await ctx.service.change.getChangeTp(tenderId);
  45. tender.change_tp = change_tp;
  46. tender.change_p_tp = change_p_tp;
  47. tender.change_n_tp = change_n_tp;
  48. tender.change_valuation_tp = change_valuation_tp;
  49. tender.change_unvaluation_tp = change_unvaluation_tp;
  50. if (lastStage) {
  51. await this.ctx.service.stage.checkStageGatherData(lastStage, ctx.session.sessionUser.is_admin);
  52. if ((!bCalcTp) && tender.measure_type === measureType.gcl.value) {
  53. bCalcTp = lastStage.status !== auditConst.stage.status.checked && !lastStage.readOnly;
  54. }
  55. if (bCalcTp) {
  56. const sum = await this.ctx.service.ledger.addUp({ tender_id: tenderId/* , is_leaf: true*/ });
  57. tender.total_price = sum.total_price;
  58. tender.deal_tp = sum.deal_tp;
  59. }
  60. tender.sum = tenderInfo.calc_type === 'tp' ? ctx.helper.add(tender.total_price, tender.change_valuation_tp) : ctx.helper.add(tender.total_price, tender.change_tp);
  61. tender.gather_tp = ctx.helper.sum([lastStage.contract_tp, lastStage.qc_tp, lastStage.pc_tp]);
  62. tender.end_contract_tp = ctx.helper.sum([lastStage.contract_tp, lastStage.pre_contract_tp, lastStage.contract_pc_tp]);
  63. tender.end_qc_tp = ctx.helper.sum([lastStage.qc_tp, lastStage.pre_qc_tp, lastStage.qc_pc_tp]);
  64. tender.end_positive_qc_tp = ctx.helper.sum([lastStage.positive_qc_tp, lastStage.pre_positive_qc_tp, lastStage.positive_qc_pc_tp]);
  65. tender.end_negative_qc_tp = ctx.helper.sum([lastStage.negative_qc_tp, lastStage.pre_negative_qc_tp, lastStage.negative_qc_pc_tp]);
  66. tender.end_gather_tp = ctx.helper.add(tender.end_contract_tp, tender.end_qc_tp);
  67. tender.pre_gather_tp = ctx.helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
  68. tender.yf_tp = lastStage.yf_tp;
  69. tender.sf_tp = lastStage.sf_tp;
  70. tender.qc_ratio = ctx.helper.mul(ctx.helper.div(tender.end_qc_tp, tenderInfo.deal_param.contractPrice, 2), 100);
  71. tender.pre_ratio = ctx.helper.mul(ctx.helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
  72. tender.cur_ratio = ctx.helper.mul(ctx.helper.div(tender.gather_tp, tender.sum, 2), 100);
  73. tender.other_tp = ctx.helper.sub(ctx.helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
  74. tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
  75. tender.end_yf_tp = ctx.helper.add(lastStage.yf_tp, lastStage.pre_yf_tp);
  76. tender.end_sf_tp = ctx.helper.add(lastStage.sf_tp, lastStage.pre_sf_tp);
  77. 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);
  78. } else {
  79. if (bCalcTp) {
  80. const sum = await this.ctx.service.ledger.addUp({ tender_id: tenderId/* , is_leaf: true*/ });
  81. tender.total_price = sum.total_price;
  82. tender.deal_tp = sum.deal_tp;
  83. }
  84. tender.sum = ctx.helper.add(tender.total_price, tender.change_tp);
  85. }
  86. const monthProgress = [];
  87. for (const s of stages) {
  88. if (s.s_time) {
  89. let progress = monthProgress.find(function (x) {
  90. return x.month === s.s_time;
  91. });
  92. if (!progress) {
  93. progress = { month: s.s_time };
  94. monthProgress.push(progress);
  95. }
  96. progress.tp = ctx.helper.add(ctx.helper.add(progress.tp, s.contract_tp), s.qc_tp);
  97. }
  98. }
  99. monthProgress.sort(function (x, y) {
  100. return Date.parse(x.month) - Date.parse(y.month);
  101. });
  102. let sum = 0;
  103. for (const p of monthProgress) {
  104. p.ratio = ctx.helper.mul(ctx.helper.div(p.tp, tender.sum, 4), 100);
  105. sum = ctx.helper.add(sum, p.tp);
  106. p.end_tp = sum;
  107. p.end_ratio = ctx.helper.mul(ctx.helper.div(p.end_tp, tender.sum, 4), 100);
  108. }
  109. ctx.body = {
  110. code: 0, msg: '', data: {
  111. tender,
  112. // monthProgress,
  113. // stagesEcharts: JSON.parse(JSON.stringify(stages)).reverse(),
  114. },
  115. };
  116. } catch (error) {
  117. this.log(error);
  118. ctx.body = { code: -1, msg: error.toString(), data: null };
  119. }
  120. }
  121. }
  122. return WeappTenderController;
  123. };