[BACK]Return to pta_import.pl CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / pta

Annotation of pta/pta_import.pl, Revision 1.24

1.17      schwarze    1: #!/usr/bin/perl
1.1       schwarze    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 = (
1.16      schwarze   25:     "bbva_usa"              => \&import_bbva_usa,
1.24    ! freda      26:     "capital_one_360"       => \&import_capital_one_360,
1.13      schwarze   27:     "capital_one_credit"    => \&import_capital_one_credit,
                     28:     "chase_credit"          => \&import_chase_credit,
1.22      freda      29:     "etrade_ira"            => \&import_etrade_ira,
1.13      schwarze   30:     "optum_hsa"             => \&import_optum_hsa,
                     31:     "sparkasse_camt"        => \&import_sparkasse_camt,
1.15      schwarze   32:     "wellsfargo"            => \&import_wellsfargo,
1.13      schwarze   33: );
                     34:
1.1       schwarze   35: # === SUBROUTINES  =====================================================
1.16      schwarze   36:
                     37: sub import_bbva_usa ($$$) {
                     38:        my ($account, $csv_account, $line) = @_;
1.17      schwarze   39:        $line =~
1.16      schwarze   40:            m#(\d+)/(\d+)/(\d+),(.*?),\S+,(\(?\d+\.\d+\)?),# or
                     41:            die "parse error: $line";
                     42:        my ($month, $day, $year, $description, $amount) =
                     43:            ($1, $2, $3, $4, $5);
                     44:        $amount =~ s/\((\d+\.\d+)\)/-$1/;
                     45:        return "$year$month$day",
                     46:            get_accounts_by_amount_sign($amount, $account, $csv_account),
                     47:            $description;
                     48: }
1.1       schwarze   49:
1.10      schwarze   50: sub import_chase_credit ($$$) {
                     51:        my ($account, $csv_account, $line) = @_;
                     52:        my ($trans_date, $post_date, $description, $category,
                     53:            $type, $amount) = split /,/, $line;
                     54:        my ($debit, $credit);
1.17      schwarze   55:        $post_date =~ s#(\d+)/(\d+)/(\d+)#$3$1$2#;
1.10      schwarze   56:        ($amount, $debit, $credit) =
                     57:            get_accounts_by_amount_sign($amount, $account,
                     58:            $csv_account);
                     59:        return ($post_date, $amount, $debit, $credit, $description);
1.24    ! freda      60: }
        !            61:
        !            62: sub import_capital_one_360 ($$$) {
        !            63:        my ($account, $csv_account, $line) = @_;
        !            64:        my @fields = split /,/, $line;
        !            65:        $fields[1] =~ s#(\d+)/(\d+)/(\d+)#20$3$1$2#;
        !            66:        my ($amount, $debit, $credit) =
        !            67:            get_accounts_by_amount_sign($fields[2], $account,
        !            68:            $csv_account);
        !            69:        return ($fields[1], $amount, $debit, $credit, $fields[4]);
1.10      schwarze   70: }
                     71:
                     72: sub import_capital_one_credit ($$$) {
                     73:        my ($account, $csv_account, $line) = @_;
                     74:        my ($trans_date, $post_date, $card_num,
                     75:            $description, $category, $csv_debit,
                     76:            $csv_credit) = split /,/, $line;
1.17      schwarze   77:        $post_date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
                     78:        my ($amount, $debit, $credit) =
1.10      schwarze   79:            get_accounts_by_csv_col($account, $csv_account,
                     80:            $csv_debit, $csv_credit);
                     81:        return ($post_date, $amount, $debit, $credit, $description);
1.22      freda      82: }
                     83:
                     84: sub import_etrade_ira ($$$) {
                     85:        my ($account, $csv_account, $line) = @_;
                     86:        my @fields = split /,/, $line;
                     87:        $fields[0] =~ s#(\d+)/(\d+)/(\d+)#20$3$1$2#;
                     88:        my ($amount, $debit, $credit) =
                     89:            get_accounts_by_amount_sign($fields[5], $account,
                     90:            $csv_account);
                     91:        if ($fields[2] eq 'EQ') {
                     92:                $fields[8] .= " [$fields[3]]";
                     93:        }
                     94:        return ($fields[0], $amount, $debit, $credit,
                     95:            join ' quantity ', $fields[8], $fields[4]);
1.10      schwarze   96: }
                     97:
                     98: sub import_optum_hsa ($$$) {
                     99:         my ($account, $csv_account, $line) = @_;
                    100:         my ($date, $description, $amount,
                    101:             $type) = split /,/, $line;
                    102:        my ($debit, $credit);
                    103:        $date =~ s/(\d+)-(\d+)-(\d+)/$1$2$3/;
                    104:        $amount =~ s/\$//;
                    105:        ($amount, $debit, $credit) =
                    106:            get_accounts_by_amount_sign($amount, $account,
                    107:            $csv_account);
                    108:        return ($date, $amount, $debit, $credit, $description);
                    109: }
                    110:
