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