payment_detail.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. 'use strict';
  2. const auditConst = require('../const/audit').stage;
  3. const paymentConst = require('../const/payment');
  4. module.exports = app => {
  5. class PaymentDetail extends app.BaseService {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.tableName = 'payment_detail';
  9. }
  10. async getValidDetails(tr_id) {
  11. const details = await this.db.select(this.tableName, {
  12. column: ['id', 'in_time', 'tr_id', 'uid', 'status', 'order', 'times', 'code', 's_time'],
  13. where: { tr_id },
  14. orders: [['order', 'desc']],
  15. });
  16. if (details.length !== 0) {
  17. const lastDetail = details[details.length - 1];
  18. if (lastDetail.status === auditConst.status.uncheck && lastDetail.uid !== this.ctx.session.sessionUser.accountId) {
  19. details.splice(details.length - 1, 1);
  20. }
  21. }
  22. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  23. if (details.length > 0 && details[0].status !== auditConst.status.checked) {
  24. const detail = details[0];
  25. const curAuditor = await this.ctx.service.paymentDetailAudit.getCurAuditor(detail.id, detail.times);
  26. const isActive = curAuditor ? curAuditor.id === this.ctx.session.sessionUser.accountId : detail.uid === this.ctx.session.sessionUser.accountId;
  27. if (isActive) {
  28. detail.curTimes = detail.times;
  29. detail.curOrder = curAuditor ? curAuditor.order : 0;
  30. }
  31. }
  32. return details;
  33. }
  34. async hadDetail(trId) {
  35. const result = await this.count({ tr_id: trId });
  36. return result !== 0;
  37. }
  38. async addCommomCheck(trInfo, code) {
  39. const details = await this.getAllDataByCondition({
  40. where: { tr_id: trInfo.id },
  41. order: ['order'],
  42. });
  43. const preDetail = details[details.length - 1];
  44. if (details.length > 0 && details[details.length - 1].status !== auditConst.status.checked) {
  45. throw '上一期未审批通过,请等待上一期审批通过后,再新增';
  46. }
  47. if (this._.findIndex(details, { code }) !== -1) {
  48. throw '编号不能重复';
  49. }
  50. trInfo.rpt_audit = JSON.parse(trInfo.rpt_audit);
  51. if (this._.findIndex(trInfo.rpt_audit, { uid: null }) !== -1) {
  52. throw '未配置好表单角色,无法新建';
  53. }
  54. return preDetail;
  55. }
  56. async addFormDetail(trInfo, code, s_time) {
  57. const transaction = await this.db.beginTransaction();
  58. try {
  59. if (!(trInfo.is_del === 0 && trInfo.rpt_audit)) {
  60. throw '报表已删除或表单人员数据有误,无法新建';
  61. }
  62. const preDetail = await this.addCommomCheck(trInfo, code);
  63. const rptTpl = await this.ctx.service.rptTpl.getDataById(trInfo.rpt_id);
  64. const pageRst = this.ctx.service.jpcReport.getAllPreviewPagesCommon(rptTpl, 'A4');
  65. const newDetail = {
  66. tender_id: this.ctx.paymentTender.id,
  67. tr_id: trInfo.id,
  68. order: preDetail && preDetail.order ? preDetail.order + 1 : 1,
  69. times: 1,
  70. status: auditConst.status.uncheck,
  71. uid: this.ctx.session.sessionUser.accountId,
  72. s_time,
  73. code,
  74. report_json: JSON.stringify(pageRst),
  75. in_time: new Date(),
  76. };
  77. const result = await transaction.insert(this.tableName, newDetail);
  78. if (result.affectedRows === 1) {
  79. newDetail.id = result.insertId;
  80. } else {
  81. throw '新增支付审批数据失败';
  82. }
  83. // 报表角色创建
  84. const insertRptAudit = [];
  85. for (const [i, r] of trInfo.rpt_audit.entries()) {
  86. insertRptAudit.push({
  87. tender_id: this.ctx.paymentTender.id,
  88. tr_id: trInfo.id,
  89. td_id: newDetail.id,
  90. uid: r.uid,
  91. signature_index: i,
  92. signature_name: r.rpt_name,
  93. in_time: new Date(),
  94. });
  95. }
  96. if (insertRptAudit.length > 0) await transaction.insert(this.ctx.service.paymentRptAudit.tableName, insertRptAudit);
  97. // 存在上一期时,复制上一期审批流程
  98. if (preDetail) {
  99. const auditResult = await this.ctx.service.paymentDetailAudit.copyPreDetailAuditors(transaction, preDetail, newDetail);
  100. if (!auditResult) {
  101. throw '复制上一期审批流程失败';
  102. }
  103. }
  104. // 更新is_change值
  105. await transaction.update(this.ctx.service.paymentTenderRpt.tableName, { id: trInfo.id, is_change: 0 });
  106. await transaction.commit();
  107. return newDetail;
  108. } catch (err) {
  109. await transaction.rollback();
  110. throw err;
  111. }
  112. }
  113. async addSafeDetail(trInfo, code, s_time) {
  114. const transaction = await this.db.beginTransaction();
  115. try {
  116. const preDetail = await this.addCommomCheck(trInfo, code);
  117. const newDetail = {
  118. tender_id: this.ctx.paymentTender.id,
  119. tr_id: trInfo.id,
  120. order: preDetail && preDetail.order ? preDetail.order + 1 : 1,
  121. times: 1,
  122. status: auditConst.status.uncheck,
  123. uid: this.ctx.session.sessionUser.accountId,
  124. s_time,
  125. code,
  126. in_time: new Date(),
  127. type: paymentConst.modes_value_object.safe,
  128. };
  129. const result = await transaction.insert(this.tableName, newDetail);
  130. if (result.affectedRows === 1) {
  131. newDetail.id = result.insertId;
  132. } else {
  133. throw '新增支付审批数据失败';
  134. }
  135. // 初始化安全生产费
  136. if (preDetail) {
  137. await this.ctx.service.paymentSafeBills.initByPre(newDetail, preDetail, transaction);
  138. } else {
  139. await this.ctx.service.paymentSafeBills.init(newDetail, transaction);
  140. }
  141. // 存在上一期时,复制上一期审批流程
  142. if (preDetail) {
  143. const auditResult = await this.ctx.service.paymentDetailAudit.copyPreDetailAuditors(transaction, preDetail, newDetail);
  144. if (!auditResult) {
  145. throw '复制上一期审批流程失败';
  146. }
  147. }
  148. // 更新is_change值
  149. await transaction.update(this.ctx.service.paymentTenderRpt.tableName, { id: trInfo.id, is_change: 0 });
  150. await transaction.commit();
  151. return newDetail;
  152. } catch (err) {
  153. await transaction.rollback();
  154. throw err;
  155. }
  156. }
  157. async addDetail(trInfo, code, s_time) {
  158. switch (trInfo.type) {
  159. case paymentConst.modes_value_object.form:
  160. return this.addFormDetail(trInfo, code, s_time);
  161. case paymentConst.modes_value_object.safe:
  162. return this.addSafeDetail(trInfo, code, s_time);
  163. default:
  164. throw '未知类型,新建失败';
  165. }
  166. }
  167. async updateReportJson(id, report_json) {
  168. return await this.db.update(this.tableName, { id, report_json: JSON.stringify(report_json) });
  169. }
  170. async signOneSignatureData(report_json, signature_name, sign_msg) {
  171. // 签名签章
  172. const signCells = this._.find(report_json.items[0].signature_cells, { signature_name });
  173. if (signCells && (sign_msg.sign_path || sign_msg.company_stamp || sign_msg.stamp_path)) {
  174. const signArray = [];
  175. sign_msg.sign_path ? signArray.push('/public/upload/sign/' + sign_msg.sign_path) : signArray.push('');
  176. sign_msg.company_stamp ? signArray.push(sign_msg.company_stamp) : signArray.push('');
  177. sign_msg.stamp_path ? signArray.push(sign_msg.stamp_path) : signArray.push('');
  178. signCells.path = signArray.join('!;!');
  179. }
  180. // 日期
  181. const dateCells = this._.find(report_json.items[0].signature_date_cells, { signature_name: signature_name + '_签字日期' });
  182. if (dateCells && sign_msg.date) {
  183. dateCells.Value = this.ctx.helper.dateTranChinese(sign_msg.date);
  184. }
  185. // 意见
  186. const contentCells = this._.find(report_json.items[0].signature_audit_cells, { signature_name: signature_name + '_审核意见' });
  187. if (contentCells && sign_msg.content) {
  188. contentCells.Value = sign_msg.content;
  189. }
  190. return report_json;
  191. }
  192. async clearOneSignatureData(report_json, rptAudit) {
  193. // 签名签章
  194. const signCells = this._.find(report_json.items[0].signature_cells, { signature_name: rptAudit.signature_name });
  195. if (signCells) {
  196. signCells.path = null;
  197. }
  198. // 日期
  199. const dateCells = this._.find(report_json.items[0].signature_date_cells, { signature_name: rptAudit.signature_name + '_签字日期' });
  200. if (dateCells) {
  201. dateCells.Value = '';
  202. }
  203. // 意见
  204. const contentCells = this._.find(report_json.items[0].signature_audit_cells, { signature_name: rptAudit.signature_name + '_审核意见' });
  205. if (contentCells) {
  206. contentCells.Value = '';
  207. }
  208. return report_json;
  209. }
  210. async clearAllSignatureData(report_json) {
  211. if (report_json.items[0].signature_cells.length > 0) {
  212. for (const cell of report_json.items[0].signature_cells) {
  213. cell.path = null;
  214. }
  215. }
  216. if (report_json.items[0].signature_audit_cells.length > 0) {
  217. for (const cell of report_json.items[0].signature_audit_cells) {
  218. cell.Value = '';
  219. }
  220. }
  221. if (report_json.items[0].signature_date_cells.length > 0) {
  222. for (const cell of report_json.items[0].signature_date_cells) {
  223. cell.Value = '';
  224. }
  225. }
  226. return report_json;
  227. }
  228. /**
  229. * 删除报表表单详情
  230. *
  231. * @param {Number} id - 期Id
  232. * @return {Promise<void>}
  233. */
  234. async deleteDetail(id) {
  235. const transaction = await this.db.beginTransaction();
  236. try {
  237. await transaction.delete(this.ctx.service.paymentDetailAudit.tableName, { td_id: id });
  238. await transaction.delete(this.ctx.service.paymentRptAudit.tableName, { td_id: id });
  239. await transaction.delete(this.ctx.service.paymentSafeBills.tableName, { detail_id: id });
  240. await transaction.delete(this.tableName, { id });
  241. await transaction.commit();
  242. return true;
  243. } catch (err) {
  244. await transaction.rollback();
  245. throw err;
  246. }
  247. }
  248. async haveNotice2Tender(tid, uid) {
  249. const sql = 'SELECT pd.`id`, pd.`tr_id`, pd.`order` FROM ?? as pd LEFT JOIN ?? as pda' +
  250. ' ON pd.`id` = pda.`td_id` LEFT JOIN ?? as pra ON pd.`id` = pra.`td_id` WHERE pd.`tender_id` = ? AND' +
  251. ' ((pd.`uid` = ? AND (pd.`status` = ? OR pd.`status` = ?))' +
  252. ' OR ((pd.`status` = ? OR pd.`status` = ?) AND pda.aid = ? AND pda.`status` = ?)' +
  253. ' OR (pra.`uid` = ? AND pra.`signature_msg` is null AND pd.`status` != ? AND pd.`status` != ?))';
  254. const params = [this.tableName, this.ctx.service.paymentDetailAudit.tableName, this.ctx.service.paymentRptAudit.tableName, tid,
  255. uid, auditConst.status.uncheck, auditConst.status.checkNo,
  256. auditConst.status.checking, auditConst.status.checkNoPre, uid, auditConst.status.checking,
  257. uid, auditConst.status.uncheck, auditConst.status.checkNo];
  258. const result = await this.db.query(sql, params);
  259. if (result && result.length > 0) {
  260. for (const one of this._.uniqBy(result, 'id')) {
  261. const maxOrder = await this.count({
  262. tr_id: one.tr_id,
  263. });
  264. if (one.order === maxOrder) {
  265. return 1;
  266. }
  267. }
  268. }
  269. return 0;
  270. }
  271. async haveNotice2TenderRpt(tr_id, uid) {
  272. const sql = 'SELECT pd.`id`, pd.`tr_id`, pd.`order` FROM ?? as pd LEFT JOIN ?? as pda' +
  273. ' ON pd.`id` = pda.`td_id` LEFT JOIN ?? as pra ON pd.`id` = pra.`td_id` WHERE pd.`tr_id` = ? AND ((pd.`uid` = ? AND (pd.`status` = ? OR pd.`status` = ?))' +
  274. ' OR ((pd.`status` = ? OR pd.`status` = ?) AND pda.aid = ? AND pda.`status` = ?)' +
  275. ' OR (pra.`uid` = ? AND pra.`signature_msg` is null AND pd.`status` != ? AND pd.`status` != ?))';
  276. const params = [this.tableName, this.ctx.service.paymentDetailAudit.tableName, this.ctx.service.paymentRptAudit.tableName, tr_id,
  277. uid, auditConst.status.uncheck, auditConst.status.checkNo,
  278. auditConst.status.checking, auditConst.status.checkNoPre, uid, auditConst.status.checking,
  279. uid, auditConst.status.uncheck, auditConst.status.checkNo];
  280. const result = await this.db.query(sql, params);
  281. if (result && result.length > 0) {
  282. for (const one of this._.uniqBy(result, 'id')) {
  283. const maxOrder = await this.count({
  284. tr_id: one.tr_id,
  285. });
  286. if (one.order === maxOrder) {
  287. return 1;
  288. }
  289. }
  290. }
  291. return 0;
  292. }
  293. async haveDetail2Tender(tid) {
  294. return this.count({ tender_id: tid });
  295. }
  296. async getCountByPidType(pid, type = 1) {
  297. const sql = 'SELECT count(pd.`id`) as count FROM ?? as pd LEFT JOIN ?? as pt ON pd.`tender_id` = pt.`id` WHERE pid = ? AND type = ?';
  298. const params = [this.tableName, this.ctx.service.paymentTender.tableName, pid, type];
  299. const result = await this.db.queryOne(sql, params);
  300. return result ? result.count : 0;
  301. }
  302. async updateReport(transaction, tr_id, uid) {
  303. const details = await this.db.select(this.tableName, {
  304. column: ['id', 'in_time', 'tr_id', 'uid', 'status', 'order', 'times', 'code', 's_time'],
  305. where: { tr_id },
  306. orders: [['order', 'desc']],
  307. });
  308. if (details && details.length > 0) {
  309. const detailInfo = details[0];
  310. if (detailInfo.status === auditConst.status.uncheck || detailInfo.status === auditConst.status.checkNo) {
  311. // 判断该人是否在审批流程里,是则移除并调整order
  312. const result2 = await this.ctx.service.paymentDetailAudit.updateAuditByReport(transaction, detailInfo.id, detailInfo.times, uid);
  313. // 更新为上报人
  314. return await transaction.update(this.tableName, { id: detailInfo.id, uid });
  315. }
  316. }
  317. return true;
  318. }
  319. async doCheckDetail(id) {
  320. const accountId = this.ctx.session.sessionUser.accountId;
  321. const auditPermission = await this.service.paymentPermissionAudit.getOnePermission(this.ctx.session.sessionUser.is_admin, accountId);
  322. if (!auditPermission) throw '权限不足';
  323. const detail = await this.getDataById(id);
  324. if (!detail) throw '支付审批表单不存在';
  325. const trInfo = await this.ctx.service.paymentTenderRpt.getDataById(detail.tr_id);
  326. if (!trInfo) throw '支付审批报表不存在';
  327. // 读取原报、审核人数据
  328. detail.auditors = await this.ctx.service.paymentDetailAudit.getAuditors(detail.id, detail.times);
  329. detail.curAuditor = await this.ctx.service.paymentDetailAudit.getCurAuditor(detail.id, detail.times);
  330. detail.rptAudits = await this.ctx.service.paymentRptAudit.getListByDetail(detail.id);
  331. const auditorIds = _.map(detail.auditors, 'aid'), rptAuditIds = _.map(detail.rptAudits, 'uid');
  332. if (accountId === detail.uid) { // 原报
  333. detail.curTimes = detail.times;
  334. if (detail.status === status.uncheck || detail.status === status.checkNo) {
  335. detail.curOrder = 0;
  336. } else if (detail.status === status.checked) {
  337. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  338. } else {
  339. detail.curOrder = detail.curAuditor.aid === accountId ? detail.curAuditor.order : detail.curAuditor.order - 1;
  340. }
  341. detail.filePermission = true;
  342. } else if (auditorIds.indexOf(accountId) !== -1 || rptAuditIds.indexOf(accountId) !== -1 || auditPermission.view_all) { // 审批人及签署人及查看所有权人
  343. if (detail.status === status.uncheck) {
  344. throw '您无权查看该数据';
  345. }
  346. detail.curTimes = detail.status === status.checkNo ? detail.times - 1 : detail.times;
  347. if (detail.status === status.checked) {
  348. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  349. } else if (detail.status === status.checkNo) {
  350. const audit = await this.ctx.service.paymentDetailAudit.getDataByCondition({
  351. td_id: detail.id, times: detail.times, status: status.checkNo,
  352. });
  353. detail.curOrder = audit.order;
  354. } else {
  355. detail.curOrder = accountId === detail.curAuditor.aid ? detail.curAuditor.order : detail.curAuditor.order - 1;
  356. }
  357. detail.filePermission = auditorIds.indexOf(accountId) !== -1 || rptAuditIds.indexOf(accountId) !== -1;
  358. } else { // 其他不可见
  359. throw '您无权查看该数据';
  360. }
  361. // 获取最新的期
  362. detail.highOrder = await this.service.paymentDetail.count({ tr_id: detail.tr_id });
  363. detail.readOnly = !((detail.status === status.uncheck || detail.status === status.checkNo) && accountId === detail.uid);
  364. if (detail.readOnly && detail.type === paymentConst.modes_value_object.safe) {
  365. if ((detail.status === status.checking || detail.status === status.checkNoPre) && detail.curAuditor && detail.curAuditor.aid === accountId) {
  366. detail.readOnly = false;
  367. }
  368. }
  369. return detail;
  370. }
  371. }
  372. return PaymentDetail;
  373. };