phase_pay.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const audit = require('../const/audit').common;
  10. module.exports = app => {
  11. class PhasePay extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'phase_pay';
  21. }
  22. analysisPhasePay(data) {
  23. const datas = data instanceof Array ? data : [data];
  24. datas.forEach(x => {
  25. x.rela_stage = x.rela_stage ? JSON.parse(x.rela_stage) : [];
  26. x.calc_base = x.calc_base ? JSON.parse(x.calc_base) : [];
  27. });
  28. }
  29. calculatePhasePay(data) {
  30. const helper = this.ctx.helper;
  31. const datas = data instanceof Array ? data : [data];
  32. const formatNum = this.ctx.tender.info.display.thousandth ? this.ctx.helper.formatNum : function(num) { return num ? num + '' : ''; };
  33. datas.forEach(x => {
  34. x.end_calc_tp = helper.add(x.calc_tp, x.pre_calc_tp);
  35. x.end_pay_tp = helper.add(x.pay_tp, x.pre_pay_tp);
  36. x.end_cut_tp = helper.add(x.cut_tp, x.pre_cut_tp);
  37. x.end_sf_tp = helper.add(x.sf_tp, x.pre_sf_tp);
  38. x.end_yf_tp = helper.add(x.yf_tp, x.pre_yf_tp);
  39. if (thousandth)
  40. for (const prop in x) {
  41. if (prop.indexOf('_tp') > 0) {
  42. x['display_' + prop] = formatNum(x[prop]);
  43. }
  44. }
  45. });
  46. }
  47. /**
  48. * 获取全部修订
  49. * @param tid
  50. * @returns {Promise<*>}
  51. */
  52. async getAllPhasePay (tid, sort = 'ASC') {
  53. const result = await this.getAllDataByCondition({
  54. where: {tid: tid},
  55. orders: [['phase_order', sort]],
  56. });
  57. this.analysisPhasePay(result);
  58. return result;
  59. }
  60. async getPhasePay(id) {
  61. const result = await this.getDataById(id);
  62. this.analysisPhasePay(result);
  63. return result;
  64. }
  65. async getPhasePayByOrder(tid, phaseOrder) {
  66. const result = await this.getDataByCondition({ tid, phase_order: phaseOrder });
  67. this.analysisPhasePay(result);
  68. return result;
  69. }
  70. async loadUser(phasePay) {
  71. // todo 加载审批人
  72. }
  73. async getNewOrder(tid) {
  74. const sql = 'SELECT Max(`phase_order`) As max_order FROM ' + this.tableName + ' Where `tid` = ?';
  75. const sqlParam = [tid];
  76. const result = await this.db.queryOne(sql, sqlParam);
  77. return result.max_order || 0;
  78. }
  79. async _checkRelaStageConflict(relaStage, phasePay) {
  80. const pays = this.getAllPhasePay(phasePay.tid);
  81. for (const p of pays) {
  82. if (p.id === phasePay.id) continue;
  83. for (const s of relaStage) {
  84. if (p.rela_stage.find(x => { return x.id === s.id; })) return true;
  85. }
  86. }
  87. return false;
  88. }
  89. async getCalcBase(relaStage) {
  90. // todo 获取计算基数
  91. }
  92. async add(tid, relaStage, phaseDate, memo) {
  93. if (!tid) throw '数据错误';
  94. const user_id = this.ctx.session.sessionUser.accountId;
  95. const maxOrder = await this.getNewOrder(tid);
  96. const data = {
  97. id: this.uuid.v4(), tid: tid, create_user_id: user_id, update_user_id: user_id,
  98. phase_order: maxOrder + 1, phase_date: phaseDate, memo,
  99. audit_times: 1, audit_status: audit.status.uncheck,
  100. rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
  101. };
  102. if (await this._checkRelaStageConflict(relaStage, data)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
  103. const calcBase = await this.getCalcBase(relaStage);
  104. data.calc_base = JSON.stringify(calcBase);
  105. const transaction = await this.db.beginTransaction();
  106. try {
  107. const result = await transaction.insert(this.tableName, data);
  108. if (result.affectedRows !== 1) throw '新增合同支付失败';
  109. await this.ctx.service.phasePayDetail.initPhaseData(data);
  110. await transaction.commit();
  111. return data;
  112. } catch(err) {
  113. await transaction.rollback();
  114. throw err;
  115. }
  116. }
  117. async refreshCalcBase(id) {
  118. const curPay = this.getPhasePay(id);
  119. if (!curPay) throw '合同支付不存在, 请刷新页面重试';
  120. const calcBase = await this.getCalcBase(relaStage);
  121. await this.defaultUpdate({
  122. id, update_user_id: this.ctx.session.sessionUser.accountId,
  123. calc_base: JSON.stringify(calcBase),
  124. rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
  125. calc_base_time: new Date()
  126. });
  127. }
  128. async resetRelaStageId(id, relaStage) {
  129. const curPay = this.getPhasePay(id);
  130. if (!curPay) throw '合同支付不存在, 请刷新页面重试';
  131. if (await this._checkRelaStageConflict(relaStage, curPay)) throw '选择的计量期,已被调用,请刷新页面后选择计量期新增合同支付';
  132. const calcBase = await this.getCalcBase(relaStage);
  133. await this.defaultUpdate({
  134. id, update_user_id: this.ctx.session.sessionUser.accountId,
  135. calc_base: JSON.stringify(calcBase),
  136. rela_stage: JSON.stringify(relaStage.map(s => { return {stage_id: s.id, stage_order: s.order}; })),
  137. calc_base_time: new Date()
  138. });
  139. }
  140. }
  141. return PhasePay;
  142. };