payment_controller.js 70 KB

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