payment_controller.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  794. const tenderInfo = await this.ctx.service.paymentTenderInfo.getTenderInfo(ctx.paymentTender.id);
  795. const renderData = {
  796. tender: ctx.paymentTender,
  797. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.list),
  798. tenderRptList,
  799. trInfo,
  800. trDetailList,
  801. rptMsg: null,
  802. auditConst,
  803. accountGroup: [],
  804. accountList: [],
  805. paymentConst,
  806. preUrl: '/payment/' + ctx.paymentTender.id + '/list/' + trInfo.id,
  807. categoryData,
  808. tenderInfo,
  809. };
  810. // 获取报表信息,新增时及设置报表角色时使用
  811. if (trInfo.uid === ctx.session.sessionUser.accountId && trInfo.is_del === 0 && trInfo.rpt_id) {
  812. const rptTpl = await ctx.service.rptTpl.getDataById(trInfo.rpt_id);
  813. if (rptTpl) {
  814. // rptTpl.source_type = 101;
  815. const pageRst = await ctx.service.jpcReport.getAllPreviewPagesCommon(rptTpl, 'A4');
  816. // const pageRst = await ctx.service.jpcReport.getPreviewPagesWithDiscreteDataCommon(ctx, rptTpl, 'A4', this.app.baseDir, null);
  817. renderData.rptMsg = pageRst.items[0];
  818. // 判断与原有的报表审批人列表是否有区别
  819. let difference = false;
  820. let auditDifference = false;
  821. let needChange = false;
  822. if (trInfo.report_items_json) {
  823. const report_items_json = JSON.parse(trInfo.report_items_json);
  824. const items = ['cells', 'signature_audit_cells', 'signature_cells', 'signature_date_cells', 'interact_cells'];
  825. for (const item of items) {
  826. if (report_items_json[item] &&
  827. !ctx.helper._.isEmpty(ctx.helper._.differenceWith(JSON.parse(JSON.stringify(renderData.rptMsg[item])), report_items_json[item], ctx.helper._.isEqual))) {
  828. // 因为interact_cells里存在undefind值,必须先用JSON.parse和JSON.stringify转义才能对比
  829. difference = true;
  830. needChange = true;
  831. if (item === 'signature_cells') {
  832. auditDifference = true;
  833. }
  834. // break;
  835. }
  836. }
  837. } else {
  838. difference = true;
  839. }
  840. if (difference) {
  841. // 删除rpt_audit重新配置
  842. const updateData = {
  843. id: trInfo.id,
  844. report_items_json: JSON.stringify(ctx.helper._.cloneDeep(renderData.rptMsg)),
  845. };
  846. // 删除rpt_audit重新配置
  847. if (auditDifference) {
  848. updateData.rpt_audit = null;
  849. trInfo.rpt_audit = null;
  850. }
  851. if (needChange) {
  852. updateData.is_change = 1;
  853. trInfo.is_change = 1;
  854. }
  855. await ctx.service.paymentTenderRpt.defaultUpdate(updateData);
  856. trInfo.report_items_json = renderData.rptMsg;
  857. }
  858. if (trInfo.rpt_audit) {
  859. trInfo.rpt_audit = JSON.parse(trInfo.rpt_audit);
  860. const prjAccList = await ctx.service.projectAccount.getAllAccountByProjectId(ctx.session.sessionProject.id);
  861. for (const audit of trInfo.rpt_audit) {
  862. if (audit.uid) {
  863. const info = ctx.helper._.find(prjAccList, { id: audit.uid });
  864. audit.name = info.name;
  865. }
  866. }
  867. } else {
  868. trInfo.rpt_audit = [];
  869. for (const sc of renderData.rptMsg.signature_cells) {
  870. trInfo.rpt_audit.push({
  871. uid: null,
  872. rpt_name: sc.signature_name,
  873. });
  874. }
  875. }
  876. renderData.trInfo = trInfo;
  877. }
  878. if (renderData.rptMsg) {
  879. // 获取所有项目参与者
  880. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  881. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  882. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  883. });
  884. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  885. const accountGroupList = unitList.map(item => {
  886. const groupList = accountList.filter(item1 => item1.company === item.name);
  887. return { groupName: item.name, groupList };
  888. });
  889. renderData.accountList = accountList;
  890. renderData.accountGroup = accountGroupList;
  891. }
  892. }
  893. await this.layout('payment/list.ejs', renderData, 'payment/list_modal.ejs');
  894. } catch (err) {
  895. console.log(err);
  896. this.log(err);
  897. ctx.session.postError = err.toString();
  898. ctx.redirect(this.request && this.request.headers && this.request.headers.referer ? this.request.headers.referer : '/payment');
  899. }
  900. }
  901. async rptSave(ctx) {
  902. try {
  903. const tr_id = ctx.params.trid;
  904. if (!tr_id) {
  905. throw '参数有误';
  906. }
  907. const trInfo = await ctx.service.paymentTenderRpt.getDataById(tr_id);
  908. if (!trInfo) {
  909. throw '标段报表不存在';
  910. }
  911. if (ctx.session.sessionUser.accountId !== trInfo.uid) {
  912. throw '权限不足';
  913. }
  914. const responseData = {
  915. err: 0, msg: '', data: {},
  916. };
  917. const data = JSON.parse(ctx.request.body.data);
  918. if (!data.type) {
  919. throw '提交数据错误';
  920. }
  921. switch (data.type) {
  922. case 'rpt_audit':
  923. responseData.data = await ctx.service.paymentTenderRpt.updateRptAudit(trInfo, data.rpt_audit);
  924. break;
  925. case 'add-detail':
  926. responseData.data = await ctx.service.paymentDetail.addDetail(trInfo, data.code, data.s_time);
  927. break;
  928. default: throw '参数有误';
  929. }
  930. ctx.body = responseData;
  931. } catch (err) {
  932. this.log(err);
  933. ctx.body = { err: 1, msg: err.toString(), data: null };
  934. }
  935. }
  936. /**
  937. * 上报和重新上报
  938. * @param ctx
  939. * @return {Promise<void>}
  940. */
  941. async startAudit(ctx) {
  942. try {
  943. if (ctx.detail.uid !== ctx.session.sessionUser.accountId) {
  944. throw '您无权上报报表表单详情数据';
  945. }
  946. if (ctx.detail.status === auditConst.status.checking || ctx.detail.status === auditConst.status.checked) {
  947. throw '该报表表单详情数据当前无法上报';
  948. }
  949. await ctx.service.paymentDetailAudit.start(ctx.detail.id, ctx.detail.times);
  950. ctx.redirect(ctx.request.header.referer);
  951. } catch (err) {
  952. this.log(err);
  953. ctx.session.postError = err.toString();
  954. ctx.redirect(ctx.request.header.referer);
  955. }
  956. }
  957. /**
  958. * 审批
  959. * @param ctx
  960. * @return {Promise<void>}
  961. */
  962. async checkAudit(ctx) {
  963. try {
  964. if (!ctx.detail || ctx.detail.status !== auditConst.status.checking) {
  965. throw '当前报表表单详情数据有误';
  966. }
  967. if (!ctx.detail.curAuditor || ctx.detail.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  968. throw '您无权进行该操作';
  969. }
  970. const data = {
  971. checkType: parseInt(ctx.request.body.checkType),
  972. opinion: ctx.request.body.opinion,
  973. };
  974. if (!data.checkType || isNaN(data.checkType)) {
  975. throw '提交数据错误';
  976. }
  977. if (data.checkType === auditConst.status.checkNo) {
  978. if (!data.checkType || isNaN(data.checkType)) {
  979. throw '提交数据错误';
  980. }
  981. }
  982. await ctx.service.paymentDetailAudit.check(ctx.detail.id, data, ctx.detail.times);
  983. ctx.redirect(ctx.request.header.referer);
  984. } catch (err) {
  985. this.log(err);
  986. ctx.session.postError = err.toString();
  987. ctx.redirect(ctx.request.header.referer);
  988. }
  989. }
  990. /**
  991. * 上传附件
  992. * @param {Object} ctx - egg全局变量
  993. * @return {void}
  994. */
  995. async uploadDetailFile(ctx) {
  996. const responseData = {
  997. err: 0,
  998. msg: '',
  999. data: [],
  1000. };
  1001. let stream;
  1002. try {
  1003. const parts = ctx.multipart({ autoFields: true });
  1004. const files = [];
  1005. let index = 0;
  1006. const extra_upload = ctx.detail.status === auditConst.status.checked;
  1007. const original_data = {
  1008. tender_id: ctx.paymentTender.id,
  1009. tr_id: ctx.trInfo.id,
  1010. td_id: ctx.detail.id,
  1011. };
  1012. while ((stream = await parts()) !== undefined) {
  1013. // 判断用户是否选择上传文件
  1014. if (!stream.filename) {
  1015. throw '请选择上传的文件!';
  1016. }
  1017. const fileInfo = path.parse(stream.filename);
  1018. const create_time = Date.parse(new Date()) / 1000;
  1019. const filepath = `app/public/upload/payment/${original_data.tender_id}/detail/fujian_${create_time + index.toString() + fileInfo.ext}`;
  1020. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  1021. await sendToWormhole(stream);
  1022. // 保存数据到att表
  1023. const fileData = {
  1024. upload_time: new Date(),
  1025. filename: fileInfo.name,
  1026. fileext: fileInfo.ext,
  1027. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  1028. filepath,
  1029. extra_upload,
  1030. };
  1031. if (parts.field.safe_id) fileData.safe_id = parts.field.safe_id;
  1032. const result = await ctx.service.paymentDetailAtt.save(original_data, fileData, ctx.session.sessionUser.accountId);
  1033. if (!result) {
  1034. throw '导入数据库保存失败';
  1035. }
  1036. fileData.uid = ctx.session.sessionUser.accountId;
  1037. fileData.u_name = ctx.session.sessionUser.name;
  1038. fileData.id = result.insertId;
  1039. fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
  1040. delete fileData.filepath;
  1041. if (!ctx.helper.canPreview(fileData.fileext)) {
  1042. fileData.filepath = `/payment/${original_data.tender_id}/detail/${original_data.td_id}/file/${fileData.id}/download`;
  1043. } else {
  1044. fileData.filepath = ctx.app.config.fujianOssPath + filepath;
  1045. fileData.viewpath = ctx.app.config.fujianOssPath + filepath;
  1046. }
  1047. files.push(fileData);
  1048. ++index;
  1049. }
  1050. responseData.data = files;
  1051. } catch (err) {
  1052. this.log(err);
  1053. // 失败需要消耗掉stream 以防卡死
  1054. if (stream) {
  1055. await sendToWormhole(stream);
  1056. }
  1057. this.setMessage(err.toString(), this.messageType.ERROR);
  1058. }
  1059. ctx.body = responseData;
  1060. }
  1061. /**
  1062. * 下载附件
  1063. * @param {Object} ctx - egg全局变量
  1064. * @return {void}
  1065. */
  1066. async downloadDetailFile(ctx) {
  1067. const id = ctx.params.fid;
  1068. if (id) {
  1069. try {
  1070. const fileInfo = await ctx.service.paymentDetailAtt.getDataById(id);
  1071. if (fileInfo !== undefined && fileInfo !== '') {
  1072. // 解决中文无法下载问题
  1073. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1074. let disposition = '';
  1075. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1076. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  1077. } else if (userAgent.indexOf('firefox') >= 0) {
  1078. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  1079. } else {
  1080. /* safari等其他非主流浏览器只能自求多福了 */
  1081. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  1082. }
  1083. ctx.response.set({
  1084. 'Content-Type': 'application/octet-stream',
  1085. 'Content-Disposition': disposition,
  1086. 'Content-Length': fileInfo.filesize,
  1087. });
  1088. // ctx.body = await fs.createReadStream(fileName);
  1089. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  1090. } else {
  1091. throw '不存在该文件';
  1092. }
  1093. } catch (err) {
  1094. this.log(err);
  1095. this.setMessage(err.toString(), this.messageType.ERROR);
  1096. }
  1097. }
  1098. }
  1099. /**
  1100. * 删除附件
  1101. * @param {Object} ctx - egg全局变量
  1102. * @return {void}
  1103. */
  1104. async deleteDetailFile(ctx) {
  1105. const responseData = {
  1106. err: 0,
  1107. msg: '',
  1108. data: '',
  1109. };
  1110. try {
  1111. const data = JSON.parse(ctx.request.body.data);
  1112. const fileInfo = await ctx.service.paymentDetailAtt.getDataById(data.id);
  1113. if (!fileInfo || !Object.keys(fileInfo).length) {
  1114. throw '该文件不存在';
  1115. }
  1116. if (!fileInfo.extra_upload && ctx.detail.status === auditConst.status.checked) {
  1117. throw '无权限删除';
  1118. }
  1119. if (fileInfo !== undefined && fileInfo !== '') {
  1120. // 先删除文件
  1121. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  1122. // 再删除数据库
  1123. await ctx.service.paymentDetailAtt.deleteById(data.id);
  1124. responseData.data = '';
  1125. } else {
  1126. throw '不存在该文件';
  1127. }
  1128. } catch (err) {
  1129. responseData.err = 1;
  1130. responseData.msg = err;
  1131. }
  1132. ctx.body = responseData;
  1133. }
  1134. async safeBills(ctx) {
  1135. try {
  1136. await this._getDetailAuditViewData(ctx);
  1137. const auditIdList = ctx.helper._.map(ctx.detail.auditors, 'aid');
  1138. const rptAuditIdList = ctx.helper._.map(ctx.detail.rptAudits, 'uid');
  1139. const uidList = ctx.helper._.uniq([...auditIdList, ...rptAuditIdList]);
  1140. // 获取附件列表
  1141. const attList = await ctx.service.paymentDetailAtt.getPaymentDetailAttachment(ctx.detail.id, 'desc');
  1142. const stdBills = await ctx.service.stdGclList.getSafeGcl();
  1143. const renderData = {
  1144. trInfo: ctx.trInfo,
  1145. paymentConst,
  1146. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.safe),
  1147. auditConst,
  1148. shenpiConst,
  1149. attList,
  1150. moment,
  1151. whiteList: ctx.app.config.multipart.whitelist,
  1152. uidList,
  1153. preUrl: '/payment/' + ctx.paymentTender.id + '/detail/' + ctx.detail.id,
  1154. OSS_PATH: ctx.app.config.fujianOssPath,
  1155. stdBills,
  1156. };
  1157. renderData.nextDetail = await ctx.service.paymentDetail.getDataByCondition({ tr_id: ctx.trInfo.id, order: ctx.detail.order + 1 });
  1158. const content = [];
  1159. renderData.content = content;
  1160. if ((ctx.detail.status === auditConst.status.uncheck || ctx.detail.status === auditConst.status.checkNo) && ctx.session.sessionUser.accountId === ctx.detail.uid) {
  1161. // 获取所有项目参与者
  1162. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  1163. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  1164. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  1165. });
  1166. renderData.accountList = accountList;
  1167. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  1168. const accountGroupList = unitList.map(item => {
  1169. const groupList = accountList.filter(item1 => item1.company === item.name);
  1170. return { groupName: item.name, groupList };
  1171. });
  1172. renderData.accountGroup = accountGroupList;
  1173. }
  1174. await this.layout('payment_safe/index.ejs', renderData, 'payment_safe/modal.ejs');
  1175. } catch (err) {
  1176. this.log(err);
  1177. this.ctx.postError(err, '读取安全生产费错误');
  1178. if (ctx.detail.tender_id && ctx.detail.tr_id) {
  1179. ctx.redirect('/payment' + ctx.detail.tender_id + '/list');
  1180. } else {
  1181. ctx.redirect(this.menu.menu.dashboard.url);
  1182. }
  1183. }
  1184. }
  1185. async safeCompare(ctx) {
  1186. try {
  1187. const renderData = {
  1188. trInfo: ctx.trInfo,
  1189. paymentConst,
  1190. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.compare),
  1191. auditConst,
  1192. };
  1193. await this.layout('payment_safe/compare.ejs', renderData);
  1194. } catch (err) {
  1195. this.log(err);
  1196. this.ctx.postError(err, '读取安全生产费错误');
  1197. if (ctx.detail.tender_id && ctx.detail.tr_id) {
  1198. ctx.redirect('/payment' + ctx.detail.tender_id + '/list');
  1199. } else {
  1200. ctx.redirect(this.menu.menu.dashboard.url);
  1201. }
  1202. }
  1203. }
  1204. async safeLoad(ctx) {
  1205. try {
  1206. const data = JSON.parse(ctx.request.body.data);
  1207. const filter = data.filter.split(';');
  1208. const responseData = { err: 0, msg: '', data: {}, hpack: [] };
  1209. for (const f of filter) {
  1210. switch (f) {
  1211. case 'bills':
  1212. responseData.data.bills = ctx.detail.readOnly
  1213. ? await ctx.service.paymentSafeBills.getReadData(ctx.detail)
  1214. : await ctx.service.paymentSafeBills.getEditData(ctx.detail);
  1215. break;
  1216. case 'billsCompare':
  1217. responseData.data[f] = await ctx.service.paymentSafeBills.getCompareData(ctx.detail);
  1218. break;
  1219. case 'auditFlow':
  1220. responseData.data[f] = await ctx.service.paymentDetailAudit.getViewFlow(ctx.detail);
  1221. break;
  1222. case 'att':
  1223. responseData.data[f] = await ctx.service.paymentDetailAtt.getPaymentDetailAttachment(ctx.detail.id, 'DESC');
  1224. break;
  1225. default:
  1226. responseData.data[f] = [];
  1227. break;
  1228. }
  1229. }
  1230. ctx.body = responseData;
  1231. } catch (err) {
  1232. this.log(err);
  1233. ctx.body = { err: 1, msg: err.toString(), data: null };
  1234. }
  1235. }
  1236. async _billsBase(detail, type, data) {
  1237. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  1238. if (type !== 'add') {
  1239. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  1240. }
  1241. switch (type) {
  1242. case 'add':
  1243. return await this.ctx.service.paymentSafeBills.addSafeBillsNode(detail, data.id, data.count);
  1244. case 'delete':
  1245. return await this.ctx.service.paymentSafeBills.delete(detail.id, data.id, data.count);
  1246. case 'up-move':
  1247. return await this.ctx.service.paymentSafeBills.upMoveNode(detail.id, data.id, data.count);
  1248. case 'down-move':
  1249. return await this.ctx.service.paymentSafeBills.downMoveNode(detail.id, data.id, data.count);
  1250. case 'up-level':
  1251. return await this.ctx.service.paymentSafeBills.upLevelNode(detail.id, data.id, data.count);
  1252. case 'down-level':
  1253. return await this.ctx.service.paymentSafeBills.downLevelNode(detail.id, data.id, data.count);
  1254. }
  1255. }
  1256. async safeUpdate(ctx) {
  1257. try {
  1258. const data = JSON.parse(ctx.request.body.data);
  1259. if (!data.postType || !data.postData) throw '数据错误';
  1260. const responseData = { err: 0, msg: '', data: {} };
  1261. switch (data.postType) {
  1262. case 'add':
  1263. case 'delete':
  1264. case 'up-move':
  1265. case 'down-move':
  1266. case 'up-level':
  1267. case 'down-level':
  1268. responseData.data = await this._billsBase(ctx.detail, data.postType, data.postData);
  1269. break;
  1270. case 'update':
  1271. responseData.data = await this.ctx.service.paymentSafeBills.updateCalc(ctx.detail, data.postData);
  1272. break;
  1273. case 'add-std':
  1274. responseData.data = await this.ctx.service.paymentSafeBills.addStdNodeWithParent(ctx.detail, data.postData.id, data.postData.stdData);
  1275. break;
  1276. default:
  1277. throw '未知操作';
  1278. }
  1279. ctx.body = responseData;
  1280. } catch (err) {
  1281. this.log(err);
  1282. ctx.body = this.ajaxErrorBody(err, '数据错误');
  1283. }
  1284. }
  1285. async safeDecimal(ctx) {
  1286. try {
  1287. const data = JSON.parse(ctx.request.body.data);
  1288. const result = await this.ctx.service.paymentSafeBills.setDecimal(data.decimal);
  1289. ctx.body = { err: 0, msg: '', data: result };
  1290. } catch (err) {
  1291. this.log(err);
  1292. ctx.body = this.ajaxErrorBody(err, '设置小数位数错误');
  1293. }
  1294. }
  1295. }
  1296. return PaymentController;
  1297. };