pay_calc.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, first, addRela) {
  41. let formula = pay.expr;
  42. for (const b of this.bases) {
  43. if ((b.code === 'bqwc') && (first && 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. }
  97. if (!p.rprice && p.rexpr && p.rexpr !== '') {
  98. p.rprice = this.ctx.helper.round(this.calculateExpr(p.rexpr), this.decimal);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * 累计 计量等数据
  106. */
  107. async getAddCalcRela() {
  108. // todo 获取截止上期数据
  109. const pre = null;
  110. const cur = await this.ctx.service.stageBills.getSumTotalPrice(this.stage);
  111. const add = {};
  112. if (pre) {
  113. add.contract_tp = this.ctx.helper.add(pre.contract_tp, cur.contract_tp);
  114. add.qc_tp = this.ctx.helper.add(pre.qc_tp, cur.qc_tp);
  115. } else {
  116. add.contract_tp = cur.contract_tp;
  117. add.qc_tp = cur.qc_tp;
  118. }
  119. add.gather_tp = this.ctx.helper.add(add.contract_tp, add.qc_tp);
  120. return add;
  121. }
  122. /**
  123. * 检查是否到达 计提期限
  124. * @param pay
  125. */
  126. checkDeadline(pay, addRela) {
  127. if (pay.dl_type === deadlineType.tp.value) {
  128. const deadlineTp = addRela[pay.dl_tp_type + '_tp'];
  129. if (deadlineTp > pay.dl_tp) {
  130. return true;
  131. }
  132. } else if (pay.dl_type === deadlineType.count.value) {
  133. return this.stage.order >= pay.dl_count;
  134. } else {
  135. return false;
  136. }
  137. }
  138. /**
  139. * 计算本期、截止本期金额
  140. * @param {Array} pays - (标段&期)合同支付数据
  141. */
  142. async calculate(pays) {
  143. await this.getCalcBase();
  144. const addRela = await this.getAddCalcRela();
  145. const yfPay = pays.find(function (p) {
  146. return p.ptype === payType.yf;
  147. });
  148. if (!yfPay) return false;
  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_tp);
  159. } else {
  160. p.tp = Math.min(this.ctx.helper.sub(p.rprice, p.pre_tp), 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. if (this.checkDeadline(p, addRela)) {
  168. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  169. } else {
  170. p.tp = Math.min(this.ctx.helper.sub(p.rprice, p.pre_tp), this.ctx.helper.round(p.tp, this.decimal));
  171. }
  172. } else {
  173. p.tp = this.ctx.helper.round(p.tp, this.decimal);
  174. }
  175. }
  176. } else {
  177. p.tp = 0;
  178. }
  179. // 累加 至 应付
  180. if (p.is_yf) {
  181. if (p.minus) {
  182. yfPay.tp = this.ctx.helper.sub(yfPay.tp, p.tp);
  183. } else {
  184. yfPay.tp = this.ctx.helper.add(yfPay.tp, p.tp);
  185. }
  186. }
  187. }
  188. p.end_tp = this.ctx.helper.round(this.ctx.helper.add(p.tp, p.pre_tp), this.decimal);
  189. }
  190. yfPay.end_tp = this.ctx.helper.add(yfPay.tp, yfPay.pre_tp);
  191. }
  192. async calculateAll(pays) {
  193. await this.calculateStartRangePrice(pays);
  194. await this.calculate(pays);
  195. }
  196. }
  197. module.exports = PayCalculate;