advance.js 5.7 KB

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