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
,
52 ; Converted from finances
.xhb using homebank2ledger
0.001
61 if (my $name = $self->name) {
62 push @out, "; Name: $name";
65 my $file = $self->file;
66 push @out, "; Converted from ${file} using homebank2ledger ${VERSION}";
73 =method format_accounts
75 @lines = $formatter->format_accounts($ledger);
77 Get formatted accounts
. For example
,
79 account Assets
:Bank
:Credit Union
:Savings
80 account Assets
:Bank
:Credit Union
:Checking
91 push @out, map { "account $_" } sort @{$ledger->accounts};
97 =method format_commodities
99 @lines = $formatter->format_commodities($ledger);
101 Get formattted commodities
. For example
,
111 sub format_commodities
{
117 for my $commodity (@{$ledger->commodities}) {
118 push @out, "commodity $commodity->{symbol}";
119 push @out, " note $commodity->{name}" if $commodity->{name
};
120 push @out, " format $commodity->{format}" if $commodity->{format
};
121 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
129 =method format_payees
131 @lines = $formatter->format_payees($ledger);
133 Get formatted payees
. For example
,
146 push @out, map { "payee $_" } sort @{$ledger->payees};
154 @lines = $formatter->format_tags($ledger);
156 Get formatted tags
. For example
,
169 push @out, map { "tag $_" } sort @{$ledger->tags};
175 =method format_transactions
177 @lines = $formatter->format_transactions($ledger);
179 Get formatted transactions
. For example
,
181 2003-02-14 * Opening Balance
182 Assets
:Bank
:Credit Union
:Savings
$ 458.21
183 Assets
:Bank
:Credit Union
:Checking
$ 194.17
184 Equity
:Opening Balances
190 sub format_transactions
{
196 for my $transaction (@{$ledger->transactions}) {
197 push @out, $self->_format_transaction($transaction);
203 sub _format_transaction
{
205 my $transaction = shift;
207 my $account_width = $self->account_width;
209 my $date = $transaction->{date
};
210 my $status = $transaction->{status
};
211 my $payee = $self->_format_string($transaction->{payee
} || '');
212 my $memo = $self->_format_string($transaction->{memo
} || '');
213 my @postings = @{$transaction->{postings
}};
217 # figure out the Ledger transaction status
218 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
219 if (!$status_symbol) {
220 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
221 if (keys(%posting_statuses) == 1) {
222 my ($status) = keys %posting_statuses;
223 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
227 $payee =~ s/(?: )|\t;/ ;/g; # don't turn into a memo
229 push @out, sprintf('%s%s%s%s', $date,
230 $status_symbol && " ${status_symbol}",
235 for my $posting (@postings) {
238 my $posting_status_symbol = '';
239 if (!$status_symbol) {
240 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
243 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
244 push @line, sprintf("\%-${account_width}s", $posting->{account
});
246 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
248 push @out, join('', @line);
250 if (my $posting_payee = $posting->{payee
}) {
251 $posting_payee = $self->_format_string($posting_payee);
252 push @out, " ; Payee: $posting_payee" if $posting_payee ne $payee;
255 if (my @tags = @{$posting->{tags
} || []}) {
256 push @out, ' ; :'.join(':', @tags).':';
275 my $commodity = shift or _croak
'Must provide a valid currency';
277 my $format = "\% .$commodity->{frac}f";
278 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
280 my $num = join($commodity->{dchar
}, commify
($whole, $commodity->{gchar
}), $fraction);
282 $num = $commodity->{syprf
} ? "$commodity->{symbol} $num" : "$num $commodity->{symbol}";