Skip to content

Commit

Permalink
Merge branch 'csv_import'
Browse files Browse the repository at this point in the history
  • Loading branch information
gjanssens committed Feb 4, 2023
2 parents f54927d + 89944ba commit 60209a7
Show file tree
Hide file tree
Showing 26 changed files with 1,826 additions and 2,030 deletions.
2 changes: 1 addition & 1 deletion gnucash/import-export/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ add_subdirectory(qif-imp)
set (generic_import_SOURCES
import-account-matcher.c
import-commodity-matcher.c
import-backend.c
import-backend.cpp
import-format-dialog.c
import-match-picker.c
import-parse.c
Expand Down
15 changes: 11 additions & 4 deletions gnucash/import-export/csv-imp/assistant-csv-trans-import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

#include "import-account-matcher.h"
#include "import-main-matcher.h"
#include "import-backend.h"
#include "gnc-csv-account-map.h"
#include "gnc-account-sel.h"

Expand Down Expand Up @@ -1732,8 +1733,6 @@ void CsvImpTransAssist::preview_validate_settings ()

/* Populates the account match view with all potential
* account names found in the parse data.
*
* @param info The data being previewed
*/
void CsvImpTransAssist::acct_match_set_accounts ()
{
Expand Down Expand Up @@ -1963,7 +1962,7 @@ CsvImpTransAssist::assist_account_match_page_prepare ()
// Load the account strings into the store
acct_match_set_accounts ();

// Match the account strings to the mappings
// Match the account strings to account maps from previous imports
auto store = gtk_tree_view_get_model (GTK_TREE_VIEW(account_match_view));
gnc_csv_account_map_load_mappings (store);

Expand Down Expand Up @@ -2098,7 +2097,15 @@ CsvImpTransAssist::assist_match_page_prepare ()
auto draft_trans = trans_it.second;
if (draft_trans->trans)
{
gnc_gen_trans_list_add_trans (gnc_csv_importer_gui, draft_trans->trans);
auto lsplit = GNCImportLastSplitInfo {
draft_trans->m_price ? static_cast<gnc_numeric>(*draft_trans->m_price) : gnc_numeric{0, 0},
draft_trans->m_taction ? draft_trans->m_taction->c_str() : nullptr,
draft_trans->m_tmemo ? draft_trans->m_tmemo->c_str() : nullptr,
draft_trans->m_trec_state ? *draft_trans->m_trec_state : '\0',
draft_trans->m_trec_date ? static_cast<time64>(GncDateTime(*draft_trans->m_trec_date, DayPart::neutral)) : 0,
};

gnc_gen_trans_list_add_trans_with_split_data (gnc_csv_importer_gui, std::move (draft_trans->trans), &lsplit);
draft_trans->trans = nullptr;
}
}
Expand Down
66 changes: 18 additions & 48 deletions gnucash/import-export/csv-imp/gnc-csv-account-map.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,6 @@
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule UNUSED_VAR log_module = G_LOG_DOMAIN;

/**************************************************
* account_imap_destroy
*
* Destroy an import map. But all stored entries will
* still continue to exist in the underlying kvp frame
* of the account.
**************************************************/
static void
account_imap_destroy (GncImportMatchMap *imap)
{
if (!imap) return;
g_free (imap);
}

/**************************************************
* gnc_csv_account_map_search
*
Expand All @@ -76,17 +62,13 @@ Account * gnc_csv_account_map_search (const gchar *map_string)
/* Go through list of accounts */
for (ptr = accts; ptr; ptr = g_list_next (ptr))
{
GncImportMatchMap *tmp_imap;
Account *tmp_acc = ptr->data;

tmp_imap = gnc_account_imap_create_imap (ptr->data);

if (gnc_account_imap_find_account (tmp_imap, CSV_CATEGORY, map_string) != NULL)
if (gnc_account_imap_find_account (tmp_acc, CSV_CATEGORY, map_string))
{
account = ptr->data;
account_imap_destroy (tmp_imap);
account = tmp_acc;
break;
}
account_imap_destroy (tmp_imap);
}
g_list_free (accts);

Expand Down Expand Up @@ -118,21 +100,19 @@ gnc_csv_account_map_load_mappings (GtkTreeModel *mappings_store)
// Walk through the list, reading each row
gtk_tree_model_get (GTK_TREE_MODEL(mappings_store), &iter, MAPPING_STRING, &map_string, MAPPING_ACCOUNT, &account, -1);

if (account == NULL) // if account is NULL, store has not been updated
// Look for an account matching the map_string
// It may already be set in the tree model. If not we try to match the map_string with
// - an entry in our saved account maps
// - a full name of any of our existing accounts
if (account ||
(account = gnc_csv_account_map_search (map_string)) ||
(account = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), map_string)))
{
account = gnc_csv_account_map_search (map_string); //search the account list for the map_string

if (account == NULL) // account still NULL, we have no map
{
g_free (map_string);
valid = gtk_tree_model_iter_next (mappings_store, &iter);
continue;
}
fullpath = gnc_account_get_full_name (account);
gtk_list_store_set (GTK_LIST_STORE(mappings_store), &iter, MAPPING_FULLPATH, fullpath, -1);
gtk_list_store_set (GTK_LIST_STORE(mappings_store), &iter, MAPPING_ACCOUNT, account, -1);
g_free (fullpath);
}
fullpath = gnc_account_get_full_name (account);
gtk_list_store_set (GTK_LIST_STORE(mappings_store), &iter, MAPPING_FULLPATH, fullpath, -1);
gtk_list_store_set (GTK_LIST_STORE(mappings_store), &iter, MAPPING_ACCOUNT, account, -1);
g_free (fullpath);

g_free (map_string);
valid = gtk_tree_model_iter_next (mappings_store, &iter);
Expand All @@ -148,22 +128,12 @@ gnc_csv_account_map_load_mappings (GtkTreeModel *mappings_store)
void
gnc_csv_account_map_change_mappings (Account *old_account, Account *new_account, const gchar *map_string)
{
GncImportMatchMap *tmp_imap;

if (strlen (map_string) == 0)
return;

if (old_account != NULL)
{
tmp_imap = gnc_account_imap_create_imap (old_account);
gnc_account_imap_delete_account (tmp_imap, CSV_CATEGORY, map_string);
account_imap_destroy (tmp_imap);
}
if (old_account)
gnc_account_imap_delete_account (old_account, CSV_CATEGORY, map_string);

if (new_account != NULL)
{
tmp_imap = gnc_account_imap_create_imap (new_account);
gnc_account_imap_add_account (tmp_imap, CSV_CATEGORY, map_string, new_account);
account_imap_destroy (tmp_imap);
}
if (new_account)
gnc_account_imap_add_account (new_account, CSV_CATEGORY, map_string, new_account);
}
9 changes: 9 additions & 0 deletions gnucash/import-export/csv-imp/gnc-csv-account-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ extern "C" {
enum GncImportColumn {MAPPING_STRING, MAPPING_FULLPATH, MAPPING_ACCOUNT};

/** Load the import mappings.
*
* For each mapping string in the tree model, try to find a
* corresponding account and account full path.
*
* - if the account was already set, just update the full path
* - if the mapping string matches an account in the account maps,
* use that account and its corresponding full name
* - otherwise search for an existing account whose full name matches the
* mapping string
*
*/
void gnc_csv_account_map_load_mappings (GtkTreeModel *mappings_store);
Expand Down

0 comments on commit 60209a7

Please sign in to comment.