]>
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.010'; # VERSION
15 my %STATUS_SYMBOLS = (
19 my $SYMBOL = qr![^\s\d.,;:?\!\
-+*/^&|=\
<\
>\
[\
]\
(\
)\
{\
}\
@]+!;
21 sub _croak
{ require Carp
; Carp
::croak
(@_) }
29 $self->format_accounts($ledger),
30 $self->format_commodities($ledger),
31 $self->format_payees($ledger),
32 $self->format_tags($ledger),
33 $self->format_transactions($ledger),
36 return join($/, map { rtrim
($_) } @out);
45 if (my $name = $self->name) {
46 push @out, "; Name: $name";
48 if (my $file = $self->file) {
49 push @out, "; File: $file";
64 push @out, map { "account $_" } sort @{$ledger->accounts};
71 sub format_commodities
{
77 for my $commodity (@{$ledger->commodities}) {
78 push @out, "commodity $commodity->{symbol}";
79 push @out, " note $commodity->{name}" if $commodity->{name
};
80 push @out, " format $commodity->{format}" if $commodity->{format
};
81 push @out, " alias $commodity->{iso}" if $commodity->{iso
};
96 push @out, map { "payee $_" } sort @{$ledger->payees};
109 push @out, map { "tag $_" } sort @{$ledger->tags};
116 sub format_transactions
{
122 for my $transaction (@{$ledger->transactions}) {
123 push @out, $self->_format_transaction($transaction);
129 sub _format_transaction
{
131 my $transaction = shift;
133 my $account_width = $self->account_width;
135 my $date = $transaction->{date
} or _croak
'Transaction date is required';
136 my $aux_date = $transaction->{aux_date
} || $transaction->{effective_date
} || '';
137 my $status = $transaction->{status
} // '';
138 my $code = $transaction->{code
};
139 my $payee = $self->_format_string($transaction->{payee
});
140 my $note = $self->_format_string($transaction->{note
} // $transaction->{memo
});
141 my @postings = @{$transaction->{postings
} || _croak
'At least one transaction posting is required'};
145 # figure out the Ledger transaction status
146 my $status_symbol = $STATUS_SYMBOLS{$status};
147 if (!$status_symbol) {
148 my %posting_statuses = map { ($_->{status
} || '') => 1 } @postings;
149 if (keys(%posting_statuses) == 1) {
150 my ($status) = keys %posting_statuses;
151 $status_symbol = $STATUS_SYMBOLS{$status || 'none'} || '';
155 $aux_date = '' if $date eq $aux_date;
156 $code =~ s/[\(\)]+// if defined $code;
157 $payee =~ s/(?: )|\t;/ ;/g if defined $payee; # don't turn into a note
159 my $has_code = defined $code && $code ne '';
160 my $has_payee = defined $payee && $payee ne '';
161 my $has_note = defined $note && $note ne '';
163 push @out, join('', $date,
164 $aux_date && "=${aux_date}",
165 $status_symbol && " ${status_symbol}",
166 $has_code && " (${code})",
167 $has_payee && " ${payee}",
168 $has_note && $has_payee && " ; ${note}",
170 if ($has_note && !$has_payee) {
171 push @out, " ; ${note}";
174 my $metadata = $transaction->{metadata
} || {};
175 for my $key (sort keys %$metadata) {
176 my $value = $self->_format_string($metadata->{$key}) ;
177 push @out, " ; ${key}: ${value}";
180 for my $posting (@postings) {
183 my $posting_status_symbol = '';
184 if (!$status_symbol) {
185 $posting_status_symbol = $STATUS_SYMBOLS{$posting->{status
} || ''} || '';
188 push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' ');
189 push @line, sprintf("\%-${account_width}s", $posting->{account
});
191 if (defined $posting->{amount
}) {
192 push @line, $self->_format_amount($posting->{amount
}, $posting->{commodity
});
193 my $lot = $posting->{lot
} || {};
194 if (my $lot_price = $lot->{price
} // $posting->{lot_price
}) {
195 my $is_fixed = $lot_price->{fixed
} // $posting->{lot_fixed
};
196 my $fixed_symbol = $is_fixed ? '=' : '';
197 push @line, " {${fixed_symbol}",
198 $self->_format_amount($lot_price->{amount
}, $lot_price->{commodity
}),
201 if (my $lot_date = $lot->{date
} // $posting->{lot_date
}) {
202 push @line, " [${lot_date}]";
204 if (my $lot_note = $self->_format_string($lot->{note
} // $posting->{lot_note
} // '')) {
205 $lot_note =~ s/[\(\)]+//; # cleanup
206 $lot_note =~ s/^\@+//;
207 push @line, " (${lot_note})" if $lot_note;
209 if (my $cost = $posting->{total_cost
} // $posting->{cost
}) {
210 my $is_total = defined $posting->{total_cost
};
211 my $cost_symbol = $is_total ? '@@' : '@';
212 push @line, ' ', $cost_symbol, ' ',
213 $self->_format_amount($cost->{amount
}, $cost->{commodity
});
216 my $posting_date = $posting->{date
} || '';
217 my $posting_aux_date = $posting->{aux_date
} || '';
218 my $posting_note = $self->_format_string($posting->{note
} // $posting->{memo
} // '');
219 $posting_date = '' if $posting_date eq $date;
220 $posting_aux_date = '' if $posting_aux_date eq $aux_date;
221 $posting_note = '' if $has_note && $posting_note eq $note;
222 my $has_posting_note = defined $posting_note && $posting_note ne '';
223 if ($posting_date || $posting_aux_date || $has_posting_note) {
224 if ($posting_date || $posting_aux_date) {
225 $posting_note = sprintf('[%s%s]%s',
227 $posting_aux_date && "=${posting_aux_date}",
228 $has_posting_note && " ${posting_note}",
231 push @line, " ; ${posting_note}";
234 push @out, join('', @line);
236 my $metadata = $posting->{metadata
} || {};
237 for my $key (sort keys %$metadata) {
238 my $value = $self->_format_string($metadata->{$key});
239 push @out, " ; ${key}: ${value}";
242 if (my $posting_payee = $self->_format_string($posting->{payee
} // '')) {
243 push @out, " ; Payee: $posting_payee" if !$has_payee || $posting_payee ne $payee;
246 if (my @tags = @{$posting->{tags
} || []}) {
247 push @out, ' ; :'.join(':', @tags).':';
258 my $str = shift // return;
273 return $self->_quote_string($str) if $str !~ /^${SYMBOL}$/;
280 my $commodity = shift or _croak
'Must provide a valid currency';
282 my $format = "\% .$commodity->{frac}f";
283 my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
286 my $num = commify
($whole, $commodity->{gchar
});
287 if ($commodity->{frac
}) {
288 $num .= $commodity->{dchar
} . $fraction;
291 my $symbol = $self->_format_symbol($commodity->{symbol
});
292 $num = $commodity->{syprf
} ? "$symbol $num" : "$num $symbol";
307 App::HomeBank2Ledger::Formatter::Ledger - Ledger formatter
315 This is a formatter for L<Ledger|https://www.ledger-cli.org/>.
321 @lines = $formatter->format_header;
323 Get formatted header. For example,
326 ; File: path/to/finances.xhb
328 =head2 format_accounts
330 @lines = $formatter->format_accounts($ledger);
332 Get formatted accounts. For example,
334 account Assets:Bank:Credit Union:Savings
335 account Assets:Bank:Credit Union:Checking
338 =head2 format_commodities
340 @lines = $formatter->format_commodities($ledger);
342 Get formattted commodities. For example,
352 @lines = $formatter->format_payees($ledger);
354 Get formatted payees. For example,
361 @lines = $formatter->format_tags($ledger);
363 Get formatted tags. For example,
368 =head2 format_transactions
370 @lines = $formatter->format_transactions($ledger);
372 Get formatted transactions. For example,
374 2003-02-14 * Opening Balance
375 Assets:Bank:Credit Union:Savings $ 458.21
376 Assets:Bank:Credit Union:Checking $ 194.17
377 Equity:Opening Balances
383 L<App::HomeBank2Ledger::Formatter>
387 Please report any bugs or feature requests on the bugtracker website
388 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
390 When submitting a bug or request, please include a test-file or a
391 patch to an existing test-file that illustrates the bug or desired
396 Charles McGarvey <chazmcgarvey@brokenzipper.com>
398 =head1 COPYRIGHT AND LICENSE
400 This software is Copyright (c) 2019 by Charles McGarvey.
402 This is free software, licensed under:
404 The MIT (X11) License
This page took 0.055295 seconds and 4 git commands to generate.