pay_calc.js 6.8 KB

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