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
64 &GetMemberIssuesAndFines
77 &GetMemberAccountRecords
82 &GetBorNotifyAcctRecord
83 &ExtendMemberSubscriptionTo
87 &GetBorrowersWhoHaveNotBorrowedSince
88 &GetBorrowersWhoHaveNeverBorrowed
89 &GetBorrowersWithIssuesHistoryOlderThan
90 &GetMembersFromSurname
121 ($count, $borrowers) = &SearchMember($searchstring, $type,$category_type);
123 Looks up patrons (borrowers) by name.
125 BUGFIX 499: C<$type> is now used to determine type of search.
126 if $type is "simple", search is performed on the first letter of the
129 $category_type is used to get a specified type of user.
130 (mainly adults when creating a child.)
132 C<$searchstring> is a space-separated list of search terms. Each term
133 must match the beginning a borrower's surname, first name, or other
136 C<&SearchMember> returns a two-element list. C<$borrowers> is a
137 reference-to-array; each element is a reference-to-hash, whose keys
138 are the fields of the C<borrowers> table in the Koha database.
139 C<$count> is the number of elements in C<$borrowers>.
144 #used by member enquiries from the intranet
147 my ($searchstring, $orderby, $type,$category_type ) = @_;
148 my $dbh = C4::Context->dbh;
154 if ( $type eq "simple" ) # simple search for one letter only
157 "SELECT * FROM borrowers
158 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode ".
159 ($category_type?" AND category_type = ".$dbh->quote($category_type):"").
160 " WHERE surname LIKE ? ORDER BY $orderby";
161 @bind = ("$searchstring%");
163 else # advanced search looking in surname, firstname and othernames
165 @data = split( ' ', $searchstring );
167 $query = "SELECT * FROM borrowers
168 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
169 WHERE ((surname LIKE ? OR surname LIKE ?
170 OR firstname LIKE ? OR firstname LIKE ?
171 OR othernames LIKE ? OR othernames LIKE ?)
173 ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
175 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
176 "$data[0]%", "% $data[0]%"
178 for ( my $i = 1 ; $i < $count ; $i++ ) {
179 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
180 OR firstname LIKE ? OR firstname LIKE ?
181 OR othernames LIKE ? OR othernames LIKE ?)";
183 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
184 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
188 $query = $query . ") OR cardnumber LIKE ?
190 push( @bind, $searchstring );
195 my $sth = $dbh->prepare($query);
197 # warn "Q $orderby : $query";
198 $sth->execute(@bind);
200 my $cnt = $sth->rows;
201 while ( my $data = $sth->fetchrow_hashref ) {
202 push( @results, $data );
207 return ( $cnt, \@results );
210 =head2 GetMemberDetails
212 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
214 Looks up a patron and returns information about him or her. If
215 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
216 up the borrower by number; otherwise, it looks up the borrower by card
219 C<$borrower> is a reference-to-hash whose keys are the fields of the
220 borrowers table in the Koha database. In addition,
221 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
222 about the patron. Its keys act as flags :
224 if $borrower->{flags}->{LOST} {
225 # Patron's card was reported lost
228 Each flag has a C<message> key, giving a human-readable explanation of
229 the flag. If the state of a flag means that the patron should not be
230 allowed to borrow any more books, then it will have a C<noissues> key
233 The possible flags are:
239 =item Shows the patron's credit or debt, if any.
247 =item (Gone, no address.) Set if the patron has left without giving a
256 =item Set if the patron's card has been reported as lost.
264 =item Set if the patron has been debarred.
272 =item Any additional notes about the patron.
280 =item Set if the patron has overdue items. This flag has several keys:
282 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
283 overdue items. Its elements are references-to-hash, each describing an
284 overdue item. The keys are selected fields from the issues, biblio,
285 biblioitems, and items tables of the Koha database.
287 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
288 the overdue items, one per line.
296 =item Set if any items that the patron has reserved are available.
298 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
299 available items. Each element is a reference-to-hash whose keys are
300 fields from the reserves table of the Koha database.
306 sub GetMemberDetails {
307 my ( $borrowernumber, $cardnumber ) = @_;
308 my $dbh = C4::Context->dbh;
311 if ($borrowernumber) {
312 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
313 $sth->execute($borrowernumber);
315 elsif ($cardnumber) {
316 $sth = $dbh->prepare("select * from borrowers where cardnumber=?");
317 $sth->execute($cardnumber);
322 my $borrower = $sth->fetchrow_hashref;
323 my ($amount) = GetMemberAccountRecords( $borrowernumber);
324 $borrower->{'amountoutstanding'} = $amount;
325 my $flags = patronflags( $borrower);
328 $sth = $dbh->prepare("select bit,flag from userflags");
330 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
331 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
332 $accessflagshash->{$flag} = 1;
336 $borrower->{'flags'} = $flags;
337 $borrower->{'authflags'} = $accessflagshash;
339 # find out how long the membership lasts
342 "select enrolmentperiod from categories where categorycode = ?");
343 $sth->execute( $borrower->{'categorycode'} );
344 my $enrolment = $sth->fetchrow;
345 $borrower->{'enrolmentperiod'} = $enrolment;
346 return ($borrower); #, $flags, $accessflagshash);
353 NOTE!: If you change this function, be sure to update the POD for
356 $flags = &patronflags($patron);
359 {message} Message showing patron's credit or debt
360 {noissues} Set if patron owes >$5.00
361 {GNA} Set if patron gone w/o address
362 {message} "Borrower has no valid address"
364 {LOST} Set if patron's card reported lost
365 {message} Message to this effect
367 {DBARRED} Set is patron is debarred
368 {message} Message to this effect
370 {NOTES} Set if patron has notes
371 {message} Notes about patron
372 {ODUES} Set if patron has overdue books
374 {itemlist} ref-to-array: list of overdue books
375 {itemlisttext} Text list of overdue items
376 {WAITING} Set if there are items available that the
378 {message} Message to this effect
379 {itemlist} ref-to-array: list of available items
385 my ( $patroninformation) = @_;
386 my $dbh=C4::Context->dbh;
387 my ($amount) = GetMemberAccountRecords( $patroninformation->{'borrowernumber'});
390 my $noissuescharge = C4::Context->preference("noissuescharge");
391 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
392 if ( $amount > $noissuescharge ) {
393 $flaginfo{'noissues'} = 1;
395 $flags{'CHARGES'} = \%flaginfo;
397 elsif ( $amount < 0 ) {
399 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
400 $flags{'CHARGES'} = \%flaginfo;
402 if ( $patroninformation->{'gonenoaddress'}
403 && $patroninformation->{'gonenoaddress'} == 1 )
406 $flaginfo{'message'} = 'Borrower has no valid address.';
407 $flaginfo{'noissues'} = 1;
408 $flags{'GNA'} = \%flaginfo;
410 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
412 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
413 $flaginfo{'noissues'} = 1;
414 $flags{'LOST'} = \%flaginfo;
416 if ( $patroninformation->{'debarred'}
417 && $patroninformation->{'debarred'} == 1 )
420 $flaginfo{'message'} = 'Borrower is Debarred.';
421 $flaginfo{'noissues'} = 1;
422 $flags{'DBARRED'} = \%flaginfo;
424 if ( $patroninformation->{'borrowernotes'}
425 && $patroninformation->{'borrowernotes'} )
428 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
429 $flags{'NOTES'} = \%flaginfo;
431 my ( $odues, $itemsoverdue ) =
432 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
435 $flaginfo{'message'} = "Yes";
436 $flaginfo{'itemlist'} = $itemsoverdue;
437 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
440 $flaginfo{'itemlisttext'} .=
441 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
443 $flags{'ODUES'} = \%flaginfo;
446 GetWaitingReserves( $patroninformation->{'borrowernumber'} );
447 my $nowaiting = scalar @$itemswaiting;
448 if ( $nowaiting > 0 ) {
450 $flaginfo{'message'} = "Reserved items available";
451 $flaginfo{'itemlist'} = $itemswaiting;
452 $flags{'WAITING'} = \%flaginfo;
460 $borrower = &GetMember($information, $type);
462 Looks up information about a patron (borrower) by either card number
463 ,firstname, or borrower number, depending on $type value.
464 If C<$type> == 'cardnumber', C<&GetBorrower>
465 searches by cardnumber then by firstname if not found in cardnumber;
466 otherwise, it searches by borrowernumber.
468 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
469 the C<borrowers> table in the Koha database.
475 my ( $information, $type ) = @_;
476 my $dbh = C4::Context->dbh;
478 if ($type eq 'cardnumber' || $type eq 'firstname'|| $type eq 'userid'|| $type eq 'borrowernumber'){
479 $information = uc $information;
482 "Select borrowers.*,categories.category_type,categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where $type=?"
484 $sth->execute($information);
489 "Select borrowers.*,categories.category_type, categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
491 $sth->execute($information);
493 my $data = $sth->fetchrow_hashref;
499 elsif ($type eq 'cardnumber' ||$type eq 'firstname') { # try with firstname
502 "Select borrowers.*,categories.category_type,categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname like ?"
504 $sth->execute($information);
505 my $data = $sth->fetchrow_hashref;
514 =item GetMemberIssuesAndFines
516 ($borrowed, $due, $fine) = &GetMemberIssuesAndFines($borrowernumber);
518 Returns aggregate data about items borrowed by the patron with the
519 given borrowernumber.
521 C<&GetMemberIssuesAndFines> returns a three-element array. C<$borrowed> is the
522 number of books the patron currently has borrowed. C<$due> is the
523 number of overdue items the patron currently has borrowed. C<$fine> is
524 the total fine currently due by the borrower.
529 sub GetMemberIssuesAndFines {
530 my ( $borrowernumber ) = @_;
531 my $dbh = C4::Context->dbh;
533 "Select count(*) from issues where borrowernumber='$borrowernumber' and
537 my $sth = $dbh->prepare($query);
539 my $data = $sth->fetchrow_hashref;
541 $sth = $dbh->prepare(
542 "Select count(*) from issues where
543 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
546 my $data2 = $sth->fetchrow_hashref;
548 $sth = $dbh->prepare(
549 "Select sum(amountoutstanding) from accountlines where
550 borrowernumber='$borrowernumber'"
553 my $data3 = $sth->fetchrow_hashref;
556 return ( $data2->{'count(*)'}, $data->{'count(*)'},
557 $data3->{'sum(amountoutstanding)'} );
564 &ModMember($borrowernumber);
566 Modify borrower's data
573 my $dbh = C4::Context->dbh;
574 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
575 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
576 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
578 # warn "num user".$data{'borrowernumber'};
581 $data{'userid'} = '' if ( $data{'password'} eq '' );
583 # test to know if u must update or not the borrower password
584 if ( $data{'password'} eq '****' ) {
586 $query = "UPDATE borrowers SET
587 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
588 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
589 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
590 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
591 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
592 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
593 ethnotes = ?,sex = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
594 WHERE borrowernumber=$data{'borrowernumber'}";
595 $sth = $dbh->prepare($query);
597 $data{'cardnumber'}, $data{'surname'},
598 $data{'firstname'}, $data{'title'},
599 $data{'othernames'}, $data{'initials'},
600 $data{'streetnumber'}, $data{'streettype'},
601 $data{'address'}, $data{'address2'},
602 $data{'city'}, $data{'zipcode'},
603 $data{'email'}, $data{'phone'},
604 $data{'mobile'}, $data{'fax'},
605 $data{'emailpro'}, $data{'phonepro'},
606 $data{'B_streetnumber'}, $data{'B_streettype'},
607 $data{'B_address'}, $data{'B_city'},
608 $data{'B_zipcode'}, $data{'B_email'},
609 $data{'B_phone'}, $data{'dateofbirth'},
610 $data{'branchcode'}, $data{'categorycode'},
611 $data{'dateenrolled'}, $data{'dateexpiry'},
612 $data{'gonenoaddress'}, $data{'lost'},
613 $data{'debarred'}, $data{'contactname'},
614 $data{'contactfirstname'}, $data{'contacttitle'},
615 $data{'guarantorid'}, $data{'borrowernotes'},
616 $data{'relationship'}, $data{'ethnicity'},
617 $data{'ethnotes'}, $data{'sex'},
618 $data{'userid'}, $data{'opacnote'},
619 $data{'contactnote'}, $data{'sort1'},
625 ( $data{'password'} = md5_base64( $data{'password'} ) )
626 if ( $data{'password'} ne '' );
627 $query = "UPDATE borrowers SET
628 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
629 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
630 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
631 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
632 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
633 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
634 ethnotes = ?,sex = ?,password = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
635 WHERE borrowernumber=$data{'borrowernumber'}";
636 $sth = $dbh->prepare($query);
638 $data{'cardnumber'}, $data{'surname'},
639 $data{'firstname'}, $data{'title'},
640 $data{'othernames'}, $data{'initials'},
641 $data{'streetnumber'}, $data{'streettype'},
642 $data{'address'}, $data{'address2'},
643 $data{'city'}, $data{'zipcode'},
644 $data{'email'}, $data{'phone'},
645 $data{'mobile'}, $data{'fax'},
646 $data{'emailpro'}, $data{'phonepro'},
647 $data{'B_streetnumber'}, $data{'B_streettype'},
648 $data{'B_address'}, $data{'B_city'},
649 $data{'B_zipcode'}, $data{'B_email'},
650 $data{'B_phone'}, $data{'dateofbirth'},
651 $data{'branchcode'}, $data{'categorycode'},
652 $data{'dateenrolled'}, $data{'dateexpiry'},
653 $data{'gonenoaddress'}, $data{'lost'},
654 $data{'debarred'}, $data{'contactname'},
655 $data{'contactfirstname'}, $data{'contacttitle'},
656 $data{'guarantorid'}, $data{'borrowernotes'},
657 $data{'relationship'}, $data{'ethnicity'},
658 $data{'ethnotes'}, $data{'sex'},
659 $data{'password'}, $data{'userid'},
660 $data{'opacnote'}, $data{'contactnote'},
661 $data{'sort1'}, $data{'sort2'}
666 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
667 # so when we update information for an adult we should check for guarantees and update the relevant part
668 # of their records, ie addresses and phone numbers
669 my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
670 if ( $borrowercategory->{'category_type'} eq 'A' ) {
671 # is adult check guarantees;
672 UpdateGuarantees(%data);
675 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
676 if C4::Context->preference("BorrowersLog");
683 $borrowernumber = &AddMember(%borrower);
685 insert new borrower into table
686 Returns the borrowernumber
693 my $dbh = C4::Context->dbh;
694 $data{'userid'} = '' unless $data{'password'};
695 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
696 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
697 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
698 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
700 "insert into borrowers set cardnumber="
701 . $dbh->quote( $data{'cardnumber'} )
703 . $dbh->quote( $data{'surname'} )
705 . $dbh->quote( $data{'firstname'} )
707 . $dbh->quote( $data{'title'} )
709 . $dbh->quote( $data{'othernames'} )
711 . $dbh->quote( $data{'initials'} )
713 . $dbh->quote( $data{'streetnumber'} )
715 . $dbh->quote( $data{'streettype'} )
717 . $dbh->quote( $data{'address'} )
719 . $dbh->quote( $data{'address2'} )
721 . $dbh->quote( $data{'zipcode'} )
723 . $dbh->quote( $data{'city'} )
725 . $dbh->quote( $data{'phone'} )
727 . $dbh->quote( $data{'email'} )
729 . $dbh->quote( $data{'mobile'} )
731 . $dbh->quote( $data{'phonepro'} )
733 . $dbh->quote( $data{'opacnote'} )
735 . $dbh->quote( $data{'guarantorid'} )
737 . $dbh->quote( $data{'dateofbirth'} )
739 . $dbh->quote( $data{'branchcode'} )
741 . $dbh->quote( $data{'categorycode'} )
743 . $dbh->quote( $data{'dateenrolled'} )
745 . $dbh->quote( $data{'contactname'} )
747 . $dbh->quote( $data{'borrowernotes'} )
749 . $dbh->quote( $data{'dateexpiry'} )
751 . $dbh->quote( $data{'contactnote'} )
753 . $dbh->quote( $data{'B_address'} )
755 . $dbh->quote( $data{'B_zipcode'} )
757 . $dbh->quote( $data{'B_city'} )
759 . $dbh->quote( $data{'B_phone'} )
761 . $dbh->quote( $data{'B_email'}, )
763 . $dbh->quote( $data{'password'} )
765 . $dbh->quote( $data{'userid'} )
767 . $dbh->quote( $data{'sort1'} )
769 . $dbh->quote( $data{'sort2'} )
771 . $dbh->quote( $data{'contacttitle'} )
773 . $dbh->quote( $data{'emailpro'} )
774 . ",contactfirstname="
775 . $dbh->quote( $data{'contactfirstname'} ) . ",sex="
776 . $dbh->quote( $data{'sex'} ) . ",fax="
777 . $dbh->quote( $data{'fax'} )
779 . $dbh->quote( $data{'relationship'} )
781 . $dbh->quote( $data{'B_streetnumber'} )
783 . $dbh->quote( $data{'B_streettype'} )
785 . $dbh->quote( $data{'gonenoaddress'} )
787 . $dbh->quote( $data{'lost'} )
789 . $dbh->quote( $data{'debarred'} )
791 . $dbh->quote( $data{'ethnicity'} )
793 . $dbh->quote( $data{'ethnotes'} );
795 my $sth = $dbh->prepare($query);
798 $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
800 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
801 if C4::Context->preference("BorrowersLog");
803 return $data{'borrowernumber'};
807 my ( $uid, $member, $digest ) = @_;
808 my $dbh = C4::Context->dbh;
810 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
811 #Then we need to tell the user and have them create a new one.
814 "select * from borrowers where userid=? and borrowernumber != ?");
815 $sth->execute( $uid, $member );
816 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
820 #Everything is good so we can update the information.
823 "update borrowers set userid=?, password=? where borrowernumber=?");
824 $sth->execute( $uid, $digest, $member );
828 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
829 if C4::Context->preference("BorrowersLog");
834 =item fixup_cardnumber
836 Warning: The caller is responsible for locking the members table in write
837 mode, to avoid database corruption.
841 use vars qw( @weightings );
842 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
844 sub fixup_cardnumber ($) {
845 my ($cardnumber) = @_;
846 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
847 $autonumber_members = 0 unless defined $autonumber_members;
849 # Find out whether member numbers should be generated
850 # automatically. Should be either "1" or something else.
851 # Defaults to "0", which is interpreted as "no".
853 # if ($cardnumber !~ /\S/ && $autonumber_members) {
854 if ($autonumber_members) {
855 my $dbh = C4::Context->dbh;
856 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
858 # if checkdigit is selected, calculate katipo-style cardnumber.
859 # otherwise, just use the max()
860 # purpose: generate checksum'd member numbers.
861 # We'll assume we just got the max value of digits 2-8 of member #'s
862 # from the database and our job is to increment that by one,
863 # determine the 1st and 9th digits and return the full string.
866 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
870 my $data = $sth->fetchrow_hashref;
871 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
873 if ( !$cardnumber ) { # If DB has no values,
874 $cardnumber = 1000000; # start at 1000000
881 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
883 # read weightings, left to right, 1 char at a time
884 my $temp1 = $weightings[$i];
886 # sequence left to right, 1 char at a time
887 my $temp2 = substr( $cardnumber, $i, 1 );
889 # mult each char 1-7 by its corresponding weighting
890 $sum += $temp1 * $temp2;
893 my $rem = ( $sum % 11 );
894 $rem = 'X' if $rem == 10;
896 $cardnumber = "V$cardnumber$rem";
900 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
901 # better. I'll leave the original in in case it needs to be changed for you
904 "select max(cast(cardnumber as signed)) from borrowers");
906 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
910 my ($result) = $sth->fetchrow;
912 $cardnumber = $result + 1;
920 ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
921 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
922 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
924 C<&GetGuarantees> takes a borrower number (e.g., that of a patron
925 with children) and looks up the borrowers who are guaranteed by that
926 borrower (i.e., the patron's children).
928 C<&GetGuarantees> returns two values: an integer giving the number of
929 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
930 of references to hash, which gives the actual results.
936 my ($borrowernumber) = @_;
937 my $dbh = C4::Context->dbh;
940 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
942 $sth->execute($borrowernumber);
945 my $data = $sth->fetchall_arrayref({});
947 return ( scalar(@$data), $data );
950 =head2 UpdateGuarantees
952 &UpdateGuarantees($parent_borrno);
955 C<&UpdateGuarantees> borrower data for an adulte and updates all the guarantees
956 with the modified information
961 sub UpdateGuarantees {
963 my $dbh = C4::Context->dbh;
964 my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
965 for ( my $i = 0 ; $i < $count ; $i++ ) {
968 # It looks like the $i is only being returned to handle walking through
969 # the array, which is probably better done as a foreach loop.
971 my $guaquery = qq|UPDATE borrowers
972 SET address='$data{'address'}',fax='$data{'fax'}',
973 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
974 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
976 my $sth3 = $dbh->prepare($guaquery);
981 =head2 GetPendingIssues
983 ($count, $issues) = &GetPendingIssues($borrowernumber);
985 Looks up what the patron with the given borrowernumber has borrowed.
987 C<&GetPendingIssues> returns a two-element array. C<$issues> is a
988 reference-to-array, where each element is a reference-to-hash; the
989 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
990 in the Koha database. C<$count> is the number of elements in
996 sub GetPendingIssues {
997 my ($borrowernumber) = @_;
998 my $dbh = C4::Context->dbh;
999 my $sth = $dbh->prepare(
1000 "Select * from issues,biblio,items where borrowernumber=?
1001 and items.itemnumber=issues.itemnumber
1002 and items.biblionumber=biblio.biblionumber
1003 and issues.returndate is NULL order by date_due"
1005 $sth->execute($borrowernumber);
1006 my $data = $sth->fetchall_arrayref({});
1007 return ( scalar(@$data), $data );
1012 ($count, $issues) = &GetAllIssues($borrowernumber, $sortkey, $limit);
1014 Looks up what the patron with the given borrowernumber has borrowed,
1015 and sorts the results.
1017 C<$sortkey> is the name of a field on which to sort the results. This
1018 should be the name of a field in the C<issues>, C<biblio>,
1019 C<biblioitems>, or C<items> table in the Koha database.
1021 C<$limit> is the maximum number of results to return.
1023 C<&GetAllIssues> returns a two-element array. C<$issues> is a
1024 reference-to-array, where each element is a reference-to-hash; the
1025 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1026 C<items> tables of the Koha database. C<$count> is the number of
1027 elements in C<$issues>
1033 my ( $borrowernumber, $order, $limit ) = @_;
1035 #FIXME: sanity-check order and limit
1036 my $dbh = C4::Context->dbh;
1039 "Select *,items.timestamp AS itemstimestamp from issues,biblio,items,biblioitems
1040 where borrowernumber=? and
1041 items.biblioitemnumber=biblioitems.biblioitemnumber and
1042 items.itemnumber=issues.itemnumber and
1043 items.biblionumber=biblio.biblionumber order by $order";
1044 if ( $limit != 0 ) {
1045 $query .= " limit $limit";
1049 my $sth = $dbh->prepare($query);
1050 $sth->execute($borrowernumber);
1053 while ( my $data = $sth->fetchrow_hashref ) {
1054 $result[$i] = $data;
1059 # get all issued items for borrowernumber from oldissues table
1060 # large chunk of older issues data put into table oldissues
1061 # to speed up db calls for issuing items
1062 if ( C4::Context->preference("ReadingHistory") ) {
1063 my $query2 = "SELECT * FROM oldissues,biblio,items,biblioitems
1064 WHERE borrowernumber=?
1065 AND items.biblioitemnumber=biblioitems.biblioitemnumber
1066 AND items.itemnumber=oldissues.itemnumber
1067 AND items.biblionumber=biblio.biblionumber
1069 if ( $limit != 0 ) {
1070 $limit = $limit - $count;
1071 $query2 .= " limit $limit";
1074 my $sth2 = $dbh->prepare($query2);
1075 $sth2->execute($borrowernumber);
1077 while ( my $data2 = $sth2->fetchrow_hashref ) {
1078 $result[$i] = $data2;
1085 return ( $i, \@result );
1089 =head2 GetMemberAccountRecords
1091 ($total, $acctlines, $count) = &GetMemberAccountRecords($borrowernumber);
1093 Looks up accounting data for the patron with the given borrowernumber.
1095 C<&GetMemberAccountRecords> returns a three-element array. C<$acctlines> is a
1096 reference-to-array, where each element is a reference-to-hash; the
1097 keys are the fields of the C<accountlines> table in the Koha database.
1098 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1099 total amount outstanding for all of the account lines.
1104 sub GetMemberAccountRecords {
1105 my ($borrowernumber,$date) = @_;
1106 my $dbh = C4::Context->dbh;
1112 WHERE borrowernumber=?);
1113 my @bind = ($borrowernumber);
1114 if ($date && $date ne ''){
1120 ORDER BY date desc,timestamp DESC";
1121 my $sth= $dbh->prepare( $strsth );
1122 $sth->execute( @bind );
1124 while ( my $data = $sth->fetchrow_hashref ) {
1125 $acctlines[$numlines] = $data;
1127 $total += $data->{'amountoutstanding'};
1130 return ( $total, \@acctlines,$numlines);
1133 =head2 GetBorNotifyAcctRecord
1135 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1137 Looks up accounting data for the patron with the given borrowernumber per file number.
1139 (FIXME - I'm not at all sure what this is about.)
1141 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1142 reference-to-array, where each element is a reference-to-hash; the
1143 keys are the fields of the C<accountlines> table in the Koha database.
1144 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1145 total amount outstanding for all of the account lines.
1149 sub GetBorNotifyAcctRecord {
1150 my ( $borrowernumber, $notifyid ) = @_;
1151 my $dbh = C4::Context->dbh;
1154 my $query = qq| SELECT *
1156 WHERE borrowernumber=?
1158 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')
1159 AND amountoutstanding != '0'
1160 ORDER BY notify_id,accounttype
1162 my $sth = $dbh->prepare($query);
1164 $sth->execute( $borrowernumber, $notifyid );
1166 while ( my $data = $sth->fetchrow_hashref ) {
1167 $acctlines[$numlines] = $data;
1169 $total += $data->{'amountoutstanding'};
1172 return ( $total, \@acctlines, $numlines );
1175 =head2 checkuniquemember (OUEST-PROVENCE)
1177 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
1179 Checks that a member exists or not in the database.
1181 C<&result> is 1 (=exist) or 0 (=does not exist)
1182 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1183 C<&surname> is the surname
1184 C<&categorycode> is from categorycode table
1185 C<&firstname> is the firstname (only if collectivity=0)
1186 C<&dateofbirth> is the date of birth (only if collectivity=0)
1190 sub checkuniquemember {
1191 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1192 my $dbh = C4::Context->dbh;
1194 if ($collectivity) {
1196 # $request="select count(*) from borrowers where surname=? and categorycode=?";
1198 "select borrowernumber,categorycode from borrowers where surname=? ";
1202 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1204 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1206 my $sth = $dbh->prepare($request);
1207 if ($collectivity) {
1208 $sth->execute( uc($surname) );
1211 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1213 my @data = $sth->fetchrow;
1216 return $data[0], $data[1];
1226 =head2 getzipnamecity (OUEST-PROVENCE)
1228 take all info from table city for the fields city and zip
1229 check for the name and the zip code of the city selected
1233 sub getzipnamecity {
1235 my $dbh = C4::Context->dbh;
1238 "select city_name,city_zipcode from cities where cityid=? ");
1239 $sth->execute($cityid);
1240 my @data = $sth->fetchrow;
1241 return $data[0], $data[1];
1245 =head2 getdcity (OUEST-PROVENCE)
1247 recover cityid with city_name condition
1252 my ($city_name) = @_;
1253 my $dbh = C4::Context->dbh;
1254 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1255 $sth->execute($city_name);
1256 my $data = $sth->fetchrow;
1261 =head2 GetExpiryDate
1263 $expirydate = GetExpiryDate($categorycode, $dateenrolled);
1264 process expiry date given a date and a categorycode
1268 my ( $categorycode, $dateenrolled ) = @_;
1269 my $dbh = C4::Context->dbh;
1272 "select enrolmentperiod from categories where categorycode=?");
1273 $sth->execute($categorycode);
1274 my ($enrolmentperiod) = $sth->fetchrow;
1275 $enrolmentperiod = 12 unless ($enrolmentperiod);
1276 my @date=split /-/,format_date_in_iso($dateenrolled);
1277 @date=Add_Delta_YM($date[0],$date[1],$date[2],0,$enrolmentperiod);
1278 return sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
1281 =head2 checkuserpassword (OUEST-PROVENCE)
1283 check for the password and login are not used
1284 return the number of record
1285 0=> NOT USED 1=> USED
1289 sub checkuserpassword {
1290 my ( $borrowernumber, $userid, $password ) = @_;
1291 $password = md5_base64($password);
1292 my $dbh = C4::Context->dbh;
1295 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1297 $sth->execute( $borrowernumber, $userid, $password );
1298 my $number_rows = $sth->fetchrow;
1299 return $number_rows;
1303 =head2 GetborCatFromCatType
1305 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1307 Looks up the different types of borrowers in the database. Returns two
1308 elements: a reference-to-array, which lists the borrower category
1309 codes, and a reference-to-hash, which maps the borrower category codes
1310 to category descriptions.
1315 sub GetborCatFromCatType {
1316 my ( $category_type, $action ) = @_;
1317 my $dbh = C4::Context->dbh;
1318 my $request = qq| SELECT categorycode,description
1321 ORDER BY categorycode|;
1322 my $sth = $dbh->prepare($request);
1324 $sth->execute($category_type);
1333 while ( my $data = $sth->fetchrow_hashref ) {
1334 push @codes, $data->{'categorycode'};
1335 $labels{ $data->{'categorycode'} } = $data->{'description'};
1338 return ( \@codes, \%labels );
1341 =head2 GetBorrowercategory
1343 $hashref = &GetBorrowercategory($categorycode);
1345 Given the borrower's category code, the function returns the corresponding
1346 data hashref for a comprehensive information display.
1350 sub GetBorrowercategory {
1352 my $dbh = C4::Context->dbh;
1355 "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?"
1357 $sth->execute($catcode);
1359 $sth->fetchrow_hashref;
1362 } # sub getborrowercategory
1364 =head2 ethnicitycategories
1366 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1368 Looks up the different ethnic types in the database. Returns two
1369 elements: a reference-to-array, which lists the ethnicity codes, and a
1370 reference-to-hash, which maps the ethnicity codes to ethnicity
1377 sub ethnicitycategories {
1378 my $dbh = C4::Context->dbh;
1379 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1383 while ( my $data = $sth->fetchrow_hashref ) {
1384 push @codes, $data->{'code'};
1385 $labels{ $data->{'code'} } = $data->{'name'};
1388 return ( \@codes, \%labels );
1393 $ethn_name = &fixEthnicity($ethn_code);
1395 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1396 corresponding descriptive name from the C<ethnicity> table in the
1397 Koha database ("European" or "Pacific Islander").
1404 my $ethnicity = shift;
1405 return unless $ethnicity;
1406 my $dbh = C4::Context->dbh;
1407 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1408 $sth->execute($ethnicity);
1409 my $data = $sth->fetchrow_hashref;
1411 return $data->{'name'};
1412 } # sub fixEthnicity
1416 $dateofbirth,$date = &GetAge($date);
1418 this function return the borrowers age with the value of dateofbirth
1424 my ( $date, $date_ref ) = @_;
1426 if ( not defined $date_ref ) {
1427 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1430 my ( $year1, $month1, $day1 ) = split /-/, $date;
1431 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1433 my $age = $year2 - $year1;
1434 if ( $month1 . $day1 > $month2 . $day2 ) {
1441 =head2 get_institutions
1442 $insitutions = get_institutions();
1444 Just returns a list of all the borrowers of type I, borrownumber and name
1449 sub get_institutions {
1450 my $dbh = C4::Context->dbh();
1453 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1457 while ( my $data = $sth->fetchrow_hashref() ) {
1458 $orgs{ $data->{'borrowernumber'} } = $data;
1463 } # sub get_institutions
1465 =head2 add_member_orgs
1467 add_member_orgs($borrowernumber,$borrowernumbers);
1469 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1474 sub add_member_orgs {
1475 my ( $borrowernumber, $otherborrowers ) = @_;
1476 my $dbh = C4::Context->dbh();
1478 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1479 my $sth = $dbh->prepare($query);
1480 foreach my $otherborrowernumber (@$otherborrowers) {
1481 $sth->execute( $borrowernumber, $otherborrowernumber );
1485 } # sub add_member_orgs
1487 =head2 GetMembersFromSurname
1491 \@resutlts = GetMembersFromSurname($surname)
1492 this function get the list of borrower names like $surname.
1494 the table of results in @results
1500 sub GetMembersFromSurname {
1501 my ($searchstring) = @_;
1502 my $dbh = C4::Context->dbh;
1503 $searchstring =~ s/\'/\\\'/g;
1504 my @data = split( ' ', $searchstring );
1507 SELECT surname,firstname
1509 WHERE (surname like ?)
1512 my $sth = $dbh->prepare($query);
1513 $sth->execute("$data[0]%");
1517 my $data = $sth->fetchall_arrayref({});
1519 return ( scalar(@$data), $data );
1522 =head2 GetCities (OUEST-PROVENCE)
1524 ($id_cityarrayref, $city_hashref) = &GetCities();
1526 Looks up the different city and zip in the database. Returns two
1527 elements: a reference-to-array, which lists the zip city
1528 codes, and a reference-to-hash, which maps the name of the city.
1529 WHERE =>OUEST PROVENCE OR EXTERIEUR
1535 #my ($type_city) = @_;
1536 my $dbh = C4::Context->dbh;
1537 my $query = qq|SELECT cityid,city_name
1539 ORDER BY city_name|;
1540 my $sth = $dbh->prepare($query);
1542 #$sth->execute($type_city);
1547 # insert empty value to create a empty choice in cgi popup
1549 while ( my $data = $sth->fetchrow_hashref ) {
1551 push @id, $data->{'cityid'};
1552 $city{ $data->{'cityid'} } = $data->{'city_name'};
1555 #test to know if the table contain some records if no the function return nothing
1563 return ( \@id, \%city );
1567 =head2 GetSortDetails (OUEST-PROVENCE)
1569 ($lib) = &GetSortDetails($category,$sortvalue);
1571 Returns the authorized value details
1572 C<&$lib>return value of authorized value details
1573 C<&$sortvalue>this is the value of authorized value
1574 C<&$category>this is the value of authorized value category
1578 sub GetSortDetails {
1579 my ( $category, $sortvalue ) = @_;
1580 my $dbh = C4::Context->dbh;
1581 my $query = qq|SELECT lib
1582 FROM authorised_values
1584 AND authorised_value=? |;
1585 my $sth = $dbh->prepare($query);
1586 $sth->execute( $category, $sortvalue );
1587 my $lib = $sth->fetchrow;
1591 =head2 DeleteBorrower
1593 () = &DeleteBorrower($member);
1595 delete all data fo borrowers and add record to deletedborrowers table
1596 C<&$member>this is the borrowernumber
1600 sub MoveMemberToDeleted {
1602 my $dbh = C4::Context->dbh;
1604 $query = qq|SELECT *
1606 WHERE borrowernumber=?|;
1607 my $sth = $dbh->prepare($query);
1608 $sth->execute($member);
1609 my @data = $sth->fetchrow_array;
1612 $dbh->prepare( "Insert into deletedborrowers values ("
1613 . ( "?," x ( scalar(@data) - 1 ) )
1615 $sth->execute(@data);
1621 DelMember($borrowernumber);
1623 This function remove directly a borrower whitout writing it on deleteborrower.
1624 + Deletes reserves for the borrower
1629 my $dbh = C4::Context->dbh;
1630 my $borrowernumber = shift;
1631 return unless $borrowernumber; # borrowernumber is mandatory.
1633 my $query = qq|DELETE
1635 WHERE borrowernumber=?|;
1636 my $sth = $dbh->prepare($query);
1637 $sth->execute($borrowernumber);
1642 WHERE borrowernumber = ?
1644 my $sth = $dbh->prepare($query);
1645 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$borrowernumber,"")
1646 if C4::Context->preference("BorrowersLog");
1650 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1652 $date= ExtendMemberSubscriptionTo($borrowerid, $date);
1653 Extending the subscription to a given date or to the expiry date calculated on local date.
1657 sub ExtendMemberSubscriptionTo {
1658 my ( $borrowerid,$date) = @_;
1659 my $dbh = C4::Context->dbh;
1661 $date=POSIX::strftime("%Y-%m-%d",localtime(time));
1662 my $borrower = GetBorrower($borrowerid,'borrowernumber');
1663 $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
1665 my $sth = $dbh->do(<<EOF);
1667 SET dateexpiry='$date'
1668 WHERE borrowernumber='$borrowerid'
1670 return $date if ($sth);
1674 =head2 GetRoadTypes (OUEST-PROVENCE)
1676 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1678 Looks up the different road type . Returns two
1679 elements: a reference-to-array, which lists the id_roadtype
1680 codes, and a reference-to-hash, which maps the road type of the road .
1686 my $dbh = C4::Context->dbh;
1687 my $query = qq|SELECT roadtypeid,road_type
1689 ORDER BY road_type|;
1690 my $sth = $dbh->prepare($query);
1695 # insert empty value to create a empty choice in cgi popup
1697 while ( my $data = $sth->fetchrow_hashref ) {
1699 push @id, $data->{'roadtypeid'};
1700 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1703 #test to know if the table contain some records if no the function return nothing
1711 return ( \@id, \%roadtype );
1717 =head2 GetTitles (OUEST-PROVENCE)
1719 ($borrowertitle)= &GetTitles();
1721 Looks up the different title . Returns array with all borrowers title
1726 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1727 unshift( @borrowerTitle, "" );
1728 return ( \@borrowerTitle);
1733 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1735 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1737 Returns the description of roadtype
1738 C<&$roadtype>return description of road type
1739 C<&$roadtypeid>this is the value of roadtype s
1743 sub GetRoadTypeDetails {
1744 my ($roadtypeid) = @_;
1745 my $dbh = C4::Context->dbh;
1746 my $query = qq|SELECT road_type
1748 WHERE roadtypeid=?|;
1749 my $sth = $dbh->prepare($query);
1750 $sth->execute($roadtypeid);
1751 my $roadtype = $sth->fetchrow;
1755 =head2 GetBorrowersWhoHaveNotBorrowedSince
1757 &GetBorrowersWhoHaveNotBorrowedSince($date)
1759 this function get all borrowers who haven't borrowed since the date given on input arg.
1763 sub GetBorrowersWhoHaveNotBorrowedSince {
1765 return unless $date; # date is mandatory.
1766 my $dbh = C4::Context->dbh;
1768 SELECT borrowers.borrowernumber,max(timestamp)
1770 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1771 WHERE issues.borrowernumber IS NOT NULL
1772 GROUP BY borrowers.borrowernumber
1774 my $sth = $dbh->prepare($query);
1778 while ( my $data = $sth->fetchrow_hashref ) {
1779 push @results, $data;
1784 =head2 GetBorrowersWhoHaveNeverBorrowed
1786 $results = &GetBorrowersWhoHaveNeverBorrowed
1788 this function get all borrowers who have never borrowed.
1790 I<$result> is a ref to an array which all elements are a hasref.
1794 sub GetBorrowersWhoHaveNeverBorrowed {
1795 my $dbh = C4::Context->dbh;
1797 SELECT borrowers.borrowernumber,max(timestamp)
1799 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1800 WHERE issues.borrowernumber IS NULL
1802 my $sth = $dbh->prepare($query);
1805 while ( my $data = $sth->fetchrow_hashref ) {
1806 push @results, $data;
1811 =head2 GetBorrowersWithIssuesHistoryOlderThan
1813 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1815 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1817 I<$result> is a ref to an array which all elements are a hashref.
1818 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
1822 sub GetBorrowersWithIssuesHistoryOlderThan {
1823 my $dbh = C4::Context->dbh;
1825 return unless $date; # date is mandatory.
1827 SELECT count(borrowernumber) as n,borrowernumber
1829 WHERE returndate < ?
1830 AND borrowernumber IS NOT NULL
1831 GROUP BY borrowernumber
1833 my $sth = $dbh->prepare($query);
1834 $sth->execute($date);
1837 while ( my $data = $sth->fetchrow_hashref ) {
1838 push @results, $data;
1843 END { } # module clean-up code here (global destructor)