]>
Dogcows Code - chaz/homebank2ledger/blob - lib/App/HomeBank2Ledger/Formatter/Beancount.pm
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);
19 use parent
'App::HomeBank2Ledger::Formatter';
21 our $VERSION = '9999.999'; # VERSION
23 my %STATUS_SYMBOLS = (
33 $self->_format_header,
34 $self->_format_accounts($ledger),
35 $self->_format_commodities($ledger),
36 # $self->_format_payees,
37 # $self->_format_tags,
38 $self->_format_transactions($ledger),
41 return join($/, @out);
49 my $file = $self->file;
50 push @out, "; Converted from $file using homebank2ledger ${VERSION}";
52 if (my $name = $self->name) {
53 push @out, "; Name: $name";
61 sub _format_accounts
{
67 for my $account (sort @{$ledger->accounts}) {
68 $account = $self->_munge_account($account);
69 push @out, "1970-01-01 open $account"; # TODO pick better date?
76 sub _format_commodities
{
82 for my $commodity (@{$ledger->commodities}) {
83 push @out, "1970-01-01 commodity $commodity->{iso}"; # TODO
84 push @out, " name: \"$commodity->{name}\"" if $commodity->{name
};
92 sub _format_transactions
{
98 for my $transaction (@{$ledger->transactions}) {
99 push @out, $self->_format_transaction($transaction);
105 sub _format_transaction
{
107 my $transaction = shift;
109 my $account_width = $self->account_width;
111 my $date = $transaction->{date
};
112 my $status = $transaction->{status
};
113 my $payee = $transaction->{payee
} || 'No Payee TODO';
114 my $memo = $transaction->{memo
} || '';
115 my @postings = @{$transaction->{postings
}};
119 # figure out the Ledger transaction status
120 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
121 if (!$status_symbol) {
122 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
123 if (keys(%posting_statuses) == 1) {
124 my ($status) = keys %posting_statuses;
125 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
126 $status_symbol .= ' ' if $status_symbol;
130 my $symbol = $status_symbol ? "${status_symbol} " : '';
131 push @out, "${date} ${symbol}\"${payee}\" \"$memo\""; # TODO handle proper quoting
132 $out[-1] =~ s/\h+$//;
134 if (my %tags = map { $_ => 1 } map { @{$_->{tags
} || []} } @postings) {
135 my @tags = map { "#$_" } keys %tags;
136 $out[-1] .= " ".join(' ', @tags);
139 for my $posting (@postings) {
142 my $posting_status_symbol = '';
143 if (!$status_symbol) {
144 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
147 my $account = $self->_munge_account($posting->{account
});
149 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
150 push @line, sprintf("\%-${account_width}s", $account);
152 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
154 push @out, join('', @line);
155 $out[-1] =~ s/\h+$//;
157 # if (my $payee = $posting->{payee}) {
158 # push @out, " ; Payee: $payee";
170 my $commodity = shift;
172 # _croak 'Must provide a valid currency' if !$commodity;
174 my $format = "\% .$commodity->{frac}f";
175 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
177 # beancount doesn't support different notations
178 my $num = join('.', commify
($whole), $fraction);
180 $num = "$num $commodity->{iso}";
188 $account =~ s/[^A-Za-z0-9:]+/-/g;
189 $account =~ s/-+/-/g;
190 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
This page took 0.042231 seconds and 4 git commands to generate.