-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankHandler.gs
271 lines (199 loc) · 9.42 KB
/
bankHandler.gs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
function bankLog(message){
sheetLog('bankLog', message);
}
function getNewDataFromBank(token){
var url = "https://www.fio.cz/ib_api/rest/last/" + token + "/transactions.json"
var data = UrlFetchApp.fetch(url);
var decodedData = processDataFromBank(data);
if(decodedData !== null) {
var dataInfo = decodedData.accountStatement.info;
runtimeLog("Data ids from:" + dataInfo.idFrom + "to:" + dataInfo.idTo);
bankLog(["Data ids from to", dataInfo.idFrom , dataInfo.idTo]);
}
return decodedData;
}
function getTestingDataFromBank(){
var token = getBankSecret();
var url = "https://www.fio.cz/ib_api/rest/periods/" + token + "/2016-07-25/2016-09-10/transactions.json"
var data = UrlFetchApp.fetch(url);
return processDataFromBank(data);
}
function processDataFromBank(data){
var responseCode = data.getResponseCode();
var contentText = data.getContentText();
if(responseCode != 200){
logError(['Could not connect to bank API:', responseCode, contentText]);
return null;
}
var responseObject = JSON.parse(contentText);
return responseObject;
}
function extactTransactions(transactionsRaw){
var transactionDictionary = {};
runtimeLog('transactions' + transactionsRaw.length);
for(var i = 0; i < transactionsRaw.length; ++i){
var currTransaction = transactionsRaw[i];
var processedTransaction = extractTransaction(currTransaction);
if (processedTransaction == null) { continue; }
//filter the ones that we don't wont (don't have variable symbol, transfer id, or date')
if (!(processedTransaction.hasOwnProperty('accountNumber') && processedTransaction.hasOwnProperty('variableSymbol'))){ continue; }
var variableSymbol = processedTransaction.variableSymbol;
if(!transactionDictionary.hasOwnProperty(variableSymbol)){
transactionDictionary[variableSymbol] = [];
}
transactionDictionary[variableSymbol].push(processedTransaction);
}
return transactionDictionary;
}
function extractTransaction(transaction){
var bankConfig = getBankConfig();
var transactionObject = {};
var transactionParametersArray = objectValuesToArray(transaction);
for(var i = 0; i < transactionParametersArray.length; ++i){
var currParameter = transactionParametersArray[i];
if(currParameter == null) {continue;}
var currParameterName = currParameter.name;
var currParameterValue = currParameter.value;
if(bankConfig.hasOwnProperty(currParameterName)){
var properPropertyName = bankConfig[currParameterName];
transactionObject[properPropertyName] = currParameterValue;
}
}
handleAdditionalInfoWhenSendingEurosToCZKAccount(transactionObject);
return transactionObject;
}
function handleAdditionalInfoWhenSendingEurosToCZKAccount(transactionObj){
if(!transactionObj.hasOwnProperty('additionalInfo')) { return;}
var additionalInfo = transactionObj['additionalInfo'];
var match = additionalInfo.match(/^([0-9]+\.[0-9]+) EUR$/);
if(match != null && match.length < 2){ return; }
var newAmountEUR = match[1];
transactionObj['currencyOrig'] = transactionObj.currency;
transactionObj['amountOrig'] = transactionObj.amount;
transactionObj.currency = 'EUR';
transactionObj.amount = newAmountEUR;
runtimeLog(transactionObj);
}
function writeDownTransactionsToBankInfo(transactionDictionary){
var varSymbolIndex = 2;
var manualOverrideIndex = 3;
var bankSheetRange = getActiveRange('money info');
if(bankSheetRange == null) { return; } //No info to be processed
var bankSheetData = bankSheetRange.getValues();
//First row is header;
for(var i = 1; i < bankSheetData.length; ++i){
var bankData = bankSheetData[i];
var manOverride = bankData[manualOverrideIndex];
if(manOverride) { continue; }
var varSymbol = bankData[varSymbolIndex];
if(!transactionDictionary.hasOwnProperty(varSymbol)){ continue; }
var transactionsForVarSymbol = transactionDictionary[varSymbol];
for(var j = 0; j < transactionsForVarSymbol.length; ++j){
var currTransaction = transactionsForVarSymbol[j];
writeDownTransactionToBankInfo(currTransaction, bankSheetRange, i);
}
}
}
function writeDownTransactionToBankInfo(transactionObj, bankSheetRange, rowIndexInRange){
var sheet = bankSheetRange.getSheet();
var values = bankSheetRange.getValues()[rowIndexInRange];
var userEmailIndex = 4;
var userLanguageIndex = 5;
var userEmail = values[userEmailIndex];
var userLanguage = values[userLanguageIndex];
var manualOverrideIndex = 3;
if(transactionObj.currency == 'CZK'){
var finalPriceIndex = 6;
var alreadyPaidIndex = 8
var paidDepositIndex = 10;
var paidEverythingIndex = 11;
var depositIndex = 12;
}
else if(transactionObj.currency == 'EUR'){
var finalPriceIndex = 7;
var alreadyPaidIndex = 9;
var paidDepositIndex = 10;
var paidEverythingIndex = 11;
var depositIndex = 13;
}
else {
logNeedsAttention(['Someone paid in non-supportd currency.', transactionObj.currency, transactionObj.amount], userEmail, transactionObj.variableSymbol);
sheet.getRange(rowIndexInRange + 1, manualOverrideIndex + 1).setValue(true);
return;
}
var values = bankSheetRange.getValues()[rowIndexInRange];
var alreadyPaid = reliableToInt(values[alreadyPaidIndex]);
var finalPrice = reliableToInt(values[finalPriceIndex]);
var deposit = reliableToInt(values[depositIndex]);
var justPaid = reliableToInt(transactionObj.amount);
var alreadyPaidNew = alreadyPaid + justPaid;
sheet.getRange(rowIndexInRange + 1, alreadyPaidIndex + 1).setValue(alreadyPaidNew);
var paidDeposit = (alreadyPaidNew >= deposit);
var paidEverything = (alreadyPaidNew >= finalPrice);
var paidDepositBefore = values[paidDepositIndex];
var paidEverythingBefore = values[paidEverythingIndex];
var variablesObject = {
'varSymbol' : transactionObj.variableSymbol,
'alreadyPaidOld' : alreadyPaid,
'alreadyPaidNew' : alreadyPaidNew,
'deposit' : deposit,
'currency' : transactionObj.currency,
'finalPrice' : finalPrice,
'leftToBePaid' : finalPrice - alreadyPaidNew,
}
var emailTemplate = null;
if(paidEverything && !paidEverythingBefore){
sheet.getRange(rowIndexInRange + 1, paidEverythingIndex + 1).setValue(true);
}
if(paidDeposit && !paidDepositBefore){
sheet.getRange(rowIndexInRange + 1, paidDepositIndex + 1).setValue(true);
}
if(paidEverything && !paidEverythingBefore){
emailTemplate = getPaidEverythingtEmail(userLanguage);
}
else if(paidDeposit && !paidDepositBefore){
emailTemplate = getPaidDepositEmail(userLanguage);
}
else if(!paidDeposit){
emailTemplate = getNotYetDepositEmail(userLanguage);
}
else {
emailTemplate = getNotYetEverythingEmail(userLanguage);
}
bankLog(["New payment:"].concat(objectValuesToArray(variablesObject)));
var templatedData = fillInTemplate(emailTemplate.text, variablesObject);
var subject = emailTemplate.subject;
sendEmail(userEmail, subject, templatedData, undefined);
}
function onGetBankingDataTick(){
var tokens = getBankSecret();
for(var i = 0; i < tokens.length; ++i){
var data = getNewDataFromBank(tokens[i]);
var transactionsRaw = data.accountStatement.transactionList.transaction;
var transactionsDictionary = extactTransactions(transactionsRaw);
writeDownTransactionsToBankInfo(transactionsDictionary);
}
}
function testBankAccess(){
var data = getTestingDataFromBank();
var transactionsRaw = data.accountStatement.transactionList.transaction;
var transactionsDictionary = extactTransactions(transactionsRaw);
bankLog(transactionsDictionary)
}
function testBankWriteDown(){
var transactionDictionary = {"0":[
{"transferId":10810816122,"date":"2016-07-25+0200","amount":270,"currency":"CZK","accountNumber":"250759191","variableSymbol":"0"},
{"transferId":10810885551,"date":"2016-07-25+0200","amount":220,"currency":"CZK","accountNumber":"35-4901320237","variableSymbol":"0"},
{"transferId":10810887096,"date":"2016-07-25+0200","amount":220,"currency":"CZK","accountNumber":"43-6614030287","variableSymbol":"0"},
{"transferId":10824264139,"date":"2016-08-23+0200","amount":440,"currency":"CZK","accountNumber":"43-5055990227","variableSymbol":"0"},
{"transferId":10824269003,"date":"2016-08-23+0200","amount":220,"currency":"CZK","accountNumber":"107-3120930257","variableSymbol":"0"},
{"transferId":10824755392,"date":"2016-08-24+0200","amount":220,"currency":"CZK","accountNumber":"224099476","variableSymbol":"0"},
{"transferId":11331170097,"date":"2016-09-07+0200","amount":500,"currency":"CZK","accountNumber":"107431492","variableSymbol":"0"}],
"0093816071":[{"transferId":10821625937,"date":"2016-08-16+0200","amount":-349,"currency":"CZK","accountNumber":"2300203634","variableSymbol":"0093816071"}],
"905806":[{"transferId":10822317506,"date":"2016-08-18+0200","amount":220,"currency":"CZK","accountNumber":"670100-2212691412","variableSymbol":"905806"}],
"12345":[{"transferId":10824880789,"date":"2016-08-24+0200","amount":220,"currency":"CZK","accountNumber":"2800394860","variableSymbol":"12345"}],
"24681012":[{"transferId":11331260158,"date":"2016-09-07+0200","amount":2000,"currency":"CZK","accountNumber":"1015593472","variableSymbol":"24681012"}],
//The last one is used for testing
"1441690989":[{"transferId":11331926089,"date":"2016-09-08+0200","amount":40,"currency":"CZK","accountNumber":"2300203634","variableSymbol":"1441690989"}]}
writeDownTransactionsToBankInfo(transactionDictionary);
}