HTL mod for till reconciliation.
[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         &getmember &fixup_cardnumber &findguarantees &modmember &newmember &changepassword
53     );
54
55         
56 =item getmember
57
58   $borrower = &getmember($cardnumber, $borrowernumber);
59
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.
63
64 C<&getmember> returns a reference-to-hash whose keys are the fields of
65 the C<borrowers> table in the Koha database.
66
67 =cut
68 #'
69 sub getmember {
70   my ($cardnumber,$bornum)=@_;
71   $cardnumber = uc $cardnumber;
72   my $dbh = C4::Context->dbh;
73   my $sth;
74   if ($bornum eq ''){
75     $sth=$dbh->prepare("Select * from borrowers where cardnumber=?");
76     $sth->execute($cardnumber);
77   } else {
78     $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
79   $sth->execute($bornum);
80   }
81   my $data=$sth->fetchrow_hashref;
82   $sth->finish;
83   if ($data) {
84         return($data);
85         } else { # try with firstname
86                 if ($cardnumber) {
87                         my $sth=$dbh->prepare("select * from borrowers where firstname=?");
88                         $sth->execute($cardnumber);
89                         my $data=$sth->fetchrow_hashref;
90                         $sth->finish;
91                         return($data);
92                 }
93         }
94         return undef;
95 }
96
97
98 sub modmember {
99         my (%data) = @_;
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);
117         $sth->execute;
118         $sth->finish;
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);
125         }
126 }
127
128 sub newmember {
129         my (%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");
141         }
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);
155         $sth->execute;
156         $sth->finish;
157         $data{borrowernumber} =$dbh->{'mysql_insertid'};
158         return $data{borrowernumber};
159 }
160
161 sub changepassword {
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) ) {
169                 return 0;
170     } else {
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);
174                 return 1;
175         }
176 }
177
178 sub getmemberfromuserid {
179         my ($userid) = @_;
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;
184 }
185 sub updateguarantees {
186         my (%data) = @_;
187         my $dbh = C4::Context->dbh;
188         my ($count,$guarantees)=findguarantees($data{'borrowernumber'});
189         for (my $i=0;$i<$count;$i++){
190                 # FIXME
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.
193                 #
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);
199                 $sth3->execute;
200                 $sth3->finish;
201         }
202 }
203 ################################################################################
204
205 =item fixup_cardnumber
206
207 Warning: The caller is responsible for locking the members table in write
208 mode, to avoid database corruption.
209
210 =cut
211
212 use vars qw( @weightings );
213 my @weightings = (8,4,6,3,5,2,1);
214
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".
222
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");
226         $sth->execute;
227
228         my $data=$sth->fetchrow_hashref;
229         $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
230         $sth->finish;
231
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.
236
237         if (! $cardnumber) {                    # If DB has no values,
238             $cardnumber = 1000000;              # start at 1000000
239         } else {
240             $cardnumber += 1;
241         }
242
243         my $sum = 0;
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];
247
248             # sequence left to right, 1 char at a time
249             my $temp2 = substr($cardnumber,$i,1);
250
251             # mult each char 1-7 by its corresponding weighting
252             $sum += $temp1 * $temp2;
253         }
254
255         my $rem = ($sum%11);
256         $rem = 'X' if $rem == 10;
257
258         $cardnumber="V$cardnumber$rem";
259     }
260     return $cardnumber;
261 }
262
263 sub findguarantees {
264   my ($bornum)=@_;
265   my $dbh = C4::Context->dbh;
266   my $sth=$dbh->prepare("select cardnumber,borrowernumber from borrowers where
267   guarantor=?");
268   $sth->execute($bornum);
269   my @dat;
270   my $i=0;
271   while (my $data=$sth->fetchrow_hashref){
272     $dat[$i]=$data;
273     $i++;
274   }
275   $sth->finish;
276   return($i,\@dat);
277 }
278
279 # =item NewBorrowerNumber
280
281 #   $num = &NewBorrowerNumber();
282
283 # Allocates a new, unused borrower number, and returns it.
284
285 # =cut
286 # #'
287 # # FIXME - This is identical to C4::Search::NewBorrowerNumber.
288 # # Pick one (preferably this one) and stick with it.
289
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 
295
296 # sub NewBorrowerNumber {
297 #   my $dbh = C4::Context->dbh;
298 #   my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
299 #   $sth->execute;
300 #   my $data=$sth->fetchrow_hashref;
301 #   $sth->finish;
302 #   $data->{'max(borrowernumber)'}++;
303 #   return($data->{'max(borrowernumber)'});
304 # }
305
306 1;