| 1 | package Business::NAB::BPAY::Payments; | ||||||||||
| 2 | 3 | $Business::NAB::BPAY::Payments::VERSION = '0.11'; | |||||||||
| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 3 | ▶ | use feature qw/ signatures /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 3 | ▶ | use autodie qw/ :all /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 3 | ▶ | use Carp qw/ croak /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 3 | with 'Business::NAB::Role::AttributeContainer'; | |||||||||
| 44 | 3 | extends 'Business::NAB::FileContainer'; | |||||||||
| 45 | |||||||||||
| 46 | 3 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 3 | ▶ | no warnings qw/ experimental::signatures /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | |||||||||||
| 49 | 3 | ▶ | use List::Util qw/ sum0 /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 3 | ▶ | use Business::NAB::BPAY::Payments::TrailerRecord; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | |||||||||||
| 52 | # we have long namespaces and use them multiple times so have | ||||||||||
| 53 | # normalised them out into the $parent and @subclasses below | ||||||||||
| 54 | 3 | my $parent = 'Business::NAB::BPAY::Payments'; | |||||||||
| 55 | |||||||||||
| 56 | 3 | 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 | 3 | __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 | 2 | ▶ | sub new_from_file ( | ||||||||
|
Subroutine
new_from_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 107 | $class, | ||||||||||
| 108 | $file, | ||||||||||
| 109 | 2 | $class_parent = $parent, # undocumented as called by subclasses | |||||||||
| 110 | ) { | ||||||||||
| 111 | |||||||||||
| 112 | 2 | my %sub_class_map = ( | |||||||||
| 113 | 1 => 'HeaderRecord', | ||||||||||
| 114 | 2 => 'DetailRecord', | ||||||||||
| 115 | 9 => 'TrailerRecord', | ||||||||||
| 116 | ); | ||||||||||
| 117 | |||||||||||
| 118 | 2 | ▶ | my $self = ref( $class ) ? $class : $class->new; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | |||||||||||
| 120 | 2 | 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 | 7 | ▶ | sub to_file ( | ||||||||
|
Subroutine
to_file: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 137 | $self, | ||||||||||
| 138 | $file, | ||||||||||
| 139 | 7 | $sep = "\r\n", | |||||||||
| 140 | ) { | ||||||||||
| 141 | 7 | open( my $fh, '>', $file ); | |||||||||
| 142 | |||||||||||
| 143 | 7 | print $fh $self->header_record->[ 0 ]->to_record . $sep; | |||||||||
| 144 | 7 | print $fh $_->to_record . $sep foreach $self->detail_record->@*; | |||||||||
| 145 | |||||||||||
| 146 | 7 | ▶ | if ( my $TrailerRecord = $self->trailer_record->[ 0 ] ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 4 | print $fh $TrailerRecord->to_record . $sep; | |||||||||
| 148 | } else { | ||||||||||
| 149 | 3 | my $total_value = sum0 map { $_->amount } | |||||||||
| 150 | $self->detail_record->@*; | ||||||||||
| 151 | |||||||||||
| 152 | 3 | my $TrailerRecord = Business::NAB::BPAY::Payments::TrailerRecord->new( | |||||||||
| 153 | total_value_of_payments => sprintf( "%.0f",$total_value ), | ||||||||||
| 154 | total_number_of_payments => scalar( $self->detail_record->@* ), | ||||||||||
| 155 | ); | ||||||||||
| 156 | |||||||||||
| 157 | 3 | print $fh $TrailerRecord->to_record . $sep; | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | 7 | close( $fh ); | |||||||||
| 161 | |||||||||||
| 162 | 7 | 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 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||