]>
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.005'; # 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 for my $posting (@postings) {
163 my $posting_status_symbol = '';
164 if (!$status_symbol) {
165 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
168 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
169 push @line, sprintf("\%-${account_width}s", $posting->{account
});
171 if (defined $posting->{amount
}) {
172 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
173 if (my $price = $posting->{lot_price
}) {
174 my $is_fixed = $posting->{lot_fixed
};
175 my $fixed_symbol = $is_fixed ? '=' : '';
176 push @line, " {${fixed_symbol}",
177 $self->_format_amount($price->{amount
}, $price->{commodity
}),
180 if (my $lot_date = $posting->{lot_date
}) {
181 push @line, " [$posting->{lot_date}]";
183 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
184 my $is_total = defined $posting->{total_cost
};
185 my $cost_symbol = $is_total ? '@@' : '@';
186 push @line, ' ', $cost_symbol, ' ',
187 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
190 if (my $note = $posting->{note
}) {
191 $note = $self->_format_string($note);
192 push @line, " ; $note" if $note ne $memo;
195 push @out, join('', @line);
197 if (my $posting_payee = $posting->{payee
}) {
198 $posting_payee = $self->_format_string($posting_payee);
199 push @out, " ; Payee: $posting_payee" if $posting_payee ne $payee;
202 if (my @tags = @{$posting->{tags
} || []}) {
203 push @out, ' ; :'.join(':', @tags).':';
229 my $commodity = shift or _croak
'Must provide a valid currency';
231 my $format = "\% .$commodity->{frac}f";
232 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
234 my $num = join($commodity->{dchar
}, commify
($whole, $commodity->{gchar
}), $fraction);
236 my $symbol = $commodity->{symbol
};
237 $symbol = $self->_quote_string($symbol) if $symbol =~ /[0-9\s]/;
239 $num = $commodity->{syprf
} ? "$symbol $num" : "$num $symbol";
254 App::HomeBank2Ledger::Formatter::Ledger - Ledger formatter
262 This is a formatter for L<Ledger|https://www.ledger-cli.org/>.
268 @lines = $formatter->format_header;
270 Get formatted header. For example,
273 ; File: path/to/finances.xhb
275 =head2 format_accounts
277 @lines = $formatter->format_accounts($ledger);
279 Get formatted accounts. For example,
281 account Assets:Bank:Credit Union:Savings
282 account Assets:Bank:Credit Union:Checking
285 =head2 format_commodities
287 @lines = $formatter->format_commodities($ledger);
289 Get formattted commodities. For example,
299 @lines = $formatter->format_payees($ledger);
301 Get formatted payees. For example,
308 @lines = $formatter->format_tags($ledger);
310 Get formatted tags. For example,
315 =head2 format_transactions
317 @lines = $formatter->format_transactions($ledger);
319 Get formatted transactions. For example,
321 2003-02-14 * Opening Balance
322 Assets:Bank:Credit Union:Savings $ 458.21
323 Assets:Bank:Credit Union:Checking $ 194.17
324 Equity:Opening Balances
330 L<App::HomeBank2Ledger::Formatter>
334 Please report any bugs or feature requests on the bugtracker website
335 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
337 When submitting a bug or request, please include a test-file or a
338 patch to an existing test-file that illustrates the bug or desired
343 Charles McGarvey <chazmcgarvey@brokenzipper.com>
345 =head1 COPYRIGHT AND LICENSE
347 This software is Copyright (c) 2019 by Charles McGarvey.
349 This is free software, licensed under:
351 The MIT (X11) License
This page took 0.058954 seconds and 5 git commands to generate.