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 &fixup_cardnumber &findguarantees &modmember &newmember
58 my $dbh = C4::Context->dbh;
59 $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
60 $data{'joining'}=format_date_in_iso($data{'joining'});
61 $data{'expiry'}=format_date_in_iso($data{'expiry'});
62 my $query="update borrowers set title='$data{'title'}',expiry='$data{'expiry'}',
63 cardnumber='$data{'cardnumber'}',sex='$data{'sex'}',ethnotes='$data{'ethnicnotes'}',
64 streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',firstname='$data{'firstname'}',
65 altnotes='$data{'altnotes'}',dateofbirth='$data{'dateofbirth'}',contactname='$data{'contactname'}',
66 emailaddress='$data{'emailaddress'}',dateenrolled='$data{'joining'}',streetcity='$data{'streetcity'}',
67 altrelationship='$data{'altrelationship'}',othernames='$data{'othernames'}',phoneday='$data{'phoneday'}',
68 categorycode='$data{'categorycode'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}',
69 borrowernotes='$data{'borrowernotes'}',altphone='$data{'altphone'}',surname='$data{'surname'}',
70 initials='$data{'initials'}',physstreet='$data{'streetaddress'}',ethnicity='$data{'ethnicity'}',
71 gonenoaddress='$data{'gna'}',lost='$data{'lost'}',debarred='$data{'debarred'}',
72 textmessaging='$data{'textmessaging'}', branchcode = '$data{'branchcode'}',
73 zipcode = '$data{'zipcode'}',homezipcode='$data{'homezipcode'}', sort1='$data{'sort1'}', sort2='$data{'sort2'}'
74 where borrowernumber=$data{'borrowernumber'}";
75 my $sth=$dbh->prepare($query);
78 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
79 # so when we update information for an adult we should check for guarantees and update the relevant part
80 # of their records, ie addresses and phone numbers
81 if ($data{'categorycode'} eq 'A' || $data{'categorycode'} eq 'W'){
82 # is adult check guarantees;
83 updateguarantees(%data);
89 my $dbh = C4::Context->dbh;
90 $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
91 $data{'joining'}=format_date_in_iso($data{'joining'});
92 $data{'expiry'}=format_date_in_iso($data{'expiry'});
93 # $data{'borrowernumber'}=NewBorrowerNumber();
94 my $query="insert into borrowers (title,expiry,cardnumber,sex,ethnotes,streetaddress,faxnumber,
95 firstname,altnotes,dateofbirth,contactname,emailaddress,textmessaging,dateenrolled,streetcity,
96 altrelationship,othernames,phoneday,categorycode,city,area,phone,borrowernotes,altphone,surname,
97 initials,ethnicity,physstreet,branchcode,zipcode,homezipcode,sort1,sort2) values ('$data{'title'}','$data{'expiry'}','$data{'cardnumber'}',
98 '$data{'sex'}','$data{'ethnotes'}','$data{'address'}','$data{'faxnumber'}',
99 '$data{'firstname'}','$data{'altnotes'}','$data{'dateofbirth'}','$data{'contactname'}','$data{'emailaddress'}','$data{'textmessaging'}',
100 '$data{'joining'}','$data{'streetcity'}','$data{'altrelationship'}','$data{'othernames'}',
101 '$data{'phoneday'}','$data{'categorycode'}','$data{'city'}','$data{'area'}','$data{'phone'}',
102 '$data{'borrowernotes'}','$data{'altphone'}','$data{'surname'}','$data{'initials'}',
103 '$data{'ethnicity'}','$data{'streetaddress'}','$data{'branchcode'}','$data{'zipcode'}','$data{'homezipcode'}','$data{'sort1'}','$data{'sort2'}')";
104 my $sth=$dbh->prepare($query);
107 $data{borrowernumber} =$dbh->{'mysql_insertid'};
108 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
109 # so when we update information for an adult we should check for guarantees and update the relevant part
110 # of their records, ie addresses and phone numbers
111 if ($data{'categorycode'} eq 'A' || $data{'categorycode'} eq 'W'){
112 # is adult check guarantees;
113 updateguarantees(%data);
115 return $data{borrowernumber};
118 sub updateguarantees {
120 my $dbh = C4::Context->dbh;
121 my ($count,$guarantees)=findguarantees($data{'borrowernumber'});
122 for (my $i=0;$i<$count;$i++){
124 # It looks like the $i is only being returned to handle walking through
125 # the array, which is probably better done as a foreach loop.
127 my $guaquery="update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
128 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
129 ,streetaddress='$data{'address'}'
130 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
131 my $sth3=$dbh->prepare($guaquery);
136 ################################################################################
138 =item fixup_cardnumber
140 Warning: The caller is responsible for locking the members table in write
141 mode, to avoid database corruption.
145 use vars qw( @weightings );
146 my @weightings = (8,4,6,3,5,2,1);
148 sub fixup_cardnumber ($) {
149 my($cardnumber) = @_;
150 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
151 $autonumber_members = 0 unless defined $autonumber_members;
152 # Find out whether member numbers should be generated
153 # automatically. Should be either "1" or something else.
154 # Defaults to "0", which is interpreted as "no".
156 if ($cardnumber !~ /\S/ && $autonumber_members) {
157 my $dbh = C4::Context->dbh;
158 my $sth=$dbh->prepare("select max(substring(borrowers.cardnumber,2,7)) from borrowers");
161 my $data=$sth->fetchrow_hashref;
162 $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
165 # purpose: generate checksum'd member numbers.
166 # We'll assume we just got the max value of digits 2-8 of member #'s
167 # from the database and our job is to increment that by one,
168 # determine the 1st and 9th digits and return the full string.
170 if (! $cardnumber) { # If DB has no values,
171 $cardnumber = 1000000; # start at 1000000
177 for (my $i = 0; $i < 8; $i += 1) {
178 # read weightings, left to right, 1 char at a time
179 my $temp1 = $weightings[$i];
181 # sequence left to right, 1 char at a time
182 my $temp2 = substr($cardnumber,$i,1);
184 # mult each char 1-7 by its corresponding weighting
185 $sum += $temp1 * $temp2;
189 $rem = 'X' if $rem == 10;
191 $cardnumber="V$cardnumber$rem";
198 my $dbh = C4::Context->dbh;
199 my $sth=$dbh->prepare("select cardnumber,borrowernumber from borrowers where
201 $sth->execute($bornum);
204 while (my $data=$sth->fetchrow_hashref){
212 # =item NewBorrowerNumber
214 # $num = &NewBorrowerNumber();
216 # Allocates a new, unused borrower number, and returns it.
220 # # FIXME - This is identical to C4::Search::NewBorrowerNumber.
221 # # Pick one (preferably this one) and stick with it.
223 # # FIXME - Race condition: this function just says what the next unused
224 # # number is, but doesn't allocate it. Hence, two clients adding
225 # # patrons at the same time could get the same new borrower number and
226 # # clobber each other.
227 # # A better approach might be to set borrowernumber autoincrement and
229 # sub NewBorrowerNumber {
230 # my $dbh = C4::Context->dbh;
231 # my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
233 # my $data=$sth->fetchrow_hashref;
235 # $data->{'max(borrowernumber)'}++;
236 # return($data->{'max(borrowernumber)'});