=head1 DESCRIPTION
-This is a formatter for L<Beancount|http://furius.ca/beancount/>.
+This is a formatter for L<Beancount|https://beancount.github.io/docs/index.html>.
=head1 SEE ALSO
=cut
+use v5.10.1; # defined-or
use warnings;
use strict;
-use App::HomeBank2Ledger::Util qw(commify);
+use App::HomeBank2Ledger::Util qw(commify rtrim);
+use Scalar::Util qw(looks_like_number);
use parent 'App::HomeBank2Ledger::Formatter';
cleared => '*',
pending => '!',
);
+my $UNKNOWN_DATE = '0001-01-01';
+
+sub _croak { require Carp; Carp::croak(@_) }
sub format {
my $self = shift;
my $ledger = shift;
my @out = (
- $self->_format_header,
- $self->_format_accounts($ledger),
- $self->_format_commodities($ledger),
- # $self->_format_payees,
- # $self->_format_tags,
- $self->_format_transactions($ledger),
+ $self->format_header,
+ $self->format_accounts($ledger),
+ $self->format_commodities($ledger),
+ # $self->format_payees,
+ # $self->format_tags,
+ $self->format_transactions($ledger),
);
- return join($/, @out);
+ return join($/, map { rtrim($_) } @out);
}
-sub _format_header {
+=method format_header
+
+ @lines = $formatter->format_header;
+
+Get formatted header. For example,
+
+ ; Name: My Finances
+ ; File: path/to/finances.xhb
+
+=cut
+
+sub format_header {
my $self = shift;
my @out;
- my $file = $self->file;
- push @out, "; Converted from $file using homebank2ledger ${VERSION}";
-
if (my $name = $self->name) {
push @out, "; Name: $name";
}
+ if (my $file = $self->file) {
+ push @out, "; File: $file";
+ }
push @out, '';
return @out;
}
-sub _format_accounts {
+=method format_accounts
+
+ @lines = $formatter->format_accounts($ledger);
+
+Get formatted accounts. For example,
+
+ 2003-02-14 open Assets:Bank:Credit-Union:Savings
+ 2003-02-14 open Assets:Bank:Credit-Union:Checking
+ ...
+
+=cut
+
+sub format_accounts {
my $self = shift;
my $ledger = shift;
my @out;
for my $account (sort @{$ledger->accounts}) {
- $account = $self->_munge_account($account);
- push @out, "1970-01-01 open $account"; # TODO pick better date?
+ my $oldest_transaction = $self->_find_oldest_transaction_by_account($account, $ledger);
+ my $account_date = $oldest_transaction->{date} || $UNKNOWN_DATE;
+ $account = $self->_format_account($account);
+
+ push @out, "${account_date} open ${account}";
}
push @out, '';
return @out;
}
-sub _format_commodities {
+=method format_commodities
+
+ @lines = $formatter->format_commodities($ledger);
+
+Get formattted commodities. For example,
+
+ 2003-02-14 commodity USD
+ name: "US Dollar"
+ ...
+
+=cut
+
+sub format_commodities {
my $self = shift;
my $ledger = shift;
my @out;
for my $commodity (@{$ledger->commodities}) {
- push @out, "1970-01-01 commodity $commodity->{iso}"; # TODO
- push @out, " name: \"$commodity->{name}\"" if $commodity->{name};
+ my $oldest_transaction = $self->_find_oldest_transaction_by_commodity($commodity, $ledger);
+ my $commodity_date = $oldest_transaction->{date} || $UNKNOWN_DATE;
+
+ push @out, "${commodity_date} commodity $commodity->{iso}";
+ push @out, ' name: '.$self->_format_string($commodity->{name}) if $commodity->{name};
}
push @out, '';
return @out;
}
-sub _format_transactions {
+=method format_transactions
+
+ @lines = $formatter->format_transactions($ledger);
+
+Get formatted transactions. For example,
+
+ 2003-02-14 * "Opening Balance"
+ Assets:Bank:Credit-Union:Savings 458.21 USD
+ Assets:Bank:Credit-Union:Checking 194.17 USD
+ Equity:Opening-Balances
+
+ ...
+
+=cut
+
+sub format_transactions {
my $self = shift;
my $ledger = shift;
my $date = $transaction->{date};
my $status = $transaction->{status};
- my $payee = $transaction->{payee} || 'No Payee TODO';
- my $memo = $transaction->{memo} || '';
+ my $payee = $transaction->{payee} || '';
+ my $memo = $transaction->{note} // $transaction->{memo} // '';
my @postings = @{$transaction->{postings}};
my @out;
if (keys(%posting_statuses) == 1) {
my ($status) = keys %posting_statuses;
$status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
- $status_symbol .= ' ' if $status_symbol;
}
}
- my $symbol = $status_symbol ? "${status_symbol} " : '';
- push @out, "${date} ${symbol}\"${payee}\" \"$memo\""; # TODO handle proper quoting
- $out[-1] =~ s/\h+$//;
+ push @out, sprintf('%s%s%s%s', $date,
+ $status_symbol && ' '.$status_symbol || ' *', # status (or "txn") is required
+ ($payee || $memo) && ' '.$self->_format_string($payee),
+ $memo && ' '.$self->_format_string($memo),
+ );
if (my %tags = map { $_ => 1 } map { @{$_->{tags} || []} } @postings) {
my @tags = map { "#$_" } keys %tags;
- $out[-1] .= " ".join(' ', @tags);
+ $out[-1] .= ' '.join(' ', @tags);
+ }
+
+ my $metadata = $transaction->{metadata} || {};
+ for my $key (sort keys %$metadata) {
+ my $value = looks_like_number($metadata->{$key}) ? $metadata->{$key}
+ : $self->_format_string($metadata->{$key});
+ push @out, " ; ${key}: ${value}";
}
for my $posting (@postings) {
$posting_status_symbol = $STATUS_SYMBOLS{$posting->{status} || ''} || '';
}
- my $account = $self->_munge_account($posting->{account});
+ my $account = $self->_format_account($posting->{account});
push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
push @line, sprintf("\%-${account_width}s", $account);
push @line, ' ';
- push @line, $self->_format_amount($posting->{amount}, $posting->{commodity}) if defined $posting->{amount};
+ if (defined $posting->{amount}) {
+ push @line, $self->_format_amount($posting->{amount}, $posting->{commodity});
+ my $lot = $posting->{lot} || {};
+ my $lot_price = $lot->{price} // $posting->{lot_price};
+ my $lot_date = $lot->{date} // $posting->{lot_date};
+ my $lot_ref = $lot->{ref} // $posting->{lot_ref};
+ if ($lot_price || $lot_date || $lot_ref) {
+ push @line, ' {',
+ join(', ',
+ $lot_price ? $self->_format_amount($lot_price->{amount}, $lot_price->{commodity}) : (),
+ $lot_date ? $lot_date : (),
+ $lot_ref ? $self->_format_string($lot_ref) : (),
+ ),
+ '}';
+ }
+ if (my $cost = $posting->{total_cost} // $posting->{cost}) {
+ my $is_total = defined $posting->{total_cost};
+ my $cost_symbol = $is_total ? '@@' : '@';
+ push @line, ' ', $cost_symbol, ' ',
+ $self->_format_amount($cost->{amount}, $cost->{commodity});
+ }
+ }
push @out, join('', @line);
- $out[-1] =~ s/\h+$//;
- # if (my $payee = $posting->{payee}) {
- # push @out, " ; Payee: $payee";
- # }
+ my $metadata = $posting->{metadata} || {};
+ for my $key (sort keys %$metadata) {
+ my $value = looks_like_number($metadata->{$key}) ? $metadata->{$key}
+ : $self->_format_string($metadata->{$key});
+ push @out, " ; ${key}: ${value}";
+ }
}
push @out, '';
return @out;
}
+sub _format_account {
+ my $self = shift;
+ my $account = shift;
+ $account =~ s/[^A-Za-z0-9:]+/-/g;
+ $account =~ s/-+/-/g;
+ $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
+ return $account;
+}
+
+sub _format_string {
+ my $self = shift;
+ my $str = shift;
+ $str =~ s/"/\\"/g;
+ return "\"$str\"";
+}
+
sub _format_amount {
my $self = shift;
my $amount = shift;
- my $commodity = shift;
-
- # _croak 'Must provide a valid currency' if !$commodity;
+ my $commodity = shift or _croak 'Must provide a valid currency';
my $format = "\% .$commodity->{frac}f";
my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
+ $fraction ||= 0;
# beancount doesn't support different notations
- my $num = join('.', commify($whole), $fraction);
+ my $num = commify($whole);
+ if ($commodity->{frac}) {
+ $num .= ".$fraction";
+ }
$num = "$num $commodity->{iso}";
return $num;
}
-sub _munge_account {
- my $self = shift;
+sub _find_oldest_transaction_by_account {
+ my $self = shift;
my $account = shift;
- $account =~ s/[^A-Za-z0-9:]+/-/g;
- $account =~ s/-+/-/g;
- $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
- return $account;
+ my $ledger = shift;
+
+ $account = $self->_format_account($account);
+
+ my $oldest = $self->{oldest_transaction_by_account};
+ if (!$oldest) {
+ # build index
+ for my $transaction (@{$ledger->transactions}) {
+ for my $posting (@{$transaction->{postings}}) {
+ my $account = $self->_format_account($posting->{account});
+
+ if ($transaction->{date} lt ($oldest->{$account}{date} || '9999-99-99')) {
+ $oldest->{$account} = $transaction;
+ }
+ }
+ }
+
+ $self->{oldest_transaction_by_account} = $oldest;
+ }
+
+ return $oldest->{$account};
+}
+
+sub _find_oldest_transaction_by_commodity {
+ my $self = shift;
+ my $commodity = shift;
+ my $ledger = shift;
+
+ my $oldest = $self->{oldest_transaction_by_commodity};
+ if (!$oldest) {
+ # build index
+ for my $transaction (@{$ledger->transactions}) {
+ for my $posting (@{$transaction->{postings}}) {
+ my $symbol = $posting->{commodity}{symbol};
+ next if !$symbol;
+
+ if ($transaction->{date} lt ($oldest->{$symbol}{date} || '9999-99-99')) {
+ $oldest->{$symbol} = $transaction;
+ }
+ }
+ }
+
+ $self->{oldest_transaction_by_commodity} = $oldest;
+ }
+
+ return $oldest->{$commodity->{symbol}};
}
1;