void xaccAccountAssignLots (Account *acc) { SplitList *splits, *node; if (!acc) return; ENTER ("acc=%s", xaccAccountGetName(acc)); xaccAccountBeginEdit (acc); restart_loop: splits = xaccAccountGetSplitList(acc); for (node = splits; node; node = node->next) { Split * split = node->data; /* If already in lot, then no-op */ if (split->lot) continue; /* Skip voided transactions */ if (gnc_numeric_zero_p (split->amount) && xaccTransGetVoidStatus(split->parent)) continue; if (xaccSplitAssign (split)) goto restart_loop; } xaccAccountCommitEdit (acc); LEAVE ("acc=%s", xaccAccountGetName(acc)); }
void xaccLotFill (GNCLot *lot) { Account *acc; Split *split; GNCPolicy *pcy; if (!lot) return; acc = gnc_lot_get_account(lot); pcy = gnc_account_get_policy(acc); ENTER ("(lot=%s, acc=%s)", gnc_lot_get_title(lot), xaccAccountGetName(acc)); /* If balance already zero, we have nothing to do. */ if (gnc_lot_is_closed (lot)) return; split = pcy->PolicyGetSplit (pcy, lot); if (!split) return; /* Handle the common case */ /* Reject voided transactions */ if (gnc_numeric_zero_p(split->amount) && xaccTransGetVoidStatus(split->parent)) return; xaccAccountBeginEdit (acc); /* Loop until we've filled up the lot, (i.e. till the * balance goes to zero) or there are no splits left. */ while (1) { Split *subsplit; subsplit = xaccSplitAssignToLot (split, lot); if (subsplit == split) { PERR ("Accounting Policy gave us a split that " "doesn't fit into this lot\n" "lot baln=%s, isclosed=%d, aplit amt=%s", gnc_num_dbg_to_string (gnc_lot_get_balance(lot)), gnc_lot_is_closed (lot), gnc_num_dbg_to_string (split->amount)); break; } if (gnc_lot_is_closed (lot)) break; split = pcy->PolicyGetSplit (pcy, lot); if (!split) break; } xaccAccountCommitEdit (acc); LEAVE ("(lot=%s, acc=%s)", gnc_lot_get_title(lot), xaccAccountGetName(acc)); }
void account_trees_merge(Account *existing_root, Account *new_accts_root) { GList *accounts, *node; g_return_if_fail(new_accts_root != NULL); g_return_if_fail(existing_root != NULL); /* since we're have a chance of mutating the list (via * gnc_account_add_child) while we're iterating over it, iterate * over a copy. */ accounts = gnc_account_get_children(new_accts_root); for (node = accounts; node; node = g_list_next(node)) { Account *existing_named, *new_acct; const char *name; new_acct = (Account*)node->data; name = xaccAccountGetName(new_acct); existing_named = gnc_account_lookup_by_name(existing_root, name); switch (determine_account_merge_disposition(existing_named, new_acct)) { case GNC_ACCOUNT_MERGE_DISPOSITION_USE_EXISTING: /* recurse */ account_trees_merge(existing_named, new_acct); break; case GNC_ACCOUNT_MERGE_DISPOSITION_CREATE_NEW: /* merge this one in. */ gnc_account_append_child(existing_root, new_acct); break; } } g_list_free(accounts); }
void xaccAccountScrubCommodity (Account *account) { gnc_commodity *commodity; if (!account) return; if (xaccAccountGetType(account) == ACCT_TYPE_ROOT) return; commodity = xaccAccountGetCommodity (account); if (commodity) return; /* Use the 'obsolete' routines to try to figure out what the * account commodity should have been. */ commodity = xaccAccountGetCommodity (account); if (commodity) { xaccAccountSetCommodity (account, commodity); return; } commodity = DxaccAccountGetCurrency (account); if (commodity) { xaccAccountSetCommodity (account, commodity); return; } PERR ("Account \"%s\" does not have a commodity!", xaccAccountGetName(account)); }
void xaccAccountScrubImbalance (Account *acc) { GList *node, *splits; const char *str; gint split_count = 0, curr_split_no = 1; if (!acc) return; str = xaccAccountGetName(acc); str = str ? str : "(null)"; PINFO ("Looking for imbalance in account %s \n", str); splits = xaccAccountGetSplitList(acc); split_count = g_list_length (splits); for (node = splits; node; node = node->next) { Split *split = node->data; Transaction *trans = xaccSplitGetParent(split); PINFO("Start processing split %d of %d", curr_split_no, split_count); xaccTransScrubCurrency(trans); xaccTransScrubImbalance (trans, gnc_account_get_root (acc), NULL); PINFO("Finished processing split %d of %d", curr_split_no, split_count); curr_split_no++; } }
static void add_groups_for_each (Account *toadd, gpointer data) { struct add_group_data_struct *dadata = data; Account *foundact; foundact = gnc_account_lookup_by_name(dadata->to, xaccAccountGetName(toadd)); if (!foundact) { foundact = clone_account (toadd, dadata->com); if (dadata->to) gnc_account_append_child (dadata->to, foundact); else if (dadata->parent) gnc_account_append_child (dadata->parent, foundact); else { g_warning ("add_groups_for_each: no valid parent"); } } { if (gnc_account_n_children(toadd) > 0) { struct add_group_data_struct downdata; downdata.to = foundact; downdata.parent = foundact; downdata.com = dadata->com; gnc_account_foreach_child (toadd, add_groups_for_each, &downdata); } } }
// Account Name short or Long static gchar* add_account_name (gchar *so_far, Account *acc, Split *split, gboolean full, CsvExportInfo *info) { gchar *name = NULL; gchar *conv; gchar *result; Account *account = NULL; if (split == NULL) { if (acc == NULL) name = g_strdup (" "); else account = acc; } else account = xaccSplitGetAccount (split); if (account != NULL) { if (full) name = gnc_account_get_full_name (account); else name = g_strdup (xaccAccountGetName (account)); } conv = csv_txn_test_field_string (info, name); result = g_strconcat (so_far, conv, info->mid_sep, NULL); g_free (name); g_free (conv); g_free (so_far); return result; }
void xaccAccountScrubOrphans (Account *acc, QofPercentageFunc percentagefunc) { GList *node, *splits; const char *str; const char *message = _( "Looking for orphans in account %s: %u of %u"); guint total_splits = 0; guint current_split = 0; if (!acc) return; str = xaccAccountGetName (acc); str = str ? str : "(null)"; PINFO ("Looking for orphans in account %s \n", str); splits = xaccAccountGetSplitList(acc); total_splits = g_list_length (splits); for (node = splits; node; node = node->next) { Split *split = node->data; if (current_split % 100 == 0) { char *progress_msg = g_strdup_printf (message, str, current_split, total_splits); (percentagefunc)(progress_msg, (100 * current_split) / total_splits); g_free (progress_msg); } TransScrubOrphansFast (xaccSplitGetParent (split), gnc_account_get_root (acc)); current_split++; } (percentagefunc)(NULL, -1.0); }
static void move_quote_source (Account *account, gpointer data) { gnc_commodity *com; gnc_quote_source *quote_source; gboolean new_style = GPOINTER_TO_INT(data); const char *source, *tz; com = xaccAccountGetCommodity(account); if (!com) return; if (!new_style) { source = dxaccAccountGetPriceSrc(account); if (!source || !*source) return; tz = dxaccAccountGetQuoteTZ(account); PINFO("to %8s from %s", gnc_commodity_get_mnemonic(com), xaccAccountGetName(account)); gnc_commodity_set_quote_flag(com, TRUE); quote_source = gnc_quote_source_lookup_by_internal(source); if (!quote_source) quote_source = gnc_quote_source_add_new(source, FALSE); gnc_commodity_set_quote_source(com, quote_source); gnc_commodity_set_quote_tz(com, tz); } dxaccAccountSetPriceSrc(account, NULL); dxaccAccountSetQuoteTZ(account, NULL); return; }
// copied from gnc-ab-kvp.c static void force_account_dirty(Account *acct) { gchar *name = g_strdup(xaccAccountGetName(acct)); /* This is necessary because modifying the KvpFrames doesn't mark * accounts dirty, which means the changes wont be propagated to the * backend. */ xaccAccountSetName(acct, name); g_free(name); }
void xaccAccountScrubImbalance (Account *acc, QofPercentageFunc percentagefunc) { GList *node, *splits; const char *str; const char *message = _( "Looking for imbalances in account %s: %u of %u"); gint split_count = 0, curr_split_no = 0; if (!acc) return; str = xaccAccountGetName(acc); str = str ? str : "(null)"; PINFO ("Looking for imbalances in account %s \n", str); splits = xaccAccountGetSplitList(acc); split_count = g_list_length (splits); for (node = splits; node; node = node->next) { Split *split = node->data; Transaction *trans = xaccSplitGetParent(split); PINFO("Start processing split %d of %d", curr_split_no + 1, split_count); if (curr_split_no % 100 == 0) { char *progress_msg = g_strdup_printf (message, str, curr_split_no, split_count); (percentagefunc)(progress_msg, (100 * curr_split_no) / split_count); g_free (progress_msg); } TransScrubOrphansFast (xaccSplitGetParent (split), gnc_account_get_root (acc)); (percentagefunc)(NULL, 0.0); xaccTransScrubCurrency(trans); xaccTransScrubImbalance (trans, gnc_account_get_root (acc), NULL); PINFO("Finished processing split %d of %d", curr_split_no + 1, split_count); curr_split_no++; } (percentagefunc)(NULL, -1.0); }
void xaccAccountScrubOrphans (Account *acc) { GList *node; const char *str; if (!acc) return; str = xaccAccountGetName (acc); str = str ? str : "(null)"; PINFO ("Looking for orphans in account %s \n", str); for (node = xaccAccountGetSplitList(acc); node; node = node->next) { Split *split = node->data; TransScrubOrphansFast (xaccSplitGetParent (split), gnc_account_get_root (acc)); } }
void xaccAccountScrubImbalance (Account *acc) { GList *node; const char *str; if (!acc) return; str = xaccAccountGetName(acc); str = str ? str : "(null)"; PINFO ("Looking for imbalance in account %s \n", str); for (node = xaccAccountGetSplitList(acc); node; node = node->next) { Split *split = node->data; Transaction *trans = xaccSplitGetParent(split); xaccTransScrubCurrency(trans); xaccTransScrubImbalance (trans, gnc_account_get_root (acc), NULL); } }
void gncScrubBusinessAccountLots (Account *acc) { LotList *lots, *node; gint lot_count = 0; gint curr_lot_no = 1; const gchar *str; if (!acc) return; if (FALSE == xaccAccountIsAPARType (xaccAccountGetType (acc))) return; str = xaccAccountGetName(acc); str = str ? str : "(null)"; ENTER ("(acc=%s)", str); PINFO ("Cleaning up superfluous lot links in account %s \n", str); xaccAccountBeginEdit(acc); lots = xaccAccountGetLotList(acc); lot_count = g_list_length (lots); for (node = lots; node; node = node->next) { GNCLot *lot = node->data; PINFO("Start processing lot %d of %d", curr_lot_no, lot_count); if (lot) gncScrubBusinessLot (lot); PINFO("Finished processing lot %d of %d", curr_lot_no, lot_count); curr_lot_no++; } g_list_free(lots); xaccAccountCommitEdit(acc); LEAVE ("(acc=%s)", str); }
/******************************************************* * csv_transactions_export * * write a list of transactions to a text file *******************************************************/ void csv_transactions_export (CsvExportInfo *info) { FILE *fh; Account *acc; GList *ptr; gboolean num_action = qof_book_use_split_action_for_num_field (gnc_get_current_book()); ENTER(""); DEBUG("File name is : %s", info->file_name); info->failed = FALSE; /* Set up separators */ if (info->use_quotes) { info->end_sep = "\""; info->mid_sep = g_strconcat ("\"", info->separator_str, "\"", NULL); } else { info->end_sep = ""; info->mid_sep = g_strconcat (info->separator_str, NULL); } /* Open File for writing */ fh = g_fopen (info->file_name, "w" ); if (fh != NULL) { gchar *header; int i; /* Header string */ if (info->simple_layout) { header = g_strconcat (info->end_sep, _("Date"), info->mid_sep, _("Account Name"), info->mid_sep, (num_action ? _("Transaction Number") : _("Number")), info->mid_sep, _("Description"), info->mid_sep, _("Full Category Path"), info->mid_sep, _("Reconcile"), info->mid_sep, _("Amount With Sym"), info->mid_sep, _("Amount Num."), info->mid_sep, _("Rate/Price"), info->end_sep, EOLSTR, NULL); } else { header = g_strconcat (info->end_sep, _("Date"), info->mid_sep, _("Transaction Type"), info->mid_sep, _("Second Date"), info->mid_sep, _("Account Name"), info->mid_sep, (num_action ? _("Transaction Number") : _("Number")), info->mid_sep, _("Description"), info->mid_sep, _("Notes"), info->mid_sep, _("Memo"), info->mid_sep, _("Full Category Path"), info->mid_sep, _("Category"), info->mid_sep, _("Row Type"), info->mid_sep, (num_action ? _("Number/Action") : _("Action")), info->mid_sep, _("Reconcile"), info->mid_sep, _("Amount With Sym"), info->mid_sep, _("Commodity Mnemonic"), info->mid_sep, _("Commodity Namespace"), info->mid_sep, _("Amount Num."), info->mid_sep, _("Rate/Price"), info->end_sep, EOLSTR, NULL); } DEBUG("Header String: %s", header); /* Write header line */ if (!write_line_to_file (fh, header)) { info->failed = TRUE; g_free (header); return; } g_free (header); /* Go through list of accounts */ for (ptr = info->csva.account_list, i = 0; ptr; ptr = g_list_next(ptr), i++) { acc = ptr->data; DEBUG("Account being processed is : %s", xaccAccountGetName (acc)); account_splits (info, acc, fh); } g_list_free (info->trans_list); // free trans_list } else info->failed = TRUE; if (fh) fclose (fh); LEAVE(""); }
void xaccTransScrubCurrency (Transaction *trans) { SplitList *node; gnc_commodity *currency; if (!trans) return; /* If there are any orphaned splits in a transaction, then the * this routine will fail. Therefore, we want to make sure that * there are no orphans (splits without parent account). */ xaccTransScrubOrphans (trans); currency = xaccTransGetCurrency (trans); if (currency && gnc_commodity_is_currency(currency)) return; currency = xaccTransFindCommonCurrency (trans, qof_instance_get_book(trans)); if (currency) { xaccTransBeginEdit (trans); xaccTransSetCurrency (trans, currency); xaccTransCommitEdit (trans); } else { if (NULL == trans->splits) { PWARN ("Transaction \"%s\" has no splits in it!", trans->description); } else { SplitList *node; char guid_str[GUID_ENCODING_LENGTH + 1]; guid_to_string_buff(xaccTransGetGUID(trans), guid_str); PWARN ("no common transaction currency found for trans=\"%s\" (%s);", trans->description, guid_str); for (node = trans->splits; node; node = node->next) { Split *split = node->data; if (NULL == split->acc) { PWARN (" split=\"%s\" is not in any account!", split->memo); } else { gnc_commodity *currency = xaccAccountGetCommodity(split->acc); PWARN ("setting to split=\"%s\" account=\"%s\" commodity=\"%s\"", split->memo, xaccAccountGetName(split->acc), gnc_commodity_get_mnemonic(currency)); xaccTransBeginEdit (trans); xaccTransSetCurrency (trans, currency); xaccTransCommitEdit (trans); return; } } } return; } for (node = trans->splits; node; node = node->next) { Split *sp = node->data; if (!gnc_numeric_equal(xaccSplitGetAmount (sp), xaccSplitGetValue (sp))) { gnc_commodity *acc_currency; acc_currency = sp->acc ? xaccAccountGetCommodity(sp->acc) : NULL; if (acc_currency == currency) { /* This Split needs fixing: The transaction-currency equals * the account-currency/commodity, but the amount/values are * inequal i.e. they still correspond to the security * (amount) and the currency (value). In the new model, the * value is the amount in the account-commodity -- so it * needs to be set to equal the amount (since the * account-currency doesn't exist anymore). * * Note: Nevertheless we lose some information here. Namely, * the information that the 'amount' in 'account-old-security' * was worth 'value' in 'account-old-currency'. Maybe it would * be better to store that information in the price database? * But then, for old currency transactions there is still the * 'other' transaction, which is going to keep that * information. So I don't bother with that here. -- cstim, * 2002/11/20. */ PWARN ("Adjusted split with mismatched values, desc=\"%s\" memo=\"%s\"" " old amount %s %s, new amount %s", trans->description, sp->memo, gnc_num_dbg_to_string (xaccSplitGetAmount(sp)), gnc_commodity_get_mnemonic (currency), gnc_num_dbg_to_string (xaccSplitGetValue(sp))); xaccTransBeginEdit (trans); xaccSplitSetAmount (sp, xaccSplitGetValue(sp)); xaccTransCommitEdit (trans); } /*else { PINFO ("Ok: Split '%s' Amount %s %s, value %s %s", xaccSplitGetMemo (sp), gnc_num_dbg_to_string (amount), gnc_commodity_get_mnemonic (currency), gnc_num_dbg_to_string (value), gnc_commodity_get_mnemonic (acc_currency)); }*/ } } }
static void refresh_model_row (GNCImportMainMatcher *gui, GtkTreeModel *model, GtkTreeIter *iter, GNCImportTransInfo *info) { GtkListStore *store; GtkTreeSelection *selection; gchar *tmp, *imbalance, *text, *color; const gchar *ro_text; Split *split; g_assert (gui); g_assert (model); g_assert (info); /*DEBUG("Begin");*/ store = GTK_LIST_STORE(model); gtk_list_store_set(store, iter, DOWNLOADED_COL_DATA, info, -1); /*Account:*/ split = gnc_import_TransInfo_get_fsplit (info); g_assert(split); // Must not be NULL ro_text = xaccAccountGetName(xaccSplitGetAccount(split)); gtk_list_store_set(store, iter, DOWNLOADED_COL_ACCOUNT, ro_text, -1); /*Date*/ text = qof_print_date ( xaccTransGetDate( gnc_import_TransInfo_get_trans(info) ) ); gtk_list_store_set(store, iter, DOWNLOADED_COL_DATE, text, -1); g_free(text); /*Amount*/ ro_text = xaccPrintAmount (xaccSplitGetAmount (split), gnc_split_amount_print_info(split, TRUE) ); gtk_list_store_set(store, iter, DOWNLOADED_COL_AMOUNT, ro_text, -1); /*Description*/ ro_text = xaccTransGetDescription(gnc_import_TransInfo_get_trans(info) ); gtk_list_store_set(store, iter, DOWNLOADED_COL_DESCRIPTION, ro_text, -1); /*Memo*/ ro_text = xaccSplitGetMemo(split); gtk_list_store_set(store, iter, DOWNLOADED_COL_MEMO, ro_text, -1); /*Actions*/ /* Action informations */ ro_text = text = color = NULL; switch (gnc_import_TransInfo_get_action(info)) { case GNCImport_ADD: if (gnc_import_TransInfo_is_balanced(info) == TRUE) { ro_text = _("New, already balanced"); color = COLOR_GREEN; } else { /* Assume that importers won't create transactions in two or more currencies so we can use xaccTransGetImbalanceValue */ imbalance = g_strdup (xaccPrintAmount (gnc_numeric_neg(xaccTransGetImbalanceValue (gnc_import_TransInfo_get_trans(info) )), gnc_commodity_print_info (xaccTransGetCurrency(gnc_import_TransInfo_get_trans (info)), TRUE) )); if (gnc_import_TransInfo_get_destacc (info) != NULL) { color = COLOR_GREEN; tmp = gnc_account_get_full_name (gnc_import_TransInfo_get_destacc (info)); if (gnc_import_TransInfo_get_destacc_selected_manually(info) == TRUE) { text = /* Translators: %1$s is the amount to be transferred. %2$s is the destination account. */ g_strdup_printf(_("New, transfer %s to (manual) \"%s\""), imbalance, tmp); } else { text = /* Translators: %1$s is the amount to be transferred. %2$s is the destination account. */ g_strdup_printf(_("New, transfer %s to (auto) \"%s\""), imbalance, tmp); } g_free (tmp); } else { color = COLOR_YELLOW; text = /* Translators: %s is the amount to be transferred. */ g_strdup_printf(_("New, UNBALANCED (need acct to transfer %s)!"), imbalance); } g_free (imbalance); } break; case GNCImport_CLEAR: if (gnc_import_TransInfo_get_selected_match(info)) { color = COLOR_GREEN; if (gnc_import_TransInfo_get_match_selected_manually(info) == TRUE) { ro_text = _("Reconcile (manual) match"); } else { ro_text = _("Reconcile (auto) match"); } } else { color = COLOR_RED; ro_text = _("Match missing!"); } break; case GNCImport_UPDATE: if (gnc_import_TransInfo_get_selected_match(info)) { color = COLOR_GREEN; if (gnc_import_TransInfo_get_match_selected_manually(info) == TRUE) { ro_text = _("Update and reconcile (manual) match"); } else { ro_text = _("Update and reconcile (auto) match"); } } else { color = COLOR_RED; ro_text = _("Match missing!"); } break; case GNCImport_SKIP: color = COLOR_RED; ro_text = _("Do not import (no action selected)"); break; default: color = "white"; ro_text = "WRITEME, this is an unknown action"; break; } gtk_list_store_set(store, iter, DOWNLOADED_COL_COLOR, color, DOWNLOADED_COL_ACTION_INFO, ro_text ? ro_text : text, -1); if (text) g_free(text); /* Set the pixmaps */ gtk_list_store_set(store, iter, DOWNLOADED_COL_ACTION_ADD, gnc_import_TransInfo_get_action(info) == GNCImport_ADD, -1); if (gnc_import_TransInfo_get_action(info) == GNCImport_SKIP) { /*Show the best match's confidence pixmap in the info column*/ gtk_list_store_set(store, iter, DOWNLOADED_COL_ACTION_PIXBUF, gen_probability_pixbuf( gnc_import_MatchInfo_get_probability ( gnc_import_TransInfo_get_selected_match (info)), gui->user_settings, GTK_WIDGET(gui->view)), -1); } gtk_list_store_set(store, iter, DOWNLOADED_COL_ACTION_CLEAR, gnc_import_TransInfo_get_action(info) == GNCImport_CLEAR, -1); if (gnc_import_TransInfo_get_action(info) == GNCImport_CLEAR) { /*Show the best match's confidence pixmap in the info column*/ gtk_list_store_set(store, iter, DOWNLOADED_COL_ACTION_PIXBUF, gen_probability_pixbuf( gnc_import_MatchInfo_get_probability ( gnc_import_TransInfo_get_selected_match (info)), gui->user_settings, GTK_WIDGET(gui->view)), -1); } gtk_list_store_set(store, iter, DOWNLOADED_COL_ACTION_UPDATE, gnc_import_TransInfo_get_action(info) == GNCImport_UPDATE, -1); if (gnc_import_TransInfo_get_action(info) == GNCImport_UPDATE) { /*Show the best match's confidence pixmap in the info column*/ gtk_list_store_set(store, iter, DOWNLOADED_COL_ACTION_PIXBUF, gen_probability_pixbuf( gnc_import_MatchInfo_get_probability ( gnc_import_TransInfo_get_selected_match (info)), gui->user_settings, GTK_WIDGET(gui->view)), -1); } selection = gtk_tree_view_get_selection(gui->view); gtk_tree_selection_unselect_all(selection); }
static void downloaded_transaction_append(GNCImportMatchPicker * matcher, GNCImportTransInfo * transaction_info) { GtkListStore *store; GtkTreeIter iter; GtkTreeSelection *selection; Transaction *trans; Split *split; gchar *text; const gchar *ro_text; gboolean found = FALSE; GNCImportTransInfo *local_info; g_assert(matcher); g_assert(transaction_info); /*DEBUG("Begin");*/ /* Has the transaction already been added? */ store = GTK_LIST_STORE(gtk_tree_view_get_model(matcher->downloaded_view)); if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) { do { gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DOWNLOADED_COL_INFO_PTR, &local_info, -1); if (local_info == transaction_info) { found = TRUE; break; } } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)); } if (!found) gtk_list_store_append(store, &iter); split = gnc_import_TransInfo_get_fsplit(transaction_info); trans = gnc_import_TransInfo_get_trans(transaction_info); /*Account*/ ro_text = xaccAccountGetName(xaccSplitGetAccount(split)); gtk_list_store_set(store, &iter, DOWNLOADED_COL_ACCOUNT, ro_text, -1); /*Date*/ text = qof_print_date(xaccTransGetDate(trans)); gtk_list_store_set(store, &iter, DOWNLOADED_COL_DATE, text, -1); g_free(text); /*Amount*/ ro_text = xaccPrintAmount(xaccSplitGetAmount(split), gnc_split_amount_print_info(split, TRUE)); gtk_list_store_set(store, &iter, DOWNLOADED_COL_AMOUNT, ro_text, -1); /*Description*/ ro_text = xaccTransGetDescription(trans); gtk_list_store_set(store, &iter, DOWNLOADED_COL_DESCRIPTION, ro_text, -1); /*Memo*/ ro_text = xaccSplitGetMemo(split); gtk_list_store_set(store, &iter, DOWNLOADED_COL_MEMO, ro_text, -1); /*Imbalance*/ /* Assume that the importer won't create a transaction that involves two or more currencies and no non-currency commodity. In that case can use the simpler value imbalance check. */ ro_text = xaccPrintAmount(xaccTransGetImbalanceValue(trans), gnc_default_print_info(TRUE)); gtk_list_store_set(store, &iter, DOWNLOADED_COL_BALANCED, ro_text, -1); gtk_list_store_set(store, &iter, DOWNLOADED_COL_INFO_PTR, transaction_info, -1); selection = gtk_tree_view_get_selection(matcher->downloaded_view); gtk_tree_selection_select_iter(selection, &iter); }
static gboolean save_transaction( GncSqlBackend* be, Transaction* pTx, gboolean do_save_splits ) { const GncGUID* guid; gint op; gboolean is_infant; QofInstance* inst; gboolean is_ok = TRUE; gchar* err = NULL; g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( pTx != NULL, FALSE ); inst = QOF_INSTANCE(pTx); is_infant = qof_instance_get_infant( inst ); if ( qof_instance_get_destroying( inst ) ) { op = OP_DB_DELETE; } else if ( be->is_pristine_db || is_infant ) { op = OP_DB_INSERT; } else { op = OP_DB_UPDATE; } if ( op != OP_DB_DELETE ) { gnc_commodity *commodity = xaccTransGetCurrency( pTx ); // Ensure the commodity is in the db is_ok = gnc_sql_save_commodity( be, commodity ); if ( ! is_ok ) { err = "Commodity save failed: Probably an invalid or missing currency"; qof_backend_set_error( &be->be, ERR_BACKEND_DATA_CORRUPT); } } if ( is_ok ) { is_ok = gnc_sql_do_db_operation( be, op, TRANSACTION_TABLE, GNC_ID_TRANS, pTx, tx_col_table ); if ( ! is_ok ) { err = "Transaction header save failed. Check trace log for SQL errors"; } } if ( is_ok ) { // Commit slots and splits guid = qof_instance_get_guid( inst ); if ( !qof_instance_get_destroying(inst) ) { is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); if ( ! is_ok ) { err = "Slots save failed. Check trace log for SQL errors"; } if ( is_ok && do_save_splits ) { is_ok = save_splits( be, guid, xaccTransGetSplitList( pTx ) ); if ( ! is_ok ) { err = "Split save failed. Check trace log for SQL errors"; } } } else { is_ok = gnc_sql_slots_delete( be, guid ); if ( ! is_ok ) { err = "Slots delete failed. Check trace log for SQL errors"; } if ( is_ok ) { is_ok = delete_splits( be, pTx ); if ( ! is_ok ) { err = "Split delete failed. Check trace log for SQL errors"; } } } } if (! is_ok ) { G_GNUC_UNUSED gchar *message1 = "Transaction %s dated %s in account %s not saved due to %s.%s"; G_GNUC_UNUSED gchar *message2 = "\nDatabase may be corrupted, check your data carefully."; Split* split = xaccTransGetSplit( pTx, 0); Account *acc = xaccSplitGetAccount( split ); /* FIXME: This needs to be implemented qof_error_format_secondary_text( GTK_MESSAGE_DIALOG( msg ), message1, xaccTransGetDescription( pTx ), qof_print_date( xaccTransGetDate( pTx ) ), xaccAccountGetName( acc ), err, message2 ); */ PERR( "Transaction %s dated %s in account %s not saved due to %s.\n", xaccTransGetDescription( pTx ), qof_print_date( xaccTransGetDate( pTx ) ), xaccAccountGetName( acc ), err ); } return is_ok; }
static gchar* node_and_account_equal (xmlNodePtr node, Account* act) { xmlNodePtr mark; while (g_strcmp0 ((char*)node->name, "text") == 0) { node = node->next; } if (!check_dom_tree_version (node, "2.0.0")) { return g_strdup ("version wrong. Not 2.0.0 or not there"); } if (!node->name || g_strcmp0 ((char*)node->name, "gnc:account")) { return g_strdup ("Name of toplevel node is bad"); } for (mark = node->xmlChildrenNode; mark; mark = mark->next) { if (g_strcmp0 ((char*)mark->name, "text") == 0) { } else if (g_strcmp0 ((char*)mark->name, "act:name") == 0) { if (!equals_node_val_vs_string (mark, xaccAccountGetName (act))) { return g_strdup ("names differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:id") == 0) { if (!equals_node_val_vs_guid (mark, xaccAccountGetGUID (act))) { return g_strdup ("ids differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:type") == 0) { gchar* txt; GNCAccountType type; txt = dom_tree_to_text (mark); if (!txt) { return g_strdup ("couldn't get type string"); } else if (!xaccAccountStringToType (txt, &type)) { g_free (txt); return g_strdup ("couldn't convert type string to int"); } else if (type != xaccAccountGetType (act)) { g_free (txt); return g_strdup ("types differ"); } else { g_free (txt); } } else if (g_strcmp0 ((char*)mark->name, "act:commodity") == 0) { /* This is somewhat BS, because if the commodity isn't a currency (and therefore built in) there isn't a corresponding currency in the XML, skip the test. jralls 2010-11-02 */ if (xaccAccountGetCommodity (act) == NULL) continue; if (!equals_node_val_vs_commodity ( mark, xaccAccountGetCommodity (act), gnc_account_get_book (act))) { return g_strdup ("commodities differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:code") == 0) { if (!equals_node_val_vs_string (mark, xaccAccountGetCode (act))) { return g_strdup ("codes differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:description") == 0) { if (!equals_node_val_vs_string ( mark, xaccAccountGetDescription (act))) { return g_strdup ("descriptions differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:slots") == 0) { /* xaccAccountDeleteOldData (act); */ if (!equals_node_val_vs_kvp_frame (mark, qof_instance_get_slots (QOF_INSTANCE (act)))) { return g_strdup ("slots differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:parent") == 0) { if (!equals_node_val_vs_guid ( mark, xaccAccountGetGUID (gnc_account_get_parent (act)))) { return g_strdup ("parent ids differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:commodity-scu") == 0) { if (!equals_node_val_vs_int (mark, xaccAccountGetCommoditySCU (act))) { return g_strdup ("commodity scus differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:hidden") == 0) { if (!equals_node_val_vs_boolean (mark, xaccAccountGetHidden (act))) { return g_strdup ("Hidden flags differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:placeholder") == 0) { if (!equals_node_val_vs_boolean (mark, xaccAccountGetPlaceholder (act))) { return g_strdup ("Placeholder flags differ"); } } else if (g_strcmp0 ((char*)mark->name, "act:security") == 0) { return NULL; // This tag is ignored. } else { return g_strdup_printf ("unknown node in dom tree: %s", mark->name); } } return NULL; }
/******************************************************* * csv_tree_export * * write a list of accounts settings to a text file *******************************************************/ void csv_tree_export (CsvExportInfo *info) { FILE *fh; Account *root; Account *acc; GList *accts, *ptr; ENTER(""); DEBUG("File name is : %s", info->file_name); /* Get list of Accounts */ root = gnc_book_get_root_account (gnc_get_current_book()); accts = gnc_account_get_descendants_sorted (root); info->failed = FALSE; /* Open File for writing */ fh = g_fopen (info->file_name, "w"); if (fh != NULL) { gchar *header; gchar *part1; gchar *part2; const gchar *currentSel; gchar *end_sep; gchar *mid_sep; int i; /* Set up separators */ if (info->use_quotes) { end_sep = "\""; mid_sep = g_strconcat ("\"", info->separator_str, "\"", NULL); } else { end_sep = ""; mid_sep = g_strconcat (info->separator_str, NULL); } /* Header string, 'eol = end of line marker' */ header = g_strconcat (end_sep, _("type"), mid_sep, _("full_name"), mid_sep, _("name"), mid_sep, _("code"), mid_sep, _("description"), mid_sep, _("color"), mid_sep, _("notes"), mid_sep, _("commoditym"), mid_sep, _("commodityn"), mid_sep, _("hidden"), mid_sep, _("tax"), mid_sep, _("place_holder"), end_sep, EOLSTR, NULL); DEBUG("Header String: %s", header); /* Write header line */ if (!write_line_to_file (fh, header)) { info->failed = TRUE; g_free (mid_sep); g_free (header); return; } g_free (header); /* Go through list of accounts */ for (ptr = accts, i = 0; ptr; ptr = g_list_next (ptr), i++) { gchar *fullname = NULL; gchar *str_temp = NULL; acc = ptr->data; DEBUG("Account being processed is : %s", xaccAccountGetName (acc)); /* Type */ currentSel = xaccAccountTypeEnumAsString (xaccAccountGetType (acc)); part1 = g_strconcat (end_sep, currentSel, mid_sep, NULL); /* Full Name */ fullname = gnc_account_get_full_name (acc); str_temp = csv_test_field_string (info, fullname); part2 = g_strconcat (part1, str_temp, mid_sep, NULL); g_free (str_temp); g_free (fullname); g_free (part1); /* Name */ currentSel = xaccAccountGetName (acc); str_temp = csv_test_field_string (info, currentSel); part1 = g_strconcat (part2, str_temp, mid_sep, NULL); g_free (str_temp); g_free (part2); /* Code */ currentSel = xaccAccountGetCode (acc) ? xaccAccountGetCode (acc) : ""; str_temp = csv_test_field_string (info, currentSel); part2 = g_strconcat (part1, str_temp, mid_sep, NULL); g_free (str_temp); g_free (part1); /* Description */ currentSel = xaccAccountGetDescription (acc) ? xaccAccountGetDescription (acc) : ""; str_temp = csv_test_field_string (info, currentSel); part1 = g_strconcat (part2, str_temp, mid_sep, NULL); g_free (str_temp); g_free (part2); /* Color */ currentSel = xaccAccountGetColor (acc) ? xaccAccountGetColor (acc) : "" ; str_temp = csv_test_field_string (info, currentSel); part2 = g_strconcat (part1, str_temp, mid_sep, NULL); g_free (str_temp); g_free (part1); /* Notes */ currentSel = xaccAccountGetNotes (acc) ? xaccAccountGetNotes (acc) : "" ; str_temp = csv_test_field_string (info, currentSel); part1 = g_strconcat (part2, str_temp, mid_sep, NULL); g_free (str_temp); g_free (part2); /* Commodity Mnemonic */ currentSel = gnc_commodity_get_mnemonic (xaccAccountGetCommodity (acc)); str_temp = csv_test_field_string (info, currentSel); part2 = g_strconcat (part1, str_temp, mid_sep, NULL); g_free (str_temp); g_free (part1); /* Commodity Namespace */ currentSel = gnc_commodity_get_namespace (xaccAccountGetCommodity (acc)); str_temp = csv_test_field_string (info, currentSel); part1 = g_strconcat (part2, str_temp, mid_sep, NULL); g_free (str_temp); g_free (part2); /* Hidden */ currentSel = xaccAccountGetHidden (acc) ? "T" : "F" ; part2 = g_strconcat (part1, currentSel, mid_sep, NULL); g_free (part1); /* Tax */ currentSel = xaccAccountGetTaxRelated (acc) ? "T" : "F" ; part1 = g_strconcat (part2, currentSel, mid_sep, NULL); g_free (part2); /* Place Holder / end of line marker */ currentSel = xaccAccountGetPlaceholder (acc) ? "T" : "F" ; part2 = g_strconcat (part1, currentSel, end_sep, EOLSTR, NULL); g_free (part1); DEBUG("Account String: %s", part2); /* Write to file */ if (!write_line_to_file (fh, part2)) { info->failed = TRUE; break; } g_free (part2); } g_free (mid_sep); } else info->failed = TRUE; if (fh) fclose (fh); g_list_free (accts); LEAVE(""); }
void xaccTransWriteLog (Transaction *trans, char flag) { GList *node; char trans_guid_str[GUID_ENCODING_LENGTH + 1]; char split_guid_str[GUID_ENCODING_LENGTH + 1]; const char *trans_notes; char dnow[100], dent[100], dpost[100], drecn[100]; Timespec ts; if (!gen_logs) return; if (!trans_log) return; timespecFromTime_t(&ts, time(NULL)); gnc_timespec_to_iso8601_buff (ts, dnow); timespecFromTime_t(&ts, trans->date_entered.tv_sec); gnc_timespec_to_iso8601_buff (ts, dent); timespecFromTime_t(&ts, trans->date_posted.tv_sec); gnc_timespec_to_iso8601_buff (ts, dpost); guid_to_string_buff (xaccTransGetGUID(trans), trans_guid_str); trans_notes = xaccTransGetNotes(trans); fprintf (trans_log, "===== START\n"); for (node = trans->splits; node; node = node->next) { Split *split = node->data; const char * accname = ""; char acc_guid_str[GUID_ENCODING_LENGTH + 1]; gnc_numeric amt, val; if (xaccSplitGetAccount(split)) { accname = xaccAccountGetName (xaccSplitGetAccount(split)); guid_to_string_buff(xaccAccountGetGUID(xaccSplitGetAccount(split)), acc_guid_str); } else { acc_guid_str[0] = '\0'; } timespecFromTime_t(&ts, split->date_reconciled.tv_sec); gnc_timespec_to_iso8601_buff (ts, drecn); guid_to_string_buff (xaccSplitGetGUID(split), split_guid_str); amt = xaccSplitGetAmount (split); val = xaccSplitGetValue (split); /* use tab-separated fields */ fprintf (trans_log, "%c\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t" "%s\t%s\t%s\t%s\t%c\t%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT "\t%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT "\t%s\n", flag, trans_guid_str, split_guid_str, /* trans+split make up unique id */ /* Note that the next three strings always exist, * so we don't need to test them. */ dnow, dent, dpost, acc_guid_str, accname ? accname : "", trans->num ? trans->num : "", trans->description ? trans->description : "", trans_notes ? trans_notes : "", split->memo ? split->memo : "", split->action ? split->action : "", split->reconciled, gnc_numeric_num(amt), gnc_numeric_denom(amt), gnc_numeric_num(val), gnc_numeric_denom(val), /* The next string always exists. No need to test it. */ drecn); } fprintf (trans_log, "===== END\n"); /* get data out to the disk */ fflush (trans_log); }
void xaccScrubSubSplitPrice (Split *split, int maxmult, int maxamtscu) { gnc_numeric src_amt, src_val; SplitList *node; if (FALSE == is_subsplit (split)) return; ENTER (" "); /* Get 'price' of the indicated split */ src_amt = xaccSplitGetAmount (split); src_val = xaccSplitGetValue (split); /* Loop over splits, adjust each so that it has the same * ratio (i.e. price). Change the value to get things * right; do not change the amount */ for (node = split->parent->splits; node; node = node->next) { Split *s = node->data; Transaction *txn = s->parent; gnc_numeric dst_amt, dst_val, target_val; gnc_numeric frac, delta; int scu; /* Skip the reference split */ if (s == split) continue; scu = gnc_commodity_get_fraction (txn->common_currency); dst_amt = xaccSplitGetAmount (s); dst_val = xaccSplitGetValue (s); frac = gnc_numeric_div (dst_amt, src_amt, GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE); target_val = gnc_numeric_mul (frac, src_val, scu, GNC_HOW_DENOM_EXACT | GNC_HOW_RND_ROUND_HALF_UP); if (gnc_numeric_check (target_val)) { PERR ("Numeric overflow of value\n" "\tAcct=%s txn=%s\n" "\tdst_amt=%s src_val=%s src_amt=%s\n", xaccAccountGetName (s->acc), xaccTransGetDescription(txn), gnc_num_dbg_to_string(dst_amt), gnc_num_dbg_to_string(src_val), gnc_num_dbg_to_string(src_amt)); continue; } /* If the required price changes are 'small', do nothing. * That is a case that the user will have to deal with * manually. This routine is really intended only for * a gross level of synchronization. */ delta = gnc_numeric_sub_fixed (target_val, dst_val); delta = gnc_numeric_abs (delta); if (maxmult * delta.num < delta.denom) continue; /* If the amount is small, pass on that too */ if ((-maxamtscu < dst_amt.num) && (dst_amt.num < maxamtscu)) continue; /* Make the actual adjustment */ xaccTransBeginEdit (txn); xaccSplitSetValue (s, target_val); xaccTransCommitEdit (txn); } LEAVE (" "); }
static gboolean gnc_schedXaction_end_handler(gpointer data_for_children, GSList* data_from_children, GSList* sibling_data, gpointer parent_data, gpointer global_data, gpointer *result, const gchar *tag) { SchedXaction *sx; gboolean successful = FALSE; xmlNodePtr tree = (xmlNodePtr)data_for_children; gxpf_data *gdata = (gxpf_data*)global_data; struct sx_pdata sx_pdata; if ( parent_data ) { return TRUE; } if ( !tag ) { return TRUE; } g_return_val_if_fail( tree, FALSE ); sx = xaccSchedXactionMalloc( gdata->bookdata ); memset(&sx_pdata, 0, sizeof(sx_pdata)); sx_pdata.sx = sx; sx_pdata.book = gdata->bookdata; g_assert( sx_dom_handlers != NULL ); successful = dom_tree_generic_parse( tree, sx_dom_handlers, &sx_pdata ); if (!successful) { g_critical("failed to parse scheduled xaction"); xmlElemDump( stdout, NULL, tree ); gnc_sx_begin_edit( sx ); xaccSchedXactionDestroy( sx ); goto done; } if (tree->properties) { gchar *sx_name = xaccSchedXactionGetName(sx); xmlAttr *attr; for (attr = tree->properties; attr != NULL; attr = attr->next) { xmlChar *attr_value = attr->children->content; g_debug("sx attribute name[%s] value[%s]", attr->name, attr_value); if (strcmp((const char *)attr->name, "version") != 0) { g_warning("unknown sx attribute [%s]", attr->name); continue; } // if version == 1.0.0: ensure freqspec, no recurrence // if version == 2.0.0: ensure recurrence, no freqspec. if (strcmp((const char *)attr_value, schedxaction_version_string) == 0) { if (!sx_pdata.saw_freqspec) g_critical("did not see freqspec in version 1 sx [%s]", sx_name); if (sx_pdata.saw_recurrence) g_warning("saw recurrence in supposedly version 1 sx [%s]", sx_name); } if (strcmp((const char *)attr_value, schedxaction_version2_string) == 0) { if (sx_pdata.saw_freqspec) g_warning("saw freqspec in version 2 sx [%s]", sx_name); if (!sx_pdata.saw_recurrence) g_critical("did not find recurrence in version 2 sx [%s]", sx_name); } } } // generic_callback -> book_callback: insert the SX in the book gdata->cb( tag, gdata->parsedata, sx ); /* FIXME: this should be removed somewhere near 1.8 release time. */ if ( sx->template_acct == NULL ) { Account *ra = NULL; const char *id = NULL; Account *acct = NULL; sixtp_gdv2 *sixdata = gdata->parsedata; QofBook *book; book = sixdata->book; /* We're dealing with a pre-200107<near-end-of-month> rgmerk change re: storing template accounts. */ /* Fix: get account with name of our GncGUID from the template accounts. Make that our template_acct pointer. */ /* THREAD-UNSAFE */ id = guid_to_string( xaccSchedXactionGetGUID( sx ) ); ra = gnc_book_get_template_root(book); if ( ra == NULL ) { g_warning( "Error getting template root account from being-parsed Book." ); xmlFreeNode( tree ); return FALSE; } acct = gnc_account_lookup_by_name( ra, id ); if ( acct == NULL ) { g_warning("no template account with name [%s]", id); xmlFreeNode( tree ); return FALSE; } g_debug("template account name [%s] for SX with GncGUID [%s]", xaccAccountGetName( acct ), id ); /* FIXME: free existing template account. * HUH????? We only execute this if there isn't * currently an existing template account, don't we? * <rgmerk> */ sx->template_acct = acct; } done: xmlFreeNode( tree ); return successful; }
/******************************************************* * account_splits * * gather the splits / transactions for an account and * send them to a file *******************************************************/ static void account_splits (CsvExportInfo *info, Account *acc, FILE *fh ) { Query *q; GSList *p1, *p2; GList *splits; QofBook *book; gchar *end_sep; gchar *mid_sep; q = qof_query_create_for(GNC_ID_SPLIT); book = gnc_get_current_book(); qof_query_set_book (q, book); /* Set up separators */ if (info->use_quotes) { end_sep = "\""; mid_sep = g_strconcat ( "\"", info->separator_str, "\"", NULL); } else { end_sep = ""; mid_sep = g_strconcat ( info->separator_str, NULL); } /* Sort by transaction date */ p1 = g_slist_prepend (NULL, TRANS_DATE_POSTED); p1 = g_slist_prepend (p1, SPLIT_TRANS); p2 = g_slist_prepend (NULL, QUERY_DEFAULT_SORT); qof_query_set_sort_order (q, p1, p2, NULL); xaccQueryAddSingleAccountMatch (q, acc, QOF_QUERY_AND); xaccQueryAddDateMatchTT (q, TRUE, info->csvd.start_time, TRUE, info->csvd.end_time, QOF_QUERY_AND); /* Run the query */ for (splits = qof_query_run(q); splits; splits = splits->next) { Split *split; Transaction *trans; SplitList *s_list; GList *node; Split *t_split; int nSplits; int cnt; gchar *part1; gchar *part2; gchar *date; const gchar *currentSel; const gchar *split_amount; split = splits->data; trans = xaccSplitGetParent(split); nSplits = xaccTransCountSplits(trans); s_list = xaccTransGetSplitList(trans); /* Date */ date = qof_print_date ( xaccTransGetDate(trans)); part1 = g_strconcat ( end_sep, date, mid_sep, NULL); g_free(date); /* Name */ currentSel = xaccAccountGetName(acc); part2 = g_strconcat ( part1, currentSel, mid_sep, NULL); g_free(part1); /* Number */ currentSel = gnc_get_num_action(trans, NULL); part1 = g_strconcat ( part2, currentSel, mid_sep, NULL); g_free(part2); /* Description */ currentSel = xaccTransGetDescription(trans); part2 = g_strconcat ( part1, currentSel, mid_sep, NULL); g_free(part1); /* Notes */ currentSel = xaccTransGetNotes(trans); if (currentSel == NULL) part1 = g_strconcat ( part2, mid_sep, NULL); else part1 = g_strconcat ( part2, currentSel, mid_sep, NULL); g_free(part2); /* Memo */ currentSel = xaccSplitGetMemo(split); part2 = g_strconcat ( part1, currentSel, mid_sep, NULL); g_free(part1); /* Category */ currentSel = xaccSplitGetCorrAccountName(split); part1 = g_strconcat ( part2, currentSel, mid_sep, "T", mid_sep, NULL); g_free(part2); /* Action */ currentSel = gnc_get_num_action(NULL, split); part2 = g_strconcat ( part1, currentSel, mid_sep, NULL); g_free(part1); /* Reconcile */ switch (xaccSplitGetReconcile (split)) { case NREC: currentSel = "N"; break; case CREC: currentSel = "C"; break; case YREC: currentSel = "Y"; break; case FREC: currentSel = "F"; break; case VREC: currentSel = "V"; break; default: currentSel = "N"; } part1 = g_strconcat ( part2, currentSel, mid_sep, NULL); g_free(part2); /* To with Symbol */ split_amount = xaccPrintAmount(xaccSplitGetAmount(split), gnc_split_amount_print_info(split, TRUE)); part2 = g_strconcat ( part1, split_amount, mid_sep, NULL); g_free(part1); /* From with Symbol */ part1 = g_strconcat ( part2, "", mid_sep, NULL); g_free(part2); /* To Number Only */ split_amount = xaccPrintAmount(xaccSplitGetAmount(split), gnc_split_amount_print_info(split, FALSE)); part2 = g_strconcat ( part1, split_amount, mid_sep, NULL); g_free(part1); /* From Number Only */ part1 = g_strconcat ( part2, "", mid_sep, "", mid_sep, "", end_sep, "\n", NULL); g_free(part2); /* Write to file */ if (!write_line_to_file(fh, part1)) { info->failed = TRUE; break; } g_free(part1); /* Loop through the list of splits for the Transcation */ node = s_list; cnt = 0; while ( (cnt < nSplits) && (info->failed == FALSE)) { t_split = node->data; /* Start of line */ part1 = g_strconcat ( end_sep, mid_sep, mid_sep, mid_sep, mid_sep, mid_sep, NULL); /* Memo */ currentSel = xaccSplitGetMemo(t_split); part2 = g_strconcat ( part1, currentSel, mid_sep, NULL); g_free(part1); /* Account */ currentSel = xaccAccountGetName( xaccSplitGetAccount(t_split)); part1 = g_strconcat ( part2, currentSel, mid_sep, "S", mid_sep, NULL); g_free(part2); /* Action */ currentSel = gnc_get_num_action(NULL, t_split); part2 = g_strconcat ( part1, currentSel, mid_sep, NULL); g_free(part1); /* Reconcile */ switch (xaccSplitGetReconcile (split)) { case NREC: currentSel = "N"; break; case CREC: currentSel = "C"; break; case YREC: currentSel = "Y"; break; case FREC: currentSel = "F"; break; case VREC: currentSel = "V"; break; default: currentSel = "N"; } part1 = g_strconcat ( part2, currentSel, mid_sep, NULL); g_free(part2); /* From / To with Symbol */ split_amount = xaccPrintAmount(xaccSplitGetAmount(t_split), gnc_split_amount_print_info(t_split, TRUE)); if (xaccSplitGetAccount(t_split) == acc) part2 = g_strconcat ( part1, split_amount, mid_sep, mid_sep, NULL); else part2 = g_strconcat ( part1, mid_sep, split_amount, mid_sep, NULL); g_free(part1); /* From / To Numbers only */ split_amount = xaccPrintAmount(xaccSplitGetAmount(t_split), gnc_split_amount_print_info(t_split, FALSE)); if (xaccSplitGetAccount(t_split) == acc) part1 = g_strconcat ( part2, split_amount, mid_sep, mid_sep, NULL); else part1 = g_strconcat ( part2, mid_sep, split_amount, mid_sep, NULL); g_free(part2); /* From / To - Share Price / Conversion factor */ split_amount = xaccPrintAmount(xaccSplitGetSharePrice(t_split), gnc_split_amount_print_info(t_split, FALSE)); if (xaccSplitGetAccount(t_split) == acc) part2 = g_strconcat ( part1, split_amount, mid_sep, end_sep, "\n", NULL); else part2 = g_strconcat ( part1, mid_sep, split_amount, end_sep, "\n", NULL); g_free(part1); if (!write_line_to_file(fh, part2)) info->failed = TRUE; g_free(part2); cnt++; node = node->next; } } g_free(mid_sep); qof_query_destroy (q); g_list_free( splits ); }
int ofx_proc_transaction_cb(struct OfxTransactionData data, void * transaction_user_data) { char dest_string[255]; time64 current_time = gnc_time (NULL); Account *account; Account *investment_account = NULL; Account *income_account = NULL; gchar *investment_account_text, *investment_account_onlineid; gnc_commodity *currency = NULL; gnc_commodity *investment_commodity = NULL; gnc_numeric gnc_amount, gnc_units; QofBook *book; Transaction *transaction; Split *split; gchar *notes, *tmp; g_assert(gnc_ofx_importer_gui); if (!data.account_id_valid) { PERR("account ID for this transaction is unavailable!"); return 0; } account = gnc_import_select_account(gnc_gen_trans_list_widget(gnc_ofx_importer_gui), data.account_id, 0, NULL, NULL, ACCT_TYPE_NONE, NULL, NULL); if (account == NULL) { PERR("Unable to find account for id %s", data.account_id); return 0; } /***** Validate the input strings to ensure utf8 *****/ if (data.name_valid) gnc_utf8_strip_invalid(data.name); if (data.memo_valid) gnc_utf8_strip_invalid(data.memo); if (data.check_number_valid) gnc_utf8_strip_invalid(data.check_number); if (data.reference_number_valid) gnc_utf8_strip_invalid(data.reference_number); /***** Create the transaction and setup transaction data *******/ book = gnc_account_get_book(account); transaction = xaccMallocTransaction(book); xaccTransBeginEdit(transaction); /* Note: Unfortunately libofx <= 0.9.5 will not report a missing * date field as an invalid one. Instead, it will report it as * valid and return a completely bogus date. Starting with * libofx-0.9.6 (not yet released as of 2012-09-09), it will still * be reported as valid but at least the date integer itself is * just plain zero. */ if (data.date_posted_valid && (data.date_posted != 0)) { /* The hopeful case: We have a posted_date */ xaccTransSetDatePostedSecsNormalized(transaction, data.date_posted); } else if (data.date_initiated_valid && (data.date_initiated != 0)) { /* No posted date? Maybe we have an initiated_date */ xaccTransSetDatePostedSecsNormalized(transaction, data.date_initiated); } else { /* Uh no, no valid date. As a workaround use today's date */ xaccTransSetDatePostedSecsNormalized(transaction, current_time); } xaccTransSetDateEnteredSecs(transaction, current_time); /* Put transaction name in Description, or memo if name unavailable */ if (data.name_valid) { xaccTransSetDescription(transaction, data.name); } else if (data.memo_valid) { xaccTransSetDescription(transaction, data.memo); } /* Put everything else in the Notes field */ notes = g_strdup_printf("OFX ext. info: "); if (data.transactiontype_valid) { tmp = notes; notes = g_strdup_printf("%s%s%s", tmp, "|Trans type:", gnc_ofx_ttype_to_string(data.transactiontype)); g_free(tmp); } if (data.invtransactiontype_valid) { tmp = notes; notes = g_strdup_printf("%s%s%s", tmp, "|Investment Trans type:", gnc_ofx_invttype_to_str(data.invtransactiontype)); g_free(tmp); } if (data.memo_valid && data.name_valid) /* Copy only if memo wasn't put in Description */ { tmp = notes; notes = g_strdup_printf("%s%s%s", tmp, "|Memo:", data.memo); g_free(tmp); } if (data.date_funds_available_valid) { Timespec ts; timespecFromTime64(&ts, data.date_funds_available); gnc_timespec_to_iso8601_buff (ts, dest_string); tmp = notes; notes = g_strdup_printf("%s%s%s", tmp, "|Date funds available:", dest_string); g_free(tmp); } if (data.server_transaction_id_valid) { tmp = notes; notes = g_strdup_printf("%s%s%s", tmp, "|Server trans ID (conf. number):", data.server_transaction_id); g_free(tmp); } if (data.standard_industrial_code_valid) { tmp = notes; notes = g_strdup_printf("%s%s%ld", tmp, "|Standard Industrial Code:", data.standard_industrial_code); g_free(tmp); } if (data.payee_id_valid) { tmp = notes; notes = g_strdup_printf("%s%s%s", tmp, "|Payee ID:", data.payee_id); g_free(tmp); } //PERR("WRITEME: GnuCash ofx_proc_transaction():Add PAYEE and ADRESS here once supported by libofx! Notes=%s\n", notes); /* Ideally, gnucash should process the corrected transactions */ if (data.fi_id_corrected_valid) { PERR("WRITEME: GnuCash ofx_proc_transaction(): WARNING: This transaction corrected a previous transaction, but we created a new one instead!\n"); tmp = notes; notes = g_strdup_printf("%s%s%s%s", tmp, "|This corrects transaction #", data.fi_id_corrected, "but GnuCash didn't process the correction!"); g_free(tmp); } xaccTransSetNotes(transaction, notes); g_free(notes); if (data.account_ptr && data.account_ptr->currency_valid) { DEBUG("Currency from libofx: %s", data.account_ptr->currency); currency = gnc_commodity_table_lookup( gnc_get_current_commodities (), GNC_COMMODITY_NS_CURRENCY, data.account_ptr->currency); } else { DEBUG("Currency from libofx unavailable, defaulting to account's default"); currency = xaccAccountGetCommodity(account); } xaccTransSetCurrency(transaction, currency); if (data.amount_valid) { if (!data.invtransactiontype_valid) { /***** Process a normal transaction ******/ DEBUG("Adding split; Ordinary banking transaction, money flows from or into the source account"); split = xaccMallocSplit(book); xaccTransAppendSplit(transaction, split); xaccAccountInsertSplit(account, split); gnc_amount = gnc_ofx_numeric_from_double_txn(data.amount, transaction); xaccSplitSetBaseValue(split, gnc_amount, xaccTransGetCurrency(transaction)); /* set tran-num and/or split-action per book option */ if (data.check_number_valid) { gnc_set_num_action(transaction, split, data.check_number, NULL); } else if (data.reference_number_valid) { gnc_set_num_action(transaction, split, data.reference_number, NULL); } /* Also put the ofx transaction's memo in the * split's memo field */ if (data.memo_valid) { xaccSplitSetMemo(split, data.memo); } if (data.fi_id_valid) { gnc_import_set_split_online_id(split, data.fi_id); } } else if (data.unique_id_valid && data.security_data_valid && data.security_data_ptr != NULL && data.security_data_ptr->secname_valid) { gboolean choosing_account = TRUE; /********* Process an investment transaction **********/ /* Note that the ACCT_TYPE_STOCK account type should be replaced with something derived from data.invtranstype*/ // We have an investment transaction. First select the correct commodity. investment_commodity = gnc_import_select_commodity(data.unique_id, FALSE, NULL, NULL); if (investment_commodity != NULL) { // As we now have the commodity, select the account with that commodity. investment_account_text = g_strdup_printf( /* This string is a default account name. It MUST NOT contain the character ':' anywhere in it or in any translations. */ _("Stock account for security \"%s\""), data.security_data_ptr->secname); investment_account_onlineid = g_strdup_printf( "%s%s", data.account_id, data.unique_id); investment_account = gnc_import_select_account(NULL, investment_account_onlineid, 1, investment_account_text, investment_commodity, ACCT_TYPE_STOCK, NULL, NULL); // but use it only if that's really the right commodity if (investment_account && xaccAccountGetCommodity(investment_account) != investment_commodity) investment_account = NULL; // Loop until we either have an account, or the user pressed Cancel while (!investment_account && choosing_account) { // No account with correct commodity automatically found. // But are we in auto-create mode and already know a parent? if (auto_create_commodity && ofx_parent_account) { // Yes, so use that as parent when auto-creating the new account below. investment_account = ofx_parent_account; } else { // Let the user choose an account investment_account = gnc_import_select_account( gnc_gen_trans_list_widget(gnc_ofx_importer_gui), data.unique_id, TRUE, investment_account_text, investment_commodity, ACCT_TYPE_STOCK, NULL, &choosing_account); } // Does the chosen account have the right commodity? if (investment_account && xaccAccountGetCommodity(investment_account) != investment_commodity) { if (auto_create_commodity && xaccAccountTypesCompatible(xaccAccountGetType(investment_account), ACCT_TYPE_STOCK)) { // The user chose an account, but it does // not have the right commodity. Also, // auto-creation is on. Hence, we create a // new child account of the selected one, // and this one will have the right // commodity. Account *parent_account = investment_account; investment_account = gnc_ofx_new_account(investment_account_text, investment_commodity, parent_account, ACCT_TYPE_STOCK); if (investment_account) { gnc_import_set_acc_online_id(investment_account, data.unique_id); choosing_account = FALSE; ofx_parent_account = parent_account; } else { ofx_parent_account = NULL; } } else { // No account with matching commodity. Ask the user // whether to continue or abort. choosing_account = gnc_verify_dialog( gnc_gen_trans_list_widget(gnc_ofx_importer_gui), TRUE, "The chosen account \"%s\" does not have the correct " "currency/security \"%s\" (it has \"%s\" instead). " "This account cannot be used. " "Do you want to choose again?", xaccAccountGetName(investment_account), gnc_commodity_get_fullname(investment_commodity), gnc_commodity_get_fullname(xaccAccountGetCommodity(investment_account))); // We must also delete the online_id that was set in gnc_import_select_account() gnc_import_set_acc_online_id(investment_account, ""); investment_account = NULL; } } } if (!investment_account) { PERR("No investment account found for text: %s\n", investment_account_text); } g_free (investment_account_text); g_free (investment_account_onlineid); investment_account_text = NULL; if (investment_account != NULL && data.unitprice_valid && data.units_valid && ( data.invtransactiontype != OFX_INCOME ) ) { DEBUG("Adding investment split; Money flows from or into the stock account"); split = xaccMallocSplit(book); xaccTransAppendSplit(transaction, split); xaccAccountInsertSplit(investment_account, split); gnc_amount = gnc_ofx_numeric_from_double (ofx_get_investment_amount(&data), investment_commodity); gnc_units = gnc_ofx_numeric_from_double (data.units, investment_commodity); xaccSplitSetAmount(split, gnc_units); xaccSplitSetValue(split, gnc_amount); /* set tran-num and/or split-action per book option */ if (data.check_number_valid) { gnc_set_num_action(transaction, split, data.check_number, NULL); } else if (data.reference_number_valid) { gnc_set_num_action(transaction, split, data.reference_number, NULL); } if (data.security_data_ptr->memo_valid) { xaccSplitSetMemo(split, data.security_data_ptr->memo); } if (data.fi_id_valid) { gnc_import_set_split_online_id(split, data.fi_id); } } else { if (investment_account) PERR("The investment account, units or unitprice was not found for the investment transaction"); } } else { PERR("Commodity not found for the investment transaction"); } if (data.invtransactiontype_valid && investment_account) { if (data.invtransactiontype == OFX_REINVEST || data.invtransactiontype == OFX_INCOME) { DEBUG("Now let's find an account for the destination split"); income_account = gnc_ofx_kvp_get_assoc_account(investment_account); if (income_account == NULL) { DEBUG("Couldn't find an associated income account"); investment_account_text = g_strdup_printf( /* This string is a default account name. It MUST NOT contain the character ':' anywhere in it or in any translations. */ _("Income account for security \"%s\""), data.security_data_ptr->secname); income_account = gnc_import_select_account( gnc_gen_trans_list_widget(gnc_ofx_importer_gui), NULL, 1, investment_account_text, currency, ACCT_TYPE_INCOME, NULL, NULL); gnc_ofx_kvp_set_assoc_account(investment_account, income_account); DEBUG("KVP written"); } else { DEBUG("Found at least one associated income account"); } } if (income_account != NULL && data.invtransactiontype == OFX_REINVEST) { DEBUG("Adding investment split; Money flows from the income account"); split = xaccMallocSplit(book); xaccTransAppendSplit(transaction, split); xaccAccountInsertSplit(income_account, split); gnc_amount = gnc_ofx_numeric_from_double_txn (data.amount, transaction); xaccSplitSetBaseValue(split, gnc_amount, xaccTransGetCurrency(transaction)); // Set split memo from ofx transaction name or memo gnc_ofx_set_split_memo(&data, split); } if (income_account != NULL && data.invtransactiontype == OFX_INCOME) { DEBUG("Adding investment split; Money flows from the income account"); split = xaccMallocSplit(book); xaccTransAppendSplit(transaction, split); xaccAccountInsertSplit(income_account, split); gnc_amount = gnc_ofx_numeric_from_double_txn (-data.amount,/*OFX_INCOME amounts come in as positive numbers*/ transaction); xaccSplitSetBaseValue(split, gnc_amount, xaccTransGetCurrency(transaction)); // Set split memo from ofx transaction name or memo gnc_ofx_set_split_memo(&data, split); } } if (data.invtransactiontype_valid && data.invtransactiontype != OFX_REINVEST) { DEBUG("Adding investment split; Money flows from or to the cash account"); split = xaccMallocSplit(book); xaccTransAppendSplit(transaction, split); xaccAccountInsertSplit(account, split); gnc_amount = gnc_ofx_numeric_from_double_txn( -ofx_get_investment_amount(&data), transaction); xaccSplitSetBaseValue(split, gnc_amount, xaccTransGetCurrency(transaction)); // Set split memo from ofx transaction name or memo gnc_ofx_set_split_memo(&data, split); } } /* Send transaction to importer GUI. */ if (xaccTransCountSplits(transaction) > 0) { DEBUG("%d splits sent to the importer gui", xaccTransCountSplits(transaction)); gnc_gen_trans_list_add_trans (gnc_ofx_importer_gui, transaction); } else { PERR("No splits in transaction (missing account?), ignoring."); xaccTransDestroy(transaction); xaccTransCommitEdit(transaction); } } else { PERR("The transaction doesn't have a valid amount"); xaccTransDestroy(transaction); xaccTransCommitEdit(transaction); } return 0; }//end ofx_proc_transaction()