forked from IvanFrescas/MiniLedger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balance.js
58 lines (44 loc) · 1.7 KB
/
Balance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const Balance = (file) => {
var sprintf=require("sprintf-js").sprintf;
const parser = require("./parser");
array_transactions = parser(file);
// console.log(JSON.stringify(array_transactions));
var balanceContent = {}
var movs = [];
var mov = [];
for (transaccion in array_transactions) {
var movements = (array_transactions[transaccion]["movements"].length);
for (var i=0; i<movements; i++) {
var amount = array_transactions[transaccion]["movements"][i]["amount"];
var curr = array_transactions[transaccion]["movements"][i]["currency"];
var desc = array_transactions[transaccion]["movements"][i]["description"];
if (balanceContent.hasOwnProperty(desc)) {
balanceContent[desc][0] += amount
} else {
balanceContent[desc] = [amount, curr]
}
}
}
var sumas = {}
var tipoDeMoneda;
keys = Object.keys(balanceContent);
for (var i in keys) {
tipoDeMoneda = balanceContent[keys[i]][1]
if (sumas.hasOwnProperty(tipoDeMoneda)){
sumas[tipoDeMoneda] += balanceContent[keys[i]][0]
} else {
sumas[tipoDeMoneda] = balanceContent[keys[i]][0]
}
}
for (var i in keys){
var cantidad = `${balanceContent[keys[i]][1] } ${ balanceContent[keys[i]][0]}`;
console.log(sprintf("%20s %5s", cantidad, keys[i]));
};
console.log('--------------------')
for (var key in sumas) {
if (sumas.hasOwnProperty(key)) {
console.log(sprintf("%20s %5s",key, sumas[key]));
}
}
}
module.exports = Balance;