| 1 | package ACH::Parser; | ||
| 2 | |||
| 3 | $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use ACH; | ||
| 9 | |||
| 10 | sub _croak { require Carp; Carp::croak(@_) } | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | ACH::Parser - Parse an ACH formatted file to ACH perl object | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | Version: 0.01 | ||
| 19 | May 2006 | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | ACH::Parser is a simple, generic ACH file to ACH object parser. | ||
| 24 | It's intentional use is for testing purposes ONLY. ACH-Parser will | ||
| 25 | allow a developer to look at the particular fields in an ACH formatted | ||
| 26 | file. | ||
| 27 | |||
| 28 | =head1 USING ACH-Parser | ||
| 29 | |||
| 30 | use ACH::Parser; | ||
| 31 | |||
| 32 | my $file = 'RETODC0104A.ACH'; | ||
| 33 | my $ach = new ACH; | ||
| 34 | $ach->parse($file); | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | =head2 parse | ||
| 39 | |||
| 40 | Parses the ACH data into the ACH object | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | # Parse the ACH file formatted text into an ACH object | ||
| 45 | sub ACH::parse { | ||
| 46 | # Get the file name | ||
| 47 | my $self = shift; | ||
| 48 | my $file = shift or _croak "Need an ACH file"; | ||
| 49 | |||
| 50 | # Open the file | ||
| 51 | if ( open(INPUT, "$file") ) {} | ||
| 52 | else { print "Error: Couldn't open file $file\n"; die; } | ||
| 53 | |||
| 54 | # Get the file contents | ||
| 55 | my @data = <INPUT>; | ||
| 56 | my $dataline = $data[0]; | ||
| 57 | my $pos = 0; | ||
| 58 | |||
| 59 | # Loop Through all entries | ||
| 60 | while ($pos < length($dataline)) { | ||
| 61 | # Get the correct ACH format array and store all parsed data in a hash | ||
| 62 | my $desc = substr($dataline, $pos, 1); | ||
| 63 | my @dataArray = []; | ||
| 64 | |||
| 65 | # Make sure file descriptor is valid | ||
| 66 | if ($desc != 1 and $desc != 5 and $desc != 6 and $desc != 7 and $desc != 8 and $desc != 9) { | ||
| 67 | die "File Error: Code: $desc\n"; | ||
| 68 | } | ||
| 69 | |||
| 70 | # Iterate through the appropriate ACH file format array and parse the data | ||
| 71 | for (my $x=0; $x < @{$self->{_achFormats}{$desc}}; $x++) { | ||
| 72 | my $field = ${$self->{_achFormats}{$desc}}[$x]; | ||
| 73 | |||
| 74 | # Get the field name and length | ||
| 75 | my ($field_name, $field_length); | ||
| 76 | while ( my ($key, $value) = each(%$field) ) { $field_name = $key; $field_length = $value; } | ||
| 77 | |||
| 78 | # Get the ACH Data from the file | ||
| 79 | my $part = substr($dataline, $pos, $field_length); chomp $part; | ||
| 80 | my %hash = ($field_name => $part); | ||
| 81 | $dataArray[$x] = \%hash; | ||
| 82 | $pos += $field_length; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Save data to list | ||
| 86 | @{$self->{_achData}}[scalar @{$self->{_achData}}] = \@dataArray; | ||
| 87 | } | ||
| 88 | |||
| 89 | # Close the Input file | ||
| 90 | close (INPUT); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | =head2 CAVEATS | ||
| 95 | |||
| 96 | This package is created for testing purposes only. It shouldn't be used | ||
| 97 | for production programs or scripts. There are other commercial products | ||
| 98 | out there that may be a more efficient solution for accomplishing your | ||
| 99 | goals. | ||
| 100 | |||
| 101 | All records in an ACH file must be formatted in the following sequence | ||
| 102 | of records. IF the file is not formatted in this exact sequence, it | ||
| 103 | may be rejected. | ||
| 104 | |||
| 105 | ACH File Layout: | ||
| 106 | 1 - File Header Record | ||
| 107 | 5 - First Company/Batch Header Record | ||
| 108 | 6 - First Entry Detail Record | ||
| 109 | 7 - First Entry Detail Addenda Record (optional) | ||
| 110 | | | ||
| 111 | Multiples of Entry Detail Records | ||
| 112 | | | ||
| 113 | 6 - Last Entry Detail Record | ||
| 114 | 7 - Last Entry Detail Addenda Record (optional) | ||
| 115 | 8 - First Company/Batch Control Record | ||
| 116 | | | ||
| 117 | Multiples of Company/Batches | ||
| 118 | | | ||
| 119 | 5 - Last Company/Batch Header Record | ||
| 120 | 6 - First Entry Detail Record | ||
| 121 | 7 - First Entry Detail Addenda Record (optional) | ||
| 122 | | | ||
| 123 | Multiples of Entry Detail Records | ||
| 124 | | | ||
| 125 | 6 - Last Entry Detail Record | ||
| 126 | 7 - Last Entry Detail Addenda Record (optional) | ||
| 127 | 8 - Last Company/Batch Control Record | ||
| 128 | 9 - File Control Record | ||
| 129 | 9999...9999 (optional) | ||
| 130 | |||
| 131 | =head1 AUTHOR | ||
| 132 | |||
| 133 | Author: Christopher Kois | ||
| 134 | Date: May, 2006 | ||
| 135 | Contact: cpkois@cpan.org | ||
| 136 | |||
| 137 | =head1 COPYRIGHTS | ||
| 138 | |||
| 139 | The ACH-Parser module is Copyright (c) May, 2006 by Christopher Kois. | ||
| 140 | http://www.christopherkois.com All rights reserved. You may distribute this | ||
| 141 | module under the terms of GNU General Public License (GPL). | ||
| 142 | |||
| 143 | =head1 SUPPORT/WARRANTY | ||
| 144 | |||
| 145 | ACH-Parser is free Open Source software. IT COMES WITHOUT WARRANTY OR SUPPORT OF ANY KIND. | ||
| 146 | |||
| 147 | =head1 KNOWN BUGS | ||
| 148 | |||
| 149 | This is version 0.01 of ACH::Parser. There are currently no known bugs. | ||
| 150 | |||
| 151 | =head1 SEE ALSO | ||
| 152 | |||
| 153 | L<ACH>. L<ACH::Generator> | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | 1; |