1 package App
::HomeBank2Ledger
::Formatter
::Beancount
;
2 # ABSTRACT: Beancount formatter
8 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
10 use parent
'App::HomeBank2Ledger::Formatter';
12 our $VERSION = '0.002'; # VERSION
14 my %STATUS_SYMBOLS = (
18 my $UNKNOWN_DATE = '0001-01-01';
20 sub _croak
{ require Carp
; Carp
::croak
(@_) }
27 $self->_format_header,
28 $self->_format_accounts($ledger),
29 $self->_format_commodities($ledger),
30 # $self->_format_payees,
31 # $self->_format_tags,
32 $self->_format_transactions($ledger),
35 return join($/, map { rtrim
($_) } @out);
43 if (my $name = $self->name) {
44 push @out, "; Name: $name";
47 my $file = $self->file;
48 push @out, "; Converted from ${file} using homebank2ledger ${VERSION}";
55 sub _format_accounts
{
61 for my $account (sort @{$ledger->accounts}) {
62 my $oldest_transaction = $self->_find_oldest_transaction_by_account($account, $ledger);
63 my $account_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
64 $account = $self->_format_account($account);
66 push @out, "${account_date} open ${account}";
73 sub _format_commodities
{
79 for my $commodity (@{$ledger->commodities}) {
80 my $oldest_transaction = $self->_find_oldest_transaction_by_commodity($commodity, $ledger);
81 my $commodity_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
83 push @out, "${commodity_date} commodity $commodity->{iso}";
84 push @out, ' name: '.$self->_format_string($commodity->{name
}) if $commodity->{name
};
92 sub _format_transactions
{
98 for my $transaction (@{$ledger->transactions}) {
99 push @out, $self->_format_transaction($transaction);
105 sub _format_transaction
{
107 my $transaction = shift;
109 my $account_width = $self->account_width;
111 my $date = $transaction->{date
};
112 my $status = $transaction->{status
};
113 my $payee = $transaction->{payee
} || '';
114 my $memo = $transaction->{memo
} || '';
115 my @postings = @{$transaction->{postings
}};
119 # figure out the Ledger transaction status
120 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
121 if (!$status_symbol) {
122 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
123 if (keys(%posting_statuses) == 1) {
124 my ($status) = keys %posting_statuses;
125 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
129 push @out, sprintf('%s%s%s%s', $date,
130 $status_symbol && ' '.$status_symbol || ' *', # status (or "txn") is required
131 ($payee || $memo) && ' '.$self->_format_string($payee),
132 $memo && ' '.$self->_format_string($memo),
135 if (my %tags = map { $_ => 1 } map { @{$_->{tags
} || []} } @postings) {
136 my @tags = map { "#$_" } keys %tags;
137 $out[-1] .= ' '.join(' ', @tags);
140 for my $posting (@postings) {
143 my $posting_status_symbol = '';
144 if (!$status_symbol) {
145 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
148 my $account = $self->_format_account($posting->{account
});
150 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
151 push @line, sprintf("\%-${account_width}s", $account);
153 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
155 push @out, join('', @line);
163 sub _format_account
{
166 $account =~ s/[^A-Za-z0-9:]+/-/g;
167 $account =~ s/-+/-/g;
168 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
182 my $commodity = shift or _croak
'Must provide a valid currency';
184 my $format = "\% .$commodity->{frac}f";
185 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
187 # beancount doesn't support different notations
188 my $num = join('.', commify
($whole), $fraction);
190 $num = "$num $commodity->{iso}";
195 sub _find_oldest_transaction_by_account
{
200 $account = $self->_format_account($account);
202 my $oldest = $self->{oldest_transaction_by_account
};
205 for my $transaction (@{$ledger->transactions}) {
206 for my $posting (@{$transaction->{postings
}}) {
207 my $account = $self->_format_account($posting->{account
});
209 if ($transaction->{date
} lt ($oldest->{$account}{date
} || '9999-99-99')) {
210 $oldest->{$account} = $transaction;
215 $self->{oldest_transaction_by_account
} = $oldest;
218 return $oldest->{$account};
221 sub _find_oldest_transaction_by_commodity
{
223 my $commodity = shift;
226 my $oldest = $self->{oldest_transaction_by_commodity
};
229 for my $transaction (@{$ledger->transactions}) {
230 for my $posting (@{$transaction->{postings
}}) {
231 my $symbol = $posting->{commodity
}{symbol
};
234 if ($transaction->{date
} lt ($oldest->{$symbol}{date
} || '9999-99-99')) {
235 $oldest->{$symbol} = $transaction;
240 $self->{oldest_transaction_by_commodity
} = $oldest;
243 return $oldest->{$commodity->{symbol
}};
256 App::HomeBank2Ledger::Formatter::Beancount - Beancount formatter
264 This is a formatter for L<Beancount|http://furius.ca/beancount/>.
268 L<App::HomeBank2Ledger::Formatter>
272 Please report any bugs or feature requests on the bugtracker website
273 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
275 When submitting a bug or request, please include a test-file or a
276 patch to an existing test-file that illustrates the bug or desired
281 Charles McGarvey <chazmcgarvey@brokenzipper.com>
283 =head1 COPYRIGHT AND LICENSE
285 This software is Copyright (c) 2019 by Charles McGarvey.
287 This is free software, licensed under:
289 The MIT (X11) License