payment_controller.js 67 KB

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