pay_calc.js 7.3 KB

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