1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 Maxime DOYEN
4 * This file is part of HomeBank.
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.
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.
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/>.
22 #include "hb-import.h"
25 #include <libofx/libofx.h>
29 /****************************************************************************/
31 /****************************************************************************/
40 /* our global datas */
41 extern struct HomeBank
*GLOBALS
;
42 extern struct Preferences
*PREFS
;
50 **** this part is quite weird,but works
55 static Account
* ofx_get_account_by_id(gchar
*id
)
59 DB( g_print("\n[import] ofx_get_account_by_id\n") );
60 DB( g_print(" -> searching for '%s'\n",id
) );
62 lacc
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
65 Account
*accitem
= list
->data
;
67 if( accitem
->imported
== FALSE
)
69 if(accitem
->name
&& accitem
->number
&& strlen(accitem
->number
) )
71 // todo: maybe smartness should be done here
72 if(g_strstr_len(id
, -1, accitem
->number
) != NULL
)
78 list
= g_list_next(list
);
84 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
87 * ofx_proc_account_cb:
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.
94 static LibofxProcStatementCallback
95 ofx_proc_account_cb(const struct OfxAccountData data
, OfxContext
*ctx
)
97 Account
*tmp_acc
, *dst_acc
;
99 DB( g_print("** ofx_proc_account_cb()\n") );
101 if(data
.account_id_valid
==true)
103 DB( g_print(" account_id: '%s'\n", data
.account_id
) );
104 DB( g_print(" account_name: '%s'\n", data
.account_name
) );
107 //if(data.account_number_valid==true)
109 DB( g_print(" account_number: '%s'\n", data
.account_number
) );
113 if(data
.account_type_valid
==true)
115 DB( g_print(" account_type: '%d'\n", data
.account_type
) );
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
128 if(data
.currency_valid
==true)
130 DB( g_print(" currency: '%s'\n", data
.currency
) );
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
) );
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
) );
143 if( dst_acc
!= NULL
)
145 tmp_acc
->imp_key
= dst_acc
->key
;
149 ctx
->curr_acc
= tmp_acc
;
150 ctx
->curr_acc_isnew
= TRUE
;
159 DB( fputs("\n",stdout
) );
165 * ofx_proc_statement_cb:
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.
171 static LibofxProcStatementCallback
172 ofx_proc_statement_cb(const struct OfxStatementData data
, OfxContext
*ctx
)
174 DB( g_print("** ofx_proc_statement_cb()\n") );
177 if(data
.ledger_balance_date_valid
==true)
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");
186 if(data
.ledger_balance_valid
==true)
188 if( ctx
->curr_acc
!= NULL
&& ctx
->curr_acc_isnew
== TRUE
)
190 ctx
->curr_acc
->initial
= data
.ledger_balance
;
192 DB( g_print("ledger_balance: $%.2f%s",data
.ledger_balance
,"\n") );
199 * ofx_proc_statement_cb:
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.
206 static LibofxProcStatementCallback
207 ofx_proc_transaction_cb(const struct OfxTransactionData data
, OfxContext
*ctx
)
213 DB( g_print("\n** ofx_proc_transaction_cb()\n") );
215 newope
= da_transaction_malloc();
219 if(data
.date_posted_valid
&& (data
.date_posted
!= 0))
221 temp_tm
= localtime(&data
.date_posted
);
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
);
228 else if (data
.date_initiated_valid
&& (data
.date_initiated
!= 0))
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
);
236 if(data
.amount_valid
==true)
238 newope
->amount
= data
.amount
;
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)
245 newope
->info
= g_strdup(data
.check_number
);
247 //todo: reference_number ?Might present in addition to or instead of a check_number. Not necessarily a number
254 // ofx:name = Can be the name of the payee or the description of the transaction
255 if(data
.name_valid
==true)
260 //#462919 name to payee or memo
261 DB( g_print(" -> ofxname option: '%d'\n", PREFS
->dtex_ofxname
) );
262 switch(PREFS
->dtex_ofxname
)
265 DB( g_print(" -> name to memo: '%s'\n", data
.name
) );
266 newope
->wording
= g_strdup(data
.name
);
269 //strip_extra_spaces(newope->wording);
273 //manage memo append to payee as well
274 if( (data
.memo_valid
==true) && (PREFS
->dtex_ofxmemo
== 3) )
276 name
= g_strjoin(" ", data
.name
, data
.memo
, NULL
);
279 name
= g_strdup(data
.name
);
283 //strip_extra_spaces(name);
285 DB( g_print(" -> name to payee: '%s'\n", name
) );
287 payitem
= da_pay_get_by_name(name
);
290 DB( g_print(" -> create new payee\n") );
292 payitem
= da_pay_malloc();
293 payitem
->name
= name
;
294 payitem
->imported
= TRUE
;
295 da_pay_append(payitem
);
297 if( payitem
->imported
== TRUE
)
298 ctx
->ictx
->cnt_new_pay
+= 1;
305 newope
->kpay
= payitem
->key
;
310 //memo ( new for v4.2) Extra information not included in name
312 DB( g_print(" -> memo is='%d'\n", data
.memo_valid
) );
314 if(data
.memo_valid
==true)
318 DB( g_print(" -> oxfmemo option: '%d'\n", PREFS
->dtex_ofxmemo
) );
319 switch(PREFS
->dtex_ofxmemo
)
321 case 1: //add to info
324 newope
->info
= g_strdup(data
.memo
);
327 newope
->info
= g_strjoin(" ", old
, data
.memo
, NULL
);
332 case 2: //add to description
333 old
= newope
->wording
;
335 newope
->wording
= g_strdup(data
.memo
);
338 newope
->wording
= g_strjoin(" ", old
, data
.memo
, NULL
);
342 DB( g_print(" -> should concatenate ='%s'\n", data
.memo
) );
343 DB( g_print(" -> old='%s', new ='%s'\n", old
, newope
->wording
) );
346 //case 3 add to payee is managed above
352 if(data
.transactiontype_valid
==true)
354 switch(data
.transactiontype
)
358 if(newope
->amount
< 0)
359 newope
->amount
*= -1;
362 if(newope
->amount
> 0)
363 newope
->amount
*= -1;
366 newope
->paymode
= PAYMODE_XFER
;
369 newope
->paymode
= PAYMODE_XFER
;
372 newope
->paymode
= PAYMODE_FEE
;
375 newope
->paymode
= PAYMODE_XFER
;
378 newope
->paymode
= PAYMODE_DEPOSIT
;
381 newope
->paymode
= PAYMODE_CASH
;
384 if(ctx
->curr_acc
&& ctx
->curr_acc
->type
== ACC_TYPE_CREDITCARD
)
385 newope
->paymode
= PAYMODE_CCARD
;
387 newope
->paymode
= PAYMODE_DCARD
;
390 newope
->paymode
= PAYMODE_XFER
;
393 newope
->paymode
= PAYMODE_CHECK
;
396 newope
->paymode
= PAYMODE_EPAYMENT
;
399 newope
->paymode
= PAYMODE_CASH
;
402 newope
->paymode
= PAYMODE_DEPOSIT
;
404 case OFX_DIRECTDEBIT
:
405 newope
->paymode
= PAYMODE_XFER
;
408 newope
->paymode
= PAYMODE_REPEATPMT
;
422 newope
->kacc
= ctx
->curr_acc
->key
;
423 newope
->flags
|= OF_ADDED
;
425 if( newope
->amount
> 0)
426 newope
->flags
|= OF_INCOME
;
428 /* ensure utf-8 here, has under windows, libofx not always return utf-8 as it should */
430 DB( g_print(" ensure UTF-8\n") );
432 newope
->info
= homebank_utf8_ensure(newope
->info
);
433 newope
->wording
= homebank_utf8_ensure(newope
->wording
);
436 ctx
->trans_list
= g_list_append(ctx
->trans_list
, newope
);
438 DB( g_print(" insert newope: acc=%d\n", newope
->kacc
) );
440 if( ctx
->curr_acc_isnew
== TRUE
)
442 DB( g_print(" sub amount from initial\n") );
443 ctx
->curr_acc
->initial
-= data
.amount
;
448 da_transaction_free(newope
);
456 static LibofxProcStatusCallback
457 ofx_proc_status_cb(const struct OfxStatusData data
, OfxContext
*ctx
)
459 DB( g_print("** ofx_proc_status_cb()\n") );
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
) );
464 if(data
.severity_valid
==true){
465 DB( g_print(" Severity: ") );
466 switch(data
.severity
){
467 case INFO
: DB( g_print("INFO\n") );
469 case WARN
: DB( g_print("WARN\n") );
471 case ERROR
: DB( g_print("ERROR\n") );
473 default: DB( g_print("WRITEME: Unknown status severity!\n") );
476 if(data
.code_valid
==true){
477 DB( g_print(" Code: %d, name: %s\n Description: %s\n", data
.code
, data
.name
, data
.description
) );
479 if(data
.server_message_valid
==true){
480 DB( g_print(" Server Message: %s\n", data
.server_message
) );
488 GList
*homebank_ofx_import(gchar
*filename
, ImportContext
*ictx
)
490 OfxContext ctx
= { 0 };
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;*/
499 DB( g_print("\n[import] ofx import (libofx=%s) \n", LIBOFX_VERSION_RELEASE_STRING
) );
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;*/
510 LibofxContextPtr libofx_context
= libofx_get_new_context();
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
);
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
);
523 libofx_proc_file(libofx_context
, filename
, AUTODETECT
);
526 libofx_free_context(libofx_context
);
528 return ctx
.trans_list
;