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,$category_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 $category_type is used to get a specified type of user.
132 (mainly adults when creating a child.)
134 C<$searchstring> is a space-separated list of search terms. Each term
135 must match the beginning a borrower's surname, first name, or other
138 C<&SearchBorrower> returns a two-element list. C<$borrowers> is a
139 reference-to-array; each element is a reference-to-hash, whose keys
140 are the fields of the C<borrowers> table in the Koha database.
141 C<$count> is the number of elements in C<$borrowers>.
146 #used by member enquiries from the intranet
149 my ($searchstring, $orderby, $type,$category_type ) = @_;
150 my $dbh = C4::Context->dbh;
156 if ( $type eq "simple" ) # simple search for one letter only
159 "SELECT * FROM borrowers
160 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode ".
161 ($category_type?" AND category_type = ".$dbh->quote($category_type):"").
162 " WHERE surname LIKE ? ORDER BY $orderby";
163 @bind = ("$searchstring%");
165 else # advanced search looking in surname, firstname and othernames
167 @data = split( ' ', $searchstring );
169 $query = "SELECT * FROM borrowers
170 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
171 WHERE ((surname LIKE ? OR surname LIKE ?
172 OR firstname LIKE ? OR firstname LIKE ?
173 OR othernames LIKE ? OR othernames LIKE ?)
175 ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
177 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
178 "$data[0]%", "% $data[0]%"
180 for ( my $i = 1 ; $i < $count ; $i++ ) {
181 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
182 OR firstname LIKE ? OR firstname LIKE ?
183 OR othernames LIKE ? OR othernames LIKE ?)";
185 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
186 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
190 $query = $query . ") OR cardnumber LIKE ?
192 push( @bind, $searchstring );
197 my $sth = $dbh->prepare($query);
199 # warn "Q $orderby : $query";
200 $sth->execute(@bind);
202 my $cnt = $sth->rows;
203 while ( my $data = $sth->fetchrow_hashref ) {
204 push( @results, $data );
209 return ( $cnt, \@results );
212 =head2 GetMemberDetails
214 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
216 Looks up a patron and returns information about him or her. If
217 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
218 up the borrower by number; otherwise, it looks up the borrower by card
221 C<$borrower> is a reference-to-hash whose keys are the fields of the
222 borrowers table in the Koha database. In addition,
223 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
224 about the patron. Its keys act as flags :
226 if $borrower->{flags}->{LOST} {
227 # Patron's card was reported lost
230 Each flag has a C<message> key, giving a human-readable explanation of
231 the flag. If the state of a flag means that the patron should not be
232 allowed to borrow any more books, then it will have a C<noissues> key
235 The possible flags are:
241 =item Shows the patron's credit or debt, if any.
249 =item (Gone, no address.) Set if the patron has left without giving a
258 =item Set if the patron's card has been reported as lost.
266 =item Set if the patron has been debarred.
274 =item Any additional notes about the patron.
282 =item Set if the patron has overdue items. This flag has several keys:
284 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
285 overdue items. Its elements are references-to-hash, each describing an
286 overdue item. The keys are selected fields from the issues, biblio,
287 biblioitems, and items tables of the Koha database.
289 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
290 the overdue items, one per line.
298 =item Set if any items that the patron has reserved are available.
300 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
301 available items. Each element is a reference-to-hash whose keys are
302 fields from the reserves table of the Koha database.
308 sub GetMemberDetails {
309 my ( $borrowernumber, $cardnumber ) = @_;
310 my $dbh = C4::Context->dbh;
313 if ($borrowernumber) {
314 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
315 $sth->execute($borrowernumber);
317 elsif ($cardnumber) {
318 $sth = $dbh->prepare("select * from borrowers where cardnumber=?");
319 $sth->execute($cardnumber);
324 my $borrower = $sth->fetchrow_hashref;
325 my ($amount) = GetBorrowerAcctRecord( $borrowernumber);
326 $borrower->{'amountoutstanding'} = $amount;
327 my $flags = patronflags( $borrower, $dbh );
330 $sth = $dbh->prepare("select bit,flag from userflags");
332 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
333 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
334 $accessflagshash->{$flag} = 1;
338 $borrower->{'flags'} = $flags;
339 $borrower->{'authflags'} = $accessflagshash;
341 # find out how long the membership lasts
344 "select enrolmentperiod from categories where categorycode = ?");
345 $sth->execute( $borrower->{'categorycode'} );
346 my $enrolment = $sth->fetchrow;
347 $borrower->{'enrolmentperiod'} = $enrolment;
348 return ($borrower); #, $flags, $accessflagshash);
355 NOTE!: If you change this function, be sure to update the POD for
358 $flags = &patronflags($patron, $dbh);
361 {message} Message showing patron's credit or debt
362 {noissues} Set if patron owes >$5.00
363 {GNA} Set if patron gone w/o address
364 {message} "Borrower has no valid address"
366 {LOST} Set if patron's card reported lost
367 {message} Message to this effect
369 {DBARRED} Set is patron is debarred
370 {message} Message to this effect
372 {NOTES} Set if patron has notes
373 {message} Notes about patron
374 {ODUES} Set if patron has overdue books
376 {itemlist} ref-to-array: list of overdue books
377 {itemlisttext} Text list of overdue items
378 {WAITING} Set if there are items available that the
380 {message} Message to this effect
381 {itemlist} ref-to-array: list of available items
387 my ( $patroninformation) = @_;
388 my $dbh=C4::Context->dbh;
389 my ($amount) = GetBorrowerAcctRecord( $patroninformation->{'borrowernumber'});
392 my $noissuescharge = C4::Context->preference("noissuescharge");
393 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
394 if ( $amount > $noissuescharge ) {
395 $flaginfo{'noissues'} = 1;
397 $flags{'CHARGES'} = \%flaginfo;
399 elsif ( $amount < 0 ) {
401 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
402 $flags{'CHARGES'} = \%flaginfo;
404 if ( $patroninformation->{'gonenoaddress'}
405 && $patroninformation->{'gonenoaddress'} == 1 )
408 $flaginfo{'message'} = 'Borrower has no valid address.';
409 $flaginfo{'noissues'} = 1;
410 $flags{'GNA'} = \%flaginfo;
412 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
414 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
415 $flaginfo{'noissues'} = 1;
416 $flags{'LOST'} = \%flaginfo;
418 if ( $patroninformation->{'debarred'}
419 && $patroninformation->{'debarred'} == 1 )
422 $flaginfo{'message'} = 'Borrower is Debarred.';
423 $flaginfo{'noissues'} = 1;
424 $flags{'DBARRED'} = \%flaginfo;
426 if ( $patroninformation->{'borrowernotes'}
427 && $patroninformation->{'borrowernotes'} )
430 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
431 $flags{'NOTES'} = \%flaginfo;
433 my ( $odues, $itemsoverdue ) =
434 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
437 $flaginfo{'message'} = "Yes";
438 $flaginfo{'itemlist'} = $itemsoverdue;
439 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
442 $flaginfo{'itemlisttext'} .=
443 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
445 $flags{'ODUES'} = \%flaginfo;
448 GetWaitingReserves( $patroninformation->{'borrowernumber'} );
449 my $nowaiting = scalar @$itemswaiting;
450 if ( $nowaiting > 0 ) {
452 $flaginfo{'message'} = "Reserved items available";
453 $flaginfo{'itemlist'} = $itemswaiting;
454 $flags{'WAITING'} = \%flaginfo;
462 $borrower = &GetMember($information, $type);
464 Looks up information about a patron (borrower) by either card number
465 ,firstname, or borrower number, depending on $type value.
466 If C<$type> == 'cardnumber', C<&GetBorrower>
467 searches by cardnumber then by firstname if not found in cardnumber;
468 otherwise, it searches by borrowernumber.
470 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
471 the C<borrowers> table in the Koha database.
477 my ( $information, $type ) = @_;
478 my $dbh = C4::Context->dbh;
480 if ($type eq 'cardnumber' || $type eq 'firstname'|| $type eq 'userid'|| $type eq 'borrowernumber'){
481 $information = uc $information;
484 "Select borrowers.*,categories.category_type,categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where $type=?"
486 $sth->execute($information);
491 "Select borrowers.*,categories.category_type, categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
493 $sth->execute($information);
495 my $data = $sth->fetchrow_hashref;
501 elsif ($type eq 'cardnumber' ||$type eq 'firstname') { # try with firstname
504 "Select borrowers.*,categories.category_type,categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname like ?"
506 $sth->execute($information);
507 my $data = $sth->fetchrow_hashref;
516 =item GetBorrowerIssuesAndFines
518 ($borrowed, $due, $fine) = &GetBorrowerIssuesAndFines($borrowernumber);
520 Returns aggregate data about items borrowed by the patron with the
521 given borrowernumber.
523 C<&GetBorrowerIssuesAndFines> returns a three-element array. C<$borrowed> is the
524 number of books the patron currently has borrowed. C<$due> is the
525 number of overdue items the patron currently has borrowed. C<$fine> is
526 the total fine currently due by the borrower.
531 sub GetBorrowerIssuesAndFines {
532 my ( $borrowernumber ) = @_;
533 my $dbh = C4::Context->dbh;
535 "Select count(*) from issues where borrowernumber='$borrowernumber' and
539 my $sth = $dbh->prepare($query);
541 my $data = $sth->fetchrow_hashref;
543 $sth = $dbh->prepare(
544 "Select count(*) from issues where
545 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
548 my $data2 = $sth->fetchrow_hashref;
550 $sth = $dbh->prepare(
551 "Select sum(amountoutstanding) from accountlines where
552 borrowernumber='$borrowernumber'"
555 my $data3 = $sth->fetchrow_hashref;
558 return ( $data2->{'count(*)'}, $data->{'count(*)'},
559 $data3->{'sum(amountoutstanding)'} );
566 &ModMember($borrowernumber);
568 Modify borrower's data
575 my $dbh = C4::Context->dbh;
576 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
577 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
578 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
580 # warn "num user".$data{'borrowernumber'};
583 $data{'userid'} = '' if ( $data{'password'} eq '' );
585 # test to know if u must update or not the borrower password
586 if ( $data{'password'} eq '****' ) {
588 $query = "UPDATE borrowers SET
589 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
590 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
591 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
592 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
593 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
594 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
595 ethnotes = ?,sex = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
596 WHERE borrowernumber=$data{'borrowernumber'}";
597 $sth = $dbh->prepare($query);
599 $data{'cardnumber'}, $data{'surname'},
600 $data{'firstname'}, $data{'title'},
601 $data{'othernames'}, $data{'initials'},
602 $data{'streetnumber'}, $data{'streettype'},
603 $data{'address'}, $data{'address2'},
604 $data{'city'}, $data{'zipcode'},
605 $data{'email'}, $data{'phone'},
606 $data{'mobile'}, $data{'fax'},
607 $data{'emailpro'}, $data{'phonepro'},
608 $data{'B_streetnumber'}, $data{'B_streettype'},
609 $data{'B_address'}, $data{'B_city'},
610 $data{'B_zipcode'}, $data{'B_email'},
611 $data{'B_phone'}, $data{'dateofbirth'},
612 $data{'branchcode'}, $data{'categorycode'},
613 $data{'dateenrolled'}, $data{'dateexpiry'},
614 $data{'gonenoaddress'}, $data{'lost'},
615 $data{'debarred'}, $data{'contactname'},
616 $data{'contactfirstname'}, $data{'contacttitle'},
617 $data{'guarantorid'}, $data{'borrowernotes'},
618 $data{'relationship'}, $data{'ethnicity'},
619 $data{'ethnotes'}, $data{'sex'},
620 $data{'userid'}, $data{'opacnote'},
621 $data{'contactnote'}, $data{'sort1'},
627 ( $data{'password'} = md5_base64( $data{'password'} ) )
628 if ( $data{'password'} ne '' );
629 $query = "UPDATE borrowers SET
630 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
631 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
632 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
633 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
634 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
635 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
636 ethnotes = ?,sex = ?,password = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
637 WHERE borrowernumber=$data{'borrowernumber'}";
638 $sth = $dbh->prepare($query);
640 $data{'cardnumber'}, $data{'surname'},
641 $data{'firstname'}, $data{'title'},
642 $data{'othernames'}, $data{'initials'},
643 $data{'streetnumber'}, $data{'streettype'},
644 $data{'address'}, $data{'address2'},
645 $data{'city'}, $data{'zipcode'},
646 $data{'email'}, $data{'phone'},
647 $data{'mobile'}, $data{'fax'},
648 $data{'emailpro'}, $data{'phonepro'},
649 $data{'B_streetnumber'}, $data{'B_streettype'},
650 $data{'B_address'}, $data{'B_city'},
651 $data{'B_zipcode'}, $data{'B_email'},
652 $data{'B_phone'}, $data{'dateofbirth'},
653 $data{'branchcode'}, $data{'categorycode'},
654 $data{'dateenrolled'}, $data{'dateexpiry'},
655 $data{'gonenoaddress'}, $data{'lost'},
656 $data{'debarred'}, $data{'contactname'},
657 $data{'contactfirstname'}, $data{'contacttitle'},
658 $data{'guarantorid'}, $data{'borrowernotes'},
659 $data{'relationship'}, $data{'ethnicity'},
660 $data{'ethnotes'}, $data{'sex'},
661 $data{'password'}, $data{'userid'},
662 $data{'opacnote'}, $data{'contactnote'},
663 $data{'sort1'}, $data{'sort2'}
668 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
669 # so when we update information for an adult we should check for guarantees and update the relevant part
670 # of their records, ie addresses and phone numbers
671 my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
672 if ( $borrowercategory->{'category_type'} eq 'A' ) {
673 # is adult check guarantees;
674 UpdateGuarantees(%data);
677 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
678 if C4::Context->preference("BorrowersLog");
685 $borrowernumber = &AddMember(%borrower);
687 insert new borrower into table
688 Returns the borrowernumber
695 my $dbh = C4::Context->dbh;
696 $data{'userid'} = '' unless $data{'password'};
697 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
698 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
699 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
700 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
702 "insert into borrowers set cardnumber="
703 . $dbh->quote( $data{'cardnumber'} )
705 . $dbh->quote( $data{'surname'} )
707 . $dbh->quote( $data{'firstname'} )
709 . $dbh->quote( $data{'title'} )
711 . $dbh->quote( $data{'othernames'} )
713 . $dbh->quote( $data{'initials'} )
715 . $dbh->quote( $data{'streetnumber'} )
717 . $dbh->quote( $data{'streettype'} )
719 . $dbh->quote( $data{'address'} )
721 . $dbh->quote( $data{'address2'} )
723 . $dbh->quote( $data{'zipcode'} )
725 . $dbh->quote( $data{'city'} )
727 . $dbh->quote( $data{'phone'} )
729 . $dbh->quote( $data{'email'} )
731 . $dbh->quote( $data{'mobile'} )
733 . $dbh->quote( $data{'phonepro'} )
735 . $dbh->quote( $data{'opacnote'} )
737 . $dbh->quote( $data{'guarantorid'} )
739 . $dbh->quote( $data{'dateofbirth'} )
741 . $dbh->quote( $data{'branchcode'} )
743 . $dbh->quote( $data{'categorycode'} )
745 . $dbh->quote( $data{'dateenrolled'} )
747 . $dbh->quote( $data{'contactname'} )
749 . $dbh->quote( $data{'borrowernotes'} )
751 . $dbh->quote( $data{'dateexpiry'} )
753 . $dbh->quote( $data{'contactnote'} )
755 . $dbh->quote( $data{'B_address'} )
757 . $dbh->quote( $data{'B_zipcode'} )
759 . $dbh->quote( $data{'B_city'} )
761 . $dbh->quote( $data{'B_phone'} )
763 . $dbh->quote( $data{'B_email'}, )
765 . $dbh->quote( $data{'password'} )
767 . $dbh->quote( $data{'userid'} )
769 . $dbh->quote( $data{'sort1'} )
771 . $dbh->quote( $data{'sort2'} )
773 . $dbh->quote( $data{'contacttitle'} )
775 . $dbh->quote( $data{'emailpro'} )
776 . ",contactfirstname="
777 . $dbh->quote( $data{'contactfirstname'} ) . ",sex="
778 . $dbh->quote( $data{'sex'} ) . ",fax="
779 . $dbh->quote( $data{'fax'} )
781 . $dbh->quote( $data{'relationship'} )
783 . $dbh->quote( $data{'B_streetnumber'} )
785 . $dbh->quote( $data{'B_streettype'} )
787 . $dbh->quote( $data{'gonenoaddress'} )
789 . $dbh->quote( $data{'lost'} )
791 . $dbh->quote( $data{'debarred'} )
793 . $dbh->quote( $data{'ethnicity'} )
795 . $dbh->quote( $data{'ethnotes'} );
797 my $sth = $dbh->prepare($query);
800 $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
802 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
803 if C4::Context->preference("BorrowersLog");
805 return $data{'borrowernumber'};
809 my ( $uid, $member, $digest ) = @_;
810 my $dbh = C4::Context->dbh;
812 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
813 #Then we need to tell the user and have them create a new one.
816 "select * from borrowers where userid=? and borrowernumber != ?");
817 $sth->execute( $uid, $member );
818 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
822 #Everything is good so we can update the information.
825 "update borrowers set userid=?, password=? where borrowernumber=?");
826 $sth->execute( $uid, $digest, $member );
830 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
831 if C4::Context->preference("BorrowersLog");
836 =item fixup_cardnumber
838 Warning: The caller is responsible for locking the members table in write
839 mode, to avoid database corruption.
843 use vars qw( @weightings );
844 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
846 sub fixup_cardnumber ($) {
847 my ($cardnumber) = @_;
848 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
849 $autonumber_members = 0 unless defined $autonumber_members;
851 # Find out whether member numbers should be generated
852 # automatically. Should be either "1" or something else.
853 # Defaults to "0", which is interpreted as "no".
855 # if ($cardnumber !~ /\S/ && $autonumber_members) {
856 if ($autonumber_members) {
857 my $dbh = C4::Context->dbh;
858 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
860 # if checkdigit is selected, calculate katipo-style cardnumber.
861 # otherwise, just use the max()
862 # purpose: generate checksum'd member numbers.
863 # We'll assume we just got the max value of digits 2-8 of member #'s
864 # from the database and our job is to increment that by one,
865 # determine the 1st and 9th digits and return the full string.
868 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
872 my $data = $sth->fetchrow_hashref;
873 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
875 if ( !$cardnumber ) { # If DB has no values,
876 $cardnumber = 1000000; # start at 1000000
883 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
885 # read weightings, left to right, 1 char at a time
886 my $temp1 = $weightings[$i];
888 # sequence left to right, 1 char at a time
889 my $temp2 = substr( $cardnumber, $i, 1 );
891 # mult each char 1-7 by its corresponding weighting
892 $sum += $temp1 * $temp2;
895 my $rem = ( $sum % 11 );
896 $rem = 'X' if $rem == 10;
898 $cardnumber = "V$cardnumber$rem";
902 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
903 # better. I'll leave the original in in case it needs to be changed for you
906 "select max(cast(cardnumber as signed)) from borrowers");
908 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
912 my ($result) = $sth->fetchrow;
914 $cardnumber = $result + 1;
922 ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
923 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
924 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
926 C<&GetGuarantees> takes a borrower number (e.g., that of a patron
927 with children) and looks up the borrowers who are guaranteed by that
928 borrower (i.e., the patron's children).
930 C<&GetGuarantees> returns two values: an integer giving the number of
931 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
932 of references to hash, which gives the actual results.
938 my ($borrowernumber) = @_;
939 my $dbh = C4::Context->dbh;
942 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
944 $sth->execute($borrowernumber);
947 my $data = $sth->fetchall_arrayref({});
949 return ( scalar(@$data), $data );
952 =head2 UpdateGuarantees
954 &UpdateGuarantees($parent_borrno);
957 C<&UpdateGuarantees> borrower data for an adulte and updates all the guarantees
958 with the modified information
963 sub UpdateGuarantees {
965 my $dbh = C4::Context->dbh;
966 my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
967 for ( my $i = 0 ; $i < $count ; $i++ ) {
970 # It looks like the $i is only being returned to handle walking through
971 # the array, which is probably better done as a foreach loop.
973 my $guaquery = qq|UPDATE borrowers
974 SET address='$data{'address'}',fax='$data{'fax'}',
975 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
976 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
978 my $sth3 = $dbh->prepare($guaquery);
983 =head2 GetPendingIssues
985 ($count, $issues) = &GetPendingIssues($borrowernumber);
987 Looks up what the patron with the given borrowernumber has borrowed.
989 C<&GetPendingIssues> returns a two-element array. C<$issues> is a
990 reference-to-array, where each element is a reference-to-hash; the
991 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
992 in the Koha database. C<$count> is the number of elements in
998 sub GetPendingIssues {
999 my ($borrowernumber) = @_;
1000 my $dbh = C4::Context->dbh;
1001 my $sth = $dbh->prepare(
1002 "Select * from issues,biblio,items where borrowernumber=?
1003 and items.itemnumber=issues.itemnumber
1004 and items.biblionumber=biblio.biblionumber
1005 and issues.returndate is NULL order by date_due"
1007 $sth->execute($borrowernumber);
1008 my $data = $sth->fetchall_arrayref({});
1009 return ( scalar(@$data), $data );
1014 ($count, $issues) = &GetAllIssues($borrowernumber, $sortkey, $limit);
1016 Looks up what the patron with the given borrowernumber has borrowed,
1017 and sorts the results.
1019 C<$sortkey> is the name of a field on which to sort the results. This
1020 should be the name of a field in the C<issues>, C<biblio>,
1021 C<biblioitems>, or C<items> table in the Koha database.
1023 C<$limit> is the maximum number of results to return.
1025 C<&GetAllIssues> returns a two-element array. C<$issues> is a
1026 reference-to-array, where each element is a reference-to-hash; the
1027 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1028 C<items> tables of the Koha database. C<$count> is the number of
1029 elements in C<$issues>
1035 my ( $borrowernumber, $order, $limit ) = @_;
1037 #FIXME: sanity-check order and limit
1038 my $dbh = C4::Context->dbh;
1041 "Select *,items.timestamp AS itemstimestamp from issues,biblio,items,biblioitems
1042 where borrowernumber=? and
1043 items.biblioitemnumber=biblioitems.biblioitemnumber and
1044 items.itemnumber=issues.itemnumber and
1045 items.biblionumber=biblio.biblionumber order by $order";
1046 if ( $limit != 0 ) {
1047 $query .= " limit $limit";
1051 my $sth = $dbh->prepare($query);
1052 $sth->execute($borrowernumber);
1055 while ( my $data = $sth->fetchrow_hashref ) {
1056 $result[$i] = $data;
1061 # get all issued items for borrowernumber from oldissues table
1062 # large chunk of older issues data put into table oldissues
1063 # to speed up db calls for issuing items
1064 if ( C4::Context->preference("ReadingHistory") ) {
1065 my $query2 = "SELECT * FROM oldissues,biblio,items,biblioitems
1066 WHERE borrowernumber=?
1067 AND items.biblioitemnumber=biblioitems.biblioitemnumber
1068 AND items.itemnumber=oldissues.itemnumber
1069 AND items.biblionumber=biblio.biblionumber
1071 if ( $limit != 0 ) {
1072 $limit = $limit - $count;
1073 $query2 .= " limit $limit";
1076 my $sth2 = $dbh->prepare($query2);
1077 $sth2->execute($borrowernumber);
1079 while ( my $data2 = $sth2->fetchrow_hashref ) {
1080 $result[$i] = $data2;
1087 return ( $i, \@result );
1091 =head2 GetBorrowerAcctRecord
1093 ($total, $acctlines, $count) = &GetBorrowerAcctRecord($borrowernumber);
1095 Looks up accounting data for the patron with the given borrowernumber.
1097 C<&GetBorrowerAcctRecord> returns a three-element array. C<$acctlines> is a
1098 reference-to-array, where each element is a reference-to-hash; the
1099 keys are the fields of the C<accountlines> table in the Koha database.
1100 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1101 total amount outstanding for all of the account lines.
1106 sub GetBorrowerAcctRecord {
1107 my ($borrowernumber,$date) = @_;
1108 my $dbh = C4::Context->dbh;
1112 Select * from accountlines where
1113 borrowernumber=? order by date desc,timestamp desc
1115 my @bind = ($borrowernumber);
1116 if ($date && $date ne ''){
1117 $strsth.=" AND date < ?";
1121 my $sth= $dbh->prepare( $strsth );
1122 $sth->execute( $borrowernumber);
1124 while ( my $data = $sth->fetchrow_hashref ) {
1126 $acctlines[$numlines] = $data;
1128 $total += $data->{'amountoutstanding'};
1131 return ( $total, \@acctlines,$numlines);
1134 =head2 GetBorNotifyAcctRecord
1136 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1138 Looks up accounting data for the patron with the given borrowernumber per file number.
1140 (FIXME - I'm not at all sure what this is about.)
1142 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1143 reference-to-array, where each element is a reference-to-hash; the
1144 keys are the fields of the C<accountlines> table in the Koha database.
1145 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1146 total amount outstanding for all of the account lines.
1150 sub GetBorNotifyAcctRecord {
1151 my ( $borrowernumber, $notifyid ) = @_;
1152 my $dbh = C4::Context->dbh;
1155 my $query = qq| SELECT *
1157 WHERE borrowernumber=?
1159 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')
1160 AND amountoutstanding != '0'
1161 ORDER BY notify_id,accounttype
1163 my $sth = $dbh->prepare($query);
1165 $sth->execute( $borrowernumber, $notifyid );
1167 while ( my $data = $sth->fetchrow_hashref ) {
1168 $acctlines[$numlines] = $data;
1170 $total += $data->{'amountoutstanding'};
1173 return ( $total, \@acctlines, $numlines );
1176 =head2 checkuniquemember (OUEST-PROVENCE)
1178 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
1180 Checks that a member exists or not in the database.
1182 C<&result> is 1 (=exist) or 0 (=does not exist)
1183 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1184 C<&surname> is the surname
1185 C<&categorycode> is from categorycode table
1186 C<&firstname> is the firstname (only if collectivity=0)
1187 C<&dateofbirth> is the date of birth (only if collectivity=0)
1191 sub checkuniquemember {
1192 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1193 my $dbh = C4::Context->dbh;
1195 if ($collectivity) {
1197 # $request="select count(*) from borrowers where surname=? and categorycode=?";
1199 "select borrowernumber,categorycode from borrowers where surname=? ";
1203 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1205 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1207 my $sth = $dbh->prepare($request);
1208 if ($collectivity) {
1209 $sth->execute( uc($surname) );
1212 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1214 my @data = $sth->fetchrow;
1217 return $data[0], $data[1];
1227 =head2 getzipnamecity (OUEST-PROVENCE)
1229 take all info from table city for the fields city and zip
1230 check for the name and the zip code of the city selected
1234 sub getzipnamecity {
1236 my $dbh = C4::Context->dbh;
1239 "select city_name,city_zipcode from cities where cityid=? ");
1240 $sth->execute($cityid);
1241 my @data = $sth->fetchrow;
1242 return $data[0], $data[1];
1246 =head2 getdcity (OUEST-PROVENCE)
1248 recover cityid with city_name condition
1253 my ($city_name) = @_;
1254 my $dbh = C4::Context->dbh;
1255 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1256 $sth->execute($city_name);
1257 my $data = $sth->fetchrow;
1262 =head2 GetExpiryDate
1264 $expirydate = GetExpiryDate($categorycode, $dateenrolled);
1265 process expiry date given a date and a categorycode
1269 my ( $categorycode, $dateenrolled ) = @_;
1270 my $dbh = C4::Context->dbh;
1273 "select enrolmentperiod from categories where categorycode=?");
1274 $sth->execute($categorycode);
1275 my ($enrolmentperiod) = $sth->fetchrow;
1276 $enrolmentperiod = 12 unless ($enrolmentperiod);
1277 my @date=split /-/,format_date_in_iso($dateenrolled);
1278 @date=Add_Delta_YM($date[0],$date[1],$date[2],0,$enrolmentperiod);
1279 return sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
1282 =head2 checkuserpassword (OUEST-PROVENCE)
1284 check for the password and login are not used
1285 return the number of record
1286 0=> NOT USED 1=> USED
1290 sub checkuserpassword {
1291 my ( $borrowernumber, $userid, $password ) = @_;
1292 $password = md5_base64($password);
1293 my $dbh = C4::Context->dbh;
1296 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1298 $sth->execute( $borrowernumber, $userid, $password );
1299 my $number_rows = $sth->fetchrow;
1300 return $number_rows;
1304 =head2 GetborCatFromCatType
1306 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1308 Looks up the different types of borrowers in the database. Returns two
1309 elements: a reference-to-array, which lists the borrower category
1310 codes, and a reference-to-hash, which maps the borrower category codes
1311 to category descriptions.
1316 sub GetborCatFromCatType {
1317 my ( $category_type, $action ) = @_;
1318 my $dbh = C4::Context->dbh;
1319 my $request = qq| SELECT categorycode,description
1322 ORDER BY categorycode|;
1323 my $sth = $dbh->prepare($request);
1325 $sth->execute($category_type);
1334 while ( my $data = $sth->fetchrow_hashref ) {
1335 push @codes, $data->{'categorycode'};
1336 $labels{ $data->{'categorycode'} } = $data->{'description'};
1339 return ( \@codes, \%labels );
1342 =head2 GetBorrowercategory
1344 $hashref = &GetBorrowercategory($categorycode);
1346 Given the borrower's category code, the function returns the corresponding
1347 data hashref for a comprehensive information display.
1351 sub GetBorrowercategory {
1353 my $dbh = C4::Context->dbh;
1356 "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?"
1358 $sth->execute($catcode);
1360 $sth->fetchrow_hashref;
1363 } # sub getborrowercategory
1365 =head2 ethnicitycategories
1367 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1369 Looks up the different ethnic types in the database. Returns two
1370 elements: a reference-to-array, which lists the ethnicity codes, and a
1371 reference-to-hash, which maps the ethnicity codes to ethnicity
1378 sub ethnicitycategories {
1379 my $dbh = C4::Context->dbh;
1380 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1384 while ( my $data = $sth->fetchrow_hashref ) {
1385 push @codes, $data->{'code'};
1386 $labels{ $data->{'code'} } = $data->{'name'};
1389 return ( \@codes, \%labels );
1394 $ethn_name = &fixEthnicity($ethn_code);
1396 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1397 corresponding descriptive name from the C<ethnicity> table in the
1398 Koha database ("European" or "Pacific Islander").
1405 my $ethnicity = shift;
1406 return unless $ethnicity;
1407 my $dbh = C4::Context->dbh;
1408 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1409 $sth->execute($ethnicity);
1410 my $data = $sth->fetchrow_hashref;
1412 return $data->{'name'};
1413 } # sub fixEthnicity
1417 $dateofbirth,$date = &GetAge($date);
1419 this function return the borrowers age with the value of dateofbirth
1425 my ( $date, $date_ref ) = @_;
1427 if ( not defined $date_ref ) {
1428 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1431 my ( $year1, $month1, $day1 ) = split /-/, $date;
1432 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1434 my $age = $year2 - $year1;
1435 if ( $month1 . $day1 > $month2 . $day2 ) {
1442 =head2 get_institutions
1443 $insitutions = get_institutions();
1445 Just returns a list of all the borrowers of type I, borrownumber and name
1450 sub get_institutions {
1451 my $dbh = C4::Context->dbh();
1454 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1458 while ( my $data = $sth->fetchrow_hashref() ) {
1459 $orgs{ $data->{'borrowernumber'} } = $data;
1464 } # sub get_institutions
1466 =head2 add_member_orgs
1468 add_member_orgs($borrowernumber,$borrowernumbers);
1470 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1475 sub add_member_orgs {
1476 my ( $borrowernumber, $otherborrowers ) = @_;
1477 my $dbh = C4::Context->dbh();
1479 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1480 my $sth = $dbh->prepare($query);
1481 foreach my $otherborrowernumber (@$otherborrowers) {
1482 $sth->execute( $borrowernumber, $otherborrowernumber );
1486 } # sub add_member_orgs
1488 =head2 GetBorrowersFromSurname
1492 \@resutlts = GetBorrowersFromSurname($surname)
1493 this function get the list of borrower names like $surname.
1495 the table of results in @results
1501 sub GetBorrowersFromSurname {
1502 my ($searchstring) = @_;
1503 my $dbh = C4::Context->dbh;
1504 $searchstring =~ s/\'/\\\'/g;
1505 my @data = split( ' ', $searchstring );
1508 SELECT surname,firstname
1510 WHERE (surname like ?)
1513 my $sth = $dbh->prepare($query);
1514 $sth->execute("$data[0]%");
1518 my $data = $sth->fetchall_arrayref({});
1520 return ( scalar(@$data), $data );
1523 =head2 GetCities (OUEST-PROVENCE)
1525 ($id_cityarrayref, $city_hashref) = &GetCities();
1527 Looks up the different city and zip in the database. Returns two
1528 elements: a reference-to-array, which lists the zip city
1529 codes, and a reference-to-hash, which maps the name of the city.
1530 WHERE =>OUEST PROVENCE OR EXTERIEUR
1536 #my ($type_city) = @_;
1537 my $dbh = C4::Context->dbh;
1538 my $query = qq|SELECT cityid,city_name
1540 ORDER BY city_name|;
1541 my $sth = $dbh->prepare($query);
1543 #$sth->execute($type_city);
1548 # insert empty value to create a empty choice in cgi popup
1550 while ( my $data = $sth->fetchrow_hashref ) {
1552 push @id, $data->{'cityid'};
1553 $city{ $data->{'cityid'} } = $data->{'city_name'};
1556 #test to know if the table contain some records if no the function return nothing
1564 return ( \@id, \%city );
1568 =head2 GetSortDetails (OUEST-PROVENCE)
1570 ($lib) = &GetSortDetails($category,$sortvalue);
1572 Returns the authorized value details
1573 C<&$lib>return value of authorized value details
1574 C<&$sortvalue>this is the value of authorized value
1575 C<&$category>this is the value of authorized value category
1579 sub GetSortDetails {
1580 my ( $category, $sortvalue ) = @_;
1581 my $dbh = C4::Context->dbh;
1582 my $query = qq|SELECT lib
1583 FROM authorised_values
1585 AND authorised_value=? |;
1586 my $sth = $dbh->prepare($query);
1587 $sth->execute( $category, $sortvalue );
1588 my $lib = $sth->fetchrow;
1592 =head2 DeleteBorrower
1594 () = &DeleteBorrower($member);
1596 delete all data fo borrowers and add record to deletedborrowers table
1597 C<&$member>this is the borrowernumber
1601 sub MoveMemberToDeleted {
1603 my $dbh = C4::Context->dbh;
1605 $query = qq|SELECT *
1607 WHERE borrowernumber=?|;
1608 my $sth = $dbh->prepare($query);
1609 $sth->execute($member);
1610 my @data = $sth->fetchrow_array;
1613 $dbh->prepare( "Insert into deletedborrowers values ("
1614 . ( "?," x ( scalar(@data) - 1 ) )
1616 $sth->execute(@data);
1622 DelMember($borrowernumber);
1624 This function remove directly a borrower whitout writing it on deleteborrower.
1625 + Deletes reserves for the borrower
1630 my $dbh = C4::Context->dbh;
1631 my $borrowernumber = shift;
1632 return unless $borrowernumber; # borrowernumber is mandatory.
1634 my $query = qq|DELETE
1636 WHERE borrowernumber=?|;
1637 my $sth = $dbh->prepare($query);
1638 $sth->execute($borrowernumber);
1643 WHERE borrowernumber = ?
1645 my $sth = $dbh->prepare($query);
1646 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$borrowernumber,"")
1647 if C4::Context->preference("BorrowersLog");
1651 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1653 $date= ExtendMemberSubscriptionTo($borrowerid, $date);
1654 Extending the subscription to a given date or to the expiry date calculated on local date.
1658 sub ExtendMemberSubscriptionTo {
1659 my ( $borrowerid,$date) = @_;
1660 my $dbh = C4::Context->dbh;
1662 $date=POSIX::strftime("%Y-%m-%d",localtime(time));
1663 my $borrower = GetBorrower($borrowerid,'borrowernumber');
1664 $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
1666 my $sth = $dbh->do(<<EOF);
1668 SET dateexpiry='$date'
1669 WHERE borrowernumber='$borrowerid'
1671 return $date if ($sth);
1675 =head2 GetRoadTypes (OUEST-PROVENCE)
1677 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1679 Looks up the different road type . Returns two
1680 elements: a reference-to-array, which lists the id_roadtype
1681 codes, and a reference-to-hash, which maps the road type of the road .
1687 my $dbh = C4::Context->dbh;
1688 my $query = qq|SELECT roadtypeid,road_type
1690 ORDER BY road_type|;
1691 my $sth = $dbh->prepare($query);
1696 # insert empty value to create a empty choice in cgi popup
1698 while ( my $data = $sth->fetchrow_hashref ) {
1700 push @id, $data->{'roadtypeid'};
1701 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1704 #test to know if the table contain some records if no the function return nothing
1712 return ( \@id, \%roadtype );
1718 =head2 GetBorrowersTitles (OUEST-PROVENCE)
1720 ($borrowertitle)= &GetBorrowersTitles();
1722 Looks up the different title . Returns array with all borrowers title
1726 sub GetBorrowersTitles {
1727 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1728 unshift( @borrowerTitle, "" );
1729 return ( \@borrowerTitle);
1734 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1736 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1738 Returns the description of roadtype
1739 C<&$roadtype>return description of road type
1740 C<&$roadtypeid>this is the value of roadtype s
1744 sub GetRoadTypeDetails {
1745 my ($roadtypeid) = @_;
1746 my $dbh = C4::Context->dbh;
1747 my $query = qq|SELECT road_type
1749 WHERE roadtypeid=?|;
1750 my $sth = $dbh->prepare($query);
1751 $sth->execute($roadtypeid);
1752 my $roadtype = $sth->fetchrow;
1756 =head2 GetBorrowersWhoHaveNotBorrowedSince
1758 &GetBorrowersWhoHaveNotBorrowedSince($date)
1760 this function get all borrowers who haven't borrowed since the date given on input arg.
1764 sub GetBorrowersWhoHaveNotBorrowedSince {
1766 return unless $date; # date is mandatory.
1767 my $dbh = C4::Context->dbh;
1769 SELECT borrowers.borrowernumber,max(timestamp)
1771 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1772 WHERE issues.borrowernumber IS NOT NULL
1773 GROUP BY borrowers.borrowernumber
1775 my $sth = $dbh->prepare($query);
1779 while ( my $data = $sth->fetchrow_hashref ) {
1780 push @results, $data;
1785 =head2 GetBorrowersWhoHaveNeverBorrowed
1787 $results = &GetBorrowersWhoHaveNeverBorrowed
1789 this function get all borrowers who have never borrowed.
1791 I<$result> is a ref to an array which all elements are a hasref.
1795 sub GetBorrowersWhoHaveNeverBorrowed {
1796 my $dbh = C4::Context->dbh;
1798 SELECT borrowers.borrowernumber,max(timestamp)
1800 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1801 WHERE issues.borrowernumber IS NULL
1803 my $sth = $dbh->prepare($query);
1806 while ( my $data = $sth->fetchrow_hashref ) {
1807 push @results, $data;
1812 =head2 GetBorrowersWithIssuesHistoryOlderThan
1814 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1816 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1818 I<$result> is a ref to an array which all elements are a hashref.
1819 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
1823 sub GetBorrowersWithIssuesHistoryOlderThan {
1824 my $dbh = C4::Context->dbh;
1826 return unless $date; # date is mandatory.
1828 SELECT count(borrowernumber) as n,borrowernumber
1830 WHERE returndate < ?
1831 AND borrowernumber IS NOT NULL
1832 GROUP BY borrowernumber
1834 my $sth = $dbh->prepare($query);
1835 $sth->execute($date);
1838 while ( my $data = $sth->fetchrow_hashref ) {
1839 push @results, $data;
1844 END { } # module clean-up code here (global destructor)