GNOME Bugzilla – Bug 754530
Add Simple Layout CSV Transaction Export
Last modified: 2018-06-29 23:42:51 UTC
This patch set will add a simple layout to the CSV transaction exporter so that each transaction is on one line.
Created attachment 310598 [details] [review] Add simple layout
The transaction that produces the complex CSV "01/19/2014"," ","","Brokerage Account","","Reinvest Dividend","","","-- Split Transaction --","-- Split Transaction --","T","","n","","USD","CURRENCY","","" "","","","","","","","","Assets:Investments:Brokerage Account:Stock:AAPL","AAPL","S","Buy","n","10 AAPL","AAPL","NASDAQ","10","552" "","","","","","","","","Expenses:Commissions","Commissions","S","","n","$35.00","USD","CURRENCY","35.00","1.00" "","","","","","","","","Expenses:Taxes:Other Tax","Other Tax","S","","n","$10.00","USD","CURRENCY","10.00","1.00" "","","","","","","","","Assets:Investments:Brokerage Account","Brokerage Account","S","","n","$5,565.00","USD","CURRENCY","5,565.00","1.00" "","","","","","","","","Orphan-USD","Orphan-USD","S","'","n","$0.00","USD","CURRENCY","0.00","1.00" "","","","","","","","Dividend Recieved","Income:Dividend Income","Dividend Income","S","","n","-$5,565.00","USD","CURRENCY","-5,565.00","1.00" "","","","","","","","","Assets:Investments:Brokerage Account","Brokerage Account","S","","n","-$5,565.00","USD","CURRENCY","-5,565.00","1.00" Produces the Simple CSV "01/19/2014","Brokerage Account","","Reinvest Dividend","-- Split Transaction --","n","-$5,555.00","-5,555.00","1.00" Since this will lose user data I think you should raise a warning and refuse to complete the export if you find a transaction with more than two splits. For the two-split case you need to put the full path of the originating account in the "Account Name" field or you won't be able to reimport it; if a user has same-name subaccounts there will be an ambiguity as well. Also the "Account Name" in the simple format needs to have the full path, not just the account name. Although I shortened the account
Comment on attachment 310598 [details] [review] Add simple layout Non-exported functions should be declared static, and callbacks generally shouldn't be exported. start_trans_string should explain what will happen if a transaction has more than 2 splits. account_splits() is already almost 300 lines and you want to add another 80, many of which are duplicates of what's already there. Yuck. Break up your layout into functions that take the line so far and return the modified line. For example: static char* begin_simple_string (Transaction *trans) { char *date = qof_print_date (xaccTransGetDate (trans)); char *result = g_strconcat (end_sep, date, mid_sep, NULL); g_free (date); return result; } static char* add_account_name(char *so_far, Transaction *trans, Account *acc, CsvExportInfo *info) { char *name = xaccAccountGetName (acc); char *conv = csv_txn_test_field_string (info, name); char *result = g_strconcat (so_far, conv, mid_sep, NULL); g_free (name); g_free (conv); g_free (so_far); return result; } Then put it together with static char* make_simple_trans_line (Account *acc, Transaction *trans, CsvExportInfo *info) { char *result = begin_simple_layout_string (trans); result = add_account_name (result, trans, acc, info); ... return result; } Create as well make_complex_trans_line() and make_split_line(); you should be able to reuse a bunch of the add_foo static functions. Then in account_splits, you can have char *()(Account*, Transaction*, CsvExportInfo*)make_trans_line = info->simple_layout ? make_simple_trans_line : make_complex_trans_line; ... for (splits = qof_query_run (q); splits; splits = splits->next) { char *line = make_trans_line(trans, acc, info); if (!write_line_to_file (fh, line)) { info->failed = TRUE; g_free (line); break; } g_free (line); for (GList *node = g_list_first (s_list); node != NULL && !info->failed; node = g_list_next (list)) { char *line = make_split_line((Split*)node->data, info); if (!write_line_to_file (fh, line)) { info->failed = TRUE; g_free (line); break; } g_free (line); } } Result: Much shorter, easier to understand, and easier to maintain code, more code reuse.
Created attachment 312068 [details] [review] Add Simple Layout to CSV Export John, This patch is based on your ideas and is much clearer than my first idea. I have added some text to point out that the simple view may loose some transfer detail but I still want it to run as it should look like a single row register view. The reason that some callbacks are not static is that they are defined in the glade files. The ones that are static have are the ones connected in the source file with g_signal_connect. Hope this is OK, always learning.
Comment on attachment 312068 [details] [review] Add Simple Layout to CSV Export This looks much better, thanks. Committed to master.
Patch committed...
GnuCash bug tracking has moved to a new Bugzilla host. This bug has been copied to https://bugs.gnucash.org/show_bug.cgi?id=754530. Please update any external references or bookmarks.