pay_calc.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const math = require('mathjs');
  10. math.config({
  11. number: 'BigNumber',
  12. });
  13. const PayConst = require('../const/deal_pay.js');
  14. const payType = PayConst.payType;
  15. const deadlineType = PayConst.deadlineType;
  16. class PayCalculate {
  17. constructor (ctx, stage, tenderInfo) {
  18. this.ctx = ctx;
  19. this.stage = stage;
  20. this.percentReg = /((\d+)|((\d+)(\.\d+)))%/g;
  21. this.tenderInfo = tenderInfo;
  22. this.decimal = tenderInfo.decimal.pay ? tenderInfo.decimal.payTp : tenderInfo.decimal.tp;
  23. this.orderReg = /f\d+/ig;
  24. /* 以下变量在调用calculate方法后获得
  25. this.add;
  26. this.pre;
  27. this.cur;
  28. this.yf;
  29. */
  30. }
  31. /**
  32. * 获取 计算基数
  33. * @returns {Promise<void>}
  34. */
  35. async getCalcBase () {
  36. if (this.bases) { return; }
  37. const bases = await this.ctx.service.stage.getStagePayCalcBase(this.stage, this.tenderInfo);
  38. this.bases = bases.sort(function (a, b) {
  39. return a.sort - b.sort;
  40. // if (a && b) {
  41. // return b.code.indexOf(a.code) >= 0 ? 1 : 0;
  42. // } else {
  43. // return 0;
  44. // }
  45. });
  46. for (const b of this.bases) {
  47. b.reg = new RegExp(b.code, 'igm');
  48. }
  49. }
  50. _calculateTpExpr(pay, pays) {
  51. let formula = pay.expr;
  52. const orderParam = pay.expr.match(this.orderReg);
  53. if (orderParam) {
  54. for (const op of orderParam) {
  55. const order = parseInt(op.substring(1, op.length));
  56. const orderPay = pays.find(x => { return x.order === order });
  57. formula = formula.replace(op, orderPay.tp || 0);
  58. }
  59. }
  60. for (const b of this.bases) {
  61. if ((b.code === 'bqwc' || b.code === 'bqht') && (!pay.pre_used && pay.sprice)) {
  62. switch (b.code) {
  63. case 'bqwc':
  64. formula = formula.replace(b.reg, this.ctx.helper.sub(this.add.gather_tp, pay.sprice));
  65. break;
  66. case 'bqht':
  67. formula = formula.replace(b.reg, this.ctx.helper.sub(this.add.contract_tp, pay.sprice));
  68. break;
  69. }
  70. } else {
  71. formula = formula.replace(b.reg, b.value);
  72. }
  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. // 使用mathjs计算 math.eval('17259401.95*0.9') = 15533461.754999999,正确应为 15533461.755
  83. // const value = math.eval(formula);
  84. // 使用逆波兰法四则运算,可防止出现误差,但是只支持四则运算,不支持科学运算
  85. // const value = this.ctx.helper.calcExprStrRpn(formula);
  86. // 使用mathjs的大数运算,可支持所有
  87. const value = parseFloat(math.eval(formula));
  88. return value;
  89. } catch(err) {
  90. return 0;
  91. }
  92. }
  93. _calculateExpr(expr) {
  94. let formula = expr;
  95. for (const b of this.bases) {
  96. formula = formula.replace(b.reg, b.value ? b.value : 0);
  97. }
  98. const percent = formula.match(this.percentReg);
  99. if (percent) {
  100. for (const p of percent) {
  101. const v = math.eval(p.replace('%', '/100'));
  102. formula = formula.replace(p, v);
  103. }
  104. }
  105. try {
  106. // 使用mathjs计算 math.eval('17259401.95*0.9') = 15533461.754999999,正确应为 15533461.755
  107. // const value = math.eval(formula);
  108. // 使用逆波兰法四则运算,可防止出现误差,但是只支持四则运算,不支持科学运算
  109. // const value = this.ctx.helper.calcExprStrRpn(formula);
  110. // 使用mathjs的大数运算,可支持所有
  111. const value = parseFloat(math.eval(formula));
  112. return value;
  113. } catch(err) {
  114. return 0;
  115. }
  116. }
  117. /**
  118. * 计算起扣金额、付(扣)款限额
  119. *
  120. * @param {Array} pays - (标段)合同支付数据
  121. */
  122. async calculateStartRangePrice (pays) {
  123. //const order = this.stage.curOrder;
  124. for (const p of pays) {
  125. // 非本期,本次添加的合同支付项,不允许计算,其中默认添加的合同支付项,归属于第一期原报
  126. // if (p.csorder === this.stage.order || (p.csorder === 0 || this.stage.order === 1)) {
  127. // if (p.csaorder === order) {
  128. // }
  129. // }
  130. // 上一期已计量的合同支付项,不予计算起扣金额、扣款限额
  131. if (!p.pre_used) {
  132. if (!p.sprice && p.sexpr && p.sexpr !== '') {
  133. p.sprice = this.ctx.helper.round(this._calculateExpr(p.sexpr), this.decimal);
  134. } else if (p.sprice && !p.sexpr) {
  135. p.sprice = this.ctx.helper.round(p.sprice, this.decimal);
  136. }
  137. if (!p.rprice && p.rexpr && p.rexpr !== '') {
  138. p.rprice = this.ctx.helper.round(this._calculateExpr(p.rexpr), this.decimal);
  139. } else if (p.rprice && !p.rexpr) {
  140. p.rprice = this.ctx.helper.round(p.rprice, this.decimal);
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * 累计 计量等数据
  147. */
  148. async _getAddCalcRela() {
  149. if (this.cur && this.add) return;
  150. this.pre = this.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getSumTotalPrice(this.stage.tid, this.stage.order - 1) : {};
  151. this.cur = await this.ctx.service.stageBills.getSumTotalPrice(this.stage);
  152. this.add = {};
  153. if (this.pre) {
  154. this.add.contract_tp = this.ctx.helper.add(this.pre.contract_tp, this.cur.contract_tp);
  155. this.add.qc_tp = this.ctx.helper.add(this.pre.qc_tp, this.cur.qc_tp);
  156. } else {
  157. this.add.contract_tp = this.cur.contract_tp;
  158. this.add.qc_tp = this.cur.qc_tp;
  159. }
  160. this.add.gather_tp = this.ctx.helper.add(this.add.contract_tp, this.add.qc_tp);
  161. }
  162. /**
  163. * 检查是否到达 计提期限
  164. * @param pay
  165. */
  166. _checkDeadline(pay) {
  167. if (pay.dl_type === deadlineType.tp.value) {
  168. const deadlineTp = this.add[pay.dl_tp_type + '_tp'];
  169. if (deadlineTp > pay.dl_tp) {
  170. return true;
  171. }
  172. } else if (pay.dl_type === deadlineType.count.value) {
  173. return this.stage.order >= pay.dl_count;
  174. } else {
  175. return false;
  176. }
  177. }
  178. getLeafOrder(data, pays) {
  179. if (!data) return [];
  180. if (!data.expr) return [`f${data.order}`];
  181. const orderParam = data.expr.match(this.orderReg);
  182. if (!orderParam || orderParam.length === 0) return [`f${data.order}`];
  183. const result = [...orderParam];
  184. for (const op of orderParam) {
  185. const order = op.substring(1, op.length);
  186. result.push(...this.getLeafOrder(pays[parseInt(order) -1], pays));
  187. }
  188. return this.ctx.helper._.uniq(result);
  189. }
  190. sortPaysByCalc(pays) {
  191. for (const pay of pays) {
  192. pay.calcLeaf = this.getLeafOrder(pay, pays);
  193. }
  194. pays.sort((x, y) => {return x.calcLeaf.length - y.calcLeaf.length; });
  195. }
  196. sortPaysByOrder(pays) {
  197. pays.sort((x, y) => { return x.order - y.order; });
  198. }
  199. /**
  200. * 计算本期、截止本期金额
  201. * @param {Array} pays - (标段&期)合同支付数据
  202. */
  203. async calculate(pays) {
  204. this.yf = pays.find(function (p) {
  205. return p.ptype === payType.yf;
  206. });
  207. this.sf = pays.find(function (p) {
  208. return p.ptype === payType.sf;
  209. });
  210. if (!this.yf) return false;
  211. await this._getAddCalcRela();
  212. this.yf.tp = 0;
  213. for (const p of pays) {
  214. if (p.ptype === payType.normal || p.ptype === payType.wc) {
  215. if (!p.pause && (!p.sprice || this.add.gather_tp >= p.sprice)) {
  216. if (p.expr && p.expr !== '') {
  217. const value = this.ctx.helper.round(this._calculateTpExpr(p, pays), this.decimal);
  218. if (p.rprice) {
  219. if (this._checkDeadline(p)) {
  220. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  221. } else {
  222. p.tp = Math.min(this.ctx.helper.sub(p.rprice, p.pre_tp), value);
  223. }
  224. } else {
  225. p.tp = value;
  226. }
  227. } else if (p.tp && !this.ctx.helper.checkZero(p.tp)) {
  228. if (p.rprice) {
  229. if (this._checkDeadline(p)) {
  230. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  231. } else {
  232. p.tp = Math.min(this.ctx.helper.sub(p.rprice, p.pre_tp), this.ctx.helper.round(p.tp, this.decimal));
  233. }
  234. } else {
  235. p.tp = this.ctx.helper.round(p.tp, this.decimal);
  236. }
  237. } else {
  238. if (p.rprice) {
  239. if (this._checkDeadline(p)) {
  240. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  241. }
  242. }
  243. }
  244. } else {
  245. p.tp = 0;
  246. }
  247. // 累加 至 应付
  248. if (p.is_yf) {
  249. if (p.minus) {
  250. this.yf.tp = this.ctx.helper.sub(this.yf.tp, p.tp);
  251. } else {
  252. this.yf.tp = this.ctx.helper.add(this.yf.tp, p.tp);
  253. }
  254. }
  255. }
  256. p.end_tp = this.ctx.helper.round(this.ctx.helper.add(p.tp, p.pre_tp), this.decimal);
  257. }
  258. this.yf.end_tp = this.ctx.helper.add(this.yf.tp, this.yf.pre_tp);
  259. const bqyf = this.bases.find(function (x) {return x.code === 'bqyf'});
  260. if (bqyf) {
  261. bqyf.value = this.yf.tp;
  262. }
  263. if (this.sf.expr === null || this.sf.expr === '') {
  264. if (this.sf.rprice) {
  265. this.sf.tp = Math.min(this.ctx.helper.sub(this.sf.rprice, this.sf.pre_tp), this.yf.tp);
  266. } else {
  267. this.sf.tp = this.yf.tp;
  268. }
  269. } else {
  270. const value = this.ctx.helper.round(this._calculateTpExpr(this.sf, pays), this.decimal);
  271. if (this.sf.rprice) {
  272. this.sf.tp = Math.min(this.ctx.helper.sub(this.sf.rprice, this.sf.pre_tp), value);
  273. } else {
  274. this.sf.tp = this.ctx.helper.round(value, this.decimal);
  275. }
  276. }
  277. this.sf.end_tp = this.ctx.helper.add(this.sf.tp, this.sf.pre_tp);
  278. }
  279. async calculateAll(pays) {
  280. await this.getCalcBase();
  281. await this._getAddCalcRela();
  282. await this.calculateStartRangePrice(pays);
  283. this.sortPaysByCalc(pays);
  284. await this.calculate(pays);
  285. this.sortPaysByOrder(pays);
  286. }
  287. }
  288. module.exports = PayCalculate;