1 package App
::HomeBank2Ledger
;
2 # ABSTRACT: A tool to convert HomeBank files to Ledger format
6 App::HomeBank2Ledger->main(@args);
10 This module is part of the L<homebank2ledger> script.
17 use App
::HomeBank2Ledger
::Formatter
;
18 use App
::HomeBank2Ledger
::Ledger
;
20 use Getopt
::Long
2.38 qw(GetOptionsFromArray);
23 our $VERSION = '9999.999'; # VERSION
25 my %ACCOUNT_TYPES = ( # map HomeBank account types to Ledger accounts
26 bank
=> 'Assets:Bank',
27 cash
=> 'Assets:Cash',
28 asset
=> 'Assets:Fixed Assets',
29 creditcard
=> 'Liabilities:Credit Card',
30 liability
=> 'Liabilities',
31 stock
=> 'Assets:Stock',
32 mutualfund
=> 'Assets:Mutual Fund',
34 expense
=> 'Expenses',
37 my %STATUS_SYMBOLS = (
39 reconciled
=> 'cleared',
42 my $UNKNOWN_ACCOUNT = 'Assets:Unknown';
43 my $OPENING_BALANCES_ACCOUNT = 'Equity:Opening Balances';
47 App
::HomeBank2Ledger-
>main(@args);
49 Run the script
and exit; does not return.
55 my $self = bless {}, $class;
57 my $opts = $self->parse_args(@_);
59 if ($opts->{version
}) {
60 print "homebank2ledger ${VERSION}\n";
64 pod2usage
(-exitval
=> 0, -verbose
=> 99, -sections
=> [qw(NAME SYNOPSIS OPTIONS)]);
66 if ($opts->{manual
}) {
67 pod2usage
(-exitval
=> 0, -verbose
=> 2);
69 if (!$opts->{input
}) {
70 print STDERR
"Input file is required.\n";
74 my $homebank = File
::HomeBank-
>new(file
=> $opts->{input
});
76 my $formatter = eval { $self->formatter($homebank, $opts) };
78 if ($err =~ /^Invalid formatter/) {
79 print STDERR
"Invalid format: $opts->{format}\n";
85 my $ledger = $self->convert_homebank_to_ledger($homebank, $opts);
87 $self->print_to_file($formatter->format($ledger), $opts->{output
});
94 $formatter = $app->formatter($homebank, $opts);
96 Generate a L
<App
::HomeBank2Ledger
::Formatter
>.
102 my $homebank = shift;
103 my $opts = shift || {};
105 return App
::HomeBank2Ledger
::Formatter-
>new(
106 type
=> $opts->{format
},
107 account_width
=> $opts->{account_width
},
108 name
=> $homebank->title,
109 file
=> $homebank->file,
113 =method convert_homebank_to_ledger
115 my $ledger = $app->convert_homebank_to_ledger($homebank, $opts);
117 Converts a L
<File
::HomeBank
> to a L
<App
::HomeBank2Ledger
::Ledger
>.
121 sub convert_homebank_to_ledger
{
123 my $homebank = shift;
124 my $opts = shift || {};
126 my $default_account_income = 'Income:Unknown';
127 my $default_account_expenses = 'Expenses:Unknown';
129 my $ledger = App
::HomeBank2Ledger
::Ledger-
>new;
131 my $transactions = $homebank->sorted_transactions;
132 my $accounts = $homebank->accounts;
133 my $categories = $homebank->categories;
135 # determine full Ledger account names
136 for my $account (@$accounts) {
137 my $type = $ACCOUNT_TYPES{$account->{type
}} || $UNKNOWN_ACCOUNT;
138 $account->{ledger_name
} = "${type}:$account->{name}";
140 for my $category (@$categories) {
141 my $type = $category->{flags
}{income
} ? 'Income' : 'Expenses';
142 my $full_name = $homebank->full_category_name($category->{key
});
143 $category->{ledger_name
} = "${type}:${full_name}";
146 # handle renaming and marking excluded accounts
147 for my $item (@$accounts, @$categories) {
148 while (my ($re, $replacement) = each %{$opts->{rename_accounts
}}) {
149 $item->{ledger_name
} =~ s/$re/$replacement/;
151 for my $re (@{$opts->{exclude_accounts
}}) {
152 $item->{excluded
} = 1 if $item->{ledger_name
} =~ /$re/;
155 while (my ($re, $replacement) = each %{$opts->{rename_accounts
}}) {
156 $default_account_income =~ s/$re/$replacement/;
157 $default_account_expenses =~ s/$re/$replacement/;
160 my $has_initial_balance = grep { $_->{initial
} && !$_->{excluded
} } @$accounts;
162 if ($opts->{accounts
}) {
163 my @accounts = map { $_->{ledger_name
} } grep { !$_->{excluded
} } @$accounts, @$categories;
165 push @accounts, $default_account_income if !grep { $_ eq $default_account_income } @accounts;
166 push @accounts, $default_account_expenses if !grep { $_ eq $default_account_expenses } @accounts;
167 push @accounts, $OPENING_BALANCES_ACCOUNT if $has_initial_balance;
169 $ledger->add_accounts(@accounts);
172 if ($opts->{payees
}) {
173 my $payees = $homebank->payees;
174 my @payees = map { $_->{name
} } @$payees;
176 $ledger->add_payees(@payees);
180 my $tags = $homebank->tags;
182 $ledger->add_tags(@$tags);
187 for my $currency (@{$homebank->currencies}) {
189 symbol
=> $currency->{symbol
},
190 format
=> $homebank->format_amount(1_000, $currency),
191 iso
=> $currency->{iso
},
192 name
=> $currency->{name
},
194 $commodities{$currency->{key
}} = {
196 syprf
=> $currency->{syprf
},
197 dchar
=> $currency->{dchar
},
198 gchar
=> $currency->{gchar
},
199 frac
=> $currency->{frac
},
202 $ledger->add_commodities($commodity) if $opts->{commodities
};
205 if ($has_initial_balance) {
206 # transactions are sorted, so the first transaction is the oldest
207 my $first_date = $opts->{opening_date
} || $transactions->[0]{date
};
208 if ($first_date !~ /^\d{4}-\d{2}-\d{2}$/) {
209 die "Opening date must be in the form YYYY-MM-DD.\n";
214 for my $account (@$accounts) {
215 next if !$account->{initial
} || $account->{excluded
};
218 account
=> $account->{ledger_name
},
219 amount
=> $account->{initial
},
220 commodity
=> $commodities{$account->{currency
}},
225 account
=> $OPENING_BALANCES_ACCOUNT,
228 $ledger->add_transactions({
230 payee
=> 'Opening Balance',
232 postings
=> \
@postings,
239 for my $transaction (@$transactions) {
240 next if $seen{$transaction->{transfer_key
} || ''};
242 my $account = $homebank->find_account_by_key($transaction->{account
});
243 my $amount = $transaction->{amount
};
244 my $status = $STATUS_SYMBOLS{$transaction->{status
} || ''} || '';
245 my $paymode = $transaction->{paymode
} || ''; # internaltransfer
246 my $memo = $transaction->{wording
} || '';
247 my $payee = $homebank->find_payee_by_key($transaction->{payee
});
248 my $tags = _split_tags
($transaction->{tags
});
253 account
=> $account->{ledger_name
},
255 commodity
=> $commodities{$account->{currency
}},
256 payee
=> $payee->{name
},
262 if ($paymode eq 'internaltransfer') {
263 my $paired_transaction = $homebank->find_transaction_transfer_pair($transaction);
265 my $dst_account = $homebank->find_account_by_key($transaction->{dst_account
});
267 if ($paired_transaction) {
268 $dst_account = $homebank->find_account_by_key($paired_transaction->{account
});
271 warn "Skipping internal transfer transaction with no destination account.\n";
276 $seen{$transaction->{transfer_key
}}++ if $transaction->{transfer_key
};
277 $seen{$paired_transaction->{transfer_key
}}++ if $paired_transaction->{transfer_key
};
279 my $paired_payee = $homebank->find_payee_by_key($paired_transaction->{payee
});
282 account
=> $dst_account->{ledger_name
},
283 amount
=> $paired_transaction->{amount
} || -$transaction->{amount
},
284 commodity
=> $commodities{$dst_account->{currency
}},
285 payee
=> $paired_payee->{name
},
286 memo
=> $paired_transaction->{wording
} || '',
287 status
=> $STATUS_SYMBOLS{$paired_transaction->{status
} || ''} || $status,
288 tags
=> _split_tags
($paired_transaction->{tags
}),
291 elsif ($transaction->{flags
}{split}) {
292 my @amounts = split(/\|\|/, $transaction->{split_amount
} || '');
293 my @memos = split(/\|\|/, $transaction->{split_memo
} || '');
294 my @categories = split(/\|\|/, $transaction->{split_category
} || '');
296 for (my $i = 0; $amounts[$i]; ++$i) {
297 my $amount = -$amounts[$i];
298 my $category = $homebank->find_category_by_key($categories[$i]);
299 my $memo = $memos[$i] || '';
300 my $other_account = $category ? $category->{ledger_name
}
301 : $amount < 0 ? $default_account_income
302 : $default_account_expenses;
305 account
=> $other_account,
306 commodity
=> $commodities{$account->{currency
}},
308 payee
=> $payee->{name
},
315 else { # with or without category
316 my $amount = -$transaction->{amount
};
317 my $category = $homebank->find_category_by_key($transaction->{category
});
318 my $other_account = $category ? $category->{ledger_name
}
319 : $amount < 0 ? $default_account_income
320 : $default_account_expenses;
323 account
=> $other_account,
324 commodity
=> $commodities{$account->{currency
}},
326 payee
=> $payee->{name
},
333 # skip excluded accounts
334 for my $posting (@postings) {
335 for my $re (@{$opts->{exclude_accounts
}}) {
336 next TRANSACTION
if $posting->{account
} =~ /$re/;
340 $ledger->add_transactions({
341 date
=> $transaction->{date
},
342 payee
=> $payee->{name
},
344 postings
=> \
@postings,
351 =method print_to_file
353 $app->print_to_file($str);
354 $app->print_to_file($str, $filepath);
356 Print a string to a file
(or STDOUT
).
363 my $filepath = shift;
365 my $out_fh = \
*STDOUT
;
367 open($out_fh, '>', $filepath) or die "open failed: $!";
374 $opts = $app->parse_args(@args);
376 Parse command-line arguments
.
397 rename_accounts
=> {},
398 exclude_accounts
=> [],
401 GetOptionsFromArray
(\
@args,
402 'version|V' => \
$opts{version
},
403 'help|h|?' => \
$opts{help
},
404 'manual|man' => \
$opts{manual
},
405 'input|file|i=s' => \
$opts{input
},
406 'output|o=s' => \
$opts{output
},
407 'format|f=s' => \
$opts{format
},
408 'account-width=i' => \
$opts{account_width
},
409 'accounts!' => \
$opts{accounts
},
410 'payees!' => \
$opts{payees
},
411 'tags!' => \
$opts{tags
},
412 'commodities!' => \
$opts{commodities
},
413 'opening-date=s' => \
$opts{opening_date
},
414 'rename-account|r=s' => \
%{$opts{rename_accounts
}},
415 'exclude-account|x=s' => \
@{$opts{exclude_accounts
}},
416 ) or pod2usage
(-exitval
=> 1, -verbose
=> 99, -sections
=> [qw(SYNOPSIS OPTIONS)]);
418 $opts{input
} = shift @args if !$opts{input
};
425 return [split(/\h+/, $tags || '')];