123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- 'use strict';
- /**
- * 过程结算 控制器
- *
- * @author Mai
- * @date 2023/10/27
- * @version
- */
- const auditConst = require('../const/audit');
- const shenpiConst = require('../const/shenpi');
- const tenderConst = require('../const/tender');
- const measureType = tenderConst.measureType;
- const spreadConst = require('../const/spread');
- const spreadSetting = require('../lib/spread_setting');
- module.exports = app => {
- class SettleController extends app.BaseController {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- ctx.showProject = true;
- ctx.showTender = true;
- ctx.showTitle = true;
- }
- /**
- * 期列表(Get)
- * @param ctx
- * @return {Promise<void>}
- */
- async list(ctx) {
- try {
- const renderData = {
- tender: ctx.tender.data,
- preUrl: `/tender/${ctx.tender.id}/measure/stage`,
- auditConst: auditConst.settle,
- auditType: auditConst.auditType,
- };
- renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.settle.list);
- renderData.settles = await ctx.service.settle.getValidSettles(ctx.tender.id);
- for (const s of renderData.settles) {
- if (s.status === auditConst.settle.status.uncheck) {
- s.curAuditors = [];
- } else if (s.status === auditConst.settle.status.checkNo) {
- s.curAuditors = await ctx.service.settleAudit.getAuditorsByStatus(sid, s.status, s.times - 1);
- } else {
- s.curAuditors = await ctx.service.settleAudit.getAuditorsByStatus(s.id, s.status, s.times);
- }
- if (s.status === auditConst.settle.status.checkNoPre) {
- s.curAuditorsPre = await ctx.service.stageAudit.getAuditorsByStatus(s.id, auditConst.settle.status.checking, s.times);
- }
- }
- renderData.checkedStageCount = await ctx.service.stage.count({ tid: ctx.tender.id, status: auditConst.stage.status.checked });
- await this.layout('settle/list.ejs', renderData, 'settle/list_modal.ejs');
- } catch (err) {
- ctx.log(err);
- ctx.redirect(this.menu.menu.dashboard.url);
- }
- }
- /**
- * 期审批流程(Get)
- * @param ctx
- * @return {Promise<void>}
- */
- async loadAuditors(ctx) {
- try {
- const order = JSON.parse(ctx.request.body.data).order;
- const tenderId = ctx.params.id;
- const settle = await ctx.service.settle.getDataByCondition({ tid: tenderId, settle_order: order });
- await ctx.service.settle.loadRelaUser(settle);
- await ctx.service.settle.loadAuditViewData(settle);
- ctx.body = { err: 0, msg: '', data: settle };
- } catch (error) {
- ctx.log(error);
- ctx.body = { err: 1, msg: error.toString(), data: null };
- }
- }
- async add(ctx) {
- try {
- if (ctx.session.sessionUser.accountId !== ctx.tender.data.user_id) throw '您无权创建计量期';
- const date = ctx.request.body.date;
- const period = ctx.request.body.period;
- if (!date || !period) throw '请选择结算年月和结算周期';
- const newSettle = await ctx.service.settle.addSettle(ctx.tender.id, date, period);
- ctx.redirect('/tender/' + ctx.tender.id + '/settle/' + newSettle.settle_order);
- } catch (err) {
- ctx.log(err);
- ctx.postError(err, '新增结算期失败,请重试');
- ctx.redirect('/tender/' + ctx.tender.id + '/settle');
- }
- }
- async save(ctx) {
- try {
- const data = {
- order: ctx.request.body.order,
- date: ctx.request.body.date,
- period: ctx.request.body.period,
- };
- const settle = await ctx.service.settle.getDataByCondition({ tid: ctx.tender.id, settle_order: data.order });
- if (!settle) throw '修改的结算期不存在';
- if (ctx.session.sessionUser.accountId !== stage.user_id) throw '您无权修改该数据';
- await this.ctx.service.stage.saveSettle(ctx.tender.id, data.order, data.date, data.period);
- ctx.redirect('/tender/' + ctx.tender.id + '/settle');
- } catch (err) {
- ctx.log(err);
- ctx.postError(err, '保存结算期数据失败,请重试');
- ctx.redirect('/tender/' + ctx.tender.id + '/settle');
- }
- }
- async delete(ctx) {
- try {
- if (ctx.request.body.confirm !== undefined && ctx.request.body.confirm !== '确认删除本期') {
- throw '请输入正确的文本信息';
- }
- const sid = ctx.request.body.settle_id;
- const settle = await ctx.service.settle.getDataById(sid);
- const settleCount = await ctx.service.settle.count({ tid: ctx.tender.id });
- if (!settle || settle.tid !== ctx.tender.id) throw '选择的结算期已不存在';
- if (settleCount === settle.settle_order) throw '选择的结算期无法删除';
- if (ctx.session.sessionUser.accountId !== settle.user_id && !ctx.session.sessionUser.is_admin) throw '您无权删除结算期';
- await ctx.service.deleteSettle(sid);
- ctx.redirect('/tender/' + ctx.tender.id + '/settle/');
- } catch (err) {
- ctx.log(err);
- ctx.postError(err, '删除结算期数据失败,请重试');
- ctx.redirect('/tender/' + ctx.tender.id + '/settle/');
- }
- }
- async _getDefaultRenderData(ctx) {
- const data = {
- tender: ctx.tender.data,
- auditConst: auditConst.settle,
- measureType,
- preUrl: '/tender/' + ctx.tender.id + '/settle/' + ctx.params.sorder,
- settle: ctx.settle,
- shenpiConst,
- auditType: auditConst.auditType,
- thirdParty: {
- gxby: ctx.session.sessionProject.gxby_status,
- dagl: ctx.session.sessionProject.dagl_status,
- },
- };
- // 是否已验证手机短信
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- data.authMobile = pa.auth_mobile;
- const loadAccount = ctx.session.sessionUser.is_admin
- ? true
- : ctx.session.sessionUser.accountId === ctx.settle.user_id && ([auditConst.settle.status.uncheck, auditConst.settle.status.checkNo].indexOf(ctx.settle.audit_status) >= 0);
- if (!loadAccount) return data;
- // 获取所有项目参与者
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: { project_id: ctx.session.sessionProject.id, enable: 1 },
- columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
- });
- data.accountList = accountList;
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
- data.accountGroup = unitList.map(item => {
- const groupList = accountList.filter(item1 => item1.company === item.name);
- return { groupName: item.name, groupList };
- });
- return data;
- }
- async index(ctx) {
- const status = auditConst.settle.status;
- let url = (ctx.settle.audit_status === status.uncheck || ctx.settle.audit_status === status.checkNo) && (ctx.session.sessionUser.is_admin || ctx.session.sessionUser.accountId === ctx.settle.user_id) ? 'select' : 'ledger';
- ctx.redirect(ctx.url + '/' + url);
- }
- async select(ctx) {
- try {
- await ctx.service.settle.loadAuditViewData(ctx.settle);
- const renderData = await this._getDefaultRenderData(ctx);
- renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.settle.select);
- const projectFunInfo = await this.ctx.service.project.getFunRela(ctx.session.sessionProject.id);
- renderData.whiteList = this.ctx.app.config.multipart.whitelist;
- await this.layout('settle/select.ejs', renderData, 'settle/select_modal.ejs');
- } catch(err) {
- console.log(err);
- ctx.log(err);
- ctx.redirect('/tender/' + ctx.tender.id + '/settle');
- }
- }
- async ledger(ctx) {
- try {
- await ctx.service.settle.loadAuditViewData(ctx.settle);
- const renderData = await this._getDefaultRenderData(ctx);
- renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.settle.ledger);
- const projectFunInfo = await this.ctx.service.project.getFunRela(ctx.session.sessionProject.id);
- renderData.whiteList = this.ctx.app.config.multipart.whitelist;
- await this.layout('settle/index.ejs', renderData, 'settle/modal.ejs');
- } catch(err) {
- console.log(err);
- ctx.log(err);
- ctx.redirect('/tender/' + ctx.tender.id + '/settle');
- }
- }
- checkNeedPretreadData(ctx) {
- return ctx.settle.audit_status === auditConst.settle.status.uncheck || (ctx.settle.audit_status === auditConst.settle.status.checkNo && !ctx.settle.readOnly);
- }
- async _loadLatestStage(ctx) {
- if (ctx.settle.latestStage) return;
- ctx.settle.latestStage = ctx.settle.final_sid
- ? await this.ctx.service.stage.getDataById(ctx.settle.final_sid)
- : await this.ctx.service.stage.getLastestCompleteStage(ctx.tender.id);
- }
- async _getStageBillsData(ctx) {
- this.ledgerColumn = [
- 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
- 'code', 'b_code', 'name', 'unit', 'unit_price',
- 'quantity', 'total_price', 'memo', 'drawing_code', 'node_type'];
- const ledgerData = await ctx.service.ledger.getAllDataByCondition({ columns: this.ledgerColumn, where: { tender_id: ctx.tender.id } });
- const endStageData = await ctx.service.stageBillsFinal.getAllDataByCondition({ where: { sid: ctx.settle.latestStage.id } });
- this.ctx.helper.assignRelaData(ledgerData, [
- { data: endStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: 'end_', relaId: 'lid' },
- ]);
- return ledgerData;
- }
- async _getStagePosData(ctx) {
- this.posColumn = ['id', 'tid', 'lid', 'name', 'position', 'porder', 'quantity', 'add_stage_order', 'drawing_code'];
- const posData = await ctx.service.pos.getAllDataByCondition({ columns: this.posColumn, where: { tid: ctx.tender.id } });
- const endStageData = await ctx.service.stagePosFinal.getAllDataByCondition({ where: { sid: ctx.settle.latestStage.id } });
- this.ctx.helper.assignRelaData(posData, [
- { data: endStageData, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'end_', relaId: 'pid' },
- ]);
- return posData;
- }
- async _loadPretreatSettleData(ctx) {
- if (this.checkNeedPretreadData(ctx)) {
- this.pretreadSettle = {};
- const Pretreat = require('../lib/settle');
- const pretreadObj = new Pretreat(ctx);
- [this.pretreadSettle.settleBills, this.pretreadSettle.settlePos] = await pretreadObj.doSettle(this.ctx.settle);
- }
- }
- async _loadSettleDataByKey(ctx, key, hpack) {
- switch (key) {
- case 'stageBills':
- await this._loadLatestStage(ctx);
- const bills = await this._getStageBillsData(ctx);
- return hpack ? [ctx.helper.hpackArr(bills), 'stageBills'] : [bills, ''];
- case 'stagePos':
- await this._loadLatestStage(ctx);
- const pos = await this._getStagePosData(ctx);
- return hpack ? [ctx.helper.hpackArr(pos), 'stagePos'] : [pos, ''];
- case 'settleChange':
- await this._loadLatestStage(ctx);
- const settleChange = await ctx.service.stageChangeFinal.getUnSettleChangeData(ctx.settle.latestStage);
- return hpack ? [ctx.helper.hpackArr(settleChange), key] : [settleChange, ''];
- case 'settleBills':
- await this._loadPretreatSettleData(ctx);
- const settleBills = this.checkNeedPretreadData(ctx)
- ? this.pretreadSettle.settleBills
- : await ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: ctx.settle.id } });
- return hpack ? [ctx.helper.hpackArr(settleBills), 'settleBills'] : [settleBills, ''];
- case 'settlePos':
- await this._loadPretreatSettleData(ctx);
- const settlePos = this.checkNeedPretreadData(ctx)
- ? this.pretreadSettle.settlePos
- : await ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: ctx.settle.id } });
- return hpack ? [ctx.helper.hpackArr(settlePos), 'settlePos'] : [settlePos, ''];
- case 'settleSelect':
- const settleSelect = await ctx.service.settleSelect.getAllDataByCondition({ where: { settle_id: ctx.settle.id } });
- return [settleSelect, ''];
- case 'tag':
- const tag = await ctx.service.ledgerTag.getDatas(ctx.tender.id, -1, ctx.settle.id);
- return [tag, ''];
- default:
- return [null, ''];
- }
- }
- async loadSettleData(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const filter = data.filter.split(';');
- const responseData = { err: 0, msg: '', data: {}, hpack: [] };
- const hpack = true;
- for (const f of filter) {
- const [relaData, hpackKey] = await this._loadSettleDataByKey(ctx, f, hpack);
- responseData.data[f] = relaData;
- if (hpackKey) responseData.hpack.push(hpackKey);
- }
- ctx.body = responseData;
- } catch (err) {
- ctx.log(err);
- ctx.ajaxErrorBody(err, '获取结算数据出错');
- }
- }
- async updateSelect(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (!data.add && !data.del) throw '提交数据错误';
- const result = await ctx.service.settleSelect.updateData(data);
- ctx.body = { err: 0, msg: '', data: result };
- } catch (err) {
- ctx.log(err);
- ctx.ajaxErrorBody(err, '提交结算数据出错');
- }
- }
- async loadGatherData(ctx) {
- try {
- const settle = await this.ctx.service.settle.getLatestCompleteSettle(ctx.tender.id);
- const bills = await this.ctx.service.settleBills.getDataByCondition({ where: { settle_id: settle.id }});
- const pos = await this.ctx.service.settlePos.getDataByCondition({ where: { settle_id: settle.id }});
- ctx.body = { err: 0, msg: '', data: { bills, pos } };
- } catch(err) {
- ctx.log(err);
- ctx.ajaxErrorBody(err, '获取结算汇总数据错误');
- }
- }
- async gather(ctx) {
- try {
- const renderData = {
- tender: ctx.tender.data,
- preUrl: `/tender/${ctx.tender.id}/measure/stage`,
- };
- renderData.jsFiles = this.app.jsFiles.common.concat(this.app.jsFiles.settle.gather);
- await this.layout('settle/gather.ejs', renderData, 'settle/gather_modal.ejs');
- } catch (err) {
- ctx.log(err);
- ctx.redirect(this.menu.menu.dashboard.url);
- }
- }
- }
- return SettleController;
- };
|