X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fhomebank2ledger;a=blobdiff_plain;f=lib%2FApp%2FHomeBank2Ledger%2FFormatter%2FLedger.pm;h=464fa04871b730b2177388d1f14d679de9b47281;hp=e4ecb2a177c89daca2f22334f9cd6ae026ee364c;hb=07329500b7d9a2e8fcfd9eb97f485deaea9f48f7;hpb=d96646d72e6ef66b272e789963f73f91369394a0 diff --git a/lib/App/HomeBank2Ledger/Formatter/Ledger.pm b/lib/App/HomeBank2Ledger/Formatter/Ledger.pm index e4ecb2a..464fa04 100644 --- a/lib/App/HomeBank2Ledger/Formatter/Ledger.pm +++ b/lib/App/HomeBank2Ledger/Formatter/Ledger.pm @@ -11,6 +11,7 @@ L =cut +use v5.10.1; # defined-or use warnings; use strict; @@ -49,7 +50,8 @@ sub format { Get formatted header. For example, - ; Converted from finances.xhb using homebank2ledger 0.001 + ; Name: My Finances + ; File: path/to/finances.xhb =cut @@ -61,9 +63,9 @@ sub format_header { if (my $name = $self->name) { push @out, "; Name: $name"; } - - my $file = $self->file; - push @out, "; Converted from ${file} using homebank2ledger ${VERSION}"; + if (my $file = $self->file) { + push @out, "; File: $file"; + } push @out, ''; @@ -232,6 +234,12 @@ sub _format_transaction { $memo && " ; $memo", ); + my $metadata = $transaction->{metadata} || {}; + for my $key (sort keys %$metadata) { + my $value = $self->_format_string($metadata->{$key}); + push @out, " ; ${key}: ${value}"; + } + for my $posting (@postings) { my @line; @@ -243,16 +251,45 @@ sub _format_transaction { push @line, ($posting_status_symbol ? " $posting_status_symbol " : ' '); push @line, sprintf("\%-${account_width}s", $posting->{account}); push @line, ' '; - push @line, $self->_format_amount($posting->{amount}, $posting->{commodity}) if defined $posting->{amount}; + if (defined $posting->{amount}) { + push @line, $self->_format_amount($posting->{amount}, $posting->{commodity}); + if (my $price = $posting->{lot_price}) { + my $is_fixed = $posting->{lot_fixed}; + my $fixed_symbol = $is_fixed ? '=' : ''; + push @line, " {${fixed_symbol}", + $self->_format_amount($price->{amount}, $price->{commodity}), + '}'; + } + if (my $lot_date = $posting->{lot_date}) { + push @line, " [$posting->{lot_date}]"; + } + if (my $cost = $posting->{total_cost} // $posting->{cost}) { + my $is_total = defined $posting->{total_cost}; + my $cost_symbol = $is_total ? '@@' : '@'; + push @line, ' ', $cost_symbol, ' ', + $self->_format_amount($cost->{amount}, $cost->{commodity}); + } + } + if (my $note = $posting->{note}) { + $note = $self->_format_string($note); + push @line, " ; $note" if $note ne $memo; + } push @out, join('', @line); - if (my $payee = $posting->{payee}) { - push @out, ' ; Payee: '.$self->_format_string($payee); + my $metadata = $posting->{metadata} || {}; + for my $key (sort keys %$metadata) { + my $value = $self->_format_string($metadata->{$key}); + push @out, " ; ${key}: ${value}"; + } + + if (my $posting_payee = $posting->{payee}) { + $posting_payee = $self->_format_string($posting_payee); + push @out, " ; Payee: $posting_payee" if $posting_payee ne $payee; } if (my @tags = @{$posting->{tags} || []}) { - push @out, " ; :".join(':', @tags).":"; + push @out, ' ; :'.join(':', @tags).':'; } } @@ -268,6 +305,13 @@ sub _format_string { return $str; } +sub _quote_string { + my $self = shift; + my $str = shift; + $str =~ s/"/\\"/g; + return "\"$str\""; +} + sub _format_amount { my $self = shift; my $amount = shift; @@ -278,7 +322,10 @@ sub _format_amount { my $num = join($commodity->{dchar}, commify($whole, $commodity->{gchar}), $fraction); - $num = $commodity->{syprf} ? "$commodity->{symbol} $num" : "$num $commodity->{symbol}"; + my $symbol = $commodity->{symbol}; + $symbol = $self->_quote_string($symbol) if $symbol =~ /[0-9\s]/; + + $num = $commodity->{syprf} ? "$symbol $num" : "$num $symbol"; return $num; }