pay_calc.js 9.0 KB

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