format payees and memo on transactions
[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 rtrim);
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 my $UNKNOWN_DATE = '0001-01-01';
28
29 sub _croak { require Carp; Carp::croak(@_) }
30
31 sub format {
32 my $self = shift;
33 my $ledger = shift;
34
35 my @out = (
36 $self->_format_header,
37 $self->_format_accounts($ledger),
38 $self->_format_commodities($ledger),
39 # $self->_format_payees,
40 # $self->_format_tags,
41 $self->_format_transactions($ledger),
42 );
43
44 return join($/, map { rtrim($_) } @out);
45 }
46
47 sub _format_header {
48 my $self = shift;
49
50 my @out;
51
52 if (my $name = $self->name) {
53 push @out, "; Name: $name";
54 }
55
56 my $file = $self->file;
57 push @out, "; Converted from ${file} using homebank2ledger ${VERSION}";
58
59 push @out, '';
60
61 return @out;
62 }
63
64 sub _format_accounts {
65 my $self = shift;
66 my $ledger = shift;
67
68 my @out;
69
70 for my $account (sort @{$ledger->accounts}) {
71 my $oldest_transaction = $self->_find_oldest_transaction_by_account($account, $ledger);
72 my $account_date = $oldest_transaction->{date} || $UNKNOWN_DATE;
73 $account = $self->_format_account($account);
74
75 push @out, "${account_date} open ${account}";
76 }
77 push @out, '';
78
79 return @out;
80 }
81
82 sub _format_commodities {
83 my $self = shift;
84 my $ledger = shift;
85
86 my @out;
87
88 for my $commodity (@{$ledger->commodities}) {
89 my $oldest_transaction = $self->_find_oldest_transaction_by_commodity($commodity, $ledger);
90 my $commodity_date = $oldest_transaction->{date} || $UNKNOWN_DATE;
91
92 push @out, "${commodity_date} commodity $commodity->{iso}";
93 push @out, ' name: '.$self->_format_string($commodity->{name}) if $commodity->{name};
94 }
95
96 push @out, '';
97
98 return @out;
99 }
100
101 sub _format_transactions {
102 my $self = shift;
103 my $ledger = shift;
104
105 my @out;
106
107 for my $transaction (@{$ledger->transactions}) {
108 push @out, $self->_format_transaction($transaction);
109 }
110
111 return @out;
112 }
113
114 sub _format_transaction {
115 my $self = shift;
116 my $transaction = shift;
117
118 my $account_width = $self->account_width;
119
120 my $date = $transaction->{date};
121 my $status = $transaction->{status};
122 my $payee = $transaction->{payee} || '';
123 my $memo = $transaction->{memo} || '';
124 my @postings = @{$transaction->{postings}};
125
126 my @out;
127
128 # figure out the Ledger transaction status
129 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
130 if (!$status_symbol) {
131 my %posting_statuses = map { ($_->{status} || '') => 1 } @postings;
132 if (keys(%posting_statuses) == 1) {
133 my ($status) = keys %posting_statuses;
134 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
135 }
136 }
137
138 push @out, sprintf('%s%s%s%s', $date,
139 $status_symbol && ' '.$status_symbol || ' *', # status (or "txn") is required
140 ($payee || $memo) && ' '.$self->_format_string($payee),
141 $memo && ' '.$self->_format_string($memo),
142 );
143
144 if (my %tags = map { $_ => 1 } map { @{$_->{tags} || []} } @postings) {
145 my @tags = map { "#$_" } keys %tags;
146 $out[-1] .= ' '.join(' ', @tags);
147 }
148
149 for my $posting (@postings) {
150 my @line;
151
152 my $posting_status_symbol = '';
153 if (!$status_symbol) {
154 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status} || ''} || '';
155 }
156
157 my $account = $self->_format_account($posting->{account});
158
159 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
160 push @line, sprintf("\%-${account_width}s", $account);
161 push @line, ' ';
162 push @line, $self->_format_amount($posting->{amount}, $posting->{commodity}) if defined $posting->{amount};
163
164 push @out, join('', @line);
165 }
166
167 push @out, '';
168
169 return @out;
170 }
171
172 sub _format_account {
173 my $self = shift;
174 my $account = shift;
175 $account =~ s/[^A-Za-z0-9:]+/-/g;
176 $account =~ s/-+/-/g;
177 $account =~ s/(?:^|(?<=:))([a-z])/uc($1)/eg;
178 return $account;
179 }
180
181 sub _format_string {
182 my $self = shift;
183 my $str = shift;
184 $str =~ s/"/\\"/g;
185 return "\"$str\"";
186 }
187
188 sub _format_amount {
189 my $self = shift;
190 my $amount = shift;
191 my $commodity = shift or _croak 'Must provide a valid currency';
192
193 my $format = "\% .$commodity->{frac}f";
194 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
195
196 # beancount doesn't support different notations
197 my $num = join('.', commify($whole), $fraction);
198
199 $num = "$num $commodity->{iso}";
200
201 return $num;
202 }
203
204 sub _find_oldest_transaction_by_account {
205 my $self = shift;
206 my $account = shift;
207 my $ledger = shift;
208
209 $account = $self->_format_account($account);
210
211 my $oldest = $self->{oldest_transaction_by_account};
212 if (!$oldest) {
213 # build index
214 for my $transaction (@{$ledger->transactions}) {
215 for my $posting (@{$transaction->{postings}}) {
216 my $account = $self->_format_account($posting->{account});
217
218 if ($transaction->{date} lt ($oldest->{$account}{date} || '9999-99-99')) {
219 $oldest->{$account} = $transaction;
220 }
221 }
222 }
223
224 $self->{oldest_transaction_by_account} = $oldest;
225 }
226
227 return $oldest->{$account};
228 }
229
230 sub _find_oldest_transaction_by_commodity {
231 my $self = shift;
232 my $commodity = shift;
233 my $ledger = shift;
234
235 my $oldest = $self->{oldest_transaction_by_commodity};
236 if (!$oldest) {
237 # build index
238 for my $transaction (@{$ledger->transactions}) {
239 for my $posting (@{$transaction->{postings}}) {
240 my $symbol = $posting->{commodity}{symbol};
241 next if !$symbol;
242
243 if ($transaction->{date} lt ($oldest->{$symbol}{date} || '9999-99-99')) {
244 $oldest->{$symbol} = $transaction;
245 }
246 }
247 }
248
249 $self->{oldest_transaction_by_commodity} = $oldest;
250 }
251
252 return $oldest->{$commodity->{symbol}};
253 }
254
255 1;
This page took 0.046125 seconds and 4 git commands to generate.