financial_pay.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const auditConst = require('../const/audit').financial;
  7. const auditType = require('../const/audit').auditType;
  8. const shenpiConst = require('../const/shenpi');
  9. module.exports = app => {
  10. class FinancialPay extends BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'financial_pay';
  20. }
  21. async getOnePay(id) {
  22. const pay = await this.getDataById(id);
  23. const tender = await this.ctx.service.tender.getDataById(pay.tid);
  24. pay.tenderName = tender.name;
  25. return pay;
  26. }
  27. /**
  28. * 获取列表
  29. * @param {int} spid - 项目id
  30. * @param {int} status - 状态
  31. * @param {int} hadlimit - 分页
  32. * @return {object} list - 列表
  33. */
  34. async getListByStatus(spid, status = 0, tid = null, used = null) {
  35. let addSql = '';
  36. if (tid !== null) {
  37. if (tid.length === 0) {
  38. return [];
  39. }
  40. addSql += ' AND a.tid in (' + this.ctx.helper.getInArrStrSqlFilter(tid) + ')';
  41. }
  42. if (used) {
  43. addSql += ' AND a.used = "' + used + '"';
  44. }
  45. let sql = '';
  46. let sqlParam = '';
  47. switch (status) {
  48. case 0: // 所有
  49. sql = 'SELECT a.* FROM ?? As a WHERE a.spid = ?' + addSql;
  50. sqlParam = [this.tableName, spid];
  51. break;
  52. case 1: // 待处理(你的)
  53. sql = 'SELECT a.* FROM ?? as a WHERE a.spid = ?' + addSql + ' AND (a.id in(SELECT b.fpid FROM ?? as b WHERE b.spid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ?)))';
  54. sqlParam = [this.tableName, spid, this.ctx.service.financialPayAudit.tableName, spid, this.ctx.session.sessionUser.accountId, auditConst.status.checking, this.ctx.session.sessionUser.accountId, auditConst.status.uncheck, auditConst.status.checkNo];
  55. break;
  56. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  57. sql =
  58. 'SELECT a.* FROM ?? as a WHERE a.spid = ?' + addSql + ' AND (a.status = ? OR a.status = ?)';
  59. sqlParam = [
  60. this.tableName,
  61. spid,
  62. auditConst.status.uncheck,
  63. auditConst.status.checkNo,
  64. ];
  65. break;
  66. case 2: // 进行中(所有的)
  67. case 4: // 终止(所有的)
  68. sql = 'SELECT a.* FROM ?? as a WHERE ' +
  69. 'a.status = ? AND a.spid = ?' + addSql + ' AND (a.uid = ? OR ' +
  70. 'a.id IN (SELECT b.fpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.fpid))';
  71. sqlParam = [this.tableName, status, spid, this.ctx.session.sessionUser.accountId,
  72. this.ctx.service.financialPayAudit.tableName, this.ctx.session.sessionUser.accountId];
  73. break;
  74. case 3: // 已完成(所有的)
  75. sql = 'SELECT a.* FROM ?? as a WHERE a.status = ? AND a.spid = ?' + addSql;
  76. sqlParam = [this.tableName, status, spid];
  77. break;
  78. default:
  79. break;
  80. }
  81. sql += ' ORDER BY a.create_time DESC';
  82. const list = await this.db.query(sql, sqlParam);
  83. return list;
  84. }
  85. /**
  86. * 获取支付个数
  87. * @param {int} spid - 项目id
  88. * @param {int} status - 状态
  89. * @return {void}
  90. */
  91. async getCountByStatus(spid, status = 0, tid = null, used = null) {
  92. let addSql = '';
  93. if (tid !== null) {
  94. if (tid.length === 0) {
  95. return 0;
  96. }
  97. addSql += ' AND a.tid in (' + this.ctx.helper.getInArrStrSqlFilter(tid) + ')';
  98. }
  99. if (used) {
  100. addSql += ' AND a.used = "' + used + '"';
  101. }
  102. switch (status) {
  103. case 0: // 所有
  104. const sql5 = 'SELECT count(*) AS count FROM ?? As a WHERE a.spid = ?' + addSql;
  105. const sqlParam5 = [this.tableName, spid];
  106. const result5 = await this.db.query(sql5, sqlParam5);
  107. return result5[0].count;
  108. case 1: // 待处理(你的)
  109. const sql6 = 'SELECT count(*) AS count FROM ?? as a WHERE a.spid = ?' + addSql + ' AND (a.id in(SELECT b.fpid FROM ?? as b WHERE b.spid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ?)))';
  110. const sqlParam6 = [this.tableName, spid, this.ctx.service.financialPayAudit.tableName, spid, this.ctx.session.sessionUser.accountId, auditConst.status.checking, this.ctx.session.sessionUser.accountId, auditConst.status.uncheck, auditConst.status.checkNo];
  111. const result6 = await this.db.query(sql6, sqlParam6);
  112. return result6[0].count;
  113. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  114. const sql2 =
  115. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  116. 'a.spid = ?' + addSql + ' AND (a.status = ? OR a.status = ?)';
  117. const sqlParam2 = [
  118. this.tableName,
  119. spid,
  120. auditConst.status.uncheck,
  121. auditConst.status.checkNo,
  122. ];
  123. const result2 = await this.db.query(sql2, sqlParam2);
  124. return result2[0].count;
  125. case 2: // 进行中(所有的)
  126. case 4: // 终止(所有的)
  127. const sql3 =
  128. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  129. 'a.status = ? AND a.spid = ?' + addSql + ' AND (a.uid = ? OR a.id IN (SELECT b.fpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.fpid))';
  130. const sqlParam3 = [this.tableName, status, spid, this.ctx.session.sessionUser.accountId, this.ctx.service.financialPayAudit.tableName, this.ctx.session.sessionUser.accountId];
  131. const result3 = await this.db.query(sql3, sqlParam3);
  132. return result3[0].count;
  133. case 3: // 已完成(所有的)
  134. const sql4 = 'SELECT count(*) AS count FROM ?? as a WHERE a.status = ? AND a.spid = ?' + addSql;
  135. const sqlParam4 = [this.tableName, status, spid];
  136. const result4 = await this.db.query(sql4, sqlParam4);
  137. return result4[0].count;
  138. default:
  139. break;
  140. }
  141. }
  142. async addPay(spid, data) {
  143. if (!data.tid || !data.code || !data.used) {
  144. throw '参数错误';
  145. }
  146. const times = new Date();
  147. const transaction = await this.db.beginTransaction();
  148. try {
  149. const info = await this.ctx.service.financialPay.getDataByCondition({ spid, tid: data.tid, code: data.code });
  150. if (info) {
  151. throw '支付编号已存在';
  152. }
  153. const insertData = {
  154. spid,
  155. tid: data.tid,
  156. code: data.code,
  157. used: data.used,
  158. uid: this.ctx.session.sessionUser.accountId,
  159. status: auditConst.status.uncheck,
  160. times: 1,
  161. create_time: times,
  162. };
  163. const payTender = await this.ctx.service.financialPayTender.getDataByCondition({ spid, tid: data.tid });
  164. if (payTender) {
  165. insertData.entity = payTender.name;
  166. insertData.bank = payTender.bank;
  167. insertData.bank_account = payTender.bank_account;
  168. }
  169. const result = await transaction.insert(this.tableName, insertData);
  170. // 添加审批流程
  171. const auditList = await this.ctx.service.shenpiAudit.getAuditList(data.tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
  172. const insertAuditDatas = [];
  173. for (const audit of auditList) {
  174. insertAuditDatas.push({
  175. spid,
  176. tid: data.tid,
  177. fpid: result.insertId,
  178. aid: audit.audit_id,
  179. order: audit.audit_order,
  180. times: 1,
  181. status: auditConst.status.uncheck,
  182. audit_type: audit.audit_type,
  183. audit_order: audit.audit_order,
  184. });
  185. }
  186. if (insertAuditDatas.length > 0) await transaction.insert(this.ctx.service.financialPayAudit.tableName, insertAuditDatas);
  187. await transaction.commit();
  188. return { id: result.insertId };
  189. } catch (e) {
  190. await transaction.rollback();
  191. throw e;
  192. }
  193. }
  194. async delPay(fpid) {
  195. if (!fpid) {
  196. throw '参数有误';
  197. }
  198. const node = await this.getDataById(fpid);
  199. if (!node) {
  200. throw '资金支付不存在';
  201. }
  202. const transaction = await this.db.beginTransaction();
  203. try {
  204. await transaction.delete(this.tableName, { id: node.id });
  205. // 删除合同附件
  206. const attList = await this.ctx.service.financialPayAtt.getAllDataByCondition({ where: { fpid } });
  207. await this.ctx.helper.delFiles(attList);
  208. await transaction.delete(this.ctx.service.financialPayAtt.tableName, { fpid });
  209. await transaction.delete(this.ctx.service.financialPayAudit.tableName, { fpid });
  210. await transaction.delete(this.ctx.service.financialPayContract.tableName, { fpid });
  211. await transaction.commit();
  212. } catch (err) {
  213. console.log(err);
  214. await transaction.rollback();
  215. throw err;
  216. }
  217. return true;
  218. }
  219. async savePay(fpid, postData) {
  220. // 初始化事务
  221. const transaction = await this.db.beginTransaction();
  222. let result = false;
  223. try {
  224. const updateData = {
  225. id: fpid,
  226. };
  227. updateData[postData.name] = postData.val;
  228. await transaction.update(this.tableName, updateData);
  229. await transaction.commit();
  230. result = true;
  231. } catch (error) {
  232. await transaction.rollback();
  233. result = false;
  234. }
  235. return result;
  236. }
  237. async loadFinancialPayAuditViewData(financialPay) {
  238. const times = financialPay.status === auditConst.status.checkNo ? financialPay.times - 1 : financialPay.times;
  239. if (!financialPay.user) financialPay.user = await this.ctx.service.projectAccount.getAccountInfoById(financialPay.uid);
  240. financialPay.auditHistory = await this.ctx.service.financialPayAudit.getAuditorHistory(financialPay.id, times);
  241. // 获取审批流程中左边列表
  242. if ((financialPay.status === auditConst.status.checkNo || financialPay.status === auditConst.status.revise) && financialPay.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) {
  243. const auditors = await this.ctx.service.financialPayAudit.getAuditors(financialPay.id, times); // 全部参与的审批人
  244. const auditorGroups = this.ctx.helper.groupAuditors(auditors, 'order', true);
  245. financialPay.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  246. financialPay.auditors2.unshift([{
  247. aid: financialPay.user.id, order: 0, times: financialPay.times - 1, audit_order: 0, audit_type: auditType.key.common,
  248. name: financialPay.user.name, role: financialPay.user.role, company: financialPay.user.company,
  249. }]);
  250. } else {
  251. financialPay.auditors2 = financialPay.userGroups;
  252. }
  253. if (financialPay.status === auditConst.status.uncheck || financialPay.status === auditConst.status.checkNo) {
  254. financialPay.auditorList = await this.ctx.service.financialPayAudit.getAuditors(financialPay.id, financialPay.times);
  255. }
  256. }
  257. async loadPayUser(pay) {
  258. const status = auditConst.status;
  259. const accountId = this.ctx.session.sessionUser.accountId;
  260. pay.permission = await this.ctx.service.subProjPermission.getFinancailPermission(this.ctx.subProject.permission.fund_trans_permission, this.ctx.subProject.permission.fund_pay_permission);
  261. pay.user = await this.ctx.service.projectAccount.getAccountInfoById(pay.uid);
  262. pay.auditors = await this.ctx.service.financialPayAudit.getAuditors(pay.id, pay.times); // 全部参与的审批人
  263. pay.auditorIds = this._.map(pay.auditors, 'aid');
  264. pay.curAuditors = pay.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
  265. pay.curAuditorIds = this._.map(pay.curAuditors, 'aid');
  266. pay.flowAuditors = pay.curAuditors.length > 0 ? pay.auditors.filter(x => { return x.order === pay.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  267. pay.flowAuditorIds = this._.map(pay.flowAuditors, 'aid');
  268. pay.nextAuditors = pay.curAuditors.length > 0 ? pay.auditors.filter(x => { return x.order === pay.curAuditors[0].order + 1; }) : [];
  269. pay.nextAuditorIds = this._.map(pay.nextAuditors, 'aid');
  270. pay.auditorGroups = this.ctx.helper.groupAuditors(pay.auditors, 'order', true);
  271. pay.userGroups = this.ctx.helper.groupAuditorsUniq(pay.auditorGroups);
  272. pay.userGroups.unshift([{
  273. aid: pay.user.id, order: 0, times: pay.times, audit_order: 0, audit_type: auditType.key.common,
  274. name: pay.user.name, role: pay.user.role, company: pay.user.company,
  275. }]);
  276. pay.finalAuditorIds = pay.userGroups[pay.userGroups.length - 1].map(x => { return x.aid; });
  277. }
  278. }
  279. return FinancialPay;
  280. };