-
Notifications
You must be signed in to change notification settings - Fork 8
/
script.js
195 lines (155 loc) · 5.73 KB
/
script.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
window.onload = function() {
var fileInput = document.getElementById('file-input');
var form = document.getElementById('form');
form.addEventListener('submit', function(e) {
e.preventDefault();
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function(e) {
output = convertRegister(reader.result, document.getElementById('bank').value);
newFileName = file.name.replace('.csv','')+'.qif';
download(output, newFileName);
}
reader.readAsText(file);
});
}
function parseCsv(file, bank) {
var delimiter;
if (bank == 'ing') delimiter = ';';
if (bank == 'asn') delimiter = ',';
if (bank == 'bcee') delimiter = ';';
if (bank == 'rabobank') delimiter = ',';
if (bank == 'abnamro') delimiter = ',';
if (bank == 'seb') delimiter = ',';
if (bank == 'skandia') delimiter = ',';
if (bank == 'jak') delimiter = ',';
if (bank == 'abnamro' || bank == 'ing') {
var header = true;
} else {
var header = false;
}
data = Papa.parse(file, {delimiter: delimiter, header: header});
return data.data;
}
function convertRegister(file, bank) {
var inputData = parseCsv(file, bank);
var registerQif = '!Type:Bank\n\n';
for (var i in inputData) {
var transactionCsv = inputData[i];
// skip rows with too few fields
if (countProperties(transactionCsv) < 3)
continue;
//For skandia, SEB and JAK, skip rows not starting with a year
if ((bank == 'seb' || bank == 'skandia' || bank == 'jak'))
if (transactionCsv[0].substr(0,2) != '20')
continue;
registerQif += convertTransaction(transactionCsv, bank);
}
return registerQif;
}
function convertTransaction(transactionCsv, bank) {
var fields;
if (bank == 'bcee') fields = bcee(transactionCsv);
if (bank == 'ing') fields = ing(transactionCsv);
if (bank == 'rabobank') fields = rabobank(transactionCsv);
if (bank == 'abnamro') fields = abnamro(transactionCsv);
if (bank == 'seb') fields = seb(transactionCsv);
if (bank == 'skandia') fields = skandia(transactionCsv);
if (bank == 'jak') fields = jak(transactionCsv);
if (bank == 'asn') fields = asn(transactionCsv);
var transaction = '';
transaction += 'D' + fields['date'] + '\n';
transaction += 'T' + fields['amount'] + '\n';
transaction += 'P' + fields['payee'] + '\n';
if (fields['category']) transaction += 'L' + fields['category'] + '\n';
transaction += '^\n\n';
return transaction;
}
function bcee(row) {
row[0] = row[0].replace(/\0/g,'');
row[1] = row[1].replace(/\0/g,'');
row[2] = row[2].replace(/\0/g,'');
row[3] = row[3].replace(/\0/g,'');
var fields = {};
fields['date'] = row[0];
fields['amount'] = row[2].replace(/\xA0/g, '').replace(/ /g, '').replace(/,/g, '.'); //this is not a space but some strange character
fields['payee'] = row[1];
fields['payee'] = fields['payee'].replace(/\s\s+/g, ' ').trim();
fields['category'] = '';
return fields;
}
function ing(row) {
row = Object.values(row);
var fields = {};
fields['date'] = row[0].substr(4,2) + '/' + row[0].substr(6,2) + '/' + row[0].substr(0,4);
fields['amount'] = (((row[5] == 'Af' || row[5] == 'Debit') ? '-' : '') + row[6]).replace(/,/g, '.');
fields['payee'] = row[1] + ' ' + row[8] + ' ' + row[3];
fields['payee'] = fields['payee'].replace(/\s\s+/g, ' ').trim();
fields['category'] = row[4];
return fields;
}
function rabobank(row) {
// Rabobank CSV strucure based on:
// https://bankieren.rabobank.nl/klanten/bedrijven/help/specificaties_elektronische_betaaldiensten/rabo_internetbankieren_professional/export/ --> 'CSV (kommagescheiden nieuw)'
var fields = {};
fields['date'] = row[2].substr(4,2) + '/' + row[2].substr(6,2) + '/' + row[2].substr(0,4);
fields['amount'] = (row[3] == 'C' ? row[4] : '-'+row[4]).replace(/,/g, '.');
fields['payee'] = row[5] + ' ' + row[6];
fields['payee'] = fields['payee'].replace(/\s\s+/g, ' ').trim();
fields['category'] = '';
return fields;
}
function abnamro(row) {
var fields = {};
fields['date'] = row['Transactiedatum'].substr(4,2) + '/' + row['Transactiedatum'].substr(6,2) + '/' + row['Transactiedatum'].substr(0,4);
fields['amount'] = row['Transactiebedrag'];
fields['payee'] = row['Omschrijving'];
fields['category'] = '';
return fields;
}
function seb(row) {
var fields = {};
fields['date'] = row[0].substr(5,2) + "/" + row[0].substr(8,2) + "/" + row[0].substr(0,4);
fields['amount'] = row[4].replace(/,/g,'.').replace(/\s/g,'');
fields['payee'] = row[3];
fields['category'] = '';
return fields;
}
function skandia(row) {
var fields = {};
fields['date'] = row[0].substr(5,2) + "/" + row[0].substr(8,2) + "/" + row[0].substr(0,4);
fields['amount'] = row[2].replace(/,/g,'.').replace(/\s/g,'');
fields['payee'] = row[1];
fields['category'] = '';
return fields;
}
function jak(row) {
var fields = {};
fields['date'] = row[0].substr(5,2) + "/" + row[0].substr(8,2) + "/" + row[0].substr(0,4);
fields['amount'] = row[2].replace(/,/g,'.').replace(/\s/g,'');
fields['payee'] = row[1];
fields['category'] = '';
return fields;
}
function asn(row) {
var fields = {};
fields['date'] = row[0].substr(3,2) + "/" + row[0].substr(0,2) + "/" + row[0].substr(6,4);
fields['amount'] = row[10].replace(/,/g,'.').replace(/\s/g,'');
fields['payee'] = row[3] + " " + row[17].replace("'", '');
fields['category'] = '';
return fields;
}
function download(text, filename) {
var element = document.createElement('a');
element.setAttribute('href', 'data:application/x-qif;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function countProperties(obj) {
var l = 0;
for (p in obj) l++;
return l;
}