advance.js 7.8 KB

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