financial_pay.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 = fpsid instanceof Array ? allfpsid : [allfpsid];
  45. addSql += ' 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 = fpsid instanceof Array ? allfpsid : [allfpsid];
  127. addSql += ' 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, 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. addSql += ' AND (a.uid = ' + uid + ' OR ' + notAdminSql + ')';
  189. }
  190. const sql = 'SELECT a.tid, t.name FROM ?? As a LEFT JOIN ?? As t ON a.tid = t.id' +
  191. ' WHERE a.spid = ?' + addSql + ' GROUP BY a.tid';
  192. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, spid];
  193. return await this.db.query(sql, sqlParam);
  194. }
  195. async addPay(spid, payStage, data) {
  196. if (!data.tid || !data.code || !data.used) {
  197. throw '参数错误';
  198. }
  199. const times = new Date();
  200. const transaction = await this.db.beginTransaction();
  201. try {
  202. const info = await this.ctx.service.financialPay.getDataByCondition({ spid, tid: data.tid, code: data.code });
  203. if (info) {
  204. throw '支付编号已存在';
  205. }
  206. const insertData = {
  207. spid,
  208. fpsid: payStage.id,
  209. tid: data.tid,
  210. code: data.code,
  211. used: data.used,
  212. uid: this.ctx.session.sessionUser.accountId,
  213. status: auditConst.status.uncheck,
  214. times: 1,
  215. create_time: times,
  216. };
  217. const payTender = await this.ctx.service.financialPayTender.getDataByCondition({ spid, tid: data.tid });
  218. if (payTender) {
  219. insertData.entity = payTender.name;
  220. insertData.bank = payTender.bank;
  221. insertData.bank_account = payTender.bank_account;
  222. }
  223. const result = await transaction.insert(this.tableName, insertData);
  224. // 添加审批流程
  225. const auditList = await this.ctx.service.shenpiAudit.getAuditList(data.tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
  226. const insertAuditDatas = [];
  227. for (const audit of auditList) {
  228. insertAuditDatas.push({
  229. spid,
  230. tid: data.tid,
  231. fpid: result.insertId,
  232. fpsid: payStage.id,
  233. aid: audit.audit_id,
  234. order: audit.audit_order,
  235. times: 1,
  236. status: auditConst.status.uncheck,
  237. audit_type: audit.audit_type,
  238. audit_order: audit.audit_order,
  239. });
  240. }
  241. if (insertAuditDatas.length > 0) await transaction.insert(this.ctx.service.financialPayAudit.tableName, insertAuditDatas);
  242. if (payStage.can_del === 1) await transaction.update(this.ctx.service.financialPayStage.tableName, { id: payStage.id, can_del: 0 });
  243. await transaction.commit();
  244. return { id: result.insertId };
  245. } catch (e) {
  246. await transaction.rollback();
  247. throw e;
  248. }
  249. }
  250. async delPay(fpid) {
  251. if (!fpid) {
  252. throw '参数有误';
  253. }
  254. const node = await this.getDataById(fpid);
  255. if (!node) {
  256. throw '资金支付不存在';
  257. }
  258. const transaction = await this.db.beginTransaction();
  259. try {
  260. await transaction.delete(this.tableName, { id: node.id });
  261. // 删除合同附件
  262. const attList = await this.ctx.service.financialPayAtt.getAllDataByCondition({ where: { fpid } });
  263. await this.ctx.helper.delFiles(attList);
  264. await transaction.delete(this.ctx.service.financialPayAtt.tableName, { fpid });
  265. await transaction.delete(this.ctx.service.financialPayAudit.tableName, { fpid });
  266. await transaction.delete(this.ctx.service.financialPayContract.tableName, { fpid });
  267. if (node.fpsid) {
  268. const payStage = await this.ctx.service.financialPayStage.getDataById(node.fpsid);
  269. await this.ctx.service.financialPayStage.updatePayStageAndAfter(transaction, payStage.spid, payStage);
  270. }
  271. await transaction.commit();
  272. } catch (err) {
  273. console.log(err);
  274. await transaction.rollback();
  275. throw err;
  276. }
  277. return true;
  278. }
  279. async savePay(fpid, postData) {
  280. // 初始化事务
  281. const transaction = await this.db.beginTransaction();
  282. let result = false;
  283. try {
  284. const updateData = {
  285. id: fpid,
  286. };
  287. updateData[postData.name] = postData.val;
  288. await transaction.update(this.tableName, updateData);
  289. await transaction.commit();
  290. result = true;
  291. } catch (error) {
  292. await transaction.rollback();
  293. result = false;
  294. }
  295. return result;
  296. }
  297. async loadFinancialPayAuditViewData(financialPay) {
  298. const times = financialPay.status === auditConst.status.checkNo ? financialPay.times - 1 : financialPay.times;
  299. if (!financialPay.user) financialPay.user = await this.ctx.service.projectAccount.getAccountInfoById(financialPay.uid);
  300. financialPay.auditHistory = await this.ctx.service.financialPayAudit.getAuditorHistory(financialPay.id, times);
  301. // 获取审批流程中左边列表
  302. if ((financialPay.status === auditConst.status.checkNo || financialPay.status === auditConst.status.revise) && financialPay.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) {
  303. const auditors = await this.ctx.service.financialPayAudit.getAuditors(financialPay.id, times); // 全部参与的审批人
  304. const auditorGroups = this.ctx.helper.groupAuditors(auditors, 'order', true);
  305. financialPay.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  306. financialPay.auditors2.unshift([{
  307. aid: financialPay.user.id, order: 0, times: financialPay.times - 1, audit_order: 0, audit_type: auditType.key.common,
  308. name: financialPay.user.name, role: financialPay.user.role, company: financialPay.user.company,
  309. }]);
  310. } else {
  311. financialPay.auditors2 = financialPay.userGroups;
  312. }
  313. if (financialPay.status === auditConst.status.uncheck || financialPay.status === auditConst.status.checkNo) {
  314. financialPay.auditorList = await this.ctx.service.financialPayAudit.getAuditors(financialPay.id, financialPay.times);
  315. }
  316. }
  317. async loadPayUser(pay) {
  318. const status = auditConst.status;
  319. const accountId = this.ctx.session.sessionUser.accountId;
  320. pay.permission = await this.ctx.service.subProjPermission.getFinancailPermission(this.ctx.subProject.permission.fund_trans_permission, this.ctx.subProject.permission.fund_pay_permission);
  321. pay.user = await this.ctx.service.projectAccount.getAccountInfoById(pay.uid);
  322. pay.auditors = await this.ctx.service.financialPayAudit.getAuditors(pay.id, pay.times); // 全部参与的审批人
  323. pay.auditorIds = this._.map(pay.auditors, 'aid');
  324. pay.curAuditors = pay.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
  325. pay.curAuditorIds = this._.map(pay.curAuditors, 'aid');
  326. pay.flowAuditors = pay.curAuditors.length > 0 ? pay.auditors.filter(x => { return x.order === pay.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  327. pay.flowAuditorIds = this._.map(pay.flowAuditors, 'aid');
  328. pay.nextAuditors = pay.curAuditors.length > 0 ? pay.auditors.filter(x => { return x.order === pay.curAuditors[0].order + 1; }) : [];
  329. pay.nextAuditorIds = this._.map(pay.nextAuditors, 'aid');
  330. pay.auditorGroups = this.ctx.helper.groupAuditors(pay.auditors, 'order', true);
  331. pay.userGroups = this.ctx.helper.groupAuditorsUniq(pay.auditorGroups);
  332. pay.userGroups.unshift([{
  333. aid: pay.user.id, order: 0, times: pay.times, audit_order: 0, audit_type: auditType.key.common,
  334. name: pay.user.name, role: pay.user.role, company: pay.user.company,
  335. }]);
  336. pay.finalAuditorIds = pay.userGroups[pay.userGroups.length - 1].map(x => { return x.aid; });
  337. }
  338. async batchOldPays(spid, payStage, payIds) {
  339. if (!payStage || !payIds || payIds.length === 0) {
  340. throw '参数有误';
  341. }
  342. const transaction = await this.db.beginTransaction();
  343. try {
  344. const updateAuditDatas = [];
  345. const updateDatas = [];
  346. const oldPays = await this.getAllDataByCondition({ where: { id: payIds } });
  347. const oldPayAudits = await this.ctx.service.financialPayAudit.getAllDataByCondition({ where: { fpid: payIds } });
  348. for (const node of oldPays) {
  349. if (node.fpsid) {
  350. throw '该支付已绑定单位期';
  351. }
  352. updateDatas.push({
  353. id: node.id,
  354. fpsid: payStage.id,
  355. });
  356. const oldPayAuditsByFpid = oldPayAudits.filter(x => { return x.fpid === node.id; });
  357. for (const audit of oldPayAuditsByFpid) {
  358. if (!audit.fpsid) {
  359. updateAuditDatas.push({
  360. id: audit.id,
  361. fpsid: payStage.id,
  362. });
  363. }
  364. }
  365. }
  366. if (updateDatas.length > 0) {
  367. await transaction.updateRows(this.tableName, updateDatas);
  368. }
  369. if (updateAuditDatas.length > 0) {
  370. await transaction.updateRows(this.ctx.service.financialPayAudit.tableName, updateAuditDatas);
  371. }
  372. // 更新本单位期金额和同单位的上期金额
  373. await transaction.update(this.ctx.service.financialPayStage.tableName, { id: payStage.id, can_del: 0 });
  374. await this.ctx.service.financialPayStage.updatePayStageAndAfter(transaction, spid, payStage);
  375. await transaction.commit();
  376. return true;
  377. } catch (err) {
  378. console.log(err);
  379. await transaction.rollback();
  380. throw err;
  381. }
  382. }
  383. }
  384. return FinancialPay;
  385. };