1.11      schwarze  111: sub import_sparkasse_camt ($$$) {
                    112:         my ($account, $csv_account, $line) = @_;
                    113:        my @fields;
                    114:        $_ = $line;
                    115:        push @fields, $1 while s/"([^"]*)";?//;
                    116:        $_ eq "" or die "CAMT parse error before $_ in $line";
                    117:        @fields == 17 or die "not 17 but @fields fields: $line";
                    118:        $fields[1] =~ s/^(\d\d)\.(\d\d)\.(\d\d)$/20$3$2$1/
                    119:            or die "date parse error: $line";
                    120:        $fields[14] =~ s/,/./;
                    121:        return $fields[1],
                    122:            get_accounts_by_amount_sign($fields[14], $account, $csv_account),
                    123:            (join ' ', $fields[11], $fields[4]);
1.15      schwarze  124: }
                    125:
                    126: sub import_wellsfargo ($$$) {
                    127:        my ($account, $csv_account, $line) = @_;
                    128:        $line =~
1.19      freda     129:            m#^"(\d+)/(\d+)/(\d+)","(-?\d+\.\d+)","\*",".*","(.*?)"$# or
1.15      schwarze  130:            die "parse error: $line" ;
                    131:        my ($month, $day, $year, $amount, $description) =
                    132:            ($1, $2, $3, $4, $5);
                    133:        return "$year$month$day",
                    134:            get_accounts_by_amount_sign($amount, $account, $csv_account),
                    135:            $description;
1.11      schwarze  136: }
                    137:
1.10      schwarze  138: sub get_accounts_by_amount_sign ($$$) {
                    139:        my ($amount, $account, $csv_account) = @_;
                    140:        my ($debit, $credit);
                    141:        if ($amount <= 0) {
                    142:                $amount = substr $amount, 1;
1.17      schwarze  143:                $credit = $csv_account;
1.10      schwarze  144:                $debit = $account;
                    145:        } else {
1.17      schwarze  146:                $debit = $csv_account;
1.10      schwarze  147:                $credit = $account;
1.5       schwarze  148:        }
1.10      schwarze  149:        return ($amount, $debit, $credit);
1.5       schwarze  150: }
1.9       schwarze  151:
1.10      schwarze  152: sub get_accounts_by_csv_col ($$$$) {
                    153:        my ($account, $csv_account, $csv_debit, $csv_credit) = @_;
                    154:        my ($amount, $debit, $credit);
                    155:        if ($csv_debit eq "") {
                    156:                $amount = $csv_credit;
                    157:                $credit = $account;
                    158:                $debit = $csv_account;
                    159:        } else {
                    160:                $amount = $csv_debit;
                    161:                $credit = $csv_account;
                    162:                $debit = $account;
                    163:        }
                    164:        return ($amount, $debit, $credit);
1.9       schwarze  165: }
                    166:
                    167: sub usage () {
1.10      schwarze  168:        printf STDERR "usage: %s -I accountname csvfilename\n", $0;
1.9       schwarze  169:        exit 1;
                    170: }
                    171:
1.1       schwarze  172: # === MAIN PROGRAM =====================================================
                    173:
1.9       schwarze  174: getopts 'I:' or usage;
1.14      schwarze  175: unless ($opt_I) {
                    176:        warn "The option -I is required.";
1.9       schwarze  177:        usage;
                    178: }
1.14      schwarze  179: my $account_name = $opt_I;
                    180: $banks{$account_name} or die "unknown accountname: $account_name";
1.13      schwarze  181:
                    182: # Parse the configuration file.
1.23      freda     183: my $fn = "import/" . $account_name . ".txt";
1.14      schwarze  184: open my $in, '<', $fn or die "$fn: $!";
1.20      freda     185: my ($csv_account, $delim, $header, $quote, @compiled, @ignored);
1.9       schwarze  186: while (<$in>) {
                    187:        chomp;
                    188:        next if /^(?:#|$)/;
                    189:        my $line = $_;
1.12      schwarze  190:        if (/^ACCOUNT\s+(\S+)$/) {
1.9       schwarze  191:                $csv_account and die "duplicate ACCOUNT line: $1";
                    192:                $csv_account = $1;
                    193:                next;
                    194:        }
1.12      schwarze  195:        if (/^DELIM\s+(\S)$/) {
                    196:                $delim and die "duplicate DELIM line: $1";
                    197:                $delim = $1;
                    198:                next;
                    199:        }
1.14      schwarze  200:        if (/^HEADER\s+(.*)$/) {
                    201:                $header and die "duplicate HEADER line: $1";
                    202:                $header = $1;
                    203:                next;
                    204:        }
1.18      freda     205:        if (/^QUOTE\s+(\S)$/) {
                    206:                $quote and die "duplicate QUOTE line: $1";
                    207:                $quote = $1;
                    208:                next;
                    209:        }
1.20      freda     210:        if (/^IGNORE\s+(.*)/) {
                    211:                push @ignored, qr/$1/;
                    212:                next;
                    213:        }
1.12      schwarze  214:        $delim or die "no DELIM line in $fn";
                    215:        s/^(.*)$delim\s+(\d+)\s+(\S+)// or
1.9       schwarze  216:            die "$fn import parse error: $line";
1.12      schwarze  217:        push @compiled, {
                    218:                re => [map { qr/$_/ } split /$delim/, $1],
                    219:                ac => $2,
                    220:                id => $3,
                    221:        };
1.1       schwarze  222: }
1.9       schwarze  223: close $in;
                    224: $csv_account or die "no ACCOUNT line in $fn";
1.13      schwarze  225:
                    226: # Parse the CSV file from the bank.
1.21      schwarze  227: if (@ARGV) {
                    228:        open STDIN, '<', $ARGV[0] or die "$ARGV[0]: $!";
                    229: }
                    230: LINE: while (<STDIN>) {
1.13      schwarze  231:        chomp;
                    232:        next if (/^$/);
1.20      freda     233:        foreach my $ignore (@ignored) {
                    234:                next LINE if /$ignore/;
                    235:        }
1.13      schwarze  236:        my $line = $_;
1.14      schwarze  237:        if ($header) {
                    238:                $line eq $header
                    239:                    or die "expected HEADER $header\nbut got $line";
                    240:                undef $header;
                    241:                next;
                    242:        }
1.18      freda     243:        my $copy_line = $line;
                    244:        my @fields;
                    245:        if ($quote) {
                    246:                push @fields, $1 while $copy_line =~ s/$quote([^$quote]*)$quote$delim?//;
                    247:        } else {
                    248:                @fields = split /$delim/, $line;
                    249:        }
1.13      schwarze  250:        my $matches = 0;
                    251:        my ($account, $booking);
                    252:        foreach my $selector (@compiled) {
                    253:                $matches = 1;
                    254:                for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
                    255:                        next if $fields[$i] =~ $selector->{re}[$i];
                    256:                        $matches = 0;
                    257:                        last;
                    258:                }
                    259:                if ($matches) {
                    260:                        $account = $selector->{ac};
                    261:                        $booking = $selector->{id};
                    262:                        last;
                    263:                }
                    264:        }
                    265:        $matches or die "unmatched CSV line: $line";
                    266:        my ($date, $amount, $debit, $credit, $description) =
                    267:            $banks{$account_name}->($account, $csv_account, $line);
                    268:        $date && $amount && $debit && $credit && $description
                    269:            or die "import parse error: $line";
                    270:        $description =~ s/#//g;
                    271:        print "$date $booking $debit $credit $amount $description\n";
                    272: }

CVSweb