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{'expiry'}=format_date_in_iso($data{'expiry'});
103 my $query="update borrowers set title='$data{'title'}',expiry='$data{'expiry'}',
104 cardnumber='$data{'cardnumber'}',sex='$data{'sex'}',ethnotes='$data{'ethnicnotes'}',
105 streetaddress='$data{'streetaddress'}',faxnumber='$data{'faxnumber'}',firstname='$data{'firstname'}',
106 altnotes='$data{'altnotes'}',dateofbirth='$data{'dateofbirth'}',contactname='$data{'contactname'}',
107 emailaddress='$data{'emailaddress'}',streetcity='$data{'streetcity'}',
108 altrelationship='$data{'altrelationship'}',othernames='$data{'othernames'}',phoneday='$data{'phoneday'}',
109 categorycode='$data{'categorycode'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}',
110 borrowernotes='$data{'borrowernotes'}',altphone='$data{'altphone'}',surname='$data{'surname'}',
111 initials='$data{'initials'}',physstreet='$data{'physstreet'}',ethnicity='$data{'ethnicity'}',
112 gonenoaddress='$data{'gna'}',lost='$data{'lost'}',debarred='$data{'debarred'}',
113 textmessaging='$data{'textmessaging'}', branchcode = '$data{'branchcode'}',
114 zipcode = '$data{'zipcode'}',homezipcode='$data{'homezipcode'}', sort1='$data{'sort1'}', sort2='$data{'sort2'}'
115 where borrowernumber=$data{'borrowernumber'}";
116 my $sth=$dbh->prepare($query);
119 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
120 # so when we update information for an adult we should check for guarantees and update the relevant part
121 # of their records, ie addresses and phone numbers
122 if ($data{'categorycode'} eq 'A' || $data{'categorycode'} eq 'W'){
123 # is adult check guarantees;
124 updateguarantees(%data);
130 my $dbh = C4::Context->dbh;
131 $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
132 $data{'joining'} = &ParseDate("today") unless $data{'joining'};
133 $data{'joining'}=format_date_in_iso($data{'joining'});
134 # if expirydate is not set, calculate it from borrower category subscription duration
135 unless ($data{'expiry'}) {
136 my $sth = $dbh->prepare("select enrolmentperiod from categories where categorycode=?");
137 $sth->execute($data{'categorycode'});
138 my ($enrolmentperiod) = $sth->fetchrow;
139 $enrolmentperiod = 12 unless ($enrolmentperiod);
140 $data{'expiry'} = &DateCalc($data{'joining'},"$enrolmentperiod years");
142 $data{'expiry'}=format_date_in_iso($data{'expiry'});
143 # $data{'borrowernumber'}=NewBorrowerNumber();
144 my $query="insert into borrowers (title,expiry,cardnumber,sex,ethnotes,streetaddress,faxnumber,
145 firstname,altnotes,dateofbirth,contactname,emailaddress,textmessaging,dateenrolled,streetcity,
146 altrelationship,othernames,phoneday,categorycode,city,area,phone,borrowernotes,altphone,surname,
147 initials,ethnicity,physstreet,branchcode,zipcode,homezipcode,sort1,sort2) values ('$data{'title'}','$data{'expiry'}','$data{'cardnumber'}',
148 '$data{'sex'}','$data{'ethnotes'}','$data{'streetaddress'}','$data{'faxnumber'}',
149 '$data{'firstname'}','$data{'altnotes'}','$data{'dateofbirth'}','$data{'contactname'}','$data{'emailaddress'}','$data{'textmessaging'}',
150 '$data{'joining'}','$data{'streetcity'}','$data{'altrelationship'}','$data{'othernames'}',
151 '$data{'phoneday'}','$data{'categorycode'}','$data{'city'}','$data{'area'}','$data{'phone'}',
152 '$data{'borrowernotes'}','$data{'altphone'}','$data{'surname'}','$data{'initials'}',
153 '$data{'ethnicity'}','$data{'physstreet'}','$data{'branchcode'}','$data{'zipcode'}','$data{'homezipcode'}','$data{'sort1'}','$data{'sort2'}')";
154 my $sth=$dbh->prepare($query);
157 $data{borrowernumber} =$dbh->{'mysql_insertid'};
158 return $data{borrowernumber};
162 my ($uid,$member,$digest) = @_;
163 my $dbh = C4::Context->dbh;
164 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
165 #Then we need to tell the user and have them create a new one.
166 my $sth=$dbh->prepare("select * from borrowers where userid=? and borrowernumber != ?");
167 $sth->execute($uid,$member);
168 if ( ($uid ne '') && ($sth->fetchrow) ) {
171 #Everything is good so we can update the information.
172 $sth=$dbh->prepare("update borrowers set userid=?, password=? where borrowernumber=?");
173 $sth->execute($uid, $digest, $member);
178 sub getmemberfromuserid {
180 my $dbh = C4::Context->dbh;
181 my $sth = $dbh->prepare("select * from borrowers where userid=?");
182 $sth->execute($userid);
183 return $sth->fetchrow_hashref;
185 sub updateguarantees {
187 my $dbh = C4::Context->dbh;
188 my ($count,$guarantees)=findguarantees($data{'borrowernumber'});
189 for (my $i=0;$i<$count;$i++){
191 # It looks like the $i is only being returned to handle walking through
192 # the array, which is probably better done as a foreach loop.
194 my $guaquery="update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
195 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
196 ,streetaddress='$data{'address'}'
197 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
198 my $sth3=$dbh->prepare($guaquery);
203 ################################################################################
205 =item fixup_cardnumber
207 Warning: The caller is responsible for locking the members table in write
208 mode, to avoid database corruption.
212 use vars qw( @weightings );
213 my @weightings = (8,4,6,3,5,2,1);
215 sub fixup_cardnumber ($) {
216 my($cardnumber) = @_;
217 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
218 $autonumber_members = 0 unless defined $autonumber_members;
219 # Find out whether member numbers should be generated
220 # automatically. Should be either "1" or something else.
221 # Defaults to "0", which is interpreted as "no".
223 if ($cardnumber !~ /\S/ && $autonumber_members) {
224 my $dbh = C4::Context->dbh;
225 my $sth=$dbh->prepare("select max(substring(borrowers.cardnumber,2,7)) from borrowers");
228 my $data=$sth->fetchrow_hashref;
229 $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
232 # purpose: generate checksum'd member numbers.
233 # We'll assume we just got the max value of digits 2-8 of member #'s
234 # from the database and our job is to increment that by one,
235 # determine the 1st and 9th digits and return the full string.
237 if (! $cardnumber) { # If DB has no values,
238 $cardnumber = 1000000; # start at 1000000
244 for (my $i = 0; $i < 8; $i += 1) {
245 # read weightings, left to right, 1 char at a time
246 my $temp1 = $weightings[$i];
248 # sequence left to right, 1 char at a time
249 my $temp2 = substr($cardnumber,$i,1);
251 # mult each char 1-7 by its corresponding weighting
252 $sum += $temp1 * $temp2;
256 $rem = 'X' if $rem == 10;
258 $cardnumber="V$cardnumber$rem";
265 my $dbh = C4::Context->dbh;
266 my $sth=$dbh->prepare("select cardnumber,borrowernumber from borrowers where
268 $sth->execute($bornum);
271 while (my $data=$sth->fetchrow_hashref){
279 # =item NewBorrowerNumber
281 # $num = &NewBorrowerNumber();
283 # Allocates a new, unused borrower number, and returns it.
287 # # FIXME - This is identical to C4::Search::NewBorrowerNumber.
288 # # Pick one (preferably this one) and stick with it.
290 # # FIXME - Race condition: this function just says what the next unused
291 # # number is, but doesn't allocate it. Hence, two clients adding
292 # # patrons at the same time could get the same new borrower number and
293 # # clobber each other.
294 # # A better approach might be to set borrowernumber autoincrement and
296 # sub NewBorrowerNumber {
297 # my $dbh = C4::Context->dbh;
298 # my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
300 # my $data=$sth->fetchrow_hashref;
302 # $data->{'max(borrowernumber)'}++;
303 # return($data->{'max(borrowernumber)'});