-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."<br>";
- while ($i <8) {
- my $temp1 = $weightings[$i-1];
- my $temp2 = substr($infl,$i,1);
- $sum = $sum + ($temp1*$temp2);
-# print "$sum $temp1 $temp2<br>";
- $i++;
- }
- my $rem = ($sum%11);
- if ($rem == 10) {
- $rem = "X";
- }
- #print $rem."<br>";
- if ($rem eq substr($infl,8,1)) {
- $valid = 1;
- }
- return $valid;
+=item checkdigit
+
+ $valid = &checkdigit($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
+C<$cardnumber> has a valid check digit.
+
+=cut
+
+#'
+sub checkdigit ($;$) {
+
+ my ($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 $query=qq{SELECT * FROM borrowers WHERE cardnumber=?};
+ my $sth=C4::Context->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;
+ foreach my $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)) {
+ return 1;
+ }
+ return 0;