advance.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. for (let idx = 0; idx < auditors.length; idx++) {
  116. const { audit_id } = auditors[idx];
  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. // auditors.forEach(async (auditor, idx) => {
  122. // const { audit_id } = auditor;
  123. // await ctx.service.advanceAudit.db.insert(ctx.service.advanceAudit.tableName, {
  124. // tid: latestOrder.tid, audit_id, type: latestOrder.type, vid: record.insertId, times: 1, order: idx + 1, status: 1, create_time: new Date(),
  125. // });
  126. // });
  127. return await this.getDataById(record.insertId);
  128. }
  129. /**
  130. * 获取上一期预付款记录
  131. * @param {Number} tid 标段id
  132. * @param {Number} type 预付款类型
  133. */
  134. async getPreviousRecord(tid, type) {
  135. this.initSqlBuilder();
  136. this.sqlBuilder.setAndWhere('tid', {
  137. value: tid,
  138. operate: '=',
  139. });
  140. this.sqlBuilder.setAndWhere('type', {
  141. value: type,
  142. operate: '=',
  143. });
  144. this.sqlBuilder.setAndWhere('order', {
  145. value: this.ctx.advance.order - 1,
  146. operate: '=',
  147. });
  148. this.sqlBuilder.orderBy = [['order', 'desc']];
  149. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  150. return await this.db.queryOne(sql, sqlParam);
  151. }
  152. /**
  153. * 计算列表进度条所需数据
  154. * @param {Object} latest 最新期的数据
  155. * @param {Number} payTotal 预付款金额总数
  156. * @return {Object} progress所需数据
  157. */
  158. async calcProgress(latest, payTotal) {
  159. const { ctx } = this;
  160. if (!latest) {
  161. latest = { prev_total_amount: 0, prev_amount: 0, cur_amount: 0 };
  162. }
  163. const surplus = ctx.helper.sub(payTotal, latest.prev_total_amount || 0);
  164. const p_ratio = ctx.helper.mul(ctx.helper.div(latest.prev_amount || 0, payTotal), 100, 2); // 截止上期金额总数
  165. const c_ratio = ctx.helper.mul(ctx.helper.div(latest.cur_amount || 0, payTotal), 100, 2); // 截止本期金额总数
  166. const s_ratio = ctx.helper.mul(ctx.helper.div(surplus, payTotal), 100, 2); // 剩余金额总数
  167. return {
  168. p_amount: latest.prev_amount,
  169. c_amount: latest.cur_amount,
  170. s_amount: surplus,
  171. p_ratio,
  172. c_ratio,
  173. s_ratio,
  174. };
  175. }
  176. /**
  177. * 更新预付款记录
  178. * @param {Object} payload 载荷
  179. * @param {Number} id 预付款id
  180. */
  181. async updateAdvance(payload, id) {
  182. const { ctx } = this;
  183. const prevRecord = await this.getPreviousRecord(ctx.tender.id, ctx.advance.type) || { prev_total_amount: 0 };
  184. const { cur_amount } = payload;
  185. payload.prev_total_amount = ctx.helper.add(cur_amount, prevRecord.prev_total_amount);
  186. return await this.update(payload, {
  187. id,
  188. });
  189. }
  190. }
  191. return Advance;
  192. };