pay_calc.js 12 KB

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