payment_controller.js 69 KB

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