Annotation of pta/pta_import.pl, Revision 1.15
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,
1.15 ! schwarze 29: "wellsfargo" => \&import_wellsfargo,
1.13 schwarze 30: );
31:
1.1 schwarze 32: # === SUBROUTINES =====================================================
33:
1.10 schwarze 34: sub import_chase_credit ($$$) {
35: my ($account, $csv_account, $line) = @_;
36: my ($trans_date, $post_date, $description, $category,
37: $type, $amount) = split /,/, $line;
38: my ($debit, $credit);
39: $post_date =~ s#(\d+)/(\d+)/(\d+)#$3$1$2#;
40: ($amount, $debit, $credit) =
41: get_accounts_by_amount_sign($amount, $account,
42: $csv_account);
43: return ($post_date, $amount, $debit, $credit, $description);
44: }
45:
46: sub import_capital_one_credit ($$$) {
47: my ($account, $csv_account, $line) = @_;
48: my ($trans_date, $post_date, $card_num,
49: $description, $category, $csv_debit,
50: $csv_credit) = split /,/, $line;
51: $post_date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
52: my ($amount, $debit, $credit) =
53: get_accounts_by_csv_col($account, $csv_account,
54: $csv_debit, $csv_credit);
55: return ($post_date, $amount, $debit, $credit, $description);
56: }
57:
58: sub import_optum_hsa ($$$) {
59: my ($account, $csv_account, $line) = @_;
60: my ($date, $description, $amount,
61: $type) = split /,/, $line;
62: my ($debit, $credit);
63: $date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
64: $amount =~ s/\$//;
65: ($amount, $debit, $credit) =
66: get_accounts_by_amount_sign($amount, $account,
67: $csv_account);
68: return ($date, $amount, $debit, $credit, $description);
69: }
70:
1.11 schwarze 71: sub import_sparkasse_camt ($$$) {
72: my ($account, $csv_account, $line) = @_;
73: my @fields;
74: $_ = $line;
75: push @fields, $1 while s/"([^"]*)";?//;
76: $_ eq "" or die "CAMT parse error before $_ in $line";
77: @fields == 17 or die "not 17 but @fields fields: $line";
78: $fields[1] =~ s/^(\d\d)\.(\d\d)\.(\d\d)$/20$3$2$1/
79: or die "date parse error: $line";
80: $fields[14] =~ s/,/./;
81: return $fields[1],
82: get_accounts_by_amount_sign($fields[14], $account, $csv_account),
83: (join ' ', $fields[11], $fields[4]);
1.15 ! schwarze 84: }
! 85:
! 86: sub import_wellsfargo ($$$) {
! 87: my ($account, $csv_account, $line) = @_;
! 88: $line =~
! 89: m#^"(\d+)/(\d+)/(\d+)","(-?\d+\.\d+)","\*","","(.*?)"$# or
! 90: die "parse error: $line" ;
! 91: my ($month, $day, $year, $amount, $description) =
! 92: ($1, $2, $3, $4, $5);
! 93: return "$year$month$day",
! 94: get_accounts_by_amount_sign($amount, $account, $csv_account),
! 95: $description;
1.11 schwarze 96: }
97:
1.10 schwarze 98: sub get_accounts_by_amount_sign ($$$) {
99: my ($amount, $account, $csv_account) = @_;
100: my ($debit, $credit);
101: if ($amount <= 0) {
102: $amount = substr $amount, 1;
103: $credit = $csv_account;
104: $debit = $account;
105: } else {
106: $debit = $csv_account;
107: $credit = $account;
1.5 schwarze 108: }
1.10 schwarze 109: return ($amount, $debit, $credit);
1.5 schwarze 110: }
1.9 schwarze 111:
1.10 schwarze 112: sub get_accounts_by_csv_col ($$$$) {
113: my ($account, $csv_account, $csv_debit, $csv_credit) = @_;
114: my ($amount, $debit, $credit);
115: if ($csv_debit eq "") {
116: $amount = $csv_credit;
117: $credit = $account;
118: $debit = $csv_account;
119: } else {
120: $amount = $csv_debit;
121: $credit = $csv_account;
122: $debit = $account;
123: }
124: return ($amount, $debit, $credit);
1.9 schwarze 125: }
126:
127: sub usage () {
1.10 schwarze 128: printf STDERR "usage: %s -I accountname csvfilename\n", $0;
1.9 schwarze 129: exit 1;
130: }
131:
1.1 schwarze 132: # === MAIN PROGRAM =====================================================
133:
1.9 schwarze 134: getopts 'I:' or usage;
1.14 schwarze 135: unless ($opt_I) {
136: warn "The option -I is required.";
1.9 schwarze 137: usage;
138: }
1.14 schwarze 139: my $account_name = $opt_I;
140: $banks{$account_name} or die "unknown accountname: $account_name";
1.13 schwarze 141:
142: # Parse the configuration file.
1.14 schwarze 143: my $fn = "import_" . $account_name . ".txt";
144: open my $in, '<', $fn or die "$fn: $!";
145: my ($csv_account, $delim, $header, @compiled);
1.9 schwarze 146: while (<$in>) {
147: chomp;
148: next if /^(?:#|$)/;
149: my $line = $_;
1.12 schwarze 150: if (/^ACCOUNT\s+(\S+)$/) {
1.9 schwarze 151: $csv_account and die "duplicate ACCOUNT line: $1";
152: $csv_account = $1;
153: next;
154: }
1.12 schwarze 155: if (/^DELIM\s+(\S)$/) {
156: $delim and die "duplicate DELIM line: $1";
157: $delim = $1;
158: next;
159: }
1.14 schwarze 160: if (/^HEADER\s+(.*)$/) {
161: $header and die "duplicate HEADER line: $1";
162: $header = $1;
163: next;
164: }
1.12 schwarze 165: $delim or die "no DELIM line in $fn";
166: s/^(.*)$delim\s+(\d+)\s+(\S+)// or
1.9 schwarze 167: die "$fn import parse error: $line";
1.12 schwarze 168: push @compiled, {
169: re => [map { qr/$_/ } split /$delim/, $1],
170: ac => $2,
171: id => $3,
172: };
1.1 schwarze 173: }
1.9 schwarze 174: close $in;
175: $csv_account or die "no ACCOUNT line in $fn";
1.13 schwarze 176:
177: # Parse the CSV file from the bank.
178: while (<>) {
179: chomp;
180: next if (/^$/);
181: my $line = $_;
1.14 schwarze 182: if ($header) {
183: $line eq $header
184: or die "expected HEADER $header\nbut got $line";
185: undef $header;
186: next;
187: }
1.13 schwarze 188: my @fields = split /$delim/, $line;
189: my $matches = 0;
190: my ($account, $booking);
191: foreach my $selector (@compiled) {
192: $matches = 1;
193: for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
194: next if $fields[$i] =~ $selector->{re}[$i];
195: $matches = 0;
196: last;
197: }
198: if ($matches) {
199: $account = $selector->{ac};
200: $booking = $selector->{id};
201: last;
202: }
203: }
204: $matches or die "unmatched CSV line: $line";
205: my ($date, $amount, $debit, $credit, $description) =
206: $banks{$account_name}->($account, $csv_account, $line);
207: $date && $amount && $debit && $credit && $description
208: or die "import parse error: $line";
209: $description =~ s/#//g;
210: print "$date $booking $debit $credit $amount $description\n";
211: }
CVSweb