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
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
63 &borrowercategories &getborrowercategory
65 ðnicitycategories get_institutions add_member_orgs
66 &get_age &GetBorrowersFromSurname &GetBranchCodeFromBorrowers
72 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
74 Looks up patrons (borrowers) by name.
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
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
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>.
94 #used by member enquiries from the intranet
97 my ( $env, $searchstring, $orderby, $type ) = @_;
98 my $dbh = C4::Context->dbh;
104 if ( $type eq "simple" ) # simple search for one letter only
107 "Select * from borrowers where surname like ? order by $orderby";
108 @bind = ("$searchstring%");
110 else # advanced search looking in surname, firstname and othernames
112 @data = split( ' ', $searchstring );
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 ?)
120 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
121 "$data[0]%", "% $data[0]%"
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 ?)";
128 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
129 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
133 $query = $query . ") or cardnumber like ?
135 push( @bind, $searchstring );
140 my $sth = $dbh->prepare($query);
142 # warn "Q $orderby : $query";
143 $sth->execute(@bind);
145 my $cnt = $sth->rows;
146 while ( my $data = $sth->fetchrow_hashref ) {
147 push( @results, $data );
152 return ( $cnt, \@results );
157 $borrower = &getmember($cardnumber, $borrowernumber);
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.
163 C<&getmember> returns a reference-to-hash whose keys are the fields of
164 the C<borrowers> table in the Koha database.
170 my ( $cardnumber, $bornum ) = @_;
171 $cardnumber = uc $cardnumber;
172 my $dbh = C4::Context->dbh;
174 if ( $bornum eq '' ) {
175 $sth = $dbh->prepare("Select * from borrowers where cardnumber=?");
176 $sth->execute($cardnumber);
179 $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
180 $sth->execute($bornum);
182 my $data = $sth->fetchrow_hashref;
187 else { # try with firstname
190 $dbh->prepare("select * from borrowers where firstname=?");
191 $sth->execute($cardnumber);
192 my $data = $sth->fetchrow_hashref;
202 $borrower = &borrdata($cardnumber, $borrowernumber);
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.
208 C<&borrdata> returns a reference-to-hash whose keys are the fields of
209 the C<borrowers> table in the Koha database.
215 my ( $cardnumber, $bornum ) = @_;
216 $cardnumber = uc $cardnumber;
217 my $dbh = C4::Context->dbh;
219 if ( $bornum eq '' ) {
222 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?"
224 $sth->execute($cardnumber);
229 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
231 $sth->execute($bornum);
233 my $data = $sth->fetchrow_hashref;
234 # warn "DATA" . $data->{category_type};
239 else { # try with firstname
243 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname=?"
245 $sth->execute($cardnumber);
246 my $data = $sth->fetchrow_hashref;
256 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
258 Returns aggregate data about items borrowed by the patron with the
259 given borrowernumber.
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.
272 my ( $env, $bornum ) = @_;
273 my $dbh = C4::Context->dbh;
274 my $query = "Select count(*) from issues where borrowernumber='$bornum' and
278 my $sth = $dbh->prepare($query);
280 my $data = $sth->fetchrow_hashref;
282 $sth = $dbh->prepare(
283 "Select count(*) from issues where
284 borrowernumber='$bornum' and date_due < now() and returndate is NULL"
287 my $data2 = $sth->fetchrow_hashref;
289 $sth = $dbh->prepare(
290 "Select sum(amountoutstanding) from accountlines where
291 borrowernumber='$bornum'"
294 my $data3 = $sth->fetchrow_hashref;
297 return ( $data2->{'count(*)'}, $data->{'count(*)'},
298 $data3->{'sum(amountoutstanding)'} );
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'};
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 '****'){
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);
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'},
346 $data{'flags'},$data{'userid'},
347 $data{'opacnote'},$data{'contactnote'},
348 $data{'sort1'},$data{'sort2'});
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);
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'}
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' ){
397 # is adult check guarantees;
398 updateguarantees(%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'} );
414 "insert into borrowers set cardnumber="
415 . $dbh->quote( $data{'cardnumber'} )
417 . $dbh->quote( $data{'surname'} )
419 . $dbh->quote( $data{'firstname'} )
421 . $dbh->quote( $data{'title'} )
423 . $dbh->quote( $data{'othernames'} )
425 . $dbh->quote( $data{'initials'} )
427 . $dbh->quote( $data{'streetnumber'} )
429 . $dbh->quote( $data{'streettype'} )
431 . $dbh->quote( $data{'address'} )
433 . $dbh->quote( $data{'address2'} )
435 . $dbh->quote( $data{'zipcode'} )
437 . $dbh->quote( $data{'city'} )
439 . $dbh->quote( $data{'phone'} )
441 . $dbh->quote( $data{'email'} )
443 . $dbh->quote( $data{'mobile'} )
445 . $dbh->quote( $data{'phonepro'} )
447 . $dbh->quote( $data{'opacnote'} )
449 . $dbh->quote( $data{'guarantorid'} )
451 . $dbh->quote( $data{'dateofbirth'} )
453 . $dbh->quote( $data{'branchcode'} )
455 . $dbh->quote( $data{'categorycode'} )
457 . $dbh->quote( $data{'dateenrolled'} )
459 . $dbh->quote( $data{'contactname'} )
461 . $dbh->quote( $data{'borrowernotes'} )
463 . $dbh->quote( $data{'dateexpiry'} )
465 . $dbh->quote( $data{'contactnote'} )
467 . $dbh->quote( $data{'B_address'} )
469 . $dbh->quote( $data{'B_zipcode'} )
471 . $dbh->quote( $data{'B_city'} )
473 . $dbh->quote( $data{'B_phone'} )
475 . $dbh->quote( $data{'B_email'}, )
477 . $dbh->quote( $data{'password'} )
479 . $dbh->quote( $data{'userid'} )
481 . $dbh->quote( $data{'sort1'} )
483 . $dbh->quote( $data{'sort2'} )
485 . $dbh->quote( $data{'contacttitle'} )
487 . $dbh->quote( $data{'emailpro'} )
488 . ",contactfirstname="
489 . $dbh->quote( $data{'contactfirstname'} )
491 . $dbh->quote( $data{'sex'} )
493 . $dbh->quote( $data{'fax'} )
495 . $dbh->quote( $data{'flags'} )
497 . $dbh->quote( $data{'relationship'} )
499 . $dbh->quote( $data{'B_streetnumber'})
501 . $dbh->quote( $data{'B_streettype'})
503 . $dbh->quote( $data{'gonenoaddress'})
505 . $dbh->quote( $data{'lost'})
507 . $dbh->quote( $data{'debarred'})
509 . $dbh->quote( $data{'ethnicity'})
511 . $dbh->quote( $data{'ethnotes'});
513 my $sth = $dbh->prepare($query);
516 $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
517 return $data{'borrowernumber'};
521 my ( $uid, $member, $digest ) = @_;
522 my $dbh = C4::Context->dbh;
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.
528 "select * from borrowers where userid=? and borrowernumber != ?");
529 $sth->execute( $uid, $member );
530 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
535 #Everything is good so we can update the information.
538 "update borrowers set userid=?, password=? where borrowernumber=?");
539 $sth->execute( $uid, $digest, $member );
544 sub getmemberfromuserid {
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;
552 sub updateguarantees {
554 my $dbh = C4::Context->dbh;
555 my ( $count, $guarantees ) = findguarantees( $data{'borrowernumber'} );
556 for ( my $i = 0 ; $i < $count ; $i++ ) {
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.
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);
572 ################################################################################
574 =item fixup_cardnumber
576 Warning: The caller is responsible for locking the members table in write
577 mode, to avoid database corruption.
581 use vars qw( @weightings );
582 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
584 sub fixup_cardnumber ($) {
585 my ($cardnumber) = @_;
586 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
587 $autonumber_members = 0 unless defined $autonumber_members;
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".
593 # if ($cardnumber !~ /\S/ && $autonumber_members) {
594 if ($autonumber_members) {
595 my $dbh = C4::Context->dbh;
596 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
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.
606 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
610 my $data = $sth->fetchrow_hashref;
611 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
613 if ( !$cardnumber ) { # If DB has no values,
614 $cardnumber = 1000000; # start at 1000000
621 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
623 # read weightings, left to right, 1 char at a time
624 my $temp1 = $weightings[$i];
626 # sequence left to right, 1 char at a time
627 my $temp2 = substr( $cardnumber, $i, 1 );
629 # mult each char 1-7 by its corresponding weighting
630 $sum += $temp1 * $temp2;
633 my $rem = ( $sum % 11 );
634 $rem = 'X' if $rem == 10;
636 $cardnumber = "V$cardnumber$rem";
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
644 "select max(cast(cardnumber as signed)) from borrowers");
646 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
650 my ($result) = $sth->fetchrow;
652 $cardnumber = $result + 1;
660 my $dbh = C4::Context->dbh;
661 my $sth = $dbh->prepare(
662 "select cardnumber,borrowernumber from borrowers where
665 $sth->execute($bornum);
668 while ( my $data = $sth->fetchrow_hashref ) {
673 return ( $i, \@dat );
678 $guarantor = &findguarantor($borrower_no);
679 $guarantor_cardno = $guarantor->{"cardnumber"};
680 $guarantor_surname = $guarantor->{"surname"};
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.
687 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
688 from the C<borrowers> database table;
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;
703 =item GuarantornameSearch
705 ($count, $borrowers) = &GuarantornameSearch($env, $searchstring, $type);
707 Looks up guarantor by name.
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
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
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>.
724 return all info from guarantor =>only category_type A
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;
739 if ( $type eq "simple" ) # simple search for one letter only
742 "Select * from borrowers,categories where borrowers.categorycode=categories.categorycode and category_type='A' and surname like ? order by $orderby";
743 @bind = ("$searchstring%");
745 else # advanced search looking in surname, firstname and othernames
747 @data = split( ' ', $searchstring );
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'
755 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
756 "$data[0]%", "% $data[0]%"
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 ?)";
763 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
764 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
768 $query = $query . ") or cardnumber like ?
770 push( @bind, $searchstring );
775 my $sth = $dbh->prepare($query);
776 $sth->execute(@bind);
778 my $cnt = $sth->rows;
779 while ( my $data = $sth->fetchrow_hashref ) {
780 push( @results, $data );
785 return ( $cnt, \@results );
788 =item NewBorrowerNumber
790 $num = &NewBorrowerNumber();
792 Allocates a new, unused borrower number, and returns it.
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");
804 my $data = $sth->fetchrow_hashref;
806 $data->{'max(borrowernumber)'}++;
807 return ( $data->{'max(borrowernumber)'} );
812 ($count, $issues) = &borrissues($borrowernumber);
814 Looks up what the patron with the given borrowernumber has borrowed.
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
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"
834 $sth->execute($bornum);
836 while ( my $data = $sth->fetchrow_hashref ) {
840 return ( scalar(@result), \@result );
845 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
847 Looks up what the patron with the given borrowernumber has borrowed,
848 and sorts the results.
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.
854 C<$limit> is the maximum number of results to return.
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>
866 my ( $bornum, $order, $limit ) = @_;
868 #FIXME: sanity-check order and limit
869 my $dbh = C4::Context->dbh;
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";
877 $query .= " limit $limit";
881 my $sth = $dbh->prepare($query);
882 $sth->execute($bornum);
885 while ( my $data = $sth->fetchrow_hashref ) {
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
902 $limit=$limit-$count;
903 $query2.=" limit $limit";
906 my $sth2=$dbh->prepare($query2);
907 $sth2->execute($bornum);
909 while (my $data2=$sth2->fetchrow_hashref){
917 return ( $i, \@result );
920 =head2 getboracctrecord
922 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
924 Looks up accounting data for the patron with the given borrowernumber.
928 (FIXME - I'm not at all sure what this is about.)
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.
939 sub getboracctrecord {
940 my ( $env, $params ) = @_;
941 my $dbh = C4::Context->dbh;
944 my $sth = $dbh->prepare(
945 "Select * from accountlines where
946 borrowernumber=? order by date desc,timestamp desc"
950 $sth->execute( $params->{'borrowernumber'} );
952 while ( my $data = $sth->fetchrow_hashref ) {
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);
960 # my $data2=$sth2->fetchrow_hashref;
964 $acctlines[$numlines] = $data;
966 $total += $data->{'amountoutstanding'};
969 return ( $numlines, \@acctlines, $total );
972 =head2 checkuniquemember (OUEST-PROVENCE)
974 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
976 Checks that a member exists or not in the database.
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)
987 sub checkuniquemember {
988 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
989 my $dbh = C4::Context->dbh;
993 # $request="select count(*) from borrowers where surname=? and categorycode=?";
995 "select borrowernumber,categorycode from borrowers where surname=? ";
999 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1001 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1003 my $sth = $dbh->prepare($request);
1004 if ($collectivity) {
1005 $sth->execute( uc($surname) );
1008 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1010 my @data = $sth->fetchrow;
1013 return $data[0], $data[1];
1023 =head2 getzipnamecity (OUEST-PROVENCE)
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
1030 sub getzipnamecity {
1032 my $dbh = C4::Context->dbh;
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];
1041 =head2 updatechildguarantor (OUEST-PROVENCE)
1043 check for title,firstname,surname,adress,zip code and city from guarantor to
1050 sub getguarantordata {
1051 my ($borrowerid) = @_;
1052 my $dbh = C4::Context->dbh;
1055 "Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro,fax from borrowers where borrowernumber =? "
1057 $sth->execute($borrowerid);
1058 my $guarantor_data = $sth->fetchrow_hashref;
1060 return $guarantor_data;
1063 =head2 getdcity (OUEST-PROVENCE)
1064 recover cityid with city_name condition
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;
1076 =head2 getcategorytype (OUEST-PROVENCE)
1078 check for the category_type with categorycode
1079 and return the category_type
1083 sub getcategorytype {
1084 my ($categorycode) = @_;
1085 my $dbh = C4::Context->dbh;
1088 "Select category_type,description from categories where categorycode=? "
1090 $sth->execute($categorycode);
1091 my ( $category_type, $description ) = $sth->fetchrow;
1092 return $category_type, $description;
1095 sub calcexpirydate {
1096 my ( $categorycode, $dateenrolled ) = @_;
1097 my $dbh = C4::Context->dbh;
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" ) );
1108 =head2 checkuserpassword (OUEST-PROVENCE)
1110 check for the password and login are not used
1111 return the number of record
1112 0=> NOT USED 1=> USED
1116 sub checkuserpassword {
1117 my ( $borrowernumber, $userid, $password ) = @_;
1118 $password = md5_base64($password);
1119 my $dbh = C4::Context->dbh;
1122 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1124 $sth->execute( $borrowernumber, $userid, $password );
1125 my $number_rows = $sth->fetchrow;
1126 return $number_rows;
1130 =head2 borrowercategories
1132 ($codes_arrayref, $labels_hashref) = &borrowercategories();
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.
1142 sub borrowercategories {
1143 my ( $category_type, $action ) = @_;
1144 my $dbh = C4::Context->dbh;
1147 "Select categorycode,description from categories where category_type=? order by categorycode";
1148 my $sth = $dbh->prepare($request);
1149 $sth->execute($category_type);
1153 while ( my $data = $sth->fetchrow_hashref ) {
1154 push @codes, $data->{'categorycode'};
1155 $labels{ $data->{'categorycode'} } = $data->{'description'};
1158 return ( \@codes, \%labels );
1161 =head2 getborrowercategory
1163 $description,$dateofbirthrequired,$upperagelimit,$category_type = &getborrowercategory($categorycode);
1165 Given the borrower's category code, the function returns the corresponding
1166 description , dateofbirthrequired , upperagelimit and category type for a comprehensive information display.
1170 sub getborrowercategory {
1172 my $dbh = C4::Context->dbh;
1175 "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?");
1176 $sth->execute($catcode);
1177 my ($description,$dateofbirthrequired,$upperagelimit,$category_type) = $sth->fetchrow();
1179 return ($description,$dateofbirthrequired,$upperagelimit,$category_type);
1180 } # sub getborrowercategory
1184 =head2 ethnicitycategories
1186 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
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
1197 sub ethnicitycategories {
1198 my $dbh = C4::Context->dbh;
1199 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1203 while ( my $data = $sth->fetchrow_hashref ) {
1204 push @codes, $data->{'code'};
1205 $labels{ $data->{'code'} } = $data->{'name'};
1208 return ( \@codes, \%labels );
1213 $ethn_name = &fixEthnicity($ethn_code);
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").
1223 sub fixEthnicity($) {
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;
1231 return $data->{'name'};
1232 } # sub fixEthnicity
1238 $dateofbirth,$date = &get_age($date);
1240 this function return the borrowers age with the value of dateofbirth
1245 my ($date, $date_ref) = @_;
1247 if (not defined $date_ref) {
1248 $date_ref = sprintf('%04d-%02d-%02d', Today());
1251 my ($year1, $month1, $day1) = split /-/, $date;
1252 my ($year2, $month2, $day2) = split /-/, $date_ref;
1254 my $age = $year2 - $year1;
1255 if ($month1.$day1 > $month2.$day2) {
1264 =head2 get_institutions
1265 $insitutions = get_institutions();
1267 Just returns a list of all the borrowers of type I, borrownumber and name
1271 sub get_institutions {
1272 my $dbh = C4::Context->dbh();
1275 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1279 while ( my $data = $sth->fetchrow_hashref() ) {
1280 $orgs{ $data->{'borrowernumber'} } = $data;
1285 } # sub get_institutions
1287 =head2 add_member_orgs
1289 add_member_orgs($borrowernumber,$borrowernumbers);
1291 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1296 sub add_member_orgs {
1297 my ( $borrowernumber, $otherborrowers ) = @_;
1298 my $dbh = C4::Context->dbh();
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 );
1307 } # sub add_member_orgs
1309 =head2 GetBorrowersFromSurname
1313 \@resutlts = GetBorrowersFromSurname($surname)
1314 this function get the list of borrower names like $surname.
1316 the table of results in @results
1321 sub GetBorrowersFromSurname {
1322 my ($searchstring)=@_;
1323 my $dbh = C4::Context->dbh;
1324 $searchstring=~ s/\'/\\\'/g;
1325 my @data=split(' ',$searchstring);
1328 SELECT surname,firstname
1330 WHERE (surname like ?)
1333 my $sth=$dbh->prepare($query);
1334 $sth->execute("$data[0]%");
1337 while (my $data=$sth->fetchrow_hashref){
1338 push(@results,$data);
1342 return ($count,\@results);
1345 =head2 GetBranchCodeFromBorrowers
1349 $sth = GetBranchCodeFromBorrowers();
1351 this function just prepare the SQL request.
1352 After this function, don't forget to execute it by using $sth->execute($borrowernumber)
1354 $sth = $dbh->prepare($query).
1359 sub GetBranchCodeFromBorrowers {
1360 my $dbh = C4::Context->dbh;
1362 SELECT flags, branchcode
1364 WHERE borrowernumber = ?
1366 return $dbh->prepare($query);
1368 END { } # module clean-up code here (global destructor)