1 package App
::HomeBank2Ledger
::Formatter
::Ledger
;
2 # ABSTRACT: Ledger formatter
6 This is a formatter for L<Ledger|https://www.ledger-cli.org/>.
10 L<App::HomeBank2Ledger::Formatter>
14 use v5
.10
.1; # defined-or
18 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
20 use parent
'App::HomeBank2Ledger::Formatter';
22 our $VERSION = '9999.999'; # VERSION
24 my %STATUS_SYMBOLS = (
28 my $SYMBOL = qr![^\s\d.,;:?\!\
-+*/^&|=\
<\
>\
[\
]\
(\
)\
{\
}\
@]+!;
30 sub _croak
{ require Carp
; Carp
::croak
(@_) }
38 $self->format_accounts($ledger),
39 $self->format_commodities($ledger),
40 $self->format_payees($ledger),
41 $self->format_tags($ledger),
42 $self->format_transactions($ledger),
45 return join($/, map { rtrim
($_) } @out);
50 @lines = $formatter->format_header;
52 Get formatted header
. For example
,
55 ; File
: path
/to/finances
.xhb
64 if (my $name = $self->name) {
65 push @out, "; Name: $name";
67 if (my $file = $self->file) {
68 push @out, "; File: $file";
76 =method format_accounts
78 @lines = $formatter->format_accounts($ledger);
80 Get formatted accounts
. For example
,
82 account Assets
:Bank
:Credit Union
:Savings
83 account Assets
:Bank
:Credit Union
:Checking
94 push @out, map { "account $_" } sort @{$ledger->accounts};
100 =method format_commodities
102 @lines = $formatter->format_commodities($ledger);
104 Get formattted commodities
. For example
,
114 sub format_commodities
{
120 for my $commodity (@{$ledger->commodities}) {
121 push @out, "commodity $commodity->{symbol}";
122 push @out, " note $commodity->{name}" if $commodity->{name
};
123 push @out, " format $commodity->{format}" if $commodity->{format
};
124 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
132 =method format_payees
134 @lines = $formatter->format_payees($ledger);
136 Get formatted payees
. For example
,
149 push @out, map { "payee $_" } sort @{$ledger->payees};
157 @lines = $formatter->format_tags($ledger);
159 Get formatted tags
. For example
,
172 push @out, map { "tag $_" } sort @{$ledger->tags};
178 =method format_transactions
180 @lines = $formatter->format_transactions($ledger);
182 Get formatted transactions
. For example
,
184 2003-02-14 * Opening Balance
185 Assets
:Bank
:Credit Union
:Savings
$ 458.21
186 Assets
:Bank
:Credit Union
:Checking
$ 194.17
187 Equity
:Opening Balances
193 sub format_transactions
{
199 for my $transaction (@{$ledger->transactions}) {
200 push @out, $self->_format_transaction($transaction);
206 sub _format_transaction
{
208 my $transaction = shift;
210 my $account_width = $self->account_width;
212 my $date = $transaction->{date
} or _croak
'Transaction date is required';
213 my $aux_date = $transaction->{aux_date
} || $transaction->{effective_date
} || '';
214 my $status = $transaction->{status
} // '';
215 my $code = $transaction->{code
};
216 my $payee = $self->_format_string($transaction->{payee
});
217 my $note = $self->_format_string($transaction->{note
} // $transaction->{memo
});
218 my @postings = @{$transaction->{postings
} || _croak
'At least one transaction posting is required'};
222 # figure out the Ledger transaction status
223 my $status_symbol = $STATUS_SYMBOLS{$status};
224 if (!$status_symbol) {
225 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
226 if (keys(%posting_statuses) == 1) {
227 my ($status) = keys %posting_statuses;
228 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
232 $aux_date = '' if $date eq $aux_date;
233 $code =~ s/[\(\)]+// if defined $code;
234 $payee =~ s/(?: )|\t;/ ;/g if defined $payee; # don't turn into a note
236 my $has_code = defined $code && $code ne '';
237 my $has_payee = defined $payee && $payee ne '';
238 my $has_note = defined $note && $note ne '';
240 push @out, join('', $date,
241 $aux_date && "=${aux_date}",
242 $status_symbol && " ${status_symbol}",
243 $has_code && " (${code})",
244 $has_payee && " ${payee}",
245 $has_note && $has_payee && " ; ${note}",
247 if ($has_note && !$has_payee) {
248 push @out, " ; ${note}";
251 my $metadata = $transaction->{metadata
} || {};
252 for my $key (sort keys %$metadata) {
253 my $value = $self->_format_string($metadata->{$key}) ;
254 push @out, " ; ${key}: ${value}";
257 for my $posting (@postings) {
260 my $posting_status_symbol = '';
261 if (!$status_symbol) {
262 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
265 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
266 push @line, sprintf("\%-${account_width}s", $posting->{account
});
268 if (defined $posting->{amount
}) {
269 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
270 my $lot = $posting->{lot
} || {};
271 if (my $lot_price = $lot->{price
} // $posting->{lot_price
}) {
272 my $is_fixed = $lot_price->{fixed
} // $posting->{lot_fixed
};
273 my $fixed_symbol = $is_fixed ? '=' : '';
274 push @line, " {${fixed_symbol}",
275 $self->_format_amount($lot_price->{amount
}, $lot_price->{commodity
}),
278 if (my $lot_date = $lot->{date
} // $posting->{lot_date
}) {
279 push @line, " [${lot_date}]";
281 if (my $lot_note = $self->_format_string($lot->{note
} // $posting->{lot_note
} // '')) {
282 $lot_note =~ s/[\(\)]+//; # cleanup
283 $lot_note =~ s/^\@+//;
284 push @line, " (${lot_note})" if $lot_note;
286 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
287 my $is_total = defined $posting->{total_cost
};
288 my $cost_symbol = $is_total ? '@@' : '@';
289 push @line, ' ', $cost_symbol, ' ',
290 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
293 my $posting_date = $posting->{date
} || '';
294 my $posting_aux_date = $posting->{aux_date
} || '';
295 my $posting_note = $self->_format_string($posting->{note
} // $posting->{memo
} // '');
296 $posting_date = '' if $posting_date eq $date;
297 $posting_aux_date = '' if $posting_aux_date eq $aux_date;
298 $posting_note = '' if $has_note && $posting_note eq $note;
299 my $has_posting_note = defined $posting_note && $posting_note ne '';
300 if ($posting_date || $posting_aux_date || $has_posting_note) {
301 if ($posting_date || $posting_aux_date) {
302 $posting_note = sprintf('[%s%s]%s',
304 $posting_aux_date && "=${posting_aux_date}",
305 $has_posting_note && " ${posting_note}",
308 push @line, " ; ${posting_note}";
311 push @out, join('', @line);
313 my $metadata = $posting->{metadata
} || {};
314 for my $key (sort keys %$metadata) {
315 my $value = $self->_format_string($metadata->{$key});
316 push @out, " ; ${key}: ${value}";
319 if (my $posting_payee = $self->_format_string($posting->{payee
} // '')) {
320 push @out, " ; Payee: $posting_payee" if !$has_payee || $posting_payee ne $payee;
323 if (my @tags = @{$posting->{tags
} || []}) {
324 push @out, ' ; :'.join(':', @tags).':';
335 my $str = shift // return;
350 return $self->_quote_string($str) if $str !~ /^${SYMBOL}$/;
357 my $commodity = shift or _croak
'Must provide a valid currency';
359 my $format = "\% .$commodity->{frac}f";
360 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
363 my $num = commify
($whole, $commodity->{gchar
});
364 if ($commodity->{frac
}) {
365 $num .= $commodity->{dchar
} . $fraction;
368 my $symbol = $self->_format_symbol($commodity->{symbol
});
369 $num = $commodity->{syprf
} ? "$symbol $num" : "$num $symbol";