1 package App
::HomeBank2Ledger
::Formatter
::Beancount
;
2 # ABSTRACT: Beancount formatter
5 use v5
.10
.1; # defined-or
9 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
10 use Scalar
::Util
qw(looks_like_number);
12 use parent
'App::HomeBank2Ledger::Formatter';
14 our $VERSION = '0.007'; # VERSION
16 my %STATUS_SYMBOLS = (
20 my $UNKNOWN_DATE = '0001-01-01';
22 sub _croak
{ require Carp
; Carp
::croak
(@_) }
30 $self->format_accounts($ledger),
31 $self->format_commodities($ledger),
32 # $self->format_payees,
34 $self->format_transactions($ledger),
37 return join($/, map { rtrim
($_) } @out);
46 if (my $name = $self->name) {
47 push @out, "; Name: $name";
49 if (my $file = $self->file) {
50 push @out, "; File: $file";
65 for my $account (sort @{$ledger->accounts}) {
66 my $oldest_transaction = $self->_find_oldest_transaction_by_account($account, $ledger);
67 my $account_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
68 $account = $self->_format_account($account);
70 push @out, "${account_date} open ${account}";
78 sub format_commodities
{
84 for my $commodity (@{$ledger->commodities}) {
85 my $oldest_transaction = $self->_find_oldest_transaction_by_commodity($commodity, $ledger);
86 my $commodity_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
88 push @out, "${commodity_date} commodity $commodity->{iso}";
89 push @out, ' name: '.$self->_format_string($commodity->{name
}) if $commodity->{name
};
98 sub format_transactions
{
104 for my $transaction (@{$ledger->transactions}) {
105 push @out, $self->_format_transaction($transaction);
111 sub _format_transaction
{
113 my $transaction = shift;
115 my $account_width = $self->account_width;
117 my $date = $transaction->{date
};
118 my $status = $transaction->{status
};
119 my $payee = $transaction->{payee
} || '';
120 my $memo = $transaction->{memo
} || '';
121 my @postings = @{$transaction->{postings
}};
125 # figure out the Ledger transaction status
126 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
127 if (!$status_symbol) {
128 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
129 if (keys(%posting_statuses) == 1) {
130 my ($status) = keys %posting_statuses;
131 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
135 push @out, sprintf('%s%s%s%s', $date,
136 $status_symbol && ' '.$status_symbol || ' *', # status (or "txn") is required
137 ($payee || $memo) && ' '.$self->_format_string($payee),
138 $memo && ' '.$self->_format_string($memo),
141 if (my %tags = map { $_ => 1 } map { @{$_->{tags
} || []} } @postings) {
142 my @tags = map { "#$_" } keys %tags;
143 $out[-1] .= ' '.join(' ', @tags);
146 my $metadata = $transaction->{metadata
} || {};
147 for my $key (sort keys %$metadata) {
148 my $value = looks_like_number
($metadata->{$key}) ? $metadata->{$key}
149 : $self->_format_string($metadata->{$key});
150 push @out, " ; ${key}: ${value}";
153 for my $posting (@postings) {
156 my $posting_status_symbol = '';
157 if (!$status_symbol) {
158 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
161 my $account = $self->_format_account($posting->{account
});
163 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
164 push @line, sprintf("\%-${account_width}s", $account);
166 if (defined $posting->{amount
}) {
167 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
168 my $lot_price = $posting->{lot_price
};
169 my $lot_date = $posting->{lot_date
};
170 my $lot_ref = $posting->{lot_ref
};
171 if ($lot_price || $lot_date || $lot_ref) {
174 $lot_price ? $self->_format_amount($lot_price->{amount
}, $lot_price->{commodity
}) : (),
175 $lot_date ? $lot_date : (),
176 $lot_ref ? $self->_format_string($lot_ref) : (),
180 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
181 my $is_total = defined $posting->{total_cost
};
182 my $cost_symbol = $is_total ? '@@' : '@';
183 push @line, ' ', $cost_symbol, ' ',
184 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
188 push @out, join('', @line);
190 my $metadata = $posting->{metadata
} || {};
191 for my $key (sort keys %$metadata) {
192 my $value = looks_like_number
($metadata->{$key}) ? $metadata->{$key}
193 : $self->_format_string($metadata->{$key});
194 push @out, " ; ${key}: ${value}";
203 sub _format_account
{
206 $account =~ s/[^A-Za-z0-9:]+/-/g;
207 $account =~ s/-+/-/g;
208 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
222 my $commodity = shift or _croak
'Must provide a valid currency';
224 my $format = "\% .$commodity->{frac}f";
225 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
228 # beancount doesn't support different notations
229 my $num = commify
($whole);
230 if ($commodity->{frac
}) {
231 $num .= ".$fraction";
234 $num = "$num $commodity->{iso}";
239 sub _find_oldest_transaction_by_account
{
244 $account = $self->_format_account($account);
246 my $oldest = $self->{oldest_transaction_by_account
};
249 for my $transaction (@{$ledger->transactions}) {
250 for my $posting (@{$transaction->{postings
}}) {
251 my $account = $self->_format_account($posting->{account
});
253 if ($transaction->{date
} lt ($oldest->{$account}{date
} || '9999-99-99')) {
254 $oldest->{$account} = $transaction;
259 $self->{oldest_transaction_by_account
} = $oldest;
262 return $oldest->{$account};
265 sub _find_oldest_transaction_by_commodity
{
267 my $commodity = shift;
270 my $oldest = $self->{oldest_transaction_by_commodity
};
273 for my $transaction (@{$ledger->transactions}) {
274 for my $posting (@{$transaction->{postings
}}) {
275 my $symbol = $posting->{commodity
}{symbol
};
278 if ($transaction->{date
} lt ($oldest->{$symbol}{date
} || '9999-99-99')) {
279 $oldest->{$symbol} = $transaction;
284 $self->{oldest_transaction_by_commodity
} = $oldest;
287 return $oldest->{$commodity->{symbol
}};
300 App::HomeBank2Ledger::Formatter::Beancount - Beancount formatter
308 This is a formatter for L<Beancount|http://furius.ca/beancount/>.
314 @lines = $formatter->format_header;
316 Get formatted header. For example,
319 ; File: path/to/finances.xhb
321 =head2 format_accounts
323 @lines = $formatter->format_accounts($ledger);
325 Get formatted accounts. For example,
327 2003-02-14 open Assets:Bank:Credit-Union:Savings
328 2003-02-14 open Assets:Bank:Credit-Union:Checking
331 =head2 format_commodities
333 @lines = $formatter->format_commodities($ledger);
335 Get formattted commodities. For example,
337 2003-02-14 commodity USD
341 =head2 format_transactions
343 @lines = $formatter->format_transactions($ledger);
345 Get formatted transactions. For example,
347 2003-02-14 * "Opening Balance"
348 Assets:Bank:Credit-Union:Savings 458.21 USD
349 Assets:Bank:Credit-Union:Checking 194.17 USD
350 Equity:Opening-Balances
356 L<App::HomeBank2Ledger::Formatter>
360 Please report any bugs or feature requests on the bugtracker website
361 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
363 When submitting a bug or request, please include a test-file or a
364 patch to an existing test-file that illustrates the bug or desired
369 Charles McGarvey <chazmcgarvey@brokenzipper.com>
371 =head1 COPYRIGHT AND LICENSE
373 This software is Copyright (c) 2019 by Charles McGarvey.
375 This is free software, licensed under:
377 The MIT (X11) License