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 "la date:".$data{dateenrolled};
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 = ?,password = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
322 WHERE borrowernumber=$data{'borrowernumber'}";
326 ($data{'password'}=md5_base64($data{'password'})) if ($data{'password'} ne '');
327 $query="UPDATE borrowers SET
328 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
329 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
330 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
331 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
332 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
333 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
334 ethnotes = ?,sex = ?,password = ?,flags = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
335 WHERE borrowernumber=$data{'borrowernumber'}";
338 my $sth=$dbh->prepare($query);
340 if ($data{'password'} eq '****'){
342 $data{'cardnumber'},$data{'surname'},
343 $data{'firstname'},$data{'title'},
344 $data{'othernames'},$data{'initials'},
345 $data{'streetnumber'},$data{'streettype'},
346 $data{'address'},$data{'address2'},
347 $data{'city'},$data{'zipcode'},
348 $data{'email'},$data{'phone'},
349 $data{'mobile'},$data{'fax'},
350 $data{'emailpro'},$data{'phonepro'},
351 $data{'B_streetnumber'},$data{'B_streettype'},
352 $data{'B_address'},$data{'B_city'},
353 $data{'B_zipcode'},$data{'B_email'},$data{'B_phone'},
354 $data{'dateofbirth'},$data{'branchcode'},
355 $data{'categorycode'},$data{'dateenrolled'},
356 $data{'dateexpiry'},$data{'gonenoaddress'},
357 $data{'lost'},$data{'debarred'},
358 $data{'contactname'},$data{'contactfirstname'},
359 $data{'contacttitle'},$data{'guarantorid'},
360 $data{'borrowernotes'},$data{'relationship'},
361 $data{'ethnicity'},$data{'ethnotes'},
362 $data{'sex'},$data{'password'},
363 $data{'flags'},$data{'userid'},
364 $data{'opacnote'},$data{'contactnote'},
365 $data{'sort1'},$data{'sort2'}
370 $data{'cardnumber'},$data{'surname'},
371 $data{'firstname'},$data{'title'},
372 $data{'othernames'},$data{'initials'},
373 $data{'streetnumber'},$data{'streettype'},
374 $data{'address'},$data{'address2'},
375 $data{'city'},$data{'zipcode'},
376 $data{'email'},$data{'phone'},
377 $data{'mobile'},$data{'fax'},
378 $data{'emailpro'},$data{'phonepro'},
379 $data{'B_streetnumber'},$data{'B_streettype'},
380 $data{'B_address'},$data{'B_city'},
381 $data{'B_zipcode'},$data{'B_email'},$data{'B_phone'},
382 $data{'dateofbirth'},$data{'branchcode'},
383 $data{'categorycode'},$data{'dateenrolled'},
384 $data{'dateexpiry'},$data{'gonenoaddress'},
385 $data{'lost'},$data{'debarred'},
386 $data{'contactname'},$data{'contactfirstname'},
387 $data{'contacttitle'},$data{'guarantorid'},
388 $data{'borrowernotes'},$data{'relationship'},
389 $data{'ethnicity'},$data{'ethnotes'},
390 $data{'sex'},$data{'password'},
391 $data{'flags'},$data{'userid'},
392 $data{'opacnote'},$data{'contactnote'},
393 $data{'sort1'},$data{'sort2'}
399 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
400 # so when we update information for an adult we should check for guarantees and update the relevant part
401 # of their records, ie addresses and phone numbers
402 my ($category_type,undef)=getcategorytype($data{'category_type'});
403 if ($category_type eq 'A' ){
405 # is adult check guarantees;
406 updateguarantees(%data);
416 my $dbh = C4::Context->dbh;
417 $data{'userid'} = '' unless $data{'password'};
418 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
419 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
420 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
421 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
423 "insert into borrowers set cardnumber="
424 . $dbh->quote( $data{'cardnumber'} )
426 . $dbh->quote( $data{'surname'} )
428 . $dbh->quote( $data{'firstname'} )
430 . $dbh->quote( $data{'title'} )
432 . $dbh->quote( $data{'othernames'} )
434 . $dbh->quote( $data{'initials'} )
436 . $dbh->quote( $data{'streetnumber'} )
438 . $dbh->quote( $data{'streettype'} )
440 . $dbh->quote( $data{'address'} )
442 . $dbh->quote( $data{'address2'} )
444 . $dbh->quote( $data{'zipcode'} )
446 . $dbh->quote( $data{'city'} )
448 . $dbh->quote( $data{'phone'} )
450 . $dbh->quote( $data{'email'} )
452 . $dbh->quote( $data{'mobile'} )
454 . $dbh->quote( $data{'phonepro'} )
456 . $dbh->quote( $data{'opacnote'} )
458 . $dbh->quote( $data{'guarantorid'} )
460 . $dbh->quote( $data{'dateofbirth'} )
462 . $dbh->quote( $data{'branchcode'} )
464 . $dbh->quote( $data{'categorycode'} )
466 . $dbh->quote( $data{'dateenrolled'} )
468 . $dbh->quote( $data{'contactname'} )
470 . $dbh->quote( $data{'borrowernotes'} )
472 . $dbh->quote( $data{'dateexpiry'} )
474 . $dbh->quote( $data{'contactnote'} )
476 . $dbh->quote( $data{'b_address'} )
478 . $dbh->quote( $data{'b_zipcode'} )
480 . $dbh->quote( $data{'b_city'} )
482 . $dbh->quote( $data{'b_phone'} )
484 . $dbh->quote( $data{'b_email'}, )
486 . $dbh->quote( $data{'password'} )
488 . $dbh->quote( $data{'userid'} )
490 . $dbh->quote( $data{'sort1'} )
492 . $dbh->quote( $data{'sort2'} )
494 . $dbh->quote( $data{'contacttitle'} )
496 . $dbh->quote( $data{'emailpro'} )
497 . ",contactfirstname="
498 . $dbh->quote( $data{'contactfirstname'} )
500 . $dbh->quote( $data{'sex'} )
502 . $dbh->quote( $data{'fax'} )
504 . $dbh->quote( $data{'flags'} )
506 . $dbh->quote( $data{'relationship'} )
508 . $dbh->quote( $data{'b_streetnumber'})
510 . $dbh->quote( $data{'b_streettype'})
512 . $dbh->quote( $data{'gonenoaddress'})
514 . $dbh->quote( $data{'lost'})
516 . $dbh->quote( $data{'debarred'})
518 . $dbh->quote( $data{'ethnicity'})
520 . $dbh->quote( $data{'ethnotes'});
522 my $sth = $dbh->prepare($query);
525 $data{'borrowerid'} = $dbh->{'mysql_insertid'};
526 return $data{'borrowerid'};
530 my ( $uid, $member, $digest ) = @_;
531 my $dbh = C4::Context->dbh;
533 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
534 #Then we need to tell the user and have them create a new one.
537 "select * from borrowers where userid=? and borrowernumber != ?");
538 $sth->execute( $uid, $member );
539 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
544 #Everything is good so we can update the information.
547 "update borrowers set userid=?, password=? where borrowernumber=?");
548 $sth->execute( $uid, $digest, $member );
553 sub getmemberfromuserid {
555 my $dbh = C4::Context->dbh;
556 my $sth = $dbh->prepare("select * from borrowers where userid=?");
557 $sth->execute($userid);
558 return $sth->fetchrow_hashref;
561 sub updateguarantees {
563 my $dbh = C4::Context->dbh;
564 my ( $count, $guarantees ) = findguarantees( $data{'borrowernumber'} );
565 for ( my $i = 0 ; $i < $count ; $i++ ) {
568 # It looks like the $i is only being returned to handle walking through
569 # the array, which is probably better done as a foreach loop.
572 "update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
573 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
574 ,streetaddress='$data{'address'}'
575 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
576 my $sth3 = $dbh->prepare($guaquery);
581 ################################################################################
583 =item fixup_cardnumber
585 Warning: The caller is responsible for locking the members table in write
586 mode, to avoid database corruption.
590 use vars qw( @weightings );
591 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
593 sub fixup_cardnumber ($) {
594 my ($cardnumber) = @_;
595 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
596 $autonumber_members = 0 unless defined $autonumber_members;
598 # Find out whether member numbers should be generated
599 # automatically. Should be either "1" or something else.
600 # Defaults to "0", which is interpreted as "no".
602 # if ($cardnumber !~ /\S/ && $autonumber_members) {
603 if ($autonumber_members) {
604 my $dbh = C4::Context->dbh;
605 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
607 # if checkdigit is selected, calculate katipo-style cardnumber.
608 # otherwise, just use the max()
609 # purpose: generate checksum'd member numbers.
610 # We'll assume we just got the max value of digits 2-8 of member #'s
611 # from the database and our job is to increment that by one,
612 # determine the 1st and 9th digits and return the full string.
615 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
619 my $data = $sth->fetchrow_hashref;
620 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
622 if ( !$cardnumber ) { # If DB has no values,
623 $cardnumber = 1000000; # start at 1000000
630 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
632 # read weightings, left to right, 1 char at a time
633 my $temp1 = $weightings[$i];
635 # sequence left to right, 1 char at a time
636 my $temp2 = substr( $cardnumber, $i, 1 );
638 # mult each char 1-7 by its corresponding weighting
639 $sum += $temp1 * $temp2;
642 my $rem = ( $sum % 11 );
643 $rem = 'X' if $rem == 10;
645 $cardnumber = "V$cardnumber$rem";
649 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
650 # better. I'll leave the original in in case it needs to be changed for you
653 "select max(cast(cardnumber as signed)) from borrowers");
655 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
659 my ($result) = $sth->fetchrow;
661 $cardnumber = $result + 1;
669 my $dbh = C4::Context->dbh;
670 my $sth = $dbh->prepare(
671 "select cardnumber,borrowernumber from borrowers where
674 $sth->execute($bornum);
677 while ( my $data = $sth->fetchrow_hashref ) {
682 return ( $i, \@dat );
687 $guarantor = &findguarantor($borrower_no);
688 $guarantor_cardno = $guarantor->{"cardnumber"};
689 $guarantor_surname = $guarantor->{"surname"};
692 C<&findguarantor> takes a borrower number (presumably that of a child
693 patron), finds the guarantor for C<$borrower_no> (the child's parent),
694 and returns the record for the guarantor.
696 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
697 from the C<borrowers> database table;
704 my $dbh = C4::Context->dbh;
705 my $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
706 $sth->execute($bornum);
707 my $data = $sth->fetchrow_hashref;
712 =item GuarantornameSearch
714 ($count, $borrowers) = &GuarantornameSearch($env, $searchstring, $type);
716 Looks up guarantor by name.
720 BUGFIX 499: C<$type> is now used to determine type of search.
721 if $type is "simple", search is performed on the first letter of the
724 C<$searchstring> is a space-separated list of search terms. Each term
725 must match the beginning a borrower's surname, first name, or other
728 C<&GuarantornameSearch> returns a two-element list. C<$borrowers> is a
729 reference-to-array; each element is a reference-to-hash, whose keys
730 are the fields of the C<borrowers> table in the Koha database.
731 C<$count> is the number of elements in C<$borrowers>.
733 return all info from guarantor =>only category_type A
738 #used by member enquiries from the intranet
739 #called by guarantor_search.pl
740 sub GuarantornameSearch {
741 my ( $env, $searchstring, $orderby, $type ) = @_;
742 my $dbh = C4::Context->dbh;
748 if ( $type eq "simple" ) # simple search for one letter only
751 "Select * from borrowers,categories where borrowers.categorycode=categories.categorycode and category_type='A' and surname like ? order by $orderby";
752 @bind = ("$searchstring%");
754 else # advanced search looking in surname, firstname and othernames
756 @data = split( ' ', $searchstring );
758 $query = "Select * from borrowers,categories
759 where ((surname like ? or surname like ?
760 or firstname like ? or firstname like ?
761 or othernames like ? or othernames like ?) and borrowers.categorycode=categories.categorycode and category_type='A'
764 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
765 "$data[0]%", "% $data[0]%"
767 for ( my $i = 1 ; $i < $count ; $i++ ) {
768 $query = $query . " and (" . " surname like ? or surname like ?
769 or firstname like ? or firstname like ?
770 or othernames like ? or othernames like ?)";
772 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
773 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
777 $query = $query . ") or cardnumber like ?
779 push( @bind, $searchstring );
784 my $sth = $dbh->prepare($query);
785 $sth->execute(@bind);
787 my $cnt = $sth->rows;
788 while ( my $data = $sth->fetchrow_hashref ) {
789 push( @results, $data );
794 return ( $cnt, \@results );
797 =item NewBorrowerNumber
799 $num = &NewBorrowerNumber();
801 Allocates a new, unused borrower number, and returns it.
806 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
807 # Pick one and stick with it. Preferably use the other one. This function
808 # doesn't belong in C4::Search.
809 sub NewBorrowerNumber {
810 my $dbh = C4::Context->dbh;
811 my $sth = $dbh->prepare("Select max(borrowernumber) from borrowers");
813 my $data = $sth->fetchrow_hashref;
815 $data->{'max(borrowernumber)'}++;
816 return ( $data->{'max(borrowernumber)'} );
821 ($count, $issues) = &borrissues($borrowernumber);
823 Looks up what the patron with the given borrowernumber has borrowed.
825 C<&borrissues> returns a two-element array. C<$issues> is a
826 reference-to-array, where each element is a reference-to-hash; the
827 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
828 in the Koha database. C<$count> is the number of elements in
836 my $dbh = C4::Context->dbh;
837 my $sth = $dbh->prepare(
838 "Select * from issues,biblio,items where borrowernumber=?
839 and items.itemnumber=issues.itemnumber
840 and items.biblionumber=biblio.biblionumber
841 and issues.returndate is NULL order by date_due"
843 $sth->execute($bornum);
845 while ( my $data = $sth->fetchrow_hashref ) {
849 return ( scalar(@result), \@result );
854 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
856 Looks up what the patron with the given borrowernumber has borrowed,
857 and sorts the results.
859 C<$sortkey> is the name of a field on which to sort the results. This
860 should be the name of a field in the C<issues>, C<biblio>,
861 C<biblioitems>, or C<items> table in the Koha database.
863 C<$limit> is the maximum number of results to return.
865 C<&allissues> returns a two-element array. C<$issues> is a
866 reference-to-array, where each element is a reference-to-hash; the
867 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
868 C<items> tables of the Koha database. C<$count> is the number of
869 elements in C<$issues>
875 my ( $bornum, $order, $limit ) = @_;
877 #FIXME: sanity-check order and limit
878 my $dbh = C4::Context->dbh;
879 my $query = "Select * from issues,biblio,items,biblioitems
880 where borrowernumber=? and
881 items.biblioitemnumber=biblioitems.biblioitemnumber and
882 items.itemnumber=issues.itemnumber and
883 items.biblionumber=biblio.biblionumber order by $order";
885 $query .= " limit $limit";
889 my $sth = $dbh->prepare($query);
890 $sth->execute($bornum);
893 while ( my $data = $sth->fetchrow_hashref ) {
898 return ( $i, \@result );
901 =head2 getboracctrecord
903 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
905 Looks up accounting data for the patron with the given borrowernumber.
909 (FIXME - I'm not at all sure what this is about.)
911 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
912 reference-to-array, where each element is a reference-to-hash; the
913 keys are the fields of the C<accountlines> table in the Koha database.
914 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
915 total amount outstanding for all of the account lines.
920 sub getboracctrecord {
921 my ( $env, $params ) = @_;
922 my $dbh = C4::Context->dbh;
925 my $sth = $dbh->prepare(
926 "Select * from accountlines where
927 borrowernumber=? order by date desc,timestamp desc"
931 $sth->execute( $params->{'borrowernumber'} );
933 while ( my $data = $sth->fetchrow_hashref ) {
935 #FIXME before reinstating: insecure?
936 # if ($data->{'itemnumber'} ne ''){
937 # $query="Select * from items,biblio where items.itemnumber=
938 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
939 # my $sth2=$dbh->prepare($query);
941 # my $data2=$sth2->fetchrow_hashref;
945 $acctlines[$numlines] = $data;
947 $total += $data->{'amountoutstanding'};
950 return ( $numlines, \@acctlines, $total );
953 =head2 checkuniquemember (OUEST-PROVENCE)
955 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
957 Checks that a member exists or not in the database.
959 C<&result> is 1 (=exist) or 0 (=does not exist)
960 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
961 C<&surname> is the surname
962 C<&categorycode> is from categorycode table
963 C<&firstname> is the firstname (only if collectivity=0)
964 C<&dateofbirth> is the date of birth (only if collectivity=0)
968 sub checkuniquemember {
969 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
970 my $dbh = C4::Context->dbh;
974 # $request="select count(*) from borrowers where surname=? and categorycode=?";
976 "select borrowernumber,categorycode from borrowers where surname=? ";
980 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
982 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
984 my $sth = $dbh->prepare($request);
986 $sth->execute( uc($surname) );
989 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
991 my @data = $sth->fetchrow;
994 return $data[0], $data[1];
1004 =head2 getzipnamecity (OUEST-PROVENCE)
1006 take all info from table city for the fields city and zip
1007 check for the name and the zip code of the city selected
1011 sub getzipnamecity {
1013 my $dbh = C4::Context->dbh;
1016 "select city_name,city_zipcode from cities where cityid=? ");
1017 $sth->execute($cityid);
1018 my @data = $sth->fetchrow;
1019 return $data[0], $data[1];
1022 =head2 updatechildguarantor (OUEST-PROVENCE)
1024 check for title,firstname,surname,adress,zip code and city from guarantor to
1031 sub getguarantordata {
1032 my ($borrowerid) = @_;
1033 my $dbh = C4::Context->dbh;
1036 "Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro from borrowers where borrowernumber =? "
1038 $sth->execute($borrowerid);
1039 my $guarantor_data = $sth->fetchrow_hashref;
1041 return $guarantor_data;
1044 =head2 getdcity (OUEST-PROVENCE)
1045 recover cityid with city_name condition
1049 my ($city_name) = @_;
1050 my $dbh = C4::Context->dbh;
1051 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1052 $sth->execute($city_name);
1053 my $data = $sth->fetchrow;
1057 =head2 getcategorytype (OUEST-PROVENCE)
1059 check for the category_type with categorycode
1060 and return the category_type
1064 sub getcategorytype {
1065 my ($categorycode) = @_;
1066 my $dbh = C4::Context->dbh;
1069 "Select category_type,description from categories where categorycode=? "
1071 $sth->execute($categorycode);
1072 my ( $category_type, $description ) = $sth->fetchrow;
1073 return $category_type, $description;
1076 sub calcexpirydate {
1077 my ( $categorycode, $dateenrolled ) = @_;
1078 my $dbh = C4::Context->dbh;
1081 "select enrolmentperiod from categories where categorycode=?");
1082 $sth->execute($categorycode);
1083 my ($enrolmentperiod) = $sth->fetchrow;
1084 $enrolmentperiod = 12 unless ($enrolmentperiod);
1085 return format_date_in_iso(
1086 &DateCalc( $dateenrolled, "$enrolmentperiod months" ) );
1089 =head2 checkuserpassword (OUEST-PROVENCE)
1091 check for the password and login are not used
1092 return the number of record
1093 0=> NOT USED 1=> USED
1097 sub checkuserpassword {
1098 my ( $borrowerid, $userid, $password ) = @_;
1099 $password = md5_base64($password);
1100 my $dbh = C4::Context->dbh;
1103 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1105 $sth->execute( $borrowerid, $userid, $password );
1106 my $number_rows = $sth->fetchrow;
1107 return $number_rows;
1111 =head2 borrowercategories
1113 ($codes_arrayref, $labels_hashref) = &borrowercategories();
1115 Looks up the different types of borrowers in the database. Returns two
1116 elements: a reference-to-array, which lists the borrower category
1117 codes, and a reference-to-hash, which maps the borrower category codes
1118 to category descriptions.
1123 sub borrowercategories {
1124 my ( $category_type, $action ) = @_;
1125 my $dbh = C4::Context->dbh;
1128 "Select categorycode,description from categories where category_type=? order by categorycode";
1129 my $sth = $dbh->prepare($request);
1130 $sth->execute($category_type);
1134 while ( my $data = $sth->fetchrow_hashref ) {
1135 push @codes, $data->{'categorycode'};
1136 $labels{ $data->{'categorycode'} } = $data->{'description'};
1139 return ( \@codes, \%labels );
1142 =head2 getborrowercategory
1144 $description = &getborrowercategory($categorycode);
1146 Given the borrower's category code, the function returns the corresponding
1147 description for a comprehensive information display.
1151 sub getborrowercategory {
1153 my $dbh = C4::Context->dbh;
1156 "SELECT description FROM categories WHERE categorycode = ?");
1157 $sth->execute($catcode);
1158 my $description = $sth->fetchrow();
1160 return $description;
1161 } # sub getborrowercategory
1163 =head2 ethnicitycategories
1165 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1167 Looks up the different ethnic types in the database. Returns two
1168 elements: a reference-to-array, which lists the ethnicity codes, and a
1169 reference-to-hash, which maps the ethnicity codes to ethnicity
1176 sub ethnicitycategories {
1177 my $dbh = C4::Context->dbh;
1178 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1182 while ( my $data = $sth->fetchrow_hashref ) {
1183 push @codes, $data->{'code'};
1184 $labels{ $data->{'code'} } = $data->{'name'};
1187 return ( \@codes, \%labels );
1192 $ethn_name = &fixEthnicity($ethn_code);
1194 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1195 corresponding descriptive name from the C<ethnicity> table in the
1196 Koha database ("European" or "Pacific Islander").
1202 sub fixEthnicity($) {
1204 my $ethnicity = shift;
1205 my $dbh = C4::Context->dbh;
1206 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1207 $sth->execute($ethnicity);
1208 my $data = $sth->fetchrow_hashref;
1210 return $data->{'name'};
1211 } # sub fixEthnicity
1217 $dateofbirth,$date = &get_age($date);
1219 this function return the borrowers age with the value of dateofbirth
1224 my ($date, $date_ref) = @_;
1226 if (not defined $date_ref) {
1227 $date_ref = sprintf('%04d-%02d-%02d', Today());
1230 my ($year1, $month1, $day1) = split /-/, $date;
1231 my ($year2, $month2, $day2) = split /-/, $date_ref;
1233 my $age = $year2 - $year1;
1234 if ($month1.$day1 > $month2.$day2) {
1243 =head2 get_institutions
1244 $insitutions = get_institutions();
1246 Just returns a list of all the borrowers of type I, borrownumber and name
1250 sub get_institutions {
1251 my $dbh = C4::Context->dbh();
1254 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1258 while ( my $data = $sth->fetchrow_hashref() ) {
1259 $orgs{ $data->{'borrowernumber'} } = $data;
1264 } # sub get_institutions
1266 =head2 add_member_orgs
1268 add_member_orgs($borrowernumber,$borrowernumbers);
1270 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1275 sub add_member_orgs {
1276 my ( $borrowernumber, $otherborrowers ) = @_;
1277 my $dbh = C4::Context->dbh();
1279 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1280 my $sth = $dbh->prepare($query);
1281 foreach my $bornum (@$otherborrowers) {
1282 $sth->execute( $borrowernumber, $bornum );
1286 } # sub add_member_orgs