pay_calc.js 6.6 KB

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