]> Dogcows Code - chaz/homebank/blob - src/hb-import-ofx.c
import homebank-5.1.3
[chaz/homebank] / src / hb-import-ofx.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "homebank.h"
21
22 #include "hb-import.h"
23
24 #ifndef NOOFX
25 #include <libofx/libofx.h>
26 #endif
27
28
29 /****************************************************************************/
30 /* Debug macros */
31 /****************************************************************************/
32 #define MYDEBUG 0
33
34 #if MYDEBUG
35 #define DB(x) (x);
36 #else
37 #define DB(x);
38 #endif
39
40 /* our global datas */
41 extern struct HomeBank *GLOBALS;
42 extern struct Preferences *PREFS;
43
44
45
46 #ifndef NOOFX
47 /*
48 **** OFX part
49 ****
50 **** this part is quite weird,but works
51 ** id is ACCTID
52
53 */
54
55 static Account * ofx_get_account_by_id(gchar *id)
56 {
57 GList *lacc, *list;
58
59 DB( g_print("\n[import] ofx_get_account_by_id\n") );
60 DB( g_print(" -> searching for '%s'\n",id) );
61
62 lacc = list = g_hash_table_get_values(GLOBALS->h_acc);
63 while (list != NULL)
64 {
65 Account *accitem = list->data;
66
67 if( accitem->imported == FALSE)
68 {
69 if(accitem->name && accitem->number && strlen(accitem->number) )
70 {
71 // todo: maybe smartness should be done here
72 if(g_strstr_len(id, -1, accitem->number) != NULL)
73 {
74 return accitem;
75 }
76 }
77 }
78 list = g_list_next(list);
79 }
80 g_list_free(lacc);
81 return NULL;
82 }
83
84 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
85
86 /**
87 * ofx_proc_account_cb:
88 *
89 * The ofx_proc_account_cb event is always generated first, to allow the application to create accounts
90 * or ask the user to match an existing account before the ofx_proc_statement and ofx_proc_transaction
91 * event are received. An OfxAccountData is passed to this event.
92 *
93 */
94 static LibofxProcStatementCallback
95 ofx_proc_account_cb(const struct OfxAccountData data, OfxContext *ctx)
96 {
97 Account *tmp_acc, *dst_acc;
98
99 DB( g_print("** ofx_proc_account_cb()\n") );
100
101 if(data.account_id_valid==true)
102 {
103 DB( g_print(" account_id: '%s'\n", data.account_id) );
104 DB( g_print(" account_name: '%s'\n", data.account_name) );
105 }
106
107 //if(data.account_number_valid==true)
108 //{
109 DB( g_print(" account_number: '%s'\n", data.account_number) );
110 //}
111
112
113 if(data.account_type_valid==true)
114 {
115 DB( g_print(" account_type: '%d'\n", data.account_type) );
116 /*
117 enum:
118 OFX_CHECKING A standard checking account
119 OFX_SAVINGS A standard savings account
120 OFX_MONEYMRKT A money market account
121 OFX_CREDITLINE A line of credit
122 OFX_CMA Cash Management Account
123 OFX_CREDITCARD A credit card account
124 OFX_INVESTMENT An investment account
125 */
126 }
127
128 if(data.currency_valid==true)
129 {
130 DB( g_print(" currency: '%s'\n", data.currency) );
131 }
132
133
134 //find target account
135 dst_acc = ofx_get_account_by_id( (gchar *)data.account_id );
136 DB( g_print(" ** hb account found result is %x\n", (unsigned int)dst_acc) );
137
138
139 // in every case we create an account here
140 tmp_acc = import_create_account((gchar *)data.account_name, (gchar *)data.account_id);
141 DB( g_print(" -> creating tmp account: %d %s - %x\n", tmp_acc->key, data.account_id, (unsigned int)tmp_acc) );
142
143 if( dst_acc != NULL )
144 {
145 tmp_acc->imp_key = dst_acc->key;
146 }
147
148
149 ctx->curr_acc = tmp_acc;
150 ctx->curr_acc_isnew = TRUE;
151
152
153
154
155
156
157
158
159 DB( fputs("\n",stdout) );
160 return 0;
161 }
162
163
164 /**
165 * ofx_proc_statement_cb:
166 *
167 * The ofx_proc_statement_cb event is sent after all ofx_proc_transaction events have been sent.
168 * An OfxStatementData is passed to this event.
169 *
170 */
171 static LibofxProcStatementCallback
172 ofx_proc_statement_cb(const struct OfxStatementData data, OfxContext *ctx)
173 {
174 DB( g_print("** ofx_proc_statement_cb()\n") );
175
176 #ifdef MYDEBUG
177 if(data.ledger_balance_date_valid==true)
178 {
179 struct tm temp_tm;
180
181 temp_tm = *localtime(&(data.ledger_balance_date));
182 g_print("ledger_balance_date : %d%s%d%s%d%s", temp_tm.tm_mday, "/", temp_tm.tm_mon+1, "/", temp_tm.tm_year+1900, "\n");
183 }
184 #endif
185
186 if(data.ledger_balance_valid==true)
187 {
188 if( ctx->curr_acc != NULL && ctx->curr_acc_isnew == TRUE )
189 {
190 ctx->curr_acc->initial = data.ledger_balance;
191 }
192 DB( g_print("ledger_balance: $%.2f%s",data.ledger_balance,"\n") );
193 }
194
195 return 0;
196 }
197
198 /**
199 * ofx_proc_statement_cb:
200 *
201 * An ofx_proc_transaction_cb event is generated for every transaction in the ofx response,
202 * after ofx_proc_statement (and possibly ofx_proc_security is generated.
203 * An OfxTransactionData structure is passed to this event.
204 *
205 */
206 static LibofxProcStatementCallback
207 ofx_proc_transaction_cb(const struct OfxTransactionData data, OfxContext *ctx)
208 {
209 struct tm *temp_tm;
210 GDate date;
211 Transaction *newope;
212
213 DB( g_print("\n** ofx_proc_transaction_cb()\n") );
214
215 newope = da_transaction_malloc();
216
217 // date
218 newope->date = 0;
219 if(data.date_posted_valid && (data.date_posted != 0))
220 {
221 temp_tm = localtime(&data.date_posted);
222 if( temp_tm != 0)
223 {
224 g_date_set_dmy(&date, temp_tm->tm_mday, temp_tm->tm_mon+1, temp_tm->tm_year+1900);
225 newope->date = g_date_get_julian(&date);
226 }
227 }
228 else if (data.date_initiated_valid && (data.date_initiated != 0))
229 {
230 temp_tm = localtime(&data.date_initiated);
231 g_date_set_dmy(&date, temp_tm->tm_mday, temp_tm->tm_mon+1, temp_tm->tm_year+1900);
232 newope->date = g_date_get_julian(&date);
233 }
234
235 // amount
236 if(data.amount_valid==true)
237 {
238 newope->amount = data.amount;
239 }
240
241 // check number :: The check number is most likely an integer and can probably be converted properly with atoi().
242 //However the spec allows for up to 12 digits, so it is not garanteed to work
243 if(data.check_number_valid==true)
244 {
245 newope->info = g_strdup(data.check_number);
246 }
247 //todo: reference_number ?Might present in addition to or instead of a check_number. Not necessarily a number
248
249 //ucfirst
250 //ucword
251
252
253
254 // ofx:name = Can be the name of the payee or the description of the transaction
255 if(data.name_valid==true)
256 {
257 Payee *payitem;
258 gchar *name = NULL;
259
260 //#462919 name to payee or memo
261 DB( g_print(" -> ofxname option: '%d'\n", PREFS->dtex_ofxname) );
262 switch(PREFS->dtex_ofxname)
263 {
264 case 1: //to memo
265 DB( g_print(" -> name to memo: '%s'\n", data.name) );
266 newope->wording = g_strdup(data.name);
267
268 //test
269 //strip_extra_spaces(newope->wording);
270
271 break;
272 case 2: //to payee
273 //manage memo append to payee as well
274 if( (data.memo_valid==true) && (PREFS->dtex_ofxmemo == 3) )
275 {
276 name = g_strjoin(" ", data.name, data.memo, NULL);
277 }
278 else
279 name = g_strdup(data.name);
280
281 g_strstrip(name);
282 //test
283 //strip_extra_spaces(name);
284
285 DB( g_print(" -> name to payee: '%s'\n", name) );
286
287 payitem = da_pay_get_by_name(name);
288 if(payitem == NULL)
289 {
290 DB( g_print(" -> create new payee\n") );
291
292 payitem = da_pay_malloc();
293 payitem->name = name;
294 payitem->imported = TRUE;
295 da_pay_append(payitem);
296
297 if( payitem->imported == TRUE )
298 ctx->ictx->cnt_new_pay += 1;
299 }
300 else
301 {
302 g_free(name);
303 }
304
305 newope->kpay = payitem->key;
306 break;
307 }
308 }
309
310 //memo ( new for v4.2) Extra information not included in name
311
312 DB( g_print(" -> memo is='%d'\n", data.memo_valid) );
313
314 if(data.memo_valid==true)
315 {
316 gchar *old = NULL;
317
318 DB( g_print(" -> oxfmemo option: '%d'\n", PREFS->dtex_ofxmemo) );
319 switch(PREFS->dtex_ofxmemo)
320 {
321 case 1: //add to info
322 old = newope->info;
323 if(old == NULL)
324 newope->info = g_strdup(data.memo);
325 else
326 {
327 newope->info = g_strjoin(" ", old, data.memo, NULL);
328 g_free(old);
329 }
330 break;
331
332 case 2: //add to description
333 old = newope->wording;
334 if(old == NULL)
335 newope->wording = g_strdup(data.memo);
336 else
337 {
338 newope->wording = g_strjoin(" ", old, data.memo, NULL);
339 g_free(old);
340 }
341
342 DB( g_print(" -> should concatenate ='%s'\n", data.memo) );
343 DB( g_print(" -> old='%s', new ='%s'\n", old, newope->wording) );
344
345 break;
346 //case 3 add to payee is managed above
347 }
348
349 }
350
351 // payment
352 if(data.transactiontype_valid==true)
353 {
354 switch(data.transactiontype)
355 {
356 //#740373
357 case OFX_CREDIT:
358 if(newope->amount < 0)
359 newope->amount *= -1;
360 break;
361 case OFX_DEBIT:
362 if(newope->amount > 0)
363 newope->amount *= -1;
364 break;
365 case OFX_INT:
366 newope->paymode = PAYMODE_XFER;
367 break;
368 case OFX_DIV:
369 newope->paymode = PAYMODE_XFER;
370 break;
371 case OFX_FEE:
372 newope->paymode = PAYMODE_FEE;
373 break;
374 case OFX_SRVCHG:
375 newope->paymode = PAYMODE_XFER;
376 break;
377 case OFX_DEP:
378 newope->paymode = PAYMODE_DEPOSIT;
379 break;
380 case OFX_ATM:
381 newope->paymode = PAYMODE_CASH;
382 break;
383 case OFX_POS:
384 if(ctx->curr_acc && ctx->curr_acc->type == ACC_TYPE_CREDITCARD)
385 newope->paymode = PAYMODE_CCARD;
386 else
387 newope->paymode = PAYMODE_DCARD;
388 break;
389 case OFX_XFER:
390 newope->paymode = PAYMODE_XFER;
391 break;
392 case OFX_CHECK:
393 newope->paymode = PAYMODE_CHECK;
394 break;
395 case OFX_PAYMENT:
396 newope->paymode = PAYMODE_EPAYMENT;
397 break;
398 case OFX_CASH:
399 newope->paymode = PAYMODE_CASH;
400 break;
401 case OFX_DIRECTDEP:
402 newope->paymode = PAYMODE_DEPOSIT;
403 break;
404 case OFX_DIRECTDEBIT:
405 newope->paymode = PAYMODE_XFER;
406 break;
407 case OFX_REPEATPMT:
408 newope->paymode = PAYMODE_REPEATPMT;
409 break;
410 case OFX_OTHER:
411
412 break;
413 default :
414
415 break;
416 }
417 }
418
419 if( ctx->curr_acc )
420 {
421
422 newope->kacc = ctx->curr_acc->key;
423 newope->flags |= OF_ADDED;
424
425 if( newope->amount > 0)
426 newope->flags |= OF_INCOME;
427
428 /* ensure utf-8 here, has under windows, libofx not always return utf-8 as it should */
429 #ifndef G_OS_UNIX
430 DB( g_print(" ensure UTF-8\n") );
431
432 newope->info = homebank_utf8_ensure(newope->info);
433 newope->wording = homebank_utf8_ensure(newope->wording);
434 #endif
435
436 ctx->trans_list = g_list_append(ctx->trans_list, newope);
437
438 DB( g_print(" insert newope: acc=%d\n", newope->kacc) );
439
440 if( ctx->curr_acc_isnew == TRUE )
441 {
442 DB( g_print(" sub amount from initial\n") );
443 ctx->curr_acc->initial -= data.amount;
444 }
445 }
446 else
447 {
448 da_transaction_free(newope);
449 }
450
451 return 0;
452 }
453
454
455
456 static LibofxProcStatusCallback
457 ofx_proc_status_cb(const struct OfxStatusData data, OfxContext *ctx)
458 {
459 DB( g_print("** ofx_proc_status_cb()\n") );
460
461 if(data.ofx_element_name_valid==true){
462 DB( g_print(" Ofx entity this status is relevent to: '%s'\n", data.ofx_element_name) );
463 }
464 if(data.severity_valid==true){
465 DB( g_print(" Severity: ") );
466 switch(data.severity){
467 case INFO : DB( g_print("INFO\n") );
468 break;
469 case WARN : DB( g_print("WARN\n") );
470 break;
471 case ERROR : DB( g_print("ERROR\n") );
472 break;
473 default: DB( g_print("WRITEME: Unknown status severity!\n") );
474 }
475 }
476 if(data.code_valid==true){
477 DB( g_print(" Code: %d, name: %s\n Description: %s\n", data.code, data.name, data.description) );
478 }
479 if(data.server_message_valid==true){
480 DB( g_print(" Server Message: %s\n", data.server_message) );
481 }
482 DB( g_print("\n") );
483
484 return 0;
485 }
486
487
488 GList *homebank_ofx_import(gchar *filename, ImportContext *ictx)
489 {
490 OfxContext ctx = { 0 };
491
492 /*extern int ofx_PARSER_msg;
493 extern int ofx_DEBUG_msg;
494 extern int ofx_WARNING_msg;
495 extern int ofx_ERROR_msg;
496 extern int ofx_INFO_msg;
497 extern int ofx_STATUS_msg;*/
498
499 DB( g_print("\n[import] ofx import (libofx=%s) \n", LIBOFX_VERSION_RELEASE_STRING) );
500
501 /*ofx_PARSER_msg = false;
502 ofx_DEBUG_msg = false;
503 ofx_WARNING_msg = false;
504 ofx_ERROR_msg = false;
505 ofx_INFO_msg = false;
506 ofx_STATUS_msg = false;*/
507
508 ctx.ictx = ictx;
509
510 LibofxContextPtr libofx_context = libofx_get_new_context();
511
512 ofx_set_status_cb (libofx_context, (LibofxProcStatusCallback) ofx_proc_status_cb , &ctx);
513 ofx_set_statement_cb (libofx_context, (LibofxProcStatementCallback) ofx_proc_statement_cb , &ctx);
514 ofx_set_account_cb (libofx_context, (LibofxProcAccountCallback) ofx_proc_account_cb , &ctx);
515 ofx_set_transaction_cb(libofx_context, (LibofxProcTransactionCallback)ofx_proc_transaction_cb, &ctx);
516
517 #ifdef G_OS_WIN32
518 //#932959: windows don't like utf8 path, so convert
519 gchar *file = g_win32_locale_filename_from_utf8(filename);
520 libofx_proc_file(libofx_context, file, AUTODETECT);
521 g_free(file);
522 #else
523 libofx_proc_file(libofx_context, filename, AUTODETECT);
524 #endif
525
526 libofx_free_context(libofx_context);
527
528 return ctx.trans_list;
529 }
530
531 #endif
This page took 0.060305 seconds and 5 git commands to generate.