tender_info.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, this.ctx.session.sessionUser.is_admin);
  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.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(this.tender, this.stage.preCheckedStage.order) : [];
  42. const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.stage.id } });
  43. this.ctx.helper.assignRelaData(billsData, [
  44. { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  45. { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  46. { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
  47. ]);
  48. }
  49. return billsData;
  50. }
  51. _getGclChapter() {
  52. const gclChapter = [];
  53. for (const c of this.ctx.tender.info.chapter) {
  54. const cc = { code: c.code, name: c.name, cType: 1 };
  55. const filter = '^[\\D]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)';
  56. cc.reg = new RegExp(filter);
  57. gclChapter.push(cc);
  58. }
  59. return gclChapter;
  60. }
  61. _findGclChapter(chapters, data, field) {
  62. for (const c of chapters) {
  63. if (c.reg && c.reg.test(data[field])) {
  64. return c;
  65. }
  66. }
  67. return null;
  68. }
  69. async gatherChapter() {
  70. await this._checkStage();
  71. const billsData = await this._getStageBillsData();
  72. const gclChapter = this._getGclChapter();
  73. for (const b of billsData) {
  74. if (!b.b_code || !b.is_leaf) continue;
  75. const chapter = this._findGclChapter(gclChapter, b, 'b_code');
  76. if (!chapter) continue;
  77. chapter.total_price = this.ctx.helper.add(chapter.total_price, b.total_price);
  78. chapter.contract_tp = this.ctx.helper.sum([chapter.contract_tp, b.contract_tp, b.contract_pc_tp]);
  79. chapter.qc_tp = this.ctx.helper.sum([chapter.qc_tp, b.qc_tp, b.qc_pc_tp]);
  80. chapter.pre_contract_tp = this.ctx.helper.add(chapter.pre_contract_tp, b.pre_contract_tp);
  81. chapter.pre_qc_tp = this.ctx.helper.add(chapter.pre_qc_tp, b.pre_qc_tp);
  82. }
  83. for (const c of gclChapter) {
  84. if (!c.total_price) c.total_price = 0;
  85. if (!c.contract_tp) c.contract_tp = 0;
  86. if (!c.qc_tp) c.qc_tp = 0;
  87. if (!c.pre_contract_tp) c.pre_contract_tp = 0;
  88. if (!c.pre_qc_tp) c.pre_qc_tp = 0;
  89. c.end_contract_tp = this.ctx.helper.add(c.pre_contract_tp, c.contract_tp);
  90. c.end_qc_tp = this.ctx.helper.add(c.pre_qc_tp, c.qc_tp);
  91. c.end_gather_tp = this.ctx.helper.add(c.end_qc_tp, c.end_contract_tp);
  92. delete c.reg;
  93. }
  94. return gclChapter;
  95. }
  96. }
  97. module.exports = TenderInfo;