123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const audit = require('../const/audit').phasePay;
- const projectLogConst = require('../const/project_log');
- const shenpiConst = require('../const/shenpi');
- const calcBase = [
- {name: '签约合同价', code: 'htj', sort: 10},
- {name: '暂列金额', code: 'zlje', sort: 2},
- {name: '签约合同价(不含暂列金)', code: 'htjszl', sort: 1},
- {name: '签约开工预付款', code: 'kgyfk', sort: 2},
- {name: '签约材料预付款', code: 'clyfk', sort: 2},
- {name: '本期完成计量', code: 'bqwc', limit: true, sort: 10, checkStart: 'gather_tp'},
- {name: '本期合同计量', code: 'bqht', limit: true, sort: 10, checkStart: 'contract_tp'},
- {name: '本期变更计量', code: 'bqbg', limit: true, sort: 10},
- {name: '本期清单完成计量', code: 'bqqdwc', limit: true, sort: 10},
- {name: '本期清单合同计量', code: 'bqqdht', limit: true, sort: 10},
- {name: '本期清单变更计量', code: 'bqqdbg', limit: true, sort: 10},
- {name: '本期一般变更计量', code: 'ybbqbg', limit: true, sort: 5},
- {name: '本期较大变更计量', code: 'jdbqbg', limit: true, sort: 5},
- {name: '本期重大变更计量', code: 'zdbqbg', limit: true, sort: 5},
- {name: '100章本期完成计量', code: 'ybbqwc', limit: true, sort: 1},
- {name: '本期应付', code: 'bqyf', limit: true, ptNormalLimit: true, sort: 20},
- {name: '奖金', code: 'bonus', limit: true, sort: 1},
- {name: '罚金', code: 'fine', limit: true, sort: 1},
- {name: '甲供材料', code: 'jgcl', limit: true, sort: 1},
- ];
- module.exports = app => {
- class PhasePay extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'phase_pay';
- }
- analysisPhasePay(data) {
- if (!data) return;
- const datas = data instanceof Array ? data : [data];
- datas.forEach(x => {
- x.rela_stage = x.rela_stage ? JSON.parse(x.rela_stage) : [];
- x.calc_base = x.calc_base ? JSON.parse(x.calc_base) : [];
- });
- }
- calculatePhasePay(data) {
- const helper = this.ctx.helper;
- const datas = data instanceof Array ? data : [data];
- const formatNum = this.ctx.tender.info.display.thousandth ? this.ctx.helper.formatNum : function(num) { return num ? num + '' : ''; };
- datas.forEach(x => {
- x.end_calc_tp = helper.add(x.calc_tp, x.pre_calc_tp);
- x.end_pay_tp = helper.add(x.pay_tp, x.pre_pay_tp);
- x.end_cut_tp = helper.add(x.cut_tp, x.pre_cut_tp);
- x.end_sf_tp = helper.add(x.sf_tp, x.pre_sf_tp);
- x.end_yf_tp = helper.add(x.yf_tp, x.pre_yf_tp);
- for (const prop in x) {
- if (prop.indexOf('_tp') > 0) {
- x['display_' + prop] = formatNum(x[prop]);
- }
- }
- });
- }
- /**
- * 获取全部修订
- * @param tid
- * @returns {Promise<*>}
- */
- async getAllPhasePay (tid, sort = 'ASC') {
- const result = await this.getAllDataByCondition({
- where: {tid: tid},
- orders: [['phase_order', sort]],
- });
- this.analysisPhasePay(result);
- return result;
- }
- async getPhasePay(id) {
- const result = await this.getDataById(id);
- this.analysisPhasePay(result);
- return result;
- }
- async getPhasePayByOrder(tid, phaseOrder) {
- const result = await this.getDataByCondition({ tid, phase_order: phaseOrder });
- this.analysisPhasePay(result);
- return result;
- }
- async getMaxOrder(tid) {
- const sql = 'SELECT Max(`phase_order`) As max_order FROM ' + this.tableName + ' Where `tid` = ?';
- const sqlParam = [tid];
- const result = await this.db.queryOne(sql, sqlParam);
- return result.max_order || 0;
- }
- async _checkRelaStageConflict(relaStage, phasePay) {
- const pays = await this.getAllPhasePay(phasePay.tid);
- for (const p of pays) {
- if (p.id === phasePay.id) continue;
- for (const s of relaStage) {
- if (p.rela_stage.find(x => { return x.id === s.id; })) return true;
- }
- }
- return false;
- }
- async getCalcBase(relaStage, prePhase) {
- const result = {};
- for (const stage of relaStage) {
- result.contract_tp = this.ctx.helper.add(result.contract_tp, stage.contract_tp);
- result.qc_tp = this.ctx.helper.add(result.contract_tp, stage.qc_tp);
- result.pc_tp = this.ctx.helper.add(result.pc_tp, stage.pc_tp);
- const qdSum = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage);
- result.qd_contract_tp = qdSum.contract_tp || 0;
- result.qd_qc_tp = qdSum.qc_tp || 0;
- result.qd_pc_tp = qdSum.pc_tp || 0;
- const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
- const sumPc = await this.ctx.service.stageBillsPc.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
- result.gather_100_tp = this.ctx.helper.sum([sumGcl.contract_tp, sumGcl.qc_tp, sumPc.pc_tp]);
- const bg = await this.ctx.service.stage.getChangeSubtotal(stage);
- result.common_bg_tp = bg.common || 0;
- result.more_bg_tp = bg.more || 0;
- result.great_bg_tp = bg.great || 0;
- }
- result.gather_tp = this.ctx.helper.sum([result.contract_tp, result.qc_tp, result.pc_tp]) || 0;
- result.qd_gather_tp = this.ctx.helper.sum([result.qd_contract_tp, result.qd_qc_tp, result.qd_pc_tp]) || 0;
- const bonusSum = await this.ctx.service.stageBonus.getSumTp(relaStage);
- result.bonus_positive_tp = bonusSum.positive_tp || 0;
- result.bonus_negative_tp = bonusSum.negative_tp || 0;
- result.bonus_tp = bonusSum.sum_tp || 0;
- const jgclSum = await this.ctx.service.stageJgcl.getSumTp(relaStage);
- result.jgcl_tp = jgclSum.sum_tp || 0;
- const otherSum = await this.ctx.service.stageOther.getSumTp(relaStage);
- result.other_tp = otherSum.sum_tp || 0;
- const safeProdSum = await this.ctx.service.stageSafeProd.getSumTp(relaStage);
- result.safe_prod_tp = safeProdSum.sum_tp || 0;
- const tempLandSum = await this.ctx.service.stageTempLand.getSumTp(relaStage);
- result.temp_land_tp = tempLandSum.sum_tp || 0;
- if (prePhase && prePhase.calc_base) {
- result.pre_contract_tp = this.ctx.helper.add(prePhase.calc_base.contract_tp, prePhase.calc_base.pre_contract_tp);
- result.pre_qc_tp = this.ctx.helper.add(prePhase.calc_base.qc_tp, prePhase.calc_base.pre_qc_tp);
- result.pre_pc_tp = this.ctx.helper.add(prePhase.calc_base.pc_tp, prePhase.calc_base.pre_pc_tp);
- result.pre_gather_tp = this.ctx.helper.add(prePhase.calc_base.gather_tp, prePhase.calc_base.pre_gather_tp);
- result.pre_qd_contract_tp = this.ctx.helper.add(prePhase.calc_base.qd_contract_tp, prePhase.calc_base.pre_qd_contract_tp);
- result.pre_qd_qc_tp = this.ctx.helper.add(prePhase.calc_base.qd_qc_tp, prePhase.calc_base.pre_qd_qc_tp);
- result.pre_qd_pc_tp = this.ctx.helper.add(prePhase.calc_base.qd_pc_tp, prePhase.calc_base.pre_qd_pc_tp);
- result.pre_qd_gather_tp = this.ctx.helper.add(prePhase.calc_base.qd_gather_tp, prePhase.calc_base.pre_qd_gather_tp);
- result.pre_bonus_positive_tp = this.ctx.helper.add(prePhase.calc_base.bonus_positive_tp, prePhase.calc_base.pre_bonus_positive_tp);
- result.pre_bonus_negative_tp = this.ctx.helper.add(prePhase.calc_base.bonus_negative_tp, prePhase.calc_base.pre_bonus_negative_tp);
- result.pre_bonus_tp = this.ctx.helper.add(prePhase.calc_base.bonus_tp, prePhase.calc_base.pre_bonus_tp);
- result.pre_jgcl_tp = this.ctx.helper.add(prePhase.calc_base.jgcl_tp, prePhase.calc_base.pre_jgcl_tp);
- result.pre_other_tp = this.ctx.helper.add(prePhase.calc_base.other_tp, prePhase.calc_base.pre_other_tp);
- result.pre_safe_prod_tp = this.ctx.helper.add(prePhase.calc_base.safe_prod_tp, prePhase.calc_base.pre_safe_prod_tp);
- result.pre_temp_land_tp = this.ctx.helper.add(prePhase.calc_base.temp_land_tp, prePhase.calc_base.pre_temp_land_tp);
- }
- return result;
- }
- /**
- * 获取 当期的 计算基数
- * @return {Promise<any>}
- */
- getPhasePayCalcBase(phasePay, tenderInfo) {
- const payCalcBase = JSON.parse(JSON.stringify(calcBase));
- for (const cb of payCalcBase) {
- switch (cb.code) {
- case 'htj':
- cb.value = tenderInfo.deal_param.contractPrice;
- break;
- case 'zlje':
- cb.value = tenderInfo.deal_param.zanLiePrice;
- break;
- case 'htjszl':
- cb.value = this.ctx.helper.sub(tenderInfo.deal_param.contractPrice, tenderInfo.deal_param.zanLiePrice);
- break;
- case 'kgyfk':
- cb.value = tenderInfo.deal_param.startAdvance;
- break;
- case 'clyfk':
- cb.value = tenderInfo.deal_param.materialAdvance;
- break;
- case 'bqwc':
- cb.value = phasePay.calc_base.gather_tp;
- break;
- case 'bqht':
- cb.value = phasePay.calc_base.contract_tp;
- break;
- case 'bqbg':
- cb.value = phasePay.calc_base.qc_tp;
- break;
- case 'bqqdwc':
- cb.value = phasePay.calc_base.qd_gather_tp;
- break;
- case 'bqqdht':
- cb.value = phasePay.calc_base.qd_contract_tp;
- break;
- case 'bqqdbg':
- cb.value = phasePay.calc_base.qd_qc_tp;
- break;
- case 'ybbqwc':
- cb.value = phasePay.calc_base.gather_100_tp;
- break;
- case 'ybbqbg':
- cb.value = phasePay.calc_base.common_bg_tp;
- break;
- case 'jdbqbg':
- cb.value = phasePay.calc_base.more_bg_tp;
- break;
- case 'zdbqbg':
- cb.value = phasePay.calc_base.great_bg_tp;
- break;
- case 'bonus':
- cb.value = phasePay.calc_base.bonus_positive_tp;
- break;
- case 'fine':
- cb.value = phasePay.calc_base.bonus_negative_tp;
- break;
- case 'jgcl':
- cb.value = phasePay.calc_base.jgcl_tp;
- break;
- case 'aqsc':
- cb.value = phasePay.calc_base.safe_prod_tp;
- break;
- case 'lsyd':
- cb.value = phasePay.calc_base.temp_land_tp;
- break;
- default:
- cb.value = 0;
- }
- }
- return payCalcBase;
- }
- async add(tid, relaStage, phaseDate, memo) {
- if (!tid) throw '数据错误';
- const user_id = this.ctx.session.sessionUser.accountId;
- const maxOrder = await this.getMaxOrder();
- const data = {
- id: this.uuid.v4(), tid: tid, create_user_id: user_id, update_user_id: user_id,
- phase_order: maxOrder + 1, phase_date: phaseDate, memo,
- audit_times: 1, audit_status: audit.status.uncheck,
- rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
- };
- if (await this._checkRelaStageConflict(relaStage, data)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
- const prePhase = maxOrder > 0 ? await this.getPhasePayByOrder(tid, maxOrder) : null;
- const calcBase = await this.getCalcBase(relaStage, prePhase);
- data.calc_base = JSON.stringify(calcBase);
- const transaction = await this.db.beginTransaction();
- try {
- const result = await transaction.insert(this.tableName, data);
- if (result.affectedRows !== 1) throw '新增合同支付失败';
- await this.ctx.service.phasePayDetail.initPhaseData(transaction, data);
- await transaction.commit();
- return data;
- } catch(err) {
- await transaction.rollback();
- throw err;
- }
- }
- async delete(id) {
- const info = await this.getDataById(id);
- const conn = await this.db.beginTransaction();
- try {
- await conn.delete(this.tableName, { id });
- await conn.delete(this.ctx.service.phasePayDetail.tableName, { phase_id: id });
- // todo 不确定是否真删除,是否需要防止用户找回附件 ?
- // await conn.delete(this.ctx.service.phasePayAtt.tableName, { phase_id: id });
- await conn.delete(this.ctx.service.phasePayAudit.tableName, { phase_id: id });
- // 记录删除日志
- await this.ctx.service.projectLog.addProjectLog(conn, projectLogConst.type.phasePay, projectLogConst.status.delete, `第${info.phase_order}期`);
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- }
- async save(phasePay, data) {
- await this.defaultUpdate({id: phasePay.id, phase_date: data.phase_date, memo: data.memo});
- }
- async refreshCalcBase(phasePay) {
- const prePhase = phasePay.phase_order > 1 ? await this.getPhasePayByOrder(phasePay.tid, phasePay.phase_order - 1) : null;
- const relaStage = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: phasePay.tid, order: phasePay.rela_stage.map(x => { return x.stage_order; }) } });
- const calcBase = await this.getCalcBase(relaStage, prePhase);
- const conn = await this.db.beginTransaction();
- try {
- await conn.update(this.tableName, {
- id: phasePay.id, update_user_id: this.ctx.session.sessionUser.accountId,
- calc_base: JSON.stringify(calcBase),
- calc_base_time: new Date()
- });
- phasePay.calc_base = calcBase;
- await this.ctx.service.phasePayDetail.calculateSave(phasePay, conn);
- await conn.commit();
- } catch(err) {
- await conn.rollback();
- throw err;
- }
- }
- async resetRelaStageId(phasePay, relaStage) {
- if (await this._checkRelaStageConflict(relaStage, phasePay)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
- const prePhase = prePhase.phase_order > 1 ? await this.getPhasePayByOrder(phasePay.tid, phasePay.phase_order - 1) : null;
- const calcBase = await this.getCalcBase(relaStage, prePhase);
- const rela_stage = relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; });
- const conn = await this.db.beginTransaction();
- try {
- await conn.update(this.tableName, {
- id: phasePay.id, update_user_id: this.ctx.session.sessionUser.accountId,
- calc_base: JSON.stringify(calcBase),
- rela_stage: JSON.stringify(rela_stage),
- calc_base_time: new Date()
- });
- phasePay.calc_base = calcBase;
- phasePay.rela_stage = relaStage;
- await this.ctx.service.phasePayDetail.calculateSave(phasePay, conn);
- await conn.commit();
- } catch(err) {
- await conn.rollback();
- throw err;
- }
- }
- async loadUser(phasePay) {
- phasePay.user = await this.ctx.service.projectAccount.getAccountInfoById(phasePay.create_user_id);
- phasePay.auditors = await this.ctx.service.phasePayAudit.getAuditors(phasePay.id, phasePay.curTimes);
- phasePay.curAuditors = phasePay.auditors.filter(x => { return x.audit_status === audit.status.checking; });
- phasePay.curAuditorIds = phasePay.curAuditors.map(x => { return x.audit_id; });
- phasePay.flowAuditors = phasePay.curAuditors.length === 0 ? [] : phasePay.auditors.filter(x => { return x.audit_sort === phasePay.curAuditors[0].audit_sort; });
- phasePay.flowAuditorIds = phasePay.curAuditors.map(x => { return x.audit_id; });
- phasePay.nextAuditors = phasePay.curAuditors.length > 0 ? phasePay.auditors.filter(x => { return x.audit_sort === phasePay.curAuditors[0].audit_sort + 1; }) : [];
- phasePay.nextAuditorIds = this._.map(phasePay.nextAuditors, 'audit_id');
- phasePay.auditorGroups = this.ctx.helper.groupAuditors(phasePay.auditors, 'active_order');
- phasePay.userGroups = this.ctx.helper.groupAuditorsUniq(phasePay.auditorGroups);
- phasePay.finalAuditorIds = phasePay.userGroups.length > 1 ? phasePay.userGroups[phasePay.userGroups.length - 1].map(x => { return x.audit_id; }) : [];
- }
- async doCheckPhase(phasePay) {
- const accountId = this.ctx.session.sessionUser.accountId;
- // 审批退回时,原报读取本轮流程,其他人读取上一轮流程
- if (phasePay.audit_status === audit.status.checkNo) {
- phasePay.curTimes = phasePay.create_user_id === accountId ? phasePay.audit_times : phasePay.audit_times - 1;
- } else {
- phasePay.curTimes = phasePay.audit_times;
- }
- // 加载参与人
- await this.loadUser(phasePay);
- if (phasePay.audit_status === audit.status.uncheck) {
- phasePay.readOnly = accountId !== phasePay.create_user_id;
- phasePay.curSort = 0;
- } else if (phasePay.audit_status === audit.status.checkNo) {
- phasePay.readOnly = accountId !== phasePay.create_user_id;
- if (!phasePay.readOnly) {
- phasePay.curSort = 0;
- } else {
- const checkNoAudit = await this.service.phasePayAudit.getDataByCondition({
- phase_id: phasePay.id, audit_times: phasePay.audit_times - 1, status: audit.status.checkNo,
- });
- phasePay.curSort = checkNoAudit.audit_sort;
- }
- } else if (phasePay.audit_status === audit.status.checked) {
- phasePay.readOnly = true;
- phasePay.curSort = phasePay.audit_max_sort;
- } else {
- // 会签,会签人部分审批通过时,只读,但是curSort需按原来的取值
- phasePay.curSort = phasePay.flowAuditorIds.indexOf(accountId) >= 0 ? phasePay.curAuditors[0].audit_sort : phasePay.curAuditors[0].audit_sort - 1;
- phasePay.readOnly = !_.isEqual(stage.flowAuditorIds, stage.curAuditorIds);
- phasePay.canCheck = phasePay.readOnly && stage.curAuditorIds.indexOf(accountId) > 0;
- }
- }
- async checkShenpi(phasePay) {
- const status = audit.status;
- const info = this.ctx.tender.info;
- const shenpi_status = info.shenpi.phasePay;
- if ((phasePay.audit_status === status.uncheck || phasePay.audit_status === status.checkNo) && shenpi_status !== shenpiConst.sp_status.sqspr) {
- // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
- const auditList = await this.ctx.service.phasePayAudit.getAllDataByCondition({ where: { phase_id: phasePay.id, audit_times: phasePay.audit_times }, orders: [['audit_order', 'asc']] });
- auditList.shift();
- if (shenpi_status === shenpiConst.sp_status.gdspl) {
- const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: phasePay.tid, sp_type: shenpiConst.sp_type.phasePay, sp_status: shenpi_status } });
- // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
- let sameAudit = auditList.length === shenpiList.length;
- if (sameAudit) {
- for (const audit of auditList) {
- const shenpi = shenpiList.find(x => { return x.audit_id === audit.audit_id; });
- if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
- sameAudit = false;
- break;
- }
- }
- }
- if (!sameAudit) {
- await this.ctx.service.phasePayAudit.updateNewAuditList(phasePay, shenpiList);
- await this.loadUser(phasePay);
- }
- } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
- const shenpiInfo = await this.ctx.service.shenpiAudit.getDataByCondition({ tid: phasePay.tid, sp_type: shenpiConst.sp_type.phasePay, sp_status: shenpi_status });
- // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
- const lastAuditors = auditList.filter(x => { x.active_order === auditList.active_order; });
- if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].audit_id))) {
- await this.ctx.service.phasePayAudit.updateLastAudit(phasePay, auditList, shenpiInfo.audit_id);
- await this.loadUser(phasePay);
- } else if (!shenpiInfo) {
- // 不存在终审人的状态下这里恢复为授权审批人
- this.tender.info.shenpi.phasePay = shenpiConst.sp_status.sqspr;
- }
- }
- }
- }
- }
- return PhasePay;
- };
|