]> Dogcows Code - chaz/homebank2ledger/commitdiff
remove --default-account option
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Tue, 18 Jun 2019 05:19:43 +0000 (23:19 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Tue, 18 Jun 2019 05:19:43 +0000 (23:19 -0600)
This wasn't very useful and didn't handle that there should be
a different default account per broad category, expenses and income.
Can use --rename-account instead.

bin/homebank2ledger
lib/App/HomeBank2Ledger.pm

index 43d4b918d62c198613a9f06e54e1515b5734fb57..92af63d4477e447337d229ad45cea48458a3ea90 100644 (file)
@@ -8,7 +8,7 @@
                     [--version|--help|--manual] [--account-width NUM]
                     [--accounts|--no-accounts] [--payees|--no-payees]
                     [--tags|--no-tags] [--commodities|--no-commodities]
-                    [--opening-date DATE] [--default-account STR]
+                    [--opening-date DATE]
                     [--rename-account STR]... [--exclude-account STR]...
 
 =head1 DESCRIPTION
@@ -124,12 +124,6 @@ needed) to support HomeBank's ability to configure accounts with opening balance
 
 Date must be in the form "YYYY-MM-DD". Defaults to the date of the first transaction.
 
-=head2 --default-account STR
-
-Specify the account to use for one-sided transactions (if any). Defaults to "Expenses:No Category".
-
-A default account may be necessary because with Ledger all transactions are double-entry.
-
 =head2 --rename-account STR
 
 Specifies a mapping for renaming accounts in the output. By default C<homebank2ledger> tries to come
index e152a7ef8b5818a48b38518f71c1f12dbe322aa4..367f42597343ab036d4a6baf2825d8eed7d27fd4 100644 (file)
@@ -123,6 +123,9 @@ sub convert_homebank_to_ledger {
     my $homebank = shift;
     my $opts     = shift || {};
 
+    my $default_account_income   = 'Income:Unknown';
+    my $default_account_expenses = 'Expenses:Unknown';
+
     my $ledger = App::HomeBank2Ledger::Ledger->new;
 
     my $transactions    = $homebank->sorted_transactions;
@@ -149,13 +152,17 @@ sub convert_homebank_to_ledger {
             $item->{excluded} = 1 if $item->{ledger_name} =~ /$re/;
         }
     }
+    while (my ($re, $replacement) = each %{$opts->{rename_accounts}}) {
+        $default_account_income   =~ s/$re/$replacement/;
+        $default_account_expenses =~ s/$re/$replacement/;
+    }
 
     my $has_initial_balance = grep { $_->{initial} && !$_->{excluded} } @$accounts;
 
     if ($opts->{accounts}) {
         my @accounts = map { $_->{ledger_name} } grep { !$_->{excluded} } @$accounts, @$categories;
 
-        push @accounts, $opts->{default_account};
+        push @accounts, $default_account_income, $default_account_expenses;
         push @accounts, $OPENING_BALANCES_ACCOUNT if $has_initial_balance;
 
         $ledger->add_accounts(@accounts);
@@ -286,10 +293,12 @@ sub convert_homebank_to_ledger {
             my @categories  = split(/\|\|/, $transaction->{split_category} || '');
 
             for (my $i = 0; $amounts[$i]; ++$i) {
-                my $amount        = -$amounts[$i];
-                my $category      = $homebank->find_category_by_key($categories[$i]);
-                my $memo          = $memos[$i] || '';
-                my $other_account = $category ? $category->{ledger_name} : $opts->{default_account};
+                my $amount          = -$amounts[$i];
+                my $category        = $homebank->find_category_by_key($categories[$i]);
+                my $memo            = $memos[$i] || '';
+                my $other_account   = $category   ? $category->{ledger_name}
+                                    : $amount < 0 ? $default_account_income
+                                    :               $default_account_expenses;
 
                 push @postings, {
                     account     => $other_account,
@@ -303,12 +312,16 @@ sub convert_homebank_to_ledger {
             }
         }
         else {  # with or without category
-            my $category      = $homebank->find_category_by_key($transaction->{category});
-            my $other_account = $category ? $category->{ledger_name} : $opts->{default_account};
+            my $amount          = -$transaction->{amount};
+            my $category        = $homebank->find_category_by_key($transaction->{category});
+            my $other_account   = $category   ? $category->{ledger_name}
+                                : $amount < 0 ? $default_account_income
+                                :               $default_account_expenses;
+
             push @postings, {
                 account     => $other_account,
                 commodity   => $commodities{$account->{currency}},
-                amount      => -$transaction->{amount},
+                amount      => $amount,
                 payee       => $payee->{name},
                 memo        => $memo,
                 status      => $status,
@@ -380,7 +393,6 @@ sub parse_args {
         tags                => 1,
         commodities         => 1,
         opening_date        => '',
-        default_account     => 'Expenses:No Category',
         rename_accounts     => {},
         exclude_accounts    => [],
     );
@@ -398,7 +410,6 @@ sub parse_args {
         'tags!'                 => \$opts{tags},
         'commodities!'          => \$opts{commodities},
         'opening-date=s'        => \$opts{opening_date},
-        'default-account=s'     => \$opts{default_account},
         'rename-account|r=s'    => \%{$opts{rename_accounts}},
         'exclude-account|x=s'   => \@{$opts{exclude_accounts}},
     ) or pod2usage(-exitval => 1, -verbose => 99, -sections => [qw(SYNOPSIS OPTIONS)]);
This page took 0.033956 seconds and 4 git commands to generate.