NAB/AccountInformation/File.pm

filter: Statementstmt 96.5% 113 / 117 Branchbran 88.6% 39 / 44 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 18 / 18 Podpod 50.0% 2 / 4 totaltotal 94.3% 183 / 194 CC 28 SCAR 18.9
CC
28
Cov
95%
new_from_file
12.0
validate_totals
9.3
reconstruct_file_records
7.0
CRAP 6.6
↑ Summary NAB/Australian/DirectEntry/Report.pm »
1 package Business::NAB::AccountInformation::File;
2 1 $Business::NAB::AccountInformation::File::VERSION = '0.11';
3 =head1 NAME
4  
5 Business::NAB::AccountInformation::File
6  
7 =head1 SYNOPSIS
8  
9 use Business::NAB::AccountInformation::File;
10  
11 # parse
12 my $AccountInfo = Business::NAB::AccountInformation::File
13 ->new_from_file( $file_path );
14  
15 foreach my $Group ( $AccountInfo->groups->@* ) {
16  
17 foreach my $Account ( $Group->accounts->@* ) {
18  
19 foreach my $Transaction ( $Account->transactions->@* ) {
20  
21 ...
22 }
23 }
24 }
25  
26 =head1 DESCRIPTION
27  
28 Class for parsing a NAB "Account Information File (NAI/BAI2)" file
29  
30 =cut
31  
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use feature qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use autodie qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶no warnings qw/ experimental::signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Text::CSV_XS qw/ csv /;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Business::NAB::Types qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
45 add_max_string_attribute
46 1 /;
47  
48 # we have long namespaces and use them multiple times so have
49 # normalised them out into the $parent and @subclasses below
50 1 my $parent = 'Business::NAB::AccountInformation';
51  
52 1 my @subclasses = (
53 qw/
54 Transaction
55 Group
56 Account
57 /
58 );
59  
60 1 load( $parent . "::$_" ) for @subclasses;
61  
62 =head1 ATTRIBUTES
63  
64 =over
65  
66 =item sender_identification (Str, max length 8)
67  
68 =item receiver_identification (Str, max length 4096)
69  
70 =item file_creation_date (DateTime)
71  
72 =item file_creation_time (Str, max length 4)
73  
74 =item file_sequence_number (NAB::Type::PositiveInt)
75  
76 =item physical_record_length (NAB::Type::PositiveIntOrZero)
77  
78 =item blocking_factor (NAB::Type::PositiveIntOrZero)
79  
80 =item version_number (NAB::Type::PositiveInt)
81  
82 =item control_total_a (Int)
83  
84 =item number_of_groups (Int)
85  
86 =item number_of_records (Int)
87  
88 =item control_total_b (Int)
89  
90 =item groups (ArrayRef[Business::NAB::AccountInformation::Group])
91  
92 =back
93  
94 =cut
95  
96 1 foreach my $str_attr (
97 'sender_identification[8]',
98 'receiver_identification[4096]',
99 'file_creation_time[4]',
100 ) {
101 3 __PACKAGE__->add_max_string_attribute(
102 $str_attr,
103 is => 'ro',
104 required => 1,
105 );
106 }
107  
108 1 has 'file_creation_date' => (
109 isa => 'NAB::Type::StatementDate',
110 is => 'ro',
111 required => 1,
112 coerce => 1,
113 );
114  
115 1 has [
116 qw/
117 file_sequence_number
118 blocking_factor
119 /
120 ] => (
121 isa => 'NAB::Type::PositiveIntOrZero',
122 is => 'ro',
123 required => 1,
124 );
125  
126 has [
127 qw/
128 version_number
129 /
130 ] => (
131 isa => 'NAB::Type::PositiveInt',
132 is => 'ro',
133 required => 0,
134 1 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
135 1 );
136  
137 1 has [
138 qw/
139 physical_record_length
140 /
141 ] => (
142 isa => 'Maybe[Str]',
143 is => 'ro',
144 required => 0,
145 predicate => '_has_physical_record_length',
146 );
147  
148 1 has [
149 qw/
150 control_total_a
151 number_of_groups
152 number_of_records
153 control_total_b
154 _raw_record_count
155 /
156 ] => (
157 isa => 'Int',
158 is => 'rw',
159 );
160  
161 1 subtype "Groups"
162 => as "ArrayRef[${parent}::Group]";
163  
164 has 'groups' => (
165 traits => [ 'Array' ],
166 is => 'rw',
167 isa => 'Groups',
168 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
169 1 handles => {
170 "add_group" => 'push',
171 },
172 );
173  
174 =head1 METHODS
175  
176 =head2 new_from_file
177  
178 Returns a new instance of the class with attributes populated from
179 the result of parsing the passed file
180  
181 my $Payments = Business::NAB::AccountInformation::File
182 ->new_from_file( $file_path );
183  
184 =cut
185  
186 3 ▶sub new_from_file ( $class, $file ) {
Subroutine new_from_file: called CC=12 SCAR=24.8
Pod documented
187  
188 3 my $reconstructed_records = $class->reconstruct_file_records( $file );
189  
190 3 my ( $File, $Group, $Account );
191  
192 3 foreach my $record ( $reconstructed_records->{ records }->@* ) {
193  
194 40 my ( $record_type, @rest ) = $record->@*;
195  
196 40 ▶ if ( $record_type eq '01' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_type eq "01") 3 37 40
197 3 $File = _file_header( $class, @rest );
198 }
199  
200 40 ▶ if ( $record_type eq '02' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_type eq "02") 3 37 40
201 3 $Group = Business::NAB::AccountInformation::Group
202 ->new_from_record( $record_type, @rest );
203  
204 3 $File->add_group( $Group );
205 }
206  
207 40 ▶ if ( $record_type eq '03' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_type eq "03") 8 32 40
208 8 $Account = Business::NAB::AccountInformation::Account
209 ->new_from_record( $record_type, @rest );
210  
211 8 $Group->add_account( $Account );
212 }
213  
214 40 ▶ if ( $record_type eq '16' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_type eq 16) 12 28 40
215 12 my $Transaction = Business::NAB::AccountInformation::Transaction
216 ->new_from_record( $record_type, @rest );
217  
218 12 $Account->add_transaction( $Transaction );
219 }
220  
221 40 ▶ if ( $record_type eq '49' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_type eq 49) 8 32 40
222 8 $Account->control_total_a( $rest[ 0 ] );
223  
224 8 ▶ $File->is_bai2
Branch: 1 branch
BranchTrueFalseTotal
$File->is_bai2 ? : 5 3 8
225 ? $Account->number_of_records( $rest[ 1 ] )
226 : $Account->control_total_b( $rest[ 1 ] );
227  
228 8 $Account->validate_totals( $File->is_bai2 );
229 }
230  
231 40 ▶ if ( $record_type eq '98' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_type eq 98) 3 37 40
232 3 $Group->control_total_a( $rest[ 0 ] );
233 3 $Group->number_of_accounts( $rest[ 1 ] );
234  
235 3 ▶ $File->is_bai2
Branch: 1 branch
BranchTrueFalseTotal
$File->is_bai2 ? : 2 1 3
236 ? $Group->number_of_records( $rest[ 2 ] )
237 : $Group->control_total_b( $rest[ 2 ] );
238  
239 3 $Group->validate_totals( $File->is_bai2 );
240 }
241  
242 40 ▶ if ( $record_type eq '99' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_type eq 99) 3 37 40
243 3 $File->control_total_a( $rest[ 0 ] );
244 3 $File->number_of_groups( $rest[ 1 ] );
245 3 $File->number_of_records( $rest[ 2 ] );
246 3 ▶ $File->control_total_b( $rest[ 3 ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $File->is_bai2 1 2 3
247 if !$File->is_bai2;
248 $File->_raw_record_count(
249 $reconstructed_records->{ raw_record_count }
250 3 );
251  
252 3 $File->validate_totals;
253 }
254  
255 }
256  
257 3 return $File;
258 }
259  
260 =head2 validate_totals
261  
262 Checks if the control_total_a and control_total_b values match the
263 expected totals of the contained group items:
264  
265 $File->validate_totals;
266  
267 Will throw an exception if any total doesn't match the expected value.
268  
269 =cut
270  
271 5 ▶sub validate_totals ( $self ) {
Subroutine validate_totals: called CC=8 SCAR=22.3
Pod documented
272  
273 5 my $num_groups = scalar( $self->groups->@* );
274 5 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
if $num_groups != $self->number_of_groups 0 5 5
275 "number of nested groups ($num_groups) != number_of_groups "
276 0 . "(@{[ $self->number_of_groups ]})"
277 ) if $num_groups != $self->number_of_groups;
278  
279 5 my $num_raw_records = $self->_raw_record_count;
280 5 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
if $num_raw_records != $self->number_of_records 0 5 5
281 "number of records ($num_raw_records) != number_of_records "
282 0 . "(@{[ $self->number_of_records ]})"
283 ) if $num_raw_records != $self->number_of_records;
284  
285 5 my ( $group_total_a, $group_total_b ) = ( 0, 0 );
286  
287 5 foreach my $Group ( $self->groups->@* ) {
288 5 $group_total_a += $Group->control_total_a;
289  
290 5 ▶ if ( !$self->is_bai2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_bai2) 2 3 5
291 2 $group_total_b += $Group->control_total_b;
292 }
293 }
294  
295 croak(
296 5 ▶ "calculated sum ($group_total_a) != control_total_a "
Branch: 1 branch
BranchTrueFalseTotal
if $group_total_a != $self->control_total_a 0 5 5
297 0 . "(@{[$self->control_total_a]})"
298 ) if $group_total_a != $self->control_total_a;
299  
300 5 ▶ if ( !$self->is_bai2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_bai2) 2 3 5
301 2 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
if $group_total_b != $self->control_total_b 0 2 2
302 "calculated sum ($group_total_b) != control_total_b "
303 0 . "(@{[$self->control_total_b]})"
304 ) if $group_total_b != $self->control_total_b;
305 }
306  
307 5 return 1;
308 }
309  
310 =head1 reconstruct_file_records
311  
312 Returns the file contents as a hashref, having reconstructed the various
313 records within it, for easier parsing:
314  
315 my $records = Business::NAB::AccountInformation::File
316 ->reconstruct_file_records( $file );
317  
318 This is due to the file format being somewhat baroque and, essentially, a
319 CSV of fixed width meaning some lines get truncated and continued on the
320 next line or multiple lines.
321  
322 The returned hashref is of the form:
323  
324 {
325 records => @records,
326 raw_record_count => $raw_record_count,
327 }
328  
329 =cut
330  
331 4 ▶sub reconstruct_file_records ( $self, $file ) {
Subroutine reconstruct_file_records: called CC=7 SCAR=19.5
Pod undocumented
332  
333 4 open( my $fh, '<', $file );
334  
335 4 my ( $field_continues, @records, $raw_record_count );
336  
337 4 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 99 4 103
338  
339 99 ▶ my $aoa = csv( in => \$line )
Branch: 1 branch
BranchTrueFalseTotal
my $aoa = csv("in", \$line) or croak("Text::CSV"->error_diag) 99 0 99
340 or croak( Text::CSV->error_diag );
341  
342 99 my ( $record_type, @rest ) = $aoa->[ 0 ]->@*;
343  
344 # a trailing / means the last field of the record is complete,
345 # otherwise it continues on the next line (which should be a
346 # Continuation record (88)
347 99 ▶ if ( $rest[ -1 ] =~ m!/$! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest[-1] =~ m[/$]) { } 84 15 99
348 84 chop( $rest[ -1 ] );
349 84 $field_continues = 0;
350 } else {
351 15 $field_continues = 1;
352 }
353  
354 99 ▶ if ( $record_type eq '88' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_type eq '88') { } 43 56 99
355  
356 # continuation of the previous record, this is a little
357 # bit messy depending on the previous record type
358  
359 43 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($records[-1][0] eq '16' and scalar @{$records[-1]} == 7) { } 6 37 43
Condition100%3/3
$records[-1][0] eq 16 and scalar @{$records[-1]} == 7
!ll&&!rl&&r
3166
MC/DC100%2/2
$records[-1][0] eq 16 and scalar @{$records[-1]} == 7
$records[-1][0] eq 16 scalar @{$records[-1]} == 7
Truth table100%3/3
A and B
A= $records[-1][0] eq 16B= scalar @{$records[-1]} == 7
ABresult
0X0
100
111
360 $records[ -1 ][ 0 ] eq '16'
361  
362 # the previous record was complete, append to the
363 # last field in that record
364 && scalar( $records[ -1 ]->@* ) == 7
365 ) {
366 6 $records[ -1 ][ -1 ] .= ' ' . join( ' ', @rest );
367 } else {
368  
369 # the previous record was incomplete, complete it
370 37 $records[ -1 ] = [ @{ $records[ -1 ] }, @rest ];
371 }
372 } else {
373 56 push( @records, [ $record_type, @rest ] );
374 }
375  
376 99 $raw_record_count++;
377 }
378  
379 return {
380 4 records => \@records,
381 raw_record_count => $raw_record_count,
382 };
383 }
384  
385 =head1 is_bai2
386  
387 Boolean check on the file type
388  
389 if ( $File->is_bai2 ) {
390 ...
391 } else {
392 # it's an NAI file
393 ...
394 }
395  
396 =cut
397  
398 40 ▶sub is_bai2 ( $self ) {
Subroutine is_bai2: called CC=1 SCAR=0.0
Pod undocumented
399 40 return $self->version_number == 2;
400 }
401  
402 3 ▶sub _file_header ( $class, @fields ) {
Subroutine _file_header: called CC=4 SCAR=13.9
403  
404 3 ▶ return $class->new(
Branch: 1 branch
BranchTrueFalseTotal
scalar @fields == 8 ? : 2 1 3
Condition100%4/4
$fields[5] || 0
l!l
12
$fields[6] || 0
l!l
12
MC/DC100%1/1
$fields[5] || 0
$fields[5]
MC/DC100%1/1
$fields[6] || 0
$fields[6]
Truth table100%2/2
A
A= $fields[5]
Aresult
11
00
Truth table100%2/2
A
A= $fields[6]
Aresult
11
00
405 sender_identification => $fields[ 0 ],
406 receiver_identification => $fields[ 1 ],
407 file_creation_date => $fields[ 2 ],
408 file_creation_time => $fields[ 3 ],
409 file_sequence_number => $fields[ 4 ],
410 physical_record_length => $fields[ 5 ] || 0,
411 blocking_factor => $fields[ 6 ] || 0,
412  
413 ( scalar( @fields ) == 8 )
414 ? ( version_number => $fields[ 7 ] )
415 : (),
416 );
417 }
418  
419 =head1 SEE ALSO
420  
421 L<Business::NAB::Types>
422  
423 L<Business::NAB::AccountInformation::Group>
424  
425 L<Business::NAB::AccountInformation::Account>
426  
427 L<Business::NAB::AccountInformation::Transaction>
428  
429 =cut
430  
431 1 __PACKAGE__->meta->make_immutable;
↑ Summary NAB/Australian/DirectEntry/Report.pm »