pay_calc.js 6.4 KB

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