Business-NAB

 view release on metacpan or  search on metacpan

lib/Business/NAB/BPAY/Remittance/File/TrailerRecord.pm  view on Meta::CPAN

        $total_payments,
        $amount_payments,
        $total_error,
        $amount_error,
        $total_reversals,
        $amount_reversals,
        $settlement_amount,
    ) = unpack( $class->_pack_template(), $line );

    if ( $record_type ne '99' ) {
        croak( "unsupported record type ($record_type)" );
    }

    return $class->new(
        biller_code                 => $biller_code,
        number_of_payments          => $total_payments,
        amount_of_payments          => $amount_payments,
        number_of_error_corrections => $total_error,
        amount_of_error_corrections => $amount_error,
        number_of_reversals         => $total_reversals,
        amount_of_reversals         => $amount_reversals,
        settlement_amount           => $settlement_amount,
    );
}

=head2 to_record

Returns a string constructed from the object's attributes, representing
the record for use in a batch file:

    my $line = $Trailer->to_record;

=cut

sub to_record ( $self ) {

    my $record = pack(
        $self->_pack_template(),
        "99",
        $self->biller_code,
        sprintf( "%09s",  $self->_brf_int( $self->number_of_payments ) ),
        sprintf( "%015s", $self->_brf_int( $self->amount_of_payments ) ),
        sprintf( "%09s",  $self->_brf_int( $self->number_of_error_corrections ) ),
        sprintf( "%015s", $self->_brf_int( $self->amount_of_error_corrections ) ),
        sprintf( "%09s",  $self->_brf_int( $self->number_of_reversals ) ),
        sprintf( "%015s", $self->_brf_int( $self->amount_of_reversals ) ),
        sprintf( "%015s", $self->_brf_int( $self->settlement_amount ) ),
    );

    return $record;
}

sub _brf_int {
    my ( $self, $str ) = @_;

    # trailer record amounts in BPAY Remittance Files use the last
    # character to represent:
    #   - the last digit
    #   - the sign
    #
    # it's a little odd, but i guess they've historically had to squeeze
    # amounts into the 15 available spaces (which are minor units, so 13,
    # which still feels like a lot, but whatever). this is the *only*
    # NAB file type that does this, so it might actually be a BPAY thing
    #
    # see also: NAB::Type::BRFInt in Business::NAB::Types which will coerce
    # NAB's value to an actual signed integer
    my $last_char = chop( $str );

    if ( $str && $str < 0 ) {
        $str *= -1;
        $last_char =~ tr/0-9$/}J-R/;
    } else {
        $last_char =~ tr/0-9$/{A-I/;
    }

    return $str . $last_char;
}

=head1 SEE ALSO

L<Business::NAB::Types>

=cut

__PACKAGE__->meta->make_immutable;



( run in 2.164 seconds using v1.01-cache-2.11-cpan-54e63673c56 )