weapp_tender_controller.js 12 KB

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