| 1 | package Finance::Huntington::Statement; | ||
| 2 | |||
| 3 | require 5.005_62; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | use AutoLoader qw(AUTOLOAD); | ||
| 9 | use HTML::Parser; | ||
| 10 | use Time::Local; | ||
| 11 | |||
| 12 | our @ISA = qw(HTML::Parser Exporter); | ||
| 13 | |||
| 14 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 15 | ) ] ); | ||
| 16 | |||
| 17 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 18 | |||
| 19 | our @EXPORT = qw( | ||
| 20 | ); | ||
| 21 | |||
| 22 | # Preloaded methods go here. | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my $usage = <<END; | ||
| 26 | Usage: | ||
| 27 | new Finance::Huntington::Statement (); | ||
| 28 | END | ||
| 29 | |||
| 30 | my $pkg = shift; | ||
| 31 | |||
| 32 | die $usage unless $pkg; | ||
| 33 | |||
| 34 | # Bless our package. | ||
| 35 | my $parser = SUPER::new $pkg ( | ||
| 36 | api_version => 3, | ||
| 37 | text_h => [\&_process, "self, dtext"] | ||
| 38 | ); | ||
| 39 | |||
| 40 | # No broken text. | ||
| 41 | $parser->unbroken_text ('TRUE'); | ||
| 42 | |||
| 43 | # Set up defaults. | ||
| 44 | $parser->{current_section} = 0; | ||
| 45 | $parser->{sections} = [ | ||
| 46 | ["A C C O U N T.*?S T A T E M E N T", \&_process_account_statement], | ||
| 47 | ["Deposits", \&_process_deposits], | ||
| 48 | ["Check", \&_process_checks], | ||
| 49 | ["E-Pay", \&_process_epay], | ||
| 50 | ["ATM Transactions", \&_process_atm], | ||
| 51 | ["Debit Card", \&_process_debit_cards], | ||
| 52 | ["MDC Transactions", \&_process_mdc], | ||
| 53 | ["Account Navigation", sub {my $self = shift; $self->eof;}]]; | ||
| 54 | |||
| 55 | return $parser; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _process { | ||
| 59 | my $self = shift; | ||
| 60 | my $text = shift; | ||
| 61 | |||
| 62 | # Throw away meaningless lines. | ||
| 63 | return if $text =~ /^[\W\s]*$/; | ||
| 64 | |||
| 65 | # Trim leading/trailing whitespace. | ||
| 66 | $text =~ s/^([^\w\(\)]*)(.*?)([^\w\(\)]*)$/$2/; | ||
| 67 | # Escape embedded parens so they don't blow up in regex's. | ||
| 68 | $text =~ s/([\(\)])/\\$1/g; | ||
| 69 | |||
| 70 | # See if we are in the next document section yet. | ||
| 71 | if ($self->{current_section} < @{$self->{sections}}) { | ||
| 72 | my $regex = $self->{sections}->[$self->{current_section}]->[0]; | ||
| 73 | $self->{current_section}++ if ( $text =~ /$regex/ ); | ||
| 74 | } | ||
| 75 | |||
| 76 | # Process according to the section we are in. We are not | ||
| 77 | # considered to be in a section until we have actually matched | ||
| 78 | # the key of the section. For this reason, we execute the | ||
| 79 | # subroutine of $self->{current_section} - 1. | ||
| 80 | if ($self->{current_section} > 0) { | ||
| 81 | no strict 'refs'; | ||
| 82 | &{ $self->{sections}->[$self->{current_section} - 1]->[1] } ($self, $text); | ||
| 83 | use strict 'refs'; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _process_account_statement { | ||
| 88 | my $self = shift; | ||
| 89 | my $text = shift; | ||
| 90 | |||
| 91 | # Determine what we are looking for. (We assume that no blank | ||
| 92 | # lines will be passed into here.) | ||
| 93 | if ($text =~ /$self->{sections}->[$self->{current_section} - 1]->[0]/) { | ||
| 94 | # Do nothing. | ||
| 95 | } | ||
| 96 | elsif (!defined ($self->{account_name})) { | ||
| 97 | $self->{account_name} = $text; | ||
| 98 | } | ||
| 99 | elsif (!defined ($self->{account_number})) { | ||
| 100 | $self->{account_number} = $text; | ||
| 101 | } | ||
| 102 | elsif (!defined ($self->{last_updated})) { | ||
| 103 | if ($text =~ m%(\d+)/(\d+)/(\d+)@(\d+):(\d+)%) { | ||
| 104 | $self->{last_updated} = | ||
| 105 | timelocal (0, $5, $4, $2, $1, $3); | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | $self->{last_updated} = 0; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | elsif (!defined ($self->{current_statement_balance})) { | ||
| 112 | if ($text =~ /(\d+)\.(\d+)/) { | ||
| 113 | $self->{current_statement_balance} = "${1}.${2}"; | ||
| 114 | } | ||
| 115 | # else we haven't got there yet | ||
| 116 | } | ||
| 117 | elsif (!defined ($self->{available_statement_balance})) { | ||
| 118 | if ($text =~ /(\d+)\.(\d+)/) { | ||
| 119 | $self->{available_statement_balance} = "${1}.${2}"; | ||
| 120 | } | ||
| 121 | # else we haven't got there yet | ||
| 122 | } | ||
| 123 | elsif (!defined ($self->{current_register_balance})) { | ||
| 124 | if ($text =~ /(\d+)\.(\d+)/) { | ||
| 125 | $self->{current_register_balance} = "${1}.${2}"; | ||
| 126 | } | ||
| 127 | # else we haven't got there yet | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _process_atm { | ||
| 132 | my $self = shift; | ||
| 133 | my $text = shift; | ||
| 134 | |||
| 135 | my @template = ( | ||
| 136 | 'number', | ||
| 137 | 'date', | ||
| 138 | 'payee', | ||
| 139 | 'category', | ||
| 140 | 'amount'); | ||
| 141 | |||
| 142 | if (!defined ($self->{atms})) { | ||
| 143 | $self->{atms} = []; | ||
| 144 | } | ||
| 145 | elsif ($text =~ /$self->{sections}->[$self->{current_section} - 1]->[0]/) { | ||
| 146 | # Do nothing. | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | $self->_process_template ( | ||
| 150 | $text, | ||
| 151 | 'atms', | ||
| 152 | \@template); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _process_checks { | ||
| 157 | my $self = shift; | ||
| 158 | my $text = shift; | ||
| 159 | |||
| 160 | my @template = ( | ||
| 161 | 'number', | ||
| 162 | 'date', | ||
| 163 | 'payee', | ||
| 164 | 'category', | ||
| 165 | 'amount'); | ||
| 166 | |||
| 167 | if (!defined ($self->{checks})) { | ||
| 168 | $self->{checks} = []; | ||
| 169 | } | ||
| 170 | elsif ($text =~ /$self->{sections}->[$self->{current_section} - 1]->[0]/) { | ||
| 171 | # Do nothing. | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | $self->_process_template ( | ||
| 175 | $text, | ||
| 176 | 'checks', | ||
| 177 | \@template); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | sub _process_debit_cards { | ||
| 182 | my $self = shift; | ||
| 183 | my $text = shift; | ||
| 184 | |||
| 185 | my @template = ( | ||
| 186 | 'number', | ||
| 187 | 'date', | ||
| 188 | 'payee', | ||
| 189 | 'category', | ||
| 190 | 'amount'); | ||
| 191 | |||
| 192 | if (!defined ($self->{debit_cards})) { | ||
| 193 | $self->{debit_cards} = []; | ||
| 194 | } | ||
| 195 | elsif ($text =~ /$self->{sections}->[$self->{current_section} - 1]->[0]/) { | ||
| 196 | # Do nothing. | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | $self->_process_template ( | ||
| 200 | $text, | ||
| 201 | 'debit_cards', | ||
| 202 | \@template); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _process_deposits { | ||
| 207 | my $self = shift; | ||
| 208 | my $text = shift; | ||
| 209 | |||
| 210 | my @template = ( | ||
| 211 | 'number', | ||
| 212 | 'date', | ||
| 213 | 'payor', | ||
| 214 | 'category', | ||
| 215 | 'amount'); | ||
| 216 | |||
| 217 | if (!defined ($self->{deposits})) { | ||
| 218 | $self->{deposits} = []; | ||
| 219 | # The regular expression for deposits will currently | ||
| 220 | # get hit twice in the Statement. The first time is | ||
| 221 | # not actually the deposits section. | ||
| 222 | $self->{current_section}--; | ||
| 223 | } | ||
| 224 | elsif ($text =~ /$self->{sections}->[$self->{current_section} - 1]->[0]/) { | ||
| 225 | # Do nothing. | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | $self->_process_template ( | ||
| 229 | $text, | ||
| 230 | 'deposits', | ||
| 231 | \@template); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | sub _process_epay { | ||
| 236 | my $self = shift; | ||
| 237 | my $text = shift; | ||
| 238 | |||
| 239 | my @template = ( | ||
| 240 | 'number', | ||
| 241 | 'date', | ||
| 242 | 'payee', | ||
| 243 | 'category', | ||
| 244 | 'amount'); | ||
| 245 | |||
| 246 | if (!defined ($self->{epays})) { | ||
| 247 | $self->{epays} = []; | ||
| 248 | } | ||
| 249 | elsif ($text =~ /$self->{sections}->[$self->{current_section} - 1]->[0]/) { | ||
| 250 | # Do nothing. | ||
| 251 | } | ||
| 252 | else { | ||
| 253 | $self->_process_template ( | ||
| 254 | $text, | ||
| 255 | 'epays', | ||
| 256 | \@template); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | sub _process_mdc { | ||
| 261 | my $self = shift; | ||
| 262 | my $text = shift; | ||
| 263 | |||
| 264 | my @template = ( | ||
| 265 | 'number', | ||
| 266 | 'date', | ||
| 267 | 'payee', | ||
| 268 | 'category', | ||
| 269 | 'amount'); | ||
| 270 | |||
| 271 | if (!defined ($self->{mdcs})) { | ||
| 272 | $self->{mdcs} = []; | ||
| 273 | } | ||
| 274 | elsif ($text =~ /$self->{sections}->[$self->{current_section} - 1]->[0]/) { | ||
| 275 | # Do nothing. | ||
| 276 | } | ||
| 277 | else { | ||
| 278 | $self->_process_template ( | ||
| 279 | $text, | ||
| 280 | 'mdcs', | ||
| 281 | \@template); | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | sub _process_template { | ||
| 286 | my $self = shift; | ||
| 287 | my $text = shift; | ||
| 288 | my $key = shift; | ||
| 289 | # Note: entries in this array must be spelled the same as | ||
| 290 | # the given section's sub-headings (case insensitive). | ||
| 291 | my $template = shift; | ||
| 292 | |||
| 293 | my $current; | ||
| 294 | my $stored_val = 0; | ||
| 295 | |||
| 296 | if (grep /^$text$/i, @{$template}) { | ||
| 297 | # This is the section sub-heading. Skip it. | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | $current = pop @{ $self->{$key} }; | ||
| 301 | |||
| 302 | if (!defined ($current)) { | ||
| 303 | $current = {}; | ||
| 304 | } | ||
| 305 | |||
| 306 | foreach (@{$template}) { | ||
| 307 | if (!defined ($current->{$_})) { | ||
| 308 | $current->{$_} = $text; | ||
| 309 | $stored_val = 1; | ||
| 310 | last; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | unless ($stored_val) { | ||
| 315 | # This entry is already full, we must create | ||
| 316 | # a new one. | ||
| 317 | push @{ $self->{$key} }, $current; | ||
| 318 | $current = {}; | ||
| 319 | $current->{@{$template}[0]} = $text; | ||
| 320 | } | ||
| 321 | |||
| 322 | push @{ $self->{$key} }, $current; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||
| 327 | |||
| 328 | 1; | ||
| 329 | __END__ |