/********************************************************************\ * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License* * along with this program; if not, contact: * * * * Free Software Foundation Voice: +1-617-542-5942 * * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * * Boston, MA 02111-1307, USA gnu@gnu.org * \********************************************************************/ /** @addtogroup Import_Export @{ */ /** @internal @file gnc-mt940-import.c @brief MT940 import module code @author Copyright (c) 2002 Benoit Grégoire , Copyright (c) 2003 Jan-Pascal van Best */ #define _GNU_SOURCE #include "config.h" #include #include #include #include #include #include #include "import-account-matcher.h" #include "import-main-matcher.h" #include "Account.h" #include "Transaction.h" #include "global-options.h" #include "gnc-book.h" #include "gnc-date.h" #include "gnc-engine-util.h" #include "gnc-file-dialog.h" #include "gnc-ui-util.h" #include "gnc-hbci-utils.h" #include "gnc-mt940-import.h" static short module = MOD_IMPORT; static void *trans_importer_cb (const HBCI_Transaction *h_trans, void *user_data); /********************************************************************\ * gnc_file_mt940_import * Entry point \********************************************************************/ SCM scm_gnc_file_mt940_import () { gnc_file_mt940_import(); return SCM_EOL; } void gnc_file_mt940_import (void) { const char *selected_filename; char *default_dir; FILE *mt940_file; GNCImportMainMatcher *gnc_mt940_importer_gui = NULL; gnc_should_log(MOD_IMPORT, GNC_LOG_TRACE); DEBUG("gnc_file_mt940_import(): Begin...\n"); default_dir = gnc_lookup_string_option("__paths", "Import MT940", NULL); if (default_dir == NULL) gnc_init_default_directory(&default_dir); selected_filename = gnc_file_dialog(_("Select an MT940 file to process"), NULL, default_dir); if(selected_filename!=NULL) { /* Remember the directory as the default. */ gnc_extract_directory(&default_dir, selected_filename); gnc_set_string_option("__paths", "Import MT940", default_dir); g_free(default_dir); /*strncpy(file,selected_filename, 255);*/ DEBUG("Filename found: %s",selected_filename); /* Create the Generic transaction importer GUI. */ gnc_mt940_importer_gui = gnc_gen_trans_list_new(NULL, NULL, FALSE); DEBUG("Opening selected file"); mt940_file = fopen(selected_filename, "r"); fseek(mt940_file,0,SEEK_END); unsigned long filesize=ftell(mt940_file); rewind(mt940_file); char *mt940_records=g_malloc(filesize+1); fread(mt940_records,1,filesize,mt940_file); mt940_records[filesize]='\0'; DEBUG("Read file data: %s\n",mt940_records); { int pos=0; int result; HBCI_transactionReport *tr; const list_HBCI_Transaction *transactions; /*list_HBCI_Transaction_iter *iter;*/ tr=HBCI_transactionReport_new(); while(pos 0) && (g_strncasecmp (custref, "NONREF", 6) != 0)) xaccTransSetNum (gnc_trans, custref); } /* Description */ { char *g_descr = gnc_hbci_descr_tognc (h_trans); xaccTransSetDescription (gnc_trans, g_descr); g_free (g_descr); } /* Notes. */ /*xaccTransSetNotes (gnc_trans, g_notes);*/ /* But Nobody ever uses the Notes field? */ /* Add one split */ split=xaccMallocSplit(book); xaccTransAppendSplit(gnc_trans, split); xaccAccountInsertSplit(gnc_acc, split); { /* Amount into the split */ gnc_numeric gnc_amount = double_to_gnc_numeric (HBCI_Value_getValue (HBCI_Transaction_value (h_trans)), xaccAccountGetCommoditySCU(gnc_acc), GNC_RND_ROUND); xaccSplitSetBaseValue(split, gnc_amount, xaccAccountGetCommodity(gnc_acc)); } /* Memo in the Split. */ { char *g_memo = gnc_hbci_memo_tognc (h_trans); xaccSplitSetMemo(split, g_memo); g_free (g_memo); } /* Instead of xaccTransCommitEdit(gnc_trans) */ gnc_gen_trans_list_add_trans (gnc_importer_gui, gnc_trans); return NULL; } /* list_HBCI_Transaction_foreach callback. The Conversion from HBCI to GNC transaction is done here, once for each HBCI_Transaction. This code is _very_ similar to the code in gnc-hbci-gettrans.c, but this version gets the account from the transactions themselves. */ static void *trans_importer_cb (const HBCI_Transaction *h_trans, void *user_data) { Account *gnc_acc; GNCImportMainMatcher *gnc_importer_gui = user_data; g_assert(gnc_importer_gui); g_assert(h_trans); const char *account_name=HBCI_Transaction_ourAccountId(h_trans); DEBUG("Account name: \"%s\"\n",account_name); DEBUG("Transaction code: %d\n", HBCI_Transaction_transactionCode(h_trans)); const HBCI_Value* value=HBCI_Transaction_value(h_trans); DEBUG("Transaction value: %f\n", HBCI_Value_getValue(value)); gnc_acc = gnc_import_select_account(account_name, 1, NULL, NULL, NO_TYPE, NULL, NULL); g_assert(gnc_acc); return trans_importer(h_trans,gnc_importer_gui,gnc_acc); } /** @} */