pay_calc.js 10 KB

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