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

Annotation of pta/pta_import.pl, Revision 1.6

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:
                     20: # === SUBROUTINES  =====================================================
                     21:
1.6     ! schwarze   22: sub parse_import_file ($) {
        !            23:        my $fn = shift;
1.5       schwarze   24:        my (@compiled, %bookings, %accounts, $import_type);
                     25:        my $csv_account = "";
                     26:        if ($fn eq "import_chase_credit.txt") {
                     27:                $import_type = "chase_credit";
                     28:        } elsif ($fn eq "import_capital_one_credit.txt") {
                     29:                $import_type = "capital_one_credit";
                     30:        } else {
                     31:                die "Undefined import type for: $fn";
                     32:        }
1.3       schwarze   33:        open my $in, $fn or die "$fn: $!";
                     34:        while (<$in>) {
                     35:                chomp;
                     36:                next if /^(?:#|$)/;
                     37:                my $line = $_;
1.5       schwarze   38:                if (/^ACCOUNT/) {
                     39:                        $csv_account = $line;
                     40:                        $csv_account =~ s/^ACCOUNT\s+(\d+)$/$1/;
                     41:                        next;
                     42:                }
                     43:                s/^(.*),\s+(\d+)\s+(\S+)// or
1.3       schwarze   44:                    die "$fn import parse error: $line";
                     45:                my ($reg, $account, $booking) = ($1, $2, $3);
1.4       schwarze   46:                $reg =~ s/(?:^|(?<=,))(?:$|(?=,))/[^,]*/g;
1.3       schwarze   47:                $reg = qr/$reg/;
                     48:                push @compiled, $reg;
                     49:                $bookings{$reg} = $booking;
                     50:                $accounts{$reg} = $account;
                     51:        }
                     52:        close $in;
1.5       schwarze   53:        die "CSV account not defined in $fn" if ($csv_account eq "");
                     54:        return (\%bookings, \%accounts, \@compiled, $csv_account, $import_type);
                     55: }
                     56:
1.6     ! schwarze   57: sub import_chase_credit_card ($$$$) {
1.5       schwarze   58:        my ($bookings_ref, $accounts_ref, $compiled_ref,
                     59:            $csv_account) = @_;
                     60:        my ($trans_date, $post_date, $description, $category,
                     61:            $type, $amount);
                     62:        chomp(my $header = <>);
1.2       schwarze   63:        while (<>) {
1.5       schwarze   64:                next if (/^$/);
1.1       schwarze   65:                chomp;
1.3       schwarze   66:                s#(\d+)/(\d+)/(\d+)#$3$1$2#g;
                     67:                my $line = $_;
1.5       schwarze   68:                ($trans_date, $post_date, $description, $category,
1.1       schwarze   69:                    $type, $amount) = split /,/;
                     70:                $description =~ s/#//g;
                     71:                my ($debit, $credit);
1.3       schwarze   72:                my $booking = "";
                     73:                my $account = "";
1.5       schwarze   74:                foreach my $regex (@$compiled_ref) {
1.3       schwarze   75:                        if ($line =~ /$regex/) {
1.5       schwarze   76:                                $account = %$accounts_ref{$regex};
                     77:                                $booking = %$bookings_ref{$regex};
1.4       schwarze   78:                                last;
1.3       schwarze   79:                        }
                     80:                }
                     81:                if ($booking eq "" || $account eq "") {
1.5       schwarze   82:                        die "import parse error: $line";
1.3       schwarze   83:                }
1.1       schwarze   84:                if ($amount <= 0) {
                     85:                        $amount = substr $amount, 1;
1.3       schwarze   86:                        $credit = $csv_account;
                     87:                        $debit = $account;
1.1       schwarze   88:                } else {
1.3       schwarze   89:                        $debit = $csv_account;
                     90:                        $credit = $account;
1.1       schwarze   91:                }
                     92:                print "$post_date $booking $debit $credit $amount " .
                     93:                    "$description\n";
                     94:        }
                     95: }
                     96:
1.6     ! schwarze   97: sub import_capital_one_credit_card ($$$$) {
1.5       schwarze   98:        my ($bookings_ref, $accounts_ref, $compiled_ref,
                     99:            $csv_account) = @_;
                    100:        my ($trans_date, $post_date, $card_num, $description,
                    101:            $category, $csv_debit, $csv_credit, $amount);
                    102:        chomp(my $header = <>);
                    103:        while (<>) {
                    104:                next if (/^$/);
                    105:                chomp;
                    106:                s/(\d+)-(\d+)-(\d+)/$1$2$3/g;
                    107:                my $line = $_;
                    108:                ($trans_date, $post_date, $card_num, $description,
                    109:                    $category, , $csv_debit, $csv_credit) = split /,/;
                    110:                $description =~ s/#//g;
                    111:                my ($debit, $credit);
                    112:                my $booking = "";
                    113:                my $account = "";
                    114:                foreach my $regex (@$compiled_ref) {
                    115:                        if ($line =~ /$regex/) {
                    116:                                $account = %$accounts_ref{$regex};
                    117:                                $booking = %$bookings_ref{$regex};
                    118:                                last;
                    119:                        }
                    120:                }
                    121:                if ($booking eq "" || $account eq "") {
                    122:                        die "import parse error: $line";
                    123:                }
                    124:                if ($csv_debit =~ /^$/) {
                    125:                        $amount = $csv_credit;
                    126:                        $credit = $account;
                    127:                        $debit = $csv_account;
                    128:                } else {
                    129:                        $amount = $csv_debit;
                    130:                        $credit = $csv_account;
                    131:                        $debit = $account;
                    132:                }
                    133:                print "$post_date $booking $debit $credit $amount " .
                    134:                    "$description\n";
                    135:        }
                    136: }
1.1       schwarze  137: # === MAIN PROGRAM =====================================================
                    138:
1.5       schwarze  139: # This begins the user-editable section to select the formatting file.
                    140: my $fn = "import_capital_one_credit.txt";
                    141: # my $fn = "import_chase_credit.txt";
                    142: # This ends the user-editable section to select the formatting file.
                    143:
                    144: my ($bookings_ref, $accounts_ref, $compiled_ref, $csv_account, $import_type) =
1.6     ! schwarze  145:     parse_import_file $fn;
1.5       schwarze  146: if ($import_type eq "chase_credit") {
1.6     ! schwarze  147:        import_chase_credit_card $bookings_ref, $accounts_ref,
        !           148:            $compiled_ref, $csv_account;
1.5       schwarze  149: } elsif ($import_type eq "capital_one_credit") {
1.6     ! schwarze  150:        import_capital_one_credit_card $bookings_ref, $accounts_ref,
        !           151:        $compiled_ref, $csv_account;
1.1       schwarze  152: } else {
1.5       schwarze  153:        die "Undefined import type: $import_type";
1.1       schwarze  154: }

CVSweb