pay_calc.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const math = require('mathjs');
  10. const PayConst = require('../const/deal_pay.js');
  11. const payType = PayConst.payType;
  12. const deadlineType = PayConst.deadlineType;
  13. class PayCalculate {
  14. constructor (ctx, decimal) {
  15. this.ctx = ctx;
  16. this.percentReg = /[0-9]+%/g;
  17. this.decimal = decimal.pay ? decimal.payTp : decimal.tp;
  18. }
  19. /**
  20. * 获取 计算基数
  21. * @returns {Promise<void>}
  22. */
  23. async getCalcBase () {
  24. if (this.bases) { return; }
  25. const bases = await this.ctx.service.stage.getStagePayCalcBase();
  26. this.bases = bases.sort(function (a, b) {
  27. if (a && b) {
  28. return b.code.indexOf(a.code) === -1 ? 1 : -1;
  29. } else {
  30. return 0;
  31. }
  32. });
  33. for (const b of this.bases) {
  34. b.reg = new RegExp(b.code, 'igm');
  35. }
  36. }
  37. calculateExpr(expr) {
  38. let formula = expr;
  39. for (const b of this.bases) {
  40. formula = formula.replace(b.reg, b.value);
  41. }
  42. const percent = formula.match(this.percentReg);
  43. if (percent) {
  44. for (const p of percent) {
  45. const v = math.eval(p.replace('%', '/100'));
  46. formula = formula.replace(p, v);
  47. }
  48. }
  49. return math.eval(formula);
  50. }
  51. /**
  52. * 计算起扣金额、付(扣)款限额
  53. *
  54. * @param {Array} pays - (标段)合同支付数据
  55. */
  56. async calculateStartRangePrice (pays) {
  57. await this.getCalcBase();
  58. const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  59. for (const p of pays) {
  60. // 非本期,本次添加的合同支付项,不允许计算,其中默认添加的合同支付项,归属于第一期原报
  61. if (p.csorder === this.ctx.stage.order || (p.csorder === 0 || this.ctx.stage.order === 1)) {
  62. if (p.csaorder === order) {
  63. if (!p.sprice && p.sexpr && p.sexpr !== '') {
  64. p.sprice = this.ctx.helper.round(this.calculateExpr(p.sexpr), this.decimal);
  65. }
  66. if (!p.rprice && p.rexpr && p.rexpr !== '') {
  67. p.rprice = this.ctx.helper.round(this.calculateExpr(p.rexpr), this.decimal);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * 累计 计量等数据
  75. */
  76. async getAddCalcRela() {
  77. // todo 获取截止上期数据
  78. const pre = null;
  79. const cur = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  80. const add = {};
  81. if (pre) {
  82. add.contract_tp = this.ctx.helper.plus(pre.contract_tp, cur.contract_tp);
  83. add.qc_tp = this.ctx.helper.plus(pre.qc_tp, cur.qc_tp);
  84. } else {
  85. add.contract_tp = cur.contract_tp;
  86. add.qc_tp = cur.qc_tp;
  87. }
  88. add.gather_tp = this.ctx.helper.plus(add.contract_tp, add.qc_tp);
  89. return add;
  90. }
  91. /**
  92. * 检查是否到达 计提期限
  93. * @param pay
  94. */
  95. checkDeadline(pay, addRela) {
  96. if (pay.dl_type === deadlineType.tp.value) {
  97. const deadlineTp = addRela[pay.dl_tp_type + '_tp'];
  98. if (deadlineTp > pay.dl_tp) {
  99. return true;
  100. }
  101. } else if (pay.dl_type === deadlineType.count.value) {
  102. return this.ctx.stage.order >= pay.dl_count;
  103. } else {
  104. return false;
  105. }
  106. }
  107. /**
  108. * 计算本期、截止本期金额
  109. * @param {Array} pays - (标段&期)合同支付数据
  110. */
  111. async calculate(pays) {
  112. await this.getCalcBase();
  113. const addRela = await this.getAddCalcRela();
  114. const yfPay = pays.find(function (p) {
  115. return p.ptype === payType.yf;
  116. });
  117. if (!yfPay) {
  118. throw '合同支付数据错误';
  119. }
  120. yfPay.tp = 0;
  121. for (const p of pays) {
  122. if (p.ptype === payType.normal || p.ptype === payType.wc) {
  123. if (p.expr && p.expr !== '') {
  124. const value = this.ctx.helper.round(this.calculateExpr(p.expr), this.decimal);
  125. if (!p.pause && (!p.sprice || addRela.gather_tp > p.sprice)) {
  126. if (p.rprice) {
  127. if (this.checkDeadline(p)) {
  128. p.tp = this.ctx.helper.minus(p.rprice, p.pre_total_price);
  129. } else {
  130. p.tp = Math.min(this.ctx.helper.minus(p.rprice, p.pre_total_price), value);
  131. }
  132. } else {
  133. p.tp = value;
  134. }
  135. } else {
  136. p.tp = null;
  137. }
  138. }
  139. // 累加 至 应付
  140. if (p.is_yf) {
  141. if (p.minus) {
  142. yfPay.tp = this.ctx.helper.minus(yfPay.tp, p.tp);
  143. } else {
  144. yfPay.tp = this.ctx.helper.plus(yfPay.tp, p.tp);
  145. }
  146. }
  147. }
  148. p.end_tp = this.ctx.helper.round(this.ctx.helper.plus(p.tp, p.pre_tp), this.decimal);
  149. }
  150. yfPay.end_tp = this.ctx.helper.plus(yfPay.tp, yfPay.pre_tp);
  151. }
  152. async calculateAll(pays) {
  153. await this.calculateStartRangePrice(pays);
  154. await this.calculate(pays);
  155. }
  156. }
  157. module.exports = PayCalculate;