phase_pay.js 31 KB

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