#!/usr/bin/perl # Copyright (c) 2020 Freda Bundchen # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. use warnings; use strict; # === SUBROUTINES ===================================================== sub import_chase_credit_card { my $csv_account = "1234"; # DEBUG. hard-coded for now my $fn = 'import_chase_credit.txt'; my (@compiled, %bookings, %accounts); open my $in, $fn or die "$fn: $!"; while (<$in>) { chomp; next if /^(?:#|$)/; my $line = $_; s/^(.*),\s+(\d+)\s+(\S+)// or die "$fn import parse error: $line"; my ($reg, $account, $booking) = ($1, $2, $3); $reg =~ s/,,/,.*?/g; $reg = qr/$reg/; push @compiled, $reg; $bookings{$reg} = $booking; $accounts{$reg} = $account; } close $in; while (<>) { chomp; s#(\d+)/(\d+)/(\d+)#$3$1$2#g; my $line = $_; my ($trans_date, $post_date, $description, $category, $type, $amount) = split /,/; $description =~ s/#//g; my ($debit, $credit); my $booking = ""; my $account = ""; foreach my $regex (@compiled) { if ($line =~ /$regex/) { $account = $accounts{$regex}; $booking = $bookings{$regex}; next; } } if ($booking eq "" || $account eq "") { die "$fn import parse error: $line"; } if ($amount <= 0) { $amount = substr $amount, 1; $credit = $csv_account; $debit = $account; } else { $debit = $csv_account; $credit = $account; } print "$post_date $booking $debit $credit $amount " . "$description\n"; } } # === MAIN PROGRAM ===================================================== my ($chase_credit_card_header, $firstLine); $chase_credit_card_header = "Transaction Date,Post Date," . "Description,Category,Type,Amount"; chomp($firstLine = <>); if ($firstLine eq $chase_credit_card_header) { import_chase_credit_card; } else { print "Undefined header\n"; }