financial_pay.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. const payStage = pay.fpsid ? await this.ctx.service.financialPayStage.getOnePayStage(pay.fpsid) : null;
  26. pay.payStage = payStage ? payStage : null;
  27. return pay;
  28. }
  29. /**
  30. * 获取列表
  31. * @param {int} spid - 项目id
  32. * @param {int} status - 状态
  33. * @param {int} hadlimit - 分页
  34. * @return {object} list - 列表
  35. */
  36. async getListByStatus(spid, allfpsid = null, fpsid = null, status = 0, tid = null, used = null, hadlimit = 0, sortBy = '', orderBy = '') {
  37. let addSql = '';
  38. if (fpsid !== null) {
  39. fpsid = fpsid instanceof Array ? fpsid : [fpsid];
  40. addSql += ' AND a.fpsid in (' + this.ctx.helper.getInArrStrSqlFilter(fpsid) + ')';
  41. } else if (!this.ctx.session.sessionUser.is_admin && this._.includes([0, 5, 3], status)) {
  42. addSql += ` AND (a.id in (SELECT b.fpid FROM ${this.ctx.service.financialPayAudit.tableName} as b WHERE b.spid = '${spid}' AND b.aid = ${this.ctx.session.sessionUser.accountId}) OR a.uid = ${this.ctx.session.sessionUser.accountId}`;
  43. if (allfpsid !== null) {
  44. allfpsid = allfpsid instanceof Array ? allfpsid : [allfpsid];
  45. addSql += allfpsid.length > 0 ? ' OR a.fpsid in (' + this.ctx.helper.getInArrStrSqlFilter(allfpsid) + ')' : '';
  46. }
  47. addSql += ')';
  48. }
  49. if (tid !== null) {
  50. if (tid.length === 0) {
  51. return [];
  52. }
  53. addSql += ' AND a.tid in (' + this.ctx.helper.getInArrStrSqlFilter(tid) + ')';
  54. }
  55. if (used) {
  56. addSql += ' AND a.used = "' + used + '"';
  57. }
  58. let sql = '';
  59. let sqlParam = '';
  60. switch (status) {
  61. case 0: // 所有
  62. sql = 'SELECT a.* FROM ?? As a WHERE a.spid = ?' + addSql;
  63. sqlParam = [this.tableName, spid];
  64. break;
  65. case 1: // 待处理(你的)
  66. 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 = ?)))';
  67. 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];
  68. break;
  69. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  70. sql =
  71. 'SELECT a.* FROM ?? as a WHERE a.spid = ?' + addSql + ' AND (a.status = ? OR a.status = ?)';
  72. sqlParam = [
  73. this.tableName,
  74. spid,
  75. auditConst.status.uncheck,
  76. auditConst.status.checkNo,
  77. ];
  78. break;
  79. case 2: // 进行中(所有的)
  80. case 4: // 终止(所有的)
  81. sql = 'SELECT a.* FROM ?? as a WHERE ' +
  82. 'a.status = ? AND a.spid = ?' + addSql + ' AND (a.uid = ? OR ' +
  83. 'a.id IN (SELECT b.fpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.fpid))';
  84. sqlParam = [this.tableName, status, spid, this.ctx.session.sessionUser.accountId,
  85. this.ctx.service.financialPayAudit.tableName, this.ctx.session.sessionUser.accountId];
  86. break;
  87. case 3: // 已完成(所有的)
  88. sql = 'SELECT a.* FROM ?? as a WHERE a.status = ? AND a.spid = ?' + addSql;
  89. sqlParam = [this.tableName, status, spid];
  90. break;
  91. default:
  92. break;
  93. }
  94. if (sortBy && orderBy) {
  95. if (sortBy === 'code') {
  96. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  97. } else {
  98. sql += ' ORDER BY a.create_time ' + orderBy;
  99. }
  100. } else {
  101. sql += ' ORDER BY a.create_time DESC';
  102. }
  103. if (hadlimit) {
  104. const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
  105. const offset = limit * (this.ctx.page - 1);
  106. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  107. sql += ' LIMIT ' + limitString;
  108. }
  109. const list = await this.db.query(sql, sqlParam);
  110. return list;
  111. }
  112. /**
  113. * 获取支付个数
  114. * @param {int} spid - 项目id
  115. * @param {int} status - 状态
  116. * @return {void}
  117. */
  118. async getCountByStatus(spid, allfpsid = null, fpsid = null, status = 0, tid = null, used = null) {
  119. let addSql = '';
  120. if (fpsid !== null) {
  121. fpsid = fpsid instanceof Array ? fpsid : [fpsid];
  122. addSql += ' AND a.fpsid in (' + this.ctx.helper.getInArrStrSqlFilter(fpsid) + ')';
  123. } else if (!this.ctx.session.sessionUser.is_admin && this._.includes([0, 5, 3], status)) {
  124. addSql += ` AND (a.id in(SELECT b.fpid FROM ${this.ctx.service.financialPayAudit.tableName} as b WHERE b.spid = '${spid}' AND b.aid = ${this.ctx.session.sessionUser.accountId}) OR a.uid = ${this.ctx.session.sessionUser.accountId}`;
  125. if (allfpsid !== null) {
  126. allfpsid = allfpsid instanceof Array ? allfpsid : [allfpsid];
  127. addSql += allfpsid.length > 0 ? ' OR a.fpsid in (' + this.ctx.helper.getInArrStrSqlFilter(allfpsid) + ')' : '';
  128. }
  129. addSql += ')';
  130. }
  131. if (tid !== null) {
  132. if (tid.length === 0) {
  133. return 0;
  134. }
  135. addSql += ' AND a.tid in (' + this.ctx.helper.getInArrStrSqlFilter(tid) + ')';
  136. }
  137. if (used) {
  138. addSql += ' AND a.used = "' + used + '"';
  139. }
  140. switch (status) {
  141. case 0: // 所有
  142. const sql5 = 'SELECT count(*) AS count FROM ?? As a WHERE a.spid = ?' + addSql;
  143. const sqlParam5 = [this.tableName, spid];
  144. const result5 = await this.db.query(sql5, sqlParam5);
  145. return result5[0].count;
  146. case 1: // 待处理(你的)
  147. 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 = ?)))';
  148. 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];
  149. const result6 = await this.db.query(sql6, sqlParam6);
  150. return result6[0].count;
  151. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  152. const sql2 =
  153. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  154. 'a.spid = ?' + addSql + ' AND (a.status = ? OR a.status = ?)';
  155. const sqlParam2 = [
  156. this.tableName,
  157. spid,
  158. auditConst.status.uncheck,
  159. auditConst.status.checkNo,
  160. ];
  161. const result2 = await this.db.query(sql2, sqlParam2);
  162. return result2[0].count;
  163. case 2: // 进行中(所有的)
  164. case 4: // 终止(所有的)
  165. const sql3 =
  166. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  167. '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))';
  168. const sqlParam3 = [this.tableName, status, spid, this.ctx.session.sessionUser.accountId, this.ctx.service.financialPayAudit.tableName, this.ctx.session.sessionUser.accountId];
  169. const result3 = await this.db.query(sql3, sqlParam3);
  170. return result3[0].count;
  171. case 3: // 已完成(所有的)
  172. const sql4 = 'SELECT count(*) AS count FROM ?? as a WHERE a.status = ? AND a.spid = ?' + addSql;
  173. const sqlParam4 = [this.tableName, status, spid];
  174. const result4 = await this.db.query(sql4, sqlParam4);
  175. return result4[0].count;
  176. default:
  177. break;
  178. }
  179. }
  180. async getUserTenderList(spid, uid, allFpsidList, fpsid = null) {
  181. let addSql = '';
  182. let notAdminSql = '';
  183. if (fpsid) {
  184. addSql += ' AND a.fpsid = ' + fpsid;
  185. }
  186. if (!this.ctx.session.sessionUser.is_admin) {
  187. notAdminSql += '(a.tid in (SELECT tid FROM ' + this.ctx.service.financialPayAudit.tableName + ' WHERE spid = "' + spid + '" AND aid = ' + this.ctx.session.sessionUser.accountId + (fpsid ? ' AND fpsid = ' + fpsid : '') + ' GROUP BY tid)';
  188. if (!fpsid && allFpsidList && allFpsidList.length > 0) {
  189. notAdminSql += ' OR a.tid in (SELECT tid FROM ' + this.tableName + ' WHERE spid = "' + spid + '" AND fpsid in (' + this.ctx.helper.getInArrStrSqlFilter(allFpsidList) + ') GROUP BY tid)';
  190. }
  191. notAdminSql += ')';
  192. addSql += ' AND (a.uid = ' + uid + ' OR ' + notAdminSql + ')';
  193. }
  194. const sql = 'SELECT a.tid, t.name FROM ?? As a LEFT JOIN ?? As t ON a.tid = t.id' +
  195. ' WHERE a.spid = ?' + addSql + ' GROUP BY a.tid';
  196. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, spid];
  197. return await this.db.query(sql, sqlParam);
  198. }
  199. async addPay(spid, payStage, data) {
  200. if (!data.tid || !data.code || !data.used) {
  201. throw '参数错误';
  202. }
  203. const times = new Date();
  204. const transaction = await this.db.beginTransaction();
  205. try {
  206. const info = await this.ctx.service.financialPay.getDataByCondition({ spid, tid: data.tid, code: data.code });
  207. if (info) {
  208. throw '支付编号已存在';
  209. }
  210. const insertData = {
  211. spid,
  212. fpsid: payStage.id,
  213. tid: data.tid,
  214. code: data.code,
  215. used: data.used,
  216. uid: this.ctx.session.sessionUser.accountId,
  217. status: auditConst.status.uncheck,
  218. times: 1,
  219. create_time: times,
  220. };
  221. const payTender = await this.ctx.service.financialPayTender.getDataByCondition({ spid, tid: data.tid });
  222. if (payTender) {
  223. insertData.entity = payTender.name;
  224. insertData.bank = payTender.bank;
  225. insertData.bank_account = payTender.bank_account;
  226. }
  227. const result = await transaction.insert(this.tableName, insertData);
  228. // 添加审批流程
  229. const auditList = await this.ctx.service.shenpiAudit.getAuditList(data.tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
  230. const insertAuditDatas = [];
  231. for (const audit of auditList) {
  232. insertAuditDatas.push({
  233. spid,
  234. tid: data.tid,
  235. fpid: result.insertId,
  236. fpsid: payStage.id,
  237. aid: audit.audit_id,
  238. order: audit.audit_order,
  239. times: 1,
  240. status: auditConst.status.uncheck,
  241. audit_type: audit.audit_type,
  242. audit_order: audit.audit_order,
  243. });
  244. }
  245. if (insertAuditDatas.length > 0) await transaction.insert(this.ctx.service.financialPayAudit.tableName, insertAuditDatas);
  246. if (payStage.can_del === 1) await transaction.update(this.ctx.service.financialPayStage.tableName, { id: payStage.id, can_del: 0 });
  247. await transaction.commit();
  248. return { id: result.insertId };
  249. } catch (e) {
  250. await transaction.rollback();
  251. throw e;
  252. }
  253. }
  254. async delPay(fpid) {
  255. if (!fpid) {
  256. throw '参数有误';
  257. }
  258. const node = await this.getDataById(fpid);
  259. if (!node) {
  260. throw '资金支付不存在';
  261. }
  262. const transaction = await this.db.beginTransaction();
  263. try {
  264. await transaction.delete(this.tableName, { id: node.id });
  265. // 删除合同附件
  266. const attList = await this.ctx.service.financialPayAtt.getAllDataByCondition({ where: { fpid } });
  267. await this.ctx.helper.delFiles(attList);
  268. await transaction.delete(this.ctx.service.financialPayAtt.tableName, { fpid });
  269. await transaction.delete(this.ctx.service.financialPayAudit.tableName, { fpid });
  270. await transaction.delete(this.ctx.service.financialPayContract.tableName, { fpid });
  271. if (node.fpsid) {
  272. const payStage = await this.ctx.service.financialPayStage.getDataById(node.fpsid);
  273. await this.ctx.service.financialPayStage.updatePayStageAndAfter(transaction, payStage.spid, payStage);
  274. }
  275. await transaction.commit();
  276. } catch (err) {
  277. console.log(err);
  278. await transaction.rollback();
  279. throw err;
  280. }
  281. return true;
  282. }
  283. async savePay(fpid, postData) {
  284. // 初始化事务
  285. const transaction = await this.db.beginTransaction();
  286. let result = false;
  287. try {
  288. const updateData = {
  289. id: fpid,
  290. };
  291. updateData[postData.name] = postData.val;
  292. await transaction.update(this.tableName, updateData);
  293. await transaction.commit();
  294. result = true;
  295. } catch (error) {
  296. await transaction.rollback();
  297. result = false;
  298. }
  299. return result;
  300. }
  301. async loadFinancialPayAuditViewData(financialPay) {
  302. const times = financialPay.status === auditConst.status.checkNo ? financialPay.times - 1 : financialPay.times;
  303. if (!financialPay.user) financialPay.user = await this.ctx.service.projectAccount.getAccountInfoById(financialPay.uid);
  304. financialPay.auditHistory = await this.ctx.service.financialPayAudit.getAuditorHistory(financialPay.id, times);
  305. // 获取审批流程中左边列表
  306. if ((financialPay.status === auditConst.status.checkNo || financialPay.status === auditConst.status.revise) && financialPay.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) {
  307. const auditors = await this.ctx.service.financialPayAudit.getAuditors(financialPay.id, times); // 全部参与的审批人
  308. const auditorGroups = this.ctx.helper.groupAuditors(auditors, 'order', true);
  309. financialPay.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  310. financialPay.auditors2.unshift([{
  311. aid: financialPay.user.id, order: 0, times: financialPay.times - 1, audit_order: 0, audit_type: auditType.key.common,
  312. name: financialPay.user.name, role: financialPay.user.role, company: financialPay.user.company,
  313. }]);
  314. } else {
  315. financialPay.auditors2 = financialPay.userGroups;
  316. }
  317. if (financialPay.status === auditConst.status.uncheck || financialPay.status === auditConst.status.checkNo) {
  318. financialPay.auditorList = await this.ctx.service.financialPayAudit.getAuditors(financialPay.id, financialPay.times);
  319. }
  320. }
  321. async loadPayUser(pay) {
  322. const status = auditConst.status;
  323. const accountId = this.ctx.session.sessionUser.accountId;
  324. pay.permission = await this.ctx.service.subProjPermission.getFinancailPermission(this.ctx.subProject.permission.fund_trans_permission, this.ctx.subProject.permission.fund_pay_permission);
  325. pay.user = await this.ctx.service.projectAccount.getAccountInfoById(pay.uid);
  326. pay.auditors = await this.ctx.service.financialPayAudit.getAuditors(pay.id, pay.times); // 全部参与的审批人
  327. pay.auditorIds = this._.map(pay.auditors, 'aid');
  328. pay.curAuditors = pay.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
  329. pay.curAuditorIds = this._.map(pay.curAuditors, 'aid');
  330. pay.flowAuditors = pay.curAuditors.length > 0 ? pay.auditors.filter(x => { return x.order === pay.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  331. pay.flowAuditorIds = this._.map(pay.flowAuditors, 'aid');
  332. pay.nextAuditors = pay.curAuditors.length > 0 ? pay.auditors.filter(x => { return x.order === pay.curAuditors[0].order + 1; }) : [];
  333. pay.nextAuditorIds = this._.map(pay.nextAuditors, 'aid');
  334. pay.auditorGroups = this.ctx.helper.groupAuditors(pay.auditors, 'order', true);
  335. pay.userGroups = this.ctx.helper.groupAuditorsUniq(pay.auditorGroups);
  336. pay.userGroups.unshift([{
  337. aid: pay.user.id, order: 0, times: pay.times, audit_order: 0, audit_type: auditType.key.common,
  338. name: pay.user.name, role: pay.user.role, company: pay.user.company,
  339. }]);
  340. pay.finalAuditorIds = pay.userGroups[pay.userGroups.length - 1].map(x => { return x.aid; });
  341. }
  342. async batchOldPays(spid, payStage, payIds) {
  343. if (!payStage || !payIds || payIds.length === 0) {
  344. throw '参数有误';
  345. }
  346. const transaction = await this.db.beginTransaction();
  347. try {
  348. const updateAuditDatas = [];
  349. const updateDatas = [];
  350. const oldPays = await this.getAllDataByCondition({ where: { id: payIds } });
  351. const oldPayAudits = await this.ctx.service.financialPayAudit.getAllDataByCondition({ where: { fpid: payIds } });
  352. for (const node of oldPays) {
  353. if (node.fpsid) {
  354. throw '该支付已绑定单位期';
  355. }
  356. updateDatas.push({
  357. id: node.id,
  358. fpsid: payStage.id,
  359. });
  360. const oldPayAuditsByFpid = oldPayAudits.filter(x => { return x.fpid === node.id; });
  361. for (const audit of oldPayAuditsByFpid) {
  362. if (!audit.fpsid) {
  363. updateAuditDatas.push({
  364. id: audit.id,
  365. fpsid: payStage.id,
  366. });
  367. }
  368. }
  369. }
  370. if (updateDatas.length > 0) {
  371. await transaction.updateRows(this.tableName, updateDatas);
  372. }
  373. if (updateAuditDatas.length > 0) {
  374. await transaction.updateRows(this.ctx.service.financialPayAudit.tableName, updateAuditDatas);
  375. }
  376. // 更新本单位期金额和同单位的上期金额
  377. await transaction.update(this.ctx.service.financialPayStage.tableName, { id: payStage.id, can_del: 0 });
  378. await this.ctx.service.financialPayStage.updatePayStageAndAfter(transaction, spid, payStage);
  379. await transaction.commit();
  380. return true;
  381. } catch (err) {
  382. console.log(err);
  383. await transaction.rollback();
  384. throw err;
  385. }
  386. }
  387. }
  388. return FinancialPay;
  389. };