payment_controller.js 67 KB

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