X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;ds=sidebyside;f=C4%2FInput.pm;h=528f6800ea7819280d5f62ddfb542f2629eeebf7;hb=6693f707b3119c4884f98255f791310acc4c76ed;hp=5c1e609fa2a83f07055807ee6f0eee1c28ef04a0;hpb=95e2b3d1e6f2da577673da4e0c6fb86c6e52bb36;p=koha.git diff --git a/C4/Input.pm b/C4/Input.pm index 5c1e609fa2..528f6800ea 100644 --- a/C4/Input.pm +++ b/C4/Input.pm @@ -20,6 +20,7 @@ package C4::Input; #assumes C4/Input use strict; require Exporter; +use C4::Context; use vars qw($VERSION @ISA @EXPORT); @@ -47,27 +48,27 @@ number or ISBN is valid. @ISA = qw(Exporter); @EXPORT = qw( - &checkflds &checkdigit &checkvalidisbn + &checkdigit &checkvalidisbn ); # FIXME - This is never used. -sub checkflds { - my ($env,$reqflds,$data) = @_; - my $numrflds = @$reqflds; - my @probarr; - my $i = 0; - while ($i < $numrflds) { - if ($data->{@$reqflds[$i]} eq "") { - push(@probarr, @$reqflds[$i]); - } - $i++ - } - return (\@probarr); -} +#sub checkflds { +# my ($env,$reqflds,$data) = @_; +# my $numrflds = @$reqflds; +# my @probarr; +# my $i = 0; +# while ($i < $numrflds) { +# if ($data->{@$reqflds[$i]} eq "") { +# push(@probarr, @$reqflds[$i]); +# } +# $i++ +# } +# return (\@probarr); +#} =item checkdigit - $valid = &checkdigit($env, $cardnumber); + $valid = &checkdigit($env, $cardnumber $nounique); Takes a card number, computes its check digit, and compares it to the checkdigit at the end of C<$cardnumber>. Returns a true value iff @@ -78,31 +79,51 @@ C<$env> is ignored. =cut #' sub checkdigit { - my ($env,$infl) = @_; - $infl = uc $infl; - my @weightings = (8,4,6,3,5,2,1); - my $sum; - my $i = 1; - my $valid = 0; - # print $infl."
"; - # FIXME - for ($i = 1; $i < 8; $i++) - # or foreach $i (1..7) - while ($i <8) { - my $temp1 = $weightings[$i-1]; - my $temp2 = substr($infl,$i,1); - $sum += $temp1 * $temp2; -# print "$sum $temp1 $temp2
"; - $i++; - } - my $rem = ($sum%11); - if ($rem == 10) { - $rem = "X"; - } - #print $rem."
"; - if ($rem eq substr($infl,8,1)) { - $valid = 1; - } - return $valid; + + my ($env,$infl, $nounique) = @_; + $infl = uc $infl; + + + #Check to make sure the cardnumber is unique + + #FIXME: We should make the error for a nonunique cardnumber + #different from the one where the checkdigit on the number is + #not correct + + unless ( $nounique ) + { + my $dbh=C4::Context->dbh; + my $query=qq{SELECT * FROM borrowers WHERE cardnumber=?}; + my $sth=$dbh->prepare($query); + $sth->execute($infl); + my %results = $sth->fetchrow_hashref(); + if ( $sth->rows != 0 ) + { + return 0; + } + } + if (C4::Context->preference("checkdigit") eq "none") { + return 1; + } + + my @weightings = (8,4,6,3,5,2,1); + my $sum; + my $i = 1; + my $valid = 0; + + foreach $i (1..7) { + my $temp1 = $weightings[$i-1]; + my $temp2 = substr($infl,$i,1); + $sum += $temp1 * $temp2; + } + my $rem = ($sum%11); + if ($rem == 10) { + $rem = "X"; + } + if ($rem eq substr($infl,8,1)) { + $valid = 1; + } + return $valid; } # sub checkdigit =item checkvalidisbn @@ -127,29 +148,24 @@ sub checkvalidisbn { $q=~s/x$/X/g; # upshift lower case X $q=~s/[^X\d]//g; $q=~s/X.//g; - if (length($q)==10) { - my $checksum=substr($q,9,1); - my $isbn=substr($q,0,9); - my $i; - my $c=0; - for ($i=0; $i<9; $i++) { - my $digit=substr($q,$i,1); - $c+=$digit*(10-$i); - } - $c=$c%11; # % is the modulus function - ($c==10) && ($c='X'); - # FIXME - $isbngood = $c eq $checksum; - if ($c eq $checksum) { - $isbngood=1; - } else { - $isbngood=0; - } - } else { - # FIXME - Put "return 0 if $length($q) != 10" near the - # top, so we don't have to indent the rest of the function - # as much. - $isbngood=0; - } # if length good + + #return 0 if $q is not ten digits long + if (length($q)!=10) { + return 0; + } + + #If we get to here, length($q) must be 10 + my $checksum=substr($q,9,1); + my $isbn=substr($q,0,9); + my $i; + my $c=0; + for ($i=0; $i<9; $i++) { + my $digit=substr($q,$i,1); + $c+=$digit*(10-$i); + } + $c %= 11; + ($c==10) && ($c='X'); + $isbngood = $c eq $checksum; return $isbngood;