pay_calc.js 12 KB

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