cost_controller.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. 'use strict';
  2. /**
  3. *
  4. * 成本管理
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. const shenpiConst = require('../const/shenpi');
  11. const sendToWormhole = require('stream-wormhole');
  12. const fs = require('fs');
  13. const path = require('path');
  14. module.exports = app => {
  15. class CostController extends app.BaseController {
  16. constructor(ctx) {
  17. super(ctx);
  18. ctx.showProject = true;
  19. // ctx.showTitle = true;
  20. }
  21. loadMenu(ctx) {
  22. super.loadMenu(ctx);
  23. // 虚拟menu,以保证标题显示正确
  24. ctx.menu = {
  25. name: '成本管理',
  26. display: false,
  27. caption: '成本管理',
  28. controller: 'cost',
  29. };
  30. }
  31. async tender(ctx) {
  32. try {
  33. if (!ctx.subProject.page_show.cost) throw '该功能已关闭';
  34. const renderData = {
  35. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.tender),
  36. };
  37. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  38. renderData.accountList = accountList;
  39. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  40. const accountGroupList = unitList.map(item => {
  41. const groupList = accountList.filter(item1 => item1.company === item.name);
  42. return { groupName: item.name, groupList };
  43. }).filter(x => { return x.groupList.length > 0; });
  44. renderData.accountGroup = accountGroupList;
  45. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  46. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'cost', ctx.session.sessionUser.is_admin ? 'all' : '');
  47. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  48. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  49. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('cost');
  50. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('cost');
  51. renderData.costLedgerTemplates = await ctx.app.mysql.select('zh_bills_template_list', { where: { sub_type: 1} });
  52. renderData.costAnalysisTemplates = await ctx.app.mysql.select('zh_bills_template_list', { where: { sub_type: 2} });
  53. renderData.costCalcTemplates = await ctx.service.calcTmpl.getAllTemplate(this.ctx.session.sessionProject.id, 'cost');
  54. await this.layout('cost/tender.ejs', renderData, 'cost/tender_modal.ejs');
  55. } catch (err) {
  56. ctx.log(err);
  57. ctx.postError(err, '无法查看成本管理数据');
  58. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  59. }
  60. }
  61. async member(ctx) {
  62. try {
  63. const data = JSON.parse(ctx.request.body.data);
  64. const result = await ctx.service.tenderPermission.getPartsPermission(data.tid, data.parts);
  65. ctx.body = { err: 0, msg: '', data: result };
  66. } catch (err) {
  67. ctx.log(err);
  68. ctx.ajaxErrorBody(err, '查询标段权限错误');
  69. }
  70. }
  71. async memberSave(ctx) {
  72. try {
  73. const data = JSON.parse(ctx.request.body.data);
  74. await ctx.service.tenderPermission.savePermission(data.tid, data.member, data.permissionBlock);
  75. ctx.body = { err: 0, msg: '', data: '' };
  76. } catch (err) {
  77. ctx.log(err);
  78. ctx.ajaxErrorBody(err, '保存标段权限错误');
  79. }
  80. }
  81. async getTypeStages(ctx, stage_type, sort = 'DESC') {
  82. const stages = await this.ctx.service.costStage.getAllStages(ctx.tender.id, stage_type, sort);
  83. for (const s of stages) {
  84. if (s.audit_status !== audit.common.status.checked) {
  85. await this.ctx.service.costStage.loadUser(s);
  86. s.is_join = s.userIds.indexOf(this.ctx.session.sessionUser.accountId) >= 0;
  87. // 实时计算 // 暂不开放
  88. // await this.ctx.service.costStage.calcRealtime(s);
  89. } else {
  90. const auditors = await this.ctx.service.costStageAudit.getAuditors(s.id, s.audit_times);
  91. s.is_join = auditors.findIndex(a => { return a.audit_id === this.ctx.session.sessionUser.accountId }) >= 0;
  92. }
  93. s.can_del = (s.create_user_id === ctx.session.sessionUser.accountId || ctx.session.sessionUser.is_admin)
  94. && (s.audit_status === audit.common.status.uncheck || s.audit_status === audit.common.status.checkNo);
  95. }
  96. if (this.ctx.session.sessionUser.is_admin || this.ctx.permission.cost.visitor || this.ctx.permission.cost[stage_type + '_add']) return stages;
  97. return stages.filter(s => { return s.is_join; });
  98. }
  99. async ledger(ctx) {
  100. try {
  101. const stage_type = this.ctx.service.costStage.stageType.ledger.key;
  102. const stages = await this.getTypeStages(ctx, stage_type);
  103. const colSet = this.ctx.service.subProject.getColSet('cost_' + stage_type);
  104. const renderData = {
  105. colSet,
  106. stage_type,
  107. auditType: audit.auditType,
  108. stages,
  109. auditConst: audit.common,
  110. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.cost_stage)
  111. };
  112. await this.layout('cost/ledger_list.ejs', renderData, 'cost/ledger_list_modal.ejs');
  113. } catch(err) {
  114. ctx.log(err);
  115. ctx.redirect(`/sp/${ctx.subProject.id}/cost`);
  116. }
  117. }
  118. async book(ctx) {
  119. try {
  120. const stage_type = this.ctx.service.costStage.stageType.book.key;
  121. const stages = await this.getTypeStages(ctx, stage_type);
  122. for (const s of stages) {
  123. s.relaStage = await this.ctx.service.costStage.getStage(s.rela_stage.sid);
  124. }
  125. const validLedgerStages = await this.ctx.service.costStage.getAllCheckedStages(ctx.tender.id, 'ledger', 'DESC');
  126. for (const vls of validLedgerStages) {
  127. vls.beenRela = stages.findIndex(x => { return x.rela_stage.sid === vls.id; }) >= 0;
  128. vls.canRela = vls.stage_order === stages.length + 1;
  129. }
  130. const renderData = {
  131. stage_type,
  132. auditType: audit.auditType,
  133. stages,
  134. validLedgerStages: validLedgerStages.filter(x => { return x.canRela; }), //validLedgerStages.filter(x => { return !x.beenRela; }),
  135. auditConst: audit.common,
  136. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.cost_stage)
  137. };
  138. await this.layout('cost/book_list.ejs', renderData, 'cost/book_list_modal.ejs');
  139. } catch(err) {
  140. ctx.log(err);
  141. ctx.redirect(`/sp/${ctx.subProject.id}/cost`);
  142. }
  143. }
  144. async analysis(ctx) {
  145. try {
  146. const stage_type = this.ctx.service.costStage.stageType.analysis.key;
  147. const stages = await this.getTypeStages(ctx, stage_type);
  148. // todo 兼容以ledger做关联的情况,届时只需在标段或者项目上设置这个类型即可。
  149. const analysisType = this.ctx.subProject.fun_rela.costAnalysisType;
  150. let validRelaStages = await this.ctx.service.costStage.getAllCheckedStages(ctx.tender.id, analysisType, 'DESC');
  151. if (stages.length > 0) {
  152. const checkedStage = stages.find(x => { return x.audit_status === audit.common.status.checked; });
  153. if (checkedStage) validRelaStages = validRelaStages.filter( x => { return x.stage_order > checkedStage.rela_stage.sorder; });
  154. }
  155. const renderData = {
  156. analysisType,
  157. analysisTypeStr: this.ctx.service.costStage.stageType[analysisType].name,
  158. stage_type,
  159. auditType: audit.auditType,
  160. stages,
  161. validRelaStages,
  162. auditConst: audit.common,
  163. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.cost_stage)
  164. };
  165. await this.layout('cost/analysis_list.ejs', renderData, 'cost/analysis_list_modal.ejs');
  166. } catch(err) {
  167. ctx.log(err);
  168. ctx.redirect(`/sp/${ctx.subProject.id}/cost`);
  169. }
  170. }
  171. async addStage(ctx) {
  172. const stage_type = ctx.request.body.stage_type;
  173. try {
  174. if (!ctx.permission.cost[stage_type + '_add']) throw '您无权创建期';
  175. const stage_date = ctx.request.body.date;
  176. if (stage_type === 'ledger' && !stage_date) throw '请选择年月';
  177. const stages = await ctx.service.costStage.getAllStages(ctx.tender.id, stage_type, 'DESC');
  178. const unCompleteCount = stages.filter(s => { return s.status !== audit.common.status.checked; }).length;
  179. if (unCompleteCount.length > 0) throw '最新一期未审批通过,请审批通过后再新增';
  180. let newStage;
  181. if (stage_type === 'ledger') {
  182. newStage = await ctx.service.costStage.add(ctx.tender.id, stage_type, stage_date);
  183. } else if (stage_type === 'book') {
  184. const stage_order = ctx.request.body.stage;
  185. if (!stage_order) throw '请选择关联成本';
  186. const ledgerStage = await ctx.service.costStage.getStageByOrder(ctx.tender.id, 'ledger', stage_order);
  187. if (!ledgerStage) throw '选择的关联成本不存在';
  188. newStage = await ctx.service.costStage.add(ctx.tender.id, stage_type, ledgerStage.stage_date, { sid: ledgerStage.id, sorder: ledgerStage.stage_order });
  189. } else if (stage_type === 'analysis') {
  190. const analysisType = this.ctx.subProject.fun_rela.costAnalysisType;
  191. if (analysisType !== ctx.request.body.analysis_type) throw '成本分析应关联的成本类型发生改变,请在界面刷新后再试';
  192. const stage_order = ctx.request.body.stage;
  193. if (!stage_order) throw '请选择关联期';
  194. const relaStage = await ctx.service.costStage.getStageByOrder(ctx.tender.id, analysisType, stage_order);
  195. if (!relaStage) throw '选择的关联期不存在';
  196. newStage = await ctx.service.costStage.add(ctx.tender.id, stage_type, relaStage.stage_date, { sid: relaStage.id, sorder: relaStage.stage_order });
  197. await this.ctx.service.costStageAnalysis.reCalcProfit(newStage.id);
  198. }
  199. if (!newStage) throw '新增期失败';
  200. ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stage_type}/${newStage.stage_order}/stage`);
  201. } catch (err) {
  202. ctx.log(err);
  203. ctx.postError(err, '新增期失败');
  204. ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stage_type}`);
  205. }
  206. }
  207. async delStage(ctx) {
  208. try {
  209. const stage_id = ctx.request.body.stage_id;
  210. const stage = await ctx.service.costStage.getDataById(stage_id);
  211. if (!stage) throw '删除的期不存在,请刷新页面';
  212. if (!ctx.session.sessionUser.is_admin && stage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权删除本期';
  213. // 获取最新的期数
  214. const stages = await ctx.service.costStage.getAllStages(ctx.tender.id, stage.stage_type, 'DESC');
  215. if (stage.id !== stages[0].id) throw '非最新一期,不可删除';
  216. if (stage.stage_type === 'ledger') {
  217. const bookStages = await ctx.service.costStage.getAllStages(ctx.tender.id, 'book', 'DESC');
  218. if (bookStages.findIndex(x => { return x.rela_stage && x.rela_stage.sid === stage.id; }) >= 0) throw '该期已关联财务账面,请勿删除';
  219. const analysisStages = await ctx.service.costStage.getAllStages(ctx.tender.id, 'analysis', 'DESC');
  220. if (analysisStages.findIndex(x => { return x.rela_stage && x.rela_stage.sid === stage.id; }) >= 0) throw '该期已关联成本分析,请勿删除';
  221. }
  222. if (stage.stage_type === 'book') {
  223. const analysisStages = await ctx.service.costStage.getAllStages(ctx.tender.id, 'analysis', 'DESC');
  224. if (analysisStages.findIndex(x => { return x.rela_stage && x.rela_stage.sid === stage.id; }) >= 0) throw '该期已关联成本分析,请勿删除';
  225. }
  226. await ctx.service.costStage.delete(stage_id);
  227. ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stage.stage_type}`);
  228. } catch (err) {
  229. ctx.log(err);
  230. ctx.postError(err, '删除期失败');
  231. ctx.redirect(ctx.request.header.referer);
  232. }
  233. }
  234. async _getStageAuditViewData(ctx) {
  235. await this.ctx.service.costStage.loadAuditViewData(ctx.costStage);
  236. }
  237. async stage(ctx) {
  238. const stageTypeInfo = ctx.service.costStage.stageType[ctx.costStage.stage_type];
  239. try {
  240. await this._getStageAuditViewData(ctx);
  241. // 流程审批人相关数据
  242. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  243. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  244. const accountGroup = unitList.map(item => {
  245. const groupList = accountList.filter(item1 => item1.company === item.name);
  246. return { groupName: item.name, groupList };
  247. }).filter(x => { return x.groupList.length > 0; });
  248. // 是否已验证手机短信
  249. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  250. const commonJson = ctx.subProject.common_json ? JSON.parse(ctx.subProject.common_json) : null;
  251. const contractTypes = commonJson && commonJson.tender_contract_type ? commonJson.tender_contract_type : [];
  252. const contractUsed = commonJson && commonJson.tender_contract_used ? commonJson.tender_contract_used : [];
  253. contractUsed.unshift('合同');
  254. contractUsed.unshift('全部');
  255. let payStages = [];
  256. if (ctx.subProject.page_show.phasePay) {
  257. const pstages = await this.ctx.service.phasePay.getAllDataByCondition({
  258. columns: ['phase_order', 'phase_date'],
  259. where: { tid: ctx.costStage.tid , audit_status: audit.common.status.checked },
  260. orders: [['create_time', 'DESC']],
  261. });
  262. payStages = pstages.map(x => { return { order: x.phase_order, date: x.phase_date}; });
  263. } else {
  264. const stages = await this.ctx.service.stage.getAllDataByCondition({
  265. columns: ['order', 's_time'],
  266. where: { tid: ctx.costStage.tid, status: audit.stage.status.checked },
  267. orders: [['in_time', 'DESC']],
  268. });
  269. payStages = stages.map(x => { return { order: x.order, date: x.s_time }; });
  270. }
  271. const colSet = this.ctx.service.subProject.getColSet('cost_' + ctx.costStage.stage_type);
  272. const renderData = {
  273. auditConst: audit.common,
  274. auditType: audit.auditType,
  275. accountList,
  276. accountGroup,
  277. shenpiConst,
  278. authMobile: pa.auth_mobile,
  279. payStages,
  280. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost[`cost_stage_${stageTypeInfo.key}`]),
  281. shenpi_status: ctx.tender.info.shenpi[stageTypeInfo.shenpi_status] || 1,
  282. contractTypes,
  283. contractUsed,
  284. colSet,
  285. };
  286. await this.layout(`cost/${stageTypeInfo.key}.ejs`, renderData, `cost/${stageTypeInfo.key}_modal.ejs`);
  287. } catch (err) {
  288. ctx.log(err);
  289. ctx.postError(err, '读取成本报审错误');
  290. ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stageTypeInfo.key}`);
  291. }
  292. }
  293. async _ledgerLoad(ctx) {
  294. try {
  295. const data = JSON.parse(ctx.request.body.data);
  296. const filter = data.filter.split(';');
  297. const responseData = { err: 0, msg: '', data: {}, hpack: [] };
  298. for (const f of filter) {
  299. switch (f) {
  300. case 'bills':
  301. responseData.data.bills = ctx.costStage.readOnly && !ctx.costStage.canCheck
  302. ? await ctx.service.costStageLedger.getReadData(ctx.costStage)
  303. : await ctx.service.costStageLedger.getEditData(ctx.costStage);
  304. break;
  305. case 'billsCompare':
  306. responseData.data[f] = await ctx.service.costStageLedger.getCompareData(ctx.costStage);
  307. break;
  308. case 'detail':
  309. responseData.data.detail = ctx.costStage.readOnly && !ctx.costStage.canCheck
  310. ? await ctx.service.costStageDetail.getReadData(ctx.costStage)
  311. : await ctx.service.costStageDetail.getEditData(ctx.costStage);
  312. break;
  313. case 'detailCompare':
  314. responseData.data.detailCompare = await ctx.service.costStageDetail.getCompareData(ctx.costStage);
  315. break;
  316. case 'auditFlow':
  317. responseData.data[f] = await ctx.service.costStageAudit.getViewFlow(ctx.costStage);
  318. break;
  319. case 'att':
  320. responseData.data[f] = await ctx.service.costStageFile.getData(ctx.costStage.id, 'DESC');
  321. break;
  322. case 'tags':
  323. responseData.data[f] = await ctx.service.costStageTag.getDatas(ctx.costStage.id);
  324. break;
  325. case 'contract':
  326. const contractTree = await ctx.service.contractTree.getAllDataByCondition({
  327. columns: ['id', 'contract_id', 'contract_pid', 'order', 'level', 'is_leaf', 'full_path', 'code', 'name'],
  328. where: {tid: ctx.costStage.tid, contract_type: 1}
  329. });
  330. const contractData = await this.ctx.service.contract.getAllDataByCondition({
  331. columns: ['id', 'contract_id', 'contract_pid', 'order', 'level', 'is_leaf', 'full_path', 'c_code', 'name'],
  332. where: {tid: ctx.costStage.tid, contract_type: 1}
  333. });
  334. responseData.data[f] = [...contractTree, ...contractData];
  335. break;
  336. default:
  337. responseData.data[f] = [];
  338. break;
  339. }
  340. }
  341. ctx.body = responseData;
  342. } catch (err) {
  343. this.log(err);
  344. ctx.body = { err: 1, msg: err.toString(), data: null };
  345. }
  346. }
  347. async _bookLoad(ctx) {
  348. try {
  349. const data = JSON.parse(ctx.request.body.data);
  350. const filter = data.filter.split(';');
  351. const responseData = { err: 0, msg: '', data: {}, hpack: [] };
  352. for (const f of filter) {
  353. switch (f) {
  354. case 'bills':
  355. const ledger = await ctx.service.costStageLedger.getReadData(ctx.costStage.relaStage);
  356. ledger.forEach(l => { delete l.postil; delete l.memo; });
  357. const book = ctx.costStage.readOnly && !ctx.costStage.canCheck
  358. ? await ctx.service.costStageBook.getReadData(ctx.costStage)
  359. : await ctx.service.costStageBook.getEditData(ctx.costStage);
  360. ctx.helper.assignRelaData(ledger, [
  361. { data: book, fields: ['pre_in_tp', 'pre_in_excl_tax_tp', 'in_tp', 'in_excl_tax_tp', 'end_in_tp', 'end_in_excl_tax_tp', 'postil', 'memo'], prefix: '', relaId: 'ledger_id' },
  362. { data: book, fields: ['id'], prefix: 'book_', relaId: 'ledger_id' },
  363. ]);
  364. responseData.data[f] = ledger;
  365. break;
  366. case 'billsCompare':
  367. const ledgerC = await ctx.service.costStageLedger.getReadData(ctx.costStage.relaStage);
  368. ledgerC.forEach(l => { delete l.postil; delete l.memo; });
  369. const bookC = await ctx.service.costStageBook.getCompareData(ctx.costStage);
  370. ctx.helper.assignRelaData(ledgerC, [
  371. { data: bookC, fields: ['pre_in_tp', 'pre_in_excl_tax_tp', 'in_tp', 'in_excl_tax_tp', 'calc_his', 'postil', 'memo'], prefix: '', relaId: 'ledger_id' },
  372. ]);
  373. responseData.data[f] = ledgerC;
  374. break;
  375. case 'detail':
  376. const detail = await ctx.service.costStageDetail.getReadData(ctx.costStage.relaStage);
  377. detail.forEach(l => { delete l.postil; delete l.memo; });
  378. const bookDetail = ctx.costStage.readOnly && !ctx.costStage.canCheck
  379. ? await ctx.service.costStageBookDetail.getReadData(ctx.costStage)
  380. : await ctx.service.costStageBookDetail.getEditData(ctx.costStage);
  381. ctx.helper.assignRelaData(detail, [
  382. { data: bookDetail, fields: ['in_tp', 'in_excl_tax_tp', 'postil', 'memo'], prefix: '', relaId: 'detail_id' },
  383. { data: bookDetail, fields: ['id'], prefix: 'book_', relaId: 'detail_id' },
  384. ]);
  385. responseData.data[f] = detail;
  386. break;
  387. case 'detailCompare':
  388. const detailC = await ctx.service.costStageLedger.getReadData(ctx.costStage.relaStage);
  389. detailC.forEach(l => { delete l.postil; delete l.memo; });
  390. const bookDetailC = await ctx.service.costStageBook.getCompareData(ctx.costStage);
  391. ctx.helper.assignRelaData(detailC, [
  392. { data: bookDetailC, fields: ['in_tp', 'in_excl_tax_tp', 'calc_his', 'postil', 'memo'], prefix: '', relaId: 'detail_id' },
  393. ]);
  394. responseData.data[f] = detailC;
  395. break;
  396. case 'auditFlow':
  397. responseData.data[f] = await ctx.service.costStageAudit.getViewFlow(ctx.costStage);
  398. break;
  399. case 'att':
  400. responseData.data[f] = await ctx.service.costStageFile.getData(ctx.costStage.id, 'DESC');
  401. break;
  402. default:
  403. responseData.data[f] = [];
  404. break;
  405. }
  406. }
  407. ctx.body = responseData;
  408. } catch (err) {
  409. this.log(err);
  410. ctx.body = { err: 1, msg: err.toString(), data: null };
  411. }
  412. }
  413. async _analysisLoad(ctx) {
  414. try {
  415. const data = JSON.parse(ctx.request.body.data);
  416. const filter = data.filter.split(';');
  417. const responseData = { err: 0, msg: '', data: {}, hpack: [] };
  418. for (const f of filter) {
  419. switch (f) {
  420. case 'bills':
  421. responseData.data.bills = ctx.costStage.readOnly && !ctx.costStage.canCheck
  422. ? await ctx.service.costStageAnalysis.getReadData(ctx.costStage)
  423. : await ctx.service.costStageAnalysis.getEditData(ctx.costStage);
  424. break;
  425. case 'billsCompare':
  426. responseData.data[f] = await ctx.service.costStageAnalysis.getCompareData(ctx.costStage);
  427. break;
  428. case 'detail':
  429. responseData.data.detail = ctx.costStage.readOnly && !ctx.costStage.canCheck
  430. ? await ctx.service.costStageAnalysisDetail.getReadData(ctx.costStage)
  431. : await ctx.service.costStageAnalysisDetail.getEditData(ctx.costStage);
  432. break;
  433. case 'detailCompare':
  434. responseData.data.detailCompare = await ctx.service.costStageAnalysisDetail.getCompareData(ctx.costStage);
  435. break;
  436. case 'auditFlow':
  437. responseData.data[f] = await ctx.service.costStageAudit.getViewFlow(ctx.costStage);
  438. break;
  439. case 'att':
  440. responseData.data[f] = await ctx.service.costStageFile.getData(ctx.costStage.id, 'DESC');
  441. break;
  442. case 'relaStageBills':
  443. const relaStage = ctx.costStage.relaStage;
  444. if (relaStage.stage_type === 'book') relaStage.relaStage = await this.ctx.service.costStage.getStage(relaStage.rela_stage.sid);
  445. responseData.data[f] = relaStage.relaStage
  446. ? await this.ctx.service.costStageLedger.getReadData(relaStage.relaStage)
  447. : await this.ctx.service.costStageLedger.getReadData(relaStage);
  448. const detail = relaStage.relaStage
  449. ? await this.ctx.service.costStageDetail.getReadData(relaStage.relaStage)
  450. : await this.ctx.service.costStageDetail.getReadData(relaStage);
  451. responseData.data[f].forEach(x => {
  452. const relaDetail = detail.filter(d => { return d.ledger_id === x.id; });
  453. x.detail_type = relaDetail.length === 0 ? '' : (relaDetail[0].is_deal ? '合同' : '一般');
  454. });
  455. break;
  456. default:
  457. responseData.data[f] = [];
  458. break;
  459. }
  460. }
  461. ctx.body = responseData;
  462. } catch (err) {
  463. this.log(err);
  464. ctx.body = { err: 1, msg: err.toString(), data: null };
  465. }
  466. }
  467. async stageLoad(ctx) {
  468. const updateFun = `_${ctx.costStage.stage_type}Load`;
  469. if (this[updateFun]) {
  470. await this[updateFun](ctx);
  471. } else {
  472. ctx.ajaxErrorBody('未知期数据类型', '加载数据错误');
  473. }
  474. }
  475. async _billsBase(stage, type, data) {
  476. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  477. if (type !== 'add') {
  478. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  479. }
  480. switch (type) {
  481. case 'add':
  482. return await this.ctx.service.costStageLedger.addLedgerNode(stage, data.id, data.count);
  483. case 'delete':
  484. return await this.ctx.service.costStageLedger.delete(stage.id, data.id, data.count);
  485. case 'up-move':
  486. return await this.ctx.service.costStageLedger.upMoveNode(stage.id, data.id, data.count);
  487. case 'down-move':
  488. return await this.ctx.service.costStageLedger.downMoveNode(stage.id, data.id, data.count);
  489. case 'up-level':
  490. return await this.ctx.service.costStageLedger.upLevelNode(stage.id, data.id, data.count);
  491. case 'down-level':
  492. return await this.ctx.service.costStageLedger.downLevelNode(stage.id, data.id, data.count);
  493. }
  494. }
  495. async _ledgerUpdate(ctx) {
  496. try {
  497. const data = JSON.parse(ctx.request.body.data);
  498. if (!data.target) throw '数据错误';
  499. const responseData = { err: 0, msg: '', data: {} };
  500. if (data.target === 'ledger') {
  501. if (!data.postType || !data.postData) throw '数据错误';
  502. switch (data.postType) {
  503. case 'add':
  504. case 'delete':
  505. case 'up-move':
  506. case 'down-move':
  507. case 'up-level':
  508. case 'down-level':
  509. responseData.data = await this._billsBase(ctx.costStage, data.postType, data.postData);
  510. break;
  511. case 'update':
  512. responseData.data = await this.ctx.service.costStageLedger.updateCalc(ctx.costStage, data.postData);
  513. break;
  514. default:
  515. throw '未知操作';
  516. }
  517. } else if (data.target === 'detail') {
  518. responseData.data = await this.ctx.service.costStageDetail.updateDatas(data);
  519. } else if (data.target === 'importContract') {
  520. if (!data.types && !data.ids) throw '数据错误';
  521. responseData.data = await this.ctx.service.costStageDetail.importContract(data);
  522. }
  523. ctx.body = responseData;
  524. } catch (err) {
  525. this.log(err);
  526. ctx.body = this.ajaxErrorBody(err, '数据错误');
  527. }
  528. }
  529. async _bookUpdate(ctx) {
  530. try {
  531. const data = JSON.parse(ctx.request.body.data);
  532. if (!data.target || !data.update) throw '数据错误';
  533. const responseData = { err: 0, msg: '', data: {} };
  534. switch (data.target) {
  535. case 'ledger':
  536. responseData.data = await this.ctx.service.costStageBook.updateDatas(data.update);
  537. break;
  538. case 'detail':
  539. responseData.data = await this.ctx.service.costStageBookDetail.updateDatas(data.update);
  540. break;
  541. default:
  542. throw '未知操作';
  543. }
  544. ctx.body = responseData;
  545. } catch (err) {
  546. this.log(err);
  547. ctx.body = this.ajaxErrorBody(err, '数据错误');
  548. }
  549. }
  550. async _analysisBillsBase(stage, type, data, addType) {
  551. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  552. if (type !== 'add') {
  553. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  554. }
  555. switch (type) {
  556. case 'add':
  557. if (addType === 'child') {
  558. return await this.ctx.service.costStageAnalysis.addAnalysisChild(stage, data.id, data.count);
  559. } else {
  560. return await this.ctx.service.costStageAnalysis.addAnalysisNode(stage, data.id, data.count);
  561. }
  562. case 'delete':
  563. return await this.ctx.service.costStageAnalysis.delete(stage.id, data.id, data.count);
  564. case 'up-move':
  565. return await this.ctx.service.costStageAnalysis.upMoveNode(stage.id, data.id, data.count);
  566. case 'down-move':
  567. return await this.ctx.service.costStageAnalysis.downMoveNode(stage.id, data.id, data.count);
  568. case 'up-level':
  569. return await this.ctx.service.costStageAnalysis.upLevelNode(stage.id, data.id, data.count);
  570. case 'down-level':
  571. return await this.ctx.service.costStageAnalysis.downLevelNode(stage.id, data.id, data.count);
  572. }
  573. }
  574. async _analysisUpdate(ctx) {
  575. try {
  576. const data = JSON.parse(ctx.request.body.data);
  577. if (!data.target) throw '数据错误';
  578. const responseData = { err: 0, msg: '', data: {} };
  579. if (data.target === 'ledger') {
  580. if (!data.postType || !data.postData) throw '数据错误';
  581. switch (data.postType) {
  582. case 'add':
  583. case 'delete':
  584. case 'up-move':
  585. case 'down-move':
  586. case 'up-level':
  587. case 'down-level':
  588. responseData.data = await this._analysisBillsBase(ctx.costStage, data.postType, data.postData, data.addType);
  589. break;
  590. case 'update':
  591. responseData.data = await this.ctx.service.costStageAnalysis.updateCalc(ctx.costStage, data.postData);
  592. break;
  593. default:
  594. throw '未知操作';
  595. }
  596. } else if (data.target === 'detail') {
  597. responseData.data = await this.ctx.service.costStageAnalysisDetail.updateDatas(data.update);
  598. } else if (data.target === 'importPay') {
  599. responseData.data = await this.ctx.service.costStageAnalysis.importPay(ctx.costStage, data.pay_order, data.pay_tax);
  600. } else if (data.target === 'importCost') {
  601. responseData.data = await this.ctx.service.costStageAnalysis.importCost(ctx.costStage, data.select_id, data.cost_id);
  602. }
  603. ctx.body = responseData;
  604. } catch (err) {
  605. this.log(err);
  606. ctx.body = this.ajaxErrorBody(err, '数据错误');
  607. }
  608. }
  609. async stageUpdate(ctx) {
  610. const updateFun = `_${ctx.costStage.stage_type}Update`;
  611. if (this[updateFun]) {
  612. await this[updateFun](ctx);
  613. } else {
  614. ctx.ajaxErrorBody('未知期数据类型', '保存数据错误');
  615. }
  616. }
  617. async setDecimal(ctx) {
  618. try {
  619. const data = JSON.parse(ctx.request.body.data);
  620. const stageTypeInfo = ctx.service.costStage.stageType[ctx.costStage.stage_type];
  621. const result = await this.ctx.service[stageTypeInfo.dataService].setDecimal(data.decimal);
  622. ctx.body = { err: 0, msg: '', data: result };
  623. } catch (err) {
  624. this.log(err);
  625. ctx.body = this.ajaxErrorBody(err, '设置小数位数错误');
  626. }
  627. }
  628. async uploadStageFile(ctx) {
  629. let stream;
  630. try {
  631. const parts = ctx.multipart({ autoFields: true });
  632. let index = 0;
  633. const create_time = Date.parse(new Date()) / 1000;
  634. let stream = await parts();
  635. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  636. const rela_id = parts.field.rela_id;
  637. const rela_sub_id = parts.field.rela_sub_id || '';
  638. const uploadfiles = [];
  639. while (stream !== undefined) {
  640. if (!stream.filename) throw '未发现上传文件!';
  641. const fileInfo = path.parse(stream.filename);
  642. const filepath = `app/public/upload/${ctx.costStage.tid}/costStage/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
  643. // 保存文件
  644. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  645. await sendToWormhole(stream);
  646. // 插入到stage_pay对应的附件列表中
  647. uploadfiles.push({
  648. rela_id, rela_sub_id,
  649. filename: fileInfo.name,
  650. fileext: fileInfo.ext,
  651. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  652. filepath,
  653. extra_upload: ctx.costStage.audit_status === audit.common.status.checked,
  654. });
  655. ++index;
  656. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  657. stream = await parts();
  658. } else {
  659. stream = undefined;
  660. }
  661. }
  662. const result = await ctx.service.costStageFile.addFiles(ctx.costStage, uploadfiles, user);
  663. ctx.body = { err: 0, msg: '', data: result };
  664. } catch (error) {
  665. ctx.log(error);
  666. // 失败需要消耗掉stream 以防卡死
  667. if (stream) await sendToWormhole(stream);
  668. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  669. }
  670. }
  671. async deleteStageFile(ctx) {
  672. try {
  673. const data = JSON.parse(ctx.request.body.data);
  674. if (!data && !data.id) throw '缺少参数';
  675. const result = await ctx.service.costStageFile.delFiles(data.id);
  676. ctx.body = { err: 0, msg: '', data: result };
  677. } catch (error) {
  678. ctx.log(error);
  679. ctx.ajaxErrorBody(error, '删除附件失败');
  680. }
  681. }
  682. async tag(ctx) {
  683. try {
  684. const isRelaUser = ctx.costStage.userIds.indexOf(this.ctx.session.sessionUser.accountId) >= 0;
  685. const isValidTourist = ctx.permission.cost.view && ctx.tender.touristPermission.tag;
  686. if (!isRelaUser && !isValidTourist) throw '您无权进行该操作';
  687. const data = JSON.parse(ctx.request.body.data);
  688. const result = await ctx.service.costStageTag.update(data);
  689. ctx.body = { err: 0, msg: '', data: result };
  690. } catch (err) {
  691. this.log(err);
  692. ctx.body = this.ajaxErrorBody(err, '书签数据错误');
  693. }
  694. }
  695. /**
  696. * 添加审批人
  697. * @param ctx
  698. * @return {Promise<void>}
  699. */
  700. async addStageAudit(ctx) {
  701. try {
  702. const data = JSON.parse(ctx.request.body.data);
  703. const id = this.app._.toInteger(data.auditorId);
  704. if (isNaN(id) || id <= 0) throw '参数错误';
  705. // 检查权限等
  706. if (ctx.costStage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权添加审核人';
  707. if (ctx.costStage.audit_status !== audit.common.status.uncheck && ctx.costStage.audit_status !== audit.common.status.checkNo) {
  708. throw '当前不允许添加审核人';
  709. }
  710. // 检查审核人是否已存在
  711. const exist = await ctx.service.costStageAudit.getDataByCondition({ stage_id: ctx.costStage.id, audit_times: ctx.costStage.audit_times, audit_id: id });
  712. if (exist) throw '该审核人已存在,请勿重复添加';
  713. const auditorInfo = await this.ctx.service.projectAccount.getDataById(id);
  714. if (!auditorInfo) throw '添加的审批人不存在';
  715. const typeInfo = ctx.service.costStage.stageType[ctx.costStage.stage_type];
  716. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.costStage.tid, sp_type: shenpiConst.cost_sp_type[typeInfo.shenpi_status], sp_status: shenpiConst.sp_status.gdzs });
  717. const is_gdzs = shenpiInfo && ctx.tender.info.shenpi[typeInfo.shenpi_status] === shenpiConst.sp_status.gdzs ? 1 : 0;
  718. const result = await ctx.service.costStageAudit.addAuditor(ctx.costStage.id, auditorInfo, ctx.costStage.audit_times, is_gdzs);
  719. if (!result) throw '添加审核人失败';
  720. const auditors = await ctx.service.costStageAudit.getAuditorGroup(ctx.costStage.id, ctx.costStage.audit_times);
  721. ctx.body = { err: 0, msg: '', data: auditors };
  722. } catch (err) {
  723. ctx.log(err);
  724. ctx.body = { err: 1, msg: err.toString(), data: null };
  725. }
  726. }
  727. /**
  728. * 移除审批人
  729. * @param ctx
  730. * @return {Promise<void>}
  731. */
  732. async deleteStageAudit(ctx) {
  733. try {
  734. const data = JSON.parse(ctx.request.body.data);
  735. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  736. if (isNaN(id) || id <= 0) throw '参数错误';
  737. const result = await ctx.service.costStageAudit.deleteAuditor(ctx.costStage.id, id, ctx.costStage.audit_times);
  738. if (!result) throw '移除审核人失败';
  739. const auditors = await ctx.service.costStageAudit.getAuditors(ctx.costStage.id, ctx.costStage.audit_times);
  740. ctx.body = { err: 0, msg: '', data: auditors };
  741. } catch (err) {
  742. ctx.log(err);
  743. ctx.body = { err: 1, msg: err.toString(), data: null };
  744. }
  745. }
  746. async stageAuditStart(ctx) {
  747. try {
  748. if (ctx.costStage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权上报该期数据';
  749. if (ctx.costStage.revising) throw '台账修订中,不可上报';
  750. if (ctx.costStage.audit_status !== audit.common.status.uncheck && ctx.costStage.audit_status !== audit.common.status.checkNo) throw '该期数据当前无法上报';
  751. await ctx.service.costStageAudit.start(ctx.costStage);
  752. ctx.redirect(ctx.request.header.referer);
  753. } catch (err) {
  754. ctx.log(err);
  755. ctx.postError(err, '上报失败');
  756. ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${ctx.costStage.stage_type}/${ctx.costStage.stage_order}/stage`);
  757. }
  758. }
  759. async stageAuditCheck(ctx) {
  760. try {
  761. if (!ctx.costStage || (ctx.costStage.audit_status !== audit.common.status.checking && ctx.costStage.audit_status !== audit.common.status.checkNoPre)) {
  762. throw '当前期数据有误';
  763. }
  764. if (ctx.costStage.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) < 0) {
  765. throw '您无权进行该操作';
  766. }
  767. if (ctx.costStage.revising) throw '台账修订中,不可审批';
  768. const checkType = parseInt(ctx.request.body.checkType);
  769. const opinion = ctx.request.body.opinion.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
  770. await ctx.service.costStageAudit.check(ctx.costStage, checkType, opinion);
  771. } catch (err) {
  772. ctx.log(err);
  773. ctx.postError(err, '审批失败');
  774. }
  775. ctx.redirect(ctx.request.header.referer);
  776. }
  777. async stageAuditCheckAgain(ctx) {
  778. try {
  779. if (!ctx.costStage.isLatest) throw '非最新一期,不可重新审批';
  780. if (ctx.costStage.audit_status !== audit.common.status.checked) throw '未审批完成,不可重新审批';
  781. if (ctx.costStage.revising) throw '台账修订中,不可重审';
  782. if (ctx.session.sessionUser.loginStatus === 0) {
  783. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  784. if (!user.auth_mobile) throw '未绑定手机号';
  785. const code = ctx.request.body.code;
  786. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  787. const cacheCode = await app.redis.get(cacheKey);
  788. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  789. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  790. throw '验证码不正确!';
  791. }
  792. }
  793. const adminCheckAgain = ctx.request.body.confirm === '确认设置终审审批' && ctx.session.sessionUser.is_admin;
  794. if (ctx.costStage.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) < 0 && !adminCheckAgain) throw '您无权重新审批';
  795. await ctx.service.costStageAudit.checkAgain(ctx.costStage, adminCheckAgain);
  796. } catch (err) {
  797. ctx.log(err);
  798. ctx.postError(err, '重新审批失败');
  799. }
  800. ctx.redirect(ctx.request.header.referer);
  801. }
  802. async stageAuditCheckCancel(ctx) {
  803. try {
  804. if (ctx.costStage.revising) throw '台账修订中,不可撤回';
  805. if (!ctx.costStage.cancancel) throw '您无权进行该操作';
  806. await ctx.service.costStageAudit.checkCancel(ctx.costStage);
  807. } catch (err) {
  808. ctx.log(err);
  809. ctx.postError(err, '撤回失败');
  810. }
  811. ctx.redirect(ctx.request.header.referer);
  812. }
  813. /**
  814. * 期审批流程(POST)
  815. * @param ctx
  816. * @return {Promise<void>}
  817. */
  818. async loadAuditors(ctx) {
  819. try {
  820. const stageOrder = JSON.parse(ctx.request.body.data).order;
  821. const stageType = ctx.params.stype;
  822. const stage = await ctx.service.costStage.getStageByOrder(ctx.tender.id, stageType, stageOrder);
  823. await ctx.service.costStage.loadUser(stage);
  824. await ctx.service.costStage.loadAuditViewData(stage);
  825. ctx.body = { err: 0, msg: '', data: stage };
  826. } catch (error) {
  827. ctx.log(error);
  828. ctx.body = { err: 1, msg: error.toString(), data: null };
  829. }
  830. }
  831. async colSet(ctx) {
  832. const stage_type = ctx.request.body.stage_type;
  833. try {
  834. const colSet = {};
  835. for (const prop in ctx.request.body) {
  836. if (prop.indexOf('col_') === 0) colSet[prop.replace('col_', '')] = ctx.request.body[prop] === '1' ? 1 : 0;
  837. }
  838. await this.ctx.service.subProject.updateColSet('cost_' + stage_type, colSet);
  839. ctx.redirect(ctx.request.header.referer);
  840. } catch (err) {
  841. ctx.log(err);
  842. ctx.postError(err, '保存列设置失败');
  843. ctx.redirect(ctx.request.header.referer);
  844. }
  845. }
  846. async compare(ctx) {
  847. const stageTypeInfo = ctx.service.costStage.stageType[ctx.costStage.stage_type];
  848. try {
  849. await this._getStageAuditViewData(ctx);
  850. // 流程审批人相关数据
  851. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  852. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  853. const accountGroup = unitList.map(item => {
  854. const groupList = accountList.filter(item1 => item1.company === item.name);
  855. return { groupName: item.name, groupList };
  856. }).filter(x => { return x.groupList.length > 0; });
  857. // 是否已验证手机短信
  858. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  859. const calcFields = ctx.service[stageTypeInfo.dataService].calcFields;
  860. const renderData = {
  861. auditConst: audit.common,
  862. auditType: audit.auditType,
  863. accountList,
  864. accountGroup,
  865. shenpiConst,
  866. authMobile: pa.auth_mobile,
  867. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.cost_compare),
  868. shenpi_status: ctx.tender.info.shenpi[stageTypeInfo.shenpi_status] || 1,
  869. calcFields,
  870. };
  871. await this.layout(`cost/compare.ejs`, renderData, `cost/compare_modal.ejs`);
  872. } catch(err) {
  873. ctx.log(err);
  874. ctx.postError(err, '读取成本报审错误');
  875. ctx.redirect(`/sp/${ctx.subProject.id}/cost/tender/${ctx.tender.id}/${stageTypeInfo.key}`);
  876. }
  877. }
  878. async gather(ctx) {
  879. try {
  880. const ledgerStages = await this.getTypeStages(ctx, 'ledger', 'ASC');
  881. const renderData = {
  882. ledgerStages,
  883. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.cost.cost_gather)
  884. };
  885. await this.layout('cost/gather.ejs', renderData, 'cost/gather_modal.ejs');
  886. } catch(err) {
  887. ctx.log(err);
  888. ctx.postError(err, '获取汇总分析页面错误');
  889. ctx.redirect(`/sp/${ctx.subProject.id}/cost`);
  890. }
  891. }
  892. async _ledgerGatherLoad(stage) {
  893. const result = {};
  894. result.bills = stage.audit_status !== audit.common.status.checked
  895. ? await this.ctx.service.costStageLedger.getReadData(stage)
  896. : await this.ctx.service.costStageLedger.getEditData(stage);
  897. return result;
  898. }
  899. async _bookGatherLoad(stage) {
  900. const result = {};
  901. stage.relaStage = await this.ctx.service.costStage.getStage(stage.rela_stage.sid);
  902. result.bills = await this.ctx.service.costStageLedger.getReadData(stage.relaStage);
  903. result.bills.forEach(l => { delete l.postil; delete l.memo; });
  904. const book = stage.audit_status !== audit.common.status.checked
  905. ? await this.ctx.service.costStageBook.getReadData(stage)
  906. : await this.ctx.service.costStageBook.getEditData(stage);
  907. this.ctx.helper.assignRelaData(result.bills, [
  908. { data: book, fields: ['in_tp', 'in_excl_tax_tp', 'postil', 'memo'], prefix: '', relaId: 'ledger_id' },
  909. { data: book, fields: ['id'], prefix: 'book_', relaId: 'ledger_id' },
  910. ]);
  911. return result;
  912. }
  913. async _analysisGatherLoad(stage) {
  914. const result = {};
  915. result.bills = stage.audit_status !== audit.common.status.checked
  916. ? await this.ctx.service.costStageAnalysis.getReadData(stage)
  917. : await this.ctx.service.costStageAnalysis.getEditData(stage);
  918. return result;
  919. }
  920. async loadGatherData(ctx) {
  921. try {
  922. const data = JSON.parse(ctx.request.body.data);
  923. const result = {};
  924. if (data.stages) {
  925. result.stages = [];
  926. for (const s of data.stages) {
  927. const stage = await this.ctx.service.costStage.getStage(s);
  928. const loadFun = `_${stage.stage_type}GatherLoad`;
  929. if (this[loadFun]) {
  930. const detail = await this[loadFun](stage);
  931. result.stages.push({ id: stage.id, stage_date: stage.stage_date, stage_order: stage.stage_order, detail });
  932. } else {
  933. throw '未知期数据类型';
  934. }
  935. }
  936. }
  937. ctx.body = { err: 0, msg: '', data: result }
  938. } catch(err) {
  939. ctx.log(err);
  940. ctx.ajaxErrorBody(err, '获取汇总数据错误');
  941. }
  942. }
  943. }
  944. return CostController;
  945. };