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;
69 # Find out whether member numbers should be generated
70 # automatically. Should be either "1" or something else.
71 # Defaults to "0", which is interpreted as "no".
73 if ($cardnumber !~ /\S/ && $autonumber_members) {
74 my $dbh = C4::Context->dbh;
75 my $sth=$dbh->prepare("select max(substring(borrowers.cardnumber,2,7)) from borrowers");
78 my $data=$sth->fetchrow_hashref;
79 $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
82 # purpose: generate checksum'd member numbers.
83 # We'll assume we just got the max value of digits 2-8 of member #'s
84 # from the database and our job is to increment that by one,
85 # determine the 1st and 9th digits and return the full string.
87 if (! $cardnumber) { # If DB has no values,
88 $cardnumber = 1000000; # start at 1000000
94 for (my $i = 0; $i < 8; $i += 1) {
95 # read weightings, left to right, 1 char at a time
96 my $temp1 = $weightings[$i];
98 # sequence left to right, 1 char at a time
99 my $temp2 = substr($cardnumber,$i,1);
101 # mult each char 1-7 by its corresponding weighting
102 $sum += $temp1 * $temp2;
106 $rem = 'X' if $rem == 10;
108 $cardnumber="V$cardnumber$rem";