| 1 | package Business::AU::TFN; | ||
| 2 | use vars qw/$VERSION/; | ||
| 3 | $VERSION = "0.1"; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Business::AU::TFN - Validate TFN - Australian Tax File Name | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Business::AU::TFN; | ||
| 12 | |||
| 13 | print Business::AU::TFN::validate("123 456 782"); | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | =head1 NOTES | ||
| 18 | |||
| 19 | Details come from http://bioinf.wehi.edu.au/folders/fred/tfn.html | ||
| 20 | |||
| 21 | =head1 METHODS | ||
| 22 | |||
| 23 | Currently these are package methods which must be called explicitly. | ||
| 24 | Although I am considering making this better. | ||
| 25 | |||
| 26 | =head1 METHODS | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use constant WEIGHT => qw/1 4 3 7 5 8 6 9 10/; | ||
| 31 | |||
| 32 | =head2 validate($tfn) | ||
| 33 | |||
| 34 | Validate a tax file number. Return value is one of | ||
| 35 | |||
| 36 | 'valid' - yep, completely valid | ||
| 37 | |||
| 38 | 'invalid length' - Must be 9 characteres (spaces accepted) | ||
| 39 | |||
| 40 | 'invalid sum' - does not match | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub validate { | ||
| 45 | my ($tfn) = @_; | ||
| 46 | my @tfn = _split($tfn); | ||
| 47 | |||
| 48 | # check length is 9 | ||
| 49 | unless (@tfn == 9) { | ||
| 50 | return "invalid length (must be 9)"; | ||
| 51 | } | ||
| 52 | |||
| 53 | # add accumulation | ||
| 54 | my $acc = 0; | ||
| 55 | for (my $i = 0; $i < 9; $i++) { | ||
| 56 | $acc += $tfn[$i] * (WEIGHT)[$i] | ||
| 57 | } | ||
| 58 | |||
| 59 | # check it is valid | ||
| 60 | if (($acc % 11) == 0) { | ||
| 61 | return "valid"; | ||
| 62 | } | ||
| 63 | |||
| 64 | return "invalid sum"; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _split { | ||
| 68 | my ($tfn) = @_; | ||
| 69 | $tfn =~ s/\s//g; | ||
| 70 | return split(//, $tfn); | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 pretty($tfn) | ||
| 74 | |||
| 75 | This prints out a valid pretty print of an TFN The standards says that it must | ||
| 76 | be showed in groups of three (nnn nnn nnn). | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub pretty { | ||
| 81 | my ($tfn) = @_; | ||
| 82 | my @tfn = _split($tfn); | ||
| 83 | if (validate($tfn) eq "valid") { | ||
| 84 | return join('', @tfn[0..2], ' ', @tfn[3..5], ' ', @tfn[6..8]); | ||
| 85 | } else { | ||
| 86 | return "invalid"; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | =head1 AUTHOR | ||
| 91 | |||
| 92 | Scott Penrose <scottp@dd.com.au> | ||
| 93 | |||
| 94 | =head1 SEE ALSO | ||
| 95 | |||
| 96 | L<Business::AU::ACN>, L<Business::AU::ABN> | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | 1; |