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
79 &GetMemberAccountRecords
80 &GetBorNotifyAcctRecord
86 &GetBorrowersWhoHaveNotBorrowedSince
87 &GetBorrowersWhoHaveNeverBorrowed
88 &GetBorrowersWithIssuesHistoryOlderThan
109 &ExtendMemberSubscriptionTo
123 ($count, $borrowers) = &SearchMember($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<&SearchMember> 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 $data = $sth->fetchall_arrayref({});
205 return ( scalar(@$data), $data );
208 =head2 GetMemberDetails
210 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
212 Looks up a patron and returns information about him or her. If
213 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
214 up the borrower by number; otherwise, it looks up the borrower by card
217 C<$borrower> is a reference-to-hash whose keys are the fields of the
218 borrowers table in the Koha database. In addition,
219 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
220 about the patron. Its keys act as flags :
222 if $borrower->{flags}->{LOST} {
223 # Patron's card was reported lost
226 Each flag has a C<message> key, giving a human-readable explanation of
227 the flag. If the state of a flag means that the patron should not be
228 allowed to borrow any more books, then it will have a C<noissues> key
231 The possible flags are:
237 =item Shows the patron's credit or debt, if any.
245 =item (Gone, no address.) Set if the patron has left without giving a
254 =item Set if the patron's card has been reported as lost.
262 =item Set if the patron has been debarred.
270 =item Any additional notes about the patron.
278 =item Set if the patron has overdue items. This flag has several keys:
280 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
281 overdue items. Its elements are references-to-hash, each describing an
282 overdue item. The keys are selected fields from the issues, biblio,
283 biblioitems, and items tables of the Koha database.
285 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
286 the overdue items, one per line.
294 =item Set if any items that the patron has reserved are available.
296 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
297 available items. Each element is a reference-to-hash whose keys are
298 fields from the reserves table of the Koha database.
304 sub GetMemberDetails {
305 my ( $borrowernumber, $cardnumber ) = @_;
306 my $dbh = C4::Context->dbh;
309 if ($borrowernumber) {
310 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
311 $sth->execute($borrowernumber);
313 elsif ($cardnumber) {
314 $sth = $dbh->prepare("select * from borrowers where cardnumber=?");
315 $sth->execute($cardnumber);
320 my $borrower = $sth->fetchrow_hashref;
321 my ($amount) = GetMemberAccountRecords( $borrowernumber);
322 $borrower->{'amountoutstanding'} = $amount;
323 my $flags = patronflags( $borrower);
326 $sth = $dbh->prepare("select bit,flag from userflags");
328 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
329 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
330 $accessflagshash->{$flag} = 1;
334 $borrower->{'flags'} = $flags;
335 $borrower->{'authflags'} = $accessflagshash;
337 # find out how long the membership lasts
340 "select enrolmentperiod from categories where categorycode = ?");
341 $sth->execute( $borrower->{'categorycode'} );
342 my $enrolment = $sth->fetchrow;
343 $borrower->{'enrolmentperiod'} = $enrolment;
344 return ($borrower); #, $flags, $accessflagshash);
351 NOTE!: If you change this function, be sure to update the POD for
354 $flags = &patronflags($patron);
357 {message} Message showing patron's credit or debt
358 {noissues} Set if patron owes >$5.00
359 {GNA} Set if patron gone w/o address
360 {message} "Borrower has no valid address"
362 {LOST} Set if patron's card reported lost
363 {message} Message to this effect
365 {DBARRED} Set is patron is debarred
366 {message} Message to this effect
368 {NOTES} Set if patron has notes
369 {message} Notes about patron
370 {ODUES} Set if patron has overdue books
372 {itemlist} ref-to-array: list of overdue books
373 {itemlisttext} Text list of overdue items
374 {WAITING} Set if there are items available that the
376 {message} Message to this effect
377 {itemlist} ref-to-array: list of available items
383 my ( $patroninformation) = @_;
384 my $dbh=C4::Context->dbh;
385 my ($amount) = GetMemberAccountRecords( $patroninformation->{'borrowernumber'});
388 my $noissuescharge = C4::Context->preference("noissuescharge");
389 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
390 if ( $amount > $noissuescharge ) {
391 $flaginfo{'noissues'} = 1;
393 $flags{'CHARGES'} = \%flaginfo;
395 elsif ( $amount < 0 ) {
397 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
398 $flags{'CHARGES'} = \%flaginfo;
400 if ( $patroninformation->{'gonenoaddress'}
401 && $patroninformation->{'gonenoaddress'} == 1 )
404 $flaginfo{'message'} = 'Borrower has no valid address.';
405 $flaginfo{'noissues'} = 1;
406 $flags{'GNA'} = \%flaginfo;
408 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
410 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
411 $flaginfo{'noissues'} = 1;
412 $flags{'LOST'} = \%flaginfo;
414 if ( $patroninformation->{'debarred'}
415 && $patroninformation->{'debarred'} == 1 )
418 $flaginfo{'message'} = 'Borrower is Debarred.';
419 $flaginfo{'noissues'} = 1;
420 $flags{'DBARRED'} = \%flaginfo;
422 if ( $patroninformation->{'borrowernotes'}
423 && $patroninformation->{'borrowernotes'} )
426 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
427 $flags{'NOTES'} = \%flaginfo;
429 my ( $odues, $itemsoverdue ) =
430 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
433 $flaginfo{'message'} = "Yes";
434 $flaginfo{'itemlist'} = $itemsoverdue;
435 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
438 $flaginfo{'itemlisttext'} .=
439 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
441 $flags{'ODUES'} = \%flaginfo;
444 GetReservesFromBorrowernumber( $patroninformation->{'borrowernumber'},'W' );
445 my $nowaiting = scalar @itemswaiting;
446 if ( $nowaiting > 0 ) {
448 $flaginfo{'message'} = "Reserved items available";
449 $flaginfo{'itemlist'} = \@itemswaiting;
450 $flags{'WAITING'} = \%flaginfo;
458 $borrower = &GetMember($information, $type);
460 Looks up information about a patron (borrower) by either card number
461 ,firstname, or borrower number, depending on $type value.
462 If C<$type> == 'cardnumber', C<&GetBorrower>
463 searches by cardnumber then by firstname if not found in cardnumber;
464 otherwise, it searches by borrowernumber.
466 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
467 the C<borrowers> table in the Koha database.
473 my ( $information, $type ) = @_;
474 my $dbh = C4::Context->dbh;
476 if ($type eq 'cardnumber' || $type eq 'firstname'|| $type eq 'userid'|| $type eq 'borrowernumber'){
477 $information = uc $information;
480 "Select borrowers.*,categories.category_type,categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where $type=?"
482 $sth->execute($information);
487 "Select borrowers.*,categories.category_type, categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
489 $sth->execute($information);
491 my $data = $sth->fetchrow_hashref;
497 elsif ($type eq 'cardnumber' ||$type eq 'firstname') { # try with firstname
500 "Select borrowers.*,categories.category_type,categories.description from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname like ?"
502 $sth->execute($information);
503 my $data = $sth->fetchrow_hashref;
512 =item GetMemberIssuesAndFines
514 ($borrowed, $due, $fine) = &GetMemberIssuesAndFines($borrowernumber);
516 Returns aggregate data about items borrowed by the patron with the
517 given borrowernumber.
519 C<&GetMemberIssuesAndFines> returns a three-element array. C<$borrowed> is the
520 number of books the patron currently has borrowed. C<$due> is the
521 number of overdue items the patron currently has borrowed. C<$fine> is
522 the total fine currently due by the borrower.
527 sub GetMemberIssuesAndFines {
528 my ( $borrowernumber ) = @_;
529 my $dbh = C4::Context->dbh;
531 "Select count(*) from issues where borrowernumber='$borrowernumber' and
535 my $sth = $dbh->prepare($query);
537 my $data = $sth->fetchrow_hashref;
539 $sth = $dbh->prepare(
540 "Select count(*) from issues where
541 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
544 my $data2 = $sth->fetchrow_hashref;
546 $sth = $dbh->prepare(
547 "Select sum(amountoutstanding) from accountlines where
548 borrowernumber='$borrowernumber'"
551 my $data3 = $sth->fetchrow_hashref;
554 return ( $data2->{'count(*)'}, $data->{'count(*)'},
555 $data3->{'sum(amountoutstanding)'} );
562 &ModMember($borrowernumber);
564 Modify borrower's data
571 my $dbh = C4::Context->dbh;
572 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
573 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
574 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
576 # warn "num user".$data{'borrowernumber'};
579 $data{'userid'} = '' if ( $data{'password'} eq '' );
581 # test to know if u must update or not the borrower password
582 if ( $data{'password'} eq '****' ) {
584 $query = "UPDATE borrowers SET
585 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
586 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
587 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
588 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
589 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
590 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
591 ethnotes = ?,sex = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
592 WHERE borrowernumber=$data{'borrowernumber'}";
593 $sth = $dbh->prepare($query);
595 $data{'cardnumber'}, $data{'surname'},
596 $data{'firstname'}, $data{'title'},
597 $data{'othernames'}, $data{'initials'},
598 $data{'streetnumber'}, $data{'streettype'},
599 $data{'address'}, $data{'address2'},
600 $data{'city'}, $data{'zipcode'},
601 $data{'email'}, $data{'phone'},
602 $data{'mobile'}, $data{'fax'},
603 $data{'emailpro'}, $data{'phonepro'},
604 $data{'B_streetnumber'}, $data{'B_streettype'},
605 $data{'B_address'}, $data{'B_city'},
606 $data{'B_zipcode'}, $data{'B_email'},
607 $data{'B_phone'}, $data{'dateofbirth'},
608 $data{'branchcode'}, $data{'categorycode'},
609 $data{'dateenrolled'}, $data{'dateexpiry'},
610 $data{'gonenoaddress'}, $data{'lost'},
611 $data{'debarred'}, $data{'contactname'},
612 $data{'contactfirstname'}, $data{'contacttitle'},
613 $data{'guarantorid'}, $data{'borrowernotes'},
614 $data{'relationship'}, $data{'ethnicity'},
615 $data{'ethnotes'}, $data{'sex'},
616 $data{'userid'}, $data{'opacnote'},
617 $data{'contactnote'}, $data{'sort1'},
623 ( $data{'password'} = md5_base64( $data{'password'} ) )
624 if ( $data{'password'} ne '' );
625 $query = "UPDATE borrowers SET
626 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
627 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
628 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
629 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
630 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
631 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
632 ethnotes = ?,sex = ?,password = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
633 WHERE borrowernumber=$data{'borrowernumber'}";
634 $sth = $dbh->prepare($query);
636 $data{'cardnumber'}, $data{'surname'},
637 $data{'firstname'}, $data{'title'},
638 $data{'othernames'}, $data{'initials'},
639 $data{'streetnumber'}, $data{'streettype'},
640 $data{'address'}, $data{'address2'},
641 $data{'city'}, $data{'zipcode'},
642 $data{'email'}, $data{'phone'},
643 $data{'mobile'}, $data{'fax'},
644 $data{'emailpro'}, $data{'phonepro'},
645 $data{'B_streetnumber'}, $data{'B_streettype'},
646 $data{'B_address'}, $data{'B_city'},
647 $data{'B_zipcode'}, $data{'B_email'},
648 $data{'B_phone'}, $data{'dateofbirth'},
649 $data{'branchcode'}, $data{'categorycode'},
650 $data{'dateenrolled'}, $data{'dateexpiry'},
651 $data{'gonenoaddress'}, $data{'lost'},
652 $data{'debarred'}, $data{'contactname'},
653 $data{'contactfirstname'}, $data{'contacttitle'},
654 $data{'guarantorid'}, $data{'borrowernotes'},
655 $data{'relationship'}, $data{'ethnicity'},
656 $data{'ethnotes'}, $data{'sex'},
657 $data{'password'}, $data{'userid'},
658 $data{'opacnote'}, $data{'contactnote'},
659 $data{'sort1'}, $data{'sort2'}
664 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
665 # so when we update information for an adult we should check for guarantees and update the relevant part
666 # of their records, ie addresses and phone numbers
667 my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
668 if ( $borrowercategory->{'category_type'} eq 'A' ) {
669 # is adult check guarantees;
670 UpdateGuarantees(%data);
673 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
674 if C4::Context->preference("BorrowersLog");
681 $borrowernumber = &AddMember(%borrower);
683 insert new borrower into table
684 Returns the borrowernumber
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 ) ) {
818 #Everything is good so we can update the information.
821 "update borrowers set userid=?, password=? where borrowernumber=?");
822 $sth->execute( $uid, $digest, $member );
826 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
827 if C4::Context->preference("BorrowersLog");
832 =item fixup_cardnumber
834 Warning: The caller is responsible for locking the members table in write
835 mode, to avoid database corruption.
839 use vars qw( @weightings );
840 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
842 sub fixup_cardnumber ($) {
843 my ($cardnumber) = @_;
844 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
845 $autonumber_members = 0 unless defined $autonumber_members;
847 # Find out whether member numbers should be generated
848 # automatically. Should be either "1" or something else.
849 # Defaults to "0", which is interpreted as "no".
851 # if ($cardnumber !~ /\S/ && $autonumber_members) {
852 if ($autonumber_members) {
853 my $dbh = C4::Context->dbh;
854 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
856 # if checkdigit is selected, calculate katipo-style cardnumber.
857 # otherwise, just use the max()
858 # purpose: generate checksum'd member numbers.
859 # We'll assume we just got the max value of digits 2-8 of member #'s
860 # from the database and our job is to increment that by one,
861 # determine the 1st and 9th digits and return the full string.
864 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
868 my $data = $sth->fetchrow_hashref;
869 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
871 if ( !$cardnumber ) { # If DB has no values,
872 $cardnumber = 1000000; # start at 1000000
879 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
881 # read weightings, left to right, 1 char at a time
882 my $temp1 = $weightings[$i];
884 # sequence left to right, 1 char at a time
885 my $temp2 = substr( $cardnumber, $i, 1 );
887 # mult each char 1-7 by its corresponding weighting
888 $sum += $temp1 * $temp2;
891 my $rem = ( $sum % 11 );
892 $rem = 'X' if $rem == 10;
894 $cardnumber = "V$cardnumber$rem";
898 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
899 # better. I'll leave the original in in case it needs to be changed for you
902 "select max(cast(cardnumber as signed)) from borrowers");
904 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
908 my ($result) = $sth->fetchrow;
910 $cardnumber = $result + 1;
918 ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
919 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
920 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
922 C<&GetGuarantees> takes a borrower number (e.g., that of a patron
923 with children) and looks up the borrowers who are guaranteed by that
924 borrower (i.e., the patron's children).
926 C<&GetGuarantees> returns two values: an integer giving the number of
927 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
928 of references to hash, which gives the actual results.
934 my ($borrowernumber) = @_;
935 my $dbh = C4::Context->dbh;
938 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
940 $sth->execute($borrowernumber);
943 my $data = $sth->fetchall_arrayref({});
945 return ( scalar(@$data), $data );
948 =head2 UpdateGuarantees
950 &UpdateGuarantees($parent_borrno);
953 C<&UpdateGuarantees> borrower data for an adulte and updates all the guarantees
954 with the modified information
959 sub UpdateGuarantees {
961 my $dbh = C4::Context->dbh;
962 my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
963 for ( my $i = 0 ; $i < $count ; $i++ ) {
966 # It looks like the $i is only being returned to handle walking through
967 # the array, which is probably better done as a foreach loop.
969 my $guaquery = qq|UPDATE borrowers
970 SET address='$data{'address'}',fax='$data{'fax'}',
971 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
972 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
974 my $sth3 = $dbh->prepare($guaquery);
979 =head2 GetPendingIssues
981 ($count, $issues) = &GetPendingIssues($borrowernumber);
983 Looks up what the patron with the given borrowernumber has borrowed.
985 C<&GetPendingIssues> returns a two-element array. C<$issues> is a
986 reference-to-array, where each element is a reference-to-hash; the
987 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
988 in the Koha database. C<$count> is the number of elements in
994 sub GetPendingIssues {
995 my ($borrowernumber) = @_;
996 my $dbh = C4::Context->dbh;
998 my $sth = $dbh->prepare(
999 "SELECT * FROM issues
1000 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1001 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1002 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1005 AND returndate IS NULL
1006 ORDER BY issues.date_due"
1008 $sth->execute($borrowernumber);
1009 my $data = $sth->fetchall_arrayref({});
1010 my $today = POSIX::strftime("%Y%m%d", localtime);
1012 my $datedue = $_->{'date_due'};
1014 if ( $datedue < $today ) {
1015 $_->{'overdue'} = 1;
1019 return ( scalar(@$data), $data );
1024 ($count, $issues) = &GetAllIssues($borrowernumber, $sortkey, $limit);
1026 Looks up what the patron with the given borrowernumber has borrowed,
1027 and sorts the results.
1029 C<$sortkey> is the name of a field on which to sort the results. This
1030 should be the name of a field in the C<issues>, C<biblio>,
1031 C<biblioitems>, or C<items> table in the Koha database.
1033 C<$limit> is the maximum number of results to return.
1035 C<&GetAllIssues> returns a two-element array. C<$issues> is a
1036 reference-to-array, where each element is a reference-to-hash; the
1037 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1038 C<items> tables of the Koha database. C<$count> is the number of
1039 elements in C<$issues>
1045 my ( $borrowernumber, $order, $limit ) = @_;
1047 #FIXME: sanity-check order and limit
1048 my $dbh = C4::Context->dbh;
1051 "Select *,items.timestamp AS itemstimestamp from issues,biblio,items,biblioitems
1052 where borrowernumber=? and
1053 items.biblioitemnumber=biblioitems.biblioitemnumber and
1054 items.itemnumber=issues.itemnumber and
1055 items.biblionumber=biblio.biblionumber order by $order";
1056 if ( $limit != 0 ) {
1057 $query .= " limit $limit";
1061 my $sth = $dbh->prepare($query);
1062 $sth->execute($borrowernumber);
1065 while ( my $data = $sth->fetchrow_hashref ) {
1066 $result[$i] = $data;
1071 # get all issued items for borrowernumber from oldissues table
1072 # large chunk of older issues data put into table oldissues
1073 # to speed up db calls for issuing items
1074 if ( C4::Context->preference("ReadingHistory") ) {
1075 my $query2 = "SELECT * FROM oldissues,biblio,items,biblioitems
1076 WHERE borrowernumber=?
1077 AND items.biblioitemnumber=biblioitems.biblioitemnumber
1078 AND items.itemnumber=oldissues.itemnumber
1079 AND items.biblionumber=biblio.biblionumber
1081 if ( $limit != 0 ) {
1082 $limit = $limit - $count;
1083 $query2 .= " limit $limit";
1086 my $sth2 = $dbh->prepare($query2);
1087 $sth2->execute($borrowernumber);
1089 while ( my $data2 = $sth2->fetchrow_hashref ) {
1090 $result[$i] = $data2;
1097 return ( $i, \@result );
1101 =head2 GetMemberAccountRecords
1103 ($total, $acctlines, $count) = &GetMemberAccountRecords($borrowernumber);
1105 Looks up accounting data for the patron with the given borrowernumber.
1107 C<&GetMemberAccountRecords> returns a three-element array. C<$acctlines> is a
1108 reference-to-array, where each element is a reference-to-hash; the
1109 keys are the fields of the C<accountlines> table in the Koha database.
1110 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1111 total amount outstanding for all of the account lines.
1116 sub GetMemberAccountRecords {
1117 my ($borrowernumber,$date) = @_;
1118 my $dbh = C4::Context->dbh;
1124 WHERE borrowernumber=?);
1125 my @bind = ($borrowernumber);
1126 if ($date && $date ne ''){
1132 ORDER BY date desc,timestamp DESC";
1133 my $sth= $dbh->prepare( $strsth );
1134 $sth->execute( @bind );
1136 while ( my $data = $sth->fetchrow_hashref ) {
1137 $acctlines[$numlines] = $data;
1139 $total += $data->{'amountoutstanding'};
1142 return ( $total, \@acctlines,$numlines);
1145 =head2 GetBorNotifyAcctRecord
1147 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1149 Looks up accounting data for the patron with the given borrowernumber per file number.
1151 (FIXME - I'm not at all sure what this is about.)
1153 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1154 reference-to-array, where each element is a reference-to-hash; the
1155 keys are the fields of the C<accountlines> table in the Koha database.
1156 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1157 total amount outstanding for all of the account lines.
1161 sub GetBorNotifyAcctRecord {
1162 my ( $borrowernumber, $notifyid ) = @_;
1163 my $dbh = C4::Context->dbh;
1166 my $query = qq| SELECT *
1168 WHERE borrowernumber=?
1170 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')
1171 AND amountoutstanding != '0'
1172 ORDER BY notify_id,accounttype
1174 my $sth = $dbh->prepare($query);
1176 $sth->execute( $borrowernumber, $notifyid );
1178 while ( my $data = $sth->fetchrow_hashref ) {
1179 $acctlines[$numlines] = $data;
1181 $total += $data->{'amountoutstanding'};
1184 return ( $total, \@acctlines, $numlines );
1187 =head2 checkuniquemember (OUEST-PROVENCE)
1189 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
1191 Checks that a member exists or not in the database.
1193 C<&result> is 1 (=exist) or 0 (=does not exist)
1194 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1195 C<&surname> is the surname
1196 C<&categorycode> is from categorycode table
1197 C<&firstname> is the firstname (only if collectivity=0)
1198 C<&dateofbirth> is the date of birth (only if collectivity=0)
1202 sub checkuniquemember {
1203 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1204 my $dbh = C4::Context->dbh;
1206 if ($collectivity) {
1208 # $request="select count(*) from borrowers where surname=? and categorycode=?";
1210 "select borrowernumber,categorycode from borrowers where surname=? ";
1214 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1216 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1218 my $sth = $dbh->prepare($request);
1219 if ($collectivity) {
1220 $sth->execute( uc($surname) );
1223 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1225 my @data = $sth->fetchrow;
1228 return $data[0], $data[1];
1238 sub checkcardnumber {
1239 my ($cardnumber) = @_;
1240 my $dbh = C4::Context->dbh;
1241 my $query = "SELECT * FROM borrowers WHERE cardnumber=?";
1242 my $sth = $dbh->prepare($query);
1243 $sth->execute($cardnumber);
1244 if (my $data= $sth->fetchrow_hashref()){
1254 =head2 getzipnamecity (OUEST-PROVENCE)
1256 take all info from table city for the fields city and zip
1257 check for the name and the zip code of the city selected
1261 sub getzipnamecity {
1263 my $dbh = C4::Context->dbh;
1266 "select city_name,city_zipcode from cities where cityid=? ");
1267 $sth->execute($cityid);
1268 my @data = $sth->fetchrow;
1269 return $data[0], $data[1];
1273 =head2 getdcity (OUEST-PROVENCE)
1275 recover cityid with city_name condition
1280 my ($city_name) = @_;
1281 my $dbh = C4::Context->dbh;
1282 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1283 $sth->execute($city_name);
1284 my $data = $sth->fetchrow;
1289 =head2 GetExpiryDate
1291 $expirydate = GetExpiryDate($categorycode, $dateenrolled);
1292 process expiry date given a date and a categorycode
1296 my ( $categorycode, $dateenrolled ) = @_;
1297 my $dbh = C4::Context->dbh;
1300 "select enrolmentperiod from categories where categorycode=?");
1301 $sth->execute($categorycode);
1302 my ($enrolmentperiod) = $sth->fetchrow;
1303 $enrolmentperiod = 12 unless ($enrolmentperiod);
1304 my @date=split /-/,format_date_in_iso($dateenrolled);
1305 @date=Add_Delta_YM($date[0],$date[1],$date[2],0,$enrolmentperiod);
1306 return sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
1309 =head2 checkuserpassword (OUEST-PROVENCE)
1311 check for the password and login are not used
1312 return the number of record
1313 0=> NOT USED 1=> USED
1317 sub checkuserpassword {
1318 my ( $borrowernumber, $userid, $password ) = @_;
1319 $password = md5_base64($password);
1320 my $dbh = C4::Context->dbh;
1323 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1325 $sth->execute( $borrowernumber, $userid, $password );
1326 my $number_rows = $sth->fetchrow;
1327 return $number_rows;
1331 =head2 GetborCatFromCatType
1333 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1335 Looks up the different types of borrowers in the database. Returns two
1336 elements: a reference-to-array, which lists the borrower category
1337 codes, and a reference-to-hash, which maps the borrower category codes
1338 to category descriptions.
1343 sub GetborCatFromCatType {
1344 my ( $category_type, $action ) = @_;
1345 my $dbh = C4::Context->dbh;
1346 my $request = qq| SELECT categorycode,description
1349 ORDER BY categorycode|;
1350 my $sth = $dbh->prepare($request);
1352 $sth->execute($category_type);
1361 while ( my $data = $sth->fetchrow_hashref ) {
1362 push @codes, $data->{'categorycode'};
1363 $labels{ $data->{'categorycode'} } = $data->{'description'};
1366 return ( \@codes, \%labels );
1369 =head2 GetBorrowercategory
1371 $hashref = &GetBorrowercategory($categorycode);
1373 Given the borrower's category code, the function returns the corresponding
1374 data hashref for a comprehensive information display.
1378 sub GetBorrowercategory {
1380 my $dbh = C4::Context->dbh;
1383 "SELECT description,dateofbirthrequired,upperagelimit,category_type
1385 WHERE categorycode = ?"
1387 $sth->execute($catcode);
1389 $sth->fetchrow_hashref;
1392 } # sub getborrowercategory
1394 =head2 ethnicitycategories
1396 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1398 Looks up the different ethnic types in the database. Returns two
1399 elements: a reference-to-array, which lists the ethnicity codes, and a
1400 reference-to-hash, which maps the ethnicity codes to ethnicity
1407 sub ethnicitycategories {
1408 my $dbh = C4::Context->dbh;
1409 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1413 while ( my $data = $sth->fetchrow_hashref ) {
1414 push @codes, $data->{'code'};
1415 $labels{ $data->{'code'} } = $data->{'name'};
1418 return ( \@codes, \%labels );
1423 $ethn_name = &fixEthnicity($ethn_code);
1425 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1426 corresponding descriptive name from the C<ethnicity> table in the
1427 Koha database ("European" or "Pacific Islander").
1434 my $ethnicity = shift;
1435 return unless $ethnicity;
1436 my $dbh = C4::Context->dbh;
1437 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1438 $sth->execute($ethnicity);
1439 my $data = $sth->fetchrow_hashref;
1441 return $data->{'name'};
1442 } # sub fixEthnicity
1446 $dateofbirth,$date = &GetAge($date);
1448 this function return the borrowers age with the value of dateofbirth
1454 my ( $date, $date_ref ) = @_;
1456 if ( not defined $date_ref ) {
1457 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1460 my ( $year1, $month1, $day1 ) = split /-/, $date;
1461 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1463 my $age = $year2 - $year1;
1464 if ( $month1 . $day1 > $month2 . $day2 ) {
1471 =head2 get_institutions
1472 $insitutions = get_institutions();
1474 Just returns a list of all the borrowers of type I, borrownumber and name
1479 sub get_institutions {
1480 my $dbh = C4::Context->dbh();
1483 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1487 while ( my $data = $sth->fetchrow_hashref() ) {
1488 $orgs{ $data->{'borrowernumber'} } = $data;
1493 } # sub get_institutions
1495 =head2 add_member_orgs
1497 add_member_orgs($borrowernumber,$borrowernumbers);
1499 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1504 sub add_member_orgs {
1505 my ( $borrowernumber, $otherborrowers ) = @_;
1506 my $dbh = C4::Context->dbh();
1508 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1509 my $sth = $dbh->prepare($query);
1510 foreach my $otherborrowernumber (@$otherborrowers) {
1511 $sth->execute( $borrowernumber, $otherborrowernumber );
1515 } # sub add_member_orgs
1517 =head2 GetCities (OUEST-PROVENCE)
1519 ($id_cityarrayref, $city_hashref) = &GetCities();
1521 Looks up the different city and zip in the database. Returns two
1522 elements: a reference-to-array, which lists the zip city
1523 codes, and a reference-to-hash, which maps the name of the city.
1524 WHERE =>OUEST PROVENCE OR EXTERIEUR
1530 #my ($type_city) = @_;
1531 my $dbh = C4::Context->dbh;
1532 my $query = qq|SELECT cityid,city_name
1534 ORDER BY city_name|;
1535 my $sth = $dbh->prepare($query);
1537 #$sth->execute($type_city);
1542 # insert empty value to create a empty choice in cgi popup
1544 while ( my $data = $sth->fetchrow_hashref ) {
1546 push @id, $data->{'cityid'};
1547 $city{ $data->{'cityid'} } = $data->{'city_name'};
1550 #test to know if the table contain some records if no the function return nothing
1558 return ( \@id, \%city );
1562 =head2 GetSortDetails (OUEST-PROVENCE)
1564 ($lib) = &GetSortDetails($category,$sortvalue);
1566 Returns the authorized value details
1567 C<&$lib>return value of authorized value details
1568 C<&$sortvalue>this is the value of authorized value
1569 C<&$category>this is the value of authorized value category
1573 sub GetSortDetails {
1574 my ( $category, $sortvalue ) = @_;
1575 my $dbh = C4::Context->dbh;
1576 my $query = qq|SELECT lib
1577 FROM authorised_values
1579 AND authorised_value=? |;
1580 my $sth = $dbh->prepare($query);
1581 $sth->execute( $category, $sortvalue );
1582 my $lib = $sth->fetchrow;
1586 =head2 DeleteBorrower
1588 () = &DeleteBorrower($member);
1590 delete all data fo borrowers and add record to deletedborrowers table
1591 C<&$member>this is the borrowernumber
1595 sub MoveMemberToDeleted {
1597 my $dbh = C4::Context->dbh;
1599 $query = qq|SELECT *
1601 WHERE borrowernumber=?|;
1602 my $sth = $dbh->prepare($query);
1603 $sth->execute($member);
1604 my @data = $sth->fetchrow_array;
1607 $dbh->prepare( "INSERT INTO deletedborrowers VALUES ("
1608 . ( "?," x ( scalar(@data) - 1 ) )
1610 $sth->execute(@data);
1616 DelMember($borrowernumber);
1618 This function remove directly a borrower whitout writing it on deleteborrower.
1619 + Deletes reserves for the borrower
1624 my $dbh = C4::Context->dbh;
1625 my $borrowernumber = shift;
1626 warn "in delmember with $borrowernumber";
1627 return unless $borrowernumber; # borrowernumber is mandatory.
1629 my $query = qq|DELETE
1631 WHERE borrowernumber=?|;
1632 my $sth = $dbh->prepare($query);
1633 $sth->execute($borrowernumber);
1638 WHERE borrowernumber = ?
1640 my $sth = $dbh->prepare($query);
1641 $sth->execute($borrowernumber);
1643 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$borrowernumber,"")
1644 if C4::Context->preference("BorrowersLog");
1648 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1650 $date= ExtendMemberSubscriptionTo($borrowerid, $date);
1651 Extending the subscription to a given date or to the expiry date calculated on local date.
1655 sub ExtendMemberSubscriptionTo {
1656 my ( $borrowerid,$date) = @_;
1657 my $dbh = C4::Context->dbh;
1659 $date=POSIX::strftime("%Y-%m-%d",localtime(time));
1660 my $borrower = GetMember($borrowerid,'borrowernumber');
1661 $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
1663 my $sth = $dbh->do(<<EOF);
1665 SET dateexpiry='$date'
1666 WHERE borrowernumber='$borrowerid'
1668 return $date if ($sth);
1672 =head2 GetRoadTypes (OUEST-PROVENCE)
1674 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1676 Looks up the different road type . Returns two
1677 elements: a reference-to-array, which lists the id_roadtype
1678 codes, and a reference-to-hash, which maps the road type of the road .
1684 my $dbh = C4::Context->dbh;
1686 SELECT roadtypeid,road_type
1688 ORDER BY road_type|;
1689 my $sth = $dbh->prepare($query);
1694 # insert empty value to create a empty choice in cgi popup
1696 while ( my $data = $sth->fetchrow_hashref ) {
1698 push @id, $data->{'roadtypeid'};
1699 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1702 #test to know if the table contain some records if no the function return nothing
1710 return ( \@id, \%roadtype );
1716 =head2 GetTitles (OUEST-PROVENCE)
1718 ($borrowertitle)= &GetTitles();
1720 Looks up the different title . Returns array with all borrowers title
1725 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1726 unshift( @borrowerTitle, "" );
1727 return ( \@borrowerTitle);
1732 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1734 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1736 Returns the description of roadtype
1737 C<&$roadtype>return description of road type
1738 C<&$roadtypeid>this is the value of roadtype s
1742 sub GetRoadTypeDetails {
1743 my ($roadtypeid) = @_;
1744 my $dbh = C4::Context->dbh;
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)