advance.js 6.3 KB

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