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>
17 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
19 use parent
'App::HomeBank2Ledger::Formatter';
21 our $VERSION = '9999.999'; # VERSION
23 my %STATUS_SYMBOLS = (
28 sub _croak
{ require Carp
; Carp
::croak
(@_) }
36 $self->format_accounts($ledger),
37 $self->format_commodities($ledger),
38 $self->format_payees($ledger),
39 $self->format_tags($ledger),
40 $self->format_transactions($ledger),
43 return join($/, map { rtrim
($_) } @out);
48 @lines = $formatter->format_header;
50 Get formatted header
. For example
,
53 ; File
: path
/to/finances
.xhb
62 if (my $name = $self->name) {
63 push @out, "; Name: $name";
65 if (my $file = $self->file) {
66 push @out, "; File: $file";
74 =method format_accounts
76 @lines = $formatter->format_accounts($ledger);
78 Get formatted accounts
. For example
,
80 account Assets
:Bank
:Credit Union
:Savings
81 account Assets
:Bank
:Credit Union
:Checking
92 push @out, map { "account $_" } sort @{$ledger->accounts};
98 =method format_commodities
100 @lines = $formatter->format_commodities($ledger);
102 Get formattted commodities
. For example
,
112 sub format_commodities
{
118 for my $commodity (@{$ledger->commodities}) {
119 push @out, "commodity $commodity->{symbol}";
120 push @out, " note $commodity->{name}" if $commodity->{name
};
121 push @out, " format $commodity->{format}" if $commodity->{format
};
122 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
130 =method format_payees
132 @lines = $formatter->format_payees($ledger);
134 Get formatted payees
. For example
,
147 push @out, map { "payee $_" } sort @{$ledger->payees};
155 @lines = $formatter->format_tags($ledger);
157 Get formatted tags
. For example
,
170 push @out, map { "tag $_" } sort @{$ledger->tags};
176 =method format_transactions
178 @lines = $formatter->format_transactions($ledger);
180 Get formatted transactions
. For example
,
182 2003-02-14 * Opening Balance
183 Assets
:Bank
:Credit Union
:Savings
$ 458.21
184 Assets
:Bank
:Credit Union
:Checking
$ 194.17
185 Equity
:Opening Balances
191 sub format_transactions
{
197 for my $transaction (@{$ledger->transactions}) {
198 push @out, $self->_format_transaction($transaction);
204 sub _format_transaction
{
206 my $transaction = shift;
208 my $account_width = $self->account_width;
210 my $date = $transaction->{date
};
211 my $status = $transaction->{status
};
212 my $payee = $self->_format_string($transaction->{payee
} || '');
213 my $memo = $self->_format_string($transaction->{memo
} || '');
214 my @postings = @{$transaction->{postings
}};
218 # figure out the Ledger transaction status
219 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
220 if (!$status_symbol) {
221 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
222 if (keys(%posting_statuses) == 1) {
223 my ($status) = keys %posting_statuses;
224 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
228 $payee =~ s/(?: )|\t;/ ;/g; # don't turn into a memo
230 push @out, sprintf('%s%s%s%s', $date,
231 $status_symbol && " ${status_symbol}",
236 for my $posting (@postings) {
239 my $posting_status_symbol = '';
240 if (!$status_symbol) {
241 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
244 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
245 push @line, sprintf("\%-${account_width}s", $posting->{account
});
247 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
249 push @out, join('', @line);
251 if (my $posting_payee = $posting->{payee
}) {
252 $posting_payee = $self->_format_string($posting_payee);
253 push @out, " ; Payee: $posting_payee" if $posting_payee ne $payee;
256 if (my @tags = @{$posting->{tags
} || []}) {
257 push @out, ' ; :'.join(':', @tags).':';
283 my $commodity = shift or _croak
'Must provide a valid currency';
285 my $format = "\% .$commodity->{frac}f";
286 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
288 my $num = join($commodity->{dchar
}, commify
($whole, $commodity->{gchar
}), $fraction);
290 my $symbol = $commodity->{symbol
};
291 $symbol = $self->_quote_string($symbol) if $symbol =~ /[0-9\s]/;
293 $num = $commodity->{syprf
} ? "$symbol $num" : "$num $symbol";