=cut
-# TODO - add posting memo
-# TODO - transaction description ("narration" in beancount)
-# TODO - payees
-# TODO - budget/scheduled
-# TODO - consolidate tags on transaction
-# TODO - consolidate payees on transaction
-
-use warnings FATAL => 'all'; # temp fatal all
+use warnings;
use strict;
use App::HomeBank2Ledger::Formatter;
if ($opts->{manual}) {
pod2usage(-exitval => 0, -verbose => 2);
}
+ if (!$opts->{input}) {
+ print STDERR "Input file is required.\n";
+ exit(1);
+ }
my $homebank = File::HomeBank->new(file => $opts->{input});
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;
$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 if !grep { $_ eq $default_account_income } @accounts;
+ push @accounts, $default_account_expenses if !grep { $_ eq $default_account_expenses } @accounts;
push @accounts, $OPENING_BALANCES_ACCOUNT if $has_initial_balance;
$ledger->add_accounts(@accounts);
}
if ($has_initial_balance) {
- # transactions are sorted, so the first transaction is the earliest
+ # transactions are sorted, so the first transaction is the oldest
my $first_date = $opts->{opening_date} || $transactions->[0]{date};
if ($first_date !~ /^\d{4}-\d{2}-\d{2}$/) {
die "Opening date must be in the form YYYY-MM-DD.\n";
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,
}
}
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 => '', # TODO
+ memo => $memo,
status => $status,
tags => $tags,
};
$ledger->add_transactions({
date => $transaction->{date},
- payee => 'Payee TODO',
+ payee => $payee->{name},
+ memo => $memo,
postings => \@postings,
});
}
tags => 1,
commodities => 1,
opening_date => '',
- default_account => 'Expenses:No Category',
rename_accounts => {},
exclude_accounts => [],
);
'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)]);
$opts{input} = shift @args if !$opts{input};
- if (!$opts{input}) {
- print STDERR "Input file is required.\n";
- exit(1);
- }
return \%opts;
}