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
29 use Digest::MD5 qw(md5_base64);
30 use Date::Calc qw/Today/;
32 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
34 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
38 C4::Members - Perl Module containing convenience functions for member handling
46 This module contains routines for adding, modifying and deleting members/patrons/borrowers
60 &BornameSearch &getmember &borrdata &borrdata2 &fixup_cardnumber &findguarantees &findguarantor &GuarantornameSearch &NewBorrowerNumber &modmember &newmember &changepassword &borrissues &allissues
61 &checkuniquemember &getzipnamecity &getidcity &getguarantordata &getcategorytype
62 &calcexpirydate &checkuserpassword
64 &borrowercategories &getborrowercategory
66 ðnicitycategories get_institutions add_member_orgs
73 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
75 Looks up patrons (borrowers) by name.
79 BUGFIX 499: C<$type> is now used to determine type of search.
80 if $type is "simple", search is performed on the first letter of the
83 C<$searchstring> is a space-separated list of search terms. Each term
84 must match the beginning a borrower's surname, first name, or other
87 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
88 reference-to-array; each element is a reference-to-hash, whose keys
89 are the fields of the C<borrowers> table in the Koha database.
90 C<$count> is the number of elements in C<$borrowers>.
95 #used by member enquiries from the intranet
98 my ( $env, $searchstring, $orderby, $type ) = @_;
99 my $dbh = C4::Context->dbh;
105 if ( $type eq "simple" ) # simple search for one letter only
108 "Select * from borrowers where surname like ? order by $orderby";
109 @bind = ("$searchstring%");
111 else # advanced search looking in surname, firstname and othernames
113 @data = split( ' ', $searchstring );
115 $query = "Select * from borrowers
116 where ((surname like ? or surname like ?
117 or firstname like ? or firstname like ?
118 or othernames like ? or othernames like ?)
121 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
122 "$data[0]%", "% $data[0]%"
124 for ( my $i = 1 ; $i < $count ; $i++ ) {
125 $query = $query . " and (" . " surname like ? or surname like ?
126 or firstname like ? or firstname like ?
127 or othernames like ? or othernames like ?)";
129 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
130 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
134 $query = $query . ") or cardnumber like ?
136 push( @bind, $searchstring );
141 my $sth = $dbh->prepare($query);
143 # warn "Q $orderby : $query";
144 $sth->execute(@bind);
146 my $cnt = $sth->rows;
147 while ( my $data = $sth->fetchrow_hashref ) {
148 push( @results, $data );
153 return ( $cnt, \@results );
158 $borrower = &getmember($cardnumber, $borrowernumber);
160 Looks up information about a patron (borrower) by either card number
161 or borrower number. If $borrowernumber is specified, C<&borrdata>
162 searches by borrower number; otherwise, it searches by card number.
164 C<&getmember> returns a reference-to-hash whose keys are the fields of
165 the C<borrowers> table in the Koha database.
171 my ( $cardnumber, $bornum ) = @_;
172 $cardnumber = uc $cardnumber;
173 my $dbh = C4::Context->dbh;
175 if ( $bornum eq '' ) {
176 $sth = $dbh->prepare("Select * from borrowers where cardnumber=?");
177 $sth->execute($cardnumber);
180 $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
181 $sth->execute($bornum);
183 my $data = $sth->fetchrow_hashref;
188 else { # try with firstname
191 $dbh->prepare("select * from borrowers where firstname=?");
192 $sth->execute($cardnumber);
193 my $data = $sth->fetchrow_hashref;
203 $borrower = &borrdata($cardnumber, $borrowernumber);
205 Looks up information about a patron (borrower) by either card number
206 or borrower number. If $borrowernumber is specified, C<&borrdata>
207 searches by borrower number; otherwise, it searches by card number.
209 C<&borrdata> returns a reference-to-hash whose keys are the fields of
210 the C<borrowers> table in the Koha database.
216 my ( $cardnumber, $bornum ) = @_;
217 $cardnumber = uc $cardnumber;
218 my $dbh = C4::Context->dbh;
220 if ( $bornum eq '' ) {
223 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?"
225 $sth->execute($cardnumber);
230 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
232 $sth->execute($bornum);
234 my $data = $sth->fetchrow_hashref;
235 # warn "DATA" . $data->{category_type};
240 else { # try with firstname
244 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname=?"
246 $sth->execute($cardnumber);
247 my $data = $sth->fetchrow_hashref;
257 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
259 Returns aggregate data about items borrowed by the patron with the
260 given borrowernumber.
264 C<&borrdata2> returns a three-element array. C<$borrowed> is the
265 number of books the patron currently has borrowed. C<$due> is the
266 number of overdue items the patron currently has borrowed. C<$fine> is
267 the total fine currently due by the borrower.
273 my ( $env, $bornum ) = @_;
274 my $dbh = C4::Context->dbh;
275 my $query = "Select count(*) from issues where borrowernumber='$bornum' and
279 my $sth = $dbh->prepare($query);
281 my $data = $sth->fetchrow_hashref;
283 $sth = $dbh->prepare(
284 "Select count(*) from issues where
285 borrowernumber='$bornum' and date_due < now() and returndate is NULL"
288 my $data2 = $sth->fetchrow_hashref;
290 $sth = $dbh->prepare(
291 "Select sum(amountoutstanding) from accountlines where
292 borrowernumber='$bornum'"
295 my $data3 = $sth->fetchrow_hashref;
298 return ( $data2->{'count(*)'}, $data->{'count(*)'},
299 $data3->{'sum(amountoutstanding)'} );
304 my $dbh = C4::Context->dbh;
305 $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
306 $data{'dateexpiry'}=format_date_in_iso($data{'dateexpiry'});
307 $data{'dateenrolled'}=format_date_in_iso($data{'dateenrolled'});
308 # warn "num user".$data{'borrowernumber'};
311 $data{'userid'}='' if ($data{'password'}eq '');
312 # test to know if u must update or not the borrower password
313 if ($data{'password'} eq '****'){
315 $query="UPDATE borrowers SET
316 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
317 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
318 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
319 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
320 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
321 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
322 ethnotes = ?,sex = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
323 WHERE borrowernumber=$data{'borrowernumber'}";
324 $sth=$dbh->prepare($query);
326 $data{'cardnumber'},$data{'surname'},
327 $data{'firstname'},$data{'title'},
328 $data{'othernames'},$data{'initials'},
329 $data{'streetnumber'},$data{'streettype'},
330 $data{'address'},$data{'address2'},
331 $data{'city'},$data{'zipcode'},
332 $data{'email'},$data{'phone'},
333 $data{'mobile'},$data{'fax'},
334 $data{'emailpro'},$data{'phonepro'},
335 $data{'B_streetnumber'},$data{'B_streettype'},
336 $data{'B_address'},$data{'B_city'},
337 $data{'B_zipcode'},$data{'B_email'},$data{'B_phone'},
338 $data{'dateofbirth'},$data{'branchcode'},
339 $data{'categorycode'},$data{'dateenrolled'},
340 $data{'dateexpiry'},$data{'gonenoaddress'},
341 $data{'lost'},$data{'debarred'},
342 $data{'contactname'},$data{'contactfirstname'},
343 $data{'contacttitle'},$data{'guarantorid'},
344 $data{'borrowernotes'},$data{'relationship'},
345 $data{'ethnicity'},$data{'ethnotes'},
347 $data{'flags'},$data{'userid'},
348 $data{'opacnote'},$data{'contactnote'},
349 $data{'sort1'},$data{'sort2'});
353 ($data{'password'}=md5_base64($data{'password'})) if ($data{'password'} ne '');
354 $query="UPDATE borrowers SET
355 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
356 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
357 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
358 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
359 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
360 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
361 ethnotes = ?,sex = ?,password = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
362 WHERE borrowernumber=$data{'borrowernumber'}";
363 $sth=$dbh->prepare($query);
365 $data{'cardnumber'},$data{'surname'},
366 $data{'firstname'},$data{'title'},
367 $data{'othernames'},$data{'initials'},
368 $data{'streetnumber'},$data{'streettype'},
369 $data{'address'},$data{'address2'},
370 $data{'city'},$data{'zipcode'},
371 $data{'email'},$data{'phone'},
372 $data{'mobile'},$data{'fax'},
373 $data{'emailpro'},$data{'phonepro'},
374 $data{'B_streetnumber'},$data{'B_streettype'},
375 $data{'B_address'},$data{'B_city'},
376 $data{'B_zipcode'},$data{'B_email'},$data{'B_phone'},
377 $data{'dateofbirth'},$data{'branchcode'},
378 $data{'categorycode'},$data{'dateenrolled'},
379 $data{'dateexpiry'},$data{'gonenoaddress'},
380 $data{'lost'},$data{'debarred'},
381 $data{'contactname'},$data{'contactfirstname'},
382 $data{'contacttitle'},$data{'guarantorid'},
383 $data{'borrowernotes'},$data{'relationship'},
384 $data{'ethnicity'},$data{'ethnotes'},
385 $data{'sex'},$data{'password'},
386 $data{'flags'},$data{'userid'},
387 $data{'opacnote'},$data{'contactnote'},
388 $data{'sort1'},$data{'sort2'}
392 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
393 # so when we update information for an adult we should check for guarantees and update the relevant part
394 # of their records, ie addresses and phone numbers
395 my ($category_type,undef)=getcategorytype($data{'category_type'});
396 if ($category_type eq 'A' ){
398 # is adult check guarantees;
399 updateguarantees(%data);
408 my $dbh = C4::Context->dbh;
409 $data{'userid'} = '' unless $data{'password'};
410 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
411 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
412 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
413 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
415 "insert into borrowers set cardnumber="
416 . $dbh->quote( $data{'cardnumber'} )
418 . $dbh->quote( $data{'surname'} )
420 . $dbh->quote( $data{'firstname'} )
422 . $dbh->quote( $data{'title'} )
424 . $dbh->quote( $data{'othernames'} )
426 . $dbh->quote( $data{'initials'} )
428 . $dbh->quote( $data{'streetnumber'} )
430 . $dbh->quote( $data{'streettype'} )
432 . $dbh->quote( $data{'address'} )
434 . $dbh->quote( $data{'address2'} )
436 . $dbh->quote( $data{'zipcode'} )
438 . $dbh->quote( $data{'city'} )
440 . $dbh->quote( $data{'phone'} )
442 . $dbh->quote( $data{'email'} )
444 . $dbh->quote( $data{'mobile'} )
446 . $dbh->quote( $data{'phonepro'} )
448 . $dbh->quote( $data{'opacnote'} )
450 . $dbh->quote( $data{'guarantorid'} )
452 . $dbh->quote( $data{'dateofbirth'} )
454 . $dbh->quote( $data{'branchcode'} )
456 . $dbh->quote( $data{'categorycode'} )
458 . $dbh->quote( $data{'dateenrolled'} )
460 . $dbh->quote( $data{'contactname'} )
462 . $dbh->quote( $data{'borrowernotes'} )
464 . $dbh->quote( $data{'dateexpiry'} )
466 . $dbh->quote( $data{'contactnote'} )
468 . $dbh->quote( $data{'B_address'} )
470 . $dbh->quote( $data{'B_zipcode'} )
472 . $dbh->quote( $data{'B_city'} )
474 . $dbh->quote( $data{'B_phone'} )
476 . $dbh->quote( $data{'B_email'}, )
478 . $dbh->quote( $data{'password'} )
480 . $dbh->quote( $data{'userid'} )
482 . $dbh->quote( $data{'sort1'} )
484 . $dbh->quote( $data{'sort2'} )
486 . $dbh->quote( $data{'contacttitle'} )
488 . $dbh->quote( $data{'emailpro'} )
489 . ",contactfirstname="
490 . $dbh->quote( $data{'contactfirstname'} )
492 . $dbh->quote( $data{'sex'} )
494 . $dbh->quote( $data{'fax'} )
496 . $dbh->quote( $data{'flags'} )
498 . $dbh->quote( $data{'relationship'} )
500 . $dbh->quote( $data{'B_streetnumber'})
502 . $dbh->quote( $data{'B_streettype'})
504 . $dbh->quote( $data{'gonenoaddress'})
506 . $dbh->quote( $data{'lost'})
508 . $dbh->quote( $data{'debarred'})
510 . $dbh->quote( $data{'ethnicity'})
512 . $dbh->quote( $data{'ethnotes'});
514 my $sth = $dbh->prepare($query);
517 $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
518 return $data{'borrowernumber'};
522 my ( $uid, $member, $digest ) = @_;
523 my $dbh = C4::Context->dbh;
525 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
526 #Then we need to tell the user and have them create a new one.
529 "select * from borrowers where userid=? and borrowernumber != ?");
530 $sth->execute( $uid, $member );
531 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
536 #Everything is good so we can update the information.
539 "update borrowers set userid=?, password=? where borrowernumber=?");
540 $sth->execute( $uid, $digest, $member );
545 sub getmemberfromuserid {
547 my $dbh = C4::Context->dbh;
548 my $sth = $dbh->prepare("select * from borrowers where userid=?");
549 $sth->execute($userid);
550 return $sth->fetchrow_hashref;
553 sub updateguarantees {
555 my $dbh = C4::Context->dbh;
556 my ( $count, $guarantees ) = findguarantees( $data{'borrowernumber'} );
557 for ( my $i = 0 ; $i < $count ; $i++ ) {
560 # It looks like the $i is only being returned to handle walking through
561 # the array, which is probably better done as a foreach loop.
564 "update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
565 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
566 ,streetaddress='$data{'address'}'
567 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
568 my $sth3 = $dbh->prepare($guaquery);
573 ################################################################################
575 =item fixup_cardnumber
577 Warning: The caller is responsible for locking the members table in write
578 mode, to avoid database corruption.
582 use vars qw( @weightings );
583 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
585 sub fixup_cardnumber ($) {
586 my ($cardnumber) = @_;
587 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
588 $autonumber_members = 0 unless defined $autonumber_members;
590 # Find out whether member numbers should be generated
591 # automatically. Should be either "1" or something else.
592 # Defaults to "0", which is interpreted as "no".
594 # if ($cardnumber !~ /\S/ && $autonumber_members) {
595 if ($autonumber_members) {
596 my $dbh = C4::Context->dbh;
597 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
599 # if checkdigit is selected, calculate katipo-style cardnumber.
600 # otherwise, just use the max()
601 # purpose: generate checksum'd member numbers.
602 # We'll assume we just got the max value of digits 2-8 of member #'s
603 # from the database and our job is to increment that by one,
604 # determine the 1st and 9th digits and return the full string.
607 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
611 my $data = $sth->fetchrow_hashref;
612 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
614 if ( !$cardnumber ) { # If DB has no values,
615 $cardnumber = 1000000; # start at 1000000
622 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
624 # read weightings, left to right, 1 char at a time
625 my $temp1 = $weightings[$i];
627 # sequence left to right, 1 char at a time
628 my $temp2 = substr( $cardnumber, $i, 1 );
630 # mult each char 1-7 by its corresponding weighting
631 $sum += $temp1 * $temp2;
634 my $rem = ( $sum % 11 );
635 $rem = 'X' if $rem == 10;
637 $cardnumber = "V$cardnumber$rem";
641 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
642 # better. I'll leave the original in in case it needs to be changed for you
645 "select max(cast(cardnumber as signed)) from borrowers");
647 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
651 my ($result) = $sth->fetchrow;
653 $cardnumber = $result + 1;
661 my $dbh = C4::Context->dbh;
662 my $sth = $dbh->prepare(
663 "select cardnumber,borrowernumber from borrowers where
666 $sth->execute($bornum);
669 while ( my $data = $sth->fetchrow_hashref ) {
674 return ( $i, \@dat );
679 $guarantor = &findguarantor($borrower_no);
680 $guarantor_cardno = $guarantor->{"cardnumber"};
681 $guarantor_surname = $guarantor->{"surname"};
684 C<&findguarantor> takes a borrower number (presumably that of a child
685 patron), finds the guarantor for C<$borrower_no> (the child's parent),
686 and returns the record for the guarantor.
688 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
689 from the C<borrowers> database table;
696 my $dbh = C4::Context->dbh;
697 my $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
698 $sth->execute($bornum);
699 my $data = $sth->fetchrow_hashref;
704 =item GuarantornameSearch
706 ($count, $borrowers) = &GuarantornameSearch($env, $searchstring, $type);
708 Looks up guarantor by name.
712 BUGFIX 499: C<$type> is now used to determine type of search.
713 if $type is "simple", search is performed on the first letter of the
716 C<$searchstring> is a space-separated list of search terms. Each term
717 must match the beginning a borrower's surname, first name, or other
720 C<&GuarantornameSearch> returns a two-element list. C<$borrowers> is a
721 reference-to-array; each element is a reference-to-hash, whose keys
722 are the fields of the C<borrowers> table in the Koha database.
723 C<$count> is the number of elements in C<$borrowers>.
725 return all info from guarantor =>only category_type A
730 #used by member enquiries from the intranet
731 #called by guarantor_search.pl
732 sub GuarantornameSearch {
733 my ( $env, $searchstring, $orderby, $type ) = @_;
734 my $dbh = C4::Context->dbh;
740 if ( $type eq "simple" ) # simple search for one letter only
743 "Select * from borrowers,categories where borrowers.categorycode=categories.categorycode and category_type='A' and surname like ? order by $orderby";
744 @bind = ("$searchstring%");
746 else # advanced search looking in surname, firstname and othernames
748 @data = split( ' ', $searchstring );
750 $query = "Select * from borrowers,categories
751 where ((surname like ? or surname like ?
752 or firstname like ? or firstname like ?
753 or othernames like ? or othernames like ?) and borrowers.categorycode=categories.categorycode and category_type='A'
756 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
757 "$data[0]%", "% $data[0]%"
759 for ( my $i = 1 ; $i < $count ; $i++ ) {
760 $query = $query . " and (" . " surname like ? or surname like ?
761 or firstname like ? or firstname like ?
762 or othernames like ? or othernames like ?)";
764 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
765 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
769 $query = $query . ") or cardnumber like ?
771 push( @bind, $searchstring );
776 my $sth = $dbh->prepare($query);
777 $sth->execute(@bind);
779 my $cnt = $sth->rows;
780 while ( my $data = $sth->fetchrow_hashref ) {
781 push( @results, $data );
786 return ( $cnt, \@results );
789 =item NewBorrowerNumber
791 $num = &NewBorrowerNumber();
793 Allocates a new, unused borrower number, and returns it.
798 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
799 # Pick one and stick with it. Preferably use the other one. This function
800 # doesn't belong in C4::Search.
801 sub NewBorrowerNumber {
802 my $dbh = C4::Context->dbh;
803 my $sth = $dbh->prepare("Select max(borrowernumber) from borrowers");
805 my $data = $sth->fetchrow_hashref;
807 $data->{'max(borrowernumber)'}++;
808 return ( $data->{'max(borrowernumber)'} );
813 ($count, $issues) = &borrissues($borrowernumber);
815 Looks up what the patron with the given borrowernumber has borrowed.
817 C<&borrissues> returns a two-element array. C<$issues> is a
818 reference-to-array, where each element is a reference-to-hash; the
819 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
820 in the Koha database. C<$count> is the number of elements in
828 my $dbh = C4::Context->dbh;
829 my $sth = $dbh->prepare(
830 "Select * from issues,biblio,items where borrowernumber=?
831 and items.itemnumber=issues.itemnumber
832 and items.biblionumber=biblio.biblionumber
833 and issues.returndate is NULL order by date_due"
835 $sth->execute($bornum);
837 while ( my $data = $sth->fetchrow_hashref ) {
841 return ( scalar(@result), \@result );
846 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
848 Looks up what the patron with the given borrowernumber has borrowed,
849 and sorts the results.
851 C<$sortkey> is the name of a field on which to sort the results. This
852 should be the name of a field in the C<issues>, C<biblio>,
853 C<biblioitems>, or C<items> table in the Koha database.
855 C<$limit> is the maximum number of results to return.
857 C<&allissues> returns a two-element array. C<$issues> is a
858 reference-to-array, where each element is a reference-to-hash; the
859 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
860 C<items> tables of the Koha database. C<$count> is the number of
861 elements in C<$issues>
867 my ( $bornum, $order, $limit ) = @_;
869 #FIXME: sanity-check order and limit
870 my $dbh = C4::Context->dbh;
872 my $query = "Select * from issues,biblio,items,biblioitems
873 where borrowernumber=? and
874 items.biblioitemnumber=biblioitems.biblioitemnumber and
875 items.itemnumber=issues.itemnumber and
876 items.biblionumber=biblio.biblionumber order by $order";
878 $query .= " limit $limit";
882 my $sth = $dbh->prepare($query);
883 $sth->execute($bornum);
886 while ( my $data = $sth->fetchrow_hashref ) {
892 # get all issued items for bornum from oldissues table
893 # large chunk of older issues data put into table oldissues
894 # to speed up db calls for issuing items
895 if(C4::Context->preference("ReadingHistory")){
896 my $query2="SELECT * FROM oldissues,biblio,items,biblioitems
897 WHERE borrowernumber=?
898 AND items.biblioitemnumber=biblioitems.biblioitemnumber
899 AND items.itemnumber=oldissues.itemnumber
900 AND items.biblionumber=biblio.biblionumber
903 $limit=$limit-$count;
904 $query2.=" limit $limit";
907 my $sth2=$dbh->prepare($query2);
908 $sth2->execute($bornum);
910 while (my $data2=$sth2->fetchrow_hashref){
918 return ( $i, \@result );
921 =head2 getboracctrecord
923 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
925 Looks up accounting data for the patron with the given borrowernumber.
929 (FIXME - I'm not at all sure what this is about.)
931 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
932 reference-to-array, where each element is a reference-to-hash; the
933 keys are the fields of the C<accountlines> table in the Koha database.
934 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
935 total amount outstanding for all of the account lines.
940 sub getboracctrecord {
941 my ( $env, $params ) = @_;
942 my $dbh = C4::Context->dbh;
945 my $sth = $dbh->prepare(
946 "Select * from accountlines where
947 borrowernumber=? order by date desc,timestamp desc"
951 $sth->execute( $params->{'borrowernumber'} );
953 while ( my $data = $sth->fetchrow_hashref ) {
955 #FIXME before reinstating: insecure?
956 # if ($data->{'itemnumber'} ne ''){
957 # $query="Select * from items,biblio where items.itemnumber=
958 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
959 # my $sth2=$dbh->prepare($query);
961 # my $data2=$sth2->fetchrow_hashref;
965 $acctlines[$numlines] = $data;
967 $total += $data->{'amountoutstanding'};
970 return ( $numlines, \@acctlines, $total );
973 =head2 checkuniquemember (OUEST-PROVENCE)
975 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
977 Checks that a member exists or not in the database.
979 C<&result> is 1 (=exist) or 0 (=does not exist)
980 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
981 C<&surname> is the surname
982 C<&categorycode> is from categorycode table
983 C<&firstname> is the firstname (only if collectivity=0)
984 C<&dateofbirth> is the date of birth (only if collectivity=0)
988 sub checkuniquemember {
989 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
990 my $dbh = C4::Context->dbh;
994 # $request="select count(*) from borrowers where surname=? and categorycode=?";
996 "select borrowernumber,categorycode from borrowers where surname=? ";
1000 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1002 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1004 my $sth = $dbh->prepare($request);
1005 if ($collectivity) {
1006 $sth->execute( uc($surname) );
1009 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1011 my @data = $sth->fetchrow;
1014 return $data[0], $data[1];
1024 =head2 getzipnamecity (OUEST-PROVENCE)
1026 take all info from table city for the fields city and zip
1027 check for the name and the zip code of the city selected
1031 sub getzipnamecity {
1033 my $dbh = C4::Context->dbh;
1036 "select city_name,city_zipcode from cities where cityid=? ");
1037 $sth->execute($cityid);
1038 my @data = $sth->fetchrow;
1039 return $data[0], $data[1];
1042 =head2 updatechildguarantor (OUEST-PROVENCE)
1044 check for title,firstname,surname,adress,zip code and city from guarantor to
1051 sub getguarantordata {
1052 my ($borrowerid) = @_;
1053 my $dbh = C4::Context->dbh;
1056 "Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro,fax from borrowers where borrowernumber =? "
1058 $sth->execute($borrowerid);
1059 my $guarantor_data = $sth->fetchrow_hashref;
1061 return $guarantor_data;
1064 =head2 getdcity (OUEST-PROVENCE)
1065 recover cityid with city_name condition
1069 my ($city_name) = @_;
1070 my $dbh = C4::Context->dbh;
1071 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1072 $sth->execute($city_name);
1073 my $data = $sth->fetchrow;
1077 =head2 getcategorytype (OUEST-PROVENCE)
1079 check for the category_type with categorycode
1080 and return the category_type
1084 sub getcategorytype {
1085 my ($categorycode) = @_;
1086 my $dbh = C4::Context->dbh;
1089 "Select category_type,description from categories where categorycode=? "
1091 $sth->execute($categorycode);
1092 my ( $category_type, $description ) = $sth->fetchrow;
1093 return $category_type, $description;
1096 sub calcexpirydate {
1097 my ( $categorycode, $dateenrolled ) = @_;
1098 my $dbh = C4::Context->dbh;
1101 "select enrolmentperiod from categories where categorycode=?");
1102 $sth->execute($categorycode);
1103 my ($enrolmentperiod) = $sth->fetchrow;
1104 $enrolmentperiod = 12 unless ($enrolmentperiod);
1105 return format_date_in_iso(
1106 &DateCalc( $dateenrolled, "$enrolmentperiod months" ) );
1109 =head2 checkuserpassword (OUEST-PROVENCE)
1111 check for the password and login are not used
1112 return the number of record
1113 0=> NOT USED 1=> USED
1117 sub checkuserpassword {
1118 my ( $borrowernumber, $userid, $password ) = @_;
1119 $password = md5_base64($password);
1120 my $dbh = C4::Context->dbh;
1123 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1125 $sth->execute( $borrowernumber, $userid, $password );
1126 my $number_rows = $sth->fetchrow;
1127 return $number_rows;
1131 =head2 borrowercategories
1133 ($codes_arrayref, $labels_hashref) = &borrowercategories();
1135 Looks up the different types of borrowers in the database. Returns two
1136 elements: a reference-to-array, which lists the borrower category
1137 codes, and a reference-to-hash, which maps the borrower category codes
1138 to category descriptions.
1143 sub borrowercategories {
1144 my ( $category_type, $action ) = @_;
1145 my $dbh = C4::Context->dbh;
1148 "Select categorycode,description from categories where category_type=? order by categorycode";
1149 my $sth = $dbh->prepare($request);
1150 $sth->execute($category_type);
1154 while ( my $data = $sth->fetchrow_hashref ) {
1155 push @codes, $data->{'categorycode'};
1156 $labels{ $data->{'categorycode'} } = $data->{'description'};
1159 return ( \@codes, \%labels );
1162 =head2 getborrowercategory
1164 $description,$dateofbirthrequired,$upperagelimit,$category_type = &getborrowercategory($categorycode);
1166 Given the borrower's category code, the function returns the corresponding
1167 description , dateofbirthrequired , upperagelimit and category type for a comprehensive information display.
1171 sub getborrowercategory {
1173 my $dbh = C4::Context->dbh;
1176 "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?");
1177 $sth->execute($catcode);
1178 my ($description,$dateofbirthrequired,$upperagelimit,$category_type) = $sth->fetchrow();
1180 return ($description,$dateofbirthrequired,$upperagelimit,$category_type);
1181 } # sub getborrowercategory
1185 =head2 ethnicitycategories
1187 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1189 Looks up the different ethnic types in the database. Returns two
1190 elements: a reference-to-array, which lists the ethnicity codes, and a
1191 reference-to-hash, which maps the ethnicity codes to ethnicity
1198 sub ethnicitycategories {
1199 my $dbh = C4::Context->dbh;
1200 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1204 while ( my $data = $sth->fetchrow_hashref ) {
1205 push @codes, $data->{'code'};
1206 $labels{ $data->{'code'} } = $data->{'name'};
1209 return ( \@codes, \%labels );
1214 $ethn_name = &fixEthnicity($ethn_code);
1216 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1217 corresponding descriptive name from the C<ethnicity> table in the
1218 Koha database ("European" or "Pacific Islander").
1224 sub fixEthnicity($) {
1226 my $ethnicity = shift;
1227 my $dbh = C4::Context->dbh;
1228 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1229 $sth->execute($ethnicity);
1230 my $data = $sth->fetchrow_hashref;
1232 return $data->{'name'};
1233 } # sub fixEthnicity
1239 $dateofbirth,$date = &get_age($date);
1241 this function return the borrowers age with the value of dateofbirth
1246 my ($date, $date_ref) = @_;
1248 if (not defined $date_ref) {
1249 $date_ref = sprintf('%04d-%02d-%02d', Today());
1252 my ($year1, $month1, $day1) = split /-/, $date;
1253 my ($year2, $month2, $day2) = split /-/, $date_ref;
1255 my $age = $year2 - $year1;
1256 if ($month1.$day1 > $month2.$day2) {
1265 =head2 get_institutions
1266 $insitutions = get_institutions();
1268 Just returns a list of all the borrowers of type I, borrownumber and name
1272 sub get_institutions {
1273 my $dbh = C4::Context->dbh();
1276 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1280 while ( my $data = $sth->fetchrow_hashref() ) {
1281 $orgs{ $data->{'borrowernumber'} } = $data;
1286 } # sub get_institutions
1288 =head2 add_member_orgs
1290 add_member_orgs($borrowernumber,$borrowernumbers);
1292 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1297 sub add_member_orgs {
1298 my ( $borrowernumber, $otherborrowers ) = @_;
1299 my $dbh = C4::Context->dbh();
1301 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1302 my $sth = $dbh->prepare($query);
1303 foreach my $bornum (@$otherborrowers) {
1304 $sth->execute( $borrowernumber, $bornum );
1308 } # sub add_member_orgs
1310 END { } # module clean-up code here (global destructor)