payment_controller.js 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. 'use strict';
  2. const accountGroup = require('../const/account_group').group;
  3. const JV = require('../reports/rpt_component/jpc_value_define');
  4. const shenpiConst = require('../const/shenpi');
  5. const auditConst = require('../const/audit').stage;
  6. const paymentConst = require('../const/payment');
  7. const moment = require('moment');
  8. const path = require('path');
  9. const sendToWormhole = require('stream-wormhole');
  10. const fs = require('fs');
  11. module.exports = app => {
  12. class PaymentController extends app.BaseController {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. ctx.showProject = true;
  22. // ctx.showTitle = true;
  23. }
  24. /**
  25. * 支付审批列表页
  26. *
  27. * @param {Object} ctx - egg全局页面
  28. * @return {void}
  29. */
  30. async index(ctx) {
  31. try {
  32. const auditPermission = await this.ctx.service.paymentPermissionAudit.getOnePermission(ctx.session.sessionUser.is_admin, ctx.session.sessionUser.accountId);
  33. if (!auditPermission) {
  34. throw '权限不足';
  35. }
  36. // 列表读取及目录读取
  37. const renderData = {
  38. auditPermission,
  39. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.index),
  40. };
  41. if (ctx.session.sessionUser.is_admin) {
  42. const projectId = ctx.session.sessionProject.id;
  43. const permissionAudits = await ctx.service.paymentPermissionAudit.getList(projectId);
  44. // 获取所有项目参与者
  45. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  46. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  47. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  48. });
  49. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
  50. const accountGroupList = unitList.map(item => {
  51. const groupList = accountList.filter(item1 => item1.company === item.name);
  52. return { groupName: item.name, groupList };
  53. });
  54. // const accountGroupList = accountGroup.map((item, idx) => {
  55. // const groupList = accountList.filter(item => item.account_group === idx);
  56. // return { groupName: item, groupList };
  57. // });
  58. renderData.permissionAudits = permissionAudits;
  59. renderData.accountList = accountList;
  60. renderData.accountGroup = accountGroupList;
  61. }
  62. await this.layout('payment/index.ejs', renderData, 'payment/modal.ejs');
  63. } catch (err) {
  64. console.log(err);
  65. this.log(err);
  66. ctx.session.postError = err.toString();
  67. ctx.redirect(this.menu.menu.dashboard.url);
  68. }
  69. }
  70. async listLoad(ctx) {
  71. const responseData = {
  72. err: 0, msg: '', data: {},
  73. };
  74. const auditPermission = await this.ctx.service.paymentPermissionAudit.getOnePermission(ctx.session.sessionUser.is_admin, ctx.session.sessionUser.accountId);
  75. if (!auditPermission) {
  76. throw '权限不足';
  77. }
  78. // 先获取你创建的标段及参与的标段
  79. const tenderList = await ctx.service.paymentTender.getList(ctx.session.sessionUser.accountId, auditPermission);
  80. // 获取你创建的目录及对应目录下的所有目录
  81. const folderList = await ctx.service.paymentFolder.getList(ctx.session.sessionUser.accountId, tenderList, auditPermission);
  82. if (tenderList.length > 0) {
  83. for (const t of tenderList) {
  84. t.have_notice = await ctx.service.paymentDetail.haveNotice2Tender(t.id, ctx.session.sessionUser.accountId);
  85. t.have_detail = await ctx.service.paymentDetail.haveDetail2Tender(t.id);
  86. }
  87. }
  88. responseData.data.folderList = ctx.helper._.orderBy(folderList, ['in_time'], ['asc']);
  89. responseData.data.tenderList = ctx.helper._.orderBy(tenderList, ['in_time'], ['asc']);
  90. ctx.body = responseData;
  91. }
  92. async permissionSave(ctx) {
  93. try {
  94. if (ctx.session.sessionUser.is_admin === 0) throw '没有设置权限';
  95. const projectId = ctx.session.sessionProject.id;
  96. const responseData = {
  97. err: 0, msg: '', data: null,
  98. };
  99. const data = JSON.parse(ctx.request.body.data);
  100. if (!data.type) {
  101. throw '提交数据错误';
  102. }
  103. let uids;
  104. let result = false;
  105. let auditList = [];
  106. switch (data.type) {
  107. case 'add-audit':
  108. // 判断用户是单个还是数组
  109. uids = data.id instanceof Array ? data.id : [data.id];
  110. // 判断该用户的组是否已加入到表中,已加入则提示无需添加
  111. auditList = await ctx.service.paymentPermissionAudit.getAllDataByCondition({ where: { pid: projectId, uid: uids } });
  112. const addAidList = ctx.helper._.difference(uids, ctx.helper._.map(auditList, 'uid'));
  113. if (addAidList.length === 0) {
  114. throw '用户已存在权限中,无需重复添加';
  115. }
  116. const accountList = await ctx.service.projectAccount.getAllDataByCondition({ where: { id: addAidList } });
  117. await ctx.service.paymentPermissionAudit.saveAudits(projectId, accountList);
  118. responseData.data = await ctx.service.paymentPermissionAudit.getList(projectId);
  119. break;
  120. case 'del-audit':
  121. uids = data.id instanceof Array ? data.id : [data.id];
  122. auditList = await ctx.service.paymentPermissionAudit.getAllDataByCondition({ where: { id: uids } });
  123. if (auditList.length !== uids.length) {
  124. throw '该用户已不存在权限中,移除失败';
  125. }
  126. await ctx.service.paymentPermissionAudit.delAudit(uids);
  127. responseData.data = await ctx.service.paymentPermissionAudit.getList(projectId);
  128. break;
  129. case 'save-permission-one':
  130. result = await ctx.service.paymentPermissionAudit.updateOnePermission(data.updateData);
  131. if (!result) {
  132. throw '修改权限失败';
  133. }
  134. break;
  135. // case 'save-permission-all':
  136. // result = await ctx.service.paymentPermissionAudit.updateAllPermission(projectId, data.permission_type, data.value);
  137. // if (!result) {
  138. // throw '修改权限失败';
  139. // }
  140. // responseData.data = await ctx.service.paymentPermissionAudit.getList(projectId);
  141. // break;
  142. default: throw '参数有误';
  143. }
  144. ctx.body = responseData;
  145. } catch (err) {
  146. this.log(err);
  147. ctx.body = { err: 1, msg: err.toString(), data: null };
  148. }
  149. }
  150. async paymentInfoSave(ctx) {
  151. try {
  152. const data = JSON.parse(ctx.request.body.data);
  153. if (!data.type) throw '提交数据错误';
  154. switch (data.type) {
  155. case 'info':
  156. await ctx.service.paymentTenderInfo.saveTenderInfo(data.postData.id, data.postData.info);
  157. break;
  158. default: throw '参数有误';
  159. }
  160. ctx.body = { err: 0, msg: '', data: {} };
  161. } catch (err) {
  162. this.log(err);
  163. ctx.body = { err: 1, msg: err.toString(), data: null };
  164. }
  165. }
  166. async save(ctx) {
  167. try {
  168. const projectId = ctx.session.sessionProject.id;
  169. const auditPermission = await this.ctx.service.paymentPermissionAudit.getOnePermission(ctx.session.sessionUser.is_admin, ctx.session.sessionUser.accountId);
  170. if (!auditPermission) {
  171. throw '权限不足';
  172. }
  173. const responseData = {
  174. err: 0, msg: '', data: {},
  175. };
  176. const data = JSON.parse(ctx.request.body.data);
  177. if (!data.type) {
  178. throw '提交数据错误';
  179. }
  180. if (!auditPermission.admin) {
  181. throw '您没有权限操作此功能';
  182. }
  183. let type = '';
  184. switch (data.type) {
  185. case 'add-folder':
  186. await ctx.service.paymentFolder.addFolder(projectId, ctx.session.sessionUser.accountId, data.parentId, data.name);
  187. break;
  188. case 'add-tender':
  189. await ctx.service.paymentTender.addTender(projectId, ctx.session.sessionUser.accountId, data.folderId, data.name);
  190. break;
  191. case 'edit-name':
  192. type = data.postData.type;
  193. const updateData = {
  194. name: data.postData.name,
  195. };
  196. const conditionData = {
  197. id: data.postData.id,
  198. };
  199. if (type === 'tender') {
  200. const tenderInfo = await ctx.service.paymentTender.getDataById(conditionData.id);
  201. if (!tenderInfo) throw '标段不存在';
  202. if (tenderInfo.uid !== ctx.session.sessionUser.accountId && !ctx.session.sessionUser.is_admin) {
  203. throw '您没有权限重命名此标段';
  204. }
  205. await ctx.service.paymentTender.update(updateData, conditionData);
  206. } else {
  207. const folderInfo = await ctx.service.paymentFolder.getDataById(conditionData.id);
  208. if (!folderInfo) throw '文件夹不存在';
  209. if (folderInfo.uid !== ctx.session.sessionUser.accountId && !ctx.session.sessionUser.is_admin) {
  210. throw '您没有权限重命名此文件夹';
  211. }
  212. await ctx.service.paymentFolder.update(updateData, conditionData);
  213. }
  214. break;
  215. case 'del':
  216. type = data.postData.type;
  217. if (type === 'tender') {
  218. await ctx.service.paymentTender.deleteTender(data.postData.id);
  219. } else {
  220. await ctx.service.paymentFolder.deleteFolder(data.postData.id);
  221. }
  222. break;
  223. case 'mode-setting':
  224. if (!ctx.session.sessionUser.is_admin) {
  225. throw '您无权操作';
  226. }
  227. const projectInfo = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  228. const modes = projectInfo.payment_setting ? JSON.parse(projectInfo.payment_setting) : ctx.helper._.cloneDeep(paymentConst.setting_modes);
  229. const checked = data.checked;
  230. if (modes[data.mode_type]) {
  231. const detailCount = await ctx.service.paymentDetail.getCountByPidType(ctx.session.sessionProject.id, modes[data.mode_type].value);
  232. if (detailCount > 0 && !checked) {
  233. throw '已存在对应模块的详情,无法关闭该模块';
  234. }
  235. const mode = modes[data.mode_type];
  236. if (mode.checked === checked) {
  237. throw '已选中该模块,无须重复选中';
  238. }
  239. mode.checked = checked;
  240. } else if (paymentConst.setting_modes[data.mode_type]) {
  241. modes[data.mode_type] = ctx.helper._.cloneDeep(paymentConst.setting_modes[data.mode_type]);
  242. modes[data.mode_type].checked = checked;
  243. } else {
  244. throw '该模块不存在';
  245. }
  246. await ctx.service.project.defaultUpdate({ id: ctx.session.sessionProject.id, payment_setting: JSON.stringify(modes) });
  247. break;
  248. case 'info':
  249. await ctx.service.paymentTenderInfo.saveTenderInfo(data.postData.id, data.postData.info);
  250. break;
  251. default: throw '参数有误';
  252. }
  253. // 先获取你创建的标段及参与的标段
  254. const tenderList = await ctx.service.paymentTender.getList(ctx.session.sessionUser.accountId, auditPermission);
  255. // 获取你创建的目录及对应目录下的所有目录
  256. const folderList = await ctx.service.paymentFolder.getList(ctx.session.sessionUser.accountId, tenderList, auditPermission);
  257. responseData.data.folderList = ctx.helper._.orderBy(folderList, ['in_time'], ['asc']);
  258. responseData.data.tenderList = ctx.helper._.orderBy(tenderList, ['in_time'], ['asc']);
  259. ctx.body = responseData;
  260. } catch (err) {
  261. this.log(err);
  262. ctx.body = { err: 1, msg: err.toString(), data: null };
  263. }
  264. }
  265. async setting(ctx) {
  266. try {
  267. if (!ctx.session.sessionUser.is_admin) {
  268. throw '您无权打开此页';
  269. }
  270. const projectInfo = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  271. const modes = projectInfo.payment_setting ? JSON.parse(projectInfo.payment_setting) : ctx.helper._.cloneDeep(paymentConst.setting_modes);
  272. for (const m in modes) {
  273. const detailCount = await ctx.service.paymentDetail.getCountByPidType(ctx.session.sessionProject.id, modes[m].value);
  274. modes[m].can_check = !detailCount;
  275. }
  276. const renderData = {
  277. setting_modes: paymentConst.setting_modes,
  278. modes,
  279. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.setting),
  280. };
  281. await this.layout('payment/setting.ejs', renderData);
  282. } catch (err) {
  283. console.log(err);
  284. this.log(err);
  285. ctx.session.postError = err.toString();
  286. ctx.redirect('/payment');
  287. }
  288. }
  289. /**
  290. * 获取审批界面所需的 原报、审批人数据等
  291. * @param ctx
  292. * @return {Promise<void>}
  293. * @private
  294. */
  295. async _getDetailAuditViewData(ctx) {
  296. const times = ctx.detail.status === auditConst.status.checkNo ? ctx.detail.times - 1 : ctx.detail.times;
  297. ctx.detail.user = await ctx.service.projectAccount.getAccountInfoById(ctx.detail.uid);
  298. ctx.detail.auditHistory = [];
  299. if (times >= 1) {
  300. for (let i = 1; i <= times; i++) {
  301. ctx.detail.auditHistory.push(await ctx.service.paymentDetailAudit.getAuditors(ctx.detail.id, i));
  302. }
  303. }
  304. // 获取审批流程中左边列表
  305. ctx.detail.auditors2 = ctx.detail.status === auditConst.status.checkNo && ctx.detail.uid !== ctx.session.sessionUser.accountId ?
  306. await ctx.service.paymentDetailAudit.getAuditorsWithOwner(ctx.detail.id, times) :
  307. await ctx.service.paymentDetailAudit.getAuditorsWithOwner(ctx.detail.id, ctx.detail.times);
  308. if (ctx.detail.status === auditConst.status.uncheck || ctx.detail.status === auditConst.status.checkNo) {
  309. ctx.detail.auditorList = await ctx.service.paymentDetailAudit.getAuditors(ctx.detail.id, ctx.detail.times);
  310. }
  311. // 是否已验证手机短信
  312. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  313. ctx.detail.authMobile = pa.auth_mobile;
  314. }
  315. /**
  316. * 支付表单页面
  317. *
  318. * @param {Object} ctx - egg全局页面
  319. * @return {void}
  320. */
  321. async detail(ctx) {
  322. try {
  323. await this._getDetailAuditViewData(ctx);
  324. // 报表信息实时更新
  325. if (ctx.trInfo.uid === ctx.session.sessionUser.accountId && ctx.trInfo.is_del === 0 && ctx.trInfo.rpt_id &&
  326. (ctx.detail.status === auditConst.status.uncheck || ctx.detail.status === auditConst.status.checkNo)) {
  327. const rptTpl = await ctx.service.rptTpl.getDataById(ctx.trInfo.rpt_id);
  328. if (rptTpl) {
  329. const pageRst = await ctx.service.jpcReport.getAllPreviewPagesCommon(rptTpl, 'A4');
  330. // rptTpl.source_type = 101;
  331. // const pageRst = await ctx.service.jpcReport.getPreviewPagesWithDiscreteDataCommon(ctx, rptTpl, 'A4', this.app.baseDir, null);
  332. const rptMsg = pageRst.items[0];
  333. // 判断与原有的报表审批人列表是否有区别
  334. let difference = false;
  335. let auditDifference = false;
  336. let needChange = false;
  337. if (ctx.trInfo.report_items_json) {
  338. const report_items_json = JSON.parse(ctx.trInfo.report_items_json);
  339. const items = ['cells', 'signature_audit_cells', 'signature_cells', 'signature_date_cells', 'interact_cells'];
  340. for (const item of items) {
  341. if (report_items_json[item] &&
  342. !ctx.helper._.isEmpty(ctx.helper._.differenceWith(JSON.parse(JSON.stringify(rptMsg[item])), report_items_json[item], ctx.helper._.isEqual))) {
  343. // 因为interact_cells里存在undefind值,必须先用JSON.parse和JSON.stringify转义才能对比
  344. difference = true;
  345. needChange = true;
  346. if (item === 'signature_cells') {
  347. auditDifference = true;
  348. }
  349. // break;
  350. }
  351. }
  352. } else {
  353. difference = true;
  354. }
  355. if (difference) {
  356. // 删除rpt_audit重新配置
  357. const updateData = {
  358. id: ctx.trInfo.id,
  359. report_items_json: JSON.stringify(ctx.helper._.cloneDeep(rptMsg)),
  360. };
  361. // 删除rpt_audit重新配置
  362. if (auditDifference) {
  363. updateData.rpt_audit = null;
  364. ctx.trInfo.rpt_audit = null;
  365. }
  366. if (needChange) {
  367. updateData.is_change = 1;
  368. ctx.trInfo.is_change = 1;
  369. }
  370. await ctx.service.paymentTenderRpt.defaultUpdate(updateData);
  371. ctx.trInfo.report_items_json = rptMsg;
  372. }
  373. }
  374. }
  375. // 非审批完成可能要更新离散数据(cells值)
  376. if (ctx.detail.status !== auditConst.status.checked) {
  377. const rptTpl = await ctx.service.rptTpl.getDataById(ctx.trInfo.rpt_id);
  378. if (rptTpl) {
  379. rptTpl.source_type = 101;
  380. const pageRst = await ctx.service.jpcReport.getPreviewPagesWithDiscreteDataCommon(ctx, rptTpl, 'A4', this.app.baseDir, null);
  381. const rptMsg = pageRst.items[0];
  382. const report_json = JSON.parse(ctx.detail.report_json);
  383. if (report_json && report_json.items && report_json.items.length > 0 && report_json.items[0].cells &&
  384. !ctx.helper._.isEmpty(ctx.helper._.differenceWith(JSON.parse(JSON.stringify(rptMsg.cells)), report_json.cells, ctx.helper._.isEqual))) {
  385. report_json.items[0].cells = ctx.helper._.cloneDeep(rptMsg.cells);
  386. const newJson = JSON.stringify(report_json);
  387. await this.service.paymentDetail.defaultUpdate({ id: ctx.detail.id, report_json: newJson });
  388. ctx.detail.report_json = newJson;
  389. }
  390. }
  391. }
  392. const auditIdList = ctx.helper._.map(ctx.detail.auditors, 'aid');
  393. const rptAuditIdList = ctx.helper._.map(ctx.detail.rptAudits, 'uid');
  394. const uidList = ctx.helper._.uniq([...auditIdList, ...rptAuditIdList]);
  395. // 获取附件列表
  396. const attList = await ctx.service.paymentDetailAtt.getPaymentDetailAttachment(ctx.detail.id, 'desc');
  397. const renderData = {
  398. trInfo: ctx.trInfo,
  399. paymentConst,
  400. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.detail),
  401. auditConst,
  402. shenpiConst,
  403. attList,
  404. moment,
  405. whiteList: ctx.app.config.multipart.whitelist,
  406. uidList,
  407. preUrl: '/payment/' + ctx.paymentTender.id + '/detail/' + ctx.detail.id,
  408. OSS_PATH: ctx.app.config.fujianOssPath,
  409. };
  410. renderData.nextDetail = await ctx.service.paymentDetail.getDataByCondition({ tr_id: ctx.trInfo.id, order: ctx.detail.order + 1 });
  411. let report_json = JSON.parse(ctx.detail.report_json);
  412. const content = [];
  413. // 获取当前报表人
  414. const rptAudit = await ctx.service.paymentRptAudit.getDataByCondition({ td_id: ctx.detail.id, uid: ctx.session.sessionUser.accountId });
  415. if (report_json && report_json.items && report_json.items.length > 0 && report_json.items[0].interact_cells.length > 0) {
  416. // if (report_json && report_json.items && report_json.items[0].interact_cells.length > 0) {
  417. for (const [i, cell] of report_json.items[0].interact_cells.entries()) {
  418. cell.index = i;
  419. }
  420. const numberList = ctx.helper._.filter(report_json.items[0].interact_cells, { DataType: 'intact_type_number' });
  421. for (const [i, cell] of report_json.items[0].interact_cells.entries()) {
  422. if (cell.Label.indexOf('大写') !== -1 && cell.link === undefined) {
  423. if (numberList.length > 0) {
  424. const numberInfo = ctx.helper._.find(ctx.helper._.orderBy(numberList, ['index'], ['desc']), function(item) {
  425. return item.index < i;
  426. });
  427. cell.link = numberInfo ? numberInfo.index : '';
  428. } else {
  429. cell.link = '';
  430. }
  431. }
  432. const push_item = {
  433. type: paymentConst.rpt_dataType[cell.DataType],
  434. value: cell.Prefix ? ctx.helper._.replace(cell.Value, cell.Prefix, '') : cell.Value,
  435. label: cell.Label,
  436. index: i,
  437. };
  438. if (cell.link !== undefined) push_item.link = cell.link;
  439. const thisBandName = paymentConst.rpt_band_name[cell.BandName] ? cell.BandName : 'content';
  440. const oneShowContent = ctx.helper._.find(content, { BandName: thisBandName });
  441. if (oneShowContent) {
  442. oneShowContent.items.push(push_item);
  443. } else {
  444. content.push({
  445. BandName: thisBandName,
  446. title: paymentConst.rpt_band_name[thisBandName],
  447. items: [push_item],
  448. });
  449. }
  450. }
  451. renderData.numberList = numberList;
  452. if (rptAudit && ((ctx.detail.status !== auditConst.status.checkNo && ctx.detail.status !== auditConst.status.checked) ||
  453. (ctx.detail.status === auditConst.status.checked && !renderData.nextDetail) ||
  454. (ctx.detail.status === auditConst.status.checkNo && ctx.detail.uid === ctx.session.sessionUser.accountId))) {
  455. // 获取个人签字,单位章,个人章地址
  456. const userInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  457. const stampPathList = userInfo.stamp_path ? userInfo.stamp_path.split('!;!') : [];
  458. const companyInfo = userInfo.company ? await ctx.service.constructionUnit.getDataByCondition({ pid: ctx.session.sessionProject.id, name: userInfo.company }) : {};
  459. if (rptAudit.signature_msg) {
  460. const sign_msg = JSON.parse(rptAudit.signature_msg);
  461. report_json = await ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg);
  462. rptAudit.signature_msg = sign_msg;
  463. } else {
  464. rptAudit.signature_msg = paymentConst.signature_msg;
  465. rptAudit.signature_msg.sign_path = userInfo.sign_path ? userInfo.sign_path : '';
  466. }
  467. renderData.signPath = userInfo.sign_path ? userInfo.sign_path : '';
  468. renderData.stampPathList = stampPathList;
  469. renderData.currentStamp = rptAudit && rptAudit.signature_msg.stamp_path ? rptAudit.signature_msg.stamp_path : (stampPathList.length > 0 ? stampPathList[0] : '');
  470. renderData.companyStamp = companyInfo && companyInfo.sign_path ? companyInfo.sign_path : '';
  471. }
  472. }
  473. renderData.rptAudit = rptAudit;
  474. renderData.content = content;
  475. renderData.report_json = report_json;
  476. if ((ctx.detail.status === auditConst.status.uncheck || ctx.detail.status === auditConst.status.checkNo) && ctx.session.sessionUser.accountId === ctx.detail.uid) {
  477. // data.accountGroup = accountGroup;
  478. // 获取所有项目参与者
  479. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  480. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  481. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  482. });
  483. renderData.accountList = accountList;
  484. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  485. const accountGroupList = unitList.map(item => {
  486. const groupList = accountList.filter(item1 => item1.company === item.name);
  487. return { groupName: item.name, groupList };
  488. });
  489. renderData.accountGroup = accountGroupList;
  490. }
  491. await this.layout('payment/detail.ejs', renderData, 'payment/detail_modal.ejs');
  492. } catch (err) {
  493. console.log(err);
  494. this.log(err);
  495. ctx.session.postError = err.toString();
  496. if (ctx.detail.tender_id && ctx.detail.tr_id) {
  497. ctx.redirect('/payment' + ctx.detail.tender_id + '/list/' + ctx.detail.tr_id);
  498. }
  499. ctx.redirect(this.menu.menu.dashboard.url);
  500. }
  501. }
  502. async detailSave(ctx) {
  503. try {
  504. const data = JSON.parse(ctx.request.body.data);
  505. if (!data.type) {
  506. throw '提交数据错误';
  507. }
  508. // 检查权限等
  509. if (ctx.detail.uid !== ctx.session.sessionUser.accountId && data.type !== 'update_sign') {
  510. throw '您无权操作';
  511. }
  512. if (data.type !== 'update_sign' && ctx.detail.status !== auditConst.status.uncheck && ctx.detail.status !== auditConst.status.checkNo) {
  513. throw '您无权操作';
  514. }
  515. const responseData = {
  516. err: 0, msg: '', data: {},
  517. };
  518. switch (data.type) {
  519. case 'update_rpt':
  520. responseData.data = await ctx.service.paymentDetail.updateReportJson(ctx.detail.id, data.report_json);
  521. break;
  522. case 'update_sign':
  523. if (ctx.detail.status === auditConst.status.checked) {
  524. // 判断是否存在下一期,是则无法签章
  525. const detailMsg = await ctx.service.paymentDetail.getDataByCondition({ tr_id: ctx.detail.tr_id, order: ctx.detail.order + 1 });
  526. if (detailMsg) {
  527. throw '您无法操作签字/签章';
  528. }
  529. }
  530. responseData.data = await ctx.service.paymentRptAudit.updateSignatureMsg(ctx.detail.id, ctx.session.sessionUser.accountId, data.signature_msg);
  531. break;
  532. case 'add_audit':
  533. const auditorId = this.app._.toInteger(data.auditorId);
  534. if (isNaN(auditorId) || auditorId <= 0) {
  535. throw '参数错误';
  536. }
  537. ctx.detail.auditorList = await ctx.service.paymentDetailAudit.getAuditors(ctx.detail.id, ctx.detail.times);
  538. // 检查审核人是否已存在
  539. const exist = this.app._.find(ctx.detail.auditorList, { aid: auditorId });
  540. if (exist) {
  541. throw '该审核人已存在,请勿重复添加';
  542. }
  543. const shenpiInfo = await ctx.service.paymentShenpiAudit.getDataByCondition({ tr_id: ctx.trInfo.id, sp_status: shenpiConst.sp_status.gdzs });
  544. const is_gdzs = shenpiInfo && ctx.trInfo.sp_status === shenpiConst.sp_status.gdzs ? 1 : 0;
  545. const result = await ctx.service.paymentDetailAudit.addAuditor(ctx.detail.id, auditorId, ctx.detail.times, is_gdzs);
  546. if (!result) {
  547. throw '添加审核人失败';
  548. }
  549. responseData.data = await ctx.service.paymentDetailAudit.getAuditorsWithOwner(ctx.detail.id, ctx.detail.times);
  550. break;
  551. case 'del_audit':
  552. const auditorId2 = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  553. if (isNaN(auditorId2) || auditorId2 <= 0) {
  554. throw '参数错误';
  555. }
  556. const result2 = await ctx.service.paymentDetailAudit.deleteAuditor(ctx.detail.id, auditorId2, ctx.detail.times);
  557. if (!result2) {
  558. throw '移除审核人失败';
  559. }
  560. responseData.data = await ctx.service.paymentDetailAudit.getAuditors(ctx.detail.id, ctx.detail.times);
  561. break;
  562. case 'follow_rpt_audit':
  563. if (ctx.trInfo.shenpi_status === shenpiConst.sp_status.gdspl) {
  564. throw '当前表单已设置为固定审批流,无法同步';
  565. }
  566. const result3 = await ctx.service.paymentDetailAudit.followAuditByRptAudit(ctx.detail);
  567. if (!result3) {
  568. throw '同步表单角色失败';
  569. }
  570. responseData.data = await ctx.service.paymentDetailAudit.getAuditorsWithOwner(ctx.detail.id, ctx.detail.times);
  571. break;
  572. default: throw '参数有误';
  573. }
  574. ctx.body = responseData;
  575. } catch (err) {
  576. this.log(err);
  577. ctx.body = { err: 1, msg: err.toString(), data: null };
  578. }
  579. }
  580. async deleteDetail(ctx) {
  581. try {
  582. const detail_id = ctx.request.body.detail_id;
  583. const detailInfo = await ctx.service.paymentDetail.getDataById(detail_id);
  584. if (!detailInfo) {
  585. throw '所选报表表单详情已删除';
  586. }
  587. // 获取最新的期数
  588. const detail_highOrder = await ctx.service.paymentDetail.count({
  589. tr_id: detailInfo.tr_id,
  590. });
  591. if (!(ctx.session.sessionUser.is_admin || ((detailInfo.status === auditConst.status.uncheck || detailInfo.status === auditConst.status.checkNo) && detailInfo.uid === ctx.session.sessionUser.accountId)) || detail_highOrder !== detailInfo.order) {
  592. throw '您无权删除所选报表表单详情';
  593. }
  594. const result = await ctx.service.paymentDetail.deleteDetail(detail_id);
  595. if (!result) {
  596. throw '删除报表表单详情失败,请重试';
  597. }
  598. ctx.redirect('/payment/' + ctx.paymentTender.id + '/list/' + detailInfo.tr_id);
  599. } catch (err) {
  600. this.log(err);
  601. ctx.session.postError = err.toString();
  602. ctx.redirect(ctx.request.header.referer);
  603. }
  604. }
  605. /**
  606. * 期审批流程(Get)
  607. * @param ctx
  608. * @return {Promise<void>}
  609. */
  610. async detailAuditors(ctx) {
  611. try {
  612. const responseData = {
  613. err: 0, msg: '', data: {},
  614. };
  615. const order = JSON.parse(ctx.request.body.data).order;
  616. const tr_id = ctx.params.trid;
  617. const detailInfo = await ctx.service.paymentDetail.getDataByCondition({ tr_id, order });
  618. // 获取审批流程中右边列表
  619. const auditHistory = [];
  620. const times = detailInfo.status === auditConst.status.checkNo ? detailInfo.times - 1 : detailInfo.times;
  621. if (times >= 1) {
  622. for (let i = 1; i <= times; i++) {
  623. auditHistory.push(await ctx.service.paymentDetailAudit.getAuditors(detailInfo.id, i));
  624. }
  625. }
  626. responseData.data.auditHistory = auditHistory;
  627. // 获取审批流程中左边列表
  628. responseData.data.auditors = await ctx.service.paymentDetailAudit.getAuditorsWithOwner(detailInfo.id, times);
  629. responseData.data.user = await ctx.service.projectAccount.getAccountInfoById(detailInfo.uid);
  630. ctx.body = responseData;
  631. } catch (error) {
  632. this.log(error);
  633. ctx.body = { err: 1, msg: error.toString(), data: null };
  634. }
  635. }
  636. async _returnRptProjectList(ctx, formProcess = false) {
  637. // 获取报表表单列表
  638. if (ctx.payment.auditPermission.view_all || ctx.paymentTender.uid === ctx.session.sessionUser.accountId || formProcess) {
  639. const rptProject = await ctx.service.rptTreeNode.getDataByCondition({ pid: ctx.session.sessionProject.id, source_type: 100 });
  640. const rptProjectList = [];
  641. const newRptList = rptProject && rptProject.items ? JSON.parse(rptProject.items) : [];
  642. ctx.rptListNum = 0;
  643. await this.getNewRptProjectList(ctx, newRptList, rptProjectList, 1);
  644. const tenderRptList = await ctx.service.paymentTenderRpt.getProcessList(ctx.paymentTender.id);
  645. if (tenderRptList === -1) {
  646. throw '未配置表单设置,请联系管理员处理';
  647. }
  648. return await ctx.service.paymentTenderRpt.checkAndUpdateList(tenderRptList, rptProjectList, formProcess);
  649. }
  650. return await ctx.service.paymentTenderRpt.getList(ctx.paymentTender.id, ctx.session.sessionUser.accountId);
  651. }
  652. // 循环获取到到rptProject
  653. async getNewRptProjectList(ctx, newRptList, rptProjectList, level = 1) {
  654. if (newRptList.length > 0) {
  655. for (const r of newRptList) {
  656. r.level = level;
  657. r.index = ctx.rptListNum;
  658. ctx.rptListNum = ctx.rptListNum + 1;
  659. rptProjectList.push(r);
  660. if (r.items && r.items.length > 0) {
  661. await this.getNewRptProjectList(ctx, r.items, rptProjectList, level + 1);
  662. }
  663. }
  664. }
  665. }
  666. async process(ctx) {
  667. try {
  668. if (!ctx.payment.auditPermission || !ctx.payment.auditPermission.admin || !(ctx.session.sessionUser.is_admin || ctx.paymentTender.uid === ctx.session.sessionUser.accountId)) {
  669. throw '权限不足';
  670. }
  671. let [tenderRptList, rptProjectList] = await this._returnRptProjectList(ctx, true);
  672. const safeRpt = tenderRptList.find(x => { return x.type === 1; });
  673. tenderRptList = ctx.helper._.filter(tenderRptList, { type: 0, is_del: 0 });
  674. // for (const tr of tenderRptList) {
  675. // if (tr.status === shenpiConst.sp_status.gdspl) {
  676. // tr.auditList = await ctx.service.paymentShenpiAudit.getAuditList(ctx.tender.id, tr.id, tr.sp_status);
  677. // } else if (tr.status === shenpiConst.sp_status.gdzs) {
  678. // tr.audit = await ctx.service.paymentShenpiAudit.getAudit(ctx.tender.id, tr.id, tr.sp_status);
  679. // }
  680. // }
  681. // 获取所有项目参与者
  682. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  683. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  684. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  685. });
  686. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  687. const accountGroupList = unitList.map(item => {
  688. const groupList = accountList.filter(item1 => item1.company === item.name);
  689. return { groupName: item.name, groupList };
  690. });
  691. const renderData = {
  692. tender: ctx.paymentTender,
  693. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.process),
  694. safeRpt,
  695. rptProjectList,
  696. tenderRptList,
  697. shenpi: shenpiConst,
  698. accountList,
  699. accountGroup: accountGroupList,
  700. };
  701. await this.layout('payment/process.ejs', renderData, 'payment/process_modal.ejs');
  702. } catch (err) {
  703. console.log(err);
  704. this.log(err);
  705. ctx.session.postError = err.toString();
  706. ctx.redirect(this.request && this.request.headers && this.request.headers.referer ? this.request.headers.referer : '/payment');
  707. }
  708. }
  709. async processSave(ctx) {
  710. try {
  711. if (!ctx.payment.auditPermission || !ctx.payment.auditPermission.admin) {
  712. throw '权限不足';
  713. }
  714. const responseData = {
  715. err: 0, msg: '', data: {},
  716. };
  717. const data = JSON.parse(ctx.request.body.data);
  718. if (!data.type) {
  719. throw '提交数据错误';
  720. }
  721. switch (data.type) {
  722. case 'add-rpt':
  723. responseData.data = await ctx.service.paymentTenderRpt.setRpt(ctx.paymentTender.id, data.rpt_list);
  724. break;
  725. case 'change-status':
  726. responseData.data = await ctx.service.paymentTenderRpt.setStatus(data.tr_id, data.status);
  727. break;
  728. case 'get-audits':
  729. responseData.data = await ctx.service.paymentShenpiAudit.getShenpiAudit(data.tr_id, data.status);
  730. break;
  731. case 'add-audit':
  732. responseData.data = await ctx.service.paymentShenpiAudit.addAudit(data);
  733. break;
  734. case 'del-audit':
  735. responseData.data = await ctx.service.paymentShenpiAudit.removeAudit(data);
  736. break;
  737. case 'update-report':
  738. responseData.data = await ctx.service.paymentTenderRpt.updateReport(data);
  739. break;
  740. default: throw '参数有误';
  741. }
  742. ctx.body = responseData;
  743. } catch (err) {
  744. this.log(err);
  745. ctx.body = { err: 1, msg: err.toString(), data: null };
  746. }
  747. }
  748. async rptList(ctx) {
  749. try {
  750. const tenderRptList = await this._returnRptProjectList(ctx);
  751. if (tenderRptList === -1) {
  752. throw '未配置表单设置,请联系管理员处理';
  753. }
  754. if (tenderRptList.length === 0) {
  755. throw '未配置表单设置,请联系管理员处理';
  756. }
  757. for (const tr of tenderRptList) {
  758. tr.have_notice = await ctx.service.paymentDetail.haveNotice2TenderRpt(tr.id, ctx.session.sessionUser.accountId);
  759. }
  760. const trid = ctx.params.trid ? parseInt(ctx.params.trid) : 0;
  761. const trInfo = trid ? ctx.helper._.find(tenderRptList, { id: trid }) : tenderRptList[0];
  762. if (!trInfo) {
  763. throw '该模块已关闭或未进行表单审批设置';
  764. }
  765. // 获取列表
  766. const trDetailList = await ctx.service.paymentDetail.getValidDetails(trInfo.id);
  767. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  768. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  769. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  770. });
  771. if (trDetailList.length > 0) {
  772. for (const s of trDetailList) {
  773. // s.curAuditor = null;
  774. // 根据期状态返回展示用户
  775. s.curAuditor = await ctx.service.paymentDetailAudit.getAuditorByStatus(s.id, s.status, s.times);
  776. const userInfo = ctx.helper._.find(accountList, { id: s.uid });
  777. s.user_name = userInfo ? userInfo.name : '';
  778. }
  779. trDetailList[0].emptySign = await ctx.service.paymentRptAudit.haveEmptySign(trDetailList[0].id);
  780. }
  781. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  782. const tenderInfo = await this.ctx.service.paymentTenderInfo.getTenderInfo(ctx.paymentTender.id);
  783. const renderData = {
  784. tender: ctx.paymentTender,
  785. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.list),
  786. tenderRptList,
  787. trInfo,
  788. trDetailList,
  789. rptMsg: null,
  790. auditConst,
  791. accountGroup: [],
  792. accountList: [],
  793. paymentConst,
  794. preUrl: '/payment/' + ctx.paymentTender.id + '/list/' + trInfo.id,
  795. categoryData,
  796. tenderInfo,
  797. };
  798. // 获取报表信息,新增时及设置报表角色时使用
  799. if (trInfo.uid === ctx.session.sessionUser.accountId && trInfo.is_del === 0 && trInfo.rpt_id) {
  800. const rptTpl = await ctx.service.rptTpl.getDataById(trInfo.rpt_id);
  801. if (rptTpl) {
  802. // rptTpl.source_type = 101;
  803. const pageRst = await ctx.service.jpcReport.getAllPreviewPagesCommon(rptTpl, 'A4');
  804. // const pageRst = await ctx.service.jpcReport.getPreviewPagesWithDiscreteDataCommon(ctx, rptTpl, 'A4', this.app.baseDir, null);
  805. renderData.rptMsg = pageRst.items[0];
  806. // 判断与原有的报表审批人列表是否有区别
  807. let difference = false;
  808. let auditDifference = false;
  809. let needChange = false;
  810. if (trInfo.report_items_json) {
  811. const report_items_json = JSON.parse(trInfo.report_items_json);
  812. const items = ['cells', 'signature_audit_cells', 'signature_cells', 'signature_date_cells', 'interact_cells'];
  813. for (const item of items) {
  814. if (report_items_json[item] &&
  815. !ctx.helper._.isEmpty(ctx.helper._.differenceWith(JSON.parse(JSON.stringify(renderData.rptMsg[item])), report_items_json[item], ctx.helper._.isEqual))) {
  816. // 因为interact_cells里存在undefind值,必须先用JSON.parse和JSON.stringify转义才能对比
  817. difference = true;
  818. needChange = true;
  819. if (item === 'signature_cells') {
  820. auditDifference = true;
  821. }
  822. // break;
  823. }
  824. }
  825. } else {
  826. difference = true;
  827. }
  828. if (difference) {
  829. // 删除rpt_audit重新配置
  830. const updateData = {
  831. id: trInfo.id,
  832. report_items_json: JSON.stringify(ctx.helper._.cloneDeep(renderData.rptMsg)),
  833. };
  834. // 删除rpt_audit重新配置
  835. if (auditDifference) {
  836. updateData.rpt_audit = null;
  837. trInfo.rpt_audit = null;
  838. }
  839. if (needChange) {
  840. updateData.is_change = 1;
  841. trInfo.is_change = 1;
  842. }
  843. await ctx.service.paymentTenderRpt.defaultUpdate(updateData);
  844. trInfo.report_items_json = renderData.rptMsg;
  845. }
  846. if (trInfo.rpt_audit) {
  847. trInfo.rpt_audit = JSON.parse(trInfo.rpt_audit);
  848. const prjAccList = await ctx.service.projectAccount.getAllAccountByProjectId(ctx.session.sessionProject.id);
  849. for (const audit of trInfo.rpt_audit) {
  850. if (audit.uid) {
  851. const info = ctx.helper._.find(prjAccList, { id: audit.uid });
  852. audit.name = info.name;
  853. }
  854. }
  855. } else {
  856. trInfo.rpt_audit = [];
  857. for (const sc of renderData.rptMsg.signature_cells) {
  858. trInfo.rpt_audit.push({
  859. uid: null,
  860. rpt_name: sc.signature_name,
  861. });
  862. }
  863. }
  864. renderData.trInfo = trInfo;
  865. }
  866. if (renderData.rptMsg) {
  867. // 获取所有项目参与者
  868. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  869. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  870. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  871. });
  872. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  873. const accountGroupList = unitList.map(item => {
  874. const groupList = accountList.filter(item1 => item1.company === item.name);
  875. return { groupName: item.name, groupList };
  876. });
  877. renderData.accountList = accountList;
  878. renderData.accountGroup = accountGroupList;
  879. }
  880. }
  881. await this.layout('payment/list.ejs', renderData, 'payment/list_modal.ejs');
  882. } catch (err) {
  883. console.log(err);
  884. this.log(err);
  885. ctx.session.postError = err.toString();
  886. ctx.redirect(this.request && this.request.headers && this.request.headers.referer ? this.request.headers.referer : '/payment');
  887. }
  888. }
  889. async rptSave(ctx) {
  890. try {
  891. const tr_id = ctx.params.trid;
  892. if (!tr_id) {
  893. throw '参数有误';
  894. }
  895. const trInfo = await ctx.service.paymentTenderRpt.getDataById(tr_id);
  896. if (!trInfo) {
  897. throw '标段报表不存在';
  898. }
  899. if (ctx.session.sessionUser.accountId !== trInfo.uid) {
  900. throw '权限不足';
  901. }
  902. const responseData = {
  903. err: 0, msg: '', data: {},
  904. };
  905. const data = JSON.parse(ctx.request.body.data);
  906. if (!data.type) {
  907. throw '提交数据错误';
  908. }
  909. switch (data.type) {
  910. case 'rpt_audit':
  911. responseData.data = await ctx.service.paymentTenderRpt.updateRptAudit(trInfo, data.rpt_audit);
  912. break;
  913. case 'add-detail':
  914. responseData.data = await ctx.service.paymentDetail.addDetail(trInfo, data.code, data.s_time);
  915. break;
  916. default: throw '参数有误';
  917. }
  918. ctx.body = responseData;
  919. } catch (err) {
  920. this.log(err);
  921. ctx.body = { err: 1, msg: err.toString(), data: null };
  922. }
  923. }
  924. /**
  925. * 上报和重新上报
  926. * @param ctx
  927. * @return {Promise<void>}
  928. */
  929. async startAudit(ctx) {
  930. try {
  931. if (ctx.detail.uid !== ctx.session.sessionUser.accountId) {
  932. throw '您无权上报报表表单详情数据';
  933. }
  934. if (ctx.detail.status === auditConst.status.checking || ctx.detail.status === auditConst.status.checked) {
  935. throw '该报表表单详情数据当前无法上报';
  936. }
  937. await ctx.service.paymentDetailAudit.start(ctx.detail.id, ctx.detail.times);
  938. ctx.redirect(ctx.request.header.referer);
  939. } catch (err) {
  940. this.log(err);
  941. ctx.session.postError = err.toString();
  942. ctx.redirect(ctx.request.header.referer);
  943. }
  944. }
  945. /**
  946. * 审批
  947. * @param ctx
  948. * @return {Promise<void>}
  949. */
  950. async checkAudit(ctx) {
  951. try {
  952. if (!ctx.detail || ctx.detail.status !== auditConst.status.checking) {
  953. throw '当前报表表单详情数据有误';
  954. }
  955. if (!ctx.detail.curAuditor || ctx.detail.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  956. throw '您无权进行该操作';
  957. }
  958. const data = {
  959. checkType: parseInt(ctx.request.body.checkType),
  960. opinion: ctx.request.body.opinion,
  961. };
  962. if (!data.checkType || isNaN(data.checkType)) {
  963. throw '提交数据错误';
  964. }
  965. if (data.checkType === auditConst.status.checkNo) {
  966. if (!data.checkType || isNaN(data.checkType)) {
  967. throw '提交数据错误';
  968. }
  969. }
  970. await ctx.service.paymentDetailAudit.check(ctx.detail.id, data, ctx.detail.times);
  971. ctx.redirect(ctx.request.header.referer);
  972. } catch (err) {
  973. this.log(err);
  974. ctx.session.postError = err.toString();
  975. ctx.redirect(ctx.request.header.referer);
  976. }
  977. }
  978. /**
  979. * 上传附件
  980. * @param {Object} ctx - egg全局变量
  981. * @return {void}
  982. */
  983. async uploadDetailFile(ctx) {
  984. const responseData = {
  985. err: 0,
  986. msg: '',
  987. data: [],
  988. };
  989. let stream;
  990. try {
  991. const parts = ctx.multipart({ autoFields: true });
  992. const files = [];
  993. let index = 0;
  994. const extra_upload = ctx.detail.status === auditConst.status.checked;
  995. const original_data = {
  996. tender_id: ctx.paymentTender.id,
  997. tr_id: ctx.trInfo.id,
  998. td_id: ctx.detail.id,
  999. };
  1000. while ((stream = await parts()) !== undefined) {
  1001. // 判断用户是否选择上传文件
  1002. if (!stream.filename) {
  1003. throw '请选择上传的文件!';
  1004. }
  1005. const fileInfo = path.parse(stream.filename);
  1006. const create_time = Date.parse(new Date()) / 1000;
  1007. const filepath = `app/public/upload/payment/${original_data.tender_id}/detail/fujian_${create_time + index.toString() + fileInfo.ext}`;
  1008. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  1009. await sendToWormhole(stream);
  1010. // 保存数据到att表
  1011. const fileData = {
  1012. upload_time: new Date(),
  1013. filename: fileInfo.name,
  1014. fileext: fileInfo.ext,
  1015. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  1016. filepath,
  1017. extra_upload,
  1018. };
  1019. if (parts.field.safe_id) fileData.safe_id = parts.field.safe_id;
  1020. const result = await ctx.service.paymentDetailAtt.save(original_data, fileData, ctx.session.sessionUser.accountId);
  1021. if (!result) {
  1022. throw '导入数据库保存失败';
  1023. }
  1024. fileData.uid = ctx.session.sessionUser.accountId;
  1025. fileData.u_name = ctx.session.sessionUser.name;
  1026. fileData.id = result.insertId;
  1027. fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
  1028. delete fileData.filepath;
  1029. if (!ctx.helper.canPreview(fileData.fileext)) {
  1030. fileData.filepath = `/payment/${original_data.tender_id}/detail/${original_data.td_id}/file/${fileData.id}/download`;
  1031. } else {
  1032. fileData.filepath = ctx.app.config.fujianOssPath + filepath;
  1033. fileData.viewpath = ctx.app.config.fujianOssPath + filepath;
  1034. }
  1035. files.push(fileData);
  1036. ++index;
  1037. }
  1038. responseData.data = files;
  1039. } catch (err) {
  1040. this.log(err);
  1041. // 失败需要消耗掉stream 以防卡死
  1042. if (stream) {
  1043. await sendToWormhole(stream);
  1044. }
  1045. this.setMessage(err.toString(), this.messageType.ERROR);
  1046. }
  1047. ctx.body = responseData;
  1048. }
  1049. /**
  1050. * 下载附件
  1051. * @param {Object} ctx - egg全局变量
  1052. * @return {void}
  1053. */
  1054. async downloadDetailFile(ctx) {
  1055. const id = ctx.params.fid;
  1056. if (id) {
  1057. try {
  1058. const fileInfo = await ctx.service.paymentDetailAtt.getDataById(id);
  1059. if (fileInfo !== undefined && fileInfo !== '') {
  1060. // 解决中文无法下载问题
  1061. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1062. let disposition = '';
  1063. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1064. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  1065. } else if (userAgent.indexOf('firefox') >= 0) {
  1066. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  1067. } else {
  1068. /* safari等其他非主流浏览器只能自求多福了 */
  1069. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  1070. }
  1071. ctx.response.set({
  1072. 'Content-Type': 'application/octet-stream',
  1073. 'Content-Disposition': disposition,
  1074. 'Content-Length': fileInfo.filesize,
  1075. });
  1076. // ctx.body = await fs.createReadStream(fileName);
  1077. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  1078. } else {
  1079. throw '不存在该文件';
  1080. }
  1081. } catch (err) {
  1082. this.log(err);
  1083. this.setMessage(err.toString(), this.messageType.ERROR);
  1084. }
  1085. }
  1086. }
  1087. /**
  1088. * 删除附件
  1089. * @param {Object} ctx - egg全局变量
  1090. * @return {void}
  1091. */
  1092. async deleteDetailFile(ctx) {
  1093. const responseData = {
  1094. err: 0,
  1095. msg: '',
  1096. data: '',
  1097. };
  1098. try {
  1099. const data = JSON.parse(ctx.request.body.data);
  1100. const fileInfo = await ctx.service.paymentDetailAtt.getDataById(data.id);
  1101. if (!fileInfo || !Object.keys(fileInfo).length) {
  1102. throw '该文件不存在';
  1103. }
  1104. if (!fileInfo.extra_upload && ctx.detail.status === auditConst.status.checked) {
  1105. throw '无权限删除';
  1106. }
  1107. if (fileInfo !== undefined && fileInfo !== '') {
  1108. // 先删除文件
  1109. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  1110. // 再删除数据库
  1111. await ctx.service.paymentDetailAtt.deleteById(data.id);
  1112. responseData.data = '';
  1113. } else {
  1114. throw '不存在该文件';
  1115. }
  1116. } catch (err) {
  1117. responseData.err = 1;
  1118. responseData.msg = err;
  1119. }
  1120. ctx.body = responseData;
  1121. }
  1122. async safeBills(ctx) {
  1123. try {
  1124. await this._getDetailAuditViewData(ctx);
  1125. const auditIdList = ctx.helper._.map(ctx.detail.auditors, 'aid');
  1126. const rptAuditIdList = ctx.helper._.map(ctx.detail.rptAudits, 'uid');
  1127. const uidList = ctx.helper._.uniq([...auditIdList, ...rptAuditIdList]);
  1128. // 获取附件列表
  1129. const attList = await ctx.service.paymentDetailAtt.getPaymentDetailAttachment(ctx.detail.id, 'desc');
  1130. const stdBills = await ctx.service.stdGclList.getSafeGcl();
  1131. const renderData = {
  1132. trInfo: ctx.trInfo,
  1133. paymentConst,
  1134. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.safe),
  1135. auditConst,
  1136. shenpiConst,
  1137. attList,
  1138. moment,
  1139. whiteList: ctx.app.config.multipart.whitelist,
  1140. uidList,
  1141. preUrl: '/payment/' + ctx.paymentTender.id + '/detail/' + ctx.detail.id,
  1142. OSS_PATH: ctx.app.config.fujianOssPath,
  1143. stdBills,
  1144. };
  1145. renderData.nextDetail = await ctx.service.paymentDetail.getDataByCondition({ tr_id: ctx.trInfo.id, order: ctx.detail.order + 1 });
  1146. const content = [];
  1147. renderData.content = content;
  1148. if ((ctx.detail.status === auditConst.status.uncheck || ctx.detail.status === auditConst.status.checkNo) && ctx.session.sessionUser.accountId === ctx.detail.uid) {
  1149. // 获取所有项目参与者
  1150. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  1151. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  1152. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  1153. });
  1154. renderData.accountList = accountList;
  1155. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  1156. const accountGroupList = unitList.map(item => {
  1157. const groupList = accountList.filter(item1 => item1.company === item.name);
  1158. return { groupName: item.name, groupList };
  1159. });
  1160. renderData.accountGroup = accountGroupList;
  1161. }
  1162. await this.layout('payment_safe/index.ejs', renderData, 'payment_safe/modal.ejs');
  1163. } catch (err) {
  1164. this.log(err);
  1165. this.ctx.postError(err, '读取安全生产费错误');
  1166. if (ctx.detail.tender_id && ctx.detail.tr_id) {
  1167. ctx.redirect('/payment' + ctx.detail.tender_id + '/list');
  1168. } else {
  1169. ctx.redirect(this.menu.menu.dashboard.url);
  1170. }
  1171. }
  1172. }
  1173. async safeCompare(ctx) {
  1174. try {
  1175. const renderData = {
  1176. trInfo: ctx.trInfo,
  1177. paymentConst,
  1178. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.compare),
  1179. auditConst,
  1180. };
  1181. await this.layout('payment_safe/compare.ejs', renderData);
  1182. } catch (err) {
  1183. this.log(err);
  1184. this.ctx.postError(err, '读取安全生产费错误');
  1185. if (ctx.detail.tender_id && ctx.detail.tr_id) {
  1186. ctx.redirect('/payment' + ctx.detail.tender_id + '/list');
  1187. } else {
  1188. ctx.redirect(this.menu.menu.dashboard.url);
  1189. }
  1190. }
  1191. }
  1192. async safeLoad(ctx) {
  1193. try {
  1194. const data = JSON.parse(ctx.request.body.data);
  1195. const filter = data.filter.split(';');
  1196. const responseData = { err: 0, msg: '', data: {}, hpack: [] };
  1197. for (const f of filter) {
  1198. switch (f) {
  1199. case 'bills':
  1200. responseData.data.bills = ctx.detail.readOnly
  1201. ? await ctx.service.paymentSafeBills.getReadData(ctx.detail)
  1202. : await ctx.service.paymentSafeBills.getEditData(ctx.detail);
  1203. break;
  1204. case 'billsCompare':
  1205. responseData.data[f] = await ctx.service.paymentSafeBills.getCompareData(ctx.detail);
  1206. break;
  1207. case 'auditFlow':
  1208. responseData.data[f] = await ctx.service.paymentDetailAudit.getViewFlow(ctx.detail);
  1209. break;
  1210. case 'att':
  1211. responseData.data[f] = await ctx.service.paymentDetailAtt.getPaymentDetailAttachment(ctx.detail.id, 'DESC');
  1212. break;
  1213. default:
  1214. responseData.data[f] = [];
  1215. break;
  1216. }
  1217. }
  1218. ctx.body = responseData;
  1219. } catch (err) {
  1220. this.log(err);
  1221. ctx.body = { err: 1, msg: err.toString(), data: null };
  1222. }
  1223. }
  1224. async _billsBase(detail, type, data) {
  1225. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  1226. if (type !== 'add') {
  1227. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  1228. }
  1229. switch (type) {
  1230. case 'add':
  1231. return await this.ctx.service.paymentSafeBills.addSafeBillsNode(detail, data.id, data.count);
  1232. case 'delete':
  1233. return await this.ctx.service.paymentSafeBills.delete(detail.id, data.id, data.count);
  1234. case 'up-move':
  1235. return await this.ctx.service.paymentSafeBills.upMoveNode(detail.id, data.id, data.count);
  1236. case 'down-move':
  1237. return await this.ctx.service.paymentSafeBills.downMoveNode(detail.id, data.id, data.count);
  1238. case 'up-level':
  1239. return await this.ctx.service.paymentSafeBills.upLevelNode(detail.id, data.id, data.count);
  1240. case 'down-level':
  1241. return await this.ctx.service.paymentSafeBills.downLevelNode(detail.id, data.id, data.count);
  1242. }
  1243. }
  1244. async safeUpdate(ctx) {
  1245. try {
  1246. const data = JSON.parse(ctx.request.body.data);
  1247. if (!data.postType || !data.postData) throw '数据错误';
  1248. const responseData = { err: 0, msg: '', data: {} };
  1249. switch (data.postType) {
  1250. case 'add':
  1251. case 'delete':
  1252. case 'up-move':
  1253. case 'down-move':
  1254. case 'up-level':
  1255. case 'down-level':
  1256. responseData.data = await this._billsBase(ctx.detail, data.postType, data.postData);
  1257. break;
  1258. case 'update':
  1259. responseData.data = await this.ctx.service.paymentSafeBills.updateCalc(ctx.detail, data.postData);
  1260. break;
  1261. case 'add-std':
  1262. responseData.data = await this.ctx.service.paymentSafeBills.addStdNodeWithParent(ctx.detail, data.postData.id, data.postData.stdData);
  1263. break;
  1264. default:
  1265. throw '未知操作';
  1266. }
  1267. ctx.body = responseData;
  1268. } catch (err) {
  1269. this.log(err);
  1270. ctx.body = this.ajaxErrorBody(err, '数据错误');
  1271. }
  1272. }
  1273. }
  1274. return PaymentController;
  1275. };