| 1 | package Business::DK::PO; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 8 | ▶ | use integer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 8 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 8 | ▶ | use vars qw($VERSION @EXPORT_OK); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 8 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 8 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 8 | my @controlcifers = qw(2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1); | |||||||||||||
| 13 | |||||||||||||||
| 14 | 8 | $VERSION = '0.08'; | |||||||||||||
| 15 | 8 | @EXPORT_OK = | |||||||||||||
| 16 | qw(calculate validate validatePO _argument _content _length _calculate_sum); | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 8 | ▶ | use constant CONTROLCODE_LENGTH => 16; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 8 | ▶ | use constant INVOICE_MINLENGTH => 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 8 | ▶ | use constant INVOICE_MAXLENGTH => 15; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 8 | ▶ | use constant MODULUS_OPERAND => 10; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 8 | ▶ | use constant SUM_THRESHOLD => 9; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | |||||||||||||||
| 24 | 17 | ▶ | sub calculate { | ||||||||||||
|
Subroutine
calculate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 25 | 17 | my ( $number, $maxlength, $minlength ) = @_; | |||||||||||||
| 26 | |||||||||||||||
| 27 | 17 | ▶ | if ( !$minlength ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | 15 | $minlength = INVOICE_MINLENGTH; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 17 | ▶ | if ( !$maxlength ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | 15 | $maxlength = INVOICE_MAXLENGTH; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 17 | ▶ | if ( !$number ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 1 | _argument( $minlength, $maxlength ); | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | 16 | _content($number); | |||||||||||||
| 39 | 16 | _length( $number, $minlength, $maxlength ); | |||||||||||||
| 40 | |||||||||||||||
| 41 | 15 | my $format = '%0' . $maxlength . 's'; | |||||||||||||
| 42 | 15 | $number = sprintf "$format", $number; | |||||||||||||
| 43 | |||||||||||||||
| 44 | 15 | my $sum = _calculate_sum($number); | |||||||||||||
| 45 | |||||||||||||||
| 46 | 15 | my $mod = $sum % MODULUS_OPERAND; | |||||||||||||
| 47 | 15 | my $checkciffer = 0; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 15 | $checkciffer = ( MODULUS_OPERAND - $mod ); | |||||||||||||
| 50 | |||||||||||||||
| 51 | 15 | return ( $number . $checkciffer ); | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | ## no critic (RequireArgUnpacking) | ||||||||||||||
| 55 | 8 | ▶ | sub validatePO { | ||||||||||||
|
Subroutine
validatePO: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 56 | 8 | return validate(@_); | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 30 | ▶ | sub validate { | ||||||||||||
|
Subroutine
validate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 60 | 30 | my $controlnumber = shift; | |||||||||||||
| 61 | |||||||||||||||
| 62 | 30 | ▶ | if ( !$controlnumber ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 5 | _argument(CONTROLCODE_LENGTH); | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | 25 | _content($controlnumber); | |||||||||||||
| 66 | 22 | _length( $controlnumber, CONTROLCODE_LENGTH ); | |||||||||||||
| 67 | |||||||||||||||
| 68 | 18 | my $sum = _calculate_sum($controlnumber); | |||||||||||||
| 69 | |||||||||||||||
| 70 | 18 | ▶ | if ( $sum % MODULUS_OPERAND ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 2 | return 0; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | else { | ||||||||||||||
| 74 | 16 | return 1; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 9 | ▶ | sub _argument { | ||||||||||||
|
Subroutine
_argument: called CC=3 SCAR=11.0
| |||||||||||||||
| 79 | 9 | my ( $length, $maxlen ) = @_; | |||||||||||||
| 80 | |||||||||||||||
| 81 | 9 | ▶ | if ($maxlen) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 82 | 2 | croak | |||||||||||||
| 83 | "function takes an argument of minimum: $length and maximum $maxlen digits"; | ||||||||||||||
| 84 | |||||||||||||||
| 85 | } | ||||||||||||||
| 86 | elsif ($length) { | ||||||||||||||
| 87 | 6 | croak "function takes an argument of $length digits"; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | else { | ||||||||||||||
| 90 | 1 | croak "function takes an argument"; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 41 | ▶ | sub _content { | ||||||||||||
|
Subroutine
_content: called CC=2 SCAR=6.9
| |||||||||||||||
| 95 | 41 | my $number = shift; | |||||||||||||
| 96 | |||||||||||||||
| 97 | 41 | ▶ | if ( $number !~ /^\d*$/ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | 3 | croak "argument: $number must only contain digits"; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | 38 | return 1; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 40 | ▶ | sub _length { | ||||||||||||
|
Subroutine
_length: called CC=5 SCAR=16.1
| |||||||||||||||
| 104 | 40 | my ( $number, $length, $maxlen ) = @_; | |||||||||||||
| 105 | |||||||||||||||
| 106 | 40 | ▶ | if ($maxlen) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | 18 | ▶ | if ( length($number) < $length ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 108 | 1 | croak "argument: $number has to be $length digits long"; | |||||||||||||
| 109 | |||||||||||||||
| 110 | } | ||||||||||||||
| 111 | elsif ( length($number) > $maxlen ) { | ||||||||||||||
| 112 | 2 | croak "argument: $number must be not more than $maxlen digits long"; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | } | ||||||||||||||
| 116 | else { | ||||||||||||||
| 117 | 22 | ▶ | if ( length($number) != $length ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 4 | croak "argument: $number has to be $length digits long"; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | } | ||||||||||||||
| 121 | 33 | return 1; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | |||||||||||||||
| 124 | 34 | ▶ | sub _calculate_sum { | ||||||||||||
|
Subroutine
_calculate_sum: called CC=3 SCAR=11.0
| |||||||||||||||
| 125 | 34 | my $number = shift; | |||||||||||||
| 126 | |||||||||||||||
| 127 | 34 | my $sum = 0; | |||||||||||||
| 128 | 34 | my @numbers = split( //, $number ); | |||||||||||||
| 129 | |||||||||||||||
| 130 | 34 | ▶ | for ( my $i = 0 ; $i < scalar(@numbers) ; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 512 | my $tmpsum2 = 0; | |||||||||||||
| 132 | 512 | my $tmpsum = $numbers[$i] * $controlcifers[$i]; | |||||||||||||
| 133 | |||||||||||||||
| 134 | 512 | ▶ | if ( $tmpsum > SUM_THRESHOLD ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | |||||||||||||||
| 136 | #TODO: address this construct | ||||||||||||||
| 137 | ## no critic (BuiltinFunctions::ProhibitVoidMap) | ||||||||||||||
| 138 | 43 | map( { $tmpsum2 += $_ } split( //, $tmpsum ) ); | |||||||||||||
| 139 | 43 | $tmpsum = $tmpsum2; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | 512 | $sum += $tmpsum; | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 34 | return $sum; | |||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | 8 | 1; | |||||||||||||
| 148 | |||||||||||||||
| 149 | __END__ | ||||||||||||||