financial_pay.js 18 KB

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