ideal-credit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +201 -0
- package/package.json +16 -0
package/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
const createGrafic = (
|
|
2
|
+
sum,
|
|
3
|
+
period,
|
|
4
|
+
interest,
|
|
5
|
+
{
|
|
6
|
+
startDate = formatDate(new Date()),
|
|
7
|
+
creditRatesToPay = 0,
|
|
8
|
+
desiredCreditRate = 0,
|
|
9
|
+
desiredTotalRate = 0,
|
|
10
|
+
desiredStartRate = 0,
|
|
11
|
+
desiredStartDateToPay = "",
|
|
12
|
+
} = {}
|
|
13
|
+
) => {
|
|
14
|
+
let rata = {};
|
|
15
|
+
let grafic = [];
|
|
16
|
+
let termenCredit = 0;
|
|
17
|
+
let totalInsertedRates = 0;
|
|
18
|
+
let creditRata = 0;
|
|
19
|
+
let totalRata = 0;
|
|
20
|
+
desiredCreditRate = Number(desiredCreditRate);
|
|
21
|
+
desiredTotalRate = Number(desiredTotalRate);
|
|
22
|
+
// calc in cite luni se achita credit
|
|
23
|
+
termenCredit = creditRatesToPay || period;
|
|
24
|
+
// calc credit rata
|
|
25
|
+
if (desiredCreditRate > 0 && (desiredTotalRate == 0 || !desiredTotalRate)) {
|
|
26
|
+
creditRata = desiredCreditRate;
|
|
27
|
+
} else {
|
|
28
|
+
creditRata = Math.round(sum / termenCredit);
|
|
29
|
+
}
|
|
30
|
+
for (var i = 0; i < Number(period); i++) {
|
|
31
|
+
rata = {
|
|
32
|
+
nr_rata: 0,
|
|
33
|
+
data_rata: "",
|
|
34
|
+
credit_rata: 0,
|
|
35
|
+
dobinda_rata: 0,
|
|
36
|
+
total_rata: 0,
|
|
37
|
+
};
|
|
38
|
+
// nr rata - 0
|
|
39
|
+
let nrRata = desiredStartRate + i + 1;
|
|
40
|
+
//data rata - 1
|
|
41
|
+
let dataRata = "";
|
|
42
|
+
if (desiredStartDateToPay) {
|
|
43
|
+
dataRata = formatDate(addMonths(i, desiredStartDateToPay));
|
|
44
|
+
} else {
|
|
45
|
+
dataRata = formatDate(addMonths(i + 1, startDate));
|
|
46
|
+
}
|
|
47
|
+
//interest din sold - 3
|
|
48
|
+
let interestRata = Math.round(
|
|
49
|
+
(Number(interest) * (Number(sum) - totalInsertedRates)) / 100
|
|
50
|
+
);
|
|
51
|
+
// if custom date exist , on first month
|
|
52
|
+
if (desiredStartDateToPay && desiredStartDateToPay != startDate && i == 0) {
|
|
53
|
+
let diffDays = daysBetween(startDate, dataRata);
|
|
54
|
+
// //check id custom date is bigger than original rata date
|
|
55
|
+
if (diffDays > 0) {
|
|
56
|
+
interestRata = Math.round((interestRata / 30.5) * diffDays);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//calc the credit rata, based on total rata dorita
|
|
60
|
+
if (desiredTotalRate > 0 && desiredTotalRate >= interestRata) {
|
|
61
|
+
creditRata = desiredTotalRate - interestRata;
|
|
62
|
+
}
|
|
63
|
+
//calc last rata credit - 2
|
|
64
|
+
if (Number(period) == i + 1) creditRata = Number(sum) - totalInsertedRates;
|
|
65
|
+
//total lunar - 4
|
|
66
|
+
if (i >= Number(period) - termenCredit) {
|
|
67
|
+
totalRata = creditRata + interestRata;
|
|
68
|
+
} else {
|
|
69
|
+
totalRata = interestRata;
|
|
70
|
+
}
|
|
71
|
+
//total Inserted Rates - for local only
|
|
72
|
+
if (i >= Number(period) - termenCredit) {
|
|
73
|
+
totalInsertedRates = totalInsertedRates + creditRata;
|
|
74
|
+
}
|
|
75
|
+
if (totalInsertedRates > Number(sum)) {
|
|
76
|
+
alert("Suma Total Lunară este prea mare, incercati o suma mai mica");
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
//0
|
|
80
|
+
rata.nr_rata = nrRata;
|
|
81
|
+
//1
|
|
82
|
+
rata.data_rata = dataRata;
|
|
83
|
+
//2
|
|
84
|
+
if (i >= period - termenCredit) rata.credit_rata = creditRata; // else is 0, by default
|
|
85
|
+
//3
|
|
86
|
+
rata.dobinda_rata = interestRata;
|
|
87
|
+
//4
|
|
88
|
+
rata.total_rata = totalRata;
|
|
89
|
+
//add one rata to grafic array
|
|
90
|
+
grafic.push(rata);
|
|
91
|
+
} //for
|
|
92
|
+
//return
|
|
93
|
+
return grafic;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const calcDAE = (grafic, loan, startDate = new Date()) => {
|
|
97
|
+
//check if grafic is not null
|
|
98
|
+
|
|
99
|
+
// loan = loan - comision if exist.
|
|
100
|
+
if (grafic.length == 0) return;
|
|
101
|
+
//new local vars
|
|
102
|
+
let graficDataSuma = [];
|
|
103
|
+
let dae = 0;
|
|
104
|
+
//prepare grafic
|
|
105
|
+
grafic.forEach((rata) => {
|
|
106
|
+
graficDataSuma[rata.data_rata] = rata.total_rata;
|
|
107
|
+
});
|
|
108
|
+
//calc dae
|
|
109
|
+
dae = daeCalculator(loan, startDate, graficDataSuma, 0.01) * 100;
|
|
110
|
+
return dae.toFixed(2);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const todayDate = () => {
|
|
114
|
+
return formatDate(new Date());
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const formatDate = (d) => {
|
|
118
|
+
let date = new Date(d);
|
|
119
|
+
let dd = date.getDate();
|
|
120
|
+
let mm = date.getMonth() + 1;
|
|
121
|
+
let yyyy = date.getFullYear();
|
|
122
|
+
if (dd < 10) dd = "0" + dd;
|
|
123
|
+
if (mm < 10) mm = "0" + mm;
|
|
124
|
+
return yyyy + "-" + mm + "-" + dd;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const addMonths = (value, d = "") => {
|
|
128
|
+
if (!d) d = new Date();
|
|
129
|
+
else d = new Date(d);
|
|
130
|
+
let n = d.getDate();
|
|
131
|
+
d.setDate(1);
|
|
132
|
+
d.setMonth(d.getMonth() + value);
|
|
133
|
+
let res = d.setDate(Math.min(n, getDaysInMonth(d.getFullYear(), d.getMonth())));
|
|
134
|
+
return res;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const isLeapYear = (year) => {
|
|
138
|
+
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const getDaysInMonth = (year, month) => {
|
|
142
|
+
return [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const daysBetween = (startDate, endDate) => {
|
|
146
|
+
return Math.abs(new Date(endDate) - new Date(startDate)) / (24 * 60 * 60 * 1000);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const daeCalculator = (principal, startDateString, payments, guess) => {
|
|
150
|
+
let values = [-1 * principal];
|
|
151
|
+
let days = [1];
|
|
152
|
+
let startDate = new Date(startDateString);
|
|
153
|
+
Object.keys(payments).forEach(function (date) {
|
|
154
|
+
values.push(payments[date]);
|
|
155
|
+
days.push(
|
|
156
|
+
1 +
|
|
157
|
+
Math.ceil(
|
|
158
|
+
Math.abs(new Date(date).getTime() - startDate.getTime()) / (1000 * 3600 * 24)
|
|
159
|
+
)
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
let fx = function (x) {
|
|
163
|
+
let sum = 0;
|
|
164
|
+
days.forEach(function (day, idx) {
|
|
165
|
+
sum += values[idx] * Math.pow(1 + x, (days[0] - day) / 365);
|
|
166
|
+
});
|
|
167
|
+
return sum;
|
|
168
|
+
};
|
|
169
|
+
let fdx = function (x) {
|
|
170
|
+
let sum = 0;
|
|
171
|
+
days.forEach(function (day, idx) {
|
|
172
|
+
sum +=
|
|
173
|
+
(1 / 365) *
|
|
174
|
+
(days[0] - day) *
|
|
175
|
+
values[idx] *
|
|
176
|
+
Math.pow(1 + x, (days[0] - day) / 365 - 1);
|
|
177
|
+
});
|
|
178
|
+
return sum;
|
|
179
|
+
};
|
|
180
|
+
return Math.abs(run(fx, fdx, guess));
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const run = (fx, fdx, guess) => {
|
|
184
|
+
let precision = 4;
|
|
185
|
+
let errorLimit = Math.pow(10, -1 * precision);
|
|
186
|
+
let previousValue = 0;
|
|
187
|
+
do {
|
|
188
|
+
guess = Number(guess);
|
|
189
|
+
previousValue = Number(guess);
|
|
190
|
+
guess = previousValue - Number(fx(guess)) / Number(fdx(guess));
|
|
191
|
+
} while (Math.abs(guess - previousValue) > errorLimit);
|
|
192
|
+
return guess;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
module.exports = {
|
|
196
|
+
createGrafic,
|
|
197
|
+
calcDAE,
|
|
198
|
+
todayDate,
|
|
199
|
+
formatDate,
|
|
200
|
+
addMonths,
|
|
201
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ideal-credit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Helper functions for www.idealcredit.md",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"remove",
|
|
11
|
+
"array",
|
|
12
|
+
"duplicates"
|
|
13
|
+
],
|
|
14
|
+
"author": "Nicolae Cojuhari <nicojuhari@gmail.com>",
|
|
15
|
+
"license": "ISC"
|
|
16
|
+
}
|