]>
Dogcows Code - chaz/homebank2ledger/blob - lib/App/HomeBank2Ledger/Formatter/Ledger.pm
1 package App
::HomeBank2Ledger
::Formatter
::Ledger
;
2 # ABSTRACT: Ledger formatter
5 use v5
.10
.1; # defined-or
9 use App
::HomeBank2Ledger
::Util
qw(commify rtrim);
11 use parent
'App::HomeBank2Ledger::Formatter';
13 our $VERSION = '0.008'; # VERSION
15 my %STATUS_SYMBOLS = (
20 sub _croak
{ require Carp
; Carp
::croak
(@_) }
28 $self->format_accounts($ledger),
29 $self->format_commodities($ledger),
30 $self->format_payees($ledger),
31 $self->format_tags($ledger),
32 $self->format_transactions($ledger),
35 return join($/, map { rtrim
($_) } @out);
44 if (my $name = $self->name) {
45 push @out, "; Name: $name";
47 if (my $file = $self->file) {
48 push @out, "; File: $file";
63 push @out, map { "account $_" } sort @{$ledger->accounts};
70 sub format_commodities
{
76 for my $commodity (@{$ledger->commodities}) {
77 push @out, "commodity $commodity->{symbol}";
78 push @out, " note $commodity->{name}" if $commodity->{name
};
79 push @out, " format $commodity->{format}" if $commodity->{format
};
80 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
95 push @out, map { "payee $_" } sort @{$ledger->payees};
108 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 = $self->_format_string($transaction->{payee
} || '');
137 my $memo = $self->_format_string($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'} || '';
152 $payee =~ s/(?: )|\t;/ ;/g; # don't turn into a memo
154 push @out, sprintf('%s%s%s%s', $date,
155 $status_symbol && " ${status_symbol}",
160 my $metadata = $transaction->{metadata
} || {};
161 for my $key (sort keys %$metadata) {
162 my $value = $self->_format_string($metadata->{$key});
163 push @out, " ; ${key}: ${value}";
166 for my $posting (@postings) {
169 my $posting_status_symbol = '';
170 if (!$status_symbol) {
171 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
174 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
175 push @line, sprintf("\%-${account_width}s", $posting->{account
});
177 if (defined $posting->{amount
}) {
178 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
179 if (my $price = $posting->{lot_price
}) {
180 my $is_fixed = $posting->{lot_fixed
};
181 my $fixed_symbol = $is_fixed ? '=' : '';
182 push @line, " {${fixed_symbol}",
183 $self->_format_amount($price->{amount
}, $price->{commodity
}),
186 if (my $lot_date = $posting->{lot_date
}) {
187 push @line, " [$posting->{lot_date}]";
189 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
190 my $is_total = defined $posting->{total_cost
};
191 my $cost_symbol = $is_total ? '@@' : '@';
192 push @line, ' ', $cost_symbol, ' ',
193 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
196 if (my $note = $posting->{note
}) {
197 $note = $self->_format_string($note);
198 push @line, " ; $note" if $note ne $memo;
201 push @out, join('', @line);
203 my $metadata = $posting->{metadata
} || {};
204 for my $key (sort keys %$metadata) {
205 my $value = $self->_format_string($metadata->{$key});
206 push @out, " ; ${key}: ${value}";
209 if (my $posting_payee = $posting->{payee
}) {
210 $posting_payee = $self->_format_string($posting_payee);
211 push @out, " ; Payee: $posting_payee" if $posting_payee ne $payee;
214 if (my @tags = @{$posting->{tags
} || []}) {
215 push @out, ' ; :'.join(':', @tags).':';
241 my $commodity = shift or _croak
'Must provide a valid currency';
243 my $format = "\% .$commodity->{frac}f";
244 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
247 my $num = commify
($whole, $commodity->{gchar
});
248 if ($commodity->{frac
}) {
249 $num .= $commodity->{dchar
} . $fraction;
252 my $symbol = $commodity->{symbol
};
253 $symbol = $self->_quote_string($symbol) if $symbol =~ /[0-9\s]/;
255 $num = $commodity->{syprf
} ? "$symbol $num" : "$num $symbol";
270 App::HomeBank2Ledger::Formatter::Ledger - Ledger formatter
278 This is a formatter for L<Ledger|https://www.ledger-cli.org/>.
284 @lines = $formatter->format_header;
286 Get formatted header. For example,
289 ; File: path/to/finances.xhb
291 =head2 format_accounts
293 @lines = $formatter->format_accounts($ledger);
295 Get formatted accounts. For example,
297 account Assets:Bank:Credit Union:Savings
298 account Assets:Bank:Credit Union:Checking
301 =head2 format_commodities
303 @lines = $formatter->format_commodities($ledger);
305 Get formattted commodities. For example,
315 @lines = $formatter->format_payees($ledger);
317 Get formatted payees. For example,
324 @lines = $formatter->format_tags($ledger);
326 Get formatted tags. For example,
331 =head2 format_transactions
333 @lines = $formatter->format_transactions($ledger);
335 Get formatted transactions. For example,
337 2003-02-14 * Opening Balance
338 Assets:Bank:Credit Union:Savings $ 458.21
339 Assets:Bank:Credit Union:Checking $ 194.17
340 Equity:Opening Balances
346 L<App::HomeBank2Ledger::Formatter>
350 Please report any bugs or feature requests on the bugtracker website
351 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
353 When submitting a bug or request, please include a test-file or a
354 patch to an existing test-file that illustrates the bug or desired
359 Charles McGarvey <chazmcgarvey@brokenzipper.com>
361 =head1 COPYRIGHT AND LICENSE
363 This software is Copyright (c) 2019 by Charles McGarvey.
365 This is free software, licensed under:
367 The MIT (X11) License
This page took 0.055072 seconds and 4 git commands to generate.