]> Dogcows Code - chaz/homebank2ledger/blob - lib/App/HomeBank2Ledger/Formatter/Ledger.pm
add posting-specific dates
[chaz/homebank2ledger] / lib / App / HomeBank2Ledger / Formatter / Ledger.pm
1 package App::HomeBank2Ledger::Formatter::Ledger;
2 # ABSTRACT: Ledger formatter
3
4 =head1 DESCRIPTION
5
6 This is a formatter for L<Ledger|https://www.ledger-cli.org/>.
7
8 =head1 SEE ALSO
9
10 L<App::HomeBank2Ledger::Formatter>
11
12 =cut
13
14 use v5.10.1; # defined-or
15 use warnings;
16 use strict;
17
18 use App::HomeBank2Ledger::Util qw(commify rtrim);
19
20 use parent 'App::HomeBank2Ledger::Formatter';
21
22 our $VERSION = '9999.999'; # VERSION
23
24 my %STATUS_SYMBOLS = (
25 cleared => '*',
26 pending => '!',
27 );
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($ledger),
40 $self->format_tags($ledger),
41 $self->format_transactions($ledger),
42 );
43
44 return join($/, map { rtrim($_) } @out);
45 }
46
47 =method format_header
48
49 @lines = $formatter->format_header;
50
51 Get formatted header. For example,
52
53 ; Name: My Finances
54 ; File: path/to/finances.xhb
55
56 =cut
57
58 sub format_header {
59 my $self = shift;
60
61 my @out;
62
63 if (my $name = $self->name) {
64 push @out, "; Name: $name";
65 }
66 if (my $file = $self->file) {
67 push @out, "; File: $file";
68 }
69
70 push @out, '';
71
72 return @out;
73 }
74
75 =method format_accounts
76
77 @lines = $formatter->format_accounts($ledger);
78
79 Get formatted accounts. For example,
80
81 account Assets:Bank:Credit Union:Savings
82 account Assets:Bank:Credit Union:Checking
83 ...
84
85 =cut
86
87 sub format_accounts {
88 my $self = shift;
89 my $ledger = shift;
90
91 my @out;
92
93 push @out, map { "account $_" } sort @{$ledger->accounts};
94 push @out, '';
95
96 return @out;
97 }
98
99 =method format_commodities
100
101 @lines = $formatter->format_commodities($ledger);
102
103 Get formattted commodities. For example,
104
105 commodity $
106 note US Dollar
107 format $ 1,000.00
108 alias USD
109 ...
110
111 =cut
112
113 sub format_commodities {
114 my $self = shift;
115 my $ledger = shift;
116
117 my @out;
118
119 for my $commodity (@{$ledger->commodities}) {
120 push @out, "commodity $commodity->{symbol}";
121 push @out, " note $commodity->{name}" if $commodity->{name};
122 push @out, " format $commodity->{format}" if $commodity->{format};
123 push @out, " alias $commodity->{iso}" if $commodity->{iso};
124 }
125
126 push @out, '';
127
128 return @out;
129 }
130
131 =method format_payees
132
133 @lines = $formatter->format_payees($ledger);
134
135 Get formatted payees. For example,
136
137 payee 180 Tacos
138 ...
139
140 =cut
141
142 sub format_payees {
143 my $self = shift;
144 my $ledger = shift;
145
146 my @out;
147
148 push @out, map { "payee $_" } sort @{$ledger->payees};
149 push @out, '';
150
151 return @out;
152 }
153
154 =method format_tags
155
156 @lines = $formatter->format_tags($ledger);
157
158 Get formatted tags. For example,
159
160 tag yapc
161 ...
162
163 =cut
164
165 sub format_tags {
166 my $self = shift;
167 my $ledger = shift;
168
169 my @out;
170
171 push @out, map { "tag $_" } sort @{$ledger->tags};
172 push @out, '';
173
174 return @out;
175 }
176
177 =method format_transactions
178
179 @lines = $formatter->format_transactions($ledger);
180
181 Get formatted transactions. For example,
182
183 2003-02-14 * Opening Balance
184 Assets:Bank:Credit Union:Savings $ 458.21
185 Assets:Bank:Credit Union:Checking $ 194.17
186 Equity:Opening Balances
187
188 ...
189
190 =cut
191
192 sub format_transactions {
193 my $self = shift;
194 my $ledger = shift;
195
196 my @out;
197
198 for my $transaction (@{$ledger->transactions}) {
199 push @out, $self->_format_transaction($transaction);
200 }
201
202 return @out;
203 }
204
205 sub _format_transaction {
206 my $self = shift;
207 my $transaction = shift;
208
209 my $account_width = $self->account_width;
210
211 my $date = $transaction->{date} or _croak 'Transaction date is required';
212 my $aux_date = $transaction->{aux_date} || $transaction->{effective_date} || '';
213 my $status = $transaction->{status} // '';
214 my $code = $transaction->{code};
215 my $payee = $self->_format_string($transaction->{payee});
216 my $note = $self->_format_string($transaction->{note} // $transaction->{memo});
217 my @postings = @{$transaction->{postings} || _croak 'At least one transaction posting is required'};
218
219 my @out;
220
221 # figure out the Ledger transaction status
222 my $status_symbol = $STATUS_SYMBOLS{$status};
223 if (!$status_symbol) {
224 my %posting_statuses = map { ($_->{status} || '') => 1 } @postings;
225 if (keys(%posting_statuses) == 1) {
226 my ($status) = keys %posting_statuses;
227 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
228 }
229 }
230
231 $aux_date = '' if $date eq $aux_date;
232 $code =~ s/[\(\)]+// if defined $code;
233 $payee =~ s/(?: )|\t;/ ;/g if defined $payee; # don't turn into a note
234
235 my $has_code = defined $code && $code ne '';
236 my $has_payee = defined $payee && $payee ne '';
237 my $has_note = defined $note && $note ne '';
238
239 push @out, join('', $date,
240 $aux_date && "=${aux_date}",
241 $status_symbol && " ${status_symbol}",
242 $has_code && " (${code})",
243 $has_payee && " ${payee}",
244 $has_note && $has_payee && " ; ${note}",
245 );
246 if ($has_note && !$has_payee) {
247 push @out, " ; ${note}";
248 }
249
250 my $metadata = $transaction->{metadata} || {};
251 for my $key (sort keys %$metadata) {
252 my $value = $self->_format_string($metadata->{$key}) ;
253 push @out, " ; ${key}: ${value}";
254 }
255
256 for my $posting (@postings) {
257 my @line;
258
259 my $posting_status_symbol = '';
260 if (!$status_symbol) {
261 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status} || ''} || '';
262 }
263
264 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
265 push @line, sprintf("\%-${account_width}s", $posting->{account});
266 push @line, ' ';
267 if (defined $posting->{amount}) {
268 push @line, $self->_format_amount($posting->{amount}, $posting->{commodity});
269 my $lot = $posting->{lot} || {};
270 if (my $lot_price = $lot->{price} // $posting->{lot_price}) {
271 my $is_fixed = $lot_price->{fixed} // $posting->{lot_fixed};
272 my $fixed_symbol = $is_fixed ? '=' : '';
273 push @line, " {${fixed_symbol}",
274 $self->_format_amount($lot_price->{amount}, $lot_price->{commodity}),
275 '}';
276 }
277 if (my $lot_date = $lot->{date} // $posting->{lot_date}) {
278 push @line, " [${lot_date}]";
279 }
280 if (my $lot_note = $self->_format_string($lot->{note} // $posting->{lot_note} // '')) {
281 $lot_note =~ s/[\(\)]+//; # cleanup
282 $lot_note =~ s/^\@+//;
283 push @line, " (${lot_note})" if $lot_note;
284 }
285 if (my $cost = $posting->{total_cost} // $posting->{cost}) {
286 my $is_total = defined $posting->{total_cost};
287 my $cost_symbol = $is_total ? '@@' : '@';
288 push @line, ' ', $cost_symbol, ' ',
289 $self->_format_amount($cost->{amount}, $cost->{commodity});
290 }
291 }
292 my $posting_date = $posting->{date} || '';
293 my $posting_aux_date = $posting->{aux_date} || '';
294 my $posting_note = $self->_format_string($posting->{note} // $posting->{memo} // '');
295 $posting_date = '' if $posting_date eq $date;
296 $posting_aux_date = '' if $posting_aux_date eq $aux_date;
297 $posting_note = '' if $has_note && $posting_note eq $note;
298 my $has_posting_note = defined $posting_note && $posting_note ne '';
299 if ($posting_date || $posting_aux_date || $has_posting_note) {
300 if ($posting_date || $posting_aux_date) {
301 $posting_note = sprintf('[%s%s]%s',
302 $posting_date,
303 $posting_aux_date && "=${posting_aux_date}",
304 $has_posting_note && " ${posting_note}",
305 );
306 }
307 push @line, " ; ${posting_note}";
308 }
309
310 push @out, join('', @line);
311
312 my $metadata = $posting->{metadata} || {};
313 for my $key (sort keys %$metadata) {
314 my $value = $self->_format_string($metadata->{$key});
315 push @out, " ; ${key}: ${value}";
316 }
317
318 if (my $posting_payee = $self->_format_string($posting->{payee} // '')) {
319 push @out, " ; Payee: $posting_payee" if !$has_payee || $posting_payee ne $payee;
320 }
321
322 if (my @tags = @{$posting->{tags} || []}) {
323 push @out, ' ; :'.join(':', @tags).':';
324 }
325 }
326
327 push @out, '';
328
329 return @out;
330 }
331
332 sub _format_string {
333 my $self = shift;
334 my $str = shift // return;
335 $str =~ s/\v//g;
336 return $str;
337 }
338
339 sub _quote_string {
340 my $self = shift;
341 my $str = shift;
342 $str =~ s/"/\\"/g;
343 return "\"$str\"";
344 }
345
346 sub _format_amount {
347 my $self = shift;
348 my $amount = shift;
349 my $commodity = shift or _croak 'Must provide a valid currency';
350
351 my $format = "\% .$commodity->{frac}f";
352 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
353 $fraction ||= 0;
354
355 my $num = commify($whole, $commodity->{gchar});
356 if ($commodity->{frac}) {
357 $num .= $commodity->{dchar} . $fraction;
358 }
359
360 my $symbol = $commodity->{symbol};
361 $symbol = $self->_quote_string($symbol) if $symbol =~ /[0-9\s]/;
362
363 $num = $commodity->{syprf} ? "$symbol $num" : "$num $symbol";
364
365 return $num;
366 }
367
368 1;
This page took 0.051926 seconds and 4 git commands to generate.