// calculateMinimumInstallment()
// -----------------------------------------------------------------------------------------------------
// Calculates and returns the smallest monthly payment (min EUR 17), including interest.
// Does not take a stand on whether the credit amount is too low or high.
// -----------------------------------------------------------------------------------------------------
// double amount - credit amount
// double interest - reference interest
// double margin - margin rate
// double monthlyFee - account management fee per month
function calculateMinimumInstallment(amount, interest, margin, monthlyFee) {
installment = 17; // lowest possible monthly payment as the lowest possible credit amount is EUR 200
while (1) {
if (installment <= 2 * (amount * (interest+margin) / 1200) + (2 * monthlyFee)) {
installment++;
}
else {
break;
}
}
return installment;
}
// calculateEstimation()
// ---------------------------------------------------------------------------------------------------------
// Calculates the loan period, account management fees, interest, credit expenses, credit price and
// actual annual interest based on the monthly payment, price of the product, reference interest,
// margin rate and account management fee without instalment-free months. Returns the values
// in an html table. The monthly payment must include interest.
// The product price must be between EUR 200 and 10,000.
// The loan period must be between 6 and 120 months.
// ---------------------------------------------------------------------------------------------------------
// double loanAmountMonth - monthly payment
// double amount - product price
// double interest - reference interest
// double margin - margin rate
// double monthlyFee - account management fee per month
function calculateEstimation(loanAmountMonth, amount, interest, margin, monthlyFee) {
loanPeriod = 0; // loan period, in months
saldo = amount; // outstanding balance, in euros
totalInterest = 0; // total interest, in euros
transactions = []; // instalments for the calculation of actual annual interest
transactions[0] = amount; // product price for the calculation of actual annual interest
if (amount < 200) {
return "
| Minimum credit amount is EUR 200&;. |
";
}
else if (amount > 10000) {
return "| Maximum credit amount is EUR 10,000&;. |
";
}
// Monthly payment is lower than interest
if (loanAmountMonth <= 2 * (saldo * (interest+margin) / 1200) + (2 * monthlyFee)) {
return "| Choose higher monthly payment. |
";
}
while (saldo > loanAmountMonth) {
interestPaid = saldo * (interest+margin) / 1200; // interest per month
installment = loanAmountMonth - monthlyFee - interestPaid; // proportion of instalment in monthly payment
saldo -= installment; // outstanding balance
totalInterest += interestPaid; // interest up to date
loanPeriod ++; // loan period up to date
transactions[loanPeriod] = "-"+loanAmountMonth; // instalment for the calculation of actual annual interest
}
interestPaid = saldo * (interest+margin) / 1200; // interest of the last month
totalInterest += interestPaid; // final total interest
loanPeriod ++; // final loan period
transactions[loanPeriod] = "-"+(saldo+interestPaid+monthlyFee); // instalment for the calculation of actual annual interest
if (loanPeriod < 6) {
return "| Choose lower monthly payment. |
";
}
else if (loanPeriod > 120) {
return "| Choose higher monthly payment. |
";
}
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer")
{
return "| Laina-aika | "
+ loanPeriod +
" kk |
| Kuukausierä | "
+ loanAmountMonth +
" € |
| Tilinhoitomaksut | "
+ loanPeriod * monthlyFee +
" € |
| Korot yhteensä | "
+ totalInterest.toFixed(0) +
" € |
| Luoton kustannukset yht. | "
+ ((loanPeriod * monthlyFee) + totalInterest).toFixed(0) +
" € |
| Luottohinta | "
+ ((loanPeriod * monthlyFee) + totalInterest + amount).toFixed(0) +
" € |
| Todellinen vuosikorko | "
+ calculateActualInterestRate(transactions, interest+margin).toFixed(1) +
" % |
";
}else{
//alert(document.getElementById('calc1').value);
//document.getElementById('calc1').innerHTML = loanPeriod;
//document.getElementById('calc11').innerHTML = loanPeriod;
//alert(document.getElementById('calc1').value);
}
}
// calculateActualInterestRate()
// ---------------------------------------------------------------------------------------------------------
// Calculates and returns the actual annual interest without interest-free months.
// Help function of CalculateEstimation().
// ---------------------------------------------------------------------------------------------------------
// array transactions - product price and instalments, eg [200, -25, -25, -25, -25, -25, -25, -25, -25, -25, -16.23]
// double interest - interest
function calculateActualInterestRate (transactions, interest) {
d = 1;
j = 0;
x = 1 + interest / 100;
error = false;
date = new Date();
year = date.getFullYear(); // current year
month = date.getMonth(); // current month
theLastYear = year + parseInt(((transactions.length)/12)); // year of last instalment
remainderMonths = (transactions.length) % 12; // month of last instalment
if (month + remainderMonths > 12) {
theLastYear ++;
theLastMonth = (month + remainderMonths) - 12;
}
else {
theLastMonth = month + remainderMonths;
}
theLastDate = new Date(theLastYear, theLastMonth, 15); // date of last instalment
do {
prev_d = d;
sum1 = 0;
sum2 = 0;
transactionMonth = month;
transactionYear = year;
for (k = 0; k < transactions.length; k++) {
transactionDate = new Date(transactionYear, transactionMonth, 15); // date of this instalment
days = Math.abs(theLastDate - transactionDate) / (1000 * 60 * 60 * 24);
years = days / 365.25;
xPowSum1 = Math.pow(x, years);
xPowSum2 = Math.pow(x, years-1);
sum1 += transactions[k] * xPowSum1;
sum2 += transactions[k] * years * xPowSum2;
if (transactionMonth < 11) {
transactionMonth ++; // month of next instalment
}
else {
transactionMonth = 0; // month of next instalment
transactionYear ++; // year of next instalment
}
}
if (sum2) {
d = sum1/sum2;
x -= d;
}
else {
error = true;
}
j++;
} while (Math.abs(d) >= 0.0001 && j < 100 && Math.abs(d) < Math.abs(prev_d) && sum2 != 0.0 );
if ((j < 100) && !error) {
return (x - 1) * 100;
}
return 0;
}