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);
11 use parent
'App::HomeBank2Ledger::Formatter';
13 our $VERSION = '0.005'; # VERSION
15 my %STATUS_SYMBOLS = (
19 my $UNKNOWN_DATE = '0001-01-01';
21 sub _croak
{ require Carp
; Carp
::croak
(@_) }
29 $self->format_accounts($ledger),
30 $self->format_commodities($ledger),
31 # $self->format_payees,
33 $self->format_transactions($ledger),
36 return join($/, map { rtrim
($_) } @out);
45 if (my $name = $self->name) {
46 push @out, "; Name: $name";
48 if (my $file = $self->file) {
49 push @out, "; File: $file";
64 for my $account (sort @{$ledger->accounts}) {
65 my $oldest_transaction = $self->_find_oldest_transaction_by_account($account, $ledger);
66 my $account_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
67 $account = $self->_format_account($account);
69 push @out, "${account_date} open ${account}";
77 sub format_commodities
{
83 for my $commodity (@{$ledger->commodities}) {
84 my $oldest_transaction = $self->_find_oldest_transaction_by_commodity($commodity, $ledger);
85 my $commodity_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
87 push @out, "${commodity_date} commodity $commodity->{iso}";
88 push @out, ' name: '.$self->_format_string($commodity->{name
}) if $commodity->{name
};
97 sub format_transactions
{
103 for my $transaction (@{$ledger->transactions}) {
104 push @out, $self->_format_transaction($transaction);
110 sub _format_transaction
{
112 my $transaction = shift;
114 my $account_width = $self->account_width;
116 my $date = $transaction->{date
};
117 my $status = $transaction->{status
};
118 my $payee = $transaction->{payee
} || '';
119 my $memo = $transaction->{memo
} || '';
120 my @postings = @{$transaction->{postings
}};
124 # figure out the Ledger transaction status
125 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
126 if (!$status_symbol) {
127 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
128 if (keys(%posting_statuses) == 1) {
129 my ($status) = keys %posting_statuses;
130 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
134 push @out, sprintf('%s%s%s%s', $date,
135 $status_symbol && ' '.$status_symbol || ' *', # status (or "txn") is required
136 ($payee || $memo) && ' '.$self->_format_string($payee),
137 $memo && ' '.$self->_format_string($memo),
140 if (my %tags = map { $_ => 1 } map { @{$_->{tags
} || []} } @postings) {
141 my @tags = map { "#$_" } keys %tags;
142 $out[-1] .= ' '.join(' ', @tags);
145 for my $posting (@postings) {
148 my $posting_status_symbol = '';
149 if (!$status_symbol) {
150 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
153 my $account = $self->_format_account($posting->{account
});
155 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
156 push @line, sprintf("\%-${account_width}s", $account);
158 if (defined $posting->{amount
}) {
159 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
160 my $lot_price = $posting->{lot_price
};
161 my $lot_date = $posting->{lot_date
};
162 my $lot_ref = $posting->{lot_ref
};
163 if ($lot_price || $lot_date || $lot_ref) {
166 $lot_price ? $self->_format_amount($lot_price->{amount
}, $lot_price->{commodity
}) : (),
167 $lot_date ? $lot_date : (),
168 $lot_ref ? $self->_format_string($lot_ref) : (),
172 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
173 my $is_total = defined $posting->{total_cost
};
174 my $cost_symbol = $is_total ? '@@' : '@';
175 push @line, ' ', $cost_symbol, ' ',
176 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
180 push @out, join('', @line);
188 sub _format_account
{
191 $account =~ s/[^A-Za-z0-9:]+/-/g;
192 $account =~ s/-+/-/g;
193 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
207 my $commodity = shift or _croak
'Must provide a valid currency';
209 my $format = "\% .$commodity->{frac}f";
210 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
212 # beancount doesn't support different notations
213 my $num = join('.', commify
($whole), $fraction);
215 $num = "$num $commodity->{iso}";
220 sub _find_oldest_transaction_by_account
{
225 $account = $self->_format_account($account);
227 my $oldest = $self->{oldest_transaction_by_account
};
230 for my $transaction (@{$ledger->transactions}) {
231 for my $posting (@{$transaction->{postings
}}) {
232 my $account = $self->_format_account($posting->{account
});
234 if ($transaction->{date
} lt ($oldest->{$account}{date
} || '9999-99-99')) {
235 $oldest->{$account} = $transaction;
240 $self->{oldest_transaction_by_account
} = $oldest;
243 return $oldest->{$account};
246 sub _find_oldest_transaction_by_commodity
{
248 my $commodity = shift;
251 my $oldest = $self->{oldest_transaction_by_commodity
};
254 for my $transaction (@{$ledger->transactions}) {
255 for my $posting (@{$transaction->{postings
}}) {
256 my $symbol = $posting->{commodity
}{symbol
};
259 if ($transaction->{date
} lt ($oldest->{$symbol}{date
} || '9999-99-99')) {
260 $oldest->{$symbol} = $transaction;
265 $self->{oldest_transaction_by_commodity
} = $oldest;
268 return $oldest->{$commodity->{symbol
}};
281 App::HomeBank2Ledger::Formatter::Beancount - Beancount formatter
289 This is a formatter for L<Beancount|http://furius.ca/beancount/>.
295 @lines = $formatter->format_header;
297 Get formatted header. For example,
300 ; File: path/to/finances.xhb
302 =head2 format_accounts
304 @lines = $formatter->format_accounts($ledger);
306 Get formatted accounts. For example,
308 2003-02-14 open Assets:Bank:Credit-Union:Savings
309 2003-02-14 open Assets:Bank:Credit-Union:Checking
312 =head2 format_commodities
314 @lines = $formatter->format_commodities($ledger);
316 Get formattted commodities. For example,
318 2003-02-14 commodity USD
322 =head2 format_transactions
324 @lines = $formatter->format_transactions($ledger);
326 Get formatted transactions. For example,
328 2003-02-14 * "Opening Balance"
329 Assets:Bank:Credit-Union:Savings 458.21 USD
330 Assets:Bank:Credit-Union:Checking 194.17 USD
331 Equity:Opening-Balances
337 L<App::HomeBank2Ledger::Formatter>
341 Please report any bugs or feature requests on the bugtracker website
342 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
344 When submitting a bug or request, please include a test-file or a
345 patch to an existing test-file that illustrates the bug or desired
350 Charles McGarvey <chazmcgarvey@brokenzipper.com>
352 =head1 COPYRIGHT AND LICENSE
354 This software is Copyright (c) 2019 by Charles McGarvey.
356 This is free software, licensed under:
358 The MIT (X11) License