Annotation of pta/pta_import.pl, Revision 1.9
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.1 schwarze 24: # === SUBROUTINES =====================================================
25:
1.9 ! schwarze 26: sub import_chase_credit ($$$$) {
1.5 schwarze 27: my ($bookings_ref, $accounts_ref, $compiled_ref,
28: $csv_account) = @_;
29: my ($trans_date, $post_date, $description, $category,
30: $type, $amount);
31: chomp(my $header = <>);
1.2 schwarze 32: while (<>) {
1.5 schwarze 33: next if (/^$/);
1.1 schwarze 34: chomp;
1.3 schwarze 35: s#(\d+)/(\d+)/(\d+)#$3$1$2#g;
36: my $line = $_;
1.5 schwarze 37: ($trans_date, $post_date, $description, $category,
1.1 schwarze 38: $type, $amount) = split /,/;
39: $description =~ s/#//g;
40: my ($debit, $credit);
1.3 schwarze 41: my $booking = "";
42: my $account = "";
1.5 schwarze 43: foreach my $regex (@$compiled_ref) {
1.3 schwarze 44: if ($line =~ /$regex/) {
1.5 schwarze 45: $account = %$accounts_ref{$regex};
46: $booking = %$bookings_ref{$regex};
1.4 schwarze 47: last;
1.3 schwarze 48: }
49: }
50: if ($booking eq "" || $account eq "") {
1.5 schwarze 51: die "import parse error: $line";
1.3 schwarze 52: }
1.1 schwarze 53: if ($amount <= 0) {
54: $amount = substr $amount, 1;
1.3 schwarze 55: $credit = $csv_account;
56: $debit = $account;
1.1 schwarze 57: } else {
1.3 schwarze 58: $debit = $csv_account;
59: $credit = $account;
1.1 schwarze 60: }
61: print "$post_date $booking $debit $credit $amount " .
62: "$description\n";
63: }
64: }
65:
1.9 ! schwarze 66: sub import_capital_one_credit ($$$$) {
1.5 schwarze 67: my ($bookings_ref, $accounts_ref, $compiled_ref,
68: $csv_account) = @_;
69: my ($trans_date, $post_date, $card_num, $description,
70: $category, $csv_debit, $csv_credit, $amount);
71: chomp(my $header = <>);
72: while (<>) {
73: next if (/^$/);
74: chomp;
75: s/(\d+)-(\d+)-(\d+)/$1$2$3/g;
76: my $line = $_;
77: ($trans_date, $post_date, $card_num, $description,
78: $category, , $csv_debit, $csv_credit) = split /,/;
79: $description =~ s/#//g;
80: my ($debit, $credit);
81: my $booking = "";
82: my $account = "";
83: foreach my $regex (@$compiled_ref) {
84: if ($line =~ /$regex/) {
85: $account = %$accounts_ref{$regex};
86: $booking = %$bookings_ref{$regex};
87: last;
88: }
89: }
90: if ($booking eq "" || $account eq "") {
91: die "import parse error: $line";
92: }
93: if ($csv_debit =~ /^$/) {
94: $amount = $csv_credit;
95: $credit = $account;
96: $debit = $csv_account;
97: } else {
98: $amount = $csv_debit;
99: $credit = $csv_account;
100: $debit = $account;
101: }
102: print "$post_date $booking $debit $credit $amount " .
103: "$description\n";
104: }
105: }
1.9 ! schwarze 106:
! 107: sub import_optum_hsa {
! 108: my ($bookings_ref, $accounts_ref, $compiled_ref,
! 109: $csv_account) = @_;
! 110: my ($date, $description, $amount,
! 111: $type);
! 112: chomp(my $header = <>);
! 113: while (<>) {
! 114: next if (/^$/);
! 115: chomp;
! 116: s#(\d+)-(\d+)-(\d+)#$1$2$3#;
! 117: my $line = $_;
! 118: ($date, $description, $amount,
! 119: $type) = split /,/;
! 120: $description =~ s/#//g;
! 121: $amount =~ s/\$//;
! 122: my ($debit, $credit);
! 123: my $booking = "";
! 124: my $account = "";
! 125: foreach my $regex (@$compiled_ref) {
! 126: if ($line =~ /$regex/) {
! 127: $account = %$accounts_ref{$regex};
! 128: $booking = %$bookings_ref{$regex};
! 129: last;
! 130: }
! 131: }
! 132: if ($booking eq "" || $account eq "") {
! 133: die "import parse error: $line";
! 134: }
! 135: if ($amount <= 0) {
! 136: $amount = substr $amount, 1;
! 137: $credit = $csv_account;
! 138: $debit = $account;
! 139: } else {
! 140: $debit = $csv_account;
! 141: $credit = $account;
! 142: }
! 143: print "$date $booking $debit $credit $amount " .
! 144: "$description\n";
! 145: }
! 146: }
! 147:
! 148: sub usage () {
! 149: printf STDERR "usage: %s -I bankname csvfilename\n", $0;
! 150: exit 1;
! 151: }
! 152:
1.1 schwarze 153: # === MAIN PROGRAM =====================================================
154:
1.9 ! schwarze 155: my ($csv_account, $fn, $in, $sub_name, %accounts, %bookings, @compiled);
! 156: getopts 'I:' or usage;
! 157: if ($opt_I) {
! 158: $sub_name = "import_" . $opt_I;
! 159: $fn = $sub_name . ".txt";
! 160: open $in, $fn or die "$fn: $!";
1.1 schwarze 161: } else {
1.9 ! schwarze 162: usage;
! 163: }
! 164: while (<$in>) {
! 165: chomp;
! 166: next if /^(?:#|$)/;
! 167: my $line = $_;
! 168: if (/^ACCOUNT\s+(\d+)$/) {
! 169: $csv_account and die "duplicate ACCOUNT line: $1";
! 170: $csv_account = $1;
! 171: next;
! 172: }
! 173: s/^(.*),\s+(\d+)\s+(\S+)// or
! 174: die "$fn import parse error: $line";
! 175: my ($reg, $account, $booking) = ($1, $2, $3);
! 176: $reg =~ s/(?:^|(?<=,))(?:$|(?=,))/[^,]*/g;
! 177: $reg = qr/$reg/;
! 178: push @compiled, $reg;
! 179: $bookings{$reg} = $booking;
! 180: $accounts{$reg} = $account;
1.1 schwarze 181: }
1.9 ! schwarze 182: close $in;
! 183: $csv_account or die "no ACCOUNT line in $fn";
! 184: my $sub_ref = \&$sub_name;
! 185: &$sub_ref(\%bookings, \%accounts, \@compiled, $csv_account);
CVSweb