pay_calc.js 7.7 KB

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