1 package App
::HomeBank2Ledger
;
2 # ABSTRACT: A tool to convert HomeBank files to Ledger format
5 use warnings FATAL
=> 'all'; # temp fatal all
8 use App
::HomeBank2Ledger
::Formatter
;
9 use App
::HomeBank2Ledger
::Ledger
;
11 use Getopt
::Long
2.38 qw(GetOptionsFromArray);
14 our $VERSION = '0.002'; # VERSION
16 my %ACCOUNT_TYPES = ( # map HomeBank account types to Ledger accounts
17 bank
=> 'Assets:Bank',
18 cash
=> 'Assets:Cash',
19 asset
=> 'Assets:Fixed Assets',
20 creditcard
=> 'Liabilities:Credit Card',
21 liability
=> 'Liabilities',
22 stock
=> 'Assets:Stock',
23 mutualfund
=> 'Assets:Mutual Fund',
25 expense
=> 'Expenses',
28 my %STATUS_SYMBOLS = (
30 reconciled
=> 'cleared',
33 my $UNKNOWN_ACCOUNT = 'Assets:Unknown';
34 my $OPENING_BALANCES_ACCOUNT = 'Equity:Opening Balances';
39 my $self = bless {}, $class;
41 my $opts = $self->parse_args(@_);
43 if ($opts->{version
}) {
44 print "homebank2ledger ${VERSION}\n";
48 pod2usage
(-exitval
=> 0, -verbose
=> 99, -sections
=> [qw(NAME SYNOPSIS OPTIONS)]);
50 if ($opts->{manual
}) {
51 pod2usage
(-exitval
=> 0, -verbose
=> 2);
54 my $homebank = File
::HomeBank-
>new(file
=> $opts->{input
});
56 my $formatter = eval { $self->formatter($homebank, $opts) };
58 if ($err =~ /^Invalid formatter/) {
59 print STDERR
"Invalid format: $opts->{format}\n";
65 my $ledger = $self->convert_homebank_to_ledger($homebank, $opts);
67 $self->print_to_file($formatter->format($ledger), $opts->{output
});
76 my $opts = shift || {};
78 return App
::HomeBank2Ledger
::Formatter-
>new(
79 type
=> $opts->{format
},
80 account_width
=> $opts->{account_width
},
81 name
=> $homebank->title,
82 file
=> $homebank->file,
87 sub convert_homebank_to_ledger
{
90 my $opts = shift || {};
92 my $ledger = App
::HomeBank2Ledger
::Ledger-
>new;
94 my $transactions = $homebank->sorted_transactions;
95 my $accounts = $homebank->accounts;
96 my $categories = $homebank->categories;
98 # determine full Ledger account names
99 for my $account (@$accounts) {
100 my $type = $ACCOUNT_TYPES{$account->{type
}} || $UNKNOWN_ACCOUNT;
101 $account->{ledger_name
} = "${type}:$account->{name}";
103 for my $category (@$categories) {
104 my $type = $category->{flags
}{income
} ? 'Income' : 'Expenses';
105 my $full_name = $homebank->full_category_name($category->{key
});
106 $category->{ledger_name
} = "${type}:${full_name}";
109 # handle renaming and marking excluded accounts
110 for my $item (@$accounts, @$categories) {
111 while (my ($re, $replacement) = each %{$opts->{rename_accounts
}}) {
112 $item->{ledger_name
} =~ s/$re/$replacement/;
114 for my $re (@{$opts->{exclude_accounts
}}) {
115 $item->{excluded
} = 1 if $item->{ledger_name
} =~ /$re/;
119 my $has_initial_balance = grep { $_->{initial
} && !$_->{excluded
} } @$accounts;
121 if ($opts->{accounts
}) {
122 my @accounts = map { $_->{ledger_name
} } grep { !$_->{excluded
} } @$accounts, @$categories;
124 push @accounts, $opts->{default_account
};
125 push @accounts, $OPENING_BALANCES_ACCOUNT if $has_initial_balance;
127 $ledger->add_accounts(@accounts);
130 if ($opts->{payees
}) {
131 my $payees = $homebank->payees;
132 my @payees = map { $_->{name
} } @$payees;
134 $ledger->add_payees(@payees);
138 my $tags = $homebank->tags;
140 $ledger->add_tags(@$tags);
145 for my $currency (@{$homebank->currencies}) {
147 symbol
=> $currency->{symbol
},
148 format
=> $homebank->format_amount(1_000, $currency),
149 iso
=> $currency->{iso
},
150 name
=> $currency->{name
},
152 $commodities{$currency->{key
}} = {
154 syprf
=> $currency->{syprf
},
155 dchar
=> $currency->{dchar
},
156 gchar
=> $currency->{gchar
},
157 frac
=> $currency->{frac
},
160 $ledger->add_commodities($commodity) if $opts->{commodities
};
163 if ($has_initial_balance) {
164 # transactions are sorted, so the first transaction is the oldest
165 my $first_date = $opts->{opening_date
} || $transactions->[0]{date
};
166 if ($first_date !~ /^\d{4}-\d{2}-\d{2}$/) {
167 die "Opening date must be in the form YYYY-MM-DD.\n";
172 for my $account (@$accounts) {
173 next if !$account->{initial
} || $account->{excluded
};
176 account
=> $account->{ledger_name
},
177 amount
=> $account->{initial
},
178 commodity
=> $commodities{$account->{currency
}},
183 account
=> $OPENING_BALANCES_ACCOUNT,
186 $ledger->add_transactions({
188 payee
=> 'Opening Balance',
190 postings
=> \
@postings,
197 for my $transaction (@$transactions) {
198 next if $seen{$transaction->{transfer_key
} || ''};
200 my $account = $homebank->find_account_by_key($transaction->{account
});
201 my $amount = $transaction->{amount
};
202 my $status = $STATUS_SYMBOLS{$transaction->{status
} || ''} || '';
203 my $paymode = $transaction->{paymode
} || ''; # internaltransfer
204 my $memo = $transaction->{wording
} || '';
205 my $payee = $homebank->find_payee_by_key($transaction->{payee
});
206 my $tags = _split_tags
($transaction->{tags
});
211 account
=> $account->{ledger_name
},
213 commodity
=> $commodities{$account->{currency
}},
214 payee
=> $payee->{name
},
220 if ($paymode eq 'internaltransfer') {
221 my $paired_transaction = $homebank->find_transaction_transfer_pair($transaction);
223 my $dst_account = $homebank->find_account_by_key($transaction->{dst_account
});
225 if ($paired_transaction) {
226 $dst_account = $homebank->find_account_by_key($paired_transaction->{account
});
229 warn "Skipping internal transfer transaction with no destination account.\n";
234 $seen{$transaction->{transfer_key
}}++ if $transaction->{transfer_key
};
235 $seen{$paired_transaction->{transfer_key
}}++ if $paired_transaction->{transfer_key
};
237 my $paired_payee = $homebank->find_payee_by_key($paired_transaction->{payee
});
240 account
=> $dst_account->{ledger_name
},
241 amount
=> $paired_transaction->{amount
} || -$transaction->{amount
},
242 commodity
=> $commodities{$dst_account->{currency
}},
243 payee
=> $paired_payee->{name
},
244 memo
=> $paired_transaction->{wording
} || '',
245 status
=> $STATUS_SYMBOLS{$paired_transaction->{status
} || ''} || $status,
246 tags
=> _split_tags
($paired_transaction->{tags
}),
249 elsif ($transaction->{flags
}{split}) {
250 my @amounts = split(/\|\|/, $transaction->{split_amount
} || '');
251 my @memos = split(/\|\|/, $transaction->{split_memo
} || '');
252 my @categories = split(/\|\|/, $transaction->{split_category
} || '');
254 for (my $i = 0; $amounts[$i]; ++$i) {
255 my $amount = -$amounts[$i];
256 my $category = $homebank->find_category_by_key($categories[$i]);
257 my $memo = $memos[$i] || '';
258 my $other_account = $category ? $category->{ledger_name
} : $opts->{default_account
};
261 account
=> $other_account,
262 commodity
=> $commodities{$account->{currency
}},
264 payee
=> $payee->{name
},
271 else { # with or without category
272 my $category = $homebank->find_category_by_key($transaction->{category
});
273 my $other_account = $category ? $category->{ledger_name
} : $opts->{default_account
};
275 account
=> $other_account,
276 commodity
=> $commodities{$account->{currency
}},
277 amount
=> -$transaction->{amount
},
278 payee
=> $payee->{name
},
285 # skip excluded accounts
286 for my $posting (@postings) {
287 for my $re (@{$opts->{exclude_accounts
}}) {
288 next TRANSACTION
if $posting->{account
} =~ /$re/;
292 $ledger->add_transactions({
293 date
=> $transaction->{date
},
294 payee
=> $payee->{name
},
296 postings
=> \
@postings,
307 my $filepath = shift;
309 my $out_fh = \
*STDOUT
;
311 open($out_fh, '>', $filepath) or die "open failed: $!";
334 default_account
=> 'Expenses:No Category',
335 rename_accounts
=> {},
336 exclude_accounts
=> [],
339 GetOptionsFromArray
(\
@args,
340 'version|V' => \
$opts{version
},
341 'help|h|?' => \
$opts{help
},
342 'manual|man' => \
$opts{manual
},
343 'input|file|i=s' => \
$opts{input
},
344 'output|o=s' => \
$opts{output
},
345 'format|f=s' => \
$opts{format
},
346 'account-width=i' => \
$opts{account_width
},
347 'accounts!' => \
$opts{accounts
},
348 'payees!' => \
$opts{payees
},
349 'tags!' => \
$opts{tags
},
350 'commodities!' => \
$opts{commodities
},
351 'opening-date=s' => \
$opts{opening_date
},
352 'default-account=s' => \
$opts{default_account
},
353 'rename-account|r=s' => \
%{$opts{rename_accounts
}},
354 'exclude-account|x=s' => \
@{$opts{exclude_accounts
}},
355 ) or pod2usage
(-exitval
=> 1, -verbose
=> 99, -sections
=> [qw(SYNOPSIS OPTIONS)]);
357 $opts{input
} = shift @args if !$opts{input
};
359 print STDERR
"Input file is required.\n";
368 return [split(/\h+/, $tags || '')];
381 App::HomeBank2Ledger - A tool to convert HomeBank files to Ledger format
389 App::HomeBank2Ledger->main(@args);
393 This module is part of the L<homebank2ledger> script.
399 App::HomeBank2Ledger->main(@args);
401 Run the script and exit; does not return.
405 $formatter = $app->formatter($homebank, $opts);
407 Generate a L<App::HomeBank2Ledger::Formatter>.
409 =head2 convert_homebank_to_ledger
411 my $ledger = $app->convert_homebank_to_ledger($homebank, $opts);
413 Converts a L<File::HomeBank> to a L<App::HomeBank2Ledger::Ledger>.
417 $app->print_to_file($str);
418 $app->print_to_file($str, $filepath);
420 Print a string to a file (or STDOUT).
424 $opts = $app->parse_args(@args);
426 Parse command-line arguments.
430 Please report any bugs or feature requests on the bugtracker website
431 L<https://github.com/chazmcgarvey/homebank2ledger/issues>
433 When submitting a bug or request, please include a test-file or a
434 patch to an existing test-file that illustrates the bug or desired
439 Charles McGarvey <chazmcgarvey@brokenzipper.com>
441 =head1 COPYRIGHT AND LICENSE
443 This software is Copyright (c) 2019 by Charles McGarvey.
445 This is free software, licensed under:
447 The MIT (X11) License