5 # Copyright 2000-2003 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
32 C4::Members - Perl Module containing convenience functions for member handling
53 ################################################################################
55 =item fixup_cardnumber
57 Warning: The caller is responsible for locking the members table in write
58 mode, to avoid database corruption.
62 use vars qw( @weightings );
63 my @weightings = (8,4,6,3,5,2,1);
65 sub fixup_cardnumber ($) {
67 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
68 $autonumber_members = 0 unless defined $autonumber_members;
70 warn "autoMemberNum is $autonumber_members\n";
72 # Find out whether member numbers should be generated
73 # automatically. Should be either "1" or something else.
74 # Defaults to "0", which is interpreted as "no".
76 if ($cardnumber !~ /\S/ && $autonumber_members) {
77 my $dbh = C4::Context->dbh;
78 my $query="select max(substring(borrowers.cardnumber,2,7)) from borrowers";
79 my $sth=$dbh->prepare($query);
82 my $data=$sth->fetchrow_hashref;
83 $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
86 # purpose: generate checksum'd member numbers.
87 # We'll assume we just got the max value of digits 2-8 of member #'s
88 # from the database and our job is to increment that by one,
89 # determine the 1st and 9th digits and return the full string.
91 if (! $cardnumber) { # If DB has no values,
92 $cardnumber = 1000000; # start at 1000000
98 for (my $i = 0; $i < 8; $i += 1) {
99 # read weightings, left to right, 1 char at a time
100 my $temp1 = $weightings[$i];
102 # sequence left to right, 1 char at a time
103 my $temp2 = substr($cardnumber,$i,1);
105 # mult each char 1-7 by its corresponding weighting
106 $sum += $temp1 * $temp2;
110 $rem = 'X' if $rem == 10;
112 $cardnumber="V$cardnumber$rem";