123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- 'use strict';
- const auditConst = require('../const/audit').advance;
- module.exports = app => {
- class Advance extends app.BaseService {
- constructor(ctx) {
- super(ctx);
- this.tableName = 'advance_pay';
- }
- /**
- * 获取预付款列表
- * @param {Number} tid 标段id
- * @param {Number} type 预付款类型
- */
- async getAdvanceList(tid, type) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('tid', {
- value: tid,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('type', {
- value: type,
- operate: '=',
- });
- if (this.ctx.session.sessionUser.accountId !== this.ctx.tender.data.user_id) {
- this.sqlBuilder.setAndWhere('status', {
- value: auditConst.status.uncheck,
- operate: '!=',
- });
- }
- this.sqlBuilder.orderBy = [['order', 'desc']];
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- console.log(sql);
- const advance = await this.db.query(sql, sqlParam);
- for (const item of advance) {
- item.curAuditor = await this.ctx.service.advanceAudit.getAuditorByStatus(item.id, item.status, item.times);
- if (item.status === auditConst.status.checkNoPre) {
- item.curAuditor2 = await this.ctx.service.advanceAudit.getAuditorByStatus(item.id, auditConst.status.checking);
- }
- item.fileList = await this.ctx.service.advanceFile.getAdvanceFiles({ vid: item.id });
- }
- return advance;
- }
- /**
- * 获取预付款最新一期
- * @param {Number} tid 标段id
- * @param {String} type 类型: 开工预付款|材料预付款 (0|1)
- * @param {Boolean} includeUnCheck 包括未上报的
- * @return {Promise<*>} 实例结果集
- */
- async getLastestAdvance(tid, type, includeUnCheck = false) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('tid', {
- value: tid,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('type', {
- value: type,
- operate: '=',
- });
- if (!includeUnCheck) {
- this.sqlBuilder.setAndWhere('status', {
- value: auditConst.status.uncheck,
- operate: '!=',
- });
- }
- this.sqlBuilder.orderBy = [['order', 'desc']];
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const advance = await this.db.queryOne(sql, sqlParam);
- return advance;
- }
- /**
- * 创建一条新的记录
- * @param {String} type 类型: 开工预付款|材料预付款 (start|material)
- * @return {Promise<*>} 插入结果集
- */
- async createRecord(type) {
- const { ctx } = this;
- const uid = ctx.session.sessionUser.accountId;
- const tid = ctx.tender.id;
- let latestOrder = await this.getLastestAdvance(tid, type);
- if (!latestOrder) {
- latestOrder = {
- order: 1,
- prev_amount: 0.00,
- prev_total_amount: 0.00,
- };
- } else {
- latestOrder.order = latestOrder.order + 1;
- }
- 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 });
- return await this.getDataById(record.insertId);
- }
- /**
- * 获取上一期预付款记录
- * @param {Number} tid 标段id
- * @param {Number} type 预付款类型
- */
- async getPreviousRecord(tid, type) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('tid', {
- value: tid,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('type', {
- value: type,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('order', {
- value: this.ctx.advance.order - 1,
- operate: '=',
- });
- this.sqlBuilder.orderBy = [['order', 'desc']];
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- return await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 计算列表进度条所需数据
- * @param {Object} latest 最新期的数据
- * @param {Number} payTotal 预付款金额总数
- * @return {Object} progress所需数据
- */
- async calcProgress(latest, payTotal) {
- const { ctx } = this;
- if (!latest) {
- latest = { prev_total_amount: 0, prev_amount: 0, cur_amount: 0 };
- }
- const surplus = ctx.helper.sub(payTotal, latest.prev_total_amount || 0);
- const p_ratio = ctx.helper.mul(ctx.helper.div(latest.prev_amount || 0, payTotal), 100, 2); // 截止上期金额总数
- const c_ratio = ctx.helper.mul(ctx.helper.div(latest.cur_amount || 0, payTotal), 100, 2); // 截止本期金额总数
- const s_ratio = ctx.helper.mul(ctx.helper.div(surplus, payTotal), 100, 2); // 剩余金额总数
- return {
- p_amount: latest.prev_amount,
- c_amount: latest.cur_amount,
- s_amount: surplus,
- p_ratio,
- c_ratio,
- s_ratio,
- };
- }
- /**
- * 更新预付款记录
- * @param {Object} payload 载荷
- * @param {Number} id 预付款id
- */
- async updateAdvance(payload, id) {
- const { ctx } = this;
- const prevRecord = await this.getPreviousRecord(ctx.tender.id, ctx.advance.type) || { prev_total_amount: 0 };
- const { cur_amount } = payload;
- payload.prev_total_amount = ctx.helper.add(cur_amount, prevRecord.prev_total_amount);
- return await this.update(payload, {
- id,
- });
- }
- }
- return Advance;
- };
|