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);
409 my $dbh = C4::Context->dbh;
410 $data{'userid'} = '' unless $data{'password'};
411 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
412 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
413 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
414 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
416 "insert into borrowers set cardnumber="
417 . $dbh->quote( $data{'cardnumber'} )
419 . $dbh->quote( $data{'surname'} )
421 . $dbh->quote( $data{'firstname'} )
423 . $dbh->quote( $data{'title'} )
425 . $dbh->quote( $data{'othernames'} )
427 . $dbh->quote( $data{'initials'} )
429 . $dbh->quote( $data{'streetnumber'} )
431 . $dbh->quote( $data{'streettype'} )
433 . $dbh->quote( $data{'address'} )
435 . $dbh->quote( $data{'address2'} )
437 . $dbh->quote( $data{'zipcode'} )
439 . $dbh->quote( $data{'city'} )
441 . $dbh->quote( $data{'phone'} )
443 . $dbh->quote( $data{'email'} )
445 . $dbh->quote( $data{'mobile'} )
447 . $dbh->quote( $data{'phonepro'} )
449 . $dbh->quote( $data{'opacnote'} )
451 . $dbh->quote( $data{'guarantorid'} )
453 . $dbh->quote( $data{'dateofbirth'} )
455 . $dbh->quote( $data{'branchcode'} )
457 . $dbh->quote( $data{'categorycode'} )
459 . $dbh->quote( $data{'dateenrolled'} )
461 . $dbh->quote( $data{'contactname'} )
463 . $dbh->quote( $data{'borrowernotes'} )
465 . $dbh->quote( $data{'dateexpiry'} )
467 . $dbh->quote( $data{'contactnote'} )
469 . $dbh->quote( $data{'b_address'} )
471 . $dbh->quote( $data{'b_zipcode'} )
473 . $dbh->quote( $data{'b_city'} )
475 . $dbh->quote( $data{'b_phone'} )
477 . $dbh->quote( $data{'b_email'}, )
479 . $dbh->quote( $data{'password'} )
481 . $dbh->quote( $data{'userid'} )
483 . $dbh->quote( $data{'sort1'} )
485 . $dbh->quote( $data{'sort2'} )
487 . $dbh->quote( $data{'contacttitle'} )
489 . $dbh->quote( $data{'emailpro'} )
490 . ",contactfirstname="
491 . $dbh->quote( $data{'contactfirstname'} )
493 . $dbh->quote( $data{'sex'} )
495 . $dbh->quote( $data{'fax'} )
497 . $dbh->quote( $data{'flags'} )
499 . $dbh->quote( $data{'relationship'} )
501 . $dbh->quote( $data{'b_streetnumber'})
503 . $dbh->quote( $data{'b_streettype'})
505 . $dbh->quote( $data{'gonenoaddress'})
507 . $dbh->quote( $data{'lost'})
509 . $dbh->quote( $data{'debarred'})
511 . $dbh->quote( $data{'ethnicity'})
513 . $dbh->quote( $data{'ethnotes'});
515 my $sth = $dbh->prepare($query);
518 $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
519 return $data{'borrowernumber'};
523 my ( $uid, $member, $digest ) = @_;
524 my $dbh = C4::Context->dbh;
526 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
527 #Then we need to tell the user and have them create a new one.
530 "select * from borrowers where userid=? and borrowernumber != ?");
531 $sth->execute( $uid, $member );
532 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
537 #Everything is good so we can update the information.
540 "update borrowers set userid=?, password=? where borrowernumber=?");
541 $sth->execute( $uid, $digest, $member );
546 sub getmemberfromuserid {
548 my $dbh = C4::Context->dbh;
549 my $sth = $dbh->prepare("select * from borrowers where userid=?");
550 $sth->execute($userid);
551 return $sth->fetchrow_hashref;
554 sub updateguarantees {
556 my $dbh = C4::Context->dbh;
557 my ( $count, $guarantees ) = findguarantees( $data{'borrowernumber'} );
558 for ( my $i = 0 ; $i < $count ; $i++ ) {
561 # It looks like the $i is only being returned to handle walking through
562 # the array, which is probably better done as a foreach loop.
565 "update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
566 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
567 ,streetaddress='$data{'address'}'
568 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
569 my $sth3 = $dbh->prepare($guaquery);
574 ################################################################################
576 =item fixup_cardnumber
578 Warning: The caller is responsible for locking the members table in write
579 mode, to avoid database corruption.
583 use vars qw( @weightings );
584 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
586 sub fixup_cardnumber ($) {
587 my ($cardnumber) = @_;
588 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
589 $autonumber_members = 0 unless defined $autonumber_members;
591 # Find out whether member numbers should be generated
592 # automatically. Should be either "1" or something else.
593 # Defaults to "0", which is interpreted as "no".
595 # if ($cardnumber !~ /\S/ && $autonumber_members) {
596 if ($autonumber_members) {
597 my $dbh = C4::Context->dbh;
598 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
600 # if checkdigit is selected, calculate katipo-style cardnumber.
601 # otherwise, just use the max()
602 # purpose: generate checksum'd member numbers.
603 # We'll assume we just got the max value of digits 2-8 of member #'s
604 # from the database and our job is to increment that by one,
605 # determine the 1st and 9th digits and return the full string.
608 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
612 my $data = $sth->fetchrow_hashref;
613 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
615 if ( !$cardnumber ) { # If DB has no values,
616 $cardnumber = 1000000; # start at 1000000
623 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
625 # read weightings, left to right, 1 char at a time
626 my $temp1 = $weightings[$i];
628 # sequence left to right, 1 char at a time
629 my $temp2 = substr( $cardnumber, $i, 1 );
631 # mult each char 1-7 by its corresponding weighting
632 $sum += $temp1 * $temp2;
635 my $rem = ( $sum % 11 );
636 $rem = 'X' if $rem == 10;
638 $cardnumber = "V$cardnumber$rem";
642 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
643 # better. I'll leave the original in in case it needs to be changed for you
646 "select max(cast(cardnumber as signed)) from borrowers");
648 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
652 my ($result) = $sth->fetchrow;
654 $cardnumber = $result + 1;
662 my $dbh = C4::Context->dbh;
663 my $sth = $dbh->prepare(
664 "select cardnumber,borrowernumber from borrowers where
667 $sth->execute($bornum);
670 while ( my $data = $sth->fetchrow_hashref ) {
675 return ( $i, \@dat );
680 $guarantor = &findguarantor($borrower_no);
681 $guarantor_cardno = $guarantor->{"cardnumber"};
682 $guarantor_surname = $guarantor->{"surname"};
685 C<&findguarantor> takes a borrower number (presumably that of a child
686 patron), finds the guarantor for C<$borrower_no> (the child's parent),
687 and returns the record for the guarantor.
689 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
690 from the C<borrowers> database table;
697 my $dbh = C4::Context->dbh;
698 my $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
699 $sth->execute($bornum);
700 my $data = $sth->fetchrow_hashref;
705 =item GuarantornameSearch
707 ($count, $borrowers) = &GuarantornameSearch($env, $searchstring, $type);
709 Looks up guarantor by name.
713 BUGFIX 499: C<$type> is now used to determine type of search.
714 if $type is "simple", search is performed on the first letter of the
717 C<$searchstring> is a space-separated list of search terms. Each term
718 must match the beginning a borrower's surname, first name, or other
721 C<&GuarantornameSearch> returns a two-element list. C<$borrowers> is a
722 reference-to-array; each element is a reference-to-hash, whose keys
723 are the fields of the C<borrowers> table in the Koha database.
724 C<$count> is the number of elements in C<$borrowers>.
726 return all info from guarantor =>only category_type A
731 #used by member enquiries from the intranet
732 #called by guarantor_search.pl
733 sub GuarantornameSearch {
734 my ( $env, $searchstring, $orderby, $type ) = @_;
735 my $dbh = C4::Context->dbh;
741 if ( $type eq "simple" ) # simple search for one letter only
744 "Select * from borrowers,categories where borrowers.categorycode=categories.categorycode and category_type='A' and surname like ? order by $orderby";
745 @bind = ("$searchstring%");
747 else # advanced search looking in surname, firstname and othernames
749 @data = split( ' ', $searchstring );
751 $query = "Select * from borrowers,categories
752 where ((surname like ? or surname like ?
753 or firstname like ? or firstname like ?
754 or othernames like ? or othernames like ?) and borrowers.categorycode=categories.categorycode and category_type='A'
757 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
758 "$data[0]%", "% $data[0]%"
760 for ( my $i = 1 ; $i < $count ; $i++ ) {
761 $query = $query . " and (" . " surname like ? or surname like ?
762 or firstname like ? or firstname like ?
763 or othernames like ? or othernames like ?)";
765 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
766 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
770 $query = $query . ") or cardnumber like ?
772 push( @bind, $searchstring );
777 my $sth = $dbh->prepare($query);
778 $sth->execute(@bind);
780 my $cnt = $sth->rows;
781 while ( my $data = $sth->fetchrow_hashref ) {
782 push( @results, $data );
787 return ( $cnt, \@results );
790 =item NewBorrowerNumber
792 $num = &NewBorrowerNumber();
794 Allocates a new, unused borrower number, and returns it.
799 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
800 # Pick one and stick with it. Preferably use the other one. This function
801 # doesn't belong in C4::Search.
802 sub NewBorrowerNumber {
803 my $dbh = C4::Context->dbh;
804 my $sth = $dbh->prepare("Select max(borrowernumber) from borrowers");
806 my $data = $sth->fetchrow_hashref;
808 $data->{'max(borrowernumber)'}++;
809 return ( $data->{'max(borrowernumber)'} );
814 ($count, $issues) = &borrissues($borrowernumber);
816 Looks up what the patron with the given borrowernumber has borrowed.
818 C<&borrissues> returns a two-element array. C<$issues> is a
819 reference-to-array, where each element is a reference-to-hash; the
820 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
821 in the Koha database. C<$count> is the number of elements in
829 my $dbh = C4::Context->dbh;
830 my $sth = $dbh->prepare(
831 "Select * from issues,biblio,items where borrowernumber=?
832 and items.itemnumber=issues.itemnumber
833 and items.biblionumber=biblio.biblionumber
834 and issues.returndate is NULL order by date_due"
836 $sth->execute($bornum);
838 while ( my $data = $sth->fetchrow_hashref ) {
842 return ( scalar(@result), \@result );
847 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
849 Looks up what the patron with the given borrowernumber has borrowed,
850 and sorts the results.
852 C<$sortkey> is the name of a field on which to sort the results. This
853 should be the name of a field in the C<issues>, C<biblio>,
854 C<biblioitems>, or C<items> table in the Koha database.
856 C<$limit> is the maximum number of results to return.
858 C<&allissues> returns a two-element array. C<$issues> is a
859 reference-to-array, where each element is a reference-to-hash; the
860 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
861 C<items> tables of the Koha database. C<$count> is the number of
862 elements in C<$issues>
868 my ( $bornum, $order, $limit ) = @_;
870 #FIXME: sanity-check order and limit
871 my $dbh = C4::Context->dbh;
873 my $query = "Select * from issues,biblio,items,biblioitems
874 where borrowernumber=? and
875 items.biblioitemnumber=biblioitems.biblioitemnumber and
876 items.itemnumber=issues.itemnumber and
877 items.biblionumber=biblio.biblionumber order by $order";
879 $query .= " limit $limit";
883 my $sth = $dbh->prepare($query);
884 $sth->execute($bornum);
887 while ( my $data = $sth->fetchrow_hashref ) {
893 # get all issued items for bornum from oldissues table
894 # large chunk of older issues data put into table oldissues
895 # to speed up db calls for issuing items
896 if(C4::Context->preference("ReadingHistory")){
897 my $query2="SELECT * FROM oldissues,biblio,items,biblioitems
898 WHERE borrowernumber=?
899 AND items.biblioitemnumber=biblioitems.biblioitemnumber
900 AND items.itemnumber=oldissues.itemnumber
901 AND items.biblionumber=biblio.biblionumber
904 $limit=$limit-$count;
905 $query2.=" limit $limit";
908 my $sth2=$dbh->prepare($query2);
909 $sth2->execute($bornum);
911 while (my $data2=$sth2->fetchrow_hashref){
919 return ( $i, \@result );
922 =head2 getboracctrecord
924 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
926 Looks up accounting data for the patron with the given borrowernumber.
930 (FIXME - I'm not at all sure what this is about.)
932 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
933 reference-to-array, where each element is a reference-to-hash; the
934 keys are the fields of the C<accountlines> table in the Koha database.
935 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
936 total amount outstanding for all of the account lines.
941 sub getboracctrecord {
942 my ( $env, $params ) = @_;
943 my $dbh = C4::Context->dbh;
946 my $sth = $dbh->prepare(
947 "Select * from accountlines where
948 borrowernumber=? order by date desc,timestamp desc"
952 $sth->execute( $params->{'borrowernumber'} );
954 while ( my $data = $sth->fetchrow_hashref ) {
956 #FIXME before reinstating: insecure?
957 # if ($data->{'itemnumber'} ne ''){
958 # $query="Select * from items,biblio where items.itemnumber=
959 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
960 # my $sth2=$dbh->prepare($query);
962 # my $data2=$sth2->fetchrow_hashref;
966 $acctlines[$numlines] = $data;
968 $total += $data->{'amountoutstanding'};
971 return ( $numlines, \@acctlines, $total );
974 =head2 checkuniquemember (OUEST-PROVENCE)
976 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
978 Checks that a member exists or not in the database.
980 C<&result> is 1 (=exist) or 0 (=does not exist)
981 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
982 C<&surname> is the surname
983 C<&categorycode> is from categorycode table
984 C<&firstname> is the firstname (only if collectivity=0)
985 C<&dateofbirth> is the date of birth (only if collectivity=0)
989 sub checkuniquemember {
990 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
991 my $dbh = C4::Context->dbh;
995 # $request="select count(*) from borrowers where surname=? and categorycode=?";
997 "select borrowernumber,categorycode from borrowers where surname=? ";
1001 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1003 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1005 my $sth = $dbh->prepare($request);
1006 if ($collectivity) {
1007 $sth->execute( uc($surname) );
1010 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1012 my @data = $sth->fetchrow;
1015 return $data[0], $data[1];
1025 =head2 getzipnamecity (OUEST-PROVENCE)
1027 take all info from table city for the fields city and zip
1028 check for the name and the zip code of the city selected
1032 sub getzipnamecity {
1034 my $dbh = C4::Context->dbh;
1037 "select city_name,city_zipcode from cities where cityid=? ");
1038 $sth->execute($cityid);
1039 my @data = $sth->fetchrow;
1040 return $data[0], $data[1];
1043 =head2 updatechildguarantor (OUEST-PROVENCE)
1045 check for title,firstname,surname,adress,zip code and city from guarantor to
1052 sub getguarantordata {
1053 my ($borrowerid) = @_;
1054 my $dbh = C4::Context->dbh;
1057 "Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro from borrowers where borrowernumber =? "
1059 $sth->execute($borrowerid);
1060 my $guarantor_data = $sth->fetchrow_hashref;
1062 return $guarantor_data;
1065 =head2 getdcity (OUEST-PROVENCE)
1066 recover cityid with city_name condition
1070 my ($city_name) = @_;
1071 my $dbh = C4::Context->dbh;
1072 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1073 $sth->execute($city_name);
1074 my $data = $sth->fetchrow;
1078 =head2 getcategorytype (OUEST-PROVENCE)
1080 check for the category_type with categorycode
1081 and return the category_type
1085 sub getcategorytype {
1086 my ($categorycode) = @_;
1087 my $dbh = C4::Context->dbh;
1090 "Select category_type,description from categories where categorycode=? "
1092 $sth->execute($categorycode);
1093 my ( $category_type, $description ) = $sth->fetchrow;
1094 return $category_type, $description;
1097 sub calcexpirydate {
1098 my ( $categorycode, $dateenrolled ) = @_;
1099 my $dbh = C4::Context->dbh;
1102 "select enrolmentperiod from categories where categorycode=?");
1103 $sth->execute($categorycode);
1104 my ($enrolmentperiod) = $sth->fetchrow;
1105 $enrolmentperiod = 12 unless ($enrolmentperiod);
1106 return format_date_in_iso(
1107 &DateCalc( $dateenrolled, "$enrolmentperiod months" ) );
1110 =head2 checkuserpassword (OUEST-PROVENCE)
1112 check for the password and login are not used
1113 return the number of record
1114 0=> NOT USED 1=> USED
1118 sub checkuserpassword {
1119 my ( $borrowernumber, $userid, $password ) = @_;
1120 $password = md5_base64($password);
1121 my $dbh = C4::Context->dbh;
1124 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1126 $sth->execute( $borrowernumber, $userid, $password );
1127 my $number_rows = $sth->fetchrow;
1128 return $number_rows;
1132 =head2 borrowercategories
1134 ($codes_arrayref, $labels_hashref) = &borrowercategories();
1136 Looks up the different types of borrowers in the database. Returns two
1137 elements: a reference-to-array, which lists the borrower category
1138 codes, and a reference-to-hash, which maps the borrower category codes
1139 to category descriptions.
1144 sub borrowercategories {
1145 my ( $category_type, $action ) = @_;
1146 my $dbh = C4::Context->dbh;
1149 "Select categorycode,description from categories where category_type=? order by categorycode";
1150 my $sth = $dbh->prepare($request);
1151 $sth->execute($category_type);
1155 while ( my $data = $sth->fetchrow_hashref ) {
1156 push @codes, $data->{'categorycode'};
1157 $labels{ $data->{'categorycode'} } = $data->{'description'};
1160 return ( \@codes, \%labels );
1163 =head2 getborrowercategory
1165 $description = &getborrowercategory($categorycode);
1167 Given the borrower's category code, the function returns the corresponding
1168 description for a comprehensive information display.
1172 sub getborrowercategory {
1174 my $dbh = C4::Context->dbh;
1177 "SELECT description,dateofbirthrequired,upperagelimit FROM categories WHERE categorycode = ?");
1178 $sth->execute($catcode);
1179 my ($description,$dateofbirthrequired,$upperagelimit) = $sth->fetchrow();
1181 return ($description,$dateofbirthrequired,$upperagelimit);
1182 } # sub getborrowercategory
1186 =head2 ethnicitycategories
1188 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1190 Looks up the different ethnic types in the database. Returns two
1191 elements: a reference-to-array, which lists the ethnicity codes, and a
1192 reference-to-hash, which maps the ethnicity codes to ethnicity
1199 sub ethnicitycategories {
1200 my $dbh = C4::Context->dbh;
1201 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1205 while ( my $data = $sth->fetchrow_hashref ) {
1206 push @codes, $data->{'code'};
1207 $labels{ $data->{'code'} } = $data->{'name'};
1210 return ( \@codes, \%labels );
1215 $ethn_name = &fixEthnicity($ethn_code);
1217 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1218 corresponding descriptive name from the C<ethnicity> table in the
1219 Koha database ("European" or "Pacific Islander").
1225 sub fixEthnicity($) {
1227 my $ethnicity = shift;
1228 my $dbh = C4::Context->dbh;
1229 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1230 $sth->execute($ethnicity);
1231 my $data = $sth->fetchrow_hashref;
1233 return $data->{'name'};
1234 } # sub fixEthnicity
1240 $dateofbirth,$date = &get_age($date);
1242 this function return the borrowers age with the value of dateofbirth
1247 my ($date, $date_ref) = @_;
1249 if (not defined $date_ref) {
1250 $date_ref = sprintf('%04d-%02d-%02d', Today());
1253 my ($year1, $month1, $day1) = split /-/, $date;
1254 my ($year2, $month2, $day2) = split /-/, $date_ref;
1256 my $age = $year2 - $year1;
1257 if ($month1.$day1 > $month2.$day2) {
1266 =head2 get_institutions
1267 $insitutions = get_institutions();
1269 Just returns a list of all the borrowers of type I, borrownumber and name
1273 sub get_institutions {
1274 my $dbh = C4::Context->dbh();
1277 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1281 while ( my $data = $sth->fetchrow_hashref() ) {
1282 $orgs{ $data->{'borrowernumber'} } = $data;
1287 } # sub get_institutions
1289 =head2 add_member_orgs
1291 add_member_orgs($borrowernumber,$borrowernumbers);
1293 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1298 sub add_member_orgs {
1299 my ( $borrowernumber, $otherborrowers ) = @_;
1300 my $dbh = C4::Context->dbh();
1302 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1303 my $sth = $dbh->prepare($query);
1304 foreach my $bornum (@$otherborrowers) {
1305 $sth->execute( $borrowernumber, $bornum );
1309 } # sub add_member_orgs
1311 END { } # module clean-up code here (global destructor)