| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- 'use strict';
- /**
- *
- * 成本管理
- * @author Mai
- * @date
- * @version
- */
- const audit = require('../const/audit');
- const shenpiConst = require('../const/shenpi');
- const moment = require('moment');
- const sendToWormhole = require('stream-wormhole');
- const fs = require('fs');
- const path = require('path');
- module.exports = app => {
- class CostController extends app.BaseController {
- constructor(ctx) {
- super(ctx);
- ctx.showProject = true;
- // ctx.showTitle = true;
- }
- loadMenu(ctx) {
- super.loadMenu(ctx);
- // 虚拟menu,以保证标题显示正确
- ctx.menu = {
- name: '成本管理',
- display: false,
- caption: '成本管理',
- controller: 'cost',
- };
- }
- async tender(ctx) {
- try {
- if (!ctx.subProject.page_show.cost) throw '该功能已关闭';
- const renderData = {
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.tender),
- };
- const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
- renderData.accountList = accountList;
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
- const accountGroupList = unitList.map(item => {
- const groupList = accountList.filter(item1 => item1.company === item.name);
- return { groupName: item.name, groupList };
- }).filter(x => { return x.groupList.length > 0; });
- renderData.accountGroup = accountGroupList;
- renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'cost', ctx.session.sessionUser.is_admin ? 'all' : '');
- renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
- renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
- renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('cost');
- renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('cost');
- renderData.costLedgerTemplates = await ctx.app.mysql.select('zh_bills_template_list', { where: { sub_type: 1} });
- renderData.costAnalysisTemplates = await ctx.app.mysql.select('zh_bills_template_list', { where: { sub_type: 2} });
- renderData.costCalcTemplates = await ctx.service.calcTmpl.getAllTemplate(this.ctx.session.sessionProject.id, 'cost');
- await this.layout('cost/tender.ejs', renderData, 'cost/tender_modal.ejs');
- } catch (err) {
- ctx.log(err);
- ctx.postError(err, '无法查看成本管理数据');
- ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
- }
- }
- async member(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const result = await ctx.service.tenderPermission.getPartsPermission(data.tid, data.parts);
- ctx.body = { err: 0, msg: '', data: result };
- } catch (err) {
- ctx.log(err);
- ctx.ajaxErrorBody(err, '查询标段权限错误');
- }
- }
- async memberSave(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- await ctx.service.tenderPermission.savePermission(data.tid, data.member, data.permissionBlock);
- ctx.body = { err: 0, msg: '', data: '' };
- } catch (err) {
- ctx.log(err);
- ctx.ajaxErrorBody(err, '保存标段权限错误');
- }
- }
- async ledger(ctx) {
- try {
- const stage_type = this.ctx.service.costStage.stageType.ledger.key;
- const stages = await this.ctx.service.costStage.getAllStages(ctx.tender.id, stage_type, 'DESC');
- for (const s of stages) {
- if (s.audit_status !== audit.common.status.checked) await this.ctx.service.costStage.loadUser(s);
- s.can_del = (s.create_user_id === ctx.session.sessionUser.accountId || ctx.session.sessionUser.is_admin)
- && (s.audit_status === audit.common.status.uncheck || s.audit_status === audit.common.status.checkNo);
- }
- const renderData = {
- stage_type,
- auditType: audit.auditType,
- stages,
- auditConst: audit.common,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.cost_stage)
- };
- await this.layout('cost/ledger_list.ejs', renderData, 'cost/ledger_list_modal.ejs');
- } catch(err) {
- ctx.log(err);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- async book(ctx) {
- try {
- const stage_type = this.ctx.service.costStage.stageType.book.key;
- const stages = await this.ctx.service.costStage.getAllStages(ctx.tender.id, stage_type, 'DESC');
- for (const s of stages) {
- if (s.audit_status !== audit.common.status.checked) await this.ctx.service.costStage.loadUser(s);
- s.can_del = (s.create_user_id === ctx.session.sessionUser.accountId || ctx.session.sessionUser.is_admin)
- && (s.audit_status === audit.common.status.uncheck || s.audit_status === audit.common.status.checkNo);
- }
- const renderData = {
- stage_type,
- auditType: audit.auditType,
- stages,
- auditConst: audit.common,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.cost_stage)
- };
- await this.layout('cost/book_list.ejs', renderData, 'cost/book_list_modal.ejs');
- } catch(err) {
- ctx.log(err);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- async analysis(ctx) {
- try {
- const stage_type = this.ctx.service.costStage.stageType.analysis.key;
- const stages = await this.ctx.service.costStage.getAllStages(ctx.tender.id, stage_type, 'DESC');
- for (const s of stages) {
- if (s.audit_status !== audit.common.status.checked) await this.ctx.service.costStage.loadUser(s);
- s.can_del = (s.create_user_id === ctx.session.sessionUser.accountId || ctx.session.sessionUser.is_admin)
- && (s.audit_status === audit.common.status.uncheck || s.audit_status === audit.common.status.checkNo);
- }
- const renderData = {
- stage_type,
- auditType: audit.auditType,
- stages,
- auditConst: audit.common,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.cost_stage)
- };
- await this.layout('cost/ledger_list.ejs', renderData, 'cost/analysis_list_modal.ejs');
- } catch(err) {
- ctx.log(err);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- async addStage(ctx) {
- const stage_type = ctx.request.body.stage_type;
- try {
- if (!ctx.permission.cost[stage_type + '_add']) throw '您无权创建期';
- const stage_date = ctx.request.body.date;
- if (!stage_date) throw '请选择年月';
- const stages = await ctx.service.costStage.getAllStages(ctx.tender.id, stage_type, 'DESC');
- const unCompleteCount = stages.filter(s => { return s.status !== auditConst.common.status.checked; }).length;
- if (unCompleteCount.length > 0) throw '最新一期未审批通过,请审批通过后再新增';
- const newStage = await ctx.service.costStage.add(ctx.tender.id, stage_type, stage_date);
- if (!newStage) throw '新增期失败';
- ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stage_type}/${newStage.stage_order}`);
- } catch (err) {
- ctx.log(err);
- ctx.postError(err, '新增期失败');
- ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stage_type}`);
- }
- }
- async delStage(ctx) {
- try {
- const stage_id = ctx.request.body.stage_id;
- const stage = await ctx.service.costStage.getDataById(stage_id);
- if (!stage) throw '删除的期不存在,请刷新页面';
- if (!ctx.session.sessionUser.is_admin && stage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权删除本期';
- // 获取最新的期数
- const stages = await ctx.service.costStage.getAllStages(ctx.tender.id, stage.stage_type, 'DESC');
- if (stage.id !== stages[0].id) throw '非最新一期,不可删除';
- await ctx.service.costStage.delete(stage_id);
- ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stage.stage_type}`);
- } catch (err) {
- ctx.log(err);
- ctx.postError(err, '删除期失败');
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 期审批流程(POST)
- * @param ctx
- * @return {Promise<void>}
- */
- async loadAuditors(ctx) {
- try {
- const stageId = JSON.parse(ctx.request.body.data).id;
- const stage = await ctx.service.costStage.get(stageId);
- await ctx.service.costStage.loadUser(stage);
- await ctx.service.costStage.loadAuditViewData(stage);
- ctx.body = { err: 0, msg: '', data: stage };
- } catch (error) {
- ctx.log(error);
- ctx.body = { err: 1, msg: error.toString(), data: null };
- }
- }
- async _getStageAuditViewData(ctx) {
- await this.ctx.service.costStage.loadAuditViewData(ctx.costStage);
- }
- async stage(ctx) {
- const stageTypeInfo = ctx.service.costStage.stageType[ctx.costStage.stage_type];
- try {
- await this._getStageAuditViewData(ctx);
- // 流程审批人相关数据
- const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
- const accountGroup = unitList.map(item => {
- const groupList = accountList.filter(item1 => item1.company === item.name);
- return { groupName: item.name, groupList };
- }).filter(x => { return x.groupList.length > 0; });
- // 是否已验证手机短信
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const renderData = {
- auditConst: audit.common,
- auditType: audit.auditType,
- accountList,
- accountGroup,
- shenpiConst,
- authMobile: pa.auth_mobile,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost[`cost_stage_${stageTypeInfo.key}`]),
- shenpi_status: ctx.tender.info.shenpi[stageTypeInfo.shenpi_status] || 1,
- };
- await this.layout(`cost/${stageTypeInfo.key}.ejs`, renderData, `cost/${stageTypeInfo.key}_modal.ejs`);
- } catch (err) {
- ctx.log(err);
- ctx.postError(err, '读取成本报审错误');
- ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stageTypeInfo.key}`);
- }
- }
- async _ledgerLoad(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const filter = data.filter.split(';');
- const responseData = { err: 0, msg: '', data: {}, hpack: [] };
- for (const f of filter) {
- switch (f) {
- case 'bills':
- responseData.data.bills = ctx.costStage.readOnly
- ? await ctx.service.costStageLedger.getReadData(ctx.costStage)
- : await ctx.service.costStageLedger.getEditData(ctx.costStage);
- break;
- case 'billsCompare':
- responseData.data[f] = await ctx.service.costStageLedger.getCompareData(ctx.costStage);
- break;
- case 'detail':
- responseData.data.detail = ctx.costStage.readOnly
- ? await ctx.service.costStageDetail.getReadData(ctx.costStage)
- : await ctx.service.costStageDetail.getEditData(ctx.costStage);
- break;
- case 'detailCompare':
- // todo
- responseData.data.detailCompare = await ctx.service.costStageDetail.getCompareData(ctx.costStage);
- break;
- case 'auditFlow':
- responseData.data[f] = await ctx.service.costStageAudit.getViewFlow(ctx.costStage);
- break;
- case 'att':
- responseData.data[f] = await ctx.service.costStageFile.getData(ctx.costStage.id, 'DESC');
- break;
- case 'tags':
- responseData.data[f] = await ctx.service.costStageTag.getDatas(ctx.costStage.id);
- break;
- default:
- responseData.data[f] = [];
- break;
- }
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- async _bookLoad(ctx) {
- }
- async _analysisLoad(ctx) {
- }
- async stageLoad(ctx) {
- const updateFun = `_${ctx.costStage.stage_type}Load`;
- if (this[updateFun]) {
- await this[updateFun](ctx);
- } else {
- ctx.ajaxErrorBody('未知期数据类型', '加载数据错误');
- }
- }
- async _billsBase(stage, type, data) {
- if (isNaN(data.id) || data.id <= 0) throw '数据错误';
- if (type !== 'add') {
- if (isNaN(data.count) || data.count <= 0) data.count = 1;
- }
- switch (type) {
- case 'add':
- return await this.ctx.service.costStageLedger.addLedgerNode(stage, data.id, data.count);
- case 'delete':
- return await this.ctx.service.costStageLedger.delete(stage.id, data.id, data.count);
- case 'up-move':
- return await this.ctx.service.costStageLedger.upMoveNode(stage.id, data.id, data.count);
- case 'down-move':
- return await this.ctx.service.costStageLedger.downMoveNode(stage.id, data.id, data.count);
- case 'up-level':
- return await this.ctx.service.costStageLedger.upLevelNode(stage.id, data.id, data.count);
- case 'down-level':
- return await this.ctx.service.costStageLedger.downLevelNode(stage.id, data.id, data.count);
- }
- }
- async _ledgerUpdate(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (!data.target) throw '数据错误';
- const responseData = { err: 0, msg: '', data: {} };
- if (data.target === 'ledger') {
- if (!data.postType || !data.postData) throw '数据错误';
- switch (data.postType) {
- case 'add':
- case 'delete':
- case 'up-move':
- case 'down-move':
- case 'up-level':
- case 'down-level':
- responseData.data = await this._billsBase(ctx.costStage, data.postType, data.postData);
- break;
- case 'update':
- responseData.data = await this.ctx.service.costStageLedger.updateCalc(ctx.costStage, data.postData);
- break;
- default:
- throw '未知操作';
- }
- } else if (data.target === 'detail') {
- responseData.data = await this.ctx.service.costStageDetail.updateDatas(data);
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = this.ajaxErrorBody(err, '数据错误');
- }
- }
- async _bookUpdate(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (!data.postType || !data.postData) throw '数据错误';
- const responseData = { err: 0, msg: '', data: {} };
- switch (data.postType) {
- case 'update':
- responseData.data = await this.ctx.service.costStageBook.updateCalc(ctx.costStage, data.postData);
- break;
- default:
- throw '未知操作';
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = this.ajaxErrorBody(err, '数据错误');
- }
- }
- async _analysisBillsBase(stage, type, data) {
- if (isNaN(data.id) || data.id <= 0) throw '数据错误';
- if (type !== 'add') {
- if (isNaN(data.count) || data.count <= 0) data.count = 1;
- }
- switch (type) {
- case 'add':
- return await this.ctx.service.costStageAnalysis.addSafeBillsNode(stage, data.id, data.count);
- case 'delete':
- return await this.ctx.service.costStageAnalysis.delete(stage.id, data.id, data.count);
- case 'up-move':
- return await this.ctx.service.costStageAnalysis.upMoveNode(stage.id, data.id, data.count);
- case 'down-move':
- return await this.ctx.service.costStageAnalysis.downMoveNode(stage.id, data.id, data.count);
- case 'up-level':
- return await this.ctx.service.costStageAnalysis.upLevelNode(stage.id, data.id, data.count);
- case 'down-level':
- return await this.ctx.service.costStageAnalysis.downLevelNode(stage.id, data.id, data.count);
- }
- }
- async _analysisUpdate(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (!data.postType || !data.postData) throw '数据错误';
- const responseData = { err: 0, msg: '', data: {} };
- switch (data.postType) {
- case 'add':
- case 'delete':
- case 'up-move':
- case 'down-move':
- case 'up-level':
- case 'down-level':
- responseData.data = await this._analysisBillsBase(ctx.costStage, data.postType, data.postData);
- break;
- case 'update':
- responseData.data = await this.ctx.service.costStageAnalysis.updateCalc(ctx.costStage, data.postData);
- break;
- default:
- throw '未知操作';
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = this.ajaxErrorBody(err, '数据错误');
- }
- }
- async stageUpdate(ctx) {
- const updateFun = `_${ctx.costStage.stage_type}Update`;
- if (this[updateFun]) {
- await this[updateFun](ctx);
- } else {
- ctx.ajaxErrorBody('未知期数据类型', '保存数据错误');
- }
- }
- async uploadStageFile(ctx) {
- let stream;
- try {
- const parts = ctx.multipart({ autoFields: true });
- let index = 0;
- const create_time = Date.parse(new Date()) / 1000;
- let stream = await parts();
- const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const rela_id = parts.field.rela_id;
- const rela_sub_id = parts.field.rela_sub_id;
- const uploadfiles = [];
- while (stream !== undefined) {
- if (!stream.filename) throw '未发现上传文件!';
- const fileInfo = path.parse(stream.filename);
- const filepath = `app/public/upload/${ctx.costStage.tid}/costStage/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
- // 保存文件
- await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
- await sendToWormhole(stream);
- // 插入到stage_pay对应的附件列表中
- uploadfiles.push({
- rela_id, rela_sub_id,
- filename: fileInfo.name,
- fileext: fileInfo.ext,
- filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
- filepath,
- });
- ++index;
- if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
- stream = await parts();
- } else {
- stream = undefined;
- }
- }
- const result = await ctx.service.costStageFile.addFiles(ctx.costStage, uploadfiles, user);
- ctx.body = { err: 0, msg: '', data: result };
- } catch (error) {
- ctx.log(error);
- // 失败需要消耗掉stream 以防卡死
- if (stream) await sendToWormhole(stream);
- ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
- }
- }
- async deleteStageFile(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- if (!data && !data.id) throw '缺少参数';
- const result = await ctx.service.costStageFile.delFiles(data.id);
- ctx.body = { err: 0, msg: '', data: result };
- } catch (error) {
- ctx.log(error);
- ctx.ajaxErrorBody(error, '删除附件失败');
- }
- }
- async tag(ctx) {
- try {
- const isRelaUser = ctx.costStage.userIds.indexOf(this.ctx.session.sessionUser.accountId) >= 0;
- const isValidTourist = ctx.permission.cost.view && ctx.tender.touristPermission.tag;
- if (!isRelaUser && !isValidTourist) throw '您无权进行该操作';
- const data = JSON.parse(ctx.request.body.data);
- const result = await ctx.service.costStageTag.update(data);
- ctx.body = { err: 0, msg: '', data: result };
- } catch (err) {
- console.log(err);
- this.log(err);
- ctx.body = this.ajaxErrorBody(err, '书签数据错误');
- }
- }
- }
- return CostController;
- };
|