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.009'; # 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->{note
} // $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 = $posting->{lot
} || {};
169 my $lot_price = $lot->{price
} // $posting->{lot_price
};
170 my $lot_date = $lot->{date
} // $posting->{lot_date
};
171 my $lot_ref = $lot->{ref} // $posting->{lot_ref
};
172 if ($lot_price || $lot_date || $lot_ref) {
175 $lot_price ? $self->_format_amount($lot_price->{amount
}, $lot_price->{commodity
}) : (),
176 $lot_date ? $lot_date : (),
177 $lot_ref ? $self->_format_string($lot_ref) : (),
181 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
182 my $is_total = defined $posting->{total_cost
};
183 my $cost_symbol = $is_total ? '@@' : '@';
184 push @line, ' ', $cost_symbol, ' ',
185 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
189 push @out, join('', @line);
191 my $metadata = $posting->{metadata
} || {};
192 for my $key (sort keys %$metadata) {
193 my $value = looks_like_number
($metadata->{$key}) ? $metadata->{$key}
194 : $self->_format_string($metadata->{$key});
195 push @out, " ; ${key}: ${value}";
204 sub _format_account
{
207 $account =~ s/[^A-Za-z0-9:]+/-/g;
208 $account =~ s/-+/-/g;
209 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
223 my $commodity = shift or _croak
'Must provide a valid currency';
225 my $format = "\% .$commodity->{frac}f";
226 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
229 # beancount doesn't support different notations
230 my $num = commify
($whole);
231 if ($commodity->{frac
}) {
232 $num .= ".$fraction";
235 $num = "$num $commodity->{iso}";
240 sub _find_oldest_transaction_by_account
{
245 $account = $self->_format_account($account);
247 my $oldest = $self->{oldest_transaction_by_account
};
250 for my $transaction (@{$ledger->transactions}) {
251 for my $posting (@{$transaction->{postings
}}) {
252 my $account = $self->_format_account($posting->{account
});
254 if ($transaction->{date
} lt ($oldest->{$account}{date
} || '9999-99-99')) {
255 $oldest->{$account} = $transaction;
260 $self->{oldest_transaction_by_account
} = $oldest;
263 return $oldest->{$account};
266 sub _find_oldest_transaction_by_commodity
{
268 my $commodity = shift;
271 my $oldest = $self->{oldest_transaction_by_commodity
};
274 for my $transaction (@{$ledger->transactions}) {
275 for my $posting (@{$transaction->{postings
}}) {
276 my $symbol = $posting->{commodity
}{symbol
};
279 if ($transaction->{date
} lt ($oldest->{$symbol}{date
} || '9999-99-99')) {
280 $oldest->{$symbol} = $transaction;
285 $self->{oldest_transaction_by_commodity
} = $oldest;
288 return $oldest->{$commodity->{symbol
}};
301 App::HomeBank2Ledger::Formatter::Beancount - Beancount formatter
309 This is a formatter for L<Beancount|https://beancount.github.io/docs/index.html>.
315 @lines = $formatter->format_header;
317 Get formatted header. For example,
320 ; File: path/to/finances.xhb
322 =head2 format_accounts
324 @lines = $formatter->format_accounts($ledger);
326 Get formatted accounts. For example,
328 2003-02-14 open Assets:Bank:Credit-Union:Savings
329 2003-02-14 open Assets:Bank:Credit-Union:Checking
332 =head2 format_commodities
334 @lines = $formatter->format_commodities($ledger);
336 Get formattted commodities. For example,
338 2003-02-14 commodity USD
342 =head2 format_transactions
344 @lines = $formatter->format_transactions($ledger);
346 Get formatted transactions. For example,
348 2003-02-14 * "Opening Balance"
349 Assets:Bank:Credit-Union:Savings 458.21 USD
350 Assets:Bank:Credit-Union:Checking 194.17 USD
351 Equity:Opening-Balances
357 L<App::HomeBank2Ledger::Formatter>
361 Please report any bugs or feature requests on the bugtracker website
362 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
364 When submitting a bug or request, please include a test-file or a
365 patch to an existing test-file that illustrates the bug or desired
370 Charles McGarvey <chazmcgarvey@brokenzipper.com>
372 =head1 COPYRIGHT AND LICENSE
374 This software is Copyright (c) 2019 by Charles McGarvey.
376 This is free software, licensed under:
378 The MIT (X11) License