Annotation of pta/pta_import.pl, Revision 1.14
1.1 schwarze 1: #!/usr/bin/perl
2:
3: # Copyright (c) 2020 Freda Bundchen
4:
5: # Permission to use, copy, modify, and distribute this software for any
6: # purpose with or without fee is hereby granted, provided that the above
7: # copyright notice and this permission notice appear in all copies.
8: #
9: # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16:
17: use warnings;
18: use strict;
19:
1.9 schwarze 20: use Getopt::Std qw(getopts);
21:
22: our ($opt_I);
23:
1.13 schwarze 24: my %banks = (
25: "capital_one_credit" => \&import_capital_one_credit,
26: "chase_credit" => \&import_chase_credit,
27: "optum_hsa" => \&import_optum_hsa,
28: "sparkasse_camt" => \&import_sparkasse_camt,
29: );
30:
1.1 schwarze 31: # === SUBROUTINES =====================================================
32:
1.10 schwarze 33: sub import_chase_credit ($$$) {
34: my ($account, $csv_account, $line) = @_;
35: my ($trans_date, $post_date, $description, $category,
36: $type, $amount) = split /,/, $line;
37: my ($debit, $credit);
38: $post_date =~ s#(\d+)/(\d+)/(\d+)#$3$1$2#;
39: ($amount, $debit, $credit) =
40: get_accounts_by_amount_sign($amount, $account,
41: $csv_account);
42: return ($post_date, $amount, $debit, $credit, $description);
43: }
44:
45: sub import_capital_one_credit ($$$) {
46: my ($account, $csv_account, $line) = @_;
47: my ($trans_date, $post_date, $card_num,
48: $description, $category, $csv_debit,
49: $csv_credit) = split /,/, $line;
50: $post_date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
51: my ($amount, $debit, $credit) =
52: get_accounts_by_csv_col($account, $csv_account,
53: $csv_debit, $csv_credit);
54: return ($post_date, $amount, $debit, $credit, $description);
55: }
56:
57: sub import_optum_hsa ($$$) {
58: my ($account, $csv_account, $line) = @_;
59: my ($date, $description, $amount,
60: $type) = split /,/, $line;
61: my ($debit, $credit);
62: $date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
63: $amount =~ s/\$//;
64: ($amount, $debit, $credit) =
65: get_accounts_by_amount_sign($amount, $account,
66: $csv_account);
67: return ($date, $amount, $debit, $credit, $description);
68: }
69:
1.11 schwarze 70: sub import_sparkasse_camt ($$$) {
71: my ($account, $csv_account, $line) = @_;
72: my @fields;
73: $_ = $line;
74: push @fields, $1 while s/"([^"]*)";?//;
75: $_ eq "" or die "CAMT parse error before $_ in $line";
76: @fields == 17 or die "not 17 but @fields fields: $line";
77: $fields[1] =~ s/^(\d\d)\.(\d\d)\.(\d\d)$/20$3$2$1/
78: or die "date parse error: $line";
79: $fields[14] =~ s/,/./;
80: return $fields[1],
81: get_accounts_by_amount_sign($fields[14], $account, $csv_account),
82: (join ' ', $fields[11], $fields[4]);
83: }
84:
1.10 schwarze 85: sub get_accounts_by_amount_sign ($$$) {
86: my ($amount, $account, $csv_account) = @_;
87: my ($debit, $credit);
88: if ($amount <= 0) {
89: $amount = substr $amount, 1;
90: $credit = $csv_account;
91: $debit = $account;
92: } else {
93: $debit = $csv_account;
94: $credit = $account;
1.5 schwarze 95: }
1.10 schwarze 96: return ($amount, $debit, $credit);
1.5 schwarze 97: }
1.9 schwarze 98:
1.10 schwarze 99: sub get_accounts_by_csv_col ($$$$) {
100: my ($account, $csv_account, $csv_debit, $csv_credit) = @_;
101: my ($amount, $debit, $credit);
102: if ($csv_debit eq "") {
103: $amount = $csv_credit;
104: $credit = $account;
105: $debit = $csv_account;
106: } else {
107: $amount = $csv_debit;
108: $credit = $csv_account;
109: $debit = $account;
110: }
111: return ($amount, $debit, $credit);
1.9 schwarze 112: }
113:
114: sub usage () {
1.10 schwarze 115: printf STDERR "usage: %s -I accountname csvfilename\n", $0;
1.9 schwarze 116: exit 1;
117: }
118:
1.1 schwarze 119: # === MAIN PROGRAM =====================================================
120:
1.9 schwarze 121: getopts 'I:' or usage;
1.14 ! schwarze 122: unless ($opt_I) {
! 123: warn "The option -I is required.";
1.9 schwarze 124: usage;
125: }
1.14 ! schwarze 126: my $account_name = $opt_I;
! 127: $banks{$account_name} or die "unknown accountname: $account_name";
1.13 schwarze 128:
129: # Parse the configuration file.
1.14 ! schwarze 130: my $fn = "import_" . $account_name . ".txt";
! 131: open my $in, '<', $fn or die "$fn: $!";
! 132: my ($csv_account, $delim, $header, @compiled);
1.9 schwarze 133: while (<$in>) {
134: chomp;
135: next if /^(?:#|$)/;
136: my $line = $_;
1.12 schwarze 137: if (/^ACCOUNT\s+(\S+)$/) {
1.9 schwarze 138: $csv_account and die "duplicate ACCOUNT line: $1";
139: $csv_account = $1;
140: next;
141: }
1.12 schwarze 142: if (/^DELIM\s+(\S)$/) {
143: $delim and die "duplicate DELIM line: $1";
144: $delim = $1;
145: next;
146: }
1.14 ! schwarze 147: if (/^HEADER\s+(.*)$/) {
! 148: $header and die "duplicate HEADER line: $1";
! 149: $header = $1;
! 150: next;
! 151: }
1.12 schwarze 152: $delim or die "no DELIM line in $fn";
153: s/^(.*)$delim\s+(\d+)\s+(\S+)// or
1.9 schwarze 154: die "$fn import parse error: $line";
1.12 schwarze 155: push @compiled, {
156: re => [map { qr/$_/ } split /$delim/, $1],
157: ac => $2,
158: id => $3,
159: };
1.1 schwarze 160: }
1.9 schwarze 161: close $in;
162: $csv_account or die "no ACCOUNT line in $fn";
1.13 schwarze 163:
164: # Parse the CSV file from the bank.
165: while (<>) {
166: chomp;
167: next if (/^$/);
168: my $line = $_;
1.14 ! schwarze 169: if ($header) {
! 170: $line eq $header
! 171: or die "expected HEADER $header\nbut got $line";
! 172: undef $header;
! 173: next;
! 174: }
1.13 schwarze 175: my @fields = split /$delim/, $line;
176: my $matches = 0;
177: my ($account, $booking);
178: foreach my $selector (@compiled) {
179: $matches = 1;
180: for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
181: next if $fields[$i] =~ $selector->{re}[$i];
182: $matches = 0;
183: last;
184: }
185: if ($matches) {
186: $account = $selector->{ac};
187: $booking = $selector->{id};
188: last;
189: }
190: }
191: $matches or die "unmatched CSV line: $line";
192: my ($date, $amount, $debit, $credit, $description) =
193: $banks{$account_name}->($account, $csv_account, $line);
194: $date && $amount && $debit && $credit && $description
195: or die "import parse error: $line";
196: $description =~ s/#//g;
197: print "$date $booking $debit $credit $amount $description\n";
198: }
CVSweb