payment_detail.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. 'use strict';
  2. const auditConst = require('../const/audit').stage;
  3. module.exports = app => {
  4. class PaymentDetail extends app.BaseService {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.tableName = 'payment_detail';
  8. }
  9. async getValidDetails(tr_id) {
  10. const details = await this.db.select(this.tableName, {
  11. column: ['id', 'in_time', 'tr_id', 'uid', 'status', 'order', 'times', 'code', 's_time'],
  12. where: { tr_id },
  13. orders: [['order', 'desc']],
  14. });
  15. if (details.length !== 0) {
  16. const lastDetail = details[details.length - 1];
  17. if (lastDetail.status === auditConst.status.uncheck && lastDetail.uid !== this.ctx.session.sessionUser.accountId) {
  18. details.splice(details.length - 1, 1);
  19. }
  20. }
  21. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  22. if (details.length > 0 && details[0].status !== auditConst.status.checked) {
  23. const detail = details[0];
  24. const curAuditor = await this.ctx.service.paymentDetailAudit.getCurAuditor(detail.id, detail.times);
  25. const isActive = curAuditor ? curAuditor.id === this.ctx.session.sessionUser.accountId : detail.uid === this.ctx.session.sessionUser.accountId;
  26. if (isActive) {
  27. detail.curTimes = detail.times;
  28. detail.curOrder = curAuditor ? curAuditor.order : 0;
  29. }
  30. }
  31. return details;
  32. }
  33. async hadDetail(trId) {
  34. const result = await this.count({ tr_id: trId });
  35. return result !== 0;
  36. }
  37. async addDetail(trInfo, code, s_time) {
  38. const transaction = await this.db.beginTransaction();
  39. try {
  40. if (!(trInfo.is_del === 0 && trInfo.rpt_audit)) {
  41. throw '报表已删除或表单人员数据有误,无法新建';
  42. }
  43. const details = await this.getAllDataByCondition({
  44. where: { tr_id: trInfo.id },
  45. order: ['order'],
  46. });
  47. const preDetail = details[details.length - 1];
  48. if (details.length > 0 && details[details.length - 1].status !== auditConst.status.checked) {
  49. throw '上一期未审批通过,请等待上一期审批通过后,再新增';
  50. }
  51. trInfo.rpt_audit = JSON.parse(trInfo.rpt_audit);
  52. if (this._.findIndex(trInfo.rpt_audit, { uid: null }) !== -1) {
  53. throw '未配置好表单角色,无法新建';
  54. }
  55. const rptTpl = await this.ctx.service.rptTpl.getDataById(trInfo.rpt_id);
  56. const pageRst = this.ctx.service.jpcReport.getAllPreviewPagesCommon(rptTpl, 'A4');
  57. const order = details.length + 1;
  58. const newDetail = {
  59. tender_id: this.ctx.tender.id,
  60. tr_id: trInfo.id,
  61. order,
  62. times: 1,
  63. status: auditConst.status.uncheck,
  64. uid: this.ctx.session.sessionUser.accountId,
  65. s_time,
  66. code,
  67. report_json: JSON.stringify(pageRst),
  68. in_time: new Date(),
  69. };
  70. const result = await transaction.insert(this.tableName, newDetail);
  71. if (result.affectedRows === 1) {
  72. newDetail.id = result.insertId;
  73. } else {
  74. throw '新增支付审批数据失败';
  75. }
  76. // 报表角色创建
  77. const insertRptAudit = [];
  78. for (const [i, r] of trInfo.rpt_audit.entries()) {
  79. insertRptAudit.push({
  80. tender_id: this.ctx.tender.id,
  81. tr_id: trInfo.id,
  82. td_id: newDetail.id,
  83. uid: r.uid,
  84. signature_index: i,
  85. signature_name: r.rpt_name,
  86. in_time: new Date(),
  87. });
  88. }
  89. if (insertRptAudit.length > 0) await transaction.insert(this.ctx.service.paymentRptAudit.tableName, insertRptAudit);
  90. // 存在上一期时,复制上一期审批流程
  91. if (preDetail) {
  92. const auditResult = await this.ctx.service.paymentDetailAudit.copyPreDetailAuditors(transaction, preDetail, newDetail);
  93. if (!auditResult) {
  94. throw '复制上一期审批流程失败';
  95. }
  96. }
  97. // 更新is_change值
  98. await transaction.update(this.ctx.service.paymentTenderRpt.tableName, { id: trInfo.id, is_change: 0 });
  99. await transaction.commit();
  100. return newDetail;
  101. } catch (err) {
  102. await transaction.rollback();
  103. throw err;
  104. }
  105. }
  106. async updateReportJson(id, report_json) {
  107. return await this.db.update(this.tableName, { id, report_json: JSON.stringify(report_json) });
  108. }
  109. async signOneSignatureData(report_json, signature_name, sign_msg) {
  110. // 签名签章
  111. const signCells = this._.find(report_json.items[0].signature_cells, { signature_name });
  112. if (signCells && (sign_msg.sign_path || sign_msg.company_stamp || sign_msg.stamp_path)) {
  113. const signArray = [];
  114. if (sign_msg.sign_path) signArray.push('/public/upload/sign/' + sign_msg.sign_path);
  115. if (sign_msg.company_stamp) signArray.push(sign_msg.company_stamp);
  116. if (sign_msg.stamp_path) signArray.push(sign_msg.stamp_path);
  117. signCells.path = signArray.join('!;!');
  118. }
  119. // 日期
  120. const dateCells = this._.find(report_json.items[0].signature_date_cells, { signature_name: signature_name + '_签字日期' });
  121. if (dateCells && sign_msg.date) {
  122. dateCells.Value = sign_msg.date;
  123. }
  124. // 意见
  125. const contentCells = this._.find(report_json.items[0].signature_audit_cells, { signature_name: signature_name + '_审核意见' });
  126. if (contentCells && sign_msg.content) {
  127. contentCells.Value = sign_msg.content;
  128. }
  129. return report_json;
  130. }
  131. async clearOneSignatureData(report_json, rptAudit) {
  132. // 签名签章
  133. const signCells = this._.find(report_json.items[0].signature_cells, { signature_name: rptAudit.signature_name });
  134. if (signCells) {
  135. signCells.path = null;
  136. }
  137. // 日期
  138. const dateCells = this._.find(report_json.items[0].signature_date_cells, { signature_name: rptAudit.signature_name + '_签字日期' });
  139. if (dateCells) {
  140. dateCells.Value = '';
  141. }
  142. // 意见
  143. const contentCells = this._.find(report_json.items[0].signature_audit_cells, { signature_name: rptAudit.signature_name + '_审核意见' });
  144. if (contentCells) {
  145. contentCells.Value = '';
  146. }
  147. return report_json;
  148. }
  149. async clearAllSignatureData(report_json) {
  150. if (report_json.items[0].signature_cells.length > 0) {
  151. for (const cell of report_json.items[0].signature_cells) {
  152. cell.path = null;
  153. }
  154. }
  155. if (report_json.items[0].signature_audit_cells.length > 0) {
  156. for (const cell of report_json.items[0].signature_audit_cells) {
  157. cell.Value = '';
  158. }
  159. }
  160. if (report_json.items[0].signature_date_cells.length > 0) {
  161. for (const cell of report_json.items[0].signature_date_cells) {
  162. cell.Value = '';
  163. }
  164. }
  165. return report_json;
  166. }
  167. /**
  168. * 删除报表表单详情
  169. *
  170. * @param {Number} id - 期Id
  171. * @return {Promise<void>}
  172. */
  173. async deleteDetail(id) {
  174. const transaction = await this.db.beginTransaction();
  175. try {
  176. await transaction.delete(this.ctx.service.paymentDetailAudit.tableName, { td_id: id });
  177. await transaction.delete(this.ctx.service.paymentRptAudit.tableName, { td_id: id });
  178. await transaction.delete(this.tableName, { id });
  179. await transaction.commit();
  180. return true;
  181. } catch (err) {
  182. await transaction.rollback();
  183. throw err;
  184. }
  185. }
  186. async haveNotice2Tender(tid, uid) {
  187. const sql = 'SELECT count(pd.`id`) as count FROM ?? as pd LEFT JOIN ?? as pda' +
  188. ' ON pd.`id` = pda.`td_id` WHERE pd.`tender_id` = ? AND (pd.`uid` = ? AND (pd.`status` = ? OR pd.`status` = ?))' +
  189. ' OR ((pd.`status` = ? OR pd.`status` = ?) AND pda.aid = ? AND pda.`status` = ?)';
  190. const params = [this.tableName, this.ctx.service.paymentDetailAudit.tableName, tid,
  191. uid, auditConst.status.uncheck, auditConst.status.checkNo,
  192. auditConst.status.checking, auditConst.status.checkNoPre, uid, auditConst.status.checking];
  193. const result = await this.db.queryOne(sql, params);
  194. return result ? result.count : 0;
  195. }
  196. async haveNotice2TenderRpt(tr_id, uid) {
  197. const sql = 'SELECT count(pd.`id`) as count FROM ?? as pd LEFT JOIN ?? as pda' +
  198. ' ON pd.`id` = pda.`td_id` WHERE pd.`tr_id` = ? AND ((pd.`uid` = ? AND (pd.`status` = ? OR pd.`status` = ?))' +
  199. ' OR ((pd.`status` = ? OR pd.`status` = ?) AND pda.aid = ? AND pda.`status` = ?))';
  200. const params = [this.tableName, this.ctx.service.paymentDetailAudit.tableName, tr_id,
  201. uid, auditConst.status.uncheck, auditConst.status.checkNo,
  202. auditConst.status.checking, auditConst.status.checkNoPre, uid, auditConst.status.checking];
  203. const result = await this.db.queryOne(sql, params);
  204. return result ? result.count : 0;
  205. }
  206. async haveDetail2Tender(tid) {
  207. return this.count({ tender_id: tid });
  208. }
  209. }
  210. return PaymentDetail;
  211. };