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 for my $posting (@postings) {
240 my $posting_status_symbol = '';
241 if (!$status_symbol) {
242 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
245 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
246 push @line, sprintf("\%-${account_width}s", $posting->{account
});
248 if (defined $posting->{amount
}) {
249 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
250 if (my $price = $posting->{lot_price
}) {
251 my $is_fixed = $posting->{lot_fixed
};
252 my $fixed_symbol = $is_fixed ? '=' : '';
253 push @line, " {${fixed_symbol}",
254 $self->_format_amount($price->{amount
}, $price->{commodity
}),
257 if (my $lot_date = $posting->{lot_date
}) {
258 push @line, " [$posting->{lot_date}]";
260 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
261 my $is_total = defined $posting->{total_cost
};
262 my $cost_symbol = $is_total ? '@@' : '@';
263 push @line, ' ', $cost_symbol, ' ',
264 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
267 if (my $note = $posting->{note
}) {
268 $note = $self->_format_string($note);
269 push @line, " ; $note" if $note ne $memo;
272 push @out, join('', @line);
274 if (my $posting_payee = $posting->{payee
}) {
275 $posting_payee = $self->_format_string($posting_payee);
276 push @out, " ; Payee: $posting_payee" if $posting_payee ne $payee;
279 if (my @tags = @{$posting->{tags
} || []}) {
280 push @out, ' ; :'.join(':', @tags).':';
306 my $commodity = shift or _croak
'Must provide a valid currency';
308 my $format = "\% .$commodity->{frac}f";
309 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
311 my $num = join($commodity->{dchar
}, commify
($whole, $commodity->{gchar
}), $fraction);
313 my $symbol = $commodity->{symbol
};
314 $symbol = $self->_quote_string($symbol) if $symbol =~ /[0-9\s]/;
316 $num = $commodity->{syprf
} ? "$symbol $num" : "$num $symbol";