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;
136 # determine full Ledger account names
137 for my $account (@$accounts) {
138 my $type = $ACCOUNT_TYPES{$account->{type
}} || $UNKNOWN_ACCOUNT;
139 $account->{ledger_name
} = "${type}:$account->{name}";
141 for my $category (@$categories) {
142 my $type = $category->{flags
}{income
} ? 'Income' : 'Expenses';
143 my $full_name = $homebank->full_category_name($category->{key
});
144 $category->{ledger_name
} = "${type}:${full_name}";
146 if ($opts->{budget
} && $category->{flags
}{budget
}) {
147 for my $month_num ($category->{flags
}{custom
} ? (1 .. 12) : 0) {
148 my $amount = $category->{budget_amounts
}[$month_num] || 0;
149 next if !$amount && !$category->{flags
}{forced
};
151 $budget[$month_num]{$category->{ledger_name
}} = $amount;
156 # handle renaming and marking excluded accounts
157 for my $item (@$accounts, @$categories) {
158 while (my ($re, $replacement) = each %{$opts->{rename_accounts
}}) {
159 $item->{ledger_name
} =~ s/$re/$replacement/;
161 for my $re (@{$opts->{exclude_accounts
}}) {
162 $item->{excluded
} = 1 if $item->{ledger_name
} =~ /$re/;
165 while (my ($re, $replacement) = each %{$opts->{rename_accounts
}}) {
166 $default_account_income =~ s/$re/$replacement/;
167 $default_account_expenses =~ s/$re/$replacement/;
170 my $has_initial_balance = grep { $_->{initial
} && !$_->{excluded
} } @$accounts;
172 if ($opts->{accounts
}) {
173 my @accounts = map { $_->{ledger_name
} } grep { !$_->{excluded
} } @$accounts, @$categories;
175 push @accounts, $default_account_income if !grep { $_ eq $default_account_income } @accounts;
176 push @accounts, $default_account_expenses if !grep { $_ eq $default_account_expenses } @accounts;
177 push @accounts, $OPENING_BALANCES_ACCOUNT if $has_initial_balance;
179 $ledger->add_accounts(@accounts);
182 if ($opts->{payees
}) {
183 my $payees = $homebank->payees;
184 my @payees = map { $_->{name
} } @$payees;
186 $ledger->add_payees(@payees);
190 my $tags = $homebank->tags;
192 $ledger->add_tags(@$tags);
197 for my $currency (@{$homebank->currencies}) {
199 symbol
=> $currency->{symbol
},
200 format
=> $homebank->format_amount(1_000, $currency),
201 iso
=> $currency->{iso
},
202 name
=> $currency->{name
},
204 $commodities{$currency->{key
}} = {
206 syprf
=> $currency->{syprf
},
207 dchar
=> $currency->{dchar
},
208 gchar
=> $currency->{gchar
},
209 frac
=> $currency->{frac
},
212 $ledger->add_commodities($commodity) if $opts->{commodities
};
216 if ($has_initial_balance) {
217 # transactions are sorted, so the first transaction is the oldest
218 $first_date = $opts->{opening_date
} || $transactions->[0]{date
};
219 if ($first_date !~ /^\d{4}-\d{2}-\d{2}$/) {
220 die "Opening date must be in the form YYYY-MM-DD.\n";
225 for my $account (@$accounts) {
226 next if !$account->{initial
} || $account->{excluded
};
229 account
=> $account->{ledger_name
},
230 amount
=> $account->{initial
},
231 commodity
=> $commodities{$account->{currency
}},
236 account
=> $OPENING_BALANCES_ACCOUNT,
239 $ledger->add_transactions({
241 payee
=> 'Opening Balance',
243 postings
=> \
@postings,
247 if ($opts->{budget
}) {
248 my ($first_year) = $first_date =~ /^(\d{4})/;
250 for my $month_num (0 .. 12) {
251 next if !$budget[$month_num];
253 my $payee = 'Monthly';
254 if (0 < $month_num) {
255 my $year = $first_year;
256 $year += 1 if sprintf('%04d-%02d-99', $first_year, $month_num) lt $first_date;
257 my $date = sprintf('%04d-%02d', $year, $month_num);
258 $payee = "Every 12 months from ${date}";
260 # my @MONTHS = qw(ALL Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
261 # $payee = "Monthly this $MONTHS[$month_num]" if 0 < $month_num;
265 for my $account (sort keys %{$budget[$month_num]}) {
266 my $amount = $budget[$month_num]{$account};
270 commodity
=> $commodities{$homebank->base_currency},
277 $ledger->add_transactions({
280 postings
=> \
@postings,
288 for my $transaction (@$transactions) {
289 next if $seen{$transaction->{transfer_key
} || ''};
291 my $account = $homebank->find_account_by_key($transaction->{account
});
292 my $amount = $transaction->{amount
};
293 my $status = $STATUS_SYMBOLS{$transaction->{status
} || ''} || '';
294 my $memo = $transaction->{wording
} || '';
295 my $payee = $homebank->find_payee_by_key($transaction->{payee
});
296 my $tags = _split_tags
($transaction->{tags
});
297 my $date = $transaction->{date
};
298 my $code = $transaction->{paymode
} =~ /^(?:check|epayment)$/ ? $transaction->{info
}
305 account
=> $account->{ledger_name
},
307 commodity
=> $commodities{$account->{currency
}},
308 payee
=> $payee->{name
},
314 if ($transaction->{dst_account
}) { # is an internal transfer
315 my $paired_transaction = $homebank->find_transaction_transfer_pair($transaction);
317 my $dst_account = $homebank->find_account_by_key($transaction->{dst_account
});
319 if ($paired_transaction) {
320 $dst_account = $homebank->find_account_by_key($paired_transaction->{account
});
323 warn "Skipping internal transfer transaction with no destination account.\n";
328 $seen{$transaction->{transfer_key
}}++ if $transaction->{transfer_key
};
329 $seen{$paired_transaction->{transfer_key
}}++ if $paired_transaction->{transfer_key
};
331 my $paired_date = $paired_transaction && $paired_transaction->{date
};
332 my $paired_payee = $homebank->find_payee_by_key($paired_transaction->{payee
});
335 date
=> $paired_date,
336 account
=> $dst_account->{ledger_name
},
337 amount
=> $paired_transaction->{amount
} || -$transaction->{amount
},
338 commodity
=> $commodities{$dst_account->{currency
}},
339 payee
=> $paired_payee->{name
},
340 note
=> $paired_transaction->{wording
} || '',
341 status
=> $STATUS_SYMBOLS{$paired_transaction->{status
} || ''} || $status,
342 tags
=> _split_tags
($paired_transaction->{tags
}),
345 elsif ($transaction->{flags
}{split}) {
346 my @amounts = split(/\|\|/, $transaction->{split_amount
} || '');
347 my @memos = split(/\|\|/, $transaction->{split_memo
} || '');
348 my @categories = split(/\|\|/, $transaction->{split_category
} || '');
350 for (my $i = 0; $amounts[$i]; ++$i) {
351 my $amount = -$amounts[$i];
352 my $category = $homebank->find_category_by_key($categories[$i]);
353 my $memo = $memos[$i] || '';
354 my $other_account = $category ? $category->{ledger_name
}
355 : $amount < 0 ? $default_account_income
356 : $default_account_expenses;
359 account
=> $other_account,
360 commodity
=> $commodities{$account->{currency
}},
362 payee
=> $payee->{name
},
369 else { # normal transaction with or without category
370 my $amount = -$transaction->{amount
};
371 my $category = $homebank->find_category_by_key($transaction->{category
});
372 my $other_account = $category ? $category->{ledger_name
}
373 : $amount < 0 ? $default_account_income
374 : $default_account_expenses;
377 account
=> $other_account,
378 commodity
=> $commodities{$account->{currency
}},
380 payee
=> $payee->{name
},
387 # skip excluded accounts
388 for my $posting (@postings) {
389 for my $re (@{$opts->{exclude_accounts
}}) {
390 next TRANSACTION
if $posting->{account
} =~ /$re/;
394 $ledger->add_transactions({
396 payee
=> $payee->{name
},
399 postings
=> \
@postings,
406 =method print_to_file
408 $app->print_to_file($str);
409 $app->print_to_file($str, $filepath);
411 Print a string to a file
(or STDOUT
).
418 my $filepath = shift;
420 my $out_fh = \
*STDOUT
;
422 open($out_fh, '>', $filepath) or die "open failed: $!";
429 $opts = $app->parse_args(@args);
431 Parse command-line arguments
.
453 rename_accounts
=> {},
454 exclude_accounts
=> [],
457 GetOptionsFromArray
(\
@args,
458 'version|V' => \
$opts{version
},
459 'help|h|?' => \
$opts{help
},
460 'manual|man' => \
$opts{manual
},
461 'input|file|i=s' => \
$opts{input
},
462 'output|o=s' => \
$opts{output
},
463 'format|f=s' => \
$opts{format
},
464 'account-width=i' => \
$opts{account_width
},
465 'accounts!' => \
$opts{accounts
},
466 'payees!' => \
$opts{payees
},
467 'tags!' => \
$opts{tags
},
468 'commodities!' => \
$opts{commodities
},
469 'budget!' => \
$opts{budget
},
470 'opening-date=s' => \
$opts{opening_date
},
471 'rename-account|r=s' => \
%{$opts{rename_accounts
}},
472 'exclude-account|x=s' => \
@{$opts{exclude_accounts
}},
473 ) or pod2usage
(-exitval
=> 1, -verbose
=> 99, -sections
=> [qw(SYNOPSIS OPTIONS)]);
475 $opts{input
} = shift @args if !$opts{input
};
476 $opts{budget
} = 0 if lc($opts{format
}) ne 'ledger';
483 return [split(/\h+/, $tags || '')];