weapp_tender_controller.js 9.3 KB

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