calculator.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. (function ($) {
  2. Calculator = function (opts) {
  3. var obj = this;
  4. this.rate = new Array();
  5. //本息本金还贷
  6. this.loanResult = function (property, model) {
  7. var data = {};
  8. var month = property.year * 12;
  9. data.month = month;
  10. if (property.type == "3") { // 组合型贷款(组合型贷款的计算,只和商业贷款额、和公积金贷款额有关,和按贷款总额计算无关)
  11. data.total = "略"; //房款总额
  12. data.firstPay = 0; //首次还款
  13. data.totalLoan = parseFloat(property.biz) + parseFloat(property.acc); //总贷款
  14. var bizRate = getRate(2, property.year, property.rate); //商贷利率
  15. var accRate = getRate(1, property.year, property.rate); //公积金利率
  16. if (model == 1) { //本金还款
  17. var all_total = 0, items = new Array(), pay = 0;
  18. for (j = 0; j < month; j++) {
  19. pay = getMonthPayForInterest(bizRate, property.biz, month, j) + getMonthPayForInterest(accRate, property.acc, month, j);
  20. all_total += pay;
  21. pay = Math.round(pay * 100) / 100;
  22. items.push({ month: j + 1, pay: pay });
  23. }
  24. data.perMonthPay = items; //每月还款数组
  25. data.totalPay = Math.round(all_total * 100) / 100; //还款总额
  26. data.interestPay = Math.round((all_total - data.totalLoan) * 100) / 100; //支付利息款
  27. } else { //本息还款
  28. data.perMonthPay = getMonthPayForPrincipal(bizRate, property.biz, month) + getMonthPayForPrincipal(accRate, property.acc, month);
  29. data.totalPay = Math.round(data.perMonthPay * month * 100) / 100;
  30. data.interestPay = Math.round((data.totalPay - data.totalLoan) * 100) / 100;
  31. }
  32. } else {
  33. var _rate = getRate(property.type, property.year, property.rate); //利率
  34. if (property.calType == 2) {
  35. data.totalLoan = property.biz;
  36. } else {
  37. data.totalLoan = property.acc;
  38. }
  39. if (model == 1) { //本金还款
  40. var all_total = 0, items = new Array(), pay = 0;
  41. for (j = 0; j < month; j++) {
  42. pay = getMonthPayForInterest(_rate, data.totalLoan, month, j);
  43. all_total += pay;
  44. pay = Math.round(pay * 100) / 100;
  45. items.push({ month: j + 1, pay: pay });
  46. }
  47. data.perMonthPay = items; //每月还款数组
  48. data.totalPay = Math.round(all_total * 100) / 100; //还款总额
  49. data.interestPay = Math.round((all_total - data.totalLoan) * 100) / 100; //支付利息款
  50. } else { //本息还款
  51. data.perMonthPay = getMonthPayForPrincipal(_rate, data.totalLoan, month);
  52. data.totalPay = Math.round(data.perMonthPay * month * 100) / 100;
  53. data.interestPay = Math.round((data.totalPay - data.totalLoan) * 100) / 100;
  54. data.perMonthPay = Math.round(data.perMonthPay * 100) / 100;
  55. }
  56. }
  57. return data;
  58. };
  59. this.getRate = function (type, year, rateIndex) {
  60. var currRate = obj.rate[rateIndex], item, rate = 0;
  61. for (var i = 0; i < currRate.items.length; i++) {
  62. item = currRate.items[i];
  63. if (year > item.min && year <= item.max) {
  64. rate = type == 1 ? item.accuFund : item.bizLoan;
  65. break;
  66. }
  67. }
  68. return rate;
  69. };
  70. //获取月利率 类型(1公积金 2商贷)、期数、选中的利率标准
  71. var getRate = function (type, year, rateIndex) {
  72. return obj.getRate(type, year, rateIndex) / 12;
  73. };
  74. //本金还款的月还款额(参数: 年利率 / 贷款总额 / 贷款总月份 / 贷款当前月0~length-1)
  75. var getMonthPayForInterest = function (lilv, total, month, cur_month) {
  76. // var lilv_month = lilv / 12; //月利率 //return total * lilv_month * Math.pow(1 + lilv_month, month) / ( Math.pow(1 + lilv_month, month) -1 );
  77. var benjin_money = total / month;
  78. return (total - benjin_money * cur_month) * lilv + benjin_money;
  79. }
  80. //本息还款的月还款额(参数: 年利率/贷款总额/贷款总月份)
  81. var getMonthPayForPrincipal = function (lilv, total, month) {
  82. //var lilv_month = lilv / 12;//月利率
  83. return total * lilv * Math.pow(1 + lilv, month) / (Math.pow(1 + lilv, month) - 1);
  84. }
  85. //装载利率
  86. var rateLoad = function (title, start, end, items) {
  87. var temps = new Array();
  88. for (var i = 0; i < items.length; i++) {
  89. temps.push({ min: items[i][0], max: items[i][1], bizLoan: items[i][2], accuFund: items[i][3] });
  90. }
  91. obj.rate.push({
  92. title: title,
  93. start: start,
  94. end: end,
  95. items: temps
  96. });
  97. };
  98. //初始化
  99. var init = function () {
  100. rateLoad('2015.08.26基准利率', 20150826, 0, [[0, 1, 0.046, 0.0275], [1, 5, 0.05, 0.0275], [5, 30, 0.0515, 0.0325]]);
  101. rateLoad('2015.06.28基准利率', 20150628, 0, [[0, 1, 0.0485, 0.03], [1, 5, 0.0525, 0.03], [5, 30, 0.054, 0.035]]);
  102. rateLoad('2015.05.11基准利率', 20150511, 0, [[0, 1, 0.051, 0.0325], [1, 5, 0.055, 0.0325], [5, 30, 0.0565, 0.0375]]);
  103. rateLoad('2015.03.01基准利率', 20150301, 0, [[0, 1, 0.0535, 0.035], [1, 5, 0.0575, 0.035], [5, 30, 0.059, 0.04]]);
  104. rateLoad('2015.08.26利率下限(7折)', 20150826, 0, [[0, 1, 0.0322, 0.0193], [1, 5, 0.035, 0.0193], [5, 30, 0.0361, 0.0228]]);
  105. rateLoad('2015.06.28利率下限(7折)', 20150628, 0, [[0, 1, 0.034, 0.021], [1, 5, 0.0368, 0.021], [5, 30, 0.0378, 0.0245]]);
  106. rateLoad('2015.05.11利率下限(7折)', 20150511, 0, [[0, 1, 0.0357, 0.0228], [1, 5, 0.0385, 0.0228], [5, 30, 0.0396, 0.0263]]);
  107. rateLoad('2015.03.01利率下限(7折)', 20150301, 0, [[0, 1, 0.0374, 0.0245], [1, 5, 0.0403, 0.0245], [5, 30, 0.0413, 0.028]]);
  108. rateLoad('2015.08.26利率下限(85折)', 20150826, 0, [[0, 1, 0.0391, 0.0234], [1, 5, 0.0425, 0.0234], [5, 30, 0.0438, 0.0376]]);
  109. rateLoad('2015.06.28利率下限(85折)', 20150628, 0, [[0, 1, 0.0412, 0.0225], [1, 5, 0.0446, 0.0225], [5, 30, 0.0459, 0.0297]]);
  110. rateLoad('2015.05.11利率下限(85折)', 20150511, 0, [[0, 1, 0.0434, 0.0276], [1, 5, 0.0468, 0.0276], [5, 30, 0.048, 0.0319]]);
  111. rateLoad('2015.03.01利率下限(85折)', 20150301, 0, [[0, 1, 0.0455, 0.0297], [1, 5, 0.0489, 0.0297], [5, 30, 0.0501, 0.034]]);
  112. rateLoad('2015.08.26利率上限(1.1倍)', 20150826, 0, [[0, 1, 0.0506, 0.0303], [1, 5, 0.055, 0.0303], [5, 30, 0.0567, 0.0358]]);
  113. rateLoad('2015.06.28利率上限(1.1倍)', 20150628, 0, [[0, 1, 0.0534, 0.033], [1, 5, 0.0578, 0.033], [5, 30, 0.0594, 0.0385]]);
  114. rateLoad('2015.05.11利率上限(1.1倍)', 20150511, 0, [[0, 1, 0.0561, 0.0358], [1, 5, 0.0605, 0.0358], [5, 30, 0.0622, 0.0413]]);
  115. rateLoad('2015.03.01利率上限(1.1倍)', 20150301, 0, [[0, 1, 0.0589, 0.0385], [1, 5, 0.0633, 0.0385], [5, 30, 0.0649, 0.044]]);
  116. rateLoad('2014.11.22首套房优惠(15%)利率', 20141122, 0, [[0, 1, 0.0476, 0.0375], [1, 5, 0.051, 0.0375], [5, 30, 0.052275, 0.0425]]);
  117. rateLoad('2014.11.22基准利率', 20141122, 0, [[0, 1, 0.056, 0.0375], [1, 5, 0.06, 0.0375], [5, 30, 0.0615, 0.0425]]);
  118. rateLoad('2014.11.22第二套房上浮(10%)利率', 20141122, 0, [[0, 1, 0.0616, 0.0375], [1, 5, 0.066, 0.0375], [5, 30, 0.06765, 0.0425]]);
  119. };
  120. init();
  121. };
  122. })(jQuery);