pay_calc.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. //
  187. // const result = [...orderParam];
  188. // for (const op of orderParam) {
  189. // const order = op.substring(1, op.length);
  190. // if (parseInt(order) === data.order || op === parentOrder) {
  191. // result.push(op);
  192. // } else {
  193. // result.push(...this.getLeafOrder(pays[parseInt(order) -1], pays, `f${data.order}`));
  194. // }
  195. // }
  196. // return this.ctx.helper._.uniq(result);
  197. if (!data || !data.expr) return [];
  198. const orderParam = data.expr.match(this.orderReg);
  199. if (!orderParam || orderParam.length === 0) return [];
  200. const result = [...orderParam];
  201. for (const op of orderParam) {
  202. const order = op.substring(1, op.length);
  203. if (parseInt(order) === data.order || op === parentOrder) {
  204. result.push(op);
  205. } else {
  206. const sub = this.getLeafOrder(pays[parseInt(order) -1], pays, `f${data.order}`);
  207. if (sub.length > 0) {
  208. result.push(...sub);
  209. } else {
  210. result.push(op);
  211. }
  212. }
  213. }
  214. return this.ctx.helper._.uniq(result);
  215. }
  216. sortPaysByCalc(pays) {
  217. for (const pay of pays) {
  218. pay.calcLeaf = this.getLeafOrder(pay, pays);
  219. }
  220. pays.sort((x, y) => {return x.calcLeaf.length - y.calcLeaf.length; });
  221. }
  222. sortPaysByOrder(pays) {
  223. pays.sort((x, y) => { return x.order - y.order; });
  224. }
  225. /**
  226. * 计算本期、截止本期金额
  227. * @param {Array} pays - (标段&期)合同支付数据
  228. */
  229. async calculate(pays) {
  230. this.yf = pays.find(function (p) {
  231. return p.ptype === payType.yf;
  232. });
  233. this.sf = pays.find(function (p) {
  234. return p.ptype === payType.sf;
  235. });
  236. if (!this.yf) return false;
  237. await this._getAddCalcRela();
  238. this.yf.tp = 0;
  239. for (const p of pays) {
  240. if (p.ptype === payType.normal || p.ptype === payType.wc) {
  241. if (!p.pause && (!p.sprice || this.add.gather_tp >= p.sprice)) {
  242. if (p.expr && p.expr !== '') {
  243. const value = this.ctx.helper.round(this._calculateTpExpr(p, pays), this.decimal);
  244. if (p.rprice) {
  245. if (this._checkDeadline(p)) {
  246. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  247. } else {
  248. p.tp = Math.min(this.ctx.helper.sub(p.rprice, p.pre_tp), value);
  249. }
  250. } else {
  251. p.tp = value;
  252. }
  253. } else if (p.tp && !this.ctx.helper.checkZero(p.tp)) {
  254. if (p.rprice) {
  255. if (this._checkDeadline(p)) {
  256. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  257. } else {
  258. p.tp = Math.min(this.ctx.helper.sub(p.rprice, p.pre_tp), this.ctx.helper.round(p.tp, this.decimal));
  259. }
  260. } else {
  261. p.tp = this.ctx.helper.round(p.tp, this.decimal);
  262. }
  263. } else {
  264. if (p.rprice) {
  265. if (this._checkDeadline(p)) {
  266. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  267. }
  268. }
  269. }
  270. } else {
  271. p.tp = 0;
  272. }
  273. // 累加 至 应付
  274. if (p.is_yf) {
  275. if (p.minus) {
  276. this.yf.tp = this.ctx.helper.sub(this.yf.tp, p.tp);
  277. } else {
  278. this.yf.tp = this.ctx.helper.add(this.yf.tp, p.tp);
  279. }
  280. }
  281. }
  282. p.end_tp = this.ctx.helper.round(this.ctx.helper.add(p.tp, p.pre_tp), this.decimal);
  283. }
  284. this.yf.end_tp = this.ctx.helper.add(this.yf.tp, this.yf.pre_tp);
  285. const bqyf = this.bases.find(function (x) {return x.code === 'bqyf'});
  286. if (bqyf) {
  287. bqyf.value = this.yf.tp;
  288. }
  289. if (this.sf.expr === null || this.sf.expr === '') {
  290. if (this.sf.rprice) {
  291. this.sf.tp = Math.min(this.ctx.helper.sub(this.sf.rprice, this.sf.pre_tp), this.yf.tp);
  292. } else {
  293. this.sf.tp = this.yf.tp;
  294. }
  295. } else {
  296. const value = this.ctx.helper.round(this._calculateTpExpr(this.sf, pays), this.decimal);
  297. if (this.sf.rprice) {
  298. this.sf.tp = Math.min(this.ctx.helper.sub(this.sf.rprice, this.sf.pre_tp), value);
  299. } else {
  300. this.sf.tp = this.ctx.helper.round(value, this.decimal);
  301. }
  302. }
  303. this.sf.end_tp = this.ctx.helper.add(this.sf.tp, this.sf.pre_tp);
  304. }
  305. async calculateAll(pays) {
  306. await this.getCalcBase();
  307. await this._getAddCalcRela();
  308. await this.calculateStartRangePrice(pays);
  309. this.sortPaysByCalc(pays);
  310. await this.calculate(pays);
  311. this.sortPaysByOrder(pays);
  312. }
  313. }
  314. module.exports = PayCalculate;