phase_pay.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const audit = require('../const/audit').common;
  10. const projectLogConst = require('../const/project_log');
  11. const shenpiConst = require('../const/shenpi');
  12. const calcBase = [
  13. {name: '签约合同价', code: 'htj', sort: 10},
  14. {name: '暂列金额', code: 'zlje', sort: 2},
  15. {name: '签约合同价(不含暂列金)', code: 'htjszl', sort: 1},
  16. {name: '签约开工预付款', code: 'kgyfk', sort: 2},
  17. {name: '签约材料预付款', code: 'clyfk', sort: 2},
  18. {name: '本期完成计量', code: 'bqwc', limit: true, sort: 10, checkStart: 'gather_tp'},
  19. {name: '本期合同计量', code: 'bqht', limit: true, sort: 10, checkStart: 'contract_tp'},
  20. {name: '本期变更计量', code: 'bqbg', limit: true, sort: 10},
  21. {name: '本期清单完成计量', code: 'bqqdwc', limit: true, sort: 10},
  22. {name: '本期清单合同计量', code: 'bqqdht', limit: true, sort: 10},
  23. {name: '本期清单变更计量', code: 'bqqdbg', limit: true, sort: 10},
  24. {name: '本期一般变更计量', code: 'ybbqbg', limit: true, sort: 5},
  25. {name: '本期较大变更计量', code: 'jdbqbg', limit: true, sort: 5},
  26. {name: '本期重大变更计量', code: 'zdbqbg', limit: true, sort: 5},
  27. {name: '100章本期完成计量', code: 'ybbqwc', limit: true, sort: 1},
  28. {name: '本期应付', code: 'bqyf', limit: true, ptNormalLimit: true, sort: 20},
  29. {name: '奖金', code: 'bonus', limit: true, sort: 1},
  30. {name: '罚金', code: 'fine', limit: true, sort: 1},
  31. {name: '甲供材料', code: 'jgcl', limit: true, sort: 1},
  32. ];
  33. module.exports = app => {
  34. class PhasePay extends app.BaseService {
  35. /**
  36. * 构造函数
  37. *
  38. * @param {Object} ctx - egg全局变量
  39. * @return {void}
  40. */
  41. constructor(ctx) {
  42. super(ctx);
  43. this.tableName = 'phase_pay';
  44. }
  45. analysisPhasePay(data) {
  46. if (!data) return;
  47. const datas = data instanceof Array ? data : [data];
  48. datas.forEach(x => {
  49. x.rela_stage = x.rela_stage ? JSON.parse(x.rela_stage) : [];
  50. x.calc_base = x.calc_base ? JSON.parse(x.calc_base) : [];
  51. });
  52. }
  53. calculatePhasePay(data) {
  54. const helper = this.ctx.helper;
  55. const datas = data instanceof Array ? data : [data];
  56. const formatNum = this.ctx.tender.info.display.thousandth ? this.ctx.helper.formatNum : function(num) { return num ? num + '' : ''; };
  57. datas.forEach(x => {
  58. x.end_calc_tp = helper.add(x.calc_tp, x.pre_calc_tp);
  59. x.end_pay_tp = helper.add(x.pay_tp, x.pre_pay_tp);
  60. x.end_cut_tp = helper.add(x.cut_tp, x.pre_cut_tp);
  61. x.end_sf_tp = helper.add(x.sf_tp, x.pre_sf_tp);
  62. x.end_yf_tp = helper.add(x.yf_tp, x.pre_yf_tp);
  63. for (const prop in x) {
  64. if (prop.indexOf('_tp') > 0) {
  65. x['display_' + prop] = formatNum(x[prop]);
  66. }
  67. }
  68. });
  69. }
  70. /**
  71. * 获取全部修订
  72. * @param tid
  73. * @returns {Promise<*>}
  74. */
  75. async getAllPhasePay (tid, sort = 'ASC') {
  76. const result = await this.getAllDataByCondition({
  77. where: {tid: tid},
  78. orders: [['phase_order', sort]],
  79. });
  80. this.analysisPhasePay(result);
  81. return result;
  82. }
  83. async getPhasePay(id) {
  84. const result = await this.getDataById(id);
  85. this.analysisPhasePay(result);
  86. return result;
  87. }
  88. async getPhasePayByOrder(tid, phaseOrder) {
  89. const result = await this.getDataByCondition({ tid, phase_order: phaseOrder });
  90. this.analysisPhasePay(result);
  91. return result;
  92. }
  93. async getMaxOrder(tid) {
  94. const sql = 'SELECT Max(`phase_order`) As max_order FROM ' + this.tableName + ' Where `tid` = ?';
  95. const sqlParam = [tid];
  96. const result = await this.db.queryOne(sql, sqlParam);
  97. return result.max_order || 0;
  98. }
  99. async _checkRelaStageConflict(relaStage, phasePay) {
  100. const pays = await this.getAllPhasePay(phasePay.tid);
  101. for (const p of pays) {
  102. if (p.id === phasePay.id) continue;
  103. for (const s of relaStage) {
  104. if (p.rela_stage.find(x => { return x.id === s.id; })) return true;
  105. }
  106. }
  107. return false;
  108. }
  109. async getCalcBase(relaStage, prePhase) {
  110. const result = {};
  111. for (const stage of relaStage) {
  112. result.contract_tp = this.ctx.helper.add(result.contract_tp, stage.contract_tp);
  113. result.qc_tp = this.ctx.helper.add(result.qc_tp, stage.qc_tp);
  114. result.pc_tp = this.ctx.helper.add(result.pc_tp, stage.pc_tp);
  115. const qdSum = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage);
  116. result.qd_contract_tp = qdSum.contract_tp || 0;
  117. result.qd_qc_tp = qdSum.qc_tp || 0;
  118. result.qd_pc_tp = qdSum.pc_tp || 0;
  119. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
  120. const sumPc = await this.ctx.service.stageBillsPc.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
  121. result.gather_100_tp = this.ctx.helper.sum([sumGcl.contract_tp, sumGcl.qc_tp, sumPc.pc_tp]);
  122. const bg = await this.ctx.service.stage.getChangeSubtotal(stage);
  123. result.common_bg_tp = bg.common || 0;
  124. result.more_bg_tp = bg.more || 0;
  125. result.great_bg_tp = bg.great || 0;
  126. }
  127. result.gather_tp = this.ctx.helper.sum([result.contract_tp, result.qc_tp, result.pc_tp]) || 0;
  128. result.qd_gather_tp = this.ctx.helper.sum([result.qd_contract_tp, result.qd_qc_tp, result.qd_pc_tp]) || 0;
  129. const bonusSum = await this.ctx.service.stageBonus.getSumTp(relaStage);
  130. result.bonus_positive_tp = bonusSum.positive_tp || 0;
  131. result.bonus_negative_tp = bonusSum.negative_tp || 0;
  132. result.bonus_tp = bonusSum.sum_tp || 0;
  133. const jgclSum = await this.ctx.service.stageJgcl.getSumTp(relaStage);
  134. result.jgcl_tp = jgclSum.sum_tp || 0;
  135. const otherSum = await this.ctx.service.stageOther.getSumTp(relaStage);
  136. result.other_tp = otherSum.sum_tp || 0;
  137. const safeProdSum = await this.ctx.service.stageSafeProd.getSumTp(relaStage);
  138. result.safe_prod_tp = safeProdSum.sum_tp || 0;
  139. const tempLandSum = await this.ctx.service.stageTempLand.getSumTp(relaStage);
  140. result.temp_land_tp = tempLandSum.sum_tp || 0;
  141. if (prePhase && prePhase.calc_base) {
  142. result.pre_contract_tp = this.ctx.helper.add(prePhase.calc_base.contract_tp, prePhase.calc_base.pre_contract_tp);
  143. result.pre_qc_tp = this.ctx.helper.add(prePhase.calc_base.qc_tp, prePhase.calc_base.pre_qc_tp);
  144. result.pre_pc_tp = this.ctx.helper.add(prePhase.calc_base.pc_tp, prePhase.calc_base.pre_pc_tp);
  145. result.pre_gather_tp = this.ctx.helper.add(prePhase.calc_base.gather_tp, prePhase.calc_base.pre_gather_tp);
  146. result.pre_qd_contract_tp = this.ctx.helper.add(prePhase.calc_base.qd_contract_tp, prePhase.calc_base.pre_qd_contract_tp);
  147. result.pre_qd_qc_tp = this.ctx.helper.add(prePhase.calc_base.qd_qc_tp, prePhase.calc_base.pre_qd_qc_tp);
  148. result.pre_qd_pc_tp = this.ctx.helper.add(prePhase.calc_base.qd_pc_tp, prePhase.calc_base.pre_qd_pc_tp);
  149. result.pre_qd_gather_tp = this.ctx.helper.add(prePhase.calc_base.qd_gather_tp, prePhase.calc_base.pre_qd_gather_tp);
  150. result.pre_bonus_positive_tp = this.ctx.helper.add(prePhase.calc_base.bonus_positive_tp, prePhase.calc_base.pre_bonus_positive_tp);
  151. result.pre_bonus_negative_tp = this.ctx.helper.add(prePhase.calc_base.bonus_negative_tp, prePhase.calc_base.pre_bonus_negative_tp);
  152. result.pre_bonus_tp = this.ctx.helper.add(prePhase.calc_base.bonus_tp, prePhase.calc_base.pre_bonus_tp);
  153. result.pre_jgcl_tp = this.ctx.helper.add(prePhase.calc_base.jgcl_tp, prePhase.calc_base.pre_jgcl_tp);
  154. result.pre_other_tp = this.ctx.helper.add(prePhase.calc_base.other_tp, prePhase.calc_base.pre_other_tp);
  155. result.pre_safe_prod_tp = this.ctx.helper.add(prePhase.calc_base.safe_prod_tp, prePhase.calc_base.pre_safe_prod_tp);
  156. result.pre_temp_land_tp = this.ctx.helper.add(prePhase.calc_base.temp_land_tp, prePhase.calc_base.pre_temp_land_tp);
  157. }
  158. return result;
  159. }
  160. /**
  161. * 获取 当期的 计算基数
  162. * @return {Promise<any>}
  163. */
  164. getPhasePayCalcBase(phasePay, tenderInfo) {
  165. const payCalcBase = JSON.parse(JSON.stringify(calcBase));
  166. for (const cb of payCalcBase) {
  167. switch (cb.code) {
  168. case 'htj':
  169. cb.value = tenderInfo.deal_param.contractPrice;
  170. break;
  171. case 'zlje':
  172. cb.value = tenderInfo.deal_param.zanLiePrice;
  173. break;
  174. case 'htjszl':
  175. cb.value = this.ctx.helper.sub(tenderInfo.deal_param.contractPrice, tenderInfo.deal_param.zanLiePrice);
  176. break;
  177. case 'kgyfk':
  178. cb.value = tenderInfo.deal_param.startAdvance;
  179. break;
  180. case 'clyfk':
  181. cb.value = tenderInfo.deal_param.materialAdvance;
  182. break;
  183. case 'bqwc':
  184. cb.value = phasePay.calc_base.gather_tp;
  185. break;
  186. case 'bqht':
  187. cb.value = phasePay.calc_base.contract_tp;
  188. break;
  189. case 'bqbg':
  190. cb.value = phasePay.calc_base.qc_tp;
  191. break;
  192. case 'bqqdwc':
  193. cb.value = phasePay.calc_base.qd_gather_tp;
  194. break;
  195. case 'bqqdht':
  196. cb.value = phasePay.calc_base.qd_contract_tp;
  197. break;
  198. case 'bqqdbg':
  199. cb.value = phasePay.calc_base.qd_qc_tp;
  200. break;
  201. case 'ybbqwc':
  202. cb.value = phasePay.calc_base.gather_100_tp;
  203. break;
  204. case 'ybbqbg':
  205. cb.value = phasePay.calc_base.common_bg_tp;
  206. break;
  207. case 'jdbqbg':
  208. cb.value = phasePay.calc_base.more_bg_tp;
  209. break;
  210. case 'zdbqbg':
  211. cb.value = phasePay.calc_base.great_bg_tp;
  212. break;
  213. case 'bonus':
  214. cb.value = phasePay.calc_base.bonus_positive_tp;
  215. break;
  216. case 'fine':
  217. cb.value = phasePay.calc_base.bonus_negative_tp;
  218. break;
  219. case 'jgcl':
  220. cb.value = phasePay.calc_base.jgcl_tp;
  221. break;
  222. case 'aqsc':
  223. cb.value = phasePay.calc_base.safe_prod_tp;
  224. break;
  225. case 'lsyd':
  226. cb.value = phasePay.calc_base.temp_land_tp;
  227. break;
  228. default:
  229. cb.value = 0;
  230. }
  231. }
  232. return payCalcBase;
  233. }
  234. async add(tid, relaStage, phaseDate, memo) {
  235. if (!tid) throw '数据错误';
  236. const user_id = this.ctx.session.sessionUser.accountId;
  237. const maxOrder = await this.getMaxOrder(tid);
  238. const data = {
  239. id: this.uuid.v4(), tid: tid, create_user_id: user_id, update_user_id: user_id,
  240. phase_order: maxOrder + 1, phase_date: phaseDate, memo,
  241. audit_times: 1, audit_status: audit.status.uncheck,
  242. rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
  243. };
  244. if (await this._checkRelaStageConflict(relaStage, data)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
  245. const prePhase = maxOrder > 0 ? await this.getPhasePayByOrder(tid, maxOrder) : null;
  246. if (prePhase) {
  247. data.end_calc_tp = this.ctx.helper.add(prePhase.calc_tp, prePhase.pre_calc_tp);
  248. data.end_pay_tp = this.ctx.helper.add(prePhase.pay_tp, prePhase.pre_pay_tp);
  249. data.end_cut_tp = this.ctx.helper.add(prePhase.cut_tp, prePhase.pre_cut_tp);
  250. data.end_sf_tp = this.ctx.helper.add(prePhase.sf_tp, prePhase.pre_sf_tp);
  251. data.end_yf_tp = this.ctx.helper.add(prePhase.yf_tp, prePhase.pre_yf_tp);
  252. }
  253. const calcBase = await this.getCalcBase(relaStage, prePhase);
  254. data.calc_base = JSON.stringify(calcBase);
  255. const transaction = await this.db.beginTransaction();
  256. try {
  257. const result = await transaction.insert(this.tableName, data);
  258. if (result.affectedRows !== 1) throw '新增合同支付失败';
  259. await this.ctx.service.phasePayDetail.initPhaseData(transaction, data, prePhase);
  260. await this.ctx.service.phasePayAudit.copyPreAuditors(transaction, prePhase, data);
  261. await transaction.commit();
  262. this.analysisPhasePay(data);
  263. return data;
  264. } catch(err) {
  265. await transaction.rollback();
  266. throw err;
  267. }
  268. }
  269. async delete(id) {
  270. const info = await this.getDataById(id);
  271. const conn = await this.db.beginTransaction();
  272. try {
  273. await conn.delete(this.tableName, { id });
  274. await conn.delete(this.ctx.service.phasePayDetail.tableName, { phase_id: id });
  275. const files = await this.ctx.service.phasePayFile.getFiles({ where: { phase_id: id} });
  276. for (const f of files) {
  277. this.ctx.app.fujianOss.delete(f.filepath);
  278. }
  279. await conn.delete(this.ctx.service.phasePayFile.tableName, { phase_id: id });
  280. await conn.delete(this.ctx.service.phasePayAudit.tableName, { phase_id: id });
  281. // 记录删除日志
  282. await this.ctx.service.projectLog.addProjectLog(conn, projectLogConst.type.phasePay, projectLogConst.status.delete, `第${info.phase_order}期`);
  283. await conn.commit();
  284. } catch (err) {
  285. await conn.rollback();
  286. throw err;
  287. }
  288. }
  289. async save(phasePay, data) {
  290. await this.defaultUpdate({id: phasePay.id, phase_date: data.phase_date, memo: data.memo});
  291. }
  292. async refreshCalcBase(phasePay) {
  293. const prePhase = phasePay.phase_order > 1 ? await this.getPhasePayByOrder(phasePay.tid, phasePay.phase_order - 1) : null;
  294. const relaStage = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: phasePay.tid, order: phasePay.rela_stage.map(x => { return x.stage_order; }) } });
  295. const calcBase = await this.getCalcBase(relaStage, prePhase);
  296. const conn = await this.db.beginTransaction();
  297. try {
  298. await conn.update(this.tableName, {
  299. id: phasePay.id, update_user_id: this.ctx.session.sessionUser.accountId,
  300. calc_base: JSON.stringify(calcBase),
  301. calc_base_time: new Date()
  302. });
  303. phasePay.calc_base = calcBase;
  304. await this.ctx.service.phasePayDetail.calculateSave(phasePay, conn);
  305. await conn.commit();
  306. } catch(err) {
  307. await conn.rollback();
  308. throw err;
  309. }
  310. }
  311. async resetRelaStageId(phasePay, relaStage) {
  312. if (await this._checkRelaStageConflict(relaStage, phasePay)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
  313. const prePhase = prePhase.phase_order > 1 ? await this.getPhasePayByOrder(phasePay.tid, phasePay.phase_order - 1) : null;
  314. const calcBase = await this.getCalcBase(relaStage, prePhase);
  315. const rela_stage = relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; });
  316. const conn = await this.db.beginTransaction();
  317. try {
  318. await conn.update(this.tableName, {
  319. id: phasePay.id, update_user_id: this.ctx.session.sessionUser.accountId,
  320. calc_base: JSON.stringify(calcBase),
  321. rela_stage: JSON.stringify(rela_stage),
  322. calc_base_time: new Date()
  323. });
  324. phasePay.calc_base = calcBase;
  325. phasePay.rela_stage = relaStage;
  326. await this.ctx.service.phasePayDetail.calculateSave(phasePay, conn);
  327. await conn.commit();
  328. } catch(err) {
  329. await conn.rollback();
  330. throw err;
  331. }
  332. }
  333. async loadUser(phasePay) {
  334. phasePay.user = await this.ctx.service.projectAccount.getAccountInfoById(phasePay.create_user_id);
  335. phasePay.auditors = await this.ctx.service.phasePayAudit.getAuditors(phasePay.id, phasePay.curTimes || phasePay.audit_times);
  336. phasePay.auditorIds = this._.map(phasePay.auditors, 'audit_id');
  337. phasePay.curAuditors = phasePay.auditors.filter(x => { return x.audit_status === audit.status.checking; });
  338. phasePay.curAuditorIds = phasePay.curAuditors.map(x => { return x.audit_id; });
  339. phasePay.flowAuditors = phasePay.curAuditors.length === 0 ? [] : phasePay.auditors.filter(x => { return x.active_order === phasePay.curAuditors[0].active_order; });
  340. phasePay.flowAuditorIds = phasePay.curAuditors.map(x => { return x.audit_id; });
  341. phasePay.nextAuditors = phasePay.curAuditors.length > 0 ? phasePay.auditors.filter(x => { return x.active_order === phasePay.curAuditors[0].active_order + 1; }) : [];
  342. phasePay.nextAuditorIds = this._.map(phasePay.nextAuditors, 'audit_id');
  343. phasePay.auditorGroups = this.ctx.helper.groupAuditors(phasePay.auditors, 'active_order');
  344. phasePay.userGroups = this.ctx.helper.groupAuditorsUniq(phasePay.auditorGroups);
  345. phasePay.finalAuditorIds = phasePay.userGroups.length > 1 ? phasePay.userGroups[phasePay.userGroups.length - 1].map(x => { return x.audit_id; }) : [];
  346. phasePay.userIds = phasePay.audit_status === audit.status.uncheck // 当前流程下全部参与人id
  347. ? [phasePay.user_id]
  348. : phasePay.auditorIds;
  349. }
  350. async loadAuditViewData(phasePay) {
  351. if (!phasePay.user) phasePay.user = await this.ctx.service.projectAccount.getAccountInfoById(phasePay.user_id);
  352. const auditTimes = phasePay.audit_status === audit.status.checkNo ? phasePay.audit_times - 1 : phasePay.audit_times;
  353. phasePay.auditHistory = await this.ctx.service.phasePayAudit.getAuditorHistory(phasePay.id, auditTimes);
  354. // 获取审批流程中左边列表
  355. if (phasePay.audit_status === audit.status.checkNo && phasePay.create_user_id !== this.ctx.session.sessionUser.accountId) {
  356. const auditors = await this.ctx.service.phasePayAudit.getAuditors(phasePay.id, phasePay.audit_times - 1); // 全部参与的审批人
  357. const auditorGroups = this.ctx.helper.groupAuditors(auditors);
  358. phasePay.hisUserGroup = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  359. } else {
  360. phasePay.hisUserGroup = phasePay.userGroups;
  361. }
  362. }
  363. /**
  364. * cancancel = 0 不可撤回
  365. * cancancel = 1 原报撤回
  366. * cancancel = 2 审批人撤回 审批通过
  367. * cancancel = 3 审批人撤回 审批退回上一人
  368. * cancancel = 4 审批人撤回 退回原报
  369. * cancancel = 5 会签未全部审批通过时,审批人撤回 审批通过
  370. *
  371. * @param phasePay
  372. * @returns {Promise<void>}
  373. */
  374. async doCheckCanCancel(phasePay) {
  375. // 默认不可撤回
  376. phasePay.cancancel = 0;
  377. // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
  378. const status = audit.status;
  379. if (phasePay.audit_status === status.checked || phasePay.audit_status === status.uncheck) return;
  380. const accountId = this.ctx.session.sessionUser.accountId;
  381. if (phasePay.audit_status !== status.checkNo) {
  382. // 找出当前操作人上一个审批人,包括审批完成的和退回上一个审批人的,同时当前操作人为第一人时,就是则为原报
  383. if (phasePay.flowAuditors.find(x => { return x.audit_status !== status.checking}) && phasePay.flowAuditorIds.indexOf(accountId) < 0) return; // 当前流程存在审批人审批通过时,不可撤回
  384. if (phasePay.curAuditorIds.indexOf(accountId) < 0 && phasePay.flowAuditorIds.indexOf(accountId) >= 0) {
  385. phasePay.cancancel = 5; // 会签未全部审批通过时,审批人撤回审批通过
  386. return;
  387. }
  388. const preAuditors = phasePay.curAuditors[0] && phasePay.curAuditors[0].active_order !== 1 ? phasePay.auditors.filter(x => { return x.active_order === phasePay.curAuditors[0].active_order - 1; }) : [];
  389. const preAuditorCheckAgain = preAuditors.find(pa => { return pa.audit_status === status.checkAgain; });
  390. const preAuditorCheckCancel = preAuditors.find(pa => { return pa.audit_status === status.checkCancel; });
  391. const preAuditorHasOld = preAuditors.find(pa => { return pa.is_old === 1; });
  392. const preAuditorIds = (preAuditorCheckAgain ? [] : preAuditors.map(x => { return x.audit_id })); // 重审不可撤回
  393. if ((this._.isEqual(phasePay.flowAuditorIds, preAuditorIds) && preAuditorCheckCancel) || preAuditorHasOld) {
  394. return; // 不可以多次撤回
  395. }
  396. const preAuditChecked = preAuditors.find(pa => { return pa.audit_status === status.checked && pa.audit_id === accountId; });
  397. const preAuditCheckNoPre = preAuditors.find(pa => { return pa.audit_status === status.checkNoPre && pa.audit_id === accountId; });
  398. if (preAuditorIds.indexOf(accountId) >= 0) {
  399. if (preAuditChecked) {
  400. phasePay.cancancel = 2;// 审批人撤回审批通过
  401. } else if (preAuditCheckNoPre) {
  402. phasePay.cancancel = 3;// 审批人撤回审批退回上一人
  403. }
  404. phasePay.preAuditors = preAuditors;
  405. } else if (preAuditors.length === 0 && accountId === phasePay.create_user_id) {
  406. phasePay.cancancel = 1;// 原报撤回
  407. }
  408. } else {
  409. const lastAuditors = await this.ctx.service.phasePayAudit.getAuditors(phasePay.id, phasePay.audit_times - 1);
  410. const onAuditor = this._.findLast(lastAuditors, { audit_status: status.checkNo });
  411. if (onAuditor.audit_id === accountId) {
  412. phasePay.cancancel = 4;// 审批人撤回退回原报
  413. phasePay.preAuditors = lastAuditors.filter(x => { return x.active_order === onAuditor.active_order });
  414. }
  415. }
  416. }
  417. async doCheckPhase(phasePay) {
  418. const accountId = this.ctx.session.sessionUser.accountId;
  419. // 审批退回时,原报读取本轮流程,其他人读取上一轮流程
  420. if (phasePay.audit_status === audit.status.checkNo) {
  421. phasePay.curTimes = phasePay.create_user_id === accountId ? phasePay.audit_times : phasePay.audit_times - 1;
  422. } else {
  423. phasePay.curTimes = phasePay.audit_times;
  424. }
  425. // 加载参与人
  426. await this.loadUser(phasePay);
  427. if (phasePay.audit_status === audit.status.uncheck) {
  428. phasePay.readOnly = accountId !== phasePay.create_user_id;
  429. phasePay.curSort = 0;
  430. } else if (phasePay.audit_status === audit.status.checkNo) {
  431. phasePay.readOnly = accountId !== phasePay.create_user_id;
  432. if (!phasePay.readOnly) {
  433. phasePay.curSort = 0;
  434. } else {
  435. const checkNoAudit = await this.service.phasePayAudit.getDataByCondition({
  436. phase_id: phasePay.id, audit_times: phasePay.audit_times - 1, audit_status: audit.status.checkNo,
  437. });
  438. phasePay.curSort = checkNoAudit.active_order;
  439. }
  440. } else if (phasePay.audit_status === audit.status.checked) {
  441. phasePay.readOnly = true;
  442. phasePay.curSort = phasePay.audit_max_sort;
  443. } else {
  444. // 会签,会签人部分审批通过时,只读,但是curSort需按原来的取值
  445. phasePay.curSort = phasePay.flowAuditorIds.indexOf(accountId) >= 0 ? phasePay.curAuditors[0].active_order : phasePay.curAuditors[0].active_order - 1;
  446. phasePay.readOnly = phasePay.curAuditorIds.indexOf(accountId) < 0;
  447. phasePay.canCheck = phasePay.readOnly && phasePay.curAuditorIds.indexOf(accountId) > 0;
  448. }
  449. await this.doCheckCanCancel(phasePay);
  450. }
  451. async checkShenpi(phasePay) {
  452. const status = audit.status;
  453. const info = this.ctx.tender.info;
  454. const shenpi_status = info.shenpi.phasePay;
  455. if ((phasePay.audit_status === status.uncheck || phasePay.audit_status === status.checkNo) && shenpi_status !== shenpiConst.sp_status.sqspr) {
  456. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  457. const auditList = await this.ctx.service.phasePayAudit.getAllDataByCondition({ where: { phase_id: phasePay.id, audit_times: phasePay.audit_times }, orders: [['audit_order', 'asc']] });
  458. auditList.shift();
  459. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  460. const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: phasePay.tid, sp_type: shenpiConst.sp_type.phasePay, sp_status: shenpi_status } });
  461. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  462. let sameAudit = auditList.length === shenpiList.length;
  463. if (sameAudit) {
  464. for (const audit of auditList) {
  465. const shenpi = shenpiList.find(x => { return x.audit_id === audit.audit_id; });
  466. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  467. sameAudit = false;
  468. break;
  469. }
  470. }
  471. }
  472. if (!sameAudit) {
  473. await this.ctx.service.phasePayAudit.updateNewAuditList(phasePay, shenpiList);
  474. await this.loadUser(phasePay);
  475. }
  476. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  477. const shenpiInfo = await this.ctx.service.shenpiAudit.getDataByCondition({ tid: phasePay.tid, sp_type: shenpiConst.sp_type.phasePay, sp_status: shenpi_status });
  478. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  479. const lastAuditors = auditList.filter(x => { x.active_order === auditList.active_order; });
  480. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].audit_id))) {
  481. await this.ctx.service.phasePayAudit.updateLastAudit(phasePay, auditList, shenpiInfo.audit_id);
  482. await this.loadUser(phasePay);
  483. } else if (!shenpiInfo) {
  484. // 不存在终审人的状态下这里恢复为授权审批人
  485. this.ctx.tender.info.shenpi.phasePay = shenpiConst.sp_status.sqspr;
  486. }
  487. }
  488. }
  489. }
  490. }
  491. return PhasePay;
  492. };