File Coverage

blib/lib/Business/NAB/BPAY/Payments.pm
Criterion Covered Total %
statement 54 54 100.0
branch 4 4 100.0
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 72 72 100.0


line stmt bran cond sub pod time code
1             package Business::NAB::BPAY::Payments;
2             $Business::NAB::BPAY::Payments::VERSION = '0.03';
3             =head1 NAME
4              
5             Business::NAB::BPAY::Payments
6              
7             =head1 SYNOPSIS
8              
9             use Business::NAB::BPAY::Payments;
10              
11             my $Payments = Business::NAB::BPAY::Payments->new_from_file(
12             "/path/to/bpay/payments/batch/file.bpb",
13             );
14              
15             # parse
16             my $Header = $Payments->header_record->[0];
17              
18             foreach my $Payment ( $Payments->detail_record->@* ) {
19             ...
20             }
21              
22             my $Trailer = $Payments->trailer_record->[0];
23              
24             # create
25             $Payments->to_file(
26             "/path/to/bpay/payments/batch/file_output.bpb",
27             $separator, # defaults to "\r\n"
28             );
29              
30             =head1 DESCRIPTION
31              
32             Class for parsing / creating a NAB BPAY batch payments file
33              
34             =cut;
35              
36 3     3   1200986 use strict;
  3         7  
  3         135  
37 3     3   16 use warnings;
  3         8  
  3         213  
38 3     3   98 use feature qw/ signatures /;
  3         8  
  3         492  
39 3     3   1132 use autodie qw/ :all /;
  3         37108  
  3         27  
40 3     3   53314 use Carp qw/ croak /;
  3         7  
  3         251  
41              
42 3     3   609 use Moose;
  3         607260  
  3         28  
43             with 'Business::NAB::Role::AttributeContainer';
44             extends 'Business::NAB::FileContainer';
45              
46 3     3   30320 use Moose::Util::TypeConstraints;
  3         9  
  3         36  
47 3     3   9426 no warnings qw/ experimental::signatures /;
  3         8  
  3         184  
48              
49 3     3   20 use List::Util qw/ sum0 /;
  3         7  
  3         346  
50 3     3   1851 use Business::NAB::BPAY::Payments::TrailerRecord;
  3         23603  
  3         1952  
51              
52             # we have long namespaces and use them multiple times so have
53             # normalised them out into the $parent and @subclasses below
54             my $parent = 'Business::NAB::BPAY::Payments';
55              
56             my @subclasses = (
57             qw/
58             HeaderRecord
59             DetailRecord
60             TrailerRecord
61             /
62             );
63              
64             =head1 ATTRIBUTES
65              
66             All attributes are ArrayRef[Obj] where Obj are of the Business::NAB::BPAY*
67             namespace:
68              
69             HeaderRecord
70             DetailRecord
71             TrailerRecord
72              
73             Convenience methods are available for trivial addition of new elements
74             to the arrays:
75              
76             $Payments->add_header_record( $HeaderRecord );
77             $Payments->add_detail_record( $DetailRecord );
78             $Payments->add_trailer_record( $TrailerRecord );
79              
80             =over
81              
82             =item header_record (ArrayRef[Obj])
83              
84             =item detail_record (ArrayRef[Obj])
85              
86             =item trailer_record (ArrayRef[Obj])
87              
88             =back
89              
90             =cut
91              
92             __PACKAGE__->load_attributes( $parent, @subclasses );
93              
94             =head1 METHODS
95              
96             =head2 new_from_file
97              
98             Returns a new instance of the class with attributes populated from
99             the result of parsing the passed file
100              
101             my $Payments = Business::NAB::BPAY::Payments
102             ->new_from_file( $file_path );
103              
104             =cut
105              
106             sub new_from_file (
107 2         8 $class,
108 2         5 $file,
109 2         6 $class_parent = $parent, # undocumented as called by subclasses
110 2     2 1 3930 ) {
  2         3  
111              
112 2         37 my %sub_class_map = (
113             1 => 'HeaderRecord',
114             2 => 'DetailRecord',
115             9 => 'TrailerRecord',
116             );
117              
118 2 100       97 my $self = ref( $class ) ? $class : $class->new;
119              
120 2         21 return $self->SUPER::new_from_file(
121             $class_parent, $file, \%sub_class_map
122             );
123             }
124              
125             =head2 to_file
126              
127             Writes the file content to the passed file path:
128              
129             $Payments->to_file(
130             $file_path,
131             $separator, # defaults to "\r\n"
132             );
133              
134             =cut
135              
136             sub to_file (
137 6         15 $self,
138 6         11 $file,
139 6         15 $sep = "\r\n",
140 6     6 1 15796 ) {
  6         10  
141 6         70 open( my $fh, '>', $file );
142              
143 6         5459 print $fh $self->header_record->[ 0 ]->to_record . $sep;
144 6         42 print $fh $_->to_record . $sep foreach $self->detail_record->@*;
145              
146 6 100       38 if ( my $TrailerRecord = $self->trailer_record->[ 0 ] ) {
147 4         65 print $fh $TrailerRecord->to_record . $sep;
148             } else {
149 2         23 my $total_value = sum0 map { $_->amount }
  10         489  
150             $self->detail_record->@*;
151              
152 2         14 my $TrailerRecord = Business::NAB::BPAY::Payments::TrailerRecord->new(
153             total_value_of_payments => $total_value,
154             total_number_of_payments => scalar( $self->detail_record->@* ),
155             );
156              
157 2         13 print $fh $TrailerRecord->to_record . $sep;
158             }
159              
160 6         32 close( $fh );
161              
162 6         3741 return 1;
163             }
164              
165             =head1 SEE ALSO
166              
167             L<Business::NAB::Types>
168              
169             L<Business::NAB::BPAY::Payments::HeaderRecord>
170              
171             L<Business::NAB::BPAY::Payments::DetailRecord>
172              
173             L<Business::NAB::BPAY::Payments::TrailerRecord>
174              
175             =cut
176              
177             __PACKAGE__->meta->make_immutable;