After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 754530 - Add Simple Layout CSV Transaction Export
Add Simple Layout CSV Transaction Export
Status: RESOLVED FIXED
Product: GnuCash
Classification: Other
Component: General
git-master
Other Linux
: Normal normal
: ---
Assigned To: gnucash-general-maint
gnucash-general-maint
Depends on:
Blocks: 754533
 
 
Reported: 2015-09-03 14:23 UTC by Bob
Modified: 2018-06-29 23:42 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add simple layout (30.83 KB, patch)
2015-09-03 14:25 UTC, Bob
none Details | Review
Add Simple Layout to CSV Export (56.04 KB, patch)
2015-09-24 15:54 UTC, Bob
committed Details | Review

Description Bob 2015-09-03 14:23:19 UTC
This patch set will add a simple layout to the CSV transaction exporter so that each transaction is on one line.
Comment 1 Bob 2015-09-03 14:25:45 UTC
Created attachment 310598 [details] [review]
Add simple layout
Comment 2 John Ralls 2015-09-15 23:29:12 UTC
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 3 John Ralls 2015-09-16 00:22:53 UTC
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.
Comment 4 Bob 2015-09-24 15:54:19 UTC
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 5 Geert Janssens 2015-10-08 20:32:00 UTC
Comment on attachment 312068 [details] [review]
Add Simple Layout to CSV Export

This looks much better, thanks.

Committed to master.
Comment 6 Bob 2016-11-18 12:17:06 UTC
Patch committed...
Comment 7 John Ralls 2018-06-29 23:42:51 UTC
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.