pay_calc.js 8.4 KB

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