]>
Dogcows Code - chaz/homebank2ledger/blob - Formatter/Ledger.pm
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
(@_) }
35 $self->_format_header,
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);
51 if (my $name = $self->name) {
52 push @out, "; Name: $name";
55 my $file = $self->file;
56 push @out, "; Converted from ${file} using homebank2ledger ${VERSION}";
63 sub _format_accounts
{
69 push @out, map { "account $_" } sort @{$ledger->accounts};
75 sub _format_commodities
{
81 for my $commodity (@{$ledger->commodities}) {
82 push @out, "commodity $commodity->{symbol}";
83 push @out, " note $commodity->{name}" if $commodity->{name
};
84 push @out, " format $commodity->{format}" if $commodity->{format
};
85 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
99 push @out, map { "payee $_" } sort @{$ledger->payees};
111 push @out, map { "tag $_" } sort @{$ledger->tags};
117 sub _format_transactions
{
123 for my $transaction (@{$ledger->transactions}) {
124 push @out, $self->_format_transaction($transaction);
130 sub _format_transaction
{
132 my $transaction = shift;
134 my $account_width = $self->account_width;
136 my $date = $transaction->{date
};
137 my $status = $transaction->{status
};
138 my $payee = $self->_format_string($transaction->{payee
} || '');
139 my $memo = $self->_format_string($transaction->{memo
} || '');
140 my @postings = @{$transaction->{postings
}};
144 # figure out the Ledger transaction status
145 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
146 if (!$status_symbol) {
147 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
148 if (keys(%posting_statuses) == 1) {
149 my ($status) = keys %posting_statuses;
150 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
154 $payee =~ s/(?: )|\t;/ ;/g; # don't turn into a memo
156 push @out, sprintf('%s%s%s%s', $date,
157 $status_symbol && " ${status_symbol}",
162 for my $posting (@postings) {
165 my $posting_status_symbol = '';
166 if (!$status_symbol) {
167 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
170 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
171 push @line, sprintf("\%-${account_width}s", $posting->{account
});
173 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
175 push @out, join('', @line);
177 if (my $payee = $posting->{payee
}) {
178 push @out, ' ; Payee: '.$self->_format_string($payee);
181 if (my @tags = @{$posting->{tags
} || []}) {
182 push @out, " ; :".join(':', @tags).":";
201 my $commodity = shift or _croak
'Must provide a valid currency';
203 my $format = "\% .$commodity->{frac}f";
204 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
206 my $num = join($commodity->{dchar
}, commify
($whole, $commodity->{gchar
}), $fraction);
208 $num = $commodity->{syprf
} ? "$commodity->{symbol} $num" : "$num $commodity->{symbol}";
This page took 0.044956 seconds and 4 git commands to generate.