pay_calc.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. try {
  50. const value = math.eval(formula);
  51. return value;
  52. } catch(err) {
  53. return 0;
  54. }
  55. }
  56. /**
  57. * 计算起扣金额、付(扣)款限额
  58. *
  59. * @param {Array} pays - (标段)合同支付数据
  60. */
  61. async calculateStartRangePrice (pays) {
  62. await this.getCalcBase();
  63. const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  64. for (const p of pays) {
  65. // 非本期,本次添加的合同支付项,不允许计算,其中默认添加的合同支付项,归属于第一期原报
  66. if (p.csorder === this.ctx.stage.order || (p.csorder === 0 || this.ctx.stage.order === 1)) {
  67. if (p.csaorder === order) {
  68. if (!p.sprice && p.sexpr && p.sexpr !== '') {
  69. p.sprice = this.ctx.helper.round(this.calculateExpr(p.sexpr), this.decimal);
  70. }
  71. if (!p.rprice && p.rexpr && p.rexpr !== '') {
  72. p.rprice = this.ctx.helper.round(this.calculateExpr(p.rexpr), this.decimal);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * 累计 计量等数据
  80. */
  81. async getAddCalcRela() {
  82. // todo 获取截止上期数据
  83. const pre = null;
  84. const cur = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  85. const add = {};
  86. if (pre) {
  87. add.contract_tp = this.ctx.helper.plus(pre.contract_tp, cur.contract_tp);
  88. add.qc_tp = this.ctx.helper.plus(pre.qc_tp, cur.qc_tp);
  89. } else {
  90. add.contract_tp = cur.contract_tp;
  91. add.qc_tp = cur.qc_tp;
  92. }
  93. add.gather_tp = this.ctx.helper.plus(add.contract_tp, add.qc_tp);
  94. return add;
  95. }
  96. /**
  97. * 检查是否到达 计提期限
  98. * @param pay
  99. */
  100. checkDeadline(pay, addRela) {
  101. if (pay.dl_type === deadlineType.tp.value) {
  102. const deadlineTp = addRela[pay.dl_tp_type + '_tp'];
  103. if (deadlineTp > pay.dl_tp) {
  104. return true;
  105. }
  106. } else if (pay.dl_type === deadlineType.count.value) {
  107. return this.ctx.stage.order >= pay.dl_count;
  108. } else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * 计算本期、截止本期金额
  114. * @param {Array} pays - (标段&期)合同支付数据
  115. */
  116. async calculate(pays) {
  117. await this.getCalcBase();
  118. const addRela = await this.getAddCalcRela();
  119. const yfPay = pays.find(function (p) {
  120. return p.ptype === payType.yf;
  121. });
  122. if (!yfPay) {
  123. throw '合同支付数据错误';
  124. }
  125. yfPay.tp = 0;
  126. for (const p of pays) {
  127. if (p.ptype === payType.normal || p.ptype === payType.wc) {
  128. if (p.expr && p.expr !== '') {
  129. const value = this.ctx.helper.round(this.calculateExpr(p.expr), this.decimal);
  130. if (!p.pause && (!p.sprice || addRela.gather_tp > p.sprice)) {
  131. if (p.rprice) {
  132. if (this.checkDeadline(p)) {
  133. p.tp = this.ctx.helper.minus(p.rprice, p.pre_total_price);
  134. } else {
  135. p.tp = Math.min(this.ctx.helper.minus(p.rprice, p.pre_total_price), value);
  136. }
  137. } else {
  138. p.tp = value;
  139. }
  140. } else {
  141. p.tp = null;
  142. }
  143. }
  144. // 累加 至 应付
  145. if (p.is_yf) {
  146. if (p.minus) {
  147. yfPay.tp = this.ctx.helper.minus(yfPay.tp, p.tp);
  148. } else {
  149. yfPay.tp = this.ctx.helper.plus(yfPay.tp, p.tp);
  150. }
  151. }
  152. }
  153. p.end_tp = this.ctx.helper.round(this.ctx.helper.plus(p.tp, p.pre_tp), this.decimal);
  154. }
  155. yfPay.end_tp = this.ctx.helper.plus(yfPay.tp, yfPay.pre_tp);
  156. }
  157. async calculateAll(pays) {
  158. await this.calculateStartRangePrice(pays);
  159. await this.calculate(pays);
  160. }
  161. }
  162. module.exports = PayCalculate;