pay_calc.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if (!p.pre_used) {
  122. if (!p.sprice && p.sexpr && p.sexpr !== '') {
  123. p.sprice = this.ctx.helper.round(this._calculateExpr(p.sexpr), this.decimal);
  124. } else if (p.sprice && !p.sexpr) {
  125. p.sprice = this.ctx.helper.round(p.sprice, this.decimal);
  126. }
  127. if (!p.rprice && p.rexpr && p.rexpr !== '') {
  128. p.rprice = this.ctx.helper.round(this._calculateExpr(p.rexpr), this.decimal);
  129. } else if (p.rprice && !p.rexpr) {
  130. p.rprice = this.ctx.helper.round(p.rprice, this.decimal);
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * 累计 计量等数据
  137. */
  138. async _getAddCalcRela() {
  139. if (this.cur && this.add) return;
  140. this.pre = this.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getSumTotalPrice(this.stage.tid, this.stage.order - 1) : {};
  141. this.cur = await this.ctx.service.stageBills.getSumTotalPrice(this.stage);
  142. this.add = {};
  143. if (this.pre) {
  144. this.add.contract_tp = this.ctx.helper.add(this.pre.contract_tp, this.cur.contract_tp);
  145. this.add.qc_tp = this.ctx.helper.add(this.pre.qc_tp, this.cur.qc_tp);
  146. } else {
  147. this.add.contract_tp = this.cur.contract_tp;
  148. this.add.qc_tp = this.cur.qc_tp;
  149. }
  150. this.add.gather_tp = this.ctx.helper.add(this.add.contract_tp, this.add.qc_tp);
  151. }
  152. /**
  153. * 检查是否到达 计提期限
  154. * @param pay
  155. */
  156. _checkDeadline(pay) {
  157. if (pay.dl_type === deadlineType.tp.value) {
  158. const deadlineTp = this.add[pay.dl_tp_type + '_tp'];
  159. if (deadlineTp > pay.dl_tp) {
  160. return true;
  161. }
  162. } else if (pay.dl_type === deadlineType.count.value) {
  163. return this.stage.order >= pay.dl_count;
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * 计算本期、截止本期金额
  170. * @param {Array} pays - (标段&期)合同支付数据
  171. */
  172. async calculate(pays) {
  173. this.yf = pays.find(function (p) {
  174. return p.ptype === payType.yf;
  175. });
  176. this.sf = pays.find(function (p) {
  177. return p.ptype === payType.sf;
  178. });
  179. if (!this.yf) return false;
  180. await this._getAddCalcRela();
  181. this.yf.tp = 0;
  182. for (const p of pays) {
  183. if (p.ptype === payType.normal || p.ptype === payType.wc) {
  184. if (!p.pause && (!p.sprice || this.add.gather_tp >= p.sprice)) {
  185. if (p.expr && p.expr !== '') {
  186. const value = this.ctx.helper.round(this._calculateTpExpr(p), this.decimal);
  187. if (p.rprice) {
  188. if (this._checkDeadline(p)) {
  189. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  190. } else {
  191. p.tp = Math.min(this.ctx.helper.sub(p.rprice, p.pre_tp), value);
  192. }
  193. } else {
  194. p.tp = value;
  195. }
  196. } else if (p.tp && !this.ctx.helper.checkZero(p.tp)) {
  197. if (p.rprice) {
  198. if (this._checkDeadline(p)) {
  199. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  200. } else {
  201. p.tp = Math.min(this.ctx.helper.sub(p.rprice, p.pre_tp), this.ctx.helper.round(p.tp, this.decimal));
  202. }
  203. } else {
  204. p.tp = this.ctx.helper.round(p.tp, this.decimal);
  205. }
  206. } else {
  207. if (p.rprice) {
  208. if (this._checkDeadline(p)) {
  209. p.tp = this.ctx.helper.sub(p.rprice, p.pre_tp);
  210. }
  211. }
  212. }
  213. } else {
  214. p.tp = 0;
  215. }
  216. // 累加 至 应付
  217. if (p.is_yf) {
  218. if (p.minus) {
  219. this.yf.tp = this.ctx.helper.sub(this.yf.tp, p.tp);
  220. } else {
  221. this.yf.tp = this.ctx.helper.add(this.yf.tp, p.tp);
  222. }
  223. }
  224. }
  225. p.end_tp = this.ctx.helper.round(this.ctx.helper.add(p.tp, p.pre_tp), this.decimal);
  226. }
  227. this.yf.end_tp = this.ctx.helper.add(this.yf.tp, this.yf.pre_tp);
  228. const bqyf = this.bases.find(function (x) {return x.code === 'bqyf'});
  229. if (bqyf) {
  230. bqyf.value = this.yf.tp;
  231. }
  232. if (this.sf.expr === null || this.sf.expr === '') {
  233. if (this.sf.rprice) {
  234. this.sf.tp = Math.min(this.ctx.helper.sub(this.sf.rprice, this.sf.pre_tp), this.yf.tp);
  235. } else {
  236. this.sf.tp = this.yf.tp;
  237. }
  238. } else {
  239. const value = this.ctx.helper.round(this._calculateTpExpr(this.sf), this.decimal);
  240. if (this.sf.rprice) {
  241. this.sf.tp = Math.min(this.ctx.helper.sub(this.sf.rprice, this.sf.pre_tp), value);
  242. } else {
  243. this.sf.tp = this.ctx.helper.round(value, this.decimal);
  244. }
  245. }
  246. this.sf.end_tp = this.ctx.helper.add(this.sf.tp, this.sf.pre_tp);
  247. }
  248. async calculateAll(pays) {
  249. await this.getCalcBase();
  250. await this._getAddCalcRela();
  251. await this.calculateStartRangePrice(pays);
  252. await this.calculate(pays);
  253. }
  254. }
  255. module.exports = PayCalculate;