File Coverage

blib/lib/Business/NAB/BPAY/Payments/Results.pm
Criterion Covered Total %
statement 42 42 100.0
branch 4 4 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 56 56 100.0


line stmt bran cond sub pod time code
1             package Business::NAB::BPAY::Payments::Results;
2             $Business::NAB::BPAY::Payments::Results::VERSION = '0.03';
3             =head1 NAME
4              
5             Business::NAB::BPAY::Payments::Results
6              
7             =head1 SYNOPSIS
8              
9             use Business::NAB::BPAY::Payments::Results;
10              
11             my $Payments = Business::NAB::BPAY::Payments::Results->new_from_file(
12             "/path/to/bpay/payments/batch/file_response.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_results.bpb",
27             $separator, # defaults to "\r\n"
28             );
29              
30              
31             =head1 DESCRIPTION
32              
33             Class for parsing / creating a NAB BPAY batch payments response files
34              
35             All methods and attributes are inherited from
36             L<Business::NAB::BPAY::Payments>
37              
38             =cut
39              
40 1     1   350317 use strict;
  1         3  
  1         48  
41 1     1   8 use warnings;
  1         30  
  1         85  
42 1     1   7 use feature qw/ signatures /;
  1         3  
  1         181  
43              
44 1     1   8 use Moose;
  1         2  
  1         16  
45             extends 'Business::NAB::BPAY::Payments';
46 1     1   8912 no warnings qw/ experimental::signatures /;
  1         4  
  1         87  
47              
48 1     1   10 use Business::NAB::BPAY::Payments::Results::TrailerRecord;
  1         2  
  1         587  
49              
50             # we have long namespaces and use them multiple times so have
51             # normalised them out into the $parent and @subclasses below
52             my $parent = 'Business::NAB::BPAY::Payments::Results';
53              
54             my @subclasses = (
55             qw/
56             HeaderRecord
57             DetailRecord
58             TrailerRecord
59             /
60             );
61              
62             __PACKAGE__->load_attributes( $parent, @subclasses );
63              
64 1     1 1 2335 sub new_from_file ( $class, $file ) {
  1         2  
  1         3  
  1         2  
65              
66 1         39 my $self = $class->new;
67              
68 1         12 return $self->SUPER::new_from_file(
69             $file, $parent
70             );
71             }
72              
73 3     3 1 7983 sub to_file ( $self, $file, $sep = "\r\n" ) {
  3         8  
  3         6  
  3         9  
  3         5  
74              
75 3 100       19 if ( !$self->trailer_record->[ 0 ] ) {
76              
77             my (
78 1         10 $total_value, $total_value_success, $total_value_failed,
79             $total_count, $total_count_success, $total_count_failed,
80             ) = ( 0, 0, 0, 0, 0, 0 );
81              
82 1         6 foreach my $Detail ( $self->detail_record->@* ) {
83 5         142 $total_value += $Detail->amount;
84 5         28 $total_count++;
85              
86 5 100       15 if ( $Detail->is_successful ) {
87 2         59 $total_value_success += $Detail->amount;
88 2         4 $total_count_success++;
89             } else {
90 3         98 $total_value_failed += $Detail->amount;
91 3         6 $total_count_failed++;
92             }
93             }
94              
95 1         45 my $TrailerRecord = Business::NAB::BPAY::Payments::Results::TrailerRecord->new(
96             total_value_of_payments => $total_value,
97             total_number_of_payments => $total_count,
98             total_value_of_successful_payments => $total_value_success,
99             total_number_of_successful_payments => $total_count_success,
100             total_value_of_declined_payments => $total_value_failed,
101             total_number_of_declined_payments => $total_count_failed,
102             );
103              
104 1         8 $self->add_trailer_record( $TrailerRecord );
105             }
106              
107 3         99 return $self->SUPER::to_file( $file, $sep );
108             }
109              
110             =head1 SEE ALSO
111              
112             L<Business::NAB::Types>
113              
114             L<Business::NAB::BPAY::Payments>
115              
116             L<Business::NAB::BPAY::Payments::Results::HeaderRecord>
117              
118             L<Business::NAB::BPAY::Payments::Results::DetailRecord>
119              
120             L<Business::NAB::BPAY::Payments::Results::TrailerRecord>
121              
122             =cut
123              
124             __PACKAGE__->meta->make_immutable;