payment_detail_audit.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. 'use strict';
  2. /**
  3. * 版本数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const shenpiConst = require('../const/shenpi');
  11. module.exports = app => {
  12. class PaymentDetailAudit extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'payment_detail_audit';
  22. }
  23. /**
  24. * 获取审核人流程列表
  25. *
  26. * @param auditorId
  27. * @return {Promise<*>}
  28. */
  29. async getAuditGroupByList(detailId, times) {
  30. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`td_id`, la.`aid`, la.`order` ' +
  31. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  32. ' WHERE la.`td_id` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  33. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, times];
  34. return await this.db.query(sql, sqlParam);
  35. }
  36. /**
  37. * 复制上一期的审批人列表给最新一期
  38. *
  39. * @param transaction - 新增一期的事务
  40. * @param {Object} preStage - 上一期
  41. * @param {Object} newStage - 最新一期
  42. * @return {Promise<*>}
  43. */
  44. async copyPreDetailAuditors(transaction, preDetail, newDetail) {
  45. const auditors = await this.getAuditGroupByList(preDetail.id, preDetail.times);
  46. const newAuditors = [];
  47. for (const a of auditors) {
  48. if (a.aid !== newDetail.uid) {
  49. const na = {
  50. tender_id: preDetail.tender_id,
  51. tr_id: preDetail.tr_id,
  52. td_id: newDetail.id,
  53. aid: a.aid,
  54. times: newDetail.times,
  55. order: newAuditors.length + 1,
  56. status: auditConst.status.uncheck,
  57. };
  58. newAuditors.push(na);
  59. }
  60. }
  61. const result = await transaction.insert(this.tableName, newAuditors);
  62. return (result.effectRows = auditors.length);
  63. }
  64. /**
  65. * 获取 审核列表信息
  66. *
  67. * @param {Number} detailId - 支付审批表单详情id
  68. * @param {Number} times - 第几次审批
  69. * @return {Promise<*>}
  70. */
  71. async getAuditors(detailId, times = 1) {
  72. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
  73. 'FROM ?? AS la, ?? AS pa, (SELECT `aid`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `td_id` = ? AND `times` = ? GROUP BY `aid`) as g ' +
  74. 'WHERE la.`td_id` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
  75. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, detailId, times, detailId, times];
  76. const result = await this.db.query(sql, sqlParam);
  77. const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `td_id` = ? AND `times` = ? GROUP BY `aid`) as a';
  78. const sqlParam2 = [this.tableName, detailId, times];
  79. const count = await this.db.queryOne(sql2, sqlParam2);
  80. for (const i in result) {
  81. result[i].max_sort = count.num;
  82. }
  83. return result;
  84. }
  85. /**
  86. * 安全生产费-审核比较专用,它用不可修改
  87. * @param detail
  88. * @returns {Promise<Array>}
  89. */
  90. async getViewFlow(detail) {
  91. const result = [];
  92. const user = await this.ctx.service.projectAccount.getDataById(detail.uid);
  93. if (detail.status === auditConst.status.uncheck) {
  94. if (detail.readOnly) return result;
  95. result.push({
  96. aid: user.id, name: user.name, company: user.company, role: user.role, mobile: user.mobile, telephone: user.telephone,
  97. times: 1, order: 0, status: auditConst.status.uncheck
  98. });
  99. } else {
  100. result.push({
  101. aid: user.id, name: user.name, company: user.company, role: user.role, mobile: user.mobile, telephone: user.telephone,
  102. times: detail.curTimes, order: 0, status: auditConst.status.uncheck, latest: !detail.readOnly,
  103. });
  104. const sql = `SELECT pda.aid, pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pda.times, pda.order, pda.status, pda.opinion, pda.begin_time, pda.end_time` +
  105. ` FROM ${this.tableName} pda LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON pda.aid = pa.id` +
  106. ' WHERE pda.td_id = ? AND pda.times = ?';
  107. //const flow = await this.db.query(sql, [detail.id, detail.status === auditConst.status.checkNo && !detail.readOnly ? detail.curTimes - 1 : detail.curTimes]);
  108. const flow = await this.db.query(sql, [detail.id, detail.curTimes]);
  109. flow.forEach(x => {
  110. if (x.status === auditConst.status.checked) result.push(x);
  111. if (x.status === auditConst.status.checking && !detail.readOnly) {
  112. x.latest = true;
  113. result.push(x);
  114. }
  115. });
  116. }
  117. return result;
  118. }
  119. /**
  120. * 获取 当前审核人
  121. *
  122. * @param {Number} materialId - 支付审批表单详情id
  123. * @param {Number} times - 第几次审批
  124. * @return {Promise<*>}
  125. */
  126. async getCurAuditor(detailId, times = 1) {
  127. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  128. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  129. ' WHERE la.`td_id` = ? and la.`status` = ? and la.`times` = ?';
  130. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, auditConst.status.checking, times];
  131. return await this.db.queryOne(sql, sqlParam);
  132. }
  133. async updateNewAuditList(detail, newIdList) {
  134. const transaction = await this.db.beginTransaction();
  135. try {
  136. // 先删除旧的审批流,再添加新的
  137. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times });
  138. const newAuditors = [];
  139. let order = 1;
  140. for (const aid of newIdList) {
  141. newAuditors.push({
  142. tender_id: detail.tender_id, tr_id: detail.tr_id, td_id: detail.id, aid,
  143. times: detail.times, order, status: auditConst.status.uncheck,
  144. });
  145. order++;
  146. }
  147. if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  148. await transaction.commit();
  149. } catch (err) {
  150. await transaction.rollback();
  151. throw err;
  152. }
  153. }
  154. async updateLastAudit(detail, auditList, lastId) {
  155. const transaction = await this.db.beginTransaction();
  156. try {
  157. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  158. const idList = this._.map(auditList, 'aid');
  159. let order = idList.length + 1;
  160. if (idList.indexOf(lastId) !== -1) {
  161. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times, aid: lastId });
  162. const audit = this._.find(auditList, { aid: lastId });
  163. // 顺移之后审核人流程顺序
  164. await this._syncOrderByDelete(transaction, detail.id, audit.order, detail.times);
  165. order = order - 1;
  166. }
  167. // 添加终审
  168. const newAuditor = {
  169. tender_id: detail.tender_id, tr_id: detail.tr_id, td_id: detail.id, aid: lastId,
  170. times: detail.times, order, status: auditConst.status.uncheck,
  171. };
  172. await transaction.insert(this.tableName, newAuditor);
  173. await transaction.commit();
  174. } catch (err) {
  175. await transaction.rollback();
  176. throw err;
  177. }
  178. }
  179. /**
  180. * 移除审核人时,同步其后审核人order
  181. * @param transaction - 事务
  182. * @param {Number} materialId - 材料调差期id
  183. * @param {Number} auditorId - 审核人id
  184. * @param {Number} times - 第几次审批
  185. * @return {Promise<*>}
  186. * @private
  187. */
  188. async _syncOrderByDelete(transaction, detailId, order, times, selfOperate = '-') {
  189. this.initSqlBuilder();
  190. this.sqlBuilder.setAndWhere('td_id', {
  191. value: detailId,
  192. operate: '=',
  193. });
  194. this.sqlBuilder.setAndWhere('order', {
  195. value: order,
  196. operate: '>=',
  197. });
  198. this.sqlBuilder.setAndWhere('times', {
  199. value: times,
  200. operate: '=',
  201. });
  202. this.sqlBuilder.setUpdateData('order', {
  203. value: 1,
  204. selfOperate,
  205. });
  206. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  207. const data = await transaction.query(sql, sqlParam);
  208. return data;
  209. }
  210. /**
  211. * 获取审核人流程列表(包括原报)
  212. * @param {Number} materialId 调差id
  213. * @param {Number} times 审核次数
  214. * @return {Promise<Array>} 查询结果集(包括原报)
  215. */
  216. async getAuditorsWithOwner(detailId, times = 1) {
  217. const result = await this.getAuditGroupByList(detailId, times);
  218. const sql =
  219. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  220. ' FROM ' +
  221. this.ctx.service.paymentDetail.tableName +
  222. ' As s' +
  223. ' LEFT JOIN ' +
  224. this.ctx.service.projectAccount.tableName +
  225. ' As pa' +
  226. ' ON s.uid = pa.id' +
  227. ' WHERE s.id = ?';
  228. const sqlParam = [times, detailId, detailId];
  229. const user = await this.db.queryOne(sql, sqlParam);
  230. result.unshift(user);
  231. return result;
  232. }
  233. /**
  234. * 获取 最新审核顺序
  235. *
  236. * @param {Number} materialId - 材料调差期id
  237. * @param {Number} times - 第几次审批
  238. * @return {Promise<number>}
  239. */
  240. async getNewOrder(detailId, times = 1) {
  241. const sql = 'SELECT Max(??) As max_order FROM ?? Where `td_id` = ? and `times` = ?';
  242. const sqlParam = ['order', this.tableName, detailId, times];
  243. const result = await this.db.queryOne(sql, sqlParam);
  244. return result && result.max_order ? result.max_order + 1 : 1;
  245. }
  246. /**
  247. * 新增审核人
  248. *
  249. * @param {Number} materialId - 材料调差期id
  250. * @param {Number} auditorId - 审核人id
  251. * @param {Number} times - 第几次审批
  252. * @return {Promise<number>}
  253. */
  254. async addAuditor(detailId, auditorId, times = 1, is_gdzs = 0) {
  255. const transaction = await this.db.beginTransaction();
  256. try {
  257. let newOrder = await this.getNewOrder(detailId, times);
  258. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  259. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  260. if (is_gdzs) await this._syncOrderByDelete(transaction, detailId, newOrder, times, '+');
  261. const data = {
  262. tender_id: this.ctx.paymentTender.id,
  263. tr_id: this.ctx.trInfo.id,
  264. td_id: detailId,
  265. aid: auditorId,
  266. times,
  267. order: newOrder,
  268. status: auditConst.status.uncheck,
  269. };
  270. const result = await transaction.insert(this.tableName, data);
  271. await transaction.commit();
  272. return result.affectedRows === 1;
  273. } catch (err) {
  274. await transaction.rollback();
  275. throw err;
  276. }
  277. return false;
  278. }
  279. /**
  280. * 移除审核人
  281. *
  282. * @param {Number} materialId - 材料调差期id
  283. * @param {Number} auditorId - 审核人id
  284. * @param {Number} times - 第几次审批
  285. * @return {Promise<boolean>}
  286. */
  287. async deleteAuditor(detailId, auditorId, times = 1) {
  288. const transaction = await this.db.beginTransaction();
  289. try {
  290. const condition = { td_id: detailId, aid: auditorId, times };
  291. const auditor = await this.getDataByCondition(condition);
  292. if (!auditor) {
  293. throw '该审核人不存在';
  294. }
  295. await this._syncOrderByDelete(transaction, detailId, auditor.order, times);
  296. await transaction.delete(this.tableName, condition);
  297. await transaction.commit();
  298. } catch (err) {
  299. await transaction.rollback();
  300. throw err;
  301. }
  302. return true;
  303. }
  304. /**
  305. * 移除审核人
  306. *
  307. * @param {Number} materialId - 材料调差期id
  308. * @param {Number} status - 期状态
  309. * @param {Number} status - 期次数
  310. * @return {Promise<boolean>}
  311. */
  312. async getAuditorByStatus(detailId, status, times = 1) {
  313. let auditor = null;
  314. let sql = '';
  315. let sqlParam = '';
  316. switch (status) {
  317. case auditConst.status.checking :
  318. case auditConst.status.checked :
  319. case auditConst.status.checkNoPre :
  320. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tr_id`, la.`td_id`, la.`aid`, la.`order` ' +
  321. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  322. ' WHERE la.`td_id` = ? and la.`status` = ? ' +
  323. ' ORDER BY la.`times` desc, la.`order` desc';
  324. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, status];
  325. auditor = await this.db.queryOne(sql, sqlParam);
  326. break;
  327. case auditConst.status.checkNo :
  328. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tr_id`, la.`td_id`, la.`aid`, la.`order` ' +
  329. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  330. ' WHERE la.`td_id` = ? and la.`status` = ? and la.`times` = ?' +
  331. ' ORDER BY la.`times` desc, la.`order` desc';
  332. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, auditConst.status.checkNo, parseInt(times) - 1];
  333. auditor = await this.db.queryOne(sql, sqlParam);
  334. break;
  335. case auditConst.status.uncheck :
  336. default:break;
  337. }
  338. return auditor;
  339. }
  340. /**
  341. * 开始审批
  342. * @param {Number} materialId - 材料调差期id
  343. * @param {Number} times - 第几次审批
  344. * @return {Promise<boolean>}
  345. */
  346. async start(detailId, times = 1) {
  347. const audit = await this.getDataByCondition({ td_id: detailId, times, order: 1 });
  348. if (!audit) {
  349. if (this.ctx.trinfo.sp_status === shenpiConst.sp_status.gdspl) {
  350. throw '请联系管理员添加审批人';
  351. } else {
  352. throw '请先选择审批人,再上报数据';
  353. }
  354. }
  355. const transaction = await this.db.beginTransaction();
  356. try {
  357. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  358. // 如果是报表角色则需要更新到detail中
  359. const updateDetailData = {
  360. id: detailId, status: auditConst.status.checking,
  361. };
  362. const rptAudit = await this.ctx.service.paymentRptAudit.getDataByCondition({ td_id: detailId, uid: this.ctx.session.sessionUser.accountId });
  363. if (rptAudit && rptAudit.signature_msg) {
  364. const report_json = JSON.parse(this.ctx.detail.report_json);
  365. const sign_msg = JSON.parse(rptAudit.signature_msg);
  366. updateDetailData.report_json = JSON.stringify(await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg));
  367. // 更新签字确定时间
  368. await this.ctx.service.paymentRptAudit.updateSignTime(transaction, rptAudit.id);
  369. }
  370. await transaction.update(this.ctx.service.paymentDetail.tableName, updateDetailData);
  371. // 安全生产费存储相关
  372. await this.ctx.service.paymentSafeBills.auditCache(transaction, detailId, {
  373. user_id: this.ctx.detail.uid, times, order: 0,
  374. });
  375. // 判断用户是否有权限查看支付审批,没有则自动加入到权限中
  376. const auditList = await this.getAllDataByCondition({ where: { td_id: detailId, times } });
  377. const rptAuditList = await this.ctx.service.paymentRptAudit.getAllDataByCondition({ where: { td_id: detailId } });
  378. const auditIdList = this._.map(auditList, 'aid');
  379. const rptAuditIdList = this._.map(rptAuditList, 'uid');
  380. const permissionAuditList = await this.ctx.service.paymentPermissionAudit.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id } });
  381. const paIdList = this._.map(permissionAuditList, 'uid');
  382. const detailIdList = this._.union(auditIdList, rptAuditIdList);
  383. const newAudits = this._.difference(detailIdList, paIdList);
  384. if (newAudits.length > 0) {
  385. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  386. where: { project_id: this.ctx.session.sessionProject.id, id: newAudits },
  387. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  388. });
  389. await this.ctx.service.paymentPermissionAudit.saveAudits(this.ctx.session.sessionProject.id, accountList, transaction);
  390. }
  391. // todo 更新标段tender状态 ?
  392. await transaction.commit();
  393. } catch (err) {
  394. await transaction.rollback();
  395. throw err;
  396. }
  397. return true;
  398. }
  399. async _checked(pid, detailId, checkData, times) {
  400. const time = new Date();
  401. // 整理当前流程审核人状态更新
  402. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  403. if (!audit) {
  404. throw '审核数据错误';
  405. }
  406. const nextAudit = await this.getDataByCondition({ td_id: detailId, times, order: audit.order + 1 });
  407. const transaction = await this.db.beginTransaction();
  408. try {
  409. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  410. // 如果是报表角色则需要更新到detail中
  411. const updateDetailData = {
  412. id: detailId,
  413. };
  414. const rptAudit = await this.ctx.service.paymentRptAudit.getDataByCondition({ td_id: detailId, uid: audit.aid });
  415. if (rptAudit && rptAudit.signature_msg) {
  416. const report_json = JSON.parse(this.ctx.detail.report_json);
  417. const sign_msg = JSON.parse(rptAudit.signature_msg);
  418. updateDetailData.report_json = JSON.stringify(await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg));
  419. await this.ctx.service.paymentRptAudit.updateSignTime(transaction, rptAudit.id);
  420. }
  421. // 无下一审核人表示,审核结束
  422. if (nextAudit) {
  423. // 流程至下一审批人
  424. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  425. // 同步 期信息
  426. updateDetailData.status = auditConst.status.checking;
  427. } else {
  428. // 本期结束
  429. // 同步 期信息
  430. updateDetailData.status = checkData.checkType;
  431. }
  432. // 安全生产费存储相关
  433. await this.ctx.service.paymentSafeBills.auditCache(transaction, detailId, {
  434. user_id: audit.id, times: audit.times, order: audit.order,
  435. });
  436. await transaction.update(this.ctx.service.paymentDetail.tableName, updateDetailData);
  437. await transaction.commit();
  438. } catch (err) {
  439. await transaction.rollback();
  440. throw err;
  441. }
  442. }
  443. async _checkNo(pid, detailId, checkData, times) {
  444. const time = new Date();
  445. const detailInfo = await this.ctx.service.paymentDetail.getDataById(detailId);
  446. const trInfo = await this.ctx.service.paymentTenderRpt.getDataById(detailInfo.tr_id);
  447. // 整理当前流程审核人状态更新
  448. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  449. if (!audit) {
  450. throw '审核数据错误';
  451. }
  452. const sql = 'SELECT `tender_id`, `tr_id`, `td_id`, `aid`, `order` FROM ?? WHERE `td_id` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  453. const sqlParam = [this.tableName, detailId, times];
  454. const auditors = await this.db.query(sql, sqlParam);
  455. // 可能更换了上报人且存在于审批流程中,需要删除
  456. const userIndex = this._.findIndex(auditors, { aid: trInfo.uid });
  457. if (userIndex !== -1) {
  458. auditors.splice(userIndex, 1);
  459. }
  460. let order = 1;
  461. for (const a of auditors) {
  462. a.times = times + 1;
  463. a.order = order;
  464. a.status = auditConst.status.uncheck;
  465. order++;
  466. }
  467. const transaction = await this.db.beginTransaction();
  468. try {
  469. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  470. // 清空所有签名数据
  471. let report_json = JSON.parse(this.ctx.detail.report_json);
  472. if (report_json) {
  473. report_json = await this.ctx.service.paymentDetail.clearAllSignatureData(report_json);
  474. }
  475. const detailUpdateData = {
  476. id: detailId, status: checkData.checkType,
  477. times: times + 1,
  478. report_json: JSON.stringify(report_json),
  479. };
  480. // 可能存在更换了上报人的情况,需要替换
  481. if (detailInfo.uid !== trInfo.uid) {
  482. detailUpdateData.uid = trInfo.uid;
  483. }
  484. // 同步期信息
  485. await transaction.update(this.ctx.service.paymentDetail.tableName, detailUpdateData);
  486. // 清空签名
  487. await transaction.update(this.ctx.service.paymentRptAudit.tableName, {
  488. signature_msg: null,
  489. sign_time: null,
  490. }, {
  491. where: {
  492. td_id: detailId,
  493. },
  494. });
  495. // 拷贝新一次审核流程列表
  496. await transaction.insert(this.tableName, auditors);
  497. // 安全生产费存储相关
  498. await this.ctx.service.paymentSafeBills.auditCache(transaction, detailId, {
  499. user_id: audit.id, times: audit.times, order: audit.order,
  500. });
  501. await transaction.commit();
  502. } catch (err) {
  503. await transaction.rollback();
  504. throw err;
  505. }
  506. }
  507. async _checkNoPre(pid, detailId, checkData, times) {
  508. const time = new Date();
  509. // 整理当前流程审核人状态更新
  510. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  511. if (!audit || audit.order <= 1) {
  512. throw '审核数据错误';
  513. }
  514. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  515. const auditors2 = await this.getAuditGroupByList(detailId, times);
  516. const auditorIndex = await auditors2.findIndex(function(item) {
  517. return item.aid === audit.aid;
  518. });
  519. const preAuditor = auditors2[auditorIndex - 1];
  520. const transaction = await this.db.beginTransaction();
  521. try {
  522. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  523. // 顺移气候审核人流程顺序
  524. this.initSqlBuilder();
  525. this.sqlBuilder.setAndWhere('td_id', { value: detailId, operate: '=' });
  526. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  527. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  528. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  529. const data = await transaction.query(sql, sqlParam);
  530. const newAuditors = [];
  531. newAuditors.push({
  532. tender_id: audit.tender_id, tr_id: audit.tr_id, td_id: audit.td_id, aid: preAuditor.aid,
  533. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  534. begin_time: time,
  535. });
  536. newAuditors.push({
  537. tender_id: audit.tender_id, tr_id: audit.tr_id, td_id: audit.td_id, aid: audit.aid,
  538. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  539. });
  540. await transaction.insert(this.tableName, newAuditors);
  541. // 清除本人的签章
  542. await this.ctx.service.paymentRptAudit.clearSignatureMsg(transaction, detailId, audit.aid);
  543. // 安全生产费存储相关
  544. await this.ctx.service.paymentSafeBills.auditCache(transaction, detailId, {
  545. user_id: audit.id, times: audit.times, order: audit.order,
  546. });
  547. await transaction.commit();
  548. } catch (error) {
  549. await transaction.rollback();
  550. throw error;
  551. }
  552. }
  553. /**
  554. * 审批
  555. * @param {Number} materialId - 材料调差期id
  556. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  557. * @param {Number} times - 第几次审批
  558. * @return {Promise<void>}
  559. */
  560. async check(detailId, checkData, times = 1) {
  561. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  562. throw '提交数据错误';
  563. }
  564. const pid = this.ctx.session.sessionProject.id;
  565. switch (checkData.checkType) {
  566. case auditConst.status.checked:
  567. await this._checked(pid, detailId, checkData, times);
  568. break;
  569. case auditConst.status.checkNo:
  570. await this._checkNo(pid, detailId, checkData, times);
  571. break;
  572. case auditConst.status.checkNoPre:
  573. await this._checkNoPre(pid, detailId, checkData, times);
  574. break;
  575. default:
  576. throw '无效审批操作';
  577. }
  578. }
  579. async followAuditByRptAudit(detail) {
  580. const transaction = await this.db.beginTransaction();
  581. try {
  582. const newAuditIds = this._.map(detail.rptAudits, 'uid');
  583. // 去除上报人
  584. if (this._.includes(newAuditIds, detail.uid)) {
  585. this._.pull(newAuditIds, detail.uid);
  586. }
  587. // 如果存在固定终审,判断它是否在报表人员里,存在则把该id移到最后,不存在则插入newAuditIds最后;
  588. if (this.ctx.trInfo.sp_status === shenpiConst.sp_status.gdzs) {
  589. const gdzsInfo = await this.ctx.service.paymentShenpiAudit.getAudit(detail.tender_id, detail.tr_id, shenpiConst.sp_status.gdzs);
  590. if (gdzsInfo && gdzsInfo.audit_id !== newAuditIds[newAuditIds.length - 1]) {
  591. if (this._.includes(newAuditIds, gdzsInfo.audit_id)) {
  592. this._.pull(newAuditIds, gdzsInfo.audit_id);
  593. }
  594. newAuditIds.push(gdzsInfo.audit_id);
  595. }
  596. }
  597. // 删除原审批流
  598. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times });
  599. // 添加新审批流
  600. const insertDatas = [];
  601. let order = 1;
  602. for (const id of newAuditIds) {
  603. insertDatas.push({
  604. tender_id: detail.tender_id,
  605. tr_id: detail.tr_id,
  606. td_id: detail.id,
  607. aid: id,
  608. order,
  609. times: detail.times,
  610. status: auditConst.status.uncheck,
  611. });
  612. order = order + 1;
  613. }
  614. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  615. await transaction.commit();
  616. return true;
  617. } catch (error) {
  618. await transaction.rollback();
  619. throw error;
  620. }
  621. }
  622. /**
  623. * 获取审核人需要审核的期列表
  624. *
  625. * @param auditorId
  626. * @return {Promise<*>}
  627. */
  628. async getAuditPayment(auditorId) {
  629. const sql =
  630. 'SELECT pda.`aid`, pda.`times`, pda.`order`, pda.`begin_time`, pda.`end_time`, pda.`tender_id`, pda.`tr_id`, pda.`td_id`,' +
  631. ' pd.`code` As `scode`, pd.`order` As `sorder`, pd.`status` As `sstatus`, pd.type,' +
  632. ' pr.`rpt_name` As `rpt_name`,' +
  633. ' t.`name`, t.`pid`, t.`uid` ' +
  634. ' FROM ?? AS pda ' +
  635. ' Left Join ?? AS pd On pda.`td_id` = pd.`id` ' +
  636. ' Left Join ?? AS pr On pda.`tr_id` = pr.`id` ' +
  637. ' Left Join ?? As t ON pda.`tender_id` = t.`id`' +
  638. ' WHERE ((pda.`aid` = ? and pda.`status` = ?) OR (pd.`uid` = ? and pda.`status` = ? and pd.`status` = ? and pda.`times` = (pd.`times`-1)))' +
  639. ' ORDER BY pda.`begin_time` DESC';
  640. const sqlParam = [
  641. this.tableName,
  642. this.ctx.service.paymentDetail.tableName,
  643. this.ctx.service.paymentTenderRpt.tableName,
  644. this.ctx.service.paymentTender.tableName,
  645. auditorId,
  646. auditConst.status.checking,
  647. auditorId,
  648. auditConst.status.checkNo,
  649. auditConst.status.checkNo,
  650. ];
  651. return await this.db.query(sql, sqlParam);
  652. }
  653. async updateAuditByReport(transaction, td_id, times, uid) {
  654. const auditor = await this.getDataByCondition({ td_id, times, aid: uid });
  655. if (auditor) {
  656. // 更新order
  657. await this._syncOrderByDelete(transaction, td_id, auditor.order, times);
  658. return await transaction.delete(this.tableName, { id: auditor.id });
  659. }
  660. }
  661. }
  662. return PaymentDetailAudit;
  663. };