advance.js 6.8 KB

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