advance.js 9.5 KB

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