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
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
34 C4::Members - Perl Module containing convenience functions for member handling
52 &getmember &fixup_cardnumber &findguarantees &modmember &newmember &changepassword
58 $borrower = &getmember($cardnumber, $borrowernumber);
60 Looks up information about a patron (borrower) by either card number
61 or borrower number. If $borrowernumber is specified, C<&borrdata>
62 searches by borrower number; otherwise, it searches by card number.
64 C<&getmember> returns a reference-to-hash whose keys are the fields of
65 the C<borrowers> table in the Koha database.
70 my ($cardnumber,$bornum)=@_;
71 $cardnumber = uc $cardnumber;
72 my $dbh = C4::Context->dbh;
75 $sth=$dbh->prepare("Select * from borrowers where cardnumber=?");
76 $sth->execute($cardnumber);
78 $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
79 $sth->execute($bornum);
81 my $data=$sth->fetchrow_hashref;
85 } else { # try with firstname
87 my $sth=$dbh->prepare("select * from borrowers where firstname=?");
88 $sth->execute($cardnumber);
89 my $data=$sth->fetchrow_hashref;
100 my $dbh = C4::Context->dbh;
101 $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
102 $data{'joining'}=format_date_in_iso($data{'joining'});
103 $data{'expiry'}=format_date_in_iso($data{'expiry'});
104 my $query="update borrowers set title='$data{'title'}',expiry='$data{'expiry'}',
105 cardnumber='$data{'cardnumber'}',sex='$data{'sex'}',ethnotes='$data{'ethnicnotes'}',
106 streetaddress='$data{'streetaddress'}',faxnumber='$data{'faxnumber'}',firstname='$data{'firstname'}',
107 altnotes='$data{'altnotes'}',dateofbirth='$data{'dateofbirth'}',contactname='$data{'contactname'}',
108 emailaddress='$data{'emailaddress'}',dateenrolled='$data{'joining'}',streetcity='$data{'streetcity'}',
109 altrelationship='$data{'altrelationship'}',othernames='$data{'othernames'}',phoneday='$data{'phoneday'}',
110 categorycode='$data{'categorycode'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}',
111 borrowernotes='$data{'borrowernotes'}',altphone='$data{'altphone'}',surname='$data{'surname'}',
112 initials='$data{'initials'}',physstreet='$data{'physstreet'}',ethnicity='$data{'ethnicity'}',
113 gonenoaddress='$data{'gna'}',lost='$data{'lost'}',debarred='$data{'debarred'}',
114 textmessaging='$data{'textmessaging'}', branchcode = '$data{'branchcode'}',
115 zipcode = '$data{'zipcode'}',homezipcode='$data{'homezipcode'}', sort1='$data{'sort1'}', sort2='$data{'sort2'}'
116 where borrowernumber=$data{'borrowernumber'}";
117 my $sth=$dbh->prepare($query);
120 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
121 # so when we update information for an adult we should check for guarantees and update the relevant part
122 # of their records, ie addresses and phone numbers
123 if ($data{'categorycode'} eq 'A' || $data{'categorycode'} eq 'W'){
124 # is adult check guarantees;
125 updateguarantees(%data);
131 my $dbh = C4::Context->dbh;
132 $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
133 $data{'joining'}=format_date_in_iso($data{'joining'});
134 $data{'expiry'}=format_date_in_iso($data{'expiry'});
135 # $data{'borrowernumber'}=NewBorrowerNumber();
136 my $query="insert into borrowers (title,expiry,cardnumber,sex,ethnotes,streetaddress,faxnumber,
137 firstname,altnotes,dateofbirth,contactname,emailaddress,textmessaging,dateenrolled,streetcity,
138 altrelationship,othernames,phoneday,categorycode,city,area,phone,borrowernotes,altphone,surname,
139 initials,ethnicity,physstreet,branchcode,zipcode,homezipcode,sort1,sort2) values ('$data{'title'}','$data{'expiry'}','$data{'cardnumber'}',
140 '$data{'sex'}','$data{'ethnotes'}','$data{'streetaddress'}','$data{'faxnumber'}',
141 '$data{'firstname'}','$data{'altnotes'}','$data{'dateofbirth'}','$data{'contactname'}','$data{'emailaddress'}','$data{'textmessaging'}',
142 '$data{'joining'}','$data{'streetcity'}','$data{'altrelationship'}','$data{'othernames'}',
143 '$data{'phoneday'}','$data{'categorycode'}','$data{'city'}','$data{'area'}','$data{'phone'}',
144 '$data{'borrowernotes'}','$data{'altphone'}','$data{'surname'}','$data{'initials'}',
145 '$data{'ethnicity'}','$data{'physstreet'}','$data{'branchcode'}','$data{'zipcode'}','$data{'homezipcode'}','$data{'sort1'}','$data{'sort2'}')";
146 my $sth=$dbh->prepare($query);
149 $data{borrowernumber} =$dbh->{'mysql_insertid'};
150 return $data{borrowernumber};
154 my ($uid,$member,$digest) = @_;
155 my $dbh = C4::Context->dbh;
156 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
157 #Then we need to tell the user and have them create a new one.
158 my $sth=$dbh->prepare("select * from borrowers where userid=? and borrowernumber != ?");
159 $sth->execute($uid,$member);
160 if ( ($uid ne '') && ($sth->fetchrow) ) {
163 #Everything is good so we can update the information.
164 $sth=$dbh->prepare("update borrowers set userid=?, password=? where borrowernumber=?");
165 $sth->execute($uid, $digest, $member);
170 sub getmemberfromuserid {
172 my $dbh = C4::Context->dbh;
173 my $sth = $dbh->prepare("select * from borrowers where userid=?");
174 $sth->execute($userid);
175 return $sth->fetchrow_hashref;
177 sub updateguarantees {
179 my $dbh = C4::Context->dbh;
180 my ($count,$guarantees)=findguarantees($data{'borrowernumber'});
181 for (my $i=0;$i<$count;$i++){
183 # It looks like the $i is only being returned to handle walking through
184 # the array, which is probably better done as a foreach loop.
186 my $guaquery="update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
187 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
188 ,streetaddress='$data{'address'}'
189 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
190 my $sth3=$dbh->prepare($guaquery);
195 ################################################################################
197 =item fixup_cardnumber
199 Warning: The caller is responsible for locking the members table in write
200 mode, to avoid database corruption.
204 use vars qw( @weightings );
205 my @weightings = (8,4,6,3,5,2,1);
207 sub fixup_cardnumber ($) {
208 my($cardnumber) = @_;
209 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
210 $autonumber_members = 0 unless defined $autonumber_members;
211 # Find out whether member numbers should be generated
212 # automatically. Should be either "1" or something else.
213 # Defaults to "0", which is interpreted as "no".
215 if ($cardnumber !~ /\S/ && $autonumber_members) {
216 my $dbh = C4::Context->dbh;
217 my $sth=$dbh->prepare("select max(substring(borrowers.cardnumber,2,7)) from borrowers");
220 my $data=$sth->fetchrow_hashref;
221 $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
224 # purpose: generate checksum'd member numbers.
225 # We'll assume we just got the max value of digits 2-8 of member #'s
226 # from the database and our job is to increment that by one,
227 # determine the 1st and 9th digits and return the full string.
229 if (! $cardnumber) { # If DB has no values,
230 $cardnumber = 1000000; # start at 1000000
236 for (my $i = 0; $i < 8; $i += 1) {
237 # read weightings, left to right, 1 char at a time
238 my $temp1 = $weightings[$i];
240 # sequence left to right, 1 char at a time
241 my $temp2 = substr($cardnumber,$i,1);
243 # mult each char 1-7 by its corresponding weighting
244 $sum += $temp1 * $temp2;
248 $rem = 'X' if $rem == 10;
250 $cardnumber="V$cardnumber$rem";
257 my $dbh = C4::Context->dbh;
258 my $sth=$dbh->prepare("select cardnumber,borrowernumber from borrowers where
260 $sth->execute($bornum);
263 while (my $data=$sth->fetchrow_hashref){
271 # =item NewBorrowerNumber
273 # $num = &NewBorrowerNumber();
275 # Allocates a new, unused borrower number, and returns it.
279 # # FIXME - This is identical to C4::Search::NewBorrowerNumber.
280 # # Pick one (preferably this one) and stick with it.
282 # # FIXME - Race condition: this function just says what the next unused
283 # # number is, but doesn't allocate it. Hence, two clients adding
284 # # patrons at the same time could get the same new borrower number and
285 # # clobber each other.
286 # # A better approach might be to set borrowernumber autoincrement and
288 # sub NewBorrowerNumber {
289 # my $dbh = C4::Context->dbh;
290 # my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
292 # my $data=$sth->fetchrow_hashref;
294 # $data->{'max(borrowernumber)'}++;
295 # return($data->{'max(borrowernumber)'});