lib/CNAB.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 22 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 109 CC 12 SCAR 47.7
CC
12
Cov
0%
parse_cnab
156.0
BEGIN
2.0
BEGIN
2.0
CRAP 117.5
1 #!/usr/local/bin/perl
2  
3 package CNAB;
4 require 5.008;
5 use strict;
6  
7 require Exporter;
8 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
9 our @ISA = qw(Exporter);
10  
11 %EXPORT_TAGS = ( 'all' => [ qw(
12 parse_cnab
13 ) ] );
14  
15 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16  
17  
18 our @EXPORT = qw(
19 parse_cnab
20 );
21  
22 our $VERSION = '0.01';
23  
24 ###############################################################################
25 #
26 # parseCNAB
27 #
28 # Expects the raw contents of a CNAB file and a hashref to store the parse result:
29 # Returns 1 on success and 0 on failure
30 #
31 ###############################################################################
32 sub parse_cnab{
33 my @content = split("\n",shift @_); # transform input into array;
34 my $output = shift @_; # get the reference for the array which should get the results
35
36 if(scalar @content == 0){ return 0; }
37 if(!defined($output)){ return 0; }
38
39 my @records; # array of hashes with the information about the records
40
41 # information taken from the headers
42 my $cnab_version; # version of the CNAB standard
43 my $file_type; # type of file being processed (1 = "Client->Bank", 2 = "Bank->Client")
44 my $file_sequence; # sequence number for the whole file
45 my $batch_type; # type of batch being processed ('R' = "Client->Bank", 'T' = "Bank->Client")
46 my $total; # total number of records extracted from the footer
47  
48 while ( $_ = shift @content){
49 my $line_type = substr($_,7,1); # type of line being processed
50 my %record;
51  
52 # process special lines
53 if($line_type == 0){ # file header
54 $cnab_version = substr($_,163,3);
55 $file_type = substr($_,142,1);
56 $file_sequence = substr($_,157,6);
57 next;
58 }elsif($line_type == 1){ # batch header
59 $batch_type = substr($_,8,1);
60 next;
61 }elsif($line_type == 5){ # batch footer
62 next;
63 }elsif($line_type == 9){ # file footer
64 $total = substr($_,23,6);
65 $total = ($total - 4)/2; # subtract the two headers and footers from the total number of lines and divide by two, since each record uses two lines
66 next;
67 }
68
69 %record->{"bank_code"} = substr($_,0,3);
70 %record->{"status_code"} = substr($_,15,2); # see CNAB documentation
71 %record->{"status_reason_code"} = substr($_,213,10); # see CNAB documentation
72 %record->{"bank_agency"} = substr($_,17,5);
73 %record->{"bank_account"} = substr($_,23,12);
74 %record->{"our_number"} = substr($_,44,13);
75 %record->{"document_number"} = substr($_,58,15);
76 %record->{"valid_date"} = substr($_,73,8);
77 %record->{"value"} = substr($_,81,13) .".". substr($_,94,2);
78 %record->{"currency_code"} = substr($_,130,2);
79 $_ = shift @content; # go to the second line of the record (yes, we jump a line)
80 if(substr($_,8,5) % 2 > 0){ return 0; } # abort if the sequence number on the second line is not a multiple of 2 (we must have missed a line somewhere!)
81 %record->{"sequence"} = substr($_,8,5)/2;
82 %record->{"value_interest"} = substr($_,17,13) .".". substr($_,30,2);
83 %record->{"value_discount"} = substr($_,32,13) .".". substr($_,45,2);
84 %record->{"value_discount2"} = substr($_,47,13) .".". substr($_,60,2);
85 %record->{"value_payed"} = substr($_,77,13) .".". substr($_,90,2);
86 %record->{"value_credited"} = substr($_,92,13) .".". substr($_,105,2);
87 %record->{"date_payed"} = substr($_,137,8);
88 %record->{"date_credited"} = substr($_,145,8);
89
90 push @records, \%record;
91 }
92
93 if($cnab_version ne "030"){ return 0; } # invalid version of file
94 if($file_type != 2){ return 0; } # we only parse "Bank->Client"
95 if(scalar @records <= 0) { return 0; } # no records processed, fail
96 if($total != scalar @records){ return 0; } # total number of processed records doesn't match number extracted from file
97
98 %{$output}->{"sequence"} = $file_sequence;
99 @{%{$output}->{"records"}} = @records;
100
101 return 1;
102 }
103  
104 __END__