payment_detail.js 20 KB

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