| 1 | package Business::NAB::AccountInformation::Group; | ||||||||||
| 2 | 2 | $Business::NAB::AccountInformation::Group::VERSION = '0.11'; | |||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Business::NAB::AccountInformation::Group | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use Business::NAB::AccountInformation::Group; | ||||||||||
| 10 | |||||||||||
| 11 | my $Group = Business::NAB::AccountInformation::Group->new( | ||||||||||
| 12 | ultimate_receiver_identification => $ult_receiver_id, | ||||||||||
| 13 | originator_identification => $org_id, | ||||||||||
| 14 | as_of_date => $as_of_date, | ||||||||||
| 15 | additional_field => $additional_field, | ||||||||||
| 16 | control_total_a => $total_a, | ||||||||||
| 17 | number_of_accounts => $number_of_accounts, | ||||||||||
| 18 | control_total_b => $total_b, | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | Class for parsing a NAB "Account Information File (NAI/BAI2)" group | ||||||||||
| 24 | line (type C<02>). | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 2 | ▶ | use feature qw/ signatures state /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 2 | ▶ | use Carp qw/ croak /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 2 | ▶ | no warnings qw/ experimental::signatures /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 2 | ▶ | use Text::CSV_XS qw/ csv /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 2 | ▶ | use Business::NAB::Types qw/ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | add_max_string_attribute | ||||||||||
| 40 | 2 | /; | |||||||||
| 41 | |||||||||||
| 42 | =head1 ATTRIBUTES | ||||||||||
| 43 | |||||||||||
| 44 | =over | ||||||||||
| 45 | |||||||||||
| 46 | =item ultimate_receiver_identification (Str, max length 4096) | ||||||||||
| 47 | |||||||||||
| 48 | =item originator_identification (Str, max length 8) | ||||||||||
| 49 | |||||||||||
| 50 | =item as_of_date (DateTime) | ||||||||||
| 51 | |||||||||||
| 52 | =item additional_field | ||||||||||
| 53 | |||||||||||
| 54 | =item control_total_a (Int) | ||||||||||
| 55 | |||||||||||
| 56 | =item number_of_accounts (Int) | ||||||||||
| 57 | |||||||||||
| 58 | =item control_total_b (Int) | ||||||||||
| 59 | |||||||||||
| 60 | =item number_of_records (Int) | ||||||||||
| 61 | |||||||||||
| 62 | =item accounts (ArrayRef[Business::NAB::AccountInformation::Account]) | ||||||||||
| 63 | |||||||||||
| 64 | =back | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 2 | foreach my $str_attr ( | |||||||||
| 69 | 'ultimate_receiver_identification[4096]', | ||||||||||
| 70 | 'originator_identification[8]', | ||||||||||
| 71 | 'additional_field[4096]', | ||||||||||
| 72 | ) { | ||||||||||
| 73 | 6 | __PACKAGE__->add_max_string_attribute( | |||||||||
| 74 | $str_attr, | ||||||||||
| 75 | is => 'ro', | ||||||||||
| 76 | required => 1, | ||||||||||
| 77 | ); | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 2 | has 'as_of_date' => ( | |||||||||
| 81 | isa => 'NAB::Type::StatementDate', | ||||||||||
| 82 | is => 'ro', | ||||||||||
| 83 | required => 1, | ||||||||||
| 84 | coerce => 1, | ||||||||||
| 85 | ); | ||||||||||
| 86 | |||||||||||
| 87 | 2 | has [ | |||||||||
| 88 | qw/ | ||||||||||
| 89 | control_total_a | ||||||||||
| 90 | number_of_accounts | ||||||||||
| 91 | control_total_b | ||||||||||
| 92 | number_of_records | ||||||||||
| 93 | / | ||||||||||
| 94 | ] => ( | ||||||||||
| 95 | isa => 'Int', | ||||||||||
| 96 | is => 'rw', | ||||||||||
| 97 | ); | ||||||||||
| 98 | |||||||||||
| 99 | 2 | subtype "Accounts" | |||||||||
| 100 | => as "ArrayRef[Business::NAB::AccountInformation::Account]"; | ||||||||||
| 101 | |||||||||||
| 102 | has 'accounts' => ( | ||||||||||
| 103 | traits => [ 'Array' ], | ||||||||||
| 104 | is => 'rw', | ||||||||||
| 105 | isa => 'Accounts', | ||||||||||
| 106 | 4 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 107 | 2 | handles => { | |||||||||
| 108 | "add_account" => 'push', | ||||||||||
| 109 | }, | ||||||||||
| 110 | ); | ||||||||||
| 111 | |||||||||||
| 112 | =head1 METHODS | ||||||||||
| 113 | |||||||||||
| 114 | =head2 new_from_raw_record | ||||||||||
| 115 | |||||||||||
| 116 | Returns a new instance of the class with attributes populated from | ||||||||||
| 117 | the result of parsing the passed line: | ||||||||||
| 118 | |||||||||||
| 119 | my $Group = Business::NAB::AccountInformation::Group | ||||||||||
| 120 | ::Payments::DescriptiveRecord->new_from_raw_record( $line ); | ||||||||||
| 121 | |||||||||||
| 122 | =cut | ||||||||||
| 123 | |||||||||||
| 124 | 2 | ▶ | sub new_from_raw_record ( $class, $line ) { | ||||||||
|
Subroutine
new_from_raw_record: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 125 | |||||||||||
| 126 | 2 | ▶ | my $aoa = csv( in => \$line ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | or croak( Text::CSV->error_diag ); | ||||||||||
| 128 | |||||||||||
| 129 | 2 | return $class->new_from_record( $aoa->[ 0 ]->@* ); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | =head2 new_from_record | ||||||||||
| 133 | |||||||||||
| 134 | Returns a new instance of the class with attributes populated from | ||||||||||
| 135 | the result of parsing the already parsed line: | ||||||||||
| 136 | |||||||||||
| 137 | my $Group = Business::NAB::AccountInformation::Group | ||||||||||
| 138 | ::Payments::DescriptiveRecord->new_from_record( @record ); | ||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||
| 141 | |||||||||||
| 142 | 5 | ▶ | sub new_from_record ( $class, @record ) { | ||||||||
|
Subroutine
new_from_record: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 143 | |||||||||||
| 144 | my ( | ||||||||||
| 145 | 5 | $record_type, | |||||||||
| 146 | $ult_receiver_id, | ||||||||||
| 147 | $org_id, | ||||||||||
| 148 | undef, # group status | ||||||||||
| 149 | $as_of_date, | ||||||||||
| 150 | undef, # as of time | ||||||||||
| 151 | $additional_field, | ||||||||||
| 152 | ) = @record; | ||||||||||
| 153 | |||||||||||
| 154 | 5 | ▶ | if ( $record_type ne '02' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 1 | croak( "unsupported record type ($record_type)" ); | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | 4 | return $class->new( | |||||||||
| 159 | ultimate_receiver_identification => $ult_receiver_id, | ||||||||||
| 160 | originator_identification => $org_id, | ||||||||||
| 161 | as_of_date => $as_of_date, | ||||||||||
| 162 | additional_field => $additional_field, | ||||||||||
| 163 | ); | ||||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | =head2 validate_totals | ||||||||||
| 167 | |||||||||||
| 168 | Checks if the control_total_a and control_total_b values match the | ||||||||||
| 169 | expected totals of the contained accounts: | ||||||||||
| 170 | |||||||||||
| 171 | $Group->validate_totals( my $is_bai2 = 0 ); | ||||||||||
| 172 | |||||||||||
| 173 | Will throw an exception if any total doesn't match the expected value. | ||||||||||
| 174 | |||||||||||
| 175 | Takes an optional boolean param to stipulate if the file type is BAI2 | ||||||||||
| 176 | (defaults to false). | ||||||||||
| 177 | |||||||||||
| 178 | =cut | ||||||||||
| 179 | |||||||||||
| 180 | 4 | ▶ | sub validate_totals ( $self, $is_bai2 = 0 ) { | ||||||||
|
Subroutine
validate_totals: called CC=8 SCAR=21.8
Pod
documented
| |||||||||||
| 181 | |||||||||||
| 182 | 4 | my $num_accounts = scalar( $self->accounts->@* ); | |||||||||
| 183 | 4 | ▶ | croak( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | "number of nested accounts ($num_accounts) != number_of_accounts " | ||||||||||
| 185 | 0 | . "(@{[ $self->number_of_accounts ]})" | |||||||||
| 186 | ) if $num_accounts != $self->number_of_accounts; | ||||||||||
| 187 | |||||||||||
| 188 | 4 | my ( $account_total_a, $account_total_b ) = ( 0, 0 ); | |||||||||
| 189 | |||||||||||
| 190 | 4 | foreach my $Account ( $self->accounts->@* ) { | |||||||||
| 191 | 9 | $account_total_a += $Account->control_total_a; | |||||||||
| 192 | |||||||||||
| 193 | 9 | ▶ | if ( !$is_bai2 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 4 | $account_total_b += $Account->control_total_b; | |||||||||
| 195 | } | ||||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | croak( | ||||||||||
| 199 | 4 | ▶ | "calculated sum ($account_total_a) != control_total_a " | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | 0 | . "(@{[$self->control_total_a]})" | |||||||||
| 201 | ) if $account_total_a != $self->control_total_a; | ||||||||||
| 202 | |||||||||||
| 203 | 4 | ▶ | if ( !$is_bai2 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 204 | 2 | ▶ | croak( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | "calculated sum ($account_total_b) != control_total_b " | ||||||||||
| 206 | 0 | . "(@{[$self->control_total_b]})" | |||||||||
| 207 | ) if $account_total_b != $self->control_total_b; | ||||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | 4 | return 1; | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | =head1 SEE ALSO | ||||||||||
| 214 | |||||||||||
| 215 | L<Business::NAB::Types> | ||||||||||
| 216 | |||||||||||
| 217 | L<Business::NAB::AccountInformation::Account> | ||||||||||
| 218 | |||||||||||
| 219 | L<Business::NAB::AccountInformation::Transaction> | ||||||||||
| 220 | |||||||||||
| 221 | =cut | ||||||||||
| 222 | |||||||||||
| 223 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||