payment_controller.js 70 KB

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