]>
Dogcows Code - chaz/homebank2ledger/blob - lib/App/HomeBank2Ledger/Formatter/Ledger.pm
1 package App
::HomeBank2Ledger
::Formatter
::Ledger
;
2 # ABSTRACT: Ledger formatter
8 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
10 use parent
'App::HomeBank2Ledger::Formatter';
12 our $VERSION = '0.003'; # VERSION
14 my %STATUS_SYMBOLS = (
19 sub _croak
{ require Carp
; Carp
::croak
(@_) }
26 $self->_format_header,
27 $self->_format_accounts($ledger),
28 $self->_format_commodities($ledger),
29 $self->_format_payees($ledger),
30 $self->_format_tags($ledger),
31 $self->_format_transactions($ledger),
34 return join($/, map { rtrim
($_) } @out);
42 if (my $name = $self->name) {
43 push @out, "; Name: $name";
46 my $file = $self->file;
47 push @out, "; Converted from ${file} using homebank2ledger ${VERSION}";
54 sub _format_accounts
{
60 push @out, map { "account $_" } sort @{$ledger->accounts};
66 sub _format_commodities
{
72 for my $commodity (@{$ledger->commodities}) {
73 push @out, "commodity $commodity->{symbol}";
74 push @out, " note $commodity->{name}" if $commodity->{name
};
75 push @out, " format $commodity->{format}" if $commodity->{format
};
76 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
90 push @out, map { "payee $_" } sort @{$ledger->payees};
102 push @out, map { "tag $_" } sort @{$ledger->tags};
108 sub _format_transactions
{
114 for my $transaction (@{$ledger->transactions}) {
115 push @out, $self->_format_transaction($transaction);
121 sub _format_transaction
{
123 my $transaction = shift;
125 my $account_width = $self->account_width;
127 my $date = $transaction->{date
};
128 my $status = $transaction->{status
};
129 my $payee = $self->_format_string($transaction->{payee
} || '');
130 my $memo = $self->_format_string($transaction->{memo
} || '');
131 my @postings = @{$transaction->{postings
}};
135 # figure out the Ledger transaction status
136 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
137 if (!$status_symbol) {
138 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
139 if (keys(%posting_statuses) == 1) {
140 my ($status) = keys %posting_statuses;
141 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
145 $payee =~ s/(?: )|\t;/ ;/g; # don't turn into a memo
147 push @out, sprintf('%s%s%s%s', $date,
148 $status_symbol && " ${status_symbol}",
153 for my $posting (@postings) {
156 my $posting_status_symbol = '';
157 if (!$status_symbol) {
158 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
161 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
162 push @line, sprintf("\%-${account_width}s", $posting->{account
});
164 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
166 push @out, join('', @line);
168 if (my $payee = $posting->{payee
}) {
169 push @out, ' ; Payee: '.$self->_format_string($payee);
172 if (my @tags = @{$posting->{tags
} || []}) {
173 push @out, " ; :".join(':', @tags).":";
192 my $commodity = shift or _croak
'Must provide a valid currency';
194 my $format = "\% .$commodity->{frac}f";
195 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
197 my $num = join($commodity->{dchar
}, commify
($whole, $commodity->{gchar
}), $fraction);
199 $num = $commodity->{syprf
} ? "$commodity->{symbol} $num" : "$num $commodity->{symbol}";
214 App::HomeBank2Ledger::Formatter::Ledger - Ledger formatter
222 This is a formatter for L<Ledger|https://www.ledger-cli.org/>.
226 L<App::HomeBank2Ledger::Formatter>
230 Please report any bugs or feature requests on the bugtracker website
231 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
233 When submitting a bug or request, please include a test-file or a
234 patch to an existing test-file that illustrates the bug or desired
239 Charles McGarvey <chazmcgarvey@brokenzipper.com>
241 =head1 COPYRIGHT AND LICENSE
243 This software is Copyright (c) 2019 by Charles McGarvey.
245 This is free software, licensed under:
247 The MIT (X11) License
This page took 0.049201 seconds and 5 git commands to generate.