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);
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
33 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
37 C4::Members - Perl Module containing convenience functions for member handling
45 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
70 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
72 Looks up patrons (borrowers) by name.
76 BUGFIX 499: C<$type> is now used to determine type of search.
77 if $type is "simple", search is performed on the first letter of the
80 C<$searchstring> is a space-separated list of search terms. Each term
81 must match the beginning a borrower's surname, first name, or other
84 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
85 reference-to-array; each element is a reference-to-hash, whose keys
86 are the fields of the C<borrowers> table in the Koha database.
87 C<$count> is the number of elements in C<$borrowers>.
92 #used by member enquiries from the intranet
95 my ( $env, $searchstring, $orderby, $type ) = @_;
96 my $dbh = C4::Context->dbh;
102 if ( $type eq "simple" ) # simple search for one letter only
105 "Select * from borrowers where surname like ? order by $orderby";
106 @bind = ("$searchstring%");
108 else # advanced search looking in surname, firstname and othernames
110 @data = split( ' ', $searchstring );
112 $query = "Select * from borrowers
113 where ((surname like ? or surname like ?
114 or firstname like ? or firstname like ?
115 or othernames like ? or othernames like ?)
118 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
119 "$data[0]%", "% $data[0]%"
121 for ( my $i = 1 ; $i < $count ; $i++ ) {
122 $query = $query . " and (" . " surname like ? or surname like ?
123 or firstname like ? or firstname like ?
124 or othernames like ? or othernames like ?)";
126 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
127 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
131 $query = $query . ") or cardnumber like ?
133 push( @bind, $searchstring );
138 my $sth = $dbh->prepare($query);
140 # warn "Q $orderby : $query";
141 $sth->execute(@bind);
143 my $cnt = $sth->rows;
144 while ( my $data = $sth->fetchrow_hashref ) {
145 push( @results, $data );
150 return ( $cnt, \@results );
155 $borrower = &getmember($cardnumber, $borrowernumber);
157 Looks up information about a patron (borrower) by either card number
158 or borrower number. If $borrowernumber is specified, C<&borrdata>
159 searches by borrower number; otherwise, it searches by card number.
161 C<&getmember> returns a reference-to-hash whose keys are the fields of
162 the C<borrowers> table in the Koha database.
168 my ( $cardnumber, $bornum ) = @_;
169 $cardnumber = uc $cardnumber;
170 my $dbh = C4::Context->dbh;
172 if ( $bornum eq '' ) {
173 $sth = $dbh->prepare("Select * from borrowers where cardnumber=?");
174 $sth->execute($cardnumber);
177 $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
178 $sth->execute($bornum);
180 my $data = $sth->fetchrow_hashref;
185 else { # try with firstname
188 $dbh->prepare("select * from borrowers where firstname=?");
189 $sth->execute($cardnumber);
190 my $data = $sth->fetchrow_hashref;
200 $borrower = &borrdata($cardnumber, $borrowernumber);
202 Looks up information about a patron (borrower) by either card number
203 or borrower number. If $borrowernumber is specified, C<&borrdata>
204 searches by borrower number; otherwise, it searches by card number.
206 C<&borrdata> returns a reference-to-hash whose keys are the fields of
207 the C<borrowers> table in the Koha database.
213 my ( $cardnumber, $bornum ) = @_;
214 $cardnumber = uc $cardnumber;
215 my $dbh = C4::Context->dbh;
217 if ( $bornum eq '' ) {
220 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?"
222 $sth->execute($cardnumber);
227 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
229 $sth->execute($bornum);
231 my $data = $sth->fetchrow_hashref;
232 warn "DATA" . $data->{category_type};
237 else { # try with firstname
241 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname=?"
243 $sth->execute($cardnumber);
244 my $data = $sth->fetchrow_hashref;
254 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
256 Returns aggregate data about items borrowed by the patron with the
257 given borrowernumber.
261 C<&borrdata2> returns a three-element array. C<$borrowed> is the
262 number of books the patron currently has borrowed. C<$due> is the
263 number of overdue items the patron currently has borrowed. C<$fine> is
264 the total fine currently due by the borrower.
270 my ( $env, $bornum ) = @_;
271 my $dbh = C4::Context->dbh;
272 my $query = "Select count(*) from issues where borrowernumber='$bornum' and
276 my $sth = $dbh->prepare($query);
278 my $data = $sth->fetchrow_hashref;
280 $sth = $dbh->prepare(
281 "Select count(*) from issues where
282 borrowernumber='$bornum' and date_due < now() and returndate is NULL"
285 my $data2 = $sth->fetchrow_hashref;
287 $sth = $dbh->prepare(
288 "Select sum(amountoutstanding) from accountlines where
289 borrowernumber='$bornum'"
292 my $data3 = $sth->fetchrow_hashref;
295 return ( $data2->{'count(*)'}, $data->{'count(*)'},
296 $data3->{'sum(amountoutstanding)'} );
301 my $dbh = C4::Context->dbh;
302 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
303 $data{'expiry'} = format_date_in_iso( $data{'expiry'} );
305 my $query = "UPDATE borrowers SET
306 title=?,dateexpiry=?,cardnumber=?,sex=?,ethnotes=?,address=?,fax=?,
307 firstname=?,contactnote=?,dateofbirth=?,contactname=?,email=?,
308 address2=?,relationship=?,othernames=?,phonepro=?,categorycode=?,
309 city=?,phone=?,borrowernotes=?,B_phone=?,surname=?,initials=?,
310 B_address=?,ethnicity=?,gonenoaddress=?,lost=?,debarred=?,opacnote=?,
311 branchcode=?,zipcode=?,B_zipcode=?,sort1=?,sort2=?
312 WHERE borrowernumber=?";
314 my $sth = $dbh->prepare($query);
316 $data{'title'}, $data{'expiry'},
317 $data{'cardnumber'}, $data{'sex'},
318 $data{'ehtnotes'}, $data{'address'},
319 $data{'fax'}, $data{'firstname'},
320 $data{'contactnote'}, $data{'dateofbirth'},
321 $data{'contactname'}, $data{'email'},
322 $data{'address2'}, $data{'relationship'},
323 $data{'othernames'}, $data{'phonepro'},
324 $data{'categorycode'}, $data{'city'},
326 $data{'borrowernotes'}, $data{'b_phone'},
327 $data{'surname'}, $data{'initials'},
328 $data{'b_address'}, $data{'ethnicity'},
329 $data{'gna'}, $data{'lost'},
330 $data{'debarred'}, $data{'opacnotes'},
331 $data{'branchcode'}, $data{'zipcode'},
332 $data{'b_zipcode'}, $data{'sort1'},
333 $data{'sort2'}, $data{'borrowerid'}
336 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
337 # so when we update information for an adult we should check for guarantees and update the relevant part
338 # of their records, ie addresses and phone numbers
339 if ( $data{'categorycode'} eq 'A' || $data{'categorycode'} eq 'W' ) {
341 # is adult check guarantees;
342 updateguarantees(%data);
348 my $dbh = C4::Context->dbh;
349 $data{'userid'} = '' unless $data{'password'};
350 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
351 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
352 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
353 $data{expiry} = format_date_in_iso( $data{expiry} );
355 "insert into borrowers set cardnumber="
356 . $dbh->quote( $data{'cardnumber'} )
358 . $dbh->quote( $data{'surname'} )
360 . $dbh->quote( $data{'firstname'} )
362 . $dbh->quote( $data{'title'} )
364 . $dbh->quote( $data{'othernames'} )
366 . $dbh->quote( $data{'initials'} )
368 . $dbh->quote( $data{'streetnumber'} )
370 . $dbh->quote( $data{'streettype'} )
372 . $dbh->quote( $data{'address'} )
374 . $dbh->quote( $data{'address2'} )
376 . $dbh->quote( $data{'zipcode'} )
378 . $dbh->quote( $data{'city'} )
380 . $dbh->quote( $data{'phone'} )
382 . $dbh->quote( $data{'email'} )
384 . $dbh->quote( $data{'mobile'} )
386 . $dbh->quote( $data{'phonepro'} )
388 . $dbh->quote( $data{'opacnote'} )
390 . $dbh->quote( $data{'guarantorid'} )
392 . $dbh->quote( $data{'dateofbirth'} )
394 . $dbh->quote( $data{'branchcode'} )
396 . $dbh->quote( $data{'categorycode'} )
398 . $dbh->quote( $data{'dateenrolled'} )
400 . $dbh->quote( $data{'contactname'} )
402 . $dbh->quote( $data{'borrowernotes'} )
404 . $dbh->quote( $data{'dateexpiry'} )
406 . $dbh->quote( $data{'contactnote'} )
408 . $dbh->quote( $data{'b_address'} )
410 . $dbh->quote( $data{'b_zipcode'} )
412 . $dbh->quote( $data{'b_city'} )
414 . $dbh->quote( $data{'b_phone'} )
416 . $dbh->quote( $data{'b_email'}, )
418 . $dbh->quote( $data{'password'} )
420 . $dbh->quote( $data{'userid'} )
422 . $dbh->quote( $data{'sort1'} )
424 . $dbh->quote( $data{'sort2'} )
426 . $dbh->quote( $data{'contacttitle'} )
428 . $dbh->quote( $data{'emailpro'} )
429 . ",contactfirstname="
430 . $dbh->quote( $data{'contactfirstname'} ) . ",sex="
431 . $dbh->quote( $data{'sex'} ) . ",fax="
432 . $dbh->quote( $data{'fax'} )
434 . $dbh->quote( $data{'flags'} )
436 . $dbh->quote( $data{'relationship'} );
437 my $sth = $dbh->prepare($query);
440 $data{'borrowerid'} = $dbh->{'mysql_insertid'};
441 return $data{'borrowerid'};
445 my ( $uid, $member, $digest ) = @_;
446 my $dbh = C4::Context->dbh;
448 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
449 #Then we need to tell the user and have them create a new one.
452 "select * from borrowers where userid=? and borrowernumber != ?");
453 $sth->execute( $uid, $member );
454 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
459 #Everything is good so we can update the information.
462 "update borrowers set userid=?, password=? where borrowernumber=?");
463 $sth->execute( $uid, $digest, $member );
468 sub getmemberfromuserid {
470 my $dbh = C4::Context->dbh;
471 my $sth = $dbh->prepare("select * from borrowers where userid=?");
472 $sth->execute($userid);
473 return $sth->fetchrow_hashref;
476 sub updateguarantees {
478 my $dbh = C4::Context->dbh;
479 my ( $count, $guarantees ) = findguarantees( $data{'borrowernumber'} );
480 for ( my $i = 0 ; $i < $count ; $i++ ) {
483 # It looks like the $i is only being returned to handle walking through
484 # the array, which is probably better done as a foreach loop.
487 "update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
488 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
489 ,streetaddress='$data{'address'}'
490 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
491 my $sth3 = $dbh->prepare($guaquery);
496 ################################################################################
498 =item fixup_cardnumber
500 Warning: The caller is responsible for locking the members table in write
501 mode, to avoid database corruption.
505 use vars qw( @weightings );
506 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
508 sub fixup_cardnumber ($) {
509 my ($cardnumber) = @_;
510 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
511 $autonumber_members = 0 unless defined $autonumber_members;
513 # Find out whether member numbers should be generated
514 # automatically. Should be either "1" or something else.
515 # Defaults to "0", which is interpreted as "no".
517 # if ($cardnumber !~ /\S/ && $autonumber_members) {
518 if ($autonumber_members) {
519 my $dbh = C4::Context->dbh;
520 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
522 # if checkdigit is selected, calculate katipo-style cardnumber.
523 # otherwise, just use the max()
524 # purpose: generate checksum'd member numbers.
525 # We'll assume we just got the max value of digits 2-8 of member #'s
526 # from the database and our job is to increment that by one,
527 # determine the 1st and 9th digits and return the full string.
530 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
534 my $data = $sth->fetchrow_hashref;
535 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
537 if ( !$cardnumber ) { # If DB has no values,
538 $cardnumber = 1000000; # start at 1000000
545 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
547 # read weightings, left to right, 1 char at a time
548 my $temp1 = $weightings[$i];
550 # sequence left to right, 1 char at a time
551 my $temp2 = substr( $cardnumber, $i, 1 );
553 # mult each char 1-7 by its corresponding weighting
554 $sum += $temp1 * $temp2;
557 my $rem = ( $sum % 11 );
558 $rem = 'X' if $rem == 10;
560 $cardnumber = "V$cardnumber$rem";
564 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
565 # better. I'll leave the original in in case it needs to be changed for you
568 "select max(cast(cardnumber as signed)) from borrowers");
570 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
574 my ($result) = $sth->fetchrow;
576 $cardnumber = $result + 1;
584 my $dbh = C4::Context->dbh;
585 my $sth = $dbh->prepare(
586 "select cardnumber,borrowernumber from borrowers where
589 $sth->execute($bornum);
592 while ( my $data = $sth->fetchrow_hashref ) {
597 return ( $i, \@dat );
602 $guarantor = &findguarantor($borrower_no);
603 $guarantor_cardno = $guarantor->{"cardnumber"};
604 $guarantor_surname = $guarantor->{"surname"};
607 C<&findguarantor> takes a borrower number (presumably that of a child
608 patron), finds the guarantor for C<$borrower_no> (the child's parent),
609 and returns the record for the guarantor.
611 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
612 from the C<borrowers> database table;
619 my $dbh = C4::Context->dbh;
620 my $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
621 $sth->execute($bornum);
622 my $data = $sth->fetchrow_hashref;
627 =item GuarantornameSearch
629 ($count, $borrowers) = &GuarantornameSearch($env, $searchstring, $type);
631 Looks up guarantor by name.
635 BUGFIX 499: C<$type> is now used to determine type of search.
636 if $type is "simple", search is performed on the first letter of the
639 C<$searchstring> is a space-separated list of search terms. Each term
640 must match the beginning a borrower's surname, first name, or other
643 C<&GuarantornameSearch> returns a two-element list. C<$borrowers> is a
644 reference-to-array; each element is a reference-to-hash, whose keys
645 are the fields of the C<borrowers> table in the Koha database.
646 C<$count> is the number of elements in C<$borrowers>.
648 return all info from guarantor =>only category_type A
653 #used by member enquiries from the intranet
654 #called by guarantor_search.pl
655 sub GuarantornameSearch {
656 my ( $env, $searchstring, $orderby, $type ) = @_;
657 my $dbh = C4::Context->dbh;
663 if ( $type eq "simple" ) # simple search for one letter only
666 "Select * from borrowers,categories where borrowers.categorycode=categories.categorycode and category_type='A' and surname like ? order by $orderby";
667 @bind = ("$searchstring%");
669 else # advanced search looking in surname, firstname and othernames
671 @data = split( ' ', $searchstring );
673 $query = "Select * from borrowers,categories
674 where ((surname like ? or surname like ?
675 or firstname like ? or firstname like ?
676 or othernames like ? or othernames like ?) and borrowers.categorycode=categories.categorycode and category_type='A'
679 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
680 "$data[0]%", "% $data[0]%"
682 for ( my $i = 1 ; $i < $count ; $i++ ) {
683 $query = $query . " and (" . " surname like ? or surname like ?
684 or firstname like ? or firstname like ?
685 or othernames like ? or othernames like ?)";
687 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
688 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
692 $query = $query . ") or cardnumber like ?
694 push( @bind, $searchstring );
699 my $sth = $dbh->prepare($query);
700 $sth->execute(@bind);
702 my $cnt = $sth->rows;
703 while ( my $data = $sth->fetchrow_hashref ) {
704 push( @results, $data );
709 return ( $cnt, \@results );
712 =item NewBorrowerNumber
714 $num = &NewBorrowerNumber();
716 Allocates a new, unused borrower number, and returns it.
721 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
722 # Pick one and stick with it. Preferably use the other one. This function
723 # doesn't belong in C4::Search.
724 sub NewBorrowerNumber {
725 my $dbh = C4::Context->dbh;
726 my $sth = $dbh->prepare("Select max(borrowernumber) from borrowers");
728 my $data = $sth->fetchrow_hashref;
730 $data->{'max(borrowernumber)'}++;
731 return ( $data->{'max(borrowernumber)'} );
736 ($count, $issues) = &borrissues($borrowernumber);
738 Looks up what the patron with the given borrowernumber has borrowed.
740 C<&borrissues> returns a two-element array. C<$issues> is a
741 reference-to-array, where each element is a reference-to-hash; the
742 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
743 in the Koha database. C<$count> is the number of elements in
751 my $dbh = C4::Context->dbh;
752 my $sth = $dbh->prepare(
753 "Select * from issues,biblio,items where borrowernumber=?
754 and items.itemnumber=issues.itemnumber
755 and items.biblionumber=biblio.biblionumber
756 and issues.returndate is NULL order by date_due"
758 $sth->execute($bornum);
760 while ( my $data = $sth->fetchrow_hashref ) {
764 return ( scalar(@result), \@result );
769 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
771 Looks up what the patron with the given borrowernumber has borrowed,
772 and sorts the results.
774 C<$sortkey> is the name of a field on which to sort the results. This
775 should be the name of a field in the C<issues>, C<biblio>,
776 C<biblioitems>, or C<items> table in the Koha database.
778 C<$limit> is the maximum number of results to return.
780 C<&allissues> returns a two-element array. C<$issues> is a
781 reference-to-array, where each element is a reference-to-hash; the
782 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
783 C<items> tables of the Koha database. C<$count> is the number of
784 elements in C<$issues>
790 my ( $bornum, $order, $limit ) = @_;
792 #FIXME: sanity-check order and limit
793 my $dbh = C4::Context->dbh;
794 my $query = "Select * from issues,biblio,items,biblioitems
795 where borrowernumber=? and
796 items.biblioitemnumber=biblioitems.biblioitemnumber and
797 items.itemnumber=issues.itemnumber and
798 items.biblionumber=biblio.biblionumber order by $order";
800 $query .= " limit $limit";
804 my $sth = $dbh->prepare($query);
805 $sth->execute($bornum);
808 while ( my $data = $sth->fetchrow_hashref ) {
813 return ( $i, \@result );
816 =head2 getboracctrecord
818 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
820 Looks up accounting data for the patron with the given borrowernumber.
824 (FIXME - I'm not at all sure what this is about.)
826 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
827 reference-to-array, where each element is a reference-to-hash; the
828 keys are the fields of the C<accountlines> table in the Koha database.
829 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
830 total amount outstanding for all of the account lines.
835 sub getboracctrecord {
836 my ( $env, $params ) = @_;
837 my $dbh = C4::Context->dbh;
840 my $sth = $dbh->prepare(
841 "Select * from accountlines where
842 borrowernumber=? order by date desc,timestamp desc"
846 $sth->execute( $params->{'borrowernumber'} );
848 while ( my $data = $sth->fetchrow_hashref ) {
850 #FIXME before reinstating: insecure?
851 # if ($data->{'itemnumber'} ne ''){
852 # $query="Select * from items,biblio where items.itemnumber=
853 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
854 # my $sth2=$dbh->prepare($query);
856 # my $data2=$sth2->fetchrow_hashref;
860 $acctlines[$numlines] = $data;
862 $total += $data->{'amountoutstanding'};
865 return ( $numlines, \@acctlines, $total );
868 =head2 checkuniquemember (OUEST-PROVENCE)
870 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
872 Checks that a member exists or not in the database.
874 C<&result> is 1 (=exist) or 0 (=does not exist)
875 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
876 C<&surname> is the surname
877 C<&categorycode> is from categorycode table
878 C<&firstname> is the firstname (only if collectivity=0)
879 C<&dateofbirth> is the date of birth (only if collectivity=0)
883 sub checkuniquemember {
884 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
885 my $dbh = C4::Context->dbh;
889 # $request="select count(*) from borrowers where surname=? and categorycode=?";
891 "select borrowernumber,categorycode from borrowers where surname=? ";
895 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
897 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
899 my $sth = $dbh->prepare($request);
901 $sth->execute( uc($surname) );
904 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
906 my @data = $sth->fetchrow;
909 return $data[0], $data[1];
919 =head2 getzipnamecity (OUEST-PROVENCE)
921 take all info from table city for the fields city and zip
922 check for the name and the zip code of the city selected
928 my $dbh = C4::Context->dbh;
931 "select city_name,city_zipcode from cities where cityid=? ");
932 $sth->execute($cityid);
933 my @data = $sth->fetchrow;
934 return $data[0], $data[1];
937 =head2 updatechildguarantor (OUEST-PROVENCE)
939 check for title,firstname,surname,adress,zip code and city from guarantor to
946 sub getguarantordata {
947 my ($borrowerid) = @_;
948 my $dbh = C4::Context->dbh;
951 "Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro from borrowers where borrowernumber =? "
953 $sth->execute($borrowerid);
954 my $guarantor_data = $sth->fetchrow_hashref;
956 return $guarantor_data;
959 =head2 getdcity (OUEST-PROVENCE)
960 recover cityid with city_name condition
964 my ($city_name) = @_;
965 my $dbh = C4::Context->dbh;
966 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
967 $sth->execute($city_name);
968 my $data = $sth->fetchrow;
972 =head2 getcategorytype (OUEST-PROVENCE)
974 check for the category_type with categorycode
975 and return the category_type
979 sub getcategorytype {
980 my ($categorycode) = @_;
981 my $dbh = C4::Context->dbh;
984 "Select category_type,description from categories where categorycode=? "
986 $sth->execute($categorycode);
987 my ( $category_type, $description ) = $sth->fetchrow;
988 return $category_type, $description;
992 my ( $categorycode, $dateenrolled ) = @_;
993 my $dbh = C4::Context->dbh;
996 "select enrolmentperiod from categories where categorycode=?");
997 $sth->execute($categorycode);
998 my ($enrolmentperiod) = $sth->fetchrow;
999 $enrolmentperiod = 12 unless ($enrolmentperiod);
1000 return format_date_in_iso(
1001 &DateCalc( $dateenrolled, "$enrolmentperiod months" ) );
1004 =head2 checkuserpassword (OUEST-PROVENCE)
1006 check for the password and login are not used
1007 return the number of record
1008 0=> NOT USED 1=> USED
1012 sub checkuserpassword {
1013 my ( $borrowerid, $userid, $password ) = @_;
1014 $password = md5_base64($password);
1015 my $dbh = C4::Context->dbh;
1018 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1020 $sth->execute( $borrowerid, $userid, $password );
1021 my $number_rows = $sth->fetchrow;
1022 return $number_rows;
1026 =head2 borrowercategories
1028 ($codes_arrayref, $labels_hashref) = &borrowercategories();
1030 Looks up the different types of borrowers in the database. Returns two
1031 elements: a reference-to-array, which lists the borrower category
1032 codes, and a reference-to-hash, which maps the borrower category codes
1033 to category descriptions.
1038 sub borrowercategories {
1039 my ( $category_type, $action ) = @_;
1040 my $dbh = C4::Context->dbh;
1043 "Select categorycode,description from categories where category_type=? order by categorycode";
1044 my $sth = $dbh->prepare($request);
1045 $sth->execute($category_type);
1049 while ( my $data = $sth->fetchrow_hashref ) {
1050 push @codes, $data->{'categorycode'};
1051 $labels{ $data->{'categorycode'} } = $data->{'description'};
1054 return ( \@codes, \%labels );
1057 =head2 getborrowercategory
1059 $description = &getborrowercategory($categorycode);
1061 Given the borrower's category code, the function returns the corresponding
1062 description for a comprehensive information display.
1066 sub getborrowercategory {
1068 my $dbh = C4::Context->dbh;
1071 "SELECT description FROM categories WHERE categorycode = ?");
1072 $sth->execute($catcode);
1073 my $description = $sth->fetchrow();
1075 return $description;
1076 } # sub getborrowercategory
1078 =head2 ethnicitycategories
1080 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1082 Looks up the different ethnic types in the database. Returns two
1083 elements: a reference-to-array, which lists the ethnicity codes, and a
1084 reference-to-hash, which maps the ethnicity codes to ethnicity
1091 sub ethnicitycategories {
1092 my $dbh = C4::Context->dbh;
1093 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1097 while ( my $data = $sth->fetchrow_hashref ) {
1098 push @codes, $data->{'code'};
1099 $labels{ $data->{'code'} } = $data->{'name'};
1102 return ( \@codes, \%labels );
1107 $ethn_name = &fixEthnicity($ethn_code);
1109 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1110 corresponding descriptive name from the C<ethnicity> table in the
1111 Koha database ("European" or "Pacific Islander").
1117 sub fixEthnicity($) {
1119 my $ethnicity = shift;
1120 my $dbh = C4::Context->dbh;
1121 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1122 $sth->execute($ethnicity);
1123 my $data = $sth->fetchrow_hashref;
1125 return $data->{'name'};
1126 } # sub fixEthnicity
1128 =head2 get_institutions
1130 $insitutions = get_institutions();
1132 Just returns a list of all the borrowers of type I, borrownumber and name
1138 sub get_institutions {
1139 my $dbh = C4::Context->dbh();
1142 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1146 while ( my $data = $sth->fetchrow_hashref() ) {
1147 $orgs{ $data->{'borrowernumber'} } = $data;
1152 } # sub get_institutions
1154 =head2 add_member_orgs
1156 add_member_orgs($borrowernumber,$borrowernumbers);
1158 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1163 sub add_member_orgs {
1164 my ( $borrowernumber, $otherborrowers ) = @_;
1165 my $dbh = C4::Context->dbh();
1167 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1168 my $sth = $dbh->prepare($query);
1169 foreach my $bornum (@$otherborrowers) {
1170 $sth->execute( $borrowernumber, $bornum );
1174 } # sub add_member_orgs