initial commit
[chaz/homebank2ledger] / lib / App / HomeBank2Ledger / Formatter / Beancount.pm
1 package App::HomeBank2Ledger::Formatter::Beancount;
2 # ABSTRACT: Beancount formatter
3
4 =head1 DESCRIPTION
5
6 This is a formatter for L<Beancount|http://furius.ca/beancount/>.
7
8 =head1 SEE ALSO
9
10 L<App::HomeBank2Ledger::Formatter>
11
12 =cut
13
14 use warnings;
15 use strict;
16
17 use App::HomeBank2Ledger::Util qw(commify);
18
19 use parent 'App::HomeBank2Ledger::Formatter';
20
21 our $VERSION = '9999.999'; # VERSION
22
23 my %STATUS_SYMBOLS = (
24 cleared => '*',
25 pending => '!',
26 );
27
28 sub format {
29 my $self = shift;
30 my $ledger = shift;
31
32 my @out = (
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),
39 );
40
41 return join($/, @out);
42 }
43
44 sub _format_header {
45 my $self = shift;
46
47 my @out;
48
49 my $file = $self->file;
50 push @out, "; Converted from $file using homebank2ledger ${VERSION}";
51
52 if (my $name = $self->name) {
53 push @out, "; Name: $name";
54 }
55
56 push @out, '';
57
58 return @out;
59 }
60
61 sub _format_accounts {
62 my $self = shift;
63 my $ledger = shift;
64
65 my @out;
66
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?
70 }
71 push @out, '';
72
73 return @out;
74 }
75
76 sub _format_commodities {
77 my $self = shift;
78 my $ledger = shift;
79
80 my @out;
81
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};
85 }
86
87 push @out, '';
88
89 return @out;
90 }
91
92 sub _format_transactions {
93 my $self = shift;
94 my $ledger = shift;
95
96 my @out;
97
98 for my $transaction (@{$ledger->transactions}) {
99 push @out, $self->_format_transaction($transaction);
100 }
101
102 return @out;
103 }
104
105 sub _format_transaction {
106 my $self = shift;
107 my $transaction = shift;
108
109 my $account_width = $self->account_width;
110
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}};
116
117 my @out;
118
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;
127 }
128 }
129
130 my $symbol = $status_symbol ? "${status_symbol} " : '';
131 push @out, "${date} ${symbol}\"${payee}\" \"$memo\""; # TODO handle proper quoting
132 $out[-1] =~ s/\h+$//;
133
134 if (my %tags = map { $_ => 1 } map { @{$_->{tags} || []} } @postings) {
135 my @tags = map { "#$_" } keys %tags;
136 $out[-1] .= " ".join(' ', @tags);
137 }
138
139 for my $posting (@postings) {
140 my @line;
141
142 my $posting_status_symbol = '';
143 if (!$status_symbol) {
144 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status} || ''} || '';
145 }
146
147 my $account = $self->_munge_account($posting->{account});
148
149 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
150 push @line, sprintf("\%-${account_width}s", $account);
151 push @line, ' ';
152 push @line, $self->_format_amount($posting->{amount}, $posting->{commodity}) if defined $posting->{amount};
153
154 push @out, join('', @line);
155 $out[-1] =~ s/\h+$//;
156
157 # if (my $payee = $posting->{payee}) {
158 # push @out, " ; Payee: $payee";
159 # }
160 }
161
162 push @out, '';
163
164 return @out;
165 }
166
167 sub _format_amount {
168 my $self = shift;
169 my $amount = shift;
170 my $commodity = shift;
171
172 # _croak 'Must provide a valid currency' if !$commodity;
173
174 my $format = "\% .$commodity->{frac}f";
175 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
176
177 # beancount doesn't support different notations
178 my $num = join('.', commify($whole), $fraction);
179
180 $num = "$num $commodity->{iso}";
181
182 return $num;
183 }
184
185 sub _munge_account {
186 my $self = shift;
187 my $account = shift;
188 $account =~ s/[^A-Za-z0-9:]+/-/g;
189 $account =~ s/-+/-/g;
190 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
191 return $account;
192 }
193
194 1;
This page took 0.044925 seconds and 5 git commands to generate.