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

Annotation of pta/pta_import.pl, Revision 1.16

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

CVSweb