From: Charles McGarvey Date: Tue, 18 Jun 2019 05:19:43 +0000 (-0600) Subject: remove --default-account option X-Git-Tag: v0.004~2 X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=b902bb516a91fd76711281b476b9911a05946dec;p=chaz%2Fhomebank2ledger remove --default-account option 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. --- diff --git a/bin/homebank2ledger b/bin/homebank2ledger index 43d4b91..92af63d 100644 --- a/bin/homebank2ledger +++ b/bin/homebank2ledger @@ -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 tries to come diff --git a/lib/App/HomeBank2Ledger.pm b/lib/App/HomeBank2Ledger.pm index e152a7e..367f425 100644 --- a/lib/App/HomeBank2Ledger.pm +++ b/lib/App/HomeBank2Ledger.pm @@ -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)]);