financial_pay.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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, pageLimit = 0) {
  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. if (pageLimit) {
  83. const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
  84. const offset = limit * (this.ctx.page - 1);
  85. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  86. sql += ' LIMIT ' + limitString;
  87. }
  88. const list = await this.db.query(sql, sqlParam);
  89. return list;
  90. }
  91. /**
  92. * 获取支付个数
  93. * @param {int} spid - 项目id
  94. * @param {int} status - 状态
  95. * @return {void}
  96. */
  97. async getCountByStatus(spid, status = 0, tid = null, used = null) {
  98. let addSql = '';
  99. if (tid !== null) {
  100. if (tid.length === 0) {
  101. return 0;
  102. }
  103. addSql += ' AND a.tid in (' + this.ctx.helper.getInArrStrSqlFilter(tid) + ')';
  104. }
  105. if (used) {
  106. addSql += ' AND a.used = "' + used + '"';
  107. }
  108. switch (status) {
  109. case 0: // 所有
  110. const sql5 = 'SELECT count(*) AS count FROM ?? As a WHERE a.spid = ?' + addSql;
  111. const sqlParam5 = [this.tableName, spid];
  112. const result5 = await this.db.query(sql5, sqlParam5);
  113. return result5[0].count;
  114. case 1: // 待处理(你的)
  115. 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 = ?)))';
  116. 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];
  117. const result6 = await this.db.query(sql6, sqlParam6);
  118. return result6[0].count;
  119. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  120. const sql2 =
  121. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  122. 'a.spid = ?' + addSql + ' AND (a.status = ? OR a.status = ?)';
  123. const sqlParam2 = [
  124. this.tableName,
  125. spid,
  126. auditConst.status.uncheck,
  127. auditConst.status.checkNo,
  128. ];
  129. const result2 = await this.db.query(sql2, sqlParam2);
  130. return result2[0].count;
  131. case 2: // 进行中(所有的)
  132. case 4: // 终止(所有的)
  133. const sql3 =
  134. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  135. '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))';
  136. const sqlParam3 = [this.tableName, status, spid, this.ctx.session.sessionUser.accountId, this.ctx.service.financialPayAudit.tableName, this.ctx.session.sessionUser.accountId];
  137. const result3 = await this.db.query(sql3, sqlParam3);
  138. return result3[0].count;
  139. case 3: // 已完成(所有的)
  140. const sql4 = 'SELECT count(*) AS count FROM ?? as a WHERE a.status = ? AND a.spid = ?' + addSql;
  141. const sqlParam4 = [this.tableName, status, spid];
  142. const result4 = await this.db.query(sql4, sqlParam4);
  143. return result4[0].count;
  144. default:
  145. break;
  146. }
  147. }
  148. async addPay(spid, data) {
  149. if (!data.tid || !data.code || !data.used) {
  150. throw '参数错误';
  151. }
  152. const times = new Date();
  153. const transaction = await this.db.beginTransaction();
  154. try {
  155. const info = await this.ctx.service.financialPay.getDataByCondition({ spid, tid: data.tid, code: data.code });
  156. if (info) {
  157. throw '支付编号已存在';
  158. }
  159. const insertData = {
  160. spid,
  161. tid: data.tid,
  162. code: data.code,
  163. used: data.used,
  164. uid: this.ctx.session.sessionUser.accountId,
  165. status: auditConst.status.uncheck,
  166. times: 1,
  167. create_time: times,
  168. };
  169. const payTender = await this.ctx.service.financialPayTender.getDataByCondition({ spid, tid: data.tid });
  170. if (payTender) {
  171. insertData.entity = payTender.name;
  172. insertData.bank = payTender.bank;
  173. insertData.bank_account = payTender.bank_account;
  174. }
  175. const result = await transaction.insert(this.tableName, insertData);
  176. // 添加审批流程
  177. const auditList = await this.ctx.service.shenpiAudit.getAuditList(data.tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
  178. const insertAuditDatas = [];
  179. for (const audit of auditList) {
  180. insertAuditDatas.push({
  181. spid,
  182. tid: data.tid,
  183. fpid: result.insertId,
  184. aid: audit.audit_id,
  185. order: audit.audit_order,
  186. times: 1,
  187. status: auditConst.status.uncheck,
  188. audit_type: audit.audit_type,
  189. audit_order: audit.audit_order,
  190. });
  191. }
  192. if (insertAuditDatas.length > 0) await transaction.insert(this.ctx.service.financialPayAudit.tableName, insertAuditDatas);
  193. await transaction.commit();
  194. return { id: result.insertId };
  195. } catch (e) {
  196. await transaction.rollback();
  197. throw e;
  198. }
  199. }
  200. async delPay(fpid) {
  201. if (!fpid) {
  202. throw '参数有误';
  203. }
  204. const node = await this.getDataById(fpid);
  205. if (!node) {
  206. throw '资金支付不存在';
  207. }
  208. const transaction = await this.db.beginTransaction();
  209. try {
  210. await transaction.delete(this.tableName, { id: node.id });
  211. // 删除合同附件
  212. const attList = await this.ctx.service.financialPayAtt.getAllDataByCondition({ where: { fpid } });
  213. await this.ctx.helper.delFiles(attList);
  214. await transaction.delete(this.ctx.service.financialPayAtt.tableName, { fpid });
  215. await transaction.delete(this.ctx.service.financialPayAudit.tableName, { fpid });
  216. await transaction.delete(this.ctx.service.financialPayContract.tableName, { fpid });
  217. await transaction.commit();
  218. } catch (err) {
  219. console.log(err);
  220. await transaction.rollback();
  221. throw err;
  222. }
  223. return true;
  224. }
  225. async savePay(fpid, postData) {
  226. // 初始化事务
  227. const transaction = await this.db.beginTransaction();
  228. let result = false;
  229. try {
  230. const updateData = {
  231. id: fpid,
  232. };
  233. updateData[postData.name] = postData.val;
  234. await transaction.update(this.tableName, updateData);
  235. await transaction.commit();
  236. result = true;
  237. } catch (error) {
  238. await transaction.rollback();
  239. result = false;
  240. }
  241. return result;
  242. }
  243. async loadFinancialPayAuditViewData(financialPay) {
  244. const times = financialPay.status === auditConst.status.checkNo ? financialPay.times - 1 : financialPay.times;
  245. if (!financialPay.user) financialPay.user = await this.ctx.service.projectAccount.getAccountInfoById(financialPay.uid);
  246. financialPay.auditHistory = await this.ctx.service.financialPayAudit.getAuditorHistory(financialPay.id, times);
  247. // 获取审批流程中左边列表
  248. if ((financialPay.status === auditConst.status.checkNo || financialPay.status === auditConst.status.revise) && financialPay.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) {
  249. const auditors = await this.ctx.service.financialPayAudit.getAuditors(financialPay.id, times); // 全部参与的审批人
  250. const auditorGroups = this.ctx.helper.groupAuditors(auditors, 'order', true);
  251. financialPay.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  252. financialPay.auditors2.unshift([{
  253. aid: financialPay.user.id, order: 0, times: financialPay.times - 1, audit_order: 0, audit_type: auditType.key.common,
  254. name: financialPay.user.name, role: financialPay.user.role, company: financialPay.user.company,
  255. }]);
  256. } else {
  257. financialPay.auditors2 = financialPay.userGroups;
  258. }
  259. if (financialPay.status === auditConst.status.uncheck || financialPay.status === auditConst.status.checkNo) {
  260. financialPay.auditorList = await this.ctx.service.financialPayAudit.getAuditors(financialPay.id, financialPay.times);
  261. }
  262. }
  263. async loadPayUser(pay) {
  264. const status = auditConst.status;
  265. const accountId = this.ctx.session.sessionUser.accountId;
  266. pay.permission = await this.ctx.service.subProjPermission.getFinancailPermission(this.ctx.subProject.permission.fund_trans_permission, this.ctx.subProject.permission.fund_pay_permission);
  267. pay.user = await this.ctx.service.projectAccount.getAccountInfoById(pay.uid);
  268. pay.auditors = await this.ctx.service.financialPayAudit.getAuditors(pay.id, pay.times); // 全部参与的审批人
  269. pay.auditorIds = this._.map(pay.auditors, 'aid');
  270. pay.curAuditors = pay.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
  271. pay.curAuditorIds = this._.map(pay.curAuditors, 'aid');
  272. pay.flowAuditors = pay.curAuditors.length > 0 ? pay.auditors.filter(x => { return x.order === pay.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  273. pay.flowAuditorIds = this._.map(pay.flowAuditors, 'aid');
  274. pay.nextAuditors = pay.curAuditors.length > 0 ? pay.auditors.filter(x => { return x.order === pay.curAuditors[0].order + 1; }) : [];
  275. pay.nextAuditorIds = this._.map(pay.nextAuditors, 'aid');
  276. pay.auditorGroups = this.ctx.helper.groupAuditors(pay.auditors, 'order', true);
  277. pay.userGroups = this.ctx.helper.groupAuditorsUniq(pay.auditorGroups);
  278. pay.userGroups.unshift([{
  279. aid: pay.user.id, order: 0, times: pay.times, audit_order: 0, audit_type: auditType.key.common,
  280. name: pay.user.name, role: pay.user.role, company: pay.user.company,
  281. }]);
  282. pay.finalAuditorIds = pay.userGroups[pay.userGroups.length - 1].map(x => { return x.aid; });
  283. }
  284. }
  285. return FinancialPay;
  286. };