financial_pay.js 20 KB

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