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
(@_) }
37 $self->format_accounts($ledger),
38 $self->format_commodities($ledger),
39 # $self->format_payees,
41 $self->format_transactions($ledger),
44 return join($/, map { rtrim
($_) } @out);
49 @lines = $formatter->format_header;
51 Get formatted header
. For example
,
53 ; Converted from finances
.xhb using homebank2ledger
0.001
62 if (my $name = $self->name) {
63 push @out, "; Name: $name";
66 my $file = $self->file;
67 push @out, "; Converted from ${file} using homebank2ledger ${VERSION}";
74 =method format_accounts
76 @lines = $formatter->format_accounts($ledger);
78 Get formatted accounts
. For example
,
80 2003-02-14 open Assets
:Bank
:Credit-Union
:Savings
81 2003-02-14 open Assets
:Bank
:Credit-Union
:Checking
92 for my $account (sort @{$ledger->accounts}) {
93 my $oldest_transaction = $self->_find_oldest_transaction_by_account($account, $ledger);
94 my $account_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
95 $account = $self->_format_account($account);
97 push @out, "${account_date} open ${account}";
104 =method format_commodities
106 @lines = $formatter->format_commodities($ledger);
108 Get formattted commodities
. For example
,
110 2003-02-14 commodity USD
116 sub format_commodities
{
122 for my $commodity (@{$ledger->commodities}) {
123 my $oldest_transaction = $self->_find_oldest_transaction_by_commodity($commodity, $ledger);
124 my $commodity_date = $oldest_transaction->{date
} || $UNKNOWN_DATE;
126 push @out, "${commodity_date} commodity $commodity->{iso}";
127 push @out, ' name: '.$self->_format_string($commodity->{name
}) if $commodity->{name
};
135 =method format_transactions
137 @lines = $formatter->format_transactions($ledger);
139 Get formatted transactions
. For example
,
141 2003-02-14 * "Opening Balance"
142 Assets
:Bank
:Credit-Union
:Savings
458.21 USD
143 Assets
:Bank
:Credit-Union
:Checking
194.17 USD
144 Equity
:Opening-Balances
150 sub format_transactions
{
156 for my $transaction (@{$ledger->transactions}) {
157 push @out, $self->_format_transaction($transaction);
163 sub _format_transaction
{
165 my $transaction = shift;
167 my $account_width = $self->account_width;
169 my $date = $transaction->{date
};
170 my $status = $transaction->{status
};
171 my $payee = $transaction->{payee
} || '';
172 my $memo = $transaction->{memo
} || '';
173 my @postings = @{$transaction->{postings
}};
177 # figure out the Ledger transaction status
178 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
179 if (!$status_symbol) {
180 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
181 if (keys(%posting_statuses) == 1) {
182 my ($status) = keys %posting_statuses;
183 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
187 push @out, sprintf('%s%s%s%s', $date,
188 $status_symbol && ' '.$status_symbol || ' *', # status (or "txn") is required
189 ($payee || $memo) && ' '.$self->_format_string($payee),
190 $memo && ' '.$self->_format_string($memo),
193 if (my %tags = map { $_ => 1 } map { @{$_->{tags
} || []} } @postings) {
194 my @tags = map { "#$_" } keys %tags;
195 $out[-1] .= ' '.join(' ', @tags);
198 for my $posting (@postings) {
201 my $posting_status_symbol = '';
202 if (!$status_symbol) {
203 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
206 my $account = $self->_format_account($posting->{account
});
208 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
209 push @line, sprintf("\%-${account_width}s", $account);
211 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
213 push @out, join('', @line);
221 sub _format_account
{
224 $account =~ s/[^A-Za-z0-9:]+/-/g;
225 $account =~ s/-+/-/g;
226 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
240 my $commodity = shift or _croak
'Must provide a valid currency';
242 my $format = "\% .$commodity->{frac}f";
243 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
245 # beancount doesn't support different notations
246 my $num = join('.', commify
($whole), $fraction);
248 $num = "$num $commodity->{iso}";
253 sub _find_oldest_transaction_by_account
{
258 $account = $self->_format_account($account);
260 my $oldest = $self->{oldest_transaction_by_account
};
263 for my $transaction (@{$ledger->transactions}) {
264 for my $posting (@{$transaction->{postings
}}) {
265 my $account = $self->_format_account($posting->{account
});
267 if ($transaction->{date
} lt ($oldest->{$account}{date
} || '9999-99-99')) {
268 $oldest->{$account} = $transaction;
273 $self->{oldest_transaction_by_account
} = $oldest;
276 return $oldest->{$account};
279 sub _find_oldest_transaction_by_commodity
{
281 my $commodity = shift;
284 my $oldest = $self->{oldest_transaction_by_commodity
};
287 for my $transaction (@{$ledger->transactions}) {
288 for my $posting (@{$transaction->{postings
}}) {
289 my $symbol = $posting->{commodity
}{symbol
};
292 if ($transaction->{date
} lt ($oldest->{$symbol}{date
} || '9999-99-99')) {
293 $oldest->{$symbol} = $transaction;
298 $self->{oldest_transaction_by_commodity
} = $oldest;
301 return $oldest->{$commodity->{symbol
}};