phase_pay_audit.js 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. 'use strict';
  2. /**
  3. * 与期不同,含原报
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const auditType = auditConst.auditType;
  11. const pushType = auditConst.pushType;
  12. const smsTypeConst = require('../const/sms_type');
  13. const wxConst = require('../const/wechat_template');
  14. module.exports = app => {
  15. class PhasePayAudit extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'phase_pay_audit';
  25. }
  26. // ***** 查询审批人相关
  27. // 获取全部参与人
  28. async getAuditors(phaseId, auditTimes) {
  29. return await this.getAllDataByCondition({ where: { phase_id: phaseId, audit_times: auditTimes }, orders: [['active_order', 'asc']] }); // 全部参与的审批人
  30. }
  31. // 获取全部参与人 分组
  32. async getAuditorGroup(phaseId, auditTimes) {
  33. const auditors = await this.getAuditors(phaseId, auditTimes); // 全部参与的审批人
  34. return this.ctx.helper.groupAuditors(auditors, 'active_order');
  35. }
  36. // 获取全部参与人 去重
  37. async getUniqAuditors(phasePay) {
  38. const auditors = await this.getAuditors(phasePay.id, phasePay.audit_times); // 全部参与的审批人
  39. const result = [];
  40. auditors.forEach(x => {
  41. if (result.findIndex(r => { return x.audit_id === r.audit_id && x.audit_order === x.audit_order; }) < 0) {
  42. result.push(x);
  43. }
  44. });
  45. return result;
  46. }
  47. // 获取全部参与人 分组 去重
  48. async getUniqAuditorsGroup(phaseId, auditTimes) {
  49. const group = await this.getAuditorGroup(phaseId, auditTimes);
  50. return this.ctx.helper.groupAuditorsUniq(group);
  51. }
  52. // 获取某个状态的审批人
  53. async getAuditorsByStatus(phaseId, auditStatus, auditTimes) {
  54. const cur = await this.db.queryOne(`SELECT * From ${this.tableName} where phase_id = ? AND audit_times = ? AND audit_status = ? ORDER By audit_times DESC, active_order DESC `, [phaseId, auditTimes, auditStatus]);
  55. if (!cur) return [];
  56. return await this.getAllDataByCondition({ where: { phase_id: phaseId, audit_times: auditTimes, audit_order: cur.audit_order, audit_status: auditStatus}});
  57. }
  58. // 获取全部审批历史
  59. async getAuditorHistory(phaseId, auditTimes, reverse = false) {
  60. const history = [];
  61. if (auditTimes >= 1) {
  62. for (let i = 1; i <= auditTimes; i++) {
  63. const auditors = await this.getAuditors(phaseId, i);
  64. const group = this.ctx.helper.groupAuditors(auditors);
  65. const historyGroup = [];
  66. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  67. for (const g of group) {
  68. const his = {
  69. auditYear: '', auditDate: '', auditTime: '', audit_time: null,
  70. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  71. auditors: g
  72. };
  73. his.is_final = his.audit_order === max_order;
  74. his.auditName = his.audit_order === 0 ? '原报' : (his.is_final ? '终审' : his.audit_order + '审');
  75. his.auditCnName = his.audit_order === 0 ? '原报' : (his.is_final ? '终审' : this.ctx.helper.transFormToChinese(his.audit_order) + '审');
  76. his.name = his.audit_type === auditType.key.common ? g[0].name : his.auditName;
  77. let audit_time;
  78. g.forEach(x => {
  79. if (x.audit_status === auditConst.phasePay.status.checkSkip) return;
  80. if (!his.audit_status || x.audit_status === auditConst.phasePay.status.checking) his.audit_status = x.audit_status;
  81. if (x.audit_time && (!audit_time || x.audit_time > audit_time)) {
  82. audit_time = x.audit_time;
  83. if (his.audit_status !== auditConst.phasePay.status.checking) his.audit_status = x.audit_status;
  84. }
  85. });
  86. if (audit_time) {
  87. his.audit_time = audit_time;
  88. const auditTime = this.ctx.moment(audit_time);
  89. his.auditYear = auditTime.format('YYYY');
  90. his.auditDate = auditTime.format('MM-DD');
  91. his.auditTime = auditTime.format('HH:mm:ss');
  92. }
  93. historyGroup.push(his);
  94. }
  95. if (reverse) {
  96. history.push(historyGroup.reverse());
  97. } else {
  98. history.push(historyGroup);
  99. }
  100. }
  101. }
  102. return history;
  103. }
  104. async getAllAuditors(tenderId) {
  105. const sql =
  106. 'SELECT audit_id, tid FROM ' + this.tableName +
  107. ' WHERE tid = ?' +
  108. ' GROUP BY audit_id';
  109. const sqlParam = [tenderId];
  110. return this.db.query(sql, sqlParam);
  111. }
  112. // ***** 查询审批人相关
  113. // ***** 修改审批人相关
  114. /**
  115. * 获取 最新审核顺序
  116. *
  117. * @param {Number} phaseId - 期id
  118. * @param {Number} auditTimes - 第几次审批
  119. * @return {Promise<number>}
  120. */
  121. async getNewOrder(phaseId, auditTimes = 1) {
  122. const sql = `SELECT Max(active_order) As max_order, Max(audit_order) As max_audit_order FROM ${this.tableName} Where phase_id = ? and audit_times = ?`;
  123. const result = await this.db.queryOne(sql, [phaseId, auditTimes]);
  124. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  125. }
  126. /**
  127. * 同步审核人order
  128. * @param transaction - 事务
  129. * @param {Number} phaseId - 结算期id
  130. * @param {Number} auditOrder - 审核顺序(实际顺序)
  131. * @param {Number} auditTimes - 第几次审批
  132. * @param {String} selfOperate - 操作符(+/-)
  133. * @return {Promise<*>}
  134. * @private
  135. */
  136. async _syncAuditOrder(transaction, phaseId, auditOrder, auditTimes, selfOperate = '-') {
  137. const sql = `Update ${this.tableName} SET audit_order = audit_order${selfOperate}1, active_order = active_order${selfOperate}1
  138. WHERE phase_id = ? and audit_times = ? and active_order >= ?`;
  139. return await transaction.query(sql, [phaseId, auditTimes, auditOrder]);
  140. }
  141. /**
  142. * 新增审核人
  143. *
  144. * @param {Number} phaseId - 期id
  145. * @param {Number} auditor - 审核人
  146. * @param {Number} auditTimes - 第几次审批
  147. * @param {Number} is_gdzs - 是否固定终审
  148. * @return {Promise<number>}
  149. */
  150. async addAuditor(phaseId, auditor, auditTimes = 1, is_gdzs = 0) {
  151. let [newOrder, newAuditOrder] = await this.getNewOrder(phaseId, auditTimes);
  152. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  153. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  154. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  155. const transaction = await this.db.beginTransaction();
  156. try {
  157. if (is_gdzs) await this._syncAuditOrder(transaction, phaseId, newOrder, auditTimes, '+');
  158. const data = {
  159. tid: this.ctx.tender.id, phase_id: phaseId, audit_id: auditor.id,
  160. name: auditor.name, company: auditor.company, role: auditor.role, mobile: auditor.mobile,
  161. audit_times: auditTimes, active_order: newOrder, audit_status: auditConst.phasePay.status.uncheck,
  162. audit_order: newAuditOrder, audit_type: auditType.key.common,
  163. };
  164. const result = await transaction.insert(this.tableName, data);
  165. await transaction.commit();
  166. return result.effectRows = 1;
  167. } catch (err) {
  168. await transaction.rollback();
  169. throw err;
  170. }
  171. return false;
  172. }
  173. /**
  174. * 移除审核人
  175. *
  176. * @param {Number} phaseId - 期id
  177. * @param {Number} auditorId - 审核人id
  178. * @param {Number} auditTimes - 第几次审批
  179. * @return {Promise<boolean>}
  180. */
  181. async deleteAuditor(phaseId, auditorId, auditTimes = 1) {
  182. const transaction = await this.db.beginTransaction();
  183. try {
  184. const condition = { phase_id: phaseId, audit_id: auditorId, audit_times: auditTimes };
  185. const auditor = await this.getDataByCondition(condition);
  186. if (!auditor) throw '审批人不存在';
  187. // 移除整个流程的人
  188. await transaction.delete(this.tableName, { phase_id: phaseId, active_order: auditor.active_order, audit_times: auditTimes});
  189. await this._syncAuditOrder(transaction, phaseId, auditor.active_order, auditTimes);
  190. await transaction.commit();
  191. } catch (err) {
  192. await transaction.rollback();
  193. throw err;
  194. }
  195. return true;
  196. }
  197. // 拷贝上一期审批流程
  198. async copyPreAuditors(transaction, prePhasePay, newPhasePay) {
  199. const auditors = prePhasePay ? await this.getUniqAuditors(prePhasePay) : [];
  200. const newAuditors = [];
  201. // 添加原报
  202. const user = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  203. newAuditors.push({
  204. tid: newPhasePay.tid, phase_id: newPhasePay.id,
  205. audit_id: this.ctx.session.sessionUser.accountId,
  206. audit_times: 1, audit_order: 0, audit_type: auditConst.auditType.key.common,
  207. active_order: 0, audit_status: auditConst.phasePay.status.uncheck,
  208. name: user.name, company: user.company, role: user.role, mobile: user.mobile,
  209. });
  210. // 添加其他参与人
  211. for (const a of auditors) {
  212. if (a.audit_order === 0) continue;
  213. newAuditors.push({
  214. tid: newPhasePay.tid, phase_id: newPhasePay.id,
  215. audit_id: a.audit_id,
  216. audit_times: 1, audit_order: a.audit_order, audit_type: a.audit_type,
  217. active_order: a.audit_order, audit_status: auditConst.phasePay.status.uncheck,
  218. name: a.name, company: a.company, role: a.role, mobile: a.mobile,
  219. });
  220. }
  221. const result = await transaction.insert(this.tableName, newAuditors);
  222. if (result.affectedRows !== newAuditors.length) throw '初始化审批流程错误';
  223. }
  224. // 固定审批流-更新
  225. async updateNewAuditList(phasePay, newList) {
  226. const newAuditsInfo = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: newList.map(x => { return x.audit_id; })} });
  227. const transaction = await this.db.beginTransaction();
  228. try {
  229. // 先删除旧的审批流,再添加新的
  230. await transaction.query(`DELETE FROM ${this.tableName} where phase_id = ? and audit_times = ? and audit_order > 0`, [phasePay.id, phasePay.audit_times]);
  231. const newAuditors = [];
  232. for (const auditor of newList) {
  233. const auditorInfo = newAuditsInfo.find(x => { return x.id === auditor.audit_id; });
  234. if (!auditorInfo) throw '配置的审批人不存在';
  235. newAuditors.push({
  236. tid: phasePay.tid, phase_id: phasePay.id, audit_id: auditor.audit_id,
  237. name: auditorInfo.name, company: auditorInfo.company, role: auditorInfo.role, mobile: auditorInfo.mobile,
  238. audit_times: phasePay.audit_times, active_order: auditor.audit_order, audit_status: auditConst.phasePay.status.uncheck,
  239. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  240. });
  241. }
  242. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  243. await transaction.commit();
  244. } catch (err) {
  245. await transaction.rollback();
  246. throw err;
  247. }
  248. }
  249. // 固定终审-更新
  250. async updateLastAudit(phasePay, auditList, lastId) {
  251. const lastUser = await this.ctx.service.projectAccount.getDataById(lastId);
  252. const transaction = await this.db.beginTransaction();
  253. try {
  254. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  255. const existAudit = auditList.find(x => { return x.audit_id === lastId });
  256. let auditOrder = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.active_order)}, 0) + 1 : 1; // 最大值 + 1
  257. if (existAudit) {
  258. await transaction.delete(this.tableName, { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_id: lastId });
  259. const sameOrder = auditList.filter(x => { return x.active_order === existAudit.active_order });
  260. if (sameOrder.length === 1) {
  261. const updateData = [];
  262. auditList.forEach(x => {
  263. if (x.active_order <= existAudit.active_order) return;
  264. updateData.push({id: x.id, active_order: x.active_order - 1, audit_order: x.audit_order - 1});
  265. });
  266. if (updateData.length > 0) {
  267. await transaction.updateRows(updateData);
  268. }
  269. auditOrder = auditOrder - 1;
  270. }
  271. }
  272. // 添加终审
  273. const newAuditor = {
  274. tid: phasePay.tid, phase_id: phasePay.id, audit_id: lastId,
  275. name: lastUser.name, company: lastUser.company, role: lastUser.role, mobile: lastUser.mobile,
  276. audit_times: phasePay.audit_times, active_order: auditOrder, audit_status: auditConst.phasePay.status.uncheck,
  277. audit_type: auditType.key.common, audit_order: auditOrder,
  278. };
  279. await transaction.insert(this.tableName, newAuditor);
  280. await transaction.commit();
  281. } catch (err) {
  282. await transaction.rollback();
  283. throw err;
  284. }
  285. }
  286. // ***** 修改审批人相关
  287. // ***** 审批操作
  288. /**
  289. * 用于添加推送所需的content内容
  290. * @param {Number} pid 项目id
  291. * @param {Number} tid 台账id
  292. * @param {Number} sid 期id
  293. * @param {Number} uid 审核人id
  294. */
  295. async _getNoticeContent(pid, tid, sid, uid, opinion = '') {
  296. const noticeSql =
  297. 'SELECT * FROM (SELECT ' +
  298. ' t.`id` As `tid`, t.`name`, s.`phase_order` as `order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  299. ` FROM (SELECT * FROM ${this.ctx.service.tender.tableName} WHERE id = ? ) As t` +
  300. ` LEFT JOIN ${this.ctx.service.phasePay.tableName} As s On s.id = ?` +
  301. ` LEFT JOIN ${this.ctx.service.projectAccount.tableName} As pa ON pa.id = ?` +
  302. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  303. const noticeSqlParam = [tid, sid, uid, pid];
  304. const content = await this.db.query(noticeSql, noticeSqlParam);
  305. if (content.length) {
  306. content[0].opinion = opinion;
  307. }
  308. return content.length ? JSON.stringify(content[0]) : '';
  309. }
  310. async start(phasePay) {
  311. const audits = await this.getAllDataByCondition({ where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_order: 1 } });
  312. if (audits.length === 0) {
  313. if(this.ctx.tender.info.shenpi.phasePay === shenpiConst.sp_status.gdspl) {
  314. throw '请联系管理员添加审批人';
  315. } else {
  316. throw '请先选择审批人,再上报数据';
  317. }
  318. }
  319. const transaction = await this.db.beginTransaction();
  320. try {
  321. const audit_time = new Date();
  322. // 更新原报数据
  323. await transaction.update(this.tableName, { audit_status: auditConst.phasePay.status.checked, audit_time },
  324. { where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_order: 0 } });
  325. // 更新下一审批人数据
  326. const updateData = audits.map(x => { return { id: x.id, audit_status: auditConst.phasePay.status.checking } });
  327. await transaction.updateRows(this.tableName, updateData);
  328. // 计算合同支付
  329. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  330. await transaction.update(this.ctx.service.phasePay.tableName, {
  331. id: phasePay.id, audit_status: auditConst.phasePay.status.checking, audit_max_sort: 1, audit_begin_time: audit_time, ...paySum
  332. });
  333. // 拷贝新流程合同支付
  334. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times, 1);
  335. // todo 添加短信通知-需要审批提醒功能
  336. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + phasePay.tid + '/phasePay/' + phasePay.phase_order);
  337. const users = this._.map(audits, 'audit_id');
  338. // 微信模板通知
  339. const wechatData = {
  340. wap_url: shenpiUrl,
  341. qi: phasePay.phase_order,
  342. status: wxConst.status.check,
  343. tips: wxConst.tips.check,
  344. code: this.ctx.session.sessionProject.code,
  345. };
  346. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.approval.toString(), wxConst.template.phasePay, wechatData);
  347. for (const a of audits) {
  348. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.PAY, {
  349. pid: this.ctx.session.sessionProject.id,
  350. tid: phasePay.tid,
  351. uid: a.audit_id,
  352. sp_type: 'phasePay',
  353. sp_id: a.id,
  354. table_name: this.tableName,
  355. template: wxConst.template.phasePay,
  356. wx_data: wechatData,
  357. });
  358. }
  359. // todo 上报/审批 - 检查三方特殊推送
  360. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  361. await transaction.commit();
  362. } catch (err) {
  363. await transaction.rollback();
  364. throw err;
  365. }
  366. }
  367. async _checked(phasePay, opinion) {
  368. const accountId = this.ctx.session.sessionUser.accountId;
  369. const time = new Date();
  370. const selfAudit = phasePay.curAuditors.find(x => { return x.audit_id === accountId; });
  371. if (!selfAudit) throw '当前标段您无权审批';
  372. const nextAudits = await this.getAllDataByCondition({ where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, active_order: selfAudit.active_order + 1 } });
  373. const transaction = await this.db.beginTransaction();
  374. try {
  375. // 添加通知
  376. const noticeContent = await this._getNoticeContent(this.ctx.session.sessionProject.id, phasePay.tid, phasePay.id, accountId, opinion);
  377. const defaultNoticeRecord = {
  378. pid: this.ctx.session.sessionProject.id,
  379. type: pushType.phasePay,
  380. status: auditConst.phasePay.status.checked,
  381. content: noticeContent,
  382. };
  383. const records = phasePay.userIds.map(x => {
  384. return { uid: x, ...defaultNoticeRecord };
  385. });
  386. await transaction.insert('zh_notice', records);
  387. // 更新本人审批状态
  388. await transaction.update(this.tableName, {
  389. id: selfAudit.id,
  390. audit_status: auditConst.phasePay.status.checked, opinion: opinion,
  391. audit_time: time,
  392. });
  393. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  394. // 计算合同支付
  395. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  396. if (phasePay.curAuditors.length === 1 || selfAudit.audit_type === auditType.key.or) {
  397. // 或签更新他人审批状态
  398. if (selfAudit.audit_type === auditType.key.or) {
  399. const updateOther = [];
  400. for (const audit of phasePay.curAuditors) {
  401. if (audit.audit_id === selfAudit.audit_id) continue;
  402. updateOther.push({
  403. id: audit.id,
  404. audit_status: auditConst.phasePay.status.checkSkip,
  405. opinion: '',
  406. audit_time: time,
  407. });
  408. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  409. }
  410. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  411. }
  412. // 无下一审核人表示,审核结束
  413. if (nextAudits.length > 0) {
  414. // 流程至下一审批人
  415. const updateData = nextAudits.map(x => { return { id: x.id, audit_status: auditConst.phasePay.status.checking }; });
  416. await transaction.updateRows(this.tableName, updateData);
  417. // 计算合同支付
  418. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  419. await transaction.update(this.ctx.service.phasePay.tableName, {
  420. id: phasePay.id, audit_max_sort: selfAudit.active_order + 1, audit_status: auditConst.phasePay.status.checking, ...paySum
  421. });
  422. // 拷贝新流程合同支付
  423. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times, selfAudit.active_order + 1);
  424. // todo 添加短信通知-需要审批提醒功能
  425. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/phasePay/' + phasePay.phase_order);
  426. const users = this._.map(nextAudits, 'audit_id');
  427. // 微信模板通知
  428. const wechatData = {
  429. wap_url: shenpiUrl,
  430. qi: phasePay.phase_order,
  431. status: wxConst.status.check,
  432. tips: wxConst.tips.check,
  433. code: this.ctx.session.sessionProject.code,
  434. };
  435. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.approval.toString(), wxConst.template.phasePay, wechatData);
  436. for (const a of nextAudits) {
  437. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.PAY, {
  438. pid: this.ctx.session.sessionProject.id,
  439. tid: this.ctx.tender.id,
  440. uid: a.audit_id,
  441. sp_type: 'phasePay',
  442. sp_id: a.id,
  443. table_name: this.tableName,
  444. template: wxConst.template.phasePay,
  445. wx_data: wechatData,
  446. });
  447. }
  448. } else {
  449. const final_auditor_str = (selfAudit.audit_type === auditType.key.common)
  450. ? `${selfAudit.name}${(selfAudit.role ? '-' + selfAudit.role : '')}`
  451. : ctx.helper.transFormToChinese(selfAudit.audit_order) + '审';
  452. await transaction.update(this.ctx.service.phasePay.tableName, {
  453. id: phasePay.id, audit_end_time: time, audit_status: auditConst.phasePay.status.checked, final_auditor_str, ...paySum
  454. });
  455. // 添加短信通知-审批通过提醒功能
  456. const users = this._.uniq(phasePay.userIds);
  457. // 微信模板通知
  458. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/phasePay/' + phasePay.phase_order);
  459. const wechatData = {
  460. wap_url: shenpiUrl,
  461. qi: phasePay.phase_order,
  462. status: wxConst.status.success,
  463. tips: wxConst.tips.success,
  464. code: this.ctx.session.sessionProject.code,
  465. };
  466. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.result.toString(), wxConst.template.phasePay, wechatData);
  467. // todo 审批通过 - 检查三方特殊推送
  468. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.checked);
  469. }
  470. // todo 上报/审批 - 检查三方特殊推送
  471. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  472. } else {
  473. // 同步 期信息
  474. await transaction.update(this.ctx.service.phasePay.tableName, { id: phasePay.id, audit_status: auditConst.phasePay.status.checking, ...paySum });
  475. }
  476. await transaction.commit();
  477. } catch (err) {
  478. await transaction.rollback();
  479. throw err;
  480. }
  481. }
  482. async _checkNo(phasePay, opinion) {
  483. const pid = this.ctx.session.sessionProject.id;
  484. const accountId = this.ctx.session.sessionUser.accountId;
  485. const time = new Date();
  486. // 整理当前流程审核人状态更新
  487. const selfAudit = phasePay.curAuditors.find(x => { return x.audit_id === accountId; });
  488. if (!selfAudit) throw '当前标段您无权审批';
  489. const auditors = await this.getUniqAuditors(phasePay); // 全部参与的审批人
  490. const newAuditors = auditors.map(x => {
  491. return {
  492. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  493. audit_times: phasePay.audit_times + 1, audit_order: x.audit_order, audit_type: x.audit_type,
  494. active_order: x.audit_order, audit_status: auditConst.phasePay.status.uncheck,
  495. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  496. }
  497. });
  498. const transaction = await this.db.beginTransaction();
  499. try {
  500. // 添加提醒
  501. const noticeContent = await this._getNoticeContent(pid, selfAudit.tid, phasePay.id, selfAudit.audit_id, opinion);
  502. const defaultNoticeRecord = { pid, type: pushType.phasePay, status: auditConst.phasePay.status.checkNo, content: noticeContent };
  503. const records = phasePay.userIds.map(x => {
  504. return { uid: x, ...defaultNoticeRecord };
  505. });
  506. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  507. // 更新审批人状态数据
  508. const updateData = [];
  509. phasePay.curAuditors.forEach(x => {
  510. if (x.audit_id === selfAudit.audit_id) {
  511. updateData.push({ id: x.id, audit_status: auditConst.phasePay.status.checkNo, opinion: opinion, audit_time: time});
  512. } else {
  513. updateData.push({ id: x.id, audit_status: auditConst.phasePay.status.checkSkip, opinion: '', audit_time: null});
  514. }
  515. });
  516. await transaction.updateRows(this.tableName, updateData);
  517. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  518. // 更新 期信息
  519. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  520. await transaction.update(this.ctx.service.phasePay.tableName, {
  521. id: phasePay.id, audit_status: auditConst.phasePay.status.checkNo, audit_times: phasePay.audit_times + 1, audit_max_sort: 0, ...paySum
  522. });
  523. // 拷贝新流程合同支付
  524. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times + 1, 0);
  525. // 拷贝新一次审核流程列表
  526. await transaction.insert(this.tableName, newAuditors);
  527. // todo 添加短信通知-审批退回提醒功能
  528. const users = this._.uniq(phasePay.userIds);
  529. // 微信模板通知
  530. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + phasePay.tid + '/phasePay/' + phasePay.phase_order);
  531. const wechatData = {
  532. wap_url: shenpiUrl,
  533. qi: phasePay.phase_order,
  534. status: wxConst.status.back,
  535. tips: wxConst.tips.back,
  536. code: this.ctx.session.sessionProject.code,
  537. };
  538. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.result.toString(), wxConst.template.phasePay, wechatData);
  539. // todo 上报/审批 - 检查三方特殊推送
  540. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  541. await transaction.commit();
  542. } catch (err) {
  543. await transaction.rollback();
  544. throw err;
  545. }
  546. }
  547. async _checkNoPre(phasePay, opinion) {
  548. const pid = this.ctx.session.sessionProject.id;
  549. const accountId = this.ctx.session.sessionUser.accountId;
  550. const time = new Date();
  551. // 整理当前流程审核人状态更新
  552. const selfAudit = phasePay.curAuditors.find(x => { return x.audit_id === accountId; });
  553. if (!selfAudit) throw '当前标段您无权审批';
  554. const preAuditors = phasePay.userGroups.find(x => { return x[0].audit_order === selfAudit.audit_order - 1; });
  555. const transaction = await this.db.beginTransaction();
  556. try {
  557. // 添加通知
  558. const noticeContent = await this._getNoticeContent(pid, phasePay.tid, phasePay.id, selfAudit.audit_id, opinion);
  559. const defaultNoticeRecord = {
  560. pid, type: pushType.phasePay, status: auditConst.phasePay.status.checkNoPre, content: noticeContent,
  561. };
  562. const records = phasePay.userIds.map(x => {
  563. return { uid: x, ...defaultNoticeRecord };
  564. });
  565. await transaction.insert('zh_notice', records);
  566. // 更新同一流程所有审批人状态
  567. const updateData = [];
  568. for (const audit of phasePay.curAuditors) {
  569. if (audit.audit_id === selfAudit.audit_id) {
  570. updateData.push({
  571. id: audit.id, audit_status: auditConst.phasePay.status.checkNoPre, opinion, audit_time: time,
  572. });
  573. } else {
  574. updateData.push({
  575. id: audit.id, audit_status: auditConst.phasePay.status.checkSkip, opinion: '', audit_time: null,
  576. });
  577. }
  578. }
  579. await transaction.updateRows(this.tableName, updateData);
  580. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  581. // 顺移其后审核人流程顺序
  582. const sql = 'UPDATE ' + this.tableName + ' SET `active_order` = `active_order` + 2 WHERE phase_id = ? AND audit_times = ? AND `active_order` > ?';
  583. await transaction.query(sql, [phasePay.id, selfAudit.audit_times, selfAudit.active_order]);
  584. // 上一审批人,当前审批人 再次添加至流程
  585. const newAuditors = [], uncheckAuditors = [];
  586. preAuditors.forEach(x => {
  587. newAuditors.push({
  588. tid: x.tid, phase_id: x.phase_id, audit_id: x.audit_id,
  589. audit_times: x.audit_times, audit_order: selfAudit.audit_order - 1,
  590. audit_status: auditConst.phasePay.status.checking,
  591. audit_type: x.audit_type, active_order: selfAudit.active_order + 1,
  592. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  593. });
  594. });
  595. const checkingAuditors_result = await transaction.insert(this.tableName, newAuditors);
  596. // 获取刚批量添加的所有list
  597. for (let j = 0; j < newAuditors.length; j++) {
  598. newAuditors[j].id = checkingAuditors_result.insertId + j;
  599. }
  600. phasePay.flowAuditors.forEach(x => {
  601. uncheckAuditors.push({
  602. tid: x.tid, phase_id: x.phase_id, audit_id: x.audit_id,
  603. audit_times: x.audit_times, active_order: selfAudit.active_order + 2,
  604. audit_status: auditConst.phasePay.status.uncheck,
  605. audit_type: x.audit_type, audit_order: x.audit_order,
  606. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  607. });
  608. });
  609. await transaction.insert(this.tableName, uncheckAuditors);
  610. const preAuditorIds = preAuditors.map(x => { return x.audit_id; });
  611. // 同步 期信息
  612. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  613. await transaction.update(this.ctx.service.phasePay.tableName, {
  614. id: phasePay.id, audit_max_sort: selfAudit.active_order + 1, ...paySum,
  615. });
  616. // 拷贝新流程合同支付
  617. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times, selfAudit.active_order + 1);
  618. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + phasePay.tid + '/phasePay/' + phasePay.phase_order);
  619. const users = preAuditorIds;
  620. // 微信模板通知
  621. const wechatData = {
  622. wap_url: shenpiUrl,
  623. qi: phasePay.phase_order,
  624. status: wxConst.status.check,
  625. tips: wxConst.tips.check,
  626. code: this.ctx.session.sessionProject.code,
  627. };
  628. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.approval.toString(), wxConst.template.phasePay, wechatData);
  629. for (const a of newAuditors) {
  630. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.PAY, {
  631. pid: this.ctx.session.sessionProject.id,
  632. tid: phasePay.tid,
  633. uid: a.audit_id,
  634. sp_type: 'phasePay',
  635. sp_id: a.id,
  636. table_name: this.tableName,
  637. template: wxConst.template.phasePay,
  638. wx_data: wechatData,
  639. });
  640. }
  641. // todo 上报/审批 - 检查三方特殊推送
  642. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  643. await transaction.commit();
  644. } catch (err) {
  645. await transaction.rollback();
  646. throw err;
  647. }
  648. }
  649. async check(phasePay, checkType, opinion = '') {
  650. switch (checkType) {
  651. case auditConst.phasePay.status.checked:
  652. await this._checked(phasePay, opinion);
  653. break;
  654. case auditConst.phasePay.status.checkNo:
  655. await this._checkNo(phasePay, opinion);
  656. break;
  657. case auditConst.phasePay.status.checkNoPre:
  658. await this._checkNoPre(phasePay, opinion);
  659. break;
  660. default:
  661. throw '无效审批操作';
  662. }
  663. }
  664. async checkAgain(phasePay, force = false) {
  665. const accountId = this.ctx.session.sessionUser.accountId;
  666. const time = new Date();
  667. // 整理当前流程审核人状态更新
  668. const finalAudits = phasePay.auditorGroups[phasePay.auditorGroups.length - 1];
  669. if (!finalAudits || finalAudits.length === 0 || finalAudits[0].audit_order < 1) throw '审核数据错误';
  670. const selfAudit = finalAudits.find(x => { return x.audit_id === accountId; });
  671. if (!selfAudit && !force) throw '当前标段您无权审批';
  672. const finalAudit = selfAudit || finalAudits[0];
  673. const transaction = await this.db.beginTransaction();
  674. try {
  675. // 当前审批人2次添加至流程中
  676. const checkAgainAuditors = [], checkingAuditors = [];
  677. finalAudits.forEach(x => {
  678. checkAgainAuditors.push({
  679. tid: x.tid, phase_id: x.phase_id, audit_id: x.audit_id,
  680. audit_type: x.audit_type, audit_order: x.audit_order,
  681. audit_times: x.audit_times, active_order: x.active_order + 1,
  682. audit_status: x.audit_id === finalAudit.audit_id ? auditConst.phasePay.status.checkAgain : auditConst.phasePay.status.checkSkip,
  683. audit_time: time, opinion: '',
  684. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  685. });
  686. });
  687. finalAudits.forEach(x => {
  688. checkingAuditors.push({
  689. tid: x.tid, phase_id: x.phase_id, audit_id: x.audit_id,
  690. audit_type: x.audit_type, audit_order: x.audit_order,
  691. audit_times: x.audit_times, active_order: x.active_order + 2,
  692. audit_status: auditConst.phasePay.status.checking, audit_time: time, opinion: '',
  693. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  694. });
  695. });
  696. await transaction.insert(this.tableName, checkAgainAuditors);
  697. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  698. // 获取刚批量添加的所有list
  699. for (let j = 0; j < checkingAuditors.length; j++) {
  700. checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  701. }
  702. // 同步 期信息
  703. await transaction.update(this.ctx.service.phasePay.tableName, {
  704. id: phasePay.id, audit_status: auditConst.phasePay.status.checking, audit_end_time: null, final_auditor_str: '', audit_max_sort: finalAudit.active_order + 2,
  705. });
  706. // 拷贝新流程合同支付
  707. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times + 1, finalAudit.active_order + 2);
  708. // todo 添加短信通知-需要审批提醒功能
  709. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + phasePay.tid + '/phasePay/' + phasePay.phase_order);
  710. const users = this._.map(phasePay.auditAssists.filter(x => { return phasePay.finalAuditorIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  711. users.push(...phasePay.finalAuditorIds);
  712. // 微信模板通知
  713. const wechatData = {
  714. wap_url: shenpiUrl,
  715. qi: phasePay.phase_order,
  716. status: wxConst.status.check,
  717. tips: wxConst.tips.check,
  718. code: this.ctx.session.sessionProject.code,
  719. };
  720. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.approval.toString(), wxConst.template.phasePay, wechatData);
  721. for (const a of checkingAuditors) {
  722. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.PAY, {
  723. pid: this.ctx.session.sessionProject.id,
  724. tid: phasePay.tid,
  725. uid: a.audit_id,
  726. sp_type: 'phasePay',
  727. sp_id: a.id,
  728. table_name: this.tableName,
  729. template: wxConst.template.phasePay,
  730. wx_data: wechatData,
  731. });
  732. }
  733. // todo 上报/审批 - 检查三方特殊推送
  734. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  735. await transaction.commit();
  736. } catch (err) {
  737. await transaction.rollback();
  738. throw err;
  739. }
  740. }
  741. /**
  742. * 原报撤回,直接改动审批人状态
  743. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  744. *
  745. * 一审 1 A checking -> A uncheck status改 pay:1->0 删1
  746. * ...
  747. *
  748. * @param phasePay
  749. * @returns {Promise<void>}
  750. * @private
  751. */
  752. async _userCheckCancel(phasePay) {
  753. const transaction = await this.db.beginTransaction();
  754. try {
  755. // 整理当前流程审核人状态更新
  756. // 审批人变成待审批状态
  757. const updateData = phasePay.curAuditors.map(x => {
  758. return {
  759. id: x.id, audit_status: auditConst.phasePay.status.uncheck,
  760. audit_time: null, opinion: '',
  761. }
  762. });
  763. await transaction.updateRows(this.tableName, updateData);
  764. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  765. await transaction.update(this.ctx.service.phasePay.tableName, {
  766. id: phasePay.id, audit_times: phasePay.audit_times, audit_max_sort: 0,
  767. audit_status: phasePay.audit_times === 1 ? auditConst.phasePay.status.uncheck : auditConst.phasePay.status.checkNo,
  768. });
  769. await transaction.delete(this.ctx.service.phasePayDetail.tableName,
  770. { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_sort: 0});
  771. await transaction.update(this.ctx.service.phasePayDetail.tableName,
  772. { audit_sort: 0, master_id: phasePay.id + '-' + phasePay.audit_times + '-0' },
  773. { where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_sort: 1 } });
  774. // todo 上报/审批 - 检查三方特殊推送
  775. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  776. await transaction.commit();
  777. } catch(err) {
  778. await transaction.rollback();
  779. throw err;
  780. }
  781. }
  782. /**
  783. * 审批人撤回审批通过,插入两条数据
  784. *
  785. * @param phasePay
  786. * @returns {Promise<void>}
  787. * @private
  788. */
  789. async _auditCheckCancel(phasePay) {
  790. if (phasePay.curAuditors.length === 0 || phasePay.curAuditors[0].active_order <= 1) {
  791. throw '撤回用户数据错误';
  792. }
  793. const accountId = this.ctx.session.sessionUser.accountId;
  794. const selfAuditor = phasePay.preAuditors.find(x => { return x.audit_id === accountId; });
  795. if (!selfAuditor) throw '撤回用户数据错误';
  796. const time = new Date();
  797. const transaction = await this.db.beginTransaction();
  798. try {
  799. // 整理当前流程审核人状态更新
  800. // 顺移其后审核人流程顺序
  801. const sql = 'UPDATE ' + this.tableName + ' SET `active_order` = `active_order` + 2 WHERE phase_id = ? AND audit_times = ? AND `active_order` > ?';
  802. await transaction.query(sql, [phasePay.id, selfAuditor.audit_times, phasePay.curAuditors[0].active_order]);
  803. // 当前审批人2次添加至流程中
  804. const checkCancelAuditors = [], checkingAuditors = [];
  805. phasePay.preAuditors.forEach(x => {
  806. checkCancelAuditors.push({
  807. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  808. audit_times: x.audit_times, active_order: x.active_order + 1,
  809. audit_type: x.audit_type, audit_order: x.audit_order,
  810. audit_status: x.audit_id === selfAuditor.audit_id ? auditConst.phasePay.status.checkCancel : auditConst.phasePay.status.checkSkip,
  811. audit_time: time, opinion: '',
  812. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  813. });
  814. });
  815. phasePay.preAuditors.forEach(x => {
  816. checkingAuditors.push({
  817. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  818. audit_times: x.audit_times, active_order: x.active_order + 2,
  819. audit_type: x.audit_type, audit_order: x.audit_order,
  820. audit_status: auditConst.phasePay.status.checking,
  821. audit_time: time, opinion: '',
  822. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  823. });
  824. });
  825. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  826. // 当前审批人变成待审批
  827. await transaction.updateRows(this.tableName, phasePay.curAuditors.map(x => { return {
  828. id: x.id, audit_time: null, audit_status: auditConst.phasePay.status.uncheck, active_order: x.active_order + 2
  829. }}));
  830. await this.ctx.service.phasePayDetail.initPhaseDataByAuditCancel(transaction, phasePay, phasePay.audit_times, selfAuditor.active_order + 1, phasePay.audit_times, selfAuditor.active_order + 2);
  831. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(phasePay.curAuditors, 'id'));
  832. // 同步 期信息
  833. await transaction.update(this.ctx.service.phasePay.tableName, {
  834. id: phasePay.id, audit_times: phasePay.audit_times,
  835. });
  836. // todo 上报/审批 - 检查三方特殊推送
  837. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  838. await transaction.commit();
  839. } catch(err) {
  840. await transaction.rollback();
  841. throw err;
  842. }
  843. }
  844. /**
  845. * 审批人撤回审批退回上一人,插入两条数据
  846. *
  847. * @param phasePay
  848. * @returns {Promise<void>}
  849. * @private
  850. */
  851. async _auditCheckCancelNoPre(phasePay) {
  852. if (phasePay.curAuditors.length === 0 || phasePay.curAuditors[0].active_order <= 1) {
  853. throw '撤回用户数据错误';
  854. }
  855. const accountId = this.ctx.session.sessionUser.accountId;
  856. const selfAuditor = phasePay.preAuditors.find(x => { return x.audit_id === accountId; });
  857. if (!selfAuditor) throw '撤回用户数据错误';
  858. const time = new Date();
  859. const transaction = await this.db.beginTransaction();
  860. try {
  861. // 整理当前流程审核人状态更新
  862. // 删除当前审批人
  863. await transaction.delete(this.tableName, { id: phasePay.curAuditors.map(x => { return x.id; }) });
  864. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(phasePay.curAuditors, 'id'));
  865. // 添加撤回人到审批流程中
  866. const newAuditors = [];
  867. phasePay.preAuditors.forEach(x => {
  868. newAuditors.push({
  869. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  870. audit_times: x.audit_times, active_order: x.active_order + 1,
  871. audit_type: x.audit_type, audit_order: x.audit_order,
  872. audit_status: x.audit_id === selfAuditor.audit_id ? auditConst.phasePay.status.checkCancel : auditConst.phasePay.status.checkSkip,
  873. audit_time: time, opinion: '',
  874. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  875. });
  876. });
  877. await transaction.insert(this.tableName, newAuditors);
  878. // 更新上一个人,最新审批状态为审批中
  879. await transaction.update(this.tableName, { audit_time: null, opinion: '', audit_status: auditConst.phasePay.status.checking }, {
  880. where: { phase_id: phasePay.id, audit_times: selfAuditor.audit_times, active_order: selfAuditor.active_order + 2 }
  881. });
  882. // 同步 期信息
  883. await transaction.update(this.ctx.service.phasePay.tableName, {
  884. id: phasePay.id, audit_times: phasePay.audit_times, audit_status: auditConst.phasePay.status.checking,
  885. });
  886. await this.ctx.service.phasePayDetail.initPhaseDataByAuditCancel(transaction, phasePay, phasePay.audit_times, selfAuditor.active_order + 1, phasePay.audit_times, selfAuditor.active_order + 2);
  887. // todo 上报/审批 - 检查三方特殊推送
  888. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  889. await transaction.commit();
  890. } catch(err) {
  891. await transaction.rollback();
  892. throw err;
  893. }
  894. }
  895. /**
  896. * 审批人撤回审批退回原报
  897. *
  898. * @param phasePay
  899. * @returns {Promise<void>}
  900. * @private
  901. */
  902. async _auditCheckCancelNo(phasePay) {
  903. const accountId = this.ctx.session.sessionUser.accountId;
  904. const selfAuditor = phasePay.preAuditors.find(x => { return x.audit_id === accountId && x.audit_status === auditConst.phasePay.status.checkNo; });
  905. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  906. const time = new Date();
  907. const transaction = await this.db.beginTransaction();
  908. try {
  909. // 整理上一个流程审核人状态更新
  910. // 顺移其后审核人流程顺序
  911. const sql = 'UPDATE ' + this.tableName + ' SET `active_order` = `active_order` + 2 WHERE phase_id = ? AND audit_times = ? AND `active_order` > ?';
  912. await transaction.query(sql, [phasePay.id, selfAuditor.audit_times, selfAuditor.active_order]);
  913. // 当前审批人2次添加至流程中
  914. const checkCancelAuditors = [], checkingAuditors = [];
  915. phasePay.preAuditors.forEach(x => {
  916. checkCancelAuditors.push({
  917. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  918. audit_times: x.audit_times, active_order: x.active_order + 1,
  919. audit_type: x.audit_type, audit_order: x.audit_order,
  920. audit_status: x.audit_id === selfAuditor.audit_id ? auditConst.phasePay.status.checkCancel : auditConst.phasePay.status.checkSkip,
  921. audit_time: time, opinion: '',
  922. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  923. });
  924. });
  925. phasePay.preAuditors.forEach(x => {
  926. checkingAuditors.push({
  927. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  928. audit_times: x.audit_times, active_order: x.active_order + 2,
  929. audit_type: x.audit_type, audit_order: x.audit_order,
  930. audit_status: auditConst.phasePay.status.checking,
  931. audit_time: time, opinion: '',
  932. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  933. });
  934. });
  935. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  936. // 删除当前次审批流
  937. await transaction.delete(this.tableName, { phase_id: phasePay.id, audit_times: phasePay.audit_times });
  938. // 计算并合同支付最终数据
  939. await transaction.update(this.ctx.service.phasePay.tableName, {
  940. id: phasePay.id, audit_times: phasePay.audit_times - 1, audit_status: auditConst.phasePay.status.checking,
  941. });
  942. await transaction.update(this.ctx.service.phasePayDetail.tableName,
  943. { audit_times: selfAuditor.audit_times, audit_sort: selfAuditor.active_order + 2, master_id: `${phasePay.id}-${selfAuditor.audit_times}-${selfAuditor.active_order + 2}` },
  944. { where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_sort: 0 } });
  945. // todo 上报/审批 - 检查三方特殊推送
  946. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  947. await transaction.commit();
  948. } catch(err) {
  949. await transaction.rollback();
  950. throw err;
  951. }
  952. }
  953. /**
  954. * 会签未全部审批通过时,撤回仅修改本人状态
  955. *
  956. * @param phasePay
  957. * @returns {Promise<void>}
  958. * @private
  959. */
  960. async _auditCheckCancelAnd(phasePay) {
  961. const accountId = this.ctx.session.sessionUser.accountId;
  962. const selfAuditor = phasePay.flowAuditors.find(x => { return x.audit_id === accountId; });
  963. if (!selfAuditor || selfAuditor.audit_status !== auditConst.phasePay.status.checked) throw '不可撤回';
  964. const transaction = await this.db.beginTransaction();
  965. try {
  966. await transaction.update(this.tableName, {
  967. id: selfAuditor.id, audit_status: auditConst.phasePay.status.checking, opinion: '', audit_time: null,
  968. });
  969. await transaction.commit();
  970. } catch(err) {
  971. await transaction.rollback();
  972. throw err;
  973. }
  974. }
  975. /**
  976. * 审批撤回
  977. * @param {Number} phasePay - 结算期
  978. * @return {Promise<void>}
  979. */
  980. async checkCancel(phasePay) {
  981. // 分5种情况,根据phasePay.cancancel值判断:
  982. // 1.原报发起撤回,当前流程删除,并回到待上报
  983. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  984. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  985. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  986. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  987. if (phasePay.cancancel === 5) {
  988. await this._auditCheckCancelAnd(phasePay);
  989. } else {
  990. switch (phasePay.cancancel) {
  991. case 1: await this._userCheckCancel(phasePay); break;
  992. case 2: await this._auditCheckCancel(phasePay); break;
  993. case 3: await this._auditCheckCancelNoPre(phasePay); break;
  994. case 4: await this._auditCheckCancelNo(phasePay); break;
  995. default: throw '不可撤回,请刷新页面重试';
  996. }
  997. }
  998. }
  999. // ***** 审批操作
  1000. }
  1001. return PhasePayAudit;
  1002. };