2 functions added : GetBorrowersFromSurname & GetBranchCodeFromBorrowers
[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 # $Id$
23
24 use strict;
25 require Exporter;
26 use C4::Context;
27 use Date::Manip;
28 use C4::Date;
29 use Digest::MD5 qw(md5_base64);
30 use Date::Calc qw/Today/;
31
32 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
33
34 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
35
36 =head1 NAME
37
38 C4::Members - Perl Module containing convenience functions for member handling
39
40 =head1 SYNOPSIS
41
42 use C4::Members;
43
44 =head1 DESCRIPTION
45
46 This module contains routines for adding, modifying and deleting members/patrons/borrowers 
47
48 =head1 FUNCTIONS
49
50 =over 2
51
52 =cut
53
54 #'
55
56 @ISA    = qw(Exporter);
57
58 @EXPORT = qw(
59  &BornameSearch &getmember &borrdata &borrdata2 &fixup_cardnumber &findguarantees &findguarantor &GuarantornameSearch &NewBorrowerNumber   &modmember &newmember &changepassword &borrissues &allissues
60   &checkuniquemember &getzipnamecity &getidcity &getguarantordata &getcategorytype
61   &calcexpirydate &checkuserpassword
62   &getboracctrecord
63   &borrowercategories &getborrowercategory
64   &fixEthnicity
65   &ethnicitycategories get_institutions add_member_orgs
66   &get_age &GetBorrowersFromSurname &GetBranchCodeFromBorrowers
67 );
68
69
70 =item BornameSearch
71
72   ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
73
74 Looks up patrons (borrowers) by name.
75
76 C<$env> is ignored.
77
78 BUGFIX 499: C<$type> is now used to determine type of search.
79 if $type is "simple", search is performed on the first letter of the
80 surname only.
81
82 C<$searchstring> is a space-separated list of search terms. Each term
83 must match the beginning a borrower's surname, first name, or other
84 name.
85
86 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
87 reference-to-array; each element is a reference-to-hash, whose keys
88 are the fields of the C<borrowers> table in the Koha database.
89 C<$count> is the number of elements in C<$borrowers>.
90
91 =cut
92
93 #'
94 #used by member enquiries from the intranet
95 #called by member.pl
96 sub BornameSearch {
97     my ( $env, $searchstring, $orderby, $type ) = @_;
98     my $dbh   = C4::Context->dbh;
99     my $query = "";
100     my $count;
101     my @data;
102     my @bind = ();
103
104     if ( $type eq "simple" )    # simple search for one letter only
105     {
106         $query =
107           "Select * from borrowers where surname like ? order by $orderby";
108         @bind = ("$searchstring%");
109     }
110     else    # advanced search looking in surname, firstname and othernames
111     {
112         @data  = split( ' ', $searchstring );
113         $count = @data;
114         $query = "Select * from borrowers
115                 where ((surname like ? or surname like ?
116                 or firstname  like ? or firstname like ?
117                 or othernames like ? or othernames like ?)
118                 ";
119         @bind = (
120             "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
121             "$data[0]%", "% $data[0]%"
122         );
123         for ( my $i = 1 ; $i < $count ; $i++ ) {
124             $query = $query . " and (" . " surname like ? or surname like ?
125                         or firstname  like ? or firstname like ?
126                         or othernames like ? or othernames like ?)";
127             push( @bind,
128                 "$data[$i]%",   "% $data[$i]%", "$data[$i]%",
129                 "% $data[$i]%", "$data[$i]%",   "% $data[$i]%" );
130
131             # FIXME - .= <<EOT;
132         }
133         $query = $query . ") or cardnumber like ?
134                 order by $orderby";
135         push( @bind, $searchstring );
136
137         # FIXME - .= <<EOT;
138     }
139
140     my $sth = $dbh->prepare($query);
141
142     #   warn "Q $orderby : $query";
143     $sth->execute(@bind);
144     my @results;
145     my $cnt = $sth->rows;
146     while ( my $data = $sth->fetchrow_hashref ) {
147         push( @results, $data );
148     }
149
150     #  $sth->execute;
151     $sth->finish;
152     return ( $cnt, \@results );
153 }
154
155 =item getmember
156
157   $borrower = &getmember($cardnumber, $borrowernumber);
158
159 Looks up information about a patron (borrower) by either card number
160 or borrower number. If $borrowernumber is specified, C<&borrdata>
161 searches by borrower number; otherwise, it searches by card number.
162
163 C<&getmember> returns a reference-to-hash whose keys are the fields of
164 the C<borrowers> table in the Koha database.
165
166 =cut
167
168 #'
169 sub getmember {
170     my ( $cardnumber, $bornum ) = @_;
171     $cardnumber = uc $cardnumber;
172     my $dbh = C4::Context->dbh;
173     my $sth;
174     if ( $bornum eq '' ) {
175         $sth = $dbh->prepare("Select * from borrowers where cardnumber=?");
176         $sth->execute($cardnumber);
177     }
178     else {
179         $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
180         $sth->execute($bornum);
181     }
182     my $data = $sth->fetchrow_hashref;
183     $sth->finish;
184     if ($data) {
185         return ($data);
186     }
187     else {    # try with firstname
188         if ($cardnumber) {
189             my $sth =
190               $dbh->prepare("select * from borrowers where firstname=?");
191             $sth->execute($cardnumber);
192             my $data = $sth->fetchrow_hashref;
193             $sth->finish;
194             return ($data);
195         }
196     }
197     return undef;
198 }
199
200 =item borrdata
201
202   $borrower = &borrdata($cardnumber, $borrowernumber);
203
204 Looks up information about a patron (borrower) by either card number
205 or borrower number. If $borrowernumber is specified, C<&borrdata>
206 searches by borrower number; otherwise, it searches by card number.
207
208 C<&borrdata> returns a reference-to-hash whose keys are the fields of
209 the C<borrowers> table in the Koha database.
210
211 =cut
212
213 #'
214 sub borrdata {
215     my ( $cardnumber, $bornum ) = @_;
216     $cardnumber = uc $cardnumber;
217     my $dbh = C4::Context->dbh;
218     my $sth;
219     if ( $bornum eq '' ) {
220         $sth =
221           $dbh->prepare(
222 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?"
223           );
224         $sth->execute($cardnumber);
225     }
226     else {
227         $sth =
228           $dbh->prepare(
229 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
230           );
231         $sth->execute($bornum);
232     }
233     my $data = $sth->fetchrow_hashref;
234 #     warn "DATA" . $data->{category_type};
235     $sth->finish;
236     if ($data) {
237         return ($data);
238     }
239     else {    # try with firstname
240         if ($cardnumber) {
241             my $sth =
242               $dbh->prepare(
243 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode  where firstname=?"
244               );
245             $sth->execute($cardnumber);
246             my $data = $sth->fetchrow_hashref;
247             $sth->finish;
248             return ($data);
249         }
250     }
251     return undef;
252 }
253
254 =item borrdata2
255
256   ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
257
258 Returns aggregate data about items borrowed by the patron with the
259 given borrowernumber.
260
261 C<$env> is ignored.
262
263 C<&borrdata2> returns a three-element array. C<$borrowed> is the
264 number of books the patron currently has borrowed. C<$due> is the
265 number of overdue items the patron currently has borrowed. C<$fine> is
266 the total fine currently due by the borrower.
267
268 =cut
269
270 #'
271 sub borrdata2 {
272     my ( $env, $bornum ) = @_;
273     my $dbh   = C4::Context->dbh;
274     my $query = "Select count(*) from issues where borrowernumber='$bornum' and
275     returndate is NULL";
276
277     # print $query;
278     my $sth = $dbh->prepare($query);
279     $sth->execute;
280     my $data = $sth->fetchrow_hashref;
281     $sth->finish;
282     $sth = $dbh->prepare(
283         "Select count(*) from issues where
284     borrowernumber='$bornum' and date_due < now() and returndate is NULL"
285     );
286     $sth->execute;
287     my $data2 = $sth->fetchrow_hashref;
288     $sth->finish;
289     $sth = $dbh->prepare(
290         "Select sum(amountoutstanding) from accountlines where
291     borrowernumber='$bornum'"
292     );
293     $sth->execute;
294     my $data3 = $sth->fetchrow_hashref;
295     $sth->finish;
296
297     return ( $data2->{'count(*)'}, $data->{'count(*)'},
298         $data3->{'sum(amountoutstanding)'} );
299 }
300
301 sub modmember {
302         my (%data) = @_;
303         my $dbh = C4::Context->dbh;
304         $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
305         $data{'dateexpiry'}=format_date_in_iso($data{'dateexpiry'});
306         $data{'dateenrolled'}=format_date_in_iso($data{'dateenrolled'});
307 #       warn "num user".$data{'borrowernumber'};
308         my $query;
309         my $sth;
310         $data{'userid'}='' if ($data{'password'}eq '');
311         # test to know if u must update or not the borrower password
312         if ($data{'password'} eq '****'){
313                 
314                 $query="UPDATE borrowers SET 
315                 cardnumber  = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
316                 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
317                 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
318                 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
319                 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
320                 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship =  ?,ethnicity = ?,
321                 ethnotes = ?,sex = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ? 
322                 WHERE borrowernumber=$data{'borrowernumber'}";
323         $sth=$dbh->prepare($query);
324         $sth->execute(
325                 $data{'cardnumber'},$data{'surname'},
326                 $data{'firstname'},$data{'title'},
327                 $data{'othernames'},$data{'initials'},
328                 $data{'streetnumber'},$data{'streettype'},
329                 $data{'address'},$data{'address2'},
330                 $data{'city'},$data{'zipcode'},
331                 $data{'email'},$data{'phone'},
332                 $data{'mobile'},$data{'fax'},
333                 $data{'emailpro'},$data{'phonepro'},
334                 $data{'B_streetnumber'},$data{'B_streettype'},
335                 $data{'B_address'},$data{'B_city'},
336                 $data{'B_zipcode'},$data{'B_email'},$data{'B_phone'},
337                 $data{'dateofbirth'},$data{'branchcode'},
338                 $data{'categorycode'},$data{'dateenrolled'},
339                 $data{'dateexpiry'},$data{'gonenoaddress'},
340                 $data{'lost'},$data{'debarred'},
341                 $data{'contactname'},$data{'contactfirstname'},
342                 $data{'contacttitle'},$data{'guarantorid'},
343                 $data{'borrowernotes'},$data{'relationship'},
344                 $data{'ethnicity'},$data{'ethnotes'},
345                 $data{'sex'},
346                 $data{'flags'},$data{'userid'},
347                 $data{'opacnote'},$data{'contactnote'},
348                 $data{'sort1'},$data{'sort2'});
349         }
350         else{
351                 
352                 ($data{'password'}=md5_base64($data{'password'})) if ($data{'password'} ne '');
353                 $query="UPDATE borrowers SET 
354                 cardnumber  = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
355                 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
356                 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
357                 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
358                 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
359                 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship =  ?,ethnicity = ?,
360                 ethnotes = ?,sex = ?,password = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ? 
361                 WHERE borrowernumber=$data{'borrowernumber'}";
362         $sth=$dbh->prepare($query);
363         $sth->execute(
364                 $data{'cardnumber'},$data{'surname'},
365                 $data{'firstname'},$data{'title'},
366                 $data{'othernames'},$data{'initials'},
367                 $data{'streetnumber'},$data{'streettype'},
368                 $data{'address'},$data{'address2'},
369                 $data{'city'},$data{'zipcode'},
370                 $data{'email'},$data{'phone'},
371                 $data{'mobile'},$data{'fax'},
372                 $data{'emailpro'},$data{'phonepro'},
373                 $data{'B_streetnumber'},$data{'B_streettype'},
374                 $data{'B_address'},$data{'B_city'},
375                 $data{'B_zipcode'},$data{'B_email'},$data{'B_phone'},
376                 $data{'dateofbirth'},$data{'branchcode'},
377                 $data{'categorycode'},$data{'dateenrolled'},
378                 $data{'dateexpiry'},$data{'gonenoaddress'},
379                 $data{'lost'},$data{'debarred'},
380                 $data{'contactname'},$data{'contactfirstname'},
381                 $data{'contacttitle'},$data{'guarantorid'},
382                 $data{'borrowernotes'},$data{'relationship'},
383                 $data{'ethnicity'},$data{'ethnotes'},
384                 $data{'sex'},$data{'password'},
385                 $data{'flags'},$data{'userid'},
386                 $data{'opacnote'},$data{'contactnote'},
387                 $data{'sort1'},$data{'sort2'}
388                 );
389         }
390         $sth->finish;
391         # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
392         # so when we update information for an adult we should check for guarantees and update the relevant part
393         # of their records, ie addresses and phone numbers
394         my ($category_type,undef)=getcategorytype($data{'category_type'});
395         if ($category_type eq 'A' ){
396         
397         # is adult check guarantees;
398                 updateguarantees(%data);
399         
400         }
401
402
403 }
404
405 sub newmember {
406     my (%data) = @_;
407     my $dbh = C4::Context->dbh;
408     $data{'userid'} = '' unless $data{'password'};
409     $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
410     $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
411     $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
412     $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
413         my $query =
414         "insert into borrowers set cardnumber="
415       . $dbh->quote( $data{'cardnumber'} )
416       . ",surname="
417       . $dbh->quote( $data{'surname'} )
418       . ",firstname="
419       . $dbh->quote( $data{'firstname'} )
420       . ",title="
421       . $dbh->quote( $data{'title'} )
422       . ",othernames="
423       . $dbh->quote( $data{'othernames'} )
424       . ",initials="
425       . $dbh->quote( $data{'initials'} )
426       . ",streetnumber="
427       . $dbh->quote( $data{'streetnumber'} )
428       . ",streettype="
429       . $dbh->quote( $data{'streettype'} )
430       . ",address="
431       . $dbh->quote( $data{'address'} )
432       . ",address2="
433       . $dbh->quote( $data{'address2'} )
434       . ",zipcode="
435       . $dbh->quote( $data{'zipcode'} )
436       . ",city="
437       . $dbh->quote( $data{'city'} )
438       . ",phone="
439       . $dbh->quote( $data{'phone'} )
440       . ",email="
441       . $dbh->quote( $data{'email'} )
442       . ",mobile="
443       . $dbh->quote( $data{'mobile'} )
444       . ",phonepro="
445       . $dbh->quote( $data{'phonepro'} )
446       . ",opacnote="
447       . $dbh->quote( $data{'opacnote'} )
448       . ",guarantorid="
449       . $dbh->quote( $data{'guarantorid'} )
450       . ",dateofbirth="
451       . $dbh->quote( $data{'dateofbirth'} )
452       . ",branchcode="
453       . $dbh->quote( $data{'branchcode'} )
454       . ",categorycode="
455       . $dbh->quote( $data{'categorycode'} )
456       . ",dateenrolled="
457       . $dbh->quote( $data{'dateenrolled'} )
458       . ",contactname="
459       . $dbh->quote( $data{'contactname'} )
460       . ",borrowernotes="
461       . $dbh->quote( $data{'borrowernotes'} )
462       . ",dateexpiry="
463       . $dbh->quote( $data{'dateexpiry'} )
464       . ",contactnote="
465       . $dbh->quote( $data{'contactnote'} )
466       . ",B_address="
467       . $dbh->quote( $data{'B_address'} )
468       . ",B_zipcode="
469       . $dbh->quote( $data{'B_zipcode'} )
470       . ",B_city="
471       . $dbh->quote( $data{'B_city'} )
472       . ",B_phone="
473       . $dbh->quote( $data{'B_phone'} )
474       . ",B_email="
475       . $dbh->quote( $data{'B_email'}, )
476       . ",password="
477       . $dbh->quote( $data{'password'} )
478       . ",userid="
479       . $dbh->quote( $data{'userid'} )
480       . ",sort1="
481       . $dbh->quote( $data{'sort1'} )
482       . ",sort2="
483       . $dbh->quote( $data{'sort2'} )
484       . ",contacttitle="
485       . $dbh->quote( $data{'contacttitle'} )
486       . ",emailpro="
487       . $dbh->quote( $data{'emailpro'} )
488       . ",contactfirstname="
489       . $dbh->quote( $data{'contactfirstname'} ) 
490       . ",sex="
491       . $dbh->quote( $data{'sex'} ) 
492       . ",fax="
493       . $dbh->quote( $data{'fax'} )
494       . ",flags="
495       . $dbh->quote( $data{'flags'} )
496       . ",relationship="
497       . $dbh->quote( $data{'relationship'} )
498       . ",B_streetnumber="
499       . $dbh->quote( $data{'B_streetnumber'}) 
500       . ",B_streettype="
501       . $dbh->quote( $data{'B_streettype'})
502       . ",gonenoaddress="
503       . $dbh->quote( $data{'gonenoaddress'})    
504       . ",lost="
505       . $dbh->quote( $data{'lost'})                     
506       . ",debarred="
507       . $dbh->quote( $data{'debarred'})
508       . ",ethnicity="
509       . $dbh->quote( $data{'ethnicity'})
510       . ",ethnotes="
511       . $dbh->quote( $data{'ethnotes'});
512
513     my $sth = $dbh->prepare($query);
514     $sth->execute;
515     $sth->finish;
516     $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
517     return $data{'borrowernumber'};
518 }
519
520 sub changepassword {
521     my ( $uid, $member, $digest ) = @_;
522     my $dbh = C4::Context->dbh;
523
524 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
525 #Then we need to tell the user and have them create a new one.
526     my $sth =
527       $dbh->prepare(
528         "select * from borrowers where userid=? and borrowernumber != ?");
529     $sth->execute( $uid, $member );
530     if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
531         return 0;
532     }
533     else {
534
535         #Everything is good so we can update the information.
536         $sth =
537           $dbh->prepare(
538             "update borrowers set userid=?, password=? where borrowernumber=?");
539         $sth->execute( $uid, $digest, $member );
540         return 1;
541     }
542 }
543
544 sub getmemberfromuserid {
545     my ($userid) = @_;
546     my $dbh      = C4::Context->dbh;
547     my $sth      = $dbh->prepare("select * from borrowers where userid=?");
548     $sth->execute($userid);
549     return $sth->fetchrow_hashref;
550 }
551
552 sub updateguarantees {
553     my (%data) = @_;
554     my $dbh = C4::Context->dbh;
555     my ( $count, $guarantees ) = findguarantees( $data{'borrowernumber'} );
556     for ( my $i = 0 ; $i < $count ; $i++ ) {
557
558         # FIXME
559         # It looks like the $i is only being returned to handle walking through
560         # the array, which is probably better done as a foreach loop.
561         #
562         my $guaquery =
563 "update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
564                 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
565                 ,streetaddress='$data{'address'}'
566                 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
567         my $sth3 = $dbh->prepare($guaquery);
568         $sth3->execute;
569         $sth3->finish;
570     }
571 }
572 ################################################################################
573
574 =item fixup_cardnumber
575
576 Warning: The caller is responsible for locking the members table in write
577 mode, to avoid database corruption.
578
579 =cut
580
581 use vars qw( @weightings );
582 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
583
584 sub fixup_cardnumber ($) {
585     my ($cardnumber) = @_;
586     my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
587     $autonumber_members = 0 unless defined $autonumber_members;
588
589     # Find out whether member numbers should be generated
590     # automatically. Should be either "1" or something else.
591     # Defaults to "0", which is interpreted as "no".
592
593     #     if ($cardnumber !~ /\S/ && $autonumber_members) {
594     if ($autonumber_members) {
595         my $dbh = C4::Context->dbh;
596         if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
597
598             # if checkdigit is selected, calculate katipo-style cardnumber.
599             # otherwise, just use the max()
600             # purpose: generate checksum'd member numbers.
601             # We'll assume we just got the max value of digits 2-8 of member #'s
602             # from the database and our job is to increment that by one,
603             # determine the 1st and 9th digits and return the full string.
604             my $sth =
605               $dbh->prepare(
606                 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
607               );
608             $sth->execute;
609
610             my $data = $sth->fetchrow_hashref;
611             $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
612             $sth->finish;
613             if ( !$cardnumber ) {    # If DB has no values,
614                 $cardnumber = 1000000;    # start at 1000000
615             }
616             else {
617                 $cardnumber += 1;
618             }
619
620             my $sum = 0;
621             for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
622
623                 # read weightings, left to right, 1 char at a time
624                 my $temp1 = $weightings[$i];
625
626                 # sequence left to right, 1 char at a time
627                 my $temp2 = substr( $cardnumber, $i, 1 );
628
629                 # mult each char 1-7 by its corresponding weighting
630                 $sum += $temp1 * $temp2;
631             }
632
633             my $rem = ( $sum % 11 );
634             $rem = 'X' if $rem == 10;
635
636             $cardnumber = "V$cardnumber$rem";
637         }
638         else {
639
640      # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
641      # better. I'll leave the original in in case it needs to be changed for you
642             my $sth =
643               $dbh->prepare(
644                 "select max(cast(cardnumber as signed)) from borrowers");
645
646       #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
647
648             $sth->execute;
649
650             my ($result) = $sth->fetchrow;
651             $sth->finish;
652             $cardnumber = $result + 1;
653         }
654     }
655     return $cardnumber;
656 }
657
658 sub findguarantees {
659     my ($bornum) = @_;
660     my $dbh      = C4::Context->dbh;
661     my $sth      = $dbh->prepare(
662         "select cardnumber,borrowernumber from borrowers where
663   guarantorid=?"
664     );
665     $sth->execute($bornum);
666     my @dat;
667     my $i = 0;
668     while ( my $data = $sth->fetchrow_hashref ) {
669         $dat[$i] = $data;
670         $i++;
671     }
672     $sth->finish;
673     return ( $i, \@dat );
674 }
675
676 =item findguarantor
677
678   $guarantor = &findguarantor($borrower_no);
679   $guarantor_cardno = $guarantor->{"cardnumber"};
680   $guarantor_surname = $guarantor->{"surname"};
681   ...
682
683 C<&findguarantor> takes a borrower number (presumably that of a child
684 patron), finds the guarantor for C<$borrower_no> (the child's parent),
685 and returns the record for the guarantor.
686
687 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
688 from the C<borrowers> database table;
689
690 =cut
691
692 #'
693 sub findguarantor {
694     my ($bornum) = @_;
695     my $dbh = C4::Context->dbh;
696     my $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
697     $sth->execute($bornum);
698     my $data = $sth->fetchrow_hashref;
699     $sth->finish;
700     return ($data);
701 }
702
703 =item GuarantornameSearch
704
705   ($count, $borrowers) = &GuarantornameSearch($env, $searchstring, $type);
706
707 Looks up guarantor  by name.
708
709 C<$env> is ignored.
710
711 BUGFIX 499: C<$type> is now used to determine type of search.
712 if $type is "simple", search is performed on the first letter of the
713 surname only.
714
715 C<$searchstring> is a space-separated list of search terms. Each term
716 must match the beginning a borrower's surname, first name, or other
717 name.
718
719 C<&GuarantornameSearch> returns a two-element list. C<$borrowers> is a
720 reference-to-array; each element is a reference-to-hash, whose keys
721 are the fields of the C<borrowers> table in the Koha database.
722 C<$count> is the number of elements in C<$borrowers>.
723
724 return all info from guarantor =>only category_type A
725
726 =cut
727
728 #'
729 #used by member enquiries from the intranet
730 #called by guarantor_search.pl
731 sub GuarantornameSearch {
732     my ( $env, $searchstring, $orderby, $type ) = @_;
733     my $dbh   = C4::Context->dbh;
734     my $query = "";
735     my $count;
736     my @data;
737     my @bind = ();
738
739     if ( $type eq "simple" )    # simple search for one letter only
740     {
741         $query =
742 "Select * from borrowers,categories  where borrowers.categorycode=categories.categorycode and category_type='A'  and  surname like ? order by $orderby";
743         @bind = ("$searchstring%");
744     }
745     else    # advanced search looking in surname, firstname and othernames
746     {
747         @data  = split( ' ', $searchstring );
748         $count = @data;
749         $query = "Select * from borrowers,categories
750                 where ((surname like ? or surname like ?
751                 or firstname  like ? or firstname like ?
752                 or othernames like ? or othernames like ?) and borrowers.categorycode=categories.categorycode and category_type='A' 
753                 ";
754         @bind = (
755             "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
756             "$data[0]%", "% $data[0]%"
757         );
758         for ( my $i = 1 ; $i < $count ; $i++ ) {
759             $query = $query . " and (" . " surname like ? or surname like ?
760                         or firstname  like ? or firstname like ?
761                         or othernames like ? or othernames like ?)";
762             push( @bind,
763                 "$data[$i]%",   "% $data[$i]%", "$data[$i]%",
764                 "% $data[$i]%", "$data[$i]%",   "% $data[$i]%" );
765
766             # FIXME - .= <<EOT;
767         }
768         $query = $query . ") or cardnumber like ?
769                 order by $orderby";
770         push( @bind, $searchstring );
771
772         # FIXME - .= <<EOT;
773     }
774
775     my $sth = $dbh->prepare($query);
776     $sth->execute(@bind);
777     my @results;
778     my $cnt = $sth->rows;
779     while ( my $data = $sth->fetchrow_hashref ) {
780         push( @results, $data );
781     }
782
783     #  $sth->execute;
784     $sth->finish;
785     return ( $cnt, \@results );
786 }
787
788 =item NewBorrowerNumber
789
790   $num = &NewBorrowerNumber();
791
792 Allocates a new, unused borrower number, and returns it.
793
794 =cut
795
796 #'
797 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
798 # Pick one and stick with it. Preferably use the other one. This function
799 # doesn't belong in C4::Search.
800 sub NewBorrowerNumber {
801     my $dbh = C4::Context->dbh;
802     my $sth = $dbh->prepare("Select max(borrowernumber) from borrowers");
803     $sth->execute;
804     my $data = $sth->fetchrow_hashref;
805     $sth->finish;
806     $data->{'max(borrowernumber)'}++;
807     return ( $data->{'max(borrowernumber)'} );
808 }
809
810 =head2 borrissues
811
812   ($count, $issues) = &borrissues($borrowernumber);
813
814 Looks up what the patron with the given borrowernumber has borrowed.
815
816 C<&borrissues> returns a two-element array. C<$issues> is a
817 reference-to-array, where each element is a reference-to-hash; the
818 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
819 in the Koha database. C<$count> is the number of elements in
820 C<$issues>.
821
822 =cut
823
824 #'
825 sub borrissues {
826     my ($bornum) = @_;
827     my $dbh      = C4::Context->dbh;
828     my $sth      = $dbh->prepare(
829         "Select * from issues,biblio,items where borrowernumber=?
830    and items.itemnumber=issues.itemnumber
831         and items.biblionumber=biblio.biblionumber
832         and issues.returndate is NULL order by date_due"
833     );
834     $sth->execute($bornum);
835     my @result;
836     while ( my $data = $sth->fetchrow_hashref ) {
837         push @result, $data;
838     }
839     $sth->finish;
840     return ( scalar(@result), \@result );
841 }
842
843 =head2 allissues
844
845   ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
846
847 Looks up what the patron with the given borrowernumber has borrowed,
848 and sorts the results.
849
850 C<$sortkey> is the name of a field on which to sort the results. This
851 should be the name of a field in the C<issues>, C<biblio>,
852 C<biblioitems>, or C<items> table in the Koha database.
853
854 C<$limit> is the maximum number of results to return.
855
856 C<&allissues> returns a two-element array. C<$issues> is a
857 reference-to-array, where each element is a reference-to-hash; the
858 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
859 C<items> tables of the Koha database. C<$count> is the number of
860 elements in C<$issues>
861
862 =cut
863
864 #'
865 sub allissues {
866     my ( $bornum, $order, $limit ) = @_;
867
868     #FIXME: sanity-check order and limit
869     my $dbh   = C4::Context->dbh;
870     my $count=0;
871     my $query = "Select * from issues,biblio,items,biblioitems
872   where borrowernumber=? and
873   items.biblioitemnumber=biblioitems.biblioitemnumber and
874   items.itemnumber=issues.itemnumber and
875   items.biblionumber=biblio.biblionumber order by $order";
876     if ( $limit != 0 ) {
877         $query .= " limit $limit";
878     }
879
880     #print $query;
881     my $sth = $dbh->prepare($query);
882     $sth->execute($bornum);
883     my @result;
884     my $i = 0;
885     while ( my $data = $sth->fetchrow_hashref ) {
886         $result[$i] = $data;
887         $i++;
888         $count++;
889     }
890
891     # get all issued items for bornum from oldissues table
892     # large chunk of older issues data put into table oldissues
893     # to speed up db calls for issuing items
894     if(C4::Context->preference("ReadingHistory")){
895           my $query2="SELECT * FROM oldissues,biblio,items,biblioitems
896                       WHERE borrowernumber=? 
897                       AND items.biblioitemnumber=biblioitems.biblioitemnumber
898                       AND items.itemnumber=oldissues.itemnumber
899                       AND items.biblionumber=biblio.biblionumber
900                       ORDER BY $order";
901           if ($limit !=0){
902                 $limit=$limit-$count;
903                 $query2.=" limit $limit";
904           }
905
906           my $sth2=$dbh->prepare($query2);
907           $sth2->execute($bornum);
908
909           while (my $data2=$sth2->fetchrow_hashref){
910                 $result[$i]=$data2;
911                 $i++;
912           }
913           $sth2->finish;
914     }
915     $sth->finish;
916
917     return ( $i, \@result );
918 }
919
920 =head2 getboracctrecord
921
922   ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
923
924 Looks up accounting data for the patron with the given borrowernumber.
925
926 C<$env> is ignored.
927
928 (FIXME - I'm not at all sure what this is about.)
929
930 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
931 reference-to-array, where each element is a reference-to-hash; the
932 keys are the fields of the C<accountlines> table in the Koha database.
933 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
934 total amount outstanding for all of the account lines.
935
936 =cut
937
938 #'
939 sub getboracctrecord {
940     my ( $env, $params ) = @_;
941     my $dbh = C4::Context->dbh;
942     my @acctlines;
943     my $numlines = 0;
944     my $sth      = $dbh->prepare(
945         "Select * from accountlines where
946 borrowernumber=? order by date desc,timestamp desc"
947     );
948
949     #   print $query;
950     $sth->execute( $params->{'borrowernumber'} );
951     my $total = 0;
952     while ( my $data = $sth->fetchrow_hashref ) {
953
954         #FIXME before reinstating: insecure?
955         #      if ($data->{'itemnumber'} ne ''){
956         #        $query="Select * from items,biblio where items.itemnumber=
957         #       '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
958         #       my $sth2=$dbh->prepare($query);
959         #       $sth2->execute;
960         #       my $data2=$sth2->fetchrow_hashref;
961         #       $sth2->finish;
962         #       $data=$data2;
963         #     }
964         $acctlines[$numlines] = $data;
965         $numlines++;
966         $total += $data->{'amountoutstanding'};
967     }
968     $sth->finish;
969     return ( $numlines, \@acctlines, $total );
970 }
971
972 =head2 checkuniquemember (OUEST-PROVENCE)
973
974   $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
975
976 Checks that a member exists or not in the database.
977
978 C<&result> is 1 (=exist) or 0 (=does not exist)
979 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
980 C<&surname> is the surname
981 C<&categorycode> is from categorycode table
982 C<&firstname> is the firstname (only if collectivity=0)
983 C<&dateofbirth> is the date of birth (only if collectivity=0)
984
985 =cut
986
987 sub checkuniquemember {
988     my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
989     my $dbh = C4::Context->dbh;
990     my $request;
991     if ($collectivity) {
992
993 #                               $request="select count(*) from borrowers where surname=? and categorycode=?";
994         $request =
995           "select borrowernumber,categorycode from borrowers where surname=? ";
996     }
997     else {
998
999 #                               $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1000         $request =
1001 "select borrowernumber,categorycode from borrowers where surname=?  and firstname=? and dateofbirth=?";
1002     }
1003     my $sth = $dbh->prepare($request);
1004     if ($collectivity) {
1005         $sth->execute( uc($surname) );
1006     }
1007     else {
1008         $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1009     }
1010     my @data = $sth->fetchrow;
1011     if ( $data[0] ) {
1012         $sth->finish;
1013         return $data[0], $data[1];
1014
1015         #
1016     }
1017     else {
1018         $sth->finish;
1019         return 0;
1020     }
1021 }
1022
1023 =head2 getzipnamecity (OUEST-PROVENCE)
1024
1025 take all info from table city for the fields city and  zip
1026 check for the name and the zip code of the city selected
1027
1028 =cut
1029
1030 sub getzipnamecity {
1031     my ($cityid) = @_;
1032     my $dbh      = C4::Context->dbh;
1033     my $sth      =
1034       $dbh->prepare(
1035         "select city_name,city_zipcode from cities where cityid=? ");
1036     $sth->execute($cityid);
1037     my @data = $sth->fetchrow;
1038     return $data[0], $data[1];
1039 }
1040
1041 =head2 updatechildguarantor (OUEST-PROVENCE)
1042
1043 check for title,firstname,surname,adress,zip code and city  from guarantor to 
1044 guarantorchild
1045
1046 =cut
1047
1048 #'
1049
1050 sub getguarantordata {
1051     my ($borrowerid) = @_;
1052     my $dbh          = C4::Context->dbh;
1053     my $sth          =
1054       $dbh->prepare(
1055 "Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro,fax  from borrowers where borrowernumber =? "
1056       );
1057     $sth->execute($borrowerid);
1058     my $guarantor_data = $sth->fetchrow_hashref;
1059     $sth->finish;
1060     return $guarantor_data;
1061 }
1062
1063 =head2 getdcity (OUEST-PROVENCE)
1064 recover cityid  with city_name condition
1065 =cut
1066
1067 sub getidcity {
1068     my ($city_name) = @_;
1069     my $dbh = C4::Context->dbh;
1070     my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1071     $sth->execute($city_name);
1072     my $data = $sth->fetchrow;
1073     return $data;
1074 }
1075
1076 =head2 getcategorytype (OUEST-PROVENCE)
1077
1078 check for the category_type with categorycode
1079 and return the category_type 
1080
1081 =cut
1082
1083 sub getcategorytype {
1084     my ($categorycode) = @_;
1085     my $dbh            = C4::Context->dbh;
1086     my $sth            =
1087       $dbh->prepare(
1088 "Select category_type,description from categories where categorycode=?  "
1089       );
1090     $sth->execute($categorycode);
1091     my ( $category_type, $description ) = $sth->fetchrow;
1092     return $category_type, $description;
1093 }
1094
1095 sub calcexpirydate {
1096     my ( $categorycode, $dateenrolled ) = @_;
1097     my $dbh = C4::Context->dbh;
1098     my $sth =
1099       $dbh->prepare(
1100         "select enrolmentperiod from categories where categorycode=?");
1101     $sth->execute($categorycode);
1102     my ($enrolmentperiod) = $sth->fetchrow;
1103     $enrolmentperiod = 12 unless ($enrolmentperiod);
1104     return format_date_in_iso(
1105         &DateCalc( $dateenrolled, "$enrolmentperiod months" ) );
1106 }
1107
1108 =head2 checkuserpassword (OUEST-PROVENCE)
1109
1110 check for the password and login are not used
1111 return the number of record 
1112 0=> NOT USED 1=> USED
1113
1114 =cut
1115
1116 sub checkuserpassword {
1117     my ( $borrowernumber, $userid, $password ) = @_;
1118     $password = md5_base64($password);
1119     my $dbh = C4::Context->dbh;
1120     my $sth =
1121       $dbh->prepare(
1122 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1123       );
1124     $sth->execute( $borrowernumber, $userid, $password );
1125     my $number_rows = $sth->fetchrow;
1126     return $number_rows;
1127
1128 }
1129
1130 =head2 borrowercategories
1131
1132   ($codes_arrayref, $labels_hashref) = &borrowercategories();
1133
1134 Looks up the different types of borrowers in the database. Returns two
1135 elements: a reference-to-array, which lists the borrower category
1136 codes, and a reference-to-hash, which maps the borrower category codes
1137 to category descriptions.
1138
1139 =cut
1140
1141 #'
1142 sub borrowercategories {
1143     my ( $category_type, $action ) = @_;
1144     my $dbh = C4::Context->dbh;
1145     my $request;
1146     $request =
1147 "Select categorycode,description from categories where category_type=? order by categorycode";
1148     my $sth = $dbh->prepare($request);
1149     $sth->execute($category_type);
1150     my %labels;
1151     my @codes;
1152
1153     while ( my $data = $sth->fetchrow_hashref ) {
1154         push @codes, $data->{'categorycode'};
1155         $labels{ $data->{'categorycode'} } = $data->{'description'};
1156     }
1157     $sth->finish;
1158     return ( \@codes, \%labels );
1159 }
1160
1161 =head2 getborrowercategory
1162
1163   $description,$dateofbirthrequired,$upperagelimit,$category_type = &getborrowercategory($categorycode);
1164
1165 Given the borrower's category code, the function returns the corresponding
1166 description , dateofbirthrequired , upperagelimit and category type for a comprehensive information display.
1167
1168 =cut
1169
1170 sub getborrowercategory {
1171     my ($catcode) = @_;
1172     my $dbh       = C4::Context->dbh;
1173     my $sth       =
1174       $dbh->prepare(
1175         "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?");
1176     $sth->execute($catcode);
1177     my ($description,$dateofbirthrequired,$upperagelimit,$category_type) = $sth->fetchrow();
1178     $sth->finish();
1179     return ($description,$dateofbirthrequired,$upperagelimit,$category_type);
1180 }    # sub getborrowercategory
1181
1182
1183
1184 =head2 ethnicitycategories
1185
1186   ($codes_arrayref, $labels_hashref) = &ethnicitycategories();
1187
1188 Looks up the different ethnic types in the database. Returns two
1189 elements: a reference-to-array, which lists the ethnicity codes, and a
1190 reference-to-hash, which maps the ethnicity codes to ethnicity
1191 descriptions.
1192
1193 =cut
1194
1195 #'
1196
1197 sub ethnicitycategories {
1198     my $dbh = C4::Context->dbh;
1199     my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1200     $sth->execute;
1201     my %labels;
1202     my @codes;
1203     while ( my $data = $sth->fetchrow_hashref ) {
1204         push @codes, $data->{'code'};
1205         $labels{ $data->{'code'} } = $data->{'name'};
1206     }
1207     $sth->finish;
1208     return ( \@codes, \%labels );
1209 }
1210
1211 =head2 fixEthnicity
1212
1213   $ethn_name = &fixEthnicity($ethn_code);
1214
1215 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1216 corresponding descriptive name from the C<ethnicity> table in the
1217 Koha database ("European" or "Pacific Islander").
1218
1219 =cut
1220
1221 #'
1222
1223 sub fixEthnicity($) {
1224
1225     my $ethnicity = shift;
1226     my $dbh       = C4::Context->dbh;
1227     my $sth       = $dbh->prepare("Select name from ethnicity where code = ?");
1228     $sth->execute($ethnicity);
1229     my $data = $sth->fetchrow_hashref;
1230     $sth->finish;
1231     return $data->{'name'};
1232 }    # sub fixEthnicity
1233
1234
1235
1236 =head2 get_age
1237
1238   $dateofbirth,$date = &get_age($date);
1239
1240 this function return the borrowers age with the value of dateofbirth
1241
1242 =cut
1243 #'
1244 sub get_age {
1245     my ($date, $date_ref) = @_;
1246
1247     if (not defined $date_ref) {
1248         $date_ref = sprintf('%04d-%02d-%02d', Today());
1249     }
1250
1251     my ($year1, $month1, $day1) = split /-/, $date;
1252     my ($year2, $month2, $day2) = split /-/, $date_ref;
1253
1254     my $age = $year2 - $year1;
1255     if ($month1.$day1 > $month2.$day2) {
1256         $age--;
1257     }
1258
1259     return $age;
1260 }# sub get_age
1261
1262
1263
1264 =head2 get_institutions
1265   $insitutions = get_institutions();
1266
1267 Just returns a list of all the borrowers of type I, borrownumber and name
1268 =cut
1269
1270 #'
1271 sub get_institutions {
1272     my $dbh = C4::Context->dbh();
1273     my $sth =
1274       $dbh->prepare(
1275 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1276       );
1277     $sth->execute('I');
1278     my %orgs;
1279     while ( my $data = $sth->fetchrow_hashref() ) {
1280         $orgs{ $data->{'borrowernumber'} } = $data;
1281     }
1282     $sth->finish();
1283     return ( \%orgs );
1284
1285 }    # sub get_institutions
1286
1287 =head2 add_member_orgs
1288
1289   add_member_orgs($borrowernumber,$borrowernumbers);
1290
1291 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1292
1293 =cut
1294
1295 #'
1296 sub add_member_orgs {
1297     my ( $borrowernumber, $otherborrowers ) = @_;
1298     my $dbh   = C4::Context->dbh();
1299     my $query =
1300       "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1301     my $sth = $dbh->prepare($query);
1302     foreach my $bornum (@$otherborrowers) {
1303         $sth->execute( $borrowernumber, $bornum );
1304     }
1305     $sth->finish();
1306
1307 }    # sub add_member_orgs
1308
1309 =head2 GetBorrowersFromSurname
1310
1311 =over 4
1312
1313 \@resutlts = GetBorrowersFromSurname($surname)
1314 this function get the list of borrower names like $surname.
1315 return :
1316 the table of results in @results
1317
1318 =back
1319
1320 =cut
1321 sub GetBorrowersFromSurname  {
1322     my ($searchstring)=@_;
1323     my $dbh = C4::Context->dbh;
1324     $searchstring=~ s/\'/\\\'/g;
1325     my @data=split(' ',$searchstring);
1326     my $count=@data;
1327     my $query = qq|
1328         SELECT   surname,firstname
1329         FROM     borrowers
1330         WHERE    (surname like ?)
1331         ORDER BY surname
1332     |;
1333     my $sth=$dbh->prepare($query);
1334     $sth->execute("$data[0]%");
1335     my @results;
1336     my $count = 0;
1337     while (my $data=$sth->fetchrow_hashref){
1338          push(@results,$data);
1339          $count++;
1340     }
1341      $sth->finish;
1342      return ($count,\@results);
1343 }
1344
1345 =head2 GetBranchCodeFromBorrowers
1346
1347 =over 4
1348
1349 $sth = GetBranchCodeFromBorrowers();
1350
1351 this function just prepare the SQL request.
1352 After this function, don't forget to execute it by using $sth->execute($borrowernumber)
1353 return :
1354 $sth = $dbh->prepare($query).
1355
1356 =back
1357
1358 =cut
1359 sub GetBranchCodeFromBorrowers {
1360     my $dbh = C4::Context->dbh;
1361     my $query = qq|
1362         SELECT flags, branchcode
1363         FROM   borrowers
1364         WHERE  borrowernumber = ?
1365     |;
1366     return $dbh->prepare($query);
1367 }
1368 END { }       # module clean-up code here (global destructor)
1369
1370 1;