payment_detail_audit.js 33 KB

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