advance.js 8.6 KB

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