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

Annotation of pta/pta_import.pl, Revision 1.13

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.12      schwarze  121: my ($csv_account, $fn, $in, $account_name, $delim, @compiled);
1.13    ! schwarze  122:
        !           123: # Parse command line arguments.
1.9       schwarze  124: getopts 'I:' or usage;
                    125: if ($opt_I) {
1.10      schwarze  126:        $account_name = $opt_I;
1.13    ! schwarze  127:        $banks{$account_name} or die "unknown accountname: $account_name";
1.10      schwarze  128:        $fn = "import_" . $account_name . ".txt";
1.9       schwarze  129:        open $in, $fn or die "$fn: $!";
1.1       schwarze  130: } else {
1.9       schwarze  131:        usage;
                    132: }
1.13    ! schwarze  133:
        !           134: # Parse the configuration file.
1.9       schwarze  135: while (<$in>) {
                    136:        chomp;
                    137:        next if /^(?:#|$)/;
                    138:        my $line = $_;
1.12      schwarze  139:        if (/^ACCOUNT\s+(\S+)$/) {
1.9       schwarze  140:                $csv_account and die "duplicate ACCOUNT line: $1";
                    141:                $csv_account = $1;
                    142:                next;
                    143:        }
1.12      schwarze  144:        if (/^DELIM\s+(\S)$/) {
                    145:                $delim and die "duplicate DELIM line: $1";
                    146:                $delim = $1;
                    147:                next;
                    148:        }
                    149:        $delim or die "no DELIM line in $fn";
                    150:        s/^(.*)$delim\s+(\d+)\s+(\S+)// or
1.9       schwarze  151:            die "$fn import parse error: $line";
1.12      schwarze  152:        push @compiled, {
                    153:                re => [map { qr/$_/ } split /$delim/, $1],
                    154:                ac => $2,
                    155:                id => $3,
                    156:        };
1.1       schwarze  157: }
1.9       schwarze  158: close $in;
                    159: $csv_account or die "no ACCOUNT line in $fn";
1.13    ! schwarze  160:
        !           161: # Parse the CSV file from the bank.
        !           162: chomp(my $header = <>);
        !           163: while (<>) {
        !           164:        chomp;
        !           165:        next if (/^$/);
        !           166:        my $line = $_;
        !           167:        my @fields = split /$delim/, $line;
        !           168:        my $matches = 0;
        !           169:        my ($account, $booking);
        !           170:        foreach my $selector (@compiled) {
        !           171:                $matches = 1;
        !           172:                for (my $i = 0; $i <= $#{$selector->{re}}; $i++) {
        !           173:                        next if $fields[$i] =~ $selector->{re}[$i];
        !           174:                        $matches = 0;
        !           175:                        last;
        !           176:                }
        !           177:                if ($matches) {
        !           178:                        $account = $selector->{ac};
        !           179:                        $booking = $selector->{id};
        !           180:                        last;
        !           181:                }
        !           182:        }
        !           183:        $matches or die "unmatched CSV line: $line";
        !           184:        my ($date, $amount, $debit, $credit, $description) =
        !           185:            $banks{$account_name}->($account, $csv_account, $line);
        !           186:        $date && $amount && $debit && $credit && $description
        !           187:            or die "import parse error: $line";
        !           188:        $description =~ s/#//g;
        !           189:        print "$date $booking $debit $credit $amount $description\n";
        !           190: }

CVSweb