| 1 | package Business::NAB::Australian::DirectEntry::Payments; | ||||||||||||||||||||||||||||||
| 2 | 3 | $Business::NAB::Australian::DirectEntry::Payments::VERSION = '0.11'; | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | Business::NAB::Australian::DirectEntry::Payments | ||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | use Business::NAB::Australian::DirectEntry::Payments; | ||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | # parse: | ||||||||||||||||||||||||||||||
| 12 | my $Payments = Business::NAB::Australian::DirectEntry::Payments | ||||||||||||||||||||||||||||||
| 13 | ->new_from_file( $file_path ); | ||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | foreach my $DetailRecord ( $Payments->detail_record->@* ) { | ||||||||||||||||||||||||||||||
| 16 | ... | ||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | # build: | ||||||||||||||||||||||||||||||
| 20 | my $Payments = Business::NAB::Australian::DirectEntry::Payments->new; | ||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | $Payments->add_descriptive_record( | ||||||||||||||||||||||||||||||
| 23 | .. # Business::NAB:: ... DescriptiveRecord object | ||||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | $Payments->add_detail_record( | ||||||||||||||||||||||||||||||
| 27 | .. # Business::NAB:: ... DetailRecord object | ||||||||||||||||||||||||||||||
| 28 | ) for ( @payments ); | ||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||
| 30 | # optional: | ||||||||||||||||||||||||||||||
| 31 | $Payments->add_total_record( | ||||||||||||||||||||||||||||||
| 32 | .. # Business::NAB:: ... TotalRecord object | ||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | $Payments->to_file( | ||||||||||||||||||||||||||||||
| 36 | $file_path, | ||||||||||||||||||||||||||||||
| 37 | $bsb_number, # if TotalRecord is not set | ||||||||||||||||||||||||||||||
| 38 | $separator, # defaults to "\r\n" | ||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | Class for building/parsing a "Australian Direct Entry Payments" file | ||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 48 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | use feature qw/ signatures /; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 50 | 3 | ▶ | use autodie qw/ :all /; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | use Carp qw/ croak /; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 54 | 3 | with 'Business::NAB::Role::AttributeContainer'; | |||||||||||||||||||||||||||||
| 55 | 3 | extends 'Business::NAB::FileContainer'; | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 58 | 3 | ▶ | no warnings qw/ experimental::signatures /; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | use List::Util qw/ sum0 /; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | # we have long namespaces and use them multiple times so have | ||||||||||||||||||||||||||||||
| 63 | # normalised them out into the $parent and @subclasses below | ||||||||||||||||||||||||||||||
| 64 | 3 | my $parent = 'Business::NAB::Australian::DirectEntry::Payments'; | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 3 | my @subclasses = ( | |||||||||||||||||||||||||||||
| 67 | qw/ | ||||||||||||||||||||||||||||||
| 68 | DescriptiveRecord | ||||||||||||||||||||||||||||||
| 69 | DetailRecord | ||||||||||||||||||||||||||||||
| 70 | TotalRecord | ||||||||||||||||||||||||||||||
| 71 | / | ||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||
| 76 | All attributes are ArrayRef[Obj] where Obj are of the Business::NAB::Australian::DirectEntry::Payments::* namespace: | ||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | DescriptiveRecord | ||||||||||||||||||||||||||||||
| 79 | DetailRecord | ||||||||||||||||||||||||||||||
| 80 | TotalRecord | ||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | Convenience methods are available for trivial addition of new elements | ||||||||||||||||||||||||||||||
| 83 | to the arrays: | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | $Payments->add_descriptive_record( $DescriptiveRecord ); | ||||||||||||||||||||||||||||||
| 86 | $Payments->add_detail_record( $DetailRecord ); | ||||||||||||||||||||||||||||||
| 87 | $Payments->add_total_record( $TotalRecord ); | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | =over | ||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||
| 91 | =item descriptive_record (ArrayRef[Obj]) | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | =item detail_record (ArrayRef[Obj]) | ||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | =item total_record (ArrayRef[Obj]) | ||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||
| 97 | =back | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 3 | __PACKAGE__->load_attributes( $parent, @subclasses ); | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | =head1 METHODS | ||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | =head2 new_from_file | ||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | Returns a new instance of the class with attributes populated from | ||||||||||||||||||||||||||||||
| 108 | the result of parsing the passed file | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | my $Payments = Business::NAB::Australian::DirectEntry::Payments | ||||||||||||||||||||||||||||||
| 111 | ->new_from_file( $file_path ); | ||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||
| 115 | 3 | ▶ | sub new_from_file ( | ||||||||||||||||||||||||||||
|
Subroutine
new_from_file: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 116 | 3 | $class, $file, $class_map = undef, $sub_parent = undef | |||||||||||||||||||||||||||||
| 117 | 3 | ) { | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||
| 119 | 3 | my %sub_class_map = ( | |||||||||||||||||||||||||||||
| 120 | 0 => 'DescriptiveRecord', | ||||||||||||||||||||||||||||||
| 121 | 1 => 'DetailRecord', | ||||||||||||||||||||||||||||||
| 122 | 7 => 'TotalRecord', | ||||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | 3 | my $self = $class->new; | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||
| 127 | 3 | ▶ | return $self->SUPER::new_from_file( | ||||||||||||||||||||||||||||
|
Condition80%4/5
$sub_parent // $parent
$class_map // \%sub_class_map
MC/DC0%0/2
$sub_parent // $parent
$sub_parent
$parent
MC/DC100%1/1
$class_map // \%sub_class_map
$class_map
Truth table66%2/3
A // B
A= $sub_parentB= $parent
Truth table100%2/2
A
A= $class_map
| |||||||||||||||||||||||||||||||
| 128 | ( $sub_parent // $parent ), | ||||||||||||||||||||||||||||||
| 129 | $file, | ||||||||||||||||||||||||||||||
| 130 | ( $class_map // \%sub_class_map ), | ||||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||
| 134 | =head2 to_file | ||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||
| 136 | Writes the file content to the passed file path: | ||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||
| 138 | $Payments->to_file( | ||||||||||||||||||||||||||||||
| 139 | $file_path, | ||||||||||||||||||||||||||||||
| 140 | $bsb_number, # if TotalRecord is not set, defaults to 999-999 | ||||||||||||||||||||||||||||||
| 141 | $separator, # defaults to "\r\n" | ||||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | This also does some validation on the totals in the records. | ||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | 6 | ▶ | sub to_file ( | ||||||||||||||||||||||||||||
|
Subroutine
to_file: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||
| 149 | $self, | ||||||||||||||||||||||||||||||
| 150 | $file, | ||||||||||||||||||||||||||||||
| 151 | 6 | $bsb_number = "999-999", | |||||||||||||||||||||||||||||
| 152 | 6 | $sep = "\r\n", | |||||||||||||||||||||||||||||
| 153 | ) { | ||||||||||||||||||||||||||||||
| 154 | 4 | my $credit_total = sum0 map { $_->amount } | |||||||||||||||||||||||||||||
| 155 | 6 | grep { $_->is_credit } $self->detail_record->@*; | |||||||||||||||||||||||||||||
| 156 | 39 | my $debit_total = sum0 map { $_->amount } | |||||||||||||||||||||||||||||
| 157 | 6 | grep { $_->is_debit } $self->detail_record->@*; | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||
| 159 | # sprintf to prevent floating point issues - we are dealing with | ||||||||||||||||||||||||||||||
| 160 | # minor units so there should *never* be a difference of < 1, if | ||||||||||||||||||||||||||||||
| 161 | # there is then it's floating point so just ignore that | ||||||||||||||||||||||||||||||
| 162 | 6 | my $net_total = sprintf( "%.0f",abs( $credit_total - $debit_total ) ); | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | # net total should be zero as it is the net of credit - debit records | ||||||||||||||||||||||||||||||
| 165 | # and there should always be a record that nets off the other record | ||||||||||||||||||||||||||||||
| 166 | # type totals (to describe where funds go to/from). however if this is | ||||||||||||||||||||||||||||||
| 167 | # a returns file then this check does not apply | ||||||||||||||||||||||||||||||
| 168 | 6 | ▶ | if ( | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$net_total != 0 and not $self->blessed =~ /::Returns$/
MC/DC100%2/2
$net_total != 0 and not $self->blessed =~ /::Returns$/
$net_total != 0
not $self->blessed =~ /::Returns$/
Truth table100%3/3
A and not(B)
A= $net_total != 0B= not $self->blessed =~ /::Returns$/
| |||||||||||||||||||||||||||||||
| 169 | $net_total != 0 | ||||||||||||||||||||||||||||||
| 170 | && $self->blessed !~ /::Returns$/ | ||||||||||||||||||||||||||||||
| 171 | ) { | ||||||||||||||||||||||||||||||
| 172 | 1 | croak( | |||||||||||||||||||||||||||||
| 173 | "Net total not equal to zero ($net_total) file detail records do" | ||||||||||||||||||||||||||||||
| 174 | . " not balance, you have debits missing a credit or vice-versa" | ||||||||||||||||||||||||||||||
| 175 | ); | ||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||
| 178 | 5 | open( my $fh, '>', $file ); | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | 5 | print $fh $self->descriptive_record->[ 0 ]->to_record . $sep; | |||||||||||||||||||||||||||||
| 181 | 5 | print $fh $_->to_record . $sep foreach $self->detail_record->@*; | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||
| 183 | 5 | ▶ | if ( my $TotalRecord = $self->total_record->[ 0 ] ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 184 | 3 | print $fh $TotalRecord->to_record . $sep; | |||||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||||
| 186 | 2 | ▶ | croak( "BSB number is required if total_record is not set" ) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 187 | if !$bsb_number; | ||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||
| 189 | 2 | my $record_count = scalar( $self->detail_record->@* ); | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | 2 | my $TotalRecord = Business::NAB::Australian::DirectEntry::Payments::TotalRecord->new( | |||||||||||||||||||||||||||||
| 192 | bsb_number => $bsb_number, | ||||||||||||||||||||||||||||||
| 193 | net_total_amount => sprintf( "%.0f",$net_total ), | ||||||||||||||||||||||||||||||
| 194 | credit_total_amount => sprintf( "%.0f",$credit_total ), | ||||||||||||||||||||||||||||||
| 195 | debit_total_amount => sprintf( "%.0f",$debit_total ), | ||||||||||||||||||||||||||||||
| 196 | record_count => $record_count, | ||||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||
| 199 | 2 | print $fh $TotalRecord->to_record . $sep; | |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||
| 202 | 5 | close( $fh ); | |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||
| 204 | 5 | return 1; | |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||
| 207 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||
| 209 | L<Business::NAB::Australian::DirectEntry::Payments::DescriptiveRecord> | ||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||
| 211 | L<Business::NAB::Australian::DirectEntry::Payments::DetailRecord> | ||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||
| 213 | L<Business::NAB::Australian::DirectEntry::Payments::TotalRecord> | ||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||
| 217 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||