3 # Copyright 2000-2003 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
26 use Digest::MD5 qw(md5_base64);
27 use Date::Calc qw/Today Add_Delta_YM/;
28 use C4::Log; # logaction
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
66 &GetBorrowerIssuesAndFines
79 &GetBorrowerAcctRecord
84 &GetBorNotifyAcctRecord
85 &GetMembeReregistration
89 &GetBorrowersWhoHaveNotBorrowedSince
90 &GetBorrowersWhoHaveNeverBorrowed
91 &GetBorrowersWithIssuesHistoryOlderThan
92 &GetBorrowersFromSurname
123 ($count, $borrowers) = &SearchBorrower($searchstring, $type);
125 Looks up patrons (borrowers) by name.
127 BUGFIX 499: C<$type> is now used to determine type of search.
128 if $type is "simple", search is performed on the first letter of the
131 C<$searchstring> is a space-separated list of search terms. Each term
132 must match the beginning a borrower's surname, first name, or other
135 C<&SearchBorrower> returns a two-element list. C<$borrowers> is a
136 reference-to-array; each element is a reference-to-hash, whose keys
137 are the fields of the C<borrowers> table in the Koha database.
138 C<$count> is the number of elements in C<$borrowers>.
143 #used by member enquiries from the intranet
146 my ($searchstring, $orderby, $type,$category_type ) = @_;
147 my $dbh = C4::Context->dbh;
153 if ( $type eq "simple" ) # simple search for one letter only
156 "SELECT * FROM borrowers
157 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode ".
158 ($category_type?" AND category_type = ".$dbh->quote($category_type):"").
159 " WHERE surname LIKE ? ORDER BY $orderby";
160 @bind = ("$searchstring%");
162 else # advanced search looking in surname, firstname and othernames
164 @data = split( ' ', $searchstring );
166 $query = "SELECT * FROM borrowers
167 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
168 WHERE ((surname LIKE ? OR surname LIKE ?
169 OR firstname LIKE ? OR firstname LIKE ?
170 OR othernames LIKE ? OR othernames LIKE ?)
172 ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
174 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
175 "$data[0]%", "% $data[0]%"
177 for ( my $i = 1 ; $i < $count ; $i++ ) {
178 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
179 OR firstname LIKE ? OR firstname LIKE ?
180 OR othernames LIKE ? OR othernames LIKE ?)";
182 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
183 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
187 $query = $query . ") OR cardnumber LIKE ?
189 push( @bind, $searchstring );
194 my $sth = $dbh->prepare($query);
196 # warn "Q $orderby : $query";
197 $sth->execute(@bind);
199 my $cnt = $sth->rows;
200 while ( my $data = $sth->fetchrow_hashref ) {
201 push( @results, $data );
206 return ( $cnt, \@results );
209 =head2 GetMemberDetails
211 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
213 Looks up a patron and returns information about him or her. If
214 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
215 up the borrower by number; otherwise, it looks up the borrower by card
218 C<$borrower> is a reference-to-hash whose keys are the fields of the
219 borrowers table in the Koha database. In addition,
220 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
221 about the patron. Its keys act as flags :
223 if $borrower->{flags}->{LOST} {
224 # Patron's card was reported lost
227 Each flag has a C<message> key, giving a human-readable explanation of
228 the flag. If the state of a flag means that the patron should not be
229 allowed to borrow any more books, then it will have a C<noissues> key
232 The possible flags are:
238 =item Shows the patron's credit or debt, if any.
246 =item (Gone, no address.) Set if the patron has left without giving a
255 =item Set if the patron's card has been reported as lost.
263 =item Set if the patron has been debarred.
271 =item Any additional notes about the patron.
279 =item Set if the patron has overdue items. This flag has several keys:
281 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
282 overdue items. Its elements are references-to-hash, each describing an
283 overdue item. The keys are selected fields from the issues, biblio,
284 biblioitems, and items tables of the Koha database.
286 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
287 the overdue items, one per line.
295 =item Set if any items that the patron has reserved are available.
297 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
298 available items. Each element is a reference-to-hash whose keys are
299 fields from the reserves table of the Koha database.
305 sub GetMemberDetails {
306 my ( $borrowernumber, $cardnumber ) = @_;
307 my $dbh = C4::Context->dbh;
310 if ($borrowernumber) {
311 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
312 $sth->execute($borrowernumber);
314 elsif ($cardnumber) {
315 $sth = $dbh->prepare("select * from borrowers where cardnumber=?");
316 $sth->execute($cardnumber);
321 my $borrower = $sth->fetchrow_hashref;
322 my ($amount) = GetBorrowerAcctRecord( $borrowernumber);
323 $borrower->{'amountoutstanding'} = $amount;
324 my $flags = patronflags( $borrower, $dbh );
327 $sth = $dbh->prepare("select bit,flag from userflags");
329 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
330 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
331 $accessflagshash->{$flag} = 1;
335 $borrower->{'flags'} = $flags;
336 $borrower->{'authflags'} = $accessflagshash;
338 # find out how long the membership lasts
341 "select enrolmentperiod from categories where categorycode = ?");
342 $sth->execute( $borrower->{'categorycode'} );
343 my $enrolment = $sth->fetchrow;
344 $borrower->{'enrolmentperiod'} = $enrolment;
345 return ($borrower); #, $flags, $accessflagshash);
352 NOTE!: If you change this function, be sure to update the POD for
355 $flags = &patronflags($patron, $dbh);
358 {message} Message showing patron's credit or debt
359 {noissues} Set if patron owes >$5.00
360 {GNA} Set if patron gone w/o address
361 {message} "Borrower has no valid address"
363 {LOST} Set if patron's card reported lost
364 {message} Message to this effect
366 {DBARRED} Set is patron is debarred
367 {message} Message to this effect
369 {NOTES} Set if patron has notes
370 {message} Notes about patron
371 {ODUES} Set if patron has overdue books
373 {itemlist} ref-to-array: list of overdue books
374 {itemlisttext} Text list of overdue items
375 {WAITING} Set if there are items available that the
377 {message} Message to this effect
378 {itemlist} ref-to-array: list of available items
384 my ( $patroninformation) = @_;
385 my $dbh=C4::Context->dbh;
386 my ($amount) = GetBorrowerAcctRecord( $patroninformation->{'borrowernumber'});
389 my $noissuescharge = C4::Context->preference("noissuescharge");
390 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
391 if ( $amount > $noissuescharge ) {
392 $flaginfo{'noissues'} = 1;
394 $flags{'CHARGES'} = \%flaginfo;
396 elsif ( $amount < 0 ) {
398 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
399 $flags{'CHARGES'} = \%flaginfo;
401 if ( $patroninformation->{'gonenoaddress'}
402 && $patroninformation->{'gonenoaddress'} == 1 )
405 $flaginfo{'message'} = 'Borrower has no valid address.';
406 $flaginfo{'noissues'} = 1;
407 $flags{'GNA'} = \%flaginfo;
409 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
411 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
412 $flaginfo{'noissues'} = 1;
413 $flags{'LOST'} = \%flaginfo;
415 if ( $patroninformation->{'debarred'}
416 && $patroninformation->{'debarred'} == 1 )
419 $flaginfo{'message'} = 'Borrower is Debarred.';
420 $flaginfo{'noissues'} = 1;
421 $flags{'DBARRED'} = \%flaginfo;
423 if ( $patroninformation->{'borrowernotes'}
424 && $patroninformation->{'borrowernotes'} )
427 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
428 $flags{'NOTES'} = \%flaginfo;
430 my ( $odues, $itemsoverdue ) =
431 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
434 $flaginfo{'message'} = "Yes";
435 $flaginfo{'itemlist'} = $itemsoverdue;
436 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
439 $flaginfo{'itemlisttext'} .=
440 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
442 $flags{'ODUES'} = \%flaginfo;
445 C4::Reserves2::GetWaitingReserves( $patroninformation->{'borrowernumber'} );
446 my $nowaiting = scalar @$itemswaiting;
447 if ( $nowaiting > 0 ) {
449 $flaginfo{'message'} = "Reserved items available";
450 $flaginfo{'itemlist'} = $itemswaiting;
451 $flags{'WAITING'} = \%flaginfo;
459 $borrower = &GetMember($information, $type);
461 Looks up information about a patron (borrower) by either card number
462 ,firstname, or borrower number, depending on $type value.
463 If C<$type> == 'cardnumber', C<&GetBorrower>
464 searches by cardnumber then by firstname if not found in cardnumber;
465 otherwise, it searches by borrowernumber.
467 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
468 the C<borrowers> table in the Koha database.
474 my ( $information, $type ) = @_;
475 my $dbh = C4::Context->dbh;
477 if ($type eq 'cardnumber' || $type eq 'firstname'|| $type eq 'userid'|| $type eq 'borrowernumber'){
478 $information = uc $information;
481 "Select borrowers.*,categories.category_type,categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where $type=?"
483 $sth->execute($information);
488 "Select borrowers.*,categories.category_type, categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
490 $sth->execute($information);
492 my $data = $sth->fetchrow_hashref;
498 elsif ($type eq 'cardnumber' ||$type eq 'firstname') { # try with firstname
501 "Select borrowers.*,categories.category_type,categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname like ?"
503 $sth->execute($information);
504 my $data = $sth->fetchrow_hashref;
513 =item GetBorrowerIssuesAndFines
515 ($borrowed, $due, $fine) = &GetBorrowerIssuesAndFines($borrowernumber);
517 Returns aggregate data about items borrowed by the patron with the
518 given borrowernumber.
520 C<&GetBorrowerIssuesAndFines> returns a three-element array. C<$borrowed> is the
521 number of books the patron currently has borrowed. C<$due> is the
522 number of overdue items the patron currently has borrowed. C<$fine> is
523 the total fine currently due by the borrower.
528 sub GetBorrowerIssuesAndFines {
529 my ( $borrowernumber ) = @_;
530 my $dbh = C4::Context->dbh;
532 "Select count(*) from issues where borrowernumber='$borrowernumber' and
536 my $sth = $dbh->prepare($query);
538 my $data = $sth->fetchrow_hashref;
540 $sth = $dbh->prepare(
541 "Select count(*) from issues where
542 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
545 my $data2 = $sth->fetchrow_hashref;
547 $sth = $dbh->prepare(
548 "Select sum(amountoutstanding) from accountlines where
549 borrowernumber='$borrowernumber'"
552 my $data3 = $sth->fetchrow_hashref;
555 return ( $data2->{'count(*)'}, $data->{'count(*)'},
556 $data3->{'sum(amountoutstanding)'} );
563 ($borrowed, $due, $fine) = &ModMember($borrowernumber);
565 Modify borrower's data
572 my $dbh = C4::Context->dbh;
573 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
574 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
575 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
577 # warn "num user".$data{'borrowernumber'};
580 $data{'userid'} = '' if ( $data{'password'} eq '' );
582 # test to know if u must update or not the borrower password
583 if ( $data{'password'} eq '****' ) {
585 $query = "UPDATE borrowers SET
586 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
587 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
588 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
589 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
590 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
591 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
592 ethnotes = ?,sex = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
593 WHERE borrowernumber=$data{'borrowernumber'}";
594 $sth = $dbh->prepare($query);
596 $data{'cardnumber'}, $data{'surname'},
597 $data{'firstname'}, $data{'title'},
598 $data{'othernames'}, $data{'initials'},
599 $data{'streetnumber'}, $data{'streettype'},
600 $data{'address'}, $data{'address2'},
601 $data{'city'}, $data{'zipcode'},
602 $data{'email'}, $data{'phone'},
603 $data{'mobile'}, $data{'fax'},
604 $data{'emailpro'}, $data{'phonepro'},
605 $data{'B_streetnumber'}, $data{'B_streettype'},
606 $data{'B_address'}, $data{'B_city'},
607 $data{'B_zipcode'}, $data{'B_email'},
608 $data{'B_phone'}, $data{'dateofbirth'},
609 $data{'branchcode'}, $data{'categorycode'},
610 $data{'dateenrolled'}, $data{'dateexpiry'},
611 $data{'gonenoaddress'}, $data{'lost'},
612 $data{'debarred'}, $data{'contactname'},
613 $data{'contactfirstname'}, $data{'contacttitle'},
614 $data{'guarantorid'}, $data{'borrowernotes'},
615 $data{'relationship'}, $data{'ethnicity'},
616 $data{'ethnotes'}, $data{'sex'},
617 $data{'userid'}, $data{'opacnote'},
618 $data{'contactnote'}, $data{'sort1'},
624 ( $data{'password'} = md5_base64( $data{'password'} ) )
625 if ( $data{'password'} ne '' );
626 $query = "UPDATE borrowers SET
627 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
628 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
629 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
630 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
631 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
632 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
633 ethnotes = ?,sex = ?,password = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
634 WHERE borrowernumber=$data{'borrowernumber'}";
635 $sth = $dbh->prepare($query);
637 $data{'cardnumber'}, $data{'surname'},
638 $data{'firstname'}, $data{'title'},
639 $data{'othernames'}, $data{'initials'},
640 $data{'streetnumber'}, $data{'streettype'},
641 $data{'address'}, $data{'address2'},
642 $data{'city'}, $data{'zipcode'},
643 $data{'email'}, $data{'phone'},
644 $data{'mobile'}, $data{'fax'},
645 $data{'emailpro'}, $data{'phonepro'},
646 $data{'B_streetnumber'}, $data{'B_streettype'},
647 $data{'B_address'}, $data{'B_city'},
648 $data{'B_zipcode'}, $data{'B_email'},
649 $data{'B_phone'}, $data{'dateofbirth'},
650 $data{'branchcode'}, $data{'categorycode'},
651 $data{'dateenrolled'}, $data{'dateexpiry'},
652 $data{'gonenoaddress'}, $data{'lost'},
653 $data{'debarred'}, $data{'contactname'},
654 $data{'contactfirstname'}, $data{'contacttitle'},
655 $data{'guarantorid'}, $data{'borrowernotes'},
656 $data{'relationship'}, $data{'ethnicity'},
657 $data{'ethnotes'}, $data{'sex'},
658 $data{'password'}, $data{'userid'},
659 $data{'opacnote'}, $data{'contactnote'},
660 $data{'sort1'}, $data{'sort2'}
665 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
666 # so when we update information for an adult we should check for guarantees and update the relevant part
667 # of their records, ie addresses and phone numbers
668 my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
669 if ( $borrowercategory->{'category_type'} eq 'A' ) {
670 # is adult check guarantees;
671 UpdateGuarantees(%data);
674 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
675 if C4::Context->preference("BorrowersLog");
682 $borrowernumber = &ModMember(%borrower);
684 insert new borrower into table
691 my $dbh = C4::Context->dbh;
692 $data{'userid'} = '' unless $data{'password'};
693 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
694 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
695 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
696 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
698 "insert into borrowers set cardnumber="
699 . $dbh->quote( $data{'cardnumber'} )
701 . $dbh->quote( $data{'surname'} )
703 . $dbh->quote( $data{'firstname'} )
705 . $dbh->quote( $data{'title'} )
707 . $dbh->quote( $data{'othernames'} )
709 . $dbh->quote( $data{'initials'} )
711 . $dbh->quote( $data{'streetnumber'} )
713 . $dbh->quote( $data{'streettype'} )
715 . $dbh->quote( $data{'address'} )
717 . $dbh->quote( $data{'address2'} )
719 . $dbh->quote( $data{'zipcode'} )
721 . $dbh->quote( $data{'city'} )
723 . $dbh->quote( $data{'phone'} )
725 . $dbh->quote( $data{'email'} )
727 . $dbh->quote( $data{'mobile'} )
729 . $dbh->quote( $data{'phonepro'} )
731 . $dbh->quote( $data{'opacnote'} )
733 . $dbh->quote( $data{'guarantorid'} )
735 . $dbh->quote( $data{'dateofbirth'} )
737 . $dbh->quote( $data{'branchcode'} )
739 . $dbh->quote( $data{'categorycode'} )
741 . $dbh->quote( $data{'dateenrolled'} )
743 . $dbh->quote( $data{'contactname'} )
745 . $dbh->quote( $data{'borrowernotes'} )
747 . $dbh->quote( $data{'dateexpiry'} )
749 . $dbh->quote( $data{'contactnote'} )
751 . $dbh->quote( $data{'B_address'} )
753 . $dbh->quote( $data{'B_zipcode'} )
755 . $dbh->quote( $data{'B_city'} )
757 . $dbh->quote( $data{'B_phone'} )
759 . $dbh->quote( $data{'B_email'}, )
761 . $dbh->quote( $data{'password'} )
763 . $dbh->quote( $data{'userid'} )
765 . $dbh->quote( $data{'sort1'} )
767 . $dbh->quote( $data{'sort2'} )
769 . $dbh->quote( $data{'contacttitle'} )
771 . $dbh->quote( $data{'emailpro'} )
772 . ",contactfirstname="
773 . $dbh->quote( $data{'contactfirstname'} ) . ",sex="
774 . $dbh->quote( $data{'sex'} ) . ",fax="
775 . $dbh->quote( $data{'fax'} )
777 . $dbh->quote( $data{'relationship'} )
779 . $dbh->quote( $data{'B_streetnumber'} )
781 . $dbh->quote( $data{'B_streettype'} )
783 . $dbh->quote( $data{'gonenoaddress'} )
785 . $dbh->quote( $data{'lost'} )
787 . $dbh->quote( $data{'debarred'} )
789 . $dbh->quote( $data{'ethnicity'} )
791 . $dbh->quote( $data{'ethnotes'} );
793 my $sth = $dbh->prepare($query);
796 $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
798 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
799 if C4::Context->preference("BorrowersLog");
801 return $data{'borrowernumber'};
805 my ( $uid, $member, $digest ) = @_;
806 my $dbh = C4::Context->dbh;
808 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
809 #Then we need to tell the user and have them create a new one.
812 "select * from borrowers where userid=? and borrowernumber != ?");
813 $sth->execute( $uid, $member );
814 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
819 #Everything is good so we can update the information.
822 "update borrowers set userid=?, password=? where borrowernumber=?");
823 $sth->execute( $uid, $digest, $member );
827 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
828 if C4::Context->preference("BorrowersLog");
832 sub UpdateGuarantees {
834 my $dbh = C4::Context->dbh;
835 my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
836 for ( my $i = 0 ; $i < $count ; $i++ ) {
839 # It looks like the $i is only being returned to handle walking through
840 # the array, which is probably better done as a foreach loop.
842 my $guaquery = qq|UPDATE borrowers
843 SET address='$data{'address'}',fax='$data{'fax'}',
844 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
845 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
847 my $sth3 = $dbh->prepare($guaquery);
853 =item fixup_cardnumber
855 Warning: The caller is responsible for locking the members table in write
856 mode, to avoid database corruption.
860 use vars qw( @weightings );
861 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
863 sub fixup_cardnumber ($) {
864 my ($cardnumber) = @_;
865 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
866 $autonumber_members = 0 unless defined $autonumber_members;
868 # Find out whether member numbers should be generated
869 # automatically. Should be either "1" or something else.
870 # Defaults to "0", which is interpreted as "no".
872 # if ($cardnumber !~ /\S/ && $autonumber_members) {
873 if ($autonumber_members) {
874 my $dbh = C4::Context->dbh;
875 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
877 # if checkdigit is selected, calculate katipo-style cardnumber.
878 # otherwise, just use the max()
879 # purpose: generate checksum'd member numbers.
880 # We'll assume we just got the max value of digits 2-8 of member #'s
881 # from the database and our job is to increment that by one,
882 # determine the 1st and 9th digits and return the full string.
885 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
889 my $data = $sth->fetchrow_hashref;
890 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
892 if ( !$cardnumber ) { # If DB has no values,
893 $cardnumber = 1000000; # start at 1000000
900 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
902 # read weightings, left to right, 1 char at a time
903 my $temp1 = $weightings[$i];
905 # sequence left to right, 1 char at a time
906 my $temp2 = substr( $cardnumber, $i, 1 );
908 # mult each char 1-7 by its corresponding weighting
909 $sum += $temp1 * $temp2;
912 my $rem = ( $sum % 11 );
913 $rem = 'X' if $rem == 10;
915 $cardnumber = "V$cardnumber$rem";
919 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
920 # better. I'll leave the original in in case it needs to be changed for you
923 "select max(cast(cardnumber as signed)) from borrowers");
925 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
929 my ($result) = $sth->fetchrow;
931 $cardnumber = $result + 1;
939 ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
940 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
941 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
943 C<&GetGuarantees> takes a borrower number (e.g., that of a patron
944 with children) and looks up the borrowers who are guaranteed by that
945 borrower (i.e., the patron's children).
947 C<&GetGuarantees> returns two values: an integer giving the number of
948 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
949 of references to hash, which gives the actual results.
955 my ($borrowernumber) = @_;
956 my $dbh = C4::Context->dbh;
959 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
961 $sth->execute($borrowernumber);
964 my $data = $sth->fetchall_arrayref({});
966 return ( scalar(@$data), $data );
969 =head2 GetPendingIssues
971 ($count, $issues) = &GetPendingIssues($borrowernumber);
973 Looks up what the patron with the given borrowernumber has borrowed.
975 C<&GetPendingIssues> returns a two-element array. C<$issues> is a
976 reference-to-array, where each element is a reference-to-hash; the
977 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
978 in the Koha database. C<$count> is the number of elements in
984 sub GetPendingIssues {
985 my ($borrowernumber) = @_;
986 my $dbh = C4::Context->dbh;
987 my $sth = $dbh->prepare(
988 "Select * from issues,biblio,items where borrowernumber=?
989 and items.itemnumber=issues.itemnumber
990 and items.biblionumber=biblio.biblionumber
991 and issues.returndate is NULL order by date_due"
993 $sth->execute($borrowernumber);
994 my $data = $sth->fetchall_arrayref({});
995 return ( scalar(@$data), $data );
1000 ($count, $issues) = &GetAllIssues($borrowernumber, $sortkey, $limit);
1002 Looks up what the patron with the given borrowernumber has borrowed,
1003 and sorts the results.
1005 C<$sortkey> is the name of a field on which to sort the results. This
1006 should be the name of a field in the C<issues>, C<biblio>,
1007 C<biblioitems>, or C<items> table in the Koha database.
1009 C<$limit> is the maximum number of results to return.
1011 C<&GetAllIssues> returns a two-element array. C<$issues> is a
1012 reference-to-array, where each element is a reference-to-hash; the
1013 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1014 C<items> tables of the Koha database. C<$count> is the number of
1015 elements in C<$issues>
1021 my ( $borrowernumber, $order, $limit ) = @_;
1023 #FIXME: sanity-check order and limit
1024 my $dbh = C4::Context->dbh;
1027 "Select *,items.timestamp AS itemstimestamp from issues,biblio,items,biblioitems
1028 where borrowernumber=? and
1029 items.biblioitemnumber=biblioitems.biblioitemnumber and
1030 items.itemnumber=issues.itemnumber and
1031 items.biblionumber=biblio.biblionumber order by $order";
1032 if ( $limit != 0 ) {
1033 $query .= " limit $limit";
1037 my $sth = $dbh->prepare($query);
1038 $sth->execute($borrowernumber);
1041 while ( my $data = $sth->fetchrow_hashref ) {
1042 $result[$i] = $data;
1047 # get all issued items for borrowernumber from oldissues table
1048 # large chunk of older issues data put into table oldissues
1049 # to speed up db calls for issuing items
1050 if ( C4::Context->preference("ReadingHistory") ) {
1051 my $query2 = "SELECT * FROM oldissues,biblio,items,biblioitems
1052 WHERE borrowernumber=?
1053 AND items.biblioitemnumber=biblioitems.biblioitemnumber
1054 AND items.itemnumber=oldissues.itemnumber
1055 AND items.biblionumber=biblio.biblionumber
1057 if ( $limit != 0 ) {
1058 $limit = $limit - $count;
1059 $query2 .= " limit $limit";
1062 my $sth2 = $dbh->prepare($query2);
1063 $sth2->execute($borrowernumber);
1065 while ( my $data2 = $sth2->fetchrow_hashref ) {
1066 $result[$i] = $data2;
1073 return ( $i, \@result );
1077 =head2 GetBorrowerAcctRecord
1079 ($total, $acctlines, $count) = &GetBorrowerAcctRecord($borrowernumber);
1081 Looks up accounting data for the patron with the given borrowernumber.
1083 C<&GetBorrowerAcctRecord> returns a three-element array. C<$acctlines> is a
1084 reference-to-array, where each element is a reference-to-hash; the
1085 keys are the fields of the C<accountlines> table in the Koha database.
1086 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1087 total amount outstanding for all of the account lines.
1092 sub GetBorrowerAcctRecord {
1093 my ($borrowernumber,$date) = @_;
1094 my $dbh = C4::Context->dbh;
1098 Select * from accountlines where
1099 borrowernumber=? order by date desc,timestamp desc
1101 my @bind = ($borrowernumber);
1102 if ($date && $date ne ''){
1103 $strsth.=" AND date < ?";
1107 my $sth= $dbh->prepare( $strsth );
1108 $sth->execute( $borrowernumber);
1110 while ( my $data = $sth->fetchrow_hashref ) {
1112 $acctlines[$numlines] = $data;
1114 $total += $data->{'amountoutstanding'};
1117 return ( $total, \@acctlines,$numlines);
1120 =head2 GetBorNotifyAcctRecord
1122 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1124 Looks up accounting data for the patron with the given borrowernumber per file number.
1126 (FIXME - I'm not at all sure what this is about.)
1128 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1129 reference-to-array, where each element is a reference-to-hash; the
1130 keys are the fields of the C<accountlines> table in the Koha database.
1131 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1132 total amount outstanding for all of the account lines.
1136 sub GetBorNotifyAcctRecord {
1137 my ( $params, $notifyid ) = @_;
1138 my $dbh = C4::Context->dbh;
1141 my $query = qq| SELECT *
1143 WHERE borrowernumber=?
1145 AND (accounttype='FU' OR accounttype='N' OR accounttype='M'OR accounttype='A'OR accounttype='F'OR accounttype='L' OR accounttype='IP' OR accounttype='CH' OR accounttype='RE' OR accounttype='RL')
1146 AND amountoutstanding != '0'
1147 ORDER BY notify_id,accounttype
1149 my $sth = $dbh->prepare($query);
1151 $sth->execute( $params->{'borrowernumber'}, $notifyid );
1153 while ( my $data = $sth->fetchrow_hashref ) {
1154 $acctlines[$numlines] = $data;
1156 $total += $data->{'amountoutstanding'};
1159 return ( $numlines, \@acctlines, $total );
1162 =head2 checkuniquemember (OUEST-PROVENCE)
1164 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
1166 Checks that a member exists or not in the database.
1168 C<&result> is 1 (=exist) or 0 (=does not exist)
1169 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1170 C<&surname> is the surname
1171 C<&categorycode> is from categorycode table
1172 C<&firstname> is the firstname (only if collectivity=0)
1173 C<&dateofbirth> is the date of birth (only if collectivity=0)
1177 sub checkuniquemember {
1178 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1179 my $dbh = C4::Context->dbh;
1181 if ($collectivity) {
1183 # $request="select count(*) from borrowers where surname=? and categorycode=?";
1185 "select borrowernumber,categorycode from borrowers where surname=? ";
1189 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1191 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1193 my $sth = $dbh->prepare($request);
1194 if ($collectivity) {
1195 $sth->execute( uc($surname) );
1198 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1200 my @data = $sth->fetchrow;
1203 return $data[0], $data[1];
1213 =head2 getzipnamecity (OUEST-PROVENCE)
1215 take all info from table city for the fields city and zip
1216 check for the name and the zip code of the city selected
1220 sub getzipnamecity {
1222 my $dbh = C4::Context->dbh;
1225 "select city_name,city_zipcode from cities where cityid=? ");
1226 $sth->execute($cityid);
1227 my @data = $sth->fetchrow;
1228 return $data[0], $data[1];
1232 =head2 getdcity (OUEST-PROVENCE)
1234 recover cityid with city_name condition
1239 my ($city_name) = @_;
1240 my $dbh = C4::Context->dbh;
1241 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1242 $sth->execute($city_name);
1243 my $data = $sth->fetchrow;
1248 =head2 GetExpiryDate
1250 $expirydate = GetExpiryDate($categorycode, $dateenrolled);
1251 process expiry date given a date and a categorycode
1255 my ( $categorycode, $dateenrolled ) = @_;
1256 my $dbh = C4::Context->dbh;
1259 "select enrolmentperiod from categories where categorycode=?");
1260 $sth->execute($categorycode);
1261 my ($enrolmentperiod) = $sth->fetchrow;
1262 $enrolmentperiod = 12 unless ($enrolmentperiod);
1263 my @date=split /-/,format_date_in_iso($dateenrolled);
1264 @date=Add_Delta_YM($date[0],$date[1],$date[2],0,$enrolmentperiod);
1265 return sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
1268 =head2 checkuserpassword (OUEST-PROVENCE)
1270 check for the password and login are not used
1271 return the number of record
1272 0=> NOT USED 1=> USED
1276 sub checkuserpassword {
1277 my ( $borrowernumber, $userid, $password ) = @_;
1278 $password = md5_base64($password);
1279 my $dbh = C4::Context->dbh;
1282 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1284 $sth->execute( $borrowernumber, $userid, $password );
1285 my $number_rows = $sth->fetchrow;
1286 return $number_rows;
1290 =head2 GetborCatFromCatType
1292 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1294 Looks up the different types of borrowers in the database. Returns two
1295 elements: a reference-to-array, which lists the borrower category
1296 codes, and a reference-to-hash, which maps the borrower category codes
1297 to category descriptions.
1302 sub GetborCatFromCatType {
1303 my ( $category_type, $action ) = @_;
1304 my $dbh = C4::Context->dbh;
1305 my $request = qq| SELECT categorycode,description
1308 ORDER BY categorycode|;
1309 my $sth = $dbh->prepare($request);
1311 $sth->execute($category_type);
1320 while ( my $data = $sth->fetchrow_hashref ) {
1321 push @codes, $data->{'categorycode'};
1322 $labels{ $data->{'categorycode'} } = $data->{'description'};
1325 return ( \@codes, \%labels );
1328 =head2 GetBorrowercategory
1330 $hashref = &GetBorrowercategory($categorycode);
1332 Given the borrower's category code, the function returns the corresponding
1333 data hashref for a comprehensive information display.
1337 sub GetBorrowercategory {
1339 my $dbh = C4::Context->dbh;
1342 "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?"
1344 $sth->execute($catcode);
1346 $sth->fetchrow_hashref;
1349 } # sub getborrowercategory
1351 =head2 ethnicitycategories
1353 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1355 Looks up the different ethnic types in the database. Returns two
1356 elements: a reference-to-array, which lists the ethnicity codes, and a
1357 reference-to-hash, which maps the ethnicity codes to ethnicity
1364 sub ethnicitycategories {
1365 my $dbh = C4::Context->dbh;
1366 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1370 while ( my $data = $sth->fetchrow_hashref ) {
1371 push @codes, $data->{'code'};
1372 $labels{ $data->{'code'} } = $data->{'name'};
1375 return ( \@codes, \%labels );
1380 $ethn_name = &fixEthnicity($ethn_code);
1382 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1383 corresponding descriptive name from the C<ethnicity> table in the
1384 Koha database ("European" or "Pacific Islander").
1391 my $ethnicity = shift;
1392 return unless $ethnicity;
1393 my $dbh = C4::Context->dbh;
1394 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1395 $sth->execute($ethnicity);
1396 my $data = $sth->fetchrow_hashref;
1398 return $data->{'name'};
1399 } # sub fixEthnicity
1403 $dateofbirth,$date = &GetAge($date);
1405 this function return the borrowers age with the value of dateofbirth
1411 my ( $date, $date_ref ) = @_;
1413 if ( not defined $date_ref ) {
1414 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1417 my ( $year1, $month1, $day1 ) = split /-/, $date;
1418 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1420 my $age = $year2 - $year1;
1421 if ( $month1 . $day1 > $month2 . $day2 ) {
1428 =head2 get_institutions
1429 $insitutions = get_institutions();
1431 Just returns a list of all the borrowers of type I, borrownumber and name
1436 sub get_institutions {
1437 my $dbh = C4::Context->dbh();
1440 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1444 while ( my $data = $sth->fetchrow_hashref() ) {
1445 $orgs{ $data->{'borrowernumber'} } = $data;
1450 } # sub get_institutions
1452 =head2 add_member_orgs
1454 add_member_orgs($borrowernumber,$borrowernumbers);
1456 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1461 sub add_member_orgs {
1462 my ( $borrowernumber, $otherborrowers ) = @_;
1463 my $dbh = C4::Context->dbh();
1465 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1466 my $sth = $dbh->prepare($query);
1467 foreach my $otherborrowernumber (@$otherborrowers) {
1468 $sth->execute( $borrowernumber, $otherborrowernumber );
1472 } # sub add_member_orgs
1474 =head2 GetBorrowersFromSurname
1478 \@resutlts = GetBorrowersFromSurname($surname)
1479 this function get the list of borrower names like $surname.
1481 the table of results in @results
1487 sub GetBorrowersFromSurname {
1488 my ($searchstring) = @_;
1489 my $dbh = C4::Context->dbh;
1490 $searchstring =~ s/\'/\\\'/g;
1491 my @data = split( ' ', $searchstring );
1494 SELECT surname,firstname
1496 WHERE (surname like ?)
1499 my $sth = $dbh->prepare($query);
1500 $sth->execute("$data[0]%");
1504 my $data = $sth->fetchall_arrayref({});
1506 return ( scalar(@$data), $data );
1509 =head2 GetCities (OUEST-PROVENCE)
1511 ($id_cityarrayref, $city_hashref) = &GetCities();
1513 Looks up the different city and zip in the database. Returns two
1514 elements: a reference-to-array, which lists the zip city
1515 codes, and a reference-to-hash, which maps the name of the city.
1516 WHERE =>OUEST PROVENCE OR EXTERIEUR
1522 #my ($type_city) = @_;
1523 my $dbh = C4::Context->dbh;
1524 my $query = qq|SELECT cityid,city_name
1526 ORDER BY city_name|;
1527 my $sth = $dbh->prepare($query);
1529 #$sth->execute($type_city);
1534 # insert empty value to create a empty choice in cgi popup
1536 while ( my $data = $sth->fetchrow_hashref ) {
1538 push @id, $data->{'cityid'};
1539 $city{ $data->{'cityid'} } = $data->{'city_name'};
1542 #test to know if the table contain some records if no the function return nothing
1550 return ( \@id, \%city );
1554 =head2 GetSortDetails (OUEST-PROVENCE)
1556 ($lib) = &GetSortDetails($category,$sortvalue);
1558 Returns the authorized value details
1559 C<&$lib>return value of authorized value details
1560 C<&$sortvalue>this is the value of authorized value
1561 C<&$category>this is the value of authorized value category
1565 sub GetSortDetails {
1566 my ( $category, $sortvalue ) = @_;
1567 my $dbh = C4::Context->dbh;
1568 my $query = qq|SELECT lib
1569 FROM authorised_values
1571 AND authorised_value=? |;
1572 my $sth = $dbh->prepare($query);
1573 $sth->execute( $category, $sortvalue );
1574 my $lib = $sth->fetchrow;
1578 =head2 DeleteBorrower
1580 () = &DeleteBorrower($member);
1582 delete all data fo borrowers and add record to deletedborrowers table
1583 C<&$member>this is the borrowernumber
1587 sub MoveMemberToDeleted {
1589 my $dbh = C4::Context->dbh;
1591 $query = qq|SELECT *
1593 WHERE borrowernumber=?|;
1594 my $sth = $dbh->prepare($query);
1595 $sth->execute($member);
1596 my @data = $sth->fetchrow_array;
1599 $dbh->prepare( "Insert into deletedborrowers values ("
1600 . ( "?," x ( scalar(@data) - 1 ) )
1602 $sth->execute(@data);
1608 DelMember($borrowernumber);
1610 This function remove directly a borrower whitout writing it on deleteborrower.
1611 + Deletes reserves for the borrower
1616 my $dbh = C4::Context->dbh;
1617 my $borrowernumber = shift;
1618 return unless $borrowernumber; # borrowernumber is mandatory.
1620 my $query = qq|DELETE
1622 WHERE borrowernumber=?|;
1623 my $sth = $dbh->prepare($query);
1624 $sth->execute($borrowernumber);
1629 WHERE borrowernumber = ?
1631 my $sth = $dbh->prepare($query);
1632 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$borrowernumber,"")
1633 if C4::Context->preference("BorrowersLog");
1637 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1639 $date= ExtendMemberSubscriptionTo($borrowerid, $date);
1640 Extending the subscription to a given date or to the expiry date calculated on local date.
1644 sub ExtendMemberSubscriptionTo {
1645 my ( $borrowerid,$date) = @_;
1646 my $dbh = C4::Context->dbh;
1648 $date=POSIX::strftime("%Y-%m-%d",localtime(time));
1649 my $borrower = GetBorrower($borrowerid,'borrowernumber');
1650 $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
1652 my $sth = $dbh->do(<<EOF);
1654 SET dateexpiry='$date'
1655 WHERE borrowernumber='$borrowerid'
1657 return $date if ($sth);
1661 =head2 GetRoadTypes (OUEST-PROVENCE)
1663 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1665 Looks up the different road type . Returns two
1666 elements: a reference-to-array, which lists the id_roadtype
1667 codes, and a reference-to-hash, which maps the road type of the road .
1673 my $dbh = C4::Context->dbh;
1674 my $query = qq|SELECT roadtypeid,road_type
1676 ORDER BY road_type|;
1677 my $sth = $dbh->prepare($query);
1682 # insert empty value to create a empty choice in cgi popup
1684 while ( my $data = $sth->fetchrow_hashref ) {
1686 push @id, $data->{'roadtypeid'};
1687 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1690 #test to know if the table contain some records if no the function return nothing
1698 return ( \@id, \%roadtype );
1704 =head2 GetBorrowersTitles (OUEST-PROVENCE)
1706 ($borrowertitle)= &GetBorrowersTitles();
1708 Looks up the different title . Returns array with all borrowers title
1712 sub GetBorrowersTitles {
1713 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1714 unshift( @borrowerTitle, "" );
1715 return ( \@borrowerTitle);
1720 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1722 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1724 Returns the description of roadtype
1725 C<&$roadtype>return description of road type
1726 C<&$roadtypeid>this is the value of roadtype s
1730 sub GetRoadTypeDetails {
1731 my ($roadtypeid) = @_;
1732 my $dbh = C4::Context->dbh;
1733 my $query = qq|SELECT road_type
1735 WHERE roadtypeid=?|;
1736 my $sth = $dbh->prepare($query);
1737 $sth->execute($roadtypeid);
1738 my $roadtype = $sth->fetchrow;
1742 =head2 GetBorrowersWhoHaveNotBorrowedSince
1744 &GetBorrowersWhoHaveNotBorrowedSince($date)
1746 this function get all borrowers who haven't borrowed since the date given on input arg.
1750 sub GetBorrowersWhoHaveNotBorrowedSince {
1752 return unless $date; # date is mandatory.
1753 my $dbh = C4::Context->dbh;
1755 SELECT borrowers.borrowernumber,max(timestamp)
1757 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1758 WHERE issues.borrowernumber IS NOT NULL
1759 GROUP BY borrowers.borrowernumber
1761 my $sth = $dbh->prepare($query);
1765 while ( my $data = $sth->fetchrow_hashref ) {
1766 push @results, $data;
1771 =head2 GetBorrowersWhoHaveNeverBorrowed
1773 $results = &GetBorrowersWhoHaveNeverBorrowed
1775 this function get all borrowers who have never borrowed.
1777 I<$result> is a ref to an array which all elements are a hasref.
1781 sub GetBorrowersWhoHaveNeverBorrowed {
1782 my $dbh = C4::Context->dbh;
1784 SELECT borrowers.borrowernumber,max(timestamp)
1786 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1787 WHERE issues.borrowernumber IS NULL
1789 my $sth = $dbh->prepare($query);
1792 while ( my $data = $sth->fetchrow_hashref ) {
1793 push @results, $data;
1798 =head2 GetBorrowersWithIssuesHistoryOlderThan
1800 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1802 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1804 I<$result> is a ref to an array which all elements are a hashref.
1805 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
1809 sub GetBorrowersWithIssuesHistoryOlderThan {
1810 my $dbh = C4::Context->dbh;
1812 return unless $date; # date is mandatory.
1814 SELECT count(borrowernumber) as n,borrowernumber
1816 WHERE returndate < ?
1817 AND borrowernumber IS NOT NULL
1818 GROUP BY borrowernumber
1820 my $sth = $dbh->prepare($query);
1821 $sth->execute($date);
1824 while ( my $data = $sth->fetchrow_hashref ) {
1825 push @results, $data;
1830 END { } # module clean-up code here (global destructor)