/** Creates a list of transactions from parsed data. Transactions that * could be created from rows are placed in parse_data->transactions; * rows that fail are placed in parse_data->error_lines. (Note: there * is no way for this function to "fail," i.e. it only returns 0, so * it may be changed to a void function in the future.) * @param parse_data Data that is being parsed * @param account Account with which transactions are created * @param redo_errors TRUE to convert only error data, FALSE for all data * @return 0 on success, 1 on failure */ int gnc_csv_parse_to_trans (GncCsvParseData* parse_data, Account* account, gboolean redo_errors) { gboolean hasBalanceColumn; int i, j, max_cols = 0; GArray* column_types = parse_data->column_types; GList *error_lines = NULL, *begin_error_lines = NULL; /* last_transaction points to the last element in * parse_data->transactions, or NULL if it's empty. */ GList* last_transaction = NULL; /* Free parse_data->error_lines and parse_data->transactions if they * already exist. */ if (redo_errors) /* If we're redoing errors, we save freeing until the end. */ { begin_error_lines = error_lines = parse_data->error_lines; } else { if (parse_data->error_lines != NULL) { g_list_free(parse_data->error_lines); } if (parse_data->transactions != NULL) { g_list_free (parse_data->transactions); } } parse_data->error_lines = NULL; if (redo_errors) /* If we're looking only at error data ... */ { if (parse_data->transactions == NULL) { last_transaction = NULL; } else { /* Move last_transaction to the end. */ last_transaction = parse_data->transactions; while (g_list_next (last_transaction) != NULL) { last_transaction = g_list_next (last_transaction); } } /* ... we use only the lines in error_lines. */ if (error_lines == NULL) i = parse_data->orig_lines->len; /* Don't go into the for loop. */ else i = GPOINTER_TO_INT(error_lines->data); } else /* Otherwise, we look at all the data. */ { /* The following while-loop effectively behaves like the following for-loop: * for(i = 0; i < parse_data->orig_lines->len; i++). */ i = parse_data->start_row; last_transaction = NULL; } /* set parse_data->end_row to number of lines */ if (parse_data->end_row > parse_data->orig_lines->len) parse_data->end_row = parse_data->orig_lines->len; while (i < parse_data->end_row) { GPtrArray* line = parse_data->orig_lines->pdata[i]; /* This flag is TRUE if there are any errors in this row. */ gboolean errors = FALSE; gchar* error_message = NULL; TransPropertyList* list = trans_property_list_new (account, parse_data->date_format, parse_data->currency_format); GncCsvTransLine* trans_line = NULL; for (j = 0; j < line->len; j++) { /* We do nothing in "None" or "Account" columns. */ if ((column_types->data[j] != GNC_CSV_NONE) && (column_types->data[j] != GNC_CSV_ACCOUNT)) { /* Affect the transaction appropriately. */ TransProperty* property = trans_property_new (column_types->data[j], list); gboolean succeeded = trans_property_set (property, line->pdata[j]); /* TODO Maybe move error handling to within TransPropertyList functions? */ if (succeeded) { trans_property_list_add (property); } else { errors = TRUE; error_message = g_strdup_printf (_("%s column could not be understood."), _(gnc_csv_column_type_strs[property->type])); trans_property_free (property); break; } } } /* If we had success, add the transaction to parse_data->transaction. */ if (!errors) { trans_line = trans_property_list_to_trans (list, &error_message); errors = trans_line == NULL; } trans_property_list_free (list); /* If there were errors, add this line to parse_data->error_lines. */ if (errors) { parse_data->error_lines = g_list_append (parse_data->error_lines, GINT_TO_POINTER(i)); /* If there's already an error message, we need to replace it. */ if (line->len > (int)(parse_data->orig_row_lengths->data[i])) { g_free(line->pdata[line->len - 1]); line->pdata[line->len - 1] = error_message; } else { /* Put the error message at the end of the line. */ g_ptr_array_add (line, error_message); } } else { /* If all went well, add this transaction to the list. */ trans_line->line_no = i; /* We keep the transactions sorted by date. We start at the end * of the list and go backward, simply because the file itself * is probably also sorted by date (but we need to handle the * exception anyway). */ /* If we can just put it at the end, do so and increment last_transaction. */ if (last_transaction == NULL || xaccTransGetDate (((GncCsvTransLine*)(last_transaction->data))->trans) <= xaccTransGetDate (trans_line->trans)) { parse_data->transactions = g_list_append (parse_data->transactions, trans_line); /* If this is the first transaction, we need to get last_transaction on track. */ if (last_transaction == NULL) last_transaction = parse_data->transactions; else /* Otherwise, we can just continue. */ last_transaction = g_list_next (last_transaction); } /* Otherwise, search backward for the correct spot. */ else { GList* insertion_spot = last_transaction; while (insertion_spot != NULL && xaccTransGetDate (((GncCsvTransLine*)(insertion_spot->data))->trans) > xaccTransGetDate (trans_line->trans)) { insertion_spot = g_list_previous (insertion_spot); } /* Move insertion_spot one location forward since we have to * use the g_list_insert_before function. */ if (insertion_spot == NULL) /* We need to handle the case of inserting at the beginning of the list. */ insertion_spot = parse_data->transactions; else insertion_spot = g_list_next (insertion_spot); parse_data->transactions = g_list_insert_before (parse_data->transactions, insertion_spot, trans_line); } } /* Increment to the next row. */ if (redo_errors) { /* Move to the next error line in the list. */ error_lines = g_list_next (error_lines); if (error_lines == NULL) i = parse_data->orig_lines->len; /* Don't continue the for loop. */ else i = GPOINTER_TO_INT(error_lines->data); } else { if (parse_data->skip_rows == FALSE) i++; else i = i + 2; } } /* If we have a balance column, set the appropriate amounts on the transactions. */ hasBalanceColumn = FALSE; for (i = 0; i < parse_data->column_types->len; i++) { if (parse_data->column_types->data[i] == GNC_CSV_BALANCE) { hasBalanceColumn = TRUE; break; } } if (hasBalanceColumn) { GList* transactions = parse_data->transactions; /* balance_offset is how much the balance currently in the account * differs from what it will be after the transactions are * imported. This will be sum of all the previous transactions for * any given transaction. */ gnc_numeric balance_offset = double_to_gnc_numeric (0.0, xaccAccountGetCommoditySCU (account), GNC_HOW_RND_ROUND_HALF_UP); while (transactions != NULL) { GncCsvTransLine* trans_line = (GncCsvTransLine*)transactions->data; if (trans_line->balance_set) { time64 date = xaccTransGetDate (trans_line->trans); /* Find what the balance should be by adding the offset to the actual balance. */ gnc_numeric existing_balance = gnc_numeric_add (balance_offset, xaccAccountGetBalanceAsOfDate (account, date), xaccAccountGetCommoditySCU (account), GNC_HOW_RND_ROUND_HALF_UP); /* The amount of the transaction is the difference between the new and existing balance. */ gnc_numeric amount = gnc_numeric_sub (trans_line->balance, existing_balance, xaccAccountGetCommoditySCU (account), GNC_HOW_RND_ROUND_HALF_UP); SplitList* splits = xaccTransGetSplitList (trans_line->trans); while (splits) { SplitList* next_splits = g_list_next (splits); xaccSplitDestroy ((Split*)splits->data); splits = next_splits; } trans_add_split (trans_line->trans, account, gnc_account_get_book (account), amount, trans_line->num); if (trans_line->num) g_free (trans_line->num); /* This new transaction needs to be added to the balance offset. */ balance_offset = gnc_numeric_add (balance_offset, amount, xaccAccountGetCommoditySCU (account), GNC_HOW_RND_ROUND_HALF_UP); } transactions = g_list_next (transactions); } } if (redo_errors) /* Now that we're at the end, we do the freeing. */ { g_list_free (begin_error_lines); } /* We need to resize parse_data->column_types since errors may have added columns. */ for (i = 0; i < parse_data->orig_lines->len; i++) { if (max_cols < ((GPtrArray*)(parse_data->orig_lines->pdata[i]))->len) max_cols = ((GPtrArray*)(parse_data->orig_lines->pdata[i]))->len; } i = parse_data->column_types->len; parse_data->column_types = g_array_set_size (parse_data->column_types, max_cols); for (; i < max_cols; i++) { parse_data->column_types->data[i] = GNC_CSV_NONE; } return 0; }
/** * @fixme Move this non-GUI code into the engine. **/ static void gnc_ui_accounts_recurse (Account *parent, GList **currency_list, GNCSummarybarOptions options) { gnc_numeric start_amount; gnc_numeric start_amount_default_currency; gnc_numeric end_amount; gnc_numeric end_amount_default_currency; GNCAccountType account_type; gnc_commodity * account_currency; gnc_commodity * euro_commodity; GNCCurrencyAcc *currency_accum = NULL; GNCCurrencyAcc *euro_accum = NULL; GNCCurrencyAcc *grand_total_accum = NULL; GNCCurrencyAcc *non_curr_accum = NULL; GList *children, *node; gboolean non_currency = FALSE; Timespec end_timespec; Timespec start_timespec; if (parent == NULL) return; children = gnc_account_get_children(parent); for (node = children; node; node = g_list_next(node)) { Account *account = node->data; account_type = xaccAccountGetType(account); account_currency = xaccAccountGetCommodity(account); if (options.grand_total) grand_total_accum = gnc_ui_get_currency_accumulator(currency_list, options.default_currency, TOTAL_GRAND_TOTAL); if (options.euro) { euro_commodity = gnc_get_euro (); euro_accum = gnc_ui_get_currency_accumulator(currency_list, euro_commodity, TOTAL_CURR_TOTAL); } else euro_commodity = NULL; if (!gnc_commodity_is_currency(account_currency)) { non_currency = TRUE; non_curr_accum = gnc_ui_get_currency_accumulator(currency_list, options.default_currency, TOTAL_NON_CURR_TOTAL); } if (!non_currency || options.non_currency) { currency_accum = gnc_ui_get_currency_accumulator(currency_list, account_currency, TOTAL_SINGLE); } switch (account_type) { case ACCT_TYPE_BANK: case ACCT_TYPE_CASH: case ACCT_TYPE_ASSET: case ACCT_TYPE_STOCK: case ACCT_TYPE_MUTUAL: case ACCT_TYPE_CREDIT: case ACCT_TYPE_LIABILITY: case ACCT_TYPE_PAYABLE: case ACCT_TYPE_RECEIVABLE: end_amount = xaccAccountGetBalanceAsOfDate(account, options.end_date); timespecFromTime_t(&end_timespec, options.end_date); end_amount_default_currency = xaccAccountConvertBalanceToCurrencyAsOfDate (account, end_amount, account_currency, options.default_currency, timespecToTime_t(timespecCanonicalDayTime(end_timespec))); if (!non_currency || options.non_currency) { currency_accum->assets = gnc_numeric_add (currency_accum->assets, end_amount, gnc_commodity_get_fraction (account_currency), GNC_HOW_RND_ROUND_HALF_UP); } if (non_currency) { non_curr_accum->assets = gnc_numeric_add (non_curr_accum->assets, end_amount_default_currency, gnc_commodity_get_fraction (options.default_currency), GNC_HOW_RND_ROUND_HALF_UP); } if (options.grand_total) { grand_total_accum->assets = gnc_numeric_add (grand_total_accum->assets, end_amount_default_currency, gnc_commodity_get_fraction (options.default_currency), GNC_HOW_RND_ROUND_HALF_UP); } if (options.euro && (currency_accum != euro_accum)) { euro_accum->assets = gnc_numeric_add (euro_accum->assets, gnc_convert_to_euro(account_currency, end_amount), gnc_commodity_get_fraction (euro_commodity), GNC_HOW_RND_ROUND_HALF_UP); } gnc_ui_accounts_recurse(account, currency_list, options); break; case ACCT_TYPE_INCOME: case ACCT_TYPE_EXPENSE: start_amount = xaccAccountGetBalanceAsOfDate(account, options.start_date); timespecFromTime_t(&start_timespec, options.start_date); start_amount_default_currency = xaccAccountConvertBalanceToCurrencyAsOfDate (account, start_amount, account_currency, options.default_currency, timespecToTime_t(timespecCanonicalDayTime(start_timespec))); end_amount = xaccAccountGetBalanceAsOfDate(account, options.end_date); timespecFromTime_t(&end_timespec, options.end_date); end_amount_default_currency = xaccAccountConvertBalanceToCurrencyAsOfDate (account, end_amount, account_currency, options.default_currency, timespecToTime_t(timespecCanonicalDayTime(end_timespec))); if (!non_currency || options.non_currency) { currency_accum->profits = gnc_numeric_add (currency_accum->profits, start_amount, gnc_commodity_get_fraction (account_currency), GNC_HOW_RND_ROUND_HALF_UP); currency_accum->profits = gnc_numeric_sub (currency_accum->profits, end_amount, gnc_commodity_get_fraction (account_currency), GNC_HOW_RND_ROUND_HALF_UP); } if (non_currency) { non_curr_accum->profits = gnc_numeric_add (non_curr_accum->profits, start_amount_default_currency, gnc_commodity_get_fraction (options.default_currency), GNC_HOW_RND_ROUND_HALF_UP); non_curr_accum->profits = gnc_numeric_sub (non_curr_accum->profits, end_amount_default_currency, gnc_commodity_get_fraction (options.default_currency), GNC_HOW_RND_ROUND_HALF_UP); } if (options.grand_total) { grand_total_accum->profits = gnc_numeric_add (grand_total_accum->profits, start_amount_default_currency, gnc_commodity_get_fraction (options.default_currency), GNC_HOW_RND_ROUND_HALF_UP); grand_total_accum->profits = gnc_numeric_sub (grand_total_accum->profits, end_amount_default_currency, gnc_commodity_get_fraction (options.default_currency), GNC_HOW_RND_ROUND_HALF_UP); } if (options.euro && (currency_accum != euro_accum)) { euro_accum->profits = gnc_numeric_add (euro_accum->profits, gnc_convert_to_euro(account_currency, start_amount), gnc_commodity_get_fraction (euro_commodity), GNC_HOW_RND_ROUND_HALF_UP); euro_accum->profits = gnc_numeric_sub (euro_accum->profits, gnc_convert_to_euro(account_currency, end_amount), gnc_commodity_get_fraction (euro_commodity), GNC_HOW_RND_ROUND_HALF_UP); } gnc_ui_accounts_recurse(account, currency_list, options); break; case ACCT_TYPE_EQUITY: /* no-op, see comments at top about summing assets */ break; /** * @fixme I don't know if this is right or if trading accounts should be * treated like income and expense accounts. **/ case ACCT_TYPE_TRADING: break; case ACCT_TYPE_CURRENCY: default: break; } } g_list_free(children); }