]> Dogcows Code - chaz/homebank2ledger/blobdiff - lib/App/HomeBank2Ledger/Formatter/Beancount.pm
Version 0.010
[chaz/homebank2ledger] / lib / App / HomeBank2Ledger / Formatter / Beancount.pm
index 2ec6a2c89437bc4b40b39909cce98356ecc12906..48b48b94eb3dc3d64bf2ddd751701bc6dec978c4 100644 (file)
@@ -7,10 +7,11 @@ use warnings;
 use strict;
 
 use App::HomeBank2Ledger::Util qw(commify rtrim);
+use Scalar::Util qw(looks_like_number);
 
 use parent 'App::HomeBank2Ledger::Formatter';
 
-our $VERSION = '0.005'; # VERSION
+our $VERSION = '0.010'; # VERSION
 
 my %STATUS_SYMBOLS = (
     cleared => '*',
@@ -116,7 +117,7 @@ sub _format_transaction {
     my $date        = $transaction->{date};
     my $status      = $transaction->{status};
     my $payee       = $transaction->{payee} || '';
-    my $memo        = $transaction->{memo}  || '';
+    my $memo        = $transaction->{note} // $transaction->{memo} // '';
     my @postings    = @{$transaction->{postings}};
 
     my @out;
@@ -142,6 +143,13 @@ sub _format_transaction {
         $out[-1] .= ' '.join(' ', @tags);
     }
 
+    my $metadata = $transaction->{metadata} || {};
+    for my $key (sort keys %$metadata) {
+        my $value = looks_like_number($metadata->{$key}) ? $metadata->{$key}
+                                                         : $self->_format_string($metadata->{$key});
+        push @out, "    ; ${key}: ${value}";
+    }
+
     for my $posting (@postings) {
         my @line;
 
@@ -157,9 +165,10 @@ sub _format_transaction {
         push @line, '  ';
         if (defined $posting->{amount}) {
             push @line, $self->_format_amount($posting->{amount}, $posting->{commodity});
-            my $lot_price = $posting->{lot_price};
-            my $lot_date  = $posting->{lot_date};
-            my $lot_ref   = $posting->{lot_ref};
+            my $lot = $posting->{lot} || {};
+            my $lot_price = $lot->{price} // $posting->{lot_price};
+            my $lot_date  = $lot->{date}  // $posting->{lot_date};
+            my $lot_ref   = $lot->{ref}   // $posting->{lot_ref};
             if ($lot_price || $lot_date || $lot_ref) {
                 push @line, ' {',
                             join(', ',
@@ -178,6 +187,13 @@ sub _format_transaction {
         }
 
         push @out, join('', @line);
+
+        my $metadata = $posting->{metadata} || {};
+        for my $key (sort keys %$metadata) {
+            my $value = looks_like_number($metadata->{$key}) ? $metadata->{$key}
+                                                             : $self->_format_string($metadata->{$key});
+            push @out, "      ; ${key}: ${value}";
+        }
     }
 
     push @out, '';
@@ -208,9 +224,13 @@ sub _format_amount {
 
     my $format = "\% .$commodity->{frac}f";
     my ($whole, $fraction) = split(/\./, sprintf($format, $amount));
+    $fraction ||= 0;
 
     # beancount doesn't support different notations
-    my $num = join('.', commify($whole), $fraction);
+    my $num = commify($whole);
+    if ($commodity->{frac}) {
+        $num .= ".$fraction";
+    }
 
     $num = "$num $commodity->{iso}";
 
@@ -282,11 +302,11 @@ App::HomeBank2Ledger::Formatter::Beancount - Beancount formatter
 
 =head1 VERSION
 
-version 0.005
+version 0.010
 
 =head1 DESCRIPTION
 
-This is a formatter for L<Beancount|http://furius.ca/beancount/>.
+This is a formatter for L<Beancount|https://beancount.github.io/docs/index.html>.
 
 =head1 METHODS
 
This page took 0.019519 seconds and 4 git commands to generate.