road to a better member management code.
[koha.git] / C4 / Members.pm
1 # -*- tab-width: 8 -*-
2
3 package C4::Members;
4
5 # Copyright 2000-2003 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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
12 # version.
13 #
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.
17 #
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
21
22 use strict;
23 require Exporter;
24 use C4::Context;
25 use Date::Manip;
26 use C4::Date;
27
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
29
30 $VERSION = 0.01;
31
32 =head1 NAME
33
34 C4::Members - Perl Module containing convenience functions for member handling
35
36 =head1 SYNOPSIS
37
38
39 =head1 DESCRIPTION
40
41
42 =head1 FUNCTIONS
43
44 =over 2
45
46 =cut
47
48 @ISA = qw(Exporter);
49 @EXPORT = qw();
50
51 @EXPORT = qw(
52         &fixup_cardnumber &findguarantees &modmember &newmember &changepassword
53     );
54
55
56 sub modmember {
57         my (%data) = @_;
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);
76         $sth->execute;
77         $sth->finish;
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);
84         }
85 }
86
87 sub newmember {
88         my (%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);
105         $sth->execute;
106         $sth->finish;
107         $data{borrowernumber} =$dbh->{'mysql_insertid'};
108         return $data{borrowernumber};
109 }
110
111 sub changepassword {
112         my ($uid,$member,$digest) = @_;
113         my $dbh = C4::Context->dbh;
114         #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
115         #Then we need to tell the user and have them create a new one.
116         my $sth=$dbh->prepare("select * from borrowers where userid=? and borrowernumber != ?");
117         $sth->execute($uid,$member);
118         if ( ($uid ne '') && ($sth->fetchrow) ) {
119                 return 0;
120     } else {
121                 #Everything is good so we can update the information.
122                 $sth=$dbh->prepare("update borrowers set userid=?, password=? where borrowernumber=?");
123                 $sth->execute($uid, $digest, $member);
124                 return 1;
125         }
126 }
127
128 sub getmemberfromuserid {
129         my ($userid) = @_;
130         my $dbh = C4::Context->dbh;
131         my $sth = $dbh->prepare("select * from borrowers where userid=?");
132         $sth->execute($userid);
133         return $sth->fetchrow_hashref;
134 }
135 sub updateguarantees {
136         my (%data) = @_;
137         my $dbh = C4::Context->dbh;
138         my ($count,$guarantees)=findguarantees($data{'borrowernumber'});
139         for (my $i=0;$i<$count;$i++){
140                 # FIXME
141                 # It looks like the $i is only being returned to handle walking through
142                 # the array, which is probably better done as a foreach loop.
143                 #
144                 my $guaquery="update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
145                 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
146                 ,streetaddress='$data{'address'}'
147                 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
148                 my $sth3=$dbh->prepare($guaquery);
149                 $sth3->execute;
150                 $sth3->finish;
151         }
152 }
153 ################################################################################
154
155 =item fixup_cardnumber
156
157 Warning: The caller is responsible for locking the members table in write
158 mode, to avoid database corruption.
159
160 =cut
161
162 use vars qw( @weightings );
163 my @weightings = (8,4,6,3,5,2,1);
164
165 sub fixup_cardnumber ($) {
166     my($cardnumber) = @_;
167     my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
168     $autonumber_members = 0 unless defined $autonumber_members;
169     # Find out whether member numbers should be generated
170     # automatically. Should be either "1" or something else.
171     # Defaults to "0", which is interpreted as "no".
172
173     if ($cardnumber !~ /\S/ && $autonumber_members) {
174         my $dbh = C4::Context->dbh;
175         my $sth=$dbh->prepare("select max(substring(borrowers.cardnumber,2,7)) from borrowers");
176         $sth->execute;
177
178         my $data=$sth->fetchrow_hashref;
179         $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
180         $sth->finish;
181
182         # purpose: generate checksum'd member numbers.
183         # We'll assume we just got the max value of digits 2-8 of member #'s
184         # from the database and our job is to increment that by one,
185         # determine the 1st and 9th digits and return the full string.
186
187         if (! $cardnumber) {                    # If DB has no values,
188             $cardnumber = 1000000;              # start at 1000000
189         } else {
190             $cardnumber += 1;
191         }
192
193         my $sum = 0;
194         for (my $i = 0; $i < 8; $i += 1) {
195             # read weightings, left to right, 1 char at a time
196             my $temp1 = $weightings[$i];
197
198             # sequence left to right, 1 char at a time
199             my $temp2 = substr($cardnumber,$i,1);
200
201             # mult each char 1-7 by its corresponding weighting
202             $sum += $temp1 * $temp2;
203         }
204
205         my $rem = ($sum%11);
206         $rem = 'X' if $rem == 10;
207
208         $cardnumber="V$cardnumber$rem";
209     }
210     return $cardnumber;
211 }
212
213 sub findguarantees {
214   my ($bornum)=@_;
215   my $dbh = C4::Context->dbh;
216   my $sth=$dbh->prepare("select cardnumber,borrowernumber from borrowers where
217   guarantor=?");
218   $sth->execute($bornum);
219   my @dat;
220   my $i=0;
221   while (my $data=$sth->fetchrow_hashref){
222     $dat[$i]=$data;
223     $i++;
224   }
225   $sth->finish;
226   return($i,\@dat);
227 }
228
229 # =item NewBorrowerNumber
230
231 #   $num = &NewBorrowerNumber();
232
233 # Allocates a new, unused borrower number, and returns it.
234
235 # =cut
236 # #'
237 # # FIXME - This is identical to C4::Search::NewBorrowerNumber.
238 # # Pick one (preferably this one) and stick with it.
239
240 # # FIXME - Race condition: this function just says what the next unused
241 # # number is, but doesn't allocate it. Hence, two clients adding
242 # # patrons at the same time could get the same new borrower number and
243 # # clobber each other.
244 # # A better approach might be to set borrowernumber autoincrement and 
245
246 # sub NewBorrowerNumber {
247 #   my $dbh = C4::Context->dbh;
248 #   my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
249 #   $sth->execute;
250 #   my $data=$sth->fetchrow_hashref;
251 #   $sth->finish;
252 #   $data->{'max(borrowernumber)'}++;
253 #   return($data->{'max(borrowernumber)'});
254 # }
255
256 1;