]>
Dogcows Code - chaz/homebank2ledger/blob - lib/App/HomeBank2Ledger/Formatter/Ledger.pm
1 package App
::HomeBank2Ledger
::Formatter
::Ledger
;
2 # ABSTRACT: Ledger formatter
5 use v5
.10
.1; # defined-or
9 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
11 use parent
'App::HomeBank2Ledger::Formatter';
13 our $VERSION = '0.009'; # VERSION
15 my %STATUS_SYMBOLS = (
20 sub _croak
{ require Carp
; Carp
::croak
(@_) }
28 $self->format_accounts($ledger),
29 $self->format_commodities($ledger),
30 $self->format_payees($ledger),
31 $self->format_tags($ledger),
32 $self->format_transactions($ledger),
35 return join($/, map { rtrim
($_) } @out);
44 if (my $name = $self->name) {
45 push @out, "; Name: $name";
47 if (my $file = $self->file) {
48 push @out, "; File: $file";
63 push @out, map { "account $_" } sort @{$ledger->accounts};
70 sub format_commodities
{
76 for my $commodity (@{$ledger->commodities}) {
77 push @out, "commodity $commodity->{symbol}";
78 push @out, " note $commodity->{name}" if $commodity->{name
};
79 push @out, " format $commodity->{format}" if $commodity->{format
};
80 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
95 push @out, map { "payee $_" } sort @{$ledger->payees};
108 push @out, map { "tag $_" } sort @{$ledger->tags};
115 sub format_transactions
{
121 for my $transaction (@{$ledger->transactions}) {
122 push @out, $self->_format_transaction($transaction);
128 sub _format_transaction
{
130 my $transaction = shift;
132 my $account_width = $self->account_width;
134 my $date = $transaction->{date
} or _croak
'Transaction date is required';
135 my $aux_date = $transaction->{aux_date
} || $transaction->{effective_date
} || '';
136 my $status = $transaction->{status
} // '';
137 my $code = $transaction->{code
};
138 my $payee = $self->_format_string($transaction->{payee
});
139 my $note = $self->_format_string($transaction->{note
} // $transaction->{memo
});
140 my @postings = @{$transaction->{postings
} || _croak
'At least one transaction posting is required'};
144 # figure out the Ledger transaction status
145 my $status_symbol = $STATUS_SYMBOLS{$status};
146 if (!$status_symbol) {
147 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
148 if (keys(%posting_statuses) == 1) {
149 my ($status) = keys %posting_statuses;
150 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
154 $aux_date = '' if $date eq $aux_date;
155 $code =~ s/[\(\)]+// if defined $code;
156 $payee =~ s/(?: )|\t;/ ;/g if defined $payee; # don't turn into a note
158 my $has_code = defined $code && $code ne '';
159 my $has_payee = defined $payee && $payee ne '';
160 my $has_note = defined $note && $note ne '';
162 push @out, join('', $date,
163 $aux_date && "=${aux_date}",
164 $status_symbol && " ${status_symbol}",
165 $has_code && " (${code})",
166 $has_payee && " ${payee}",
167 $has_note && $has_payee && " ; ${note}",
169 if ($has_note && !$has_payee) {
170 push @out, " ; ${note}";
173 my $metadata = $transaction->{metadata
} || {};
174 for my $key (sort keys %$metadata) {
175 my $value = $self->_format_string($metadata->{$key}) ;
176 push @out, " ; ${key}: ${value}";
179 for my $posting (@postings) {
182 my $posting_status_symbol = '';
183 if (!$status_symbol) {
184 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
187 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
188 push @line, sprintf("\%-${account_width}s", $posting->{account
});
190 if (defined $posting->{amount
}) {
191 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
192 my $lot = $posting->{lot
} || {};
193 if (my $lot_price = $lot->{price
} // $posting->{lot_price
}) {
194 my $is_fixed = $lot_price->{fixed
} // $posting->{lot_fixed
};
195 my $fixed_symbol = $is_fixed ? '=' : '';
196 push @line, " {${fixed_symbol}",
197 $self->_format_amount($lot_price->{amount
}, $lot_price->{commodity
}),
200 if (my $lot_date = $lot->{date
} // $posting->{lot_date
}) {
201 push @line, " [${lot_date}]";
203 if (my $lot_note = $self->_format_string($lot->{note
} // $posting->{lot_note
} // '')) {
204 $lot_note =~ s/[\(\)]+//; # cleanup
205 $lot_note =~ s/^\@+//;
206 push @line, " (${lot_note})" if $lot_note;
208 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
209 my $is_total = defined $posting->{total_cost
};
210 my $cost_symbol = $is_total ? '@@' : '@';
211 push @line, ' ', $cost_symbol, ' ',
212 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
215 my $posting_date = $posting->{date
} || '';
216 my $posting_aux_date = $posting->{aux_date
} || '';
217 my $posting_note = $self->_format_string($posting->{note
} // $posting->{memo
} // '');
218 $posting_date = '' if $posting_date eq $date;
219 $posting_aux_date = '' if $posting_aux_date eq $aux_date;
220 $posting_note = '' if $has_note && $posting_note eq $note;
221 my $has_posting_note = defined $posting_note && $posting_note ne '';
222 if ($posting_date || $posting_aux_date || $has_posting_note) {
223 if ($posting_date || $posting_aux_date) {
224 $posting_note = sprintf('[%s%s]%s',
226 $posting_aux_date && "=${posting_aux_date}",
227 $has_posting_note && " ${posting_note}",
230 push @line, " ; ${posting_note}";
233 push @out, join('', @line);
235 my $metadata = $posting->{metadata
} || {};
236 for my $key (sort keys %$metadata) {
237 my $value = $self->_format_string($metadata->{$key});
238 push @out, " ; ${key}: ${value}";
241 if (my $posting_payee = $self->_format_string($posting->{payee
} // '')) {
242 push @out, " ; Payee: $posting_payee" if !$has_payee || $posting_payee ne $payee;
245 if (my @tags = @{$posting->{tags
} || []}) {
246 push @out, ' ; :'.join(':', @tags).':';
257 my $str = shift // return;
272 my $commodity = shift or _croak
'Must provide a valid currency';
274 my $format = "\% .$commodity->{frac}f";
275 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
278 my $num = commify
($whole, $commodity->{gchar
});
279 if ($commodity->{frac
}) {
280 $num .= $commodity->{dchar
} . $fraction;
283 my $symbol = $commodity->{symbol
};
284 $symbol = $self->_quote_string($symbol) if $symbol =~ /[0-9\s]/;
286 $num = $commodity->{syprf
} ? "$symbol $num" : "$num $symbol";
301 App::HomeBank2Ledger::Formatter::Ledger - Ledger formatter
309 This is a formatter for L<Ledger|https://www.ledger-cli.org/>.
315 @lines = $formatter->format_header;
317 Get formatted header. For example,
320 ; File: path/to/finances.xhb
322 =head2 format_accounts
324 @lines = $formatter->format_accounts($ledger);
326 Get formatted accounts. For example,
328 account Assets:Bank:Credit Union:Savings
329 account Assets:Bank:Credit Union:Checking
332 =head2 format_commodities
334 @lines = $formatter->format_commodities($ledger);
336 Get formattted commodities. For example,
346 @lines = $formatter->format_payees($ledger);
348 Get formatted payees. For example,
355 @lines = $formatter->format_tags($ledger);
357 Get formatted tags. For example,
362 =head2 format_transactions
364 @lines = $formatter->format_transactions($ledger);
366 Get formatted transactions. For example,
368 2003-02-14 * Opening Balance
369 Assets:Bank:Credit Union:Savings $ 458.21
370 Assets:Bank:Credit Union:Checking $ 194.17
371 Equity:Opening Balances
377 L<App::HomeBank2Ledger::Formatter>
381 Please report any bugs or feature requests on the bugtracker website
382 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
384 When submitting a bug or request, please include a test-file or a
385 patch to an existing test-file that illustrates the bug or desired
390 Charles McGarvey <chazmcgarvey@brokenzipper.com>
392 =head1 COPYRIGHT AND LICENSE
394 This software is Copyright (c) 2019 by Charles McGarvey.
396 This is free software, licensed under:
398 The MIT (X11) License
This page took 0.058272 seconds and 5 git commands to generate.