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 = (
29 sub _croak
{ require Carp
; Carp
::croak
(@_) }
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),
44 return join($/, map { rtrim
($_) } @out);
49 @lines = $formatter->format_header;
51 Get formatted header
. For example
,
54 ; File
: path
/to/finances
.xhb
63 if (my $name = $self->name) {
64 push @out, "; Name: $name";
66 if (my $file = $self->file) {
67 push @out, "; File: $file";
75 =method format_accounts
77 @lines = $formatter->format_accounts($ledger);
79 Get formatted accounts
. For example
,
81 account Assets
:Bank
:Credit Union
:Savings
82 account Assets
:Bank
:Credit Union
:Checking
93 push @out, map { "account $_" } sort @{$ledger->accounts};
99 =method format_commodities
101 @lines = $formatter->format_commodities($ledger);
103 Get formattted commodities
. For example
,
113 sub format_commodities
{
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
};
131 =method format_payees
133 @lines = $formatter->format_payees($ledger);
135 Get formatted payees
. For example
,
148 push @out, map { "payee $_" } sort @{$ledger->payees};
156 @lines = $formatter->format_tags($ledger);
158 Get formatted tags
. For example
,
171 push @out, map { "tag $_" } sort @{$ledger->tags};
177 =method format_transactions
179 @lines = $formatter->format_transactions($ledger);
181 Get formatted transactions
. For example
,
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
192 sub format_transactions
{
198 for my $transaction (@{$ledger->transactions}) {
199 push @out, $self->_format_transaction($transaction);
205 sub _format_transaction
{
207 my $transaction = shift;
209 my $account_width = $self->account_width;
211 my $date = $transaction->{date
};
212 my $status = $transaction->{status
};
213 my $payee = $self->_format_string($transaction->{payee
} || '');
214 my $memo = $self->_format_string($transaction->{memo
} || '');
215 my @postings = @{$transaction->{postings
}};
219 # figure out the Ledger transaction status
220 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
221 if (!$status_symbol) {
222 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
223 if (keys(%posting_statuses) == 1) {
224 my ($status) = keys %posting_statuses;
225 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
229 $payee =~ s/(?: )|\t;/ ;/g; # don't turn into a memo
231 push @out, sprintf('%s%s%s%s', $date,
232 $status_symbol && " ${status_symbol}",
237 my $metadata = $transaction->{metadata
} || {};
238 for my $key (sort keys %$metadata) {
239 my $value = $self->_format_string($metadata->{$key});
240 push @out, " ; ${key}: ${value}";
243 for my $posting (@postings) {
246 my $posting_status_symbol = '';
247 if (!$status_symbol) {
248 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
251 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
252 push @line, sprintf("\%-${account_width}s", $posting->{account
});
254 if (defined $posting->{amount
}) {
255 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
256 if (my $price = $posting->{lot_price
}) {
257 my $is_fixed = $posting->{lot_fixed
};
258 my $fixed_symbol = $is_fixed ? '=' : '';
259 push @line, " {${fixed_symbol}",
260 $self->_format_amount($price->{amount
}, $price->{commodity
}),
263 if (my $lot_date = $posting->{lot_date
}) {
264 push @line, " [$posting->{lot_date}]";
266 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
267 my $is_total = defined $posting->{total_cost
};
268 my $cost_symbol = $is_total ? '@@' : '@';
269 push @line, ' ', $cost_symbol, ' ',
270 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
273 if (my $note = $posting->{note
}) {
274 $note = $self->_format_string($note);
275 push @line, " ; $note" if $note ne $memo;
278 push @out, join('', @line);
280 my $metadata = $posting->{metadata
} || {};
281 for my $key (sort keys %$metadata) {
282 my $value = $self->_format_string($metadata->{$key});
283 push @out, " ; ${key}: ${value}";
286 if (my $posting_payee = $posting->{payee
}) {
287 $posting_payee = $self->_format_string($posting_payee);
288 push @out, " ; Payee: $posting_payee" if $posting_payee ne $payee;
291 if (my @tags = @{$posting->{tags
} || []}) {
292 push @out, ' ; :'.join(':', @tags).':';
318 my $commodity = shift or _croak
'Must provide a valid currency';
320 my $format = "\% .$commodity->{frac}f";
321 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
324 my $num = commify
($whole, $commodity->{gchar
});
325 if ($commodity->{frac
}) {
326 $num .= $commodity->{dchar
} . $fraction;
329 my $symbol = $commodity->{symbol
};
330 $symbol = $self->_quote_string($symbol) if $symbol =~ /[0-9\s]/;
332 $num = $commodity->{syprf
} ? "$symbol $num" : "$num $symbol";