1 package App
::HomeBank2Ledger
::Formatter
::Beancount
;
2 # ABSTRACT: Beancount formatter
6 This is a formatter for L<Beancount|https://beancount.github.io/docs/index.html>.
10 L<App::HomeBank2Ledger::Formatter>
14 use v5
.10
.1; # defined-or
18 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
19 use Scalar
::Util
qw(looks_like_number);
21 use parent
'App::HomeBank2Ledger::Formatter';
23 our $VERSION = '9999.999'; # VERSION
25 my %STATUS_SYMBOLS = (
29 my $UNKNOWN_DATE = '0001-01-01';
31 sub _croak
{ require Carp
; Carp
::croak
(@_) }
39 $self->format_accounts($ledger),
40 $self->format_commodities($ledger),
41 # $self->format_payees,
43 $self->format_transactions($ledger),
46 return join($/, map { rtrim
($_) } @out);
51 @lines = $formatter->format_header;
53 Get formatted header
. For example
,
56 ; File
: path
/to/finances
.xhb
65 if (my $name = $self->name) {
66 push @out, "; Name: $name";
68 if (my $file = $self->file) {
69 push @out, "; File: $file";
77 =method format_accounts
79 @lines = $formatter->format_accounts($ledger);
81 Get formatted accounts
. For example
,
83 2003-02-14 open Assets
:Bank
:Credit-Union
:Savings
84 2003-02-14 open Assets
:Bank
:Credit-Union
:Checking
95 for my $account (sort @{$ledger->accounts}) {
96 my $oldest_transaction = $self->_find_oldest_transaction_by_account($account, $ledger);
97 my $account_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
98 $account = $self->_format_account($account);
100 push @out, "${account_date} open ${account}";
107 =method format_commodities
109 @lines = $formatter->format_commodities($ledger);
111 Get formattted commodities
. For example
,
113 2003-02-14 commodity USD
119 sub format_commodities
{
125 for my $commodity (@{$ledger->commodities}) {
126 my $oldest_transaction = $self->_find_oldest_transaction_by_commodity($commodity, $ledger);
127 my $commodity_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
129 push @out, "${commodity_date} commodity $commodity->{iso}";
130 push @out, ' name: '.$self->_format_string($commodity->{name
}) if $commodity->{name
};
138 =method format_transactions
140 @lines = $formatter->format_transactions($ledger);
142 Get formatted transactions
. For example
,
144 2003-02-14 * "Opening Balance"
145 Assets
:Bank
:Credit-Union
:Savings
458.21 USD
146 Assets
:Bank
:Credit-Union
:Checking
194.17 USD
147 Equity
:Opening-Balances
153 sub format_transactions
{
159 for my $transaction (@{$ledger->transactions}) {
160 push @out, $self->_format_transaction($transaction);
166 sub _format_transaction
{
168 my $transaction = shift;
170 my $account_width = $self->account_width;
172 my $date = $transaction->{date
};
173 my $status = $transaction->{status
};
174 my $payee = $transaction->{payee
} || '';
175 my $memo = $transaction->{note
} // $transaction->{memo
} // '';
176 my @postings = @{$transaction->{postings
}};
180 # figure out the Ledger transaction status
181 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
182 if (!$status_symbol) {
183 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
184 if (keys(%posting_statuses) == 1) {
185 my ($status) = keys %posting_statuses;
186 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
190 push @out, sprintf('%s%s%s%s', $date,
191 $status_symbol && ' '.$status_symbol || ' *', # status (or "txn") is required
192 ($payee || $memo) && ' '.$self->_format_string($payee),
193 $memo && ' '.$self->_format_string($memo),
196 if (my %tags = map { $_ => 1 } map { @{$_->{tags
} || []} } @postings) {
197 my @tags = map { "#$_" } keys %tags;
198 $out[-1] .= ' '.join(' ', @tags);
201 my $metadata = $transaction->{metadata
} || {};
202 for my $key (sort keys %$metadata) {
203 my $value = looks_like_number
($metadata->{$key}) ? $metadata->{$key}
204 : $self->_format_string($metadata->{$key});
205 push @out, " ; ${key}: ${value}";
208 for my $posting (@postings) {
211 my $posting_status_symbol = '';
212 if (!$status_symbol) {
213 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
216 my $account = $self->_format_account($posting->{account
});
218 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
219 push @line, sprintf("\%-${account_width}s", $account);
221 if (defined $posting->{amount
}) {
222 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
223 my $lot = $posting->{lot
} || {};
224 my $lot_price = $lot->{price
} // $posting->{lot_price
};
225 my $lot_date = $lot->{date
} // $posting->{lot_date
};
226 my $lot_ref = $lot->{ref} // $posting->{lot_ref
};
227 if ($lot_price || $lot_date || $lot_ref) {
230 $lot_price ? $self->_format_amount($lot_price->{amount
}, $lot_price->{commodity
}) : (),
231 $lot_date ? $lot_date : (),
232 $lot_ref ? $self->_format_string($lot_ref) : (),
236 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
237 my $is_total = defined $posting->{total_cost
};
238 my $cost_symbol = $is_total ? '@@' : '@';
239 push @line, ' ', $cost_symbol, ' ',
240 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
244 push @out, join('', @line);
246 my $metadata = $posting->{metadata
} || {};
247 for my $key (sort keys %$metadata) {
248 my $value = looks_like_number
($metadata->{$key}) ? $metadata->{$key}
249 : $self->_format_string($metadata->{$key});
250 push @out, " ; ${key}: ${value}";
259 sub _format_account
{
262 $account =~ s/[^A-Za-z0-9:]+/-/g;
263 $account =~ s/-+/-/g;
264 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
278 my $commodity = shift or _croak
'Must provide a valid currency';
280 my $format = "\% .$commodity->{frac}f";
281 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
284 # beancount doesn't support different notations
285 my $num = commify
($whole);
286 if ($commodity->{frac
}) {
287 $num .= ".$fraction";
290 $num = "$num $commodity->{iso}";
295 sub _find_oldest_transaction_by_account
{
300 $account = $self->_format_account($account);
302 my $oldest = $self->{oldest_transaction_by_account
};
305 for my $transaction (@{$ledger->transactions}) {
306 for my $posting (@{$transaction->{postings
}}) {
307 my $account = $self->_format_account($posting->{account
});
309 if ($transaction->{date
} lt ($oldest->{$account}{date
} || '9999-99-99')) {
310 $oldest->{$account} = $transaction;
315 $self->{oldest_transaction_by_account
} = $oldest;
318 return $oldest->{$account};
321 sub _find_oldest_transaction_by_commodity
{
323 my $commodity = shift;
326 my $oldest = $self->{oldest_transaction_by_commodity
};
329 for my $transaction (@{$ledger->transactions}) {
330 for my $posting (@{$transaction->{postings
}}) {
331 my $symbol = $posting->{commodity
}{symbol
};
334 if ($transaction->{date
} lt ($oldest->{$symbol}{date
} || '9999-99-99')) {
335 $oldest->{$symbol} = $transaction;
340 $self->{oldest_transaction_by_commodity
} = $oldest;
343 return $oldest->{$commodity->{symbol
}};