]> Dogcows Code - chaz/homebank2ledger/blobdiff - lib/App/HomeBank2Ledger/Formatter/Ledger.pm
add support for formatting metadata
[chaz/homebank2ledger] / lib / App / HomeBank2Ledger / Formatter / Ledger.pm
index 3a5a71379b6d6941f58c70e47793ed1e9c43c718..464fa04871b730b2177388d1f14d679de9b47281 100644 (file)
@@ -11,6 +11,7 @@ L<App::HomeBank2Ledger::Formatter>
 
 =cut
 
+use v5.10.1;    # defined-or
 use warnings;
 use strict;
 
@@ -233,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;
 
@@ -244,10 +251,38 @@ 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);
 
+        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;
This page took 0.020547 seconds and 4 git commands to generate.