advance.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. const auditConst = require('../const/audit').advance;
  3. const bc = require('../lib/base_calc.js');
  4. module.exports = app => {
  5. class Advance extends app.BaseService {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.tableName = 'advance_pay';
  9. }
  10. /**
  11. * 获取预付款列表
  12. * @param {Number} tid 标段id
  13. * @param {Number} type 预付款类型
  14. */
  15. async getAdvanceList(tid, type) {
  16. this.initSqlBuilder();
  17. this.sqlBuilder.setAndWhere('tid', {
  18. value: tid,
  19. operate: '=',
  20. });
  21. this.sqlBuilder.setAndWhere('type', {
  22. value: type,
  23. operate: '=',
  24. });
  25. this.sqlBuilder.orderBy = [['order', 'desc']];
  26. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  27. const advance = await this.db.query(sql, sqlParam);
  28. for (const item of advance) {
  29. item.curAuditor = await this.ctx.service.advanceAudit.getAuditorByStatus(item.id, item.status, item.times);
  30. if (item.status === auditConst.status.checkNoPre) {
  31. item.curAuditor2 = await this.ctx.service.advanceAudit.getAuditorByStatus(item.id, auditConst.status.checking);
  32. }
  33. }
  34. return advance;
  35. }
  36. /**
  37. * 获取预付款最新一期
  38. * @param {Number} tid 标段id
  39. * @param {String} type 类型: 开工预付款|材料预付款 (0|1)
  40. * @param {Boolean} includeUnCheck 包括未上报的
  41. * @return {Promise<*>} 实例结果集
  42. */
  43. async getLastestAdvance(tid, type, includeUnCheck = false) {
  44. this.initSqlBuilder();
  45. this.sqlBuilder.setAndWhere('tid', {
  46. value: tid,
  47. operate: '=',
  48. });
  49. this.sqlBuilder.setAndWhere('type', {
  50. value: type,
  51. operate: '=',
  52. });
  53. if (!includeUnCheck) {
  54. this.sqlBuilder.setAndWhere('status', {
  55. value: auditConst.status.uncheck,
  56. operate: '!=',
  57. });
  58. }
  59. this.sqlBuilder.orderBy = [['order', 'desc']];
  60. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  61. const advance = await this.db.queryOne(sql, sqlParam);
  62. return advance;
  63. }
  64. /**
  65. * 创建一条新的记录
  66. * @param {String} type 类型: 开工预付款|材料预付款 (start|material)
  67. * @return {Promise<*>} 插入结果集
  68. */
  69. async createRecord(type) {
  70. const { ctx } = this;
  71. const uid = ctx.session.sessionUser.accountId;
  72. const tid = ctx.tender.id;
  73. const latestOrder = await this.getLastestAdvance(tid, type);
  74. const order = !latestOrder ? 1 : latestOrder.order + 1;
  75. const record = await this.db.insert(this.tableName, { type, uid, tid, status: auditConst.status.uncheck, order });
  76. return await this.getDataById(record.insertId);
  77. }
  78. /**
  79. * 获取上一期预付款记录
  80. * @param {Number} tid 标段id
  81. * @param {Number} type 预付款类型
  82. */
  83. async getPreviousRecord(tid, type) {
  84. this.initSqlBuilder();
  85. this.sqlBuilder.setAndWhere('tid', {
  86. value: tid,
  87. operate: '=',
  88. });
  89. this.sqlBuilder.setAndWhere('type', {
  90. value: type,
  91. operate: '=',
  92. });
  93. this.sqlBuilder.setAndWhere('order', {
  94. value: this.ctx.advance.order - 1,
  95. operate: '=',
  96. });
  97. this.sqlBuilder.orderBy = [['order', 'desc']];
  98. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  99. return await this.db.queryOne(sql, sqlParam);
  100. }
  101. /**
  102. * 计算列表进度条所需数据
  103. * @param {Object} latest 最新期的数据
  104. * @param {Number} payTotal 预付款金额总数
  105. * @return {Object} progress所需数据
  106. */
  107. async calcProgress(latest, payTotal) {
  108. const { ctx } = this;
  109. if (!latest) {
  110. latest = { prev_total_amount: 0, prev_amount: 0, cur_amount: 0 };
  111. }
  112. const surplus = ctx.helper.sub(payTotal, latest.prev_total_amount || 0);
  113. const p_ratio = ctx.helper.mul(ctx.helper.div(latest.prev_amount || 0, payTotal), 100, 2); // 截止上期金额总数
  114. const c_ratio = ctx.helper.mul(ctx.helper.div(latest.cur_amount || 0, payTotal), 100, 0); // 截止本期金额总数
  115. const s_ratio = ctx.helper.mul(ctx.helper.div(surplus, payTotal), 100, 0); // 剩余金额总数
  116. return {
  117. p_amount: latest.prev_amount,
  118. c_amount: latest.cur_amount,
  119. s_amount: surplus,
  120. p_ratio,
  121. c_ratio,
  122. s_ratio,
  123. };
  124. }
  125. /**
  126. * 更新预付款记录
  127. * @param {Object} condition 载荷
  128. * @param {Number} id 预付款id
  129. */
  130. async updateAdvance(condition, id) {
  131. return await this.update(condition, {
  132. id,
  133. });
  134. }
  135. }
  136. return Advance;
  137. };