1 package App
::HomeBank2Ledger
::Formatter
::Beancount
;
2 # ABSTRACT: Beancount formatter
6 This is a formatter for L<Beancount|http://furius.ca/beancount/>.
10 L<App::HomeBank2Ledger::Formatter>
17 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
19 use parent
'App::HomeBank2Ledger::Formatter';
21 our $VERSION = '9999.999'; # VERSION
23 my %STATUS_SYMBOLS = (
27 my $UNKNOWN_DATE = '0001-01-01';
29 sub _croak
{ require Carp
; Carp
::croak
(@_) }
36 $self->_format_header,
37 $self->_format_accounts($ledger),
38 $self->_format_commodities($ledger),
39 # $self->_format_payees,
40 # $self->_format_tags,
41 $self->_format_transactions($ledger),
44 return join($/, map { rtrim
($_) } @out);
52 if (my $name = $self->name) {
53 push @out, "; Name: $name";
56 my $file = $self->file;
57 push @out, "; Converted from ${file} using homebank2ledger ${VERSION}";
64 sub _format_accounts
{
70 for my $account (sort @{$ledger->accounts}) {
71 my $oldest_transaction = $self->_find_oldest_transaction_by_account($account, $ledger);
72 my $account_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
73 $account = $self->_format_account($account);
75 push @out, "${account_date} open ${account}";
82 sub _format_commodities
{
88 for my $commodity (@{$ledger->commodities}) {
89 my $oldest_transaction = $self->_find_oldest_transaction_by_commodity($commodity, $ledger);
90 my $commodity_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
92 push @out, "${commodity_date} commodity $commodity->{iso}";
93 push @out, ' name: '.$self->_format_string($commodity->{name
}) if $commodity->{name
};
101 sub _format_transactions
{
107 for my $transaction (@{$ledger->transactions}) {
108 push @out, $self->_format_transaction($transaction);
114 sub _format_transaction
{
116 my $transaction = shift;
118 my $account_width = $self->account_width;
120 my $date = $transaction->{date
};
121 my $status = $transaction->{status
};
122 my $payee = $transaction->{payee
} || '';
123 my $memo = $transaction->{memo
} || '';
124 my @postings = @{$transaction->{postings
}};
128 # figure out the Ledger transaction status
129 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
130 if (!$status_symbol) {
131 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
132 if (keys(%posting_statuses) == 1) {
133 my ($status) = keys %posting_statuses;
134 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
138 push @out, sprintf('%s%s%s%s', $date,
139 $status_symbol && ' '.$status_symbol || ' *', # status (or "txn") is required
140 ($payee || $memo) && ' '.$self->_format_string($payee),
141 $memo && ' '.$self->_format_string($memo),
144 if (my %tags = map { $_ => 1 } map { @{$_->{tags
} || []} } @postings) {
145 my @tags = map { "#$_" } keys %tags;
146 $out[-1] .= ' '.join(' ', @tags);
149 for my $posting (@postings) {
152 my $posting_status_symbol = '';
153 if (!$status_symbol) {
154 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
157 my $account = $self->_format_account($posting->{account
});
159 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
160 push @line, sprintf("\%-${account_width}s", $account);
162 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
164 push @out, join('', @line);
172 sub _format_account
{
175 $account =~ s/[^A-Za-z0-9:]+/-/g;
176 $account =~ s/-+/-/g;
177 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
191 my $commodity = shift or _croak
'Must provide a valid currency';
193 my $format = "\% .$commodity->{frac}f";
194 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
196 # beancount doesn't support different notations
197 my $num = join('.', commify
($whole), $fraction);
199 $num = "$num $commodity->{iso}";
204 sub _find_oldest_transaction_by_account
{
209 $account = $self->_format_account($account);
211 my $oldest = $self->{oldest_transaction_by_account
};
214 for my $transaction (@{$ledger->transactions}) {
215 for my $posting (@{$transaction->{postings
}}) {
216 my $account = $self->_format_account($posting->{account
});
218 if ($transaction->{date
} lt ($oldest->{$account}{date
} || '9999-99-99')) {
219 $oldest->{$account} = $transaction;
224 $self->{oldest_transaction_by_account
} = $oldest;
227 return $oldest->{$account};
230 sub _find_oldest_transaction_by_commodity
{
232 my $commodity = shift;
235 my $oldest = $self->{oldest_transaction_by_commodity
};
238 for my $transaction (@{$ledger->transactions}) {
239 for my $posting (@{$transaction->{postings
}}) {
240 my $symbol = $posting->{commodity
}{symbol
};
243 if ($transaction->{date
} lt ($oldest->{$symbol}{date
} || '9999-99-99')) {
244 $oldest->{$symbol} = $transaction;
249 $self->{oldest_transaction_by_commodity
} = $oldest;
252 return $oldest->{$commodity->{symbol
}};