tender_info.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. class TenderInfo {
  11. constructor (ctx) {
  12. if (!ctx.tender) throw '汇总标段信息错误';
  13. this.ctx = ctx;
  14. this.tender = this.ctx.tender;
  15. }
  16. async _getValidStages(tenderId) {
  17. const stages = await this.ctx.service.stage.db.select(this.ctx.service.stage.tableName, {
  18. where: { tid: tenderId },
  19. orders: [['order', 'desc']],
  20. });
  21. if (stages.length !== 0) {
  22. const lastStage = stages[0];
  23. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  24. stages.splice(0, 1);
  25. }
  26. }
  27. return stages;
  28. }
  29. async _checkStage() {
  30. if (this.stage) return;
  31. this.stages = await this._getValidStages(this.tender.id);
  32. this.stage = this.stages[0];
  33. if (this.stage) await this.ctx.service.stage.doCheckStage(this.stage);
  34. }
  35. async _getStageBillsData () {
  36. const billsData = await this.ctx.service.ledger.getData(this.tender.id);
  37. if (this.stage) {
  38. const curStage = this.stage.readOnly
  39. ? await this.ctx.service.stageBills.getAuditorStageData2(this.tender.id, this.stage.id, this.stage.curTimes, this.stage.curOrder)
  40. : await this.ctx.service.stageBills.getLastestStageData2(this.tender.id, this.stage.id);
  41. const preStage = this.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.tender, this.stage.order - 1) : [];
  42. this.ctx.helper.assignRelaData(billsData, [
  43. { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  44. { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  45. ]);
  46. }
  47. return billsData;
  48. }
  49. _getGclChapter() {
  50. const gclChapter = [];
  51. for (const c of this.ctx.tender.info.chapter) {
  52. const cc = { code: c.code, name: c.name, cType: 1 };
  53. const filter = '^[\\D]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)';
  54. cc.reg = new RegExp(filter);
  55. gclChapter.push(cc);
  56. }
  57. return gclChapter;
  58. }
  59. _findGclChapter(chapters, data, field) {
  60. for (const c of chapters) {
  61. if (c.reg && c.reg.test(data[field])) {
  62. return c;
  63. }
  64. }
  65. return null;
  66. }
  67. async gatherChapter() {
  68. await this._checkStage();
  69. const billsData = await this._getStageBillsData();
  70. const gclChapter = this._getGclChapter();
  71. for (const b of billsData) {
  72. if (!b.b_code || !b.is_leaf) continue;
  73. const chapter = this._findGclChapter(gclChapter, b, 'b_code');
  74. if (!chapter) continue;
  75. chapter.total_price = this.ctx.helper.add(chapter.total_price, b.total_price);
  76. chapter.contract_tp = this.ctx.helper.add(chapter.contract_tp, b.contract_tp);
  77. chapter.qc_tp = this.ctx.helper.add(chapter.qc_tp, b.qc_tp);
  78. chapter.pre_contract_tp = this.ctx.helper.add(chapter.pre_contract_tp, b.pre_contract_tp);
  79. chapter.pre_qc_tp = this.ctx.helper.add(chapter.pre_qc_tp, b.pre_qc_tp);
  80. }
  81. for (const c of gclChapter) {
  82. if (!c.total_price) c.total_price = 0;
  83. if (!c.contract_tp) c.contract_tp = 0;
  84. if (!c.qc_tp) c.qc_tp = 0;
  85. if (!c.pre_contract_tp) c.pre_contract_tp = 0;
  86. if (!c.pre_qc_tp) c.pre_qc_tp = 0;
  87. c.end_contract_tp = this.ctx.helper.add(c.pre_contract_tp, c.contract_tp);
  88. c.end_qc_tp = this.ctx.helper.add(c.pre_qc_tp, c.qc_tp);
  89. c.end_gather_tp = this.ctx.helper.add(c.end_qc_tp, c.end_contract_tp);
  90. delete c.reg;
  91. }
  92. return gclChapter;
  93. }
  94. }
  95. module.exports = TenderInfo;