Annotation of pta/pta_import.pl, Revision 1.3
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.3 ! schwarze 22: sub import_chase_credit_card {
! 23: my $csv_account = "1234"; # DEBUG. hard-coded for now
! 24: my $fn = 'import_chase_credit.txt';
! 25: my (@compiled, %bookings, %accounts);
! 26: open my $in, $fn or die "$fn: $!";
! 27: while (<$in>) {
! 28: chomp;
! 29: next if /^(?:#|$)/;
! 30: my $line = $_;
! 31: s/^(.*),\s+(\d+)\s+(\S+)// or
! 32: die "$fn import parse error: $line";
! 33: my ($reg, $account, $booking) = ($1, $2, $3);
! 34: $reg =~ s/,,/,.*?/g;
! 35: $reg = qr/$reg/;
! 36: push @compiled, $reg;
! 37: $bookings{$reg} = $booking;
! 38: $accounts{$reg} = $account;
! 39: }
! 40: close $in;
1.2 schwarze 41: while (<>) {
1.1 schwarze 42: chomp;
1.3 ! schwarze 43: s#(\d+)/(\d+)/(\d+)#$3$1$2#g;
! 44: my $line = $_;
1.1 schwarze 45: my ($trans_date, $post_date, $description, $category,
46: $type, $amount) = split /,/;
47: $description =~ s/#//g;
48: my ($debit, $credit);
1.3 ! schwarze 49: my $booking = "";
! 50: my $account = "";
! 51: foreach my $regex (@compiled) {
! 52: if ($line =~ /$regex/) {
! 53: $account = $accounts{$regex};
! 54: $booking = $bookings{$regex};
! 55: next;
! 56: }
! 57: }
! 58: if ($booking eq "" || $account eq "") {
! 59: die "$fn import parse error: $line";
! 60: }
1.1 schwarze 61: if ($amount <= 0) {
62: $amount = substr $amount, 1;
1.3 ! schwarze 63: $credit = $csv_account;
! 64: $debit = $account;
1.1 schwarze 65: } else {
1.3 ! schwarze 66: $debit = $csv_account;
! 67: $credit = $account;
1.1 schwarze 68: }
69: print "$post_date $booking $debit $credit $amount " .
70: "$description\n";
71: }
72: }
73:
74: # === MAIN PROGRAM =====================================================
75:
1.3 ! schwarze 76: my ($chase_credit_card_header, $firstLine);
! 77: $chase_credit_card_header = "Transaction Date,Post Date," .
1.1 schwarze 78: "Description,Category,Type,Amount";
1.2 schwarze 79: chomp($firstLine = <>);
1.1 schwarze 80: if ($firstLine eq $chase_credit_card_header) {
81: import_chase_credit_card;
82: } else {
1.3 ! schwarze 83: print "Undefined header\n";
1.1 schwarze 84: }
CVSweb