]>
Dogcows Code - chaz/homebank2ledger/blob - lib/App/HomeBank2Ledger/Formatter/Ledger.pm
332be8ca9d6303386497d187693debf78cd0b40a
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);
19 use parent
'App::HomeBank2Ledger::Formatter';
21 our $VERSION = '9999.999'; # VERSION
23 my %STATUS_SYMBOLS = (
33 $self->_format_header,
34 $self->_format_accounts($ledger),
35 $self->_format_commodities($ledger),
36 $self->_format_payees($ledger),
37 $self->_format_tags($ledger),
38 $self->_format_transactions($ledger),
41 return join($/, @out);
49 my $file = $self->file;
50 push @out, "; Converted from $file using homebank2ledger ${VERSION}";
52 if (my $name = $self->name) {
53 push @out, "; Name: $name";
61 sub _format_accounts
{
67 push @out, map { "account $_" } sort @{$ledger->accounts};
73 sub _format_commodities
{
79 for my $commodity (@{$ledger->commodities}) {
80 push @out, "commodity $commodity->{symbol}";
81 push @out, " note $commodity->{name}" if $commodity->{name
};
82 push @out, " format $commodity->{format}" if $commodity->{format
};
83 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
97 push @out, map { "payee $_" } sort @{$ledger->payees};
109 push @out, map { "tag $_" } sort @{$ledger->tags};
115 sub _format_transactions
{
121 for my $transaction (@{$ledger->transactions}) {
122 push @out, $self->_format_transaction($transaction);
128 sub _format_transaction
{
130 my $transaction = shift;
132 my $account_width = $self->account_width;
134 my $date = $transaction->{date
};
135 my $status = $transaction->{status
};
136 my $payee = $transaction->{payee
} || 'No Payee TODO';
137 my $memo = $transaction->{memo
} || '';
138 my @postings = @{$transaction->{postings
}};
142 # figure out the Ledger transaction status
143 my $status_symbol = $STATUS_SYMBOLS{$status || ''};
144 if (!$status_symbol) {
145 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
146 if (keys(%posting_statuses) == 1) {
147 my ($status) = keys %posting_statuses;
148 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
149 $status_symbol .= ' ' if $status_symbol;
153 my $symbol = $status_symbol ? "${status_symbol} " : '';
154 push @out, "${date} ${symbol}${payee} ; $memo";
155 $out[-1] =~ s/\h+$//;
157 for my $posting (@postings) {
160 my $posting_status_symbol = '';
161 if (!$status_symbol) {
162 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
165 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
166 push @line, sprintf("\%-${account_width}s", $posting->{account
});
168 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
}) if defined $posting->{amount
};
170 push @out, join('', @line);
171 $out[-1] =~ s/\h+$//;
173 if (my $payee = $posting->{payee
}) {
174 push @out, " ; Payee: $payee";
177 if (my @tags = @{$posting->{tags
} || []}) {
178 push @out, " ; :".join(':', @tags).":";
190 my $commodity = shift;
192 # _croak 'Must provide a valid currency' if !$commodity;
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}";
This page took 0.049478 seconds and 3 git commands to generate.