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
23 use C4::Dates qw(format_date_in_iso);
24 use Digest::MD5 qw(md5_base64);
25 use Date::Calc qw/Today Add_Delta_YM/;
26 use C4::Log; # logaction
31 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,$debug);
35 $debug = $ENV{DEBUG} || 0;
46 &GetMemberIssuesAndFines
63 &GetMemberAccountRecords
64 &GetBorNotifyAcctRecord
69 &GetBorrowersWhoHaveNotBorrowedSince
70 &GetBorrowersWhoHaveNeverBorrowed
71 &GetBorrowersWithIssuesHistoryOlderThan
92 &ExtendMemberSubscriptionTo
109 C4::Members - Perl Module containing convenience functions for member handling
117 This module contains routines for adding, modifying and deleting members/patrons/borrowers
125 ($count, $borrowers) = &SearchMember($searchstring, $type,$category_type,$filter,$showallbranches);
127 Looks up patrons (borrowers) by name.
129 BUGFIX 499: C<$type> is now used to determine type of search.
130 if $type is "simple", search is performed on the first letter of the
133 $category_type is used to get a specified type of user.
134 (mainly adults when creating a child.)
136 C<$searchstring> is a space-separated list of search terms. Each term
137 must match the beginning a borrower's surname, first name, or other
140 C<$filter> is assumed to be a list of elements to filter results on
142 C<$showallbranches> is used in IndependantBranches Context to display all branches results.
144 C<&SearchMember> returns a two-element list. C<$borrowers> is a
145 reference-to-array; each element is a reference-to-hash, whose keys
146 are the fields of the C<borrowers> table in the Koha database.
147 C<$count> is the number of elements in C<$borrowers>.
152 #used by member enquiries from the intranet
153 #called by member.pl and circ/circulation.pl
155 my ($searchstring, $orderby, $type,$category_type,$filter,$showallbranches ) = @_;
156 my $dbh = C4::Context->dbh;
162 # this is used by circulation everytime a new borrowers cardnumber is scanned
163 # so we can check an exact match first, if that works return, otherwise do the rest
164 $query = "SELECT * FROM borrowers
165 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
167 my $sth = $dbh->prepare("$query WHERE cardnumber = ?");
168 $sth->execute($searchstring);
169 my $data = $sth->fetchall_arrayref({});
171 return ( scalar(@$data), $data );
175 if ( $type eq "simple" ) # simple search for one letter only
177 $query .= ($category_type ? " AND category_type = ".$dbh->quote($category_type) : "");
178 $query .= " WHERE (surname LIKE ? OR cardnumber like ?) ";
179 if (C4::Context->preference("IndependantBranches") && !$showallbranches){
180 if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
181 $query.=" AND borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'}) unless (C4::Context->userenv->{'branch'} eq "insecure");
184 $query.=" ORDER BY $orderby";
185 @bind = ("$searchstring%","$searchstring");
187 else # advanced search looking in surname, firstname and othernames
189 @data = split( ' ', $searchstring );
192 if (C4::Context->preference("IndependantBranches") && !$showallbranches){
193 if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
194 $query.=" borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'})." AND " unless (C4::Context->userenv->{'branch'} eq "insecure");
197 $query.="((surname LIKE ? OR surname LIKE ?
198 OR firstname LIKE ? OR firstname LIKE ?
199 OR othernames LIKE ? OR othernames LIKE ?)
201 ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
203 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
204 "$data[0]%", "% $data[0]%"
206 for ( my $i = 1 ; $i < $count ; $i++ ) {
207 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
208 OR firstname LIKE ? OR firstname LIKE ?
209 OR othernames LIKE ? OR othernames LIKE ?)";
211 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
212 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
216 $query = $query . ") OR cardnumber LIKE ?
218 push( @bind, $searchstring );
223 $sth = $dbh->prepare($query);
225 $debug and print STDERR "Q $orderby : $query\n";
226 $sth->execute(@bind);
228 $data = $sth->fetchall_arrayref({});
231 return ( scalar(@$data), $data );
234 =head2 GetMemberDetails
236 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
238 Looks up a patron and returns information about him or her. If
239 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
240 up the borrower by number; otherwise, it looks up the borrower by card
243 C<$borrower> is a reference-to-hash whose keys are the fields of the
244 borrowers table in the Koha database. In addition,
245 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
246 about the patron. Its keys act as flags :
248 if $borrower->{flags}->{LOST} {
249 # Patron's card was reported lost
252 Each flag has a C<message> key, giving a human-readable explanation of
253 the flag. If the state of a flag means that the patron should not be
254 allowed to borrow any more books, then it will have a C<noissues> key
257 The possible flags are:
263 =item Shows the patron's credit or debt, if any.
271 =item (Gone, no address.) Set if the patron has left without giving a
280 =item Set if the patron's card has been reported as lost.
288 =item Set if the patron has been debarred.
296 =item Any additional notes about the patron.
304 =item Set if the patron has overdue items. This flag has several keys:
306 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
307 overdue items. Its elements are references-to-hash, each describing an
308 overdue item. The keys are selected fields from the issues, biblio,
309 biblioitems, and items tables of the Koha database.
311 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
312 the overdue items, one per line.
320 =item Set if any items that the patron has reserved are available.
322 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
323 available items. Each element is a reference-to-hash whose keys are
324 fields from the reserves table of the Koha database.
330 sub GetMemberDetails {
331 my ( $borrowernumber, $cardnumber ) = @_;
332 my $dbh = C4::Context->dbh;
335 if ($borrowernumber) {
336 $sth = $dbh->prepare("select borrowers.*,category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?");
337 $sth->execute($borrowernumber);
339 elsif ($cardnumber) {
340 $sth = $dbh->prepare("select borrowers.*,category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?");
341 $sth->execute($cardnumber);
346 my $borrower = $sth->fetchrow_hashref;
347 my ($amount) = GetMemberAccountRecords( $borrowernumber);
348 $borrower->{'amountoutstanding'} = $amount;
349 # FIXME - patronflags calls GetMemberAccountRecords... just have patronflags return $amount
350 my $flags = patronflags( $borrower);
353 $sth = $dbh->prepare("select bit,flag from userflags");
355 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
356 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
357 $accessflagshash->{$flag} = 1;
361 $borrower->{'flags'} = $flags;
362 $borrower->{'authflags'} = $accessflagshash;
364 # find out how long the membership lasts
367 "select enrolmentperiod from categories where categorycode = ?");
368 $sth->execute( $borrower->{'categorycode'} );
369 my $enrolment = $sth->fetchrow;
370 $borrower->{'enrolmentperiod'} = $enrolment;
371 return ($borrower); #, $flags, $accessflagshash);
378 NOTE!: If you change this function, be sure to update the POD for
381 $flags = &patronflags($patron);
384 {message} Message showing patron's credit or debt
385 {noissues} Set if patron owes >$5.00
386 {GNA} Set if patron gone w/o address
387 {message} "Borrower has no valid address"
389 {LOST} Set if patron's card reported lost
390 {message} Message to this effect
392 {DBARRED} Set is patron is debarred
393 {message} Message to this effect
395 {NOTES} Set if patron has notes
396 {message} Notes about patron
397 {ODUES} Set if patron has overdue books
399 {itemlist} ref-to-array: list of overdue books
400 {itemlisttext} Text list of overdue items
401 {WAITING} Set if there are items available that the
403 {message} Message to this effect
404 {itemlist} ref-to-array: list of available items
407 # FIXME rename this function.
410 my ( $patroninformation) = @_;
411 my $dbh=C4::Context->dbh;
412 my ($amount) = GetMemberAccountRecords( $patroninformation->{'borrowernumber'});
415 my $noissuescharge = C4::Context->preference("noissuescharge");
416 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
417 $flaginfo{'amount'} = sprintf "%.02f",$amount;
418 if ( $amount > $noissuescharge ) {
419 $flaginfo{'noissues'} = 1;
421 $flags{'CHARGES'} = \%flaginfo;
423 elsif ( $amount < 0 ) {
425 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
426 $flags{'CREDITS'} = \%flaginfo;
428 if ( $patroninformation->{'gonenoaddress'}
429 && $patroninformation->{'gonenoaddress'} == 1 )
432 $flaginfo{'message'} = 'Borrower has no valid address.';
433 $flaginfo{'noissues'} = 1;
434 $flags{'GNA'} = \%flaginfo;
436 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
438 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
439 $flaginfo{'noissues'} = 1;
440 $flags{'LOST'} = \%flaginfo;
442 if ( $patroninformation->{'debarred'}
443 && $patroninformation->{'debarred'} == 1 )
446 $flaginfo{'message'} = 'Borrower is Debarred.';
447 $flaginfo{'noissues'} = 1;
448 $flags{'DBARRED'} = \%flaginfo;
450 if ( $patroninformation->{'borrowernotes'}
451 && $patroninformation->{'borrowernotes'} )
454 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
455 $flags{'NOTES'} = \%flaginfo;
457 my ( $odues, $itemsoverdue ) =
458 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
461 $flaginfo{'message'} = "Yes";
462 $flaginfo{'itemlist'} = $itemsoverdue;
463 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
466 $flaginfo{'itemlisttext'} .=
467 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
469 $flags{'ODUES'} = \%flaginfo;
471 my @itemswaiting = C4::Reserves::GetReservesFromBorrowernumber( $patroninformation->{'borrowernumber'},'W' );
472 my $nowaiting = scalar @itemswaiting;
473 if ( $nowaiting > 0 ) {
475 $flaginfo{'message'} = "Reserved items available";
476 $flaginfo{'itemlist'} = \@itemswaiting;
477 $flags{'WAITING'} = \%flaginfo;
485 $borrower = &GetMember($information, $type);
487 Looks up information about a patron (borrower) by either card number
488 ,firstname, or borrower number, depending on $type value.
489 If C<$type> == 'cardnumber', C<&GetBorrower>
490 searches by cardnumber then by firstname if not found in cardnumber;
491 otherwise, it searches by borrowernumber.
493 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
494 the C<borrowers> table in the Koha database.
500 my ( $information, $type ) = @_;
501 my $dbh = C4::Context->dbh;
504 SELECT borrowers.*, categories.category_type, categories.description
506 LEFT JOIN categories on borrowers.categorycode=categories.categorycode
508 if ($type eq 'cardnumber' || $type eq 'firstname'|| $type eq 'userid'|| $type eq 'borrowernumber'){
509 $information = uc $information;
510 $sth = $dbh->prepare("$select WHERE $type=?");
512 $sth = $dbh->prepare("$select WHERE borrowernumber=?");
514 $sth->execute($information);
515 my $data = $sth->fetchrow_hashref;
517 ($data) and return ($data);
519 if ($type eq 'cardnumber' || $type eq 'firstname') { # otherwise, try with firstname
520 $sth = $dbh->prepare("$select WHERE firstname like ?");
521 $sth->execute($information);
522 $data = $sth->fetchrow_hashref;
524 ($data) and return ($data);
529 =item GetMemberIssuesAndFines
531 ($borrowed, $due, $fine) = &GetMemberIssuesAndFines($borrowernumber);
533 Returns aggregate data about items borrowed by the patron with the
534 given borrowernumber.
536 C<&GetMemberIssuesAndFines> returns a three-element array. C<$borrowed> is the
537 number of books the patron currently has borrowed. C<$due> is the
538 number of overdue items the patron currently has borrowed. C<$fine> is
539 the total fine currently due by the borrower.
544 sub GetMemberIssuesAndFines {
545 my ( $borrowernumber ) = @_;
546 my $dbh = C4::Context->dbh;
548 "Select count(*) from issues where borrowernumber='$borrowernumber' and
551 $debug and warn $query."\n";
552 my $sth = $dbh->prepare($query);
554 my $data = $sth->fetchrow_hashref;
556 $sth = $dbh->prepare(
557 "Select count(*) from issues where
558 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
561 my $data2 = $sth->fetchrow_hashref;
563 $sth = $dbh->prepare(
564 "Select sum(amountoutstanding) from accountlines where
565 borrowernumber='$borrowernumber'"
568 my $data3 = $sth->fetchrow_hashref;
571 return ( $data2->{'count(*)'}, $data->{'count(*)'},
572 $data3->{'sum(amountoutstanding)'} );
579 &ModMember($borrowernumber);
581 Modify borrower's data. All date fields should ALREADY be in ISO format.
588 my $dbh = C4::Context->dbh;
589 my $iso_re = C4::Dates->new()->regexp('iso');
590 foreach (qw(dateofbirth dateexpiry dateenrolled)) {
591 if (my $tempdate = $data{$_}) { # assignment, not comparison
592 ($tempdate =~ /$iso_re/) and next; # Congatulations, you sent a valid ISO date.
593 warn "ModMember given $_ not in ISO format ($tempdate)";
594 if (my $tempdate2 = format_date_in_iso($tempdate)) { # assignment, not comparison
595 $data{$_} = $tempdate2;
597 warn "ModMember cannot convert '$tempdate' (from syspref)";
601 if (!$data{'dateofbirth'}){
602 undef $data{'dateofbirth'};
604 my $qborrower=$dbh->prepare("SHOW columns from borrowers");
606 my %hashborrowerfields;
607 while (my ($field)=$qborrower->fetchrow){
608 $hashborrowerfields{$field}=1;
610 my $query = "UPDATE borrowers SET \n";
614 # test to know if you must update or not the borrower password
615 if ( $data{'password'} eq '****' ) {
616 delete $data{'password'};
618 $data{'password'} = md5_base64( $data{'password'} ) if ($data{'password'} ne "");
619 delete $data{'password'} if ($data{password} eq "");
622 { push @parameters,"$_ = ".$dbh->quote($data{$_}) if ($_ ne 'borrowernumber' and $_ ne 'flags' and $hashborrowerfields{$_}); }
623 $query .= join (',',@parameters) . "\n WHERE borrowernumber=? \n";
624 $debug and print STDERR "$query (executed w/ arg: $data{'borrowernumber'})";
625 $sth = $dbh->prepare($query);
626 $sth->execute($data{'borrowernumber'});
629 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
630 # so when we update information for an adult we should check for guarantees and update the relevant part
631 # of their records, ie addresses and phone numbers
632 my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
633 if ( $borrowercategory->{'category_type'} eq ('A' || 'S') ) {
634 # is adult check guarantees;
635 UpdateGuarantees(%data);
637 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
638 if C4::Context->preference("BorrowersLog");
646 $borrowernumber = &AddMember(%borrower);
648 insert new borrower into table
649 Returns the borrowernumber
656 my $dbh = C4::Context->dbh;
657 $data{'userid'} = '' unless $data{'password'};
658 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
660 # WE SHOULD NEVER PASS THIS SUBROUTINE ANYTHING OTHER THAN ISO DATES
661 # IF YOU UNCOMMENT THESE LINES YOU BETTER HAVE A DARN COMPELLING REASON
662 # $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
663 # $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'});
664 # $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
665 # This query should be rewritten to use "?" at execute.
666 if (!$data{'dateofbirth'}){
667 undef ($data{'dateofbirth'});
670 "insert into borrowers set cardnumber=" . $dbh->quote( $data{'cardnumber'} )
671 . ",surname=" . $dbh->quote( $data{'surname'} )
672 . ",firstname=" . $dbh->quote( $data{'firstname'} )
673 . ",title=" . $dbh->quote( $data{'title'} )
674 . ",othernames=" . $dbh->quote( $data{'othernames'} )
675 . ",initials=" . $dbh->quote( $data{'initials'} )
676 . ",streetnumber=". $dbh->quote( $data{'streetnumber'} )
677 . ",streettype=" . $dbh->quote( $data{'streettype'} )
678 . ",address=" . $dbh->quote( $data{'address'} )
679 . ",address2=" . $dbh->quote( $data{'address2'} )
680 . ",zipcode=" . $dbh->quote( $data{'zipcode'} )
681 . ",city=" . $dbh->quote( $data{'city'} )
682 . ",phone=" . $dbh->quote( $data{'phone'} )
683 . ",email=" . $dbh->quote( $data{'email'} )
684 . ",mobile=" . $dbh->quote( $data{'mobile'} )
685 . ",phonepro=" . $dbh->quote( $data{'phonepro'} )
686 . ",opacnote=" . $dbh->quote( $data{'opacnote'} )
687 . ",guarantorid=" . $dbh->quote( $data{'guarantorid'} )
688 . ",dateofbirth=" . $dbh->quote( $data{'dateofbirth'} )
689 . ",branchcode=" . $dbh->quote( $data{'branchcode'} )
690 . ",categorycode=" . $dbh->quote( $data{'categorycode'} )
691 . ",dateenrolled=" . $dbh->quote( $data{'dateenrolled'} )
692 . ",contactname=" . $dbh->quote( $data{'contactname'} )
693 . ",borrowernotes=" . $dbh->quote( $data{'borrowernotes'} )
694 . ",dateexpiry=" . $dbh->quote( $data{'dateexpiry'} )
695 . ",contactnote=" . $dbh->quote( $data{'contactnote'} )
696 . ",B_address=" . $dbh->quote( $data{'B_address'} )
697 . ",B_zipcode=" . $dbh->quote( $data{'B_zipcode'} )
698 . ",B_city=" . $dbh->quote( $data{'B_city'} )
699 . ",B_phone=" . $dbh->quote( $data{'B_phone'} )
700 . ",B_email=" . $dbh->quote( $data{'B_email'} )
701 . ",password=" . $dbh->quote( $data{'password'} )
702 . ",userid=" . $dbh->quote( $data{'userid'} )
703 . ",sort1=" . $dbh->quote( $data{'sort1'} )
704 . ",sort2=" . $dbh->quote( $data{'sort2'} )
705 . ",contacttitle=" . $dbh->quote( $data{'contacttitle'} )
706 . ",emailpro=" . $dbh->quote( $data{'emailpro'} )
707 . ",contactfirstname=" . $dbh->quote( $data{'contactfirstname'} )
708 . ",sex=" . $dbh->quote( $data{'sex'} )
709 . ",fax=" . $dbh->quote( $data{'fax'} )
710 . ",relationship=" . $dbh->quote( $data{'relationship'} )
711 . ",B_streetnumber=" . $dbh->quote( $data{'B_streetnumber'} )
712 . ",B_streettype=" . $dbh->quote( $data{'B_streettype'} )
713 . ",gonenoaddress=" . $dbh->quote( $data{'gonenoaddress'} )
714 . ",lost=" . $dbh->quote( $data{'lost'} )
715 . ",debarred=" . $dbh->quote( $data{'debarred'} )
716 . ",ethnicity=" . $dbh->quote( $data{'ethnicity'} )
717 . ",ethnotes=" . $dbh->quote( $data{'ethnotes'} )
718 . ",altcontactsurname=" . $dbh->quote( $data{'altcontactsurname'} )
719 . ",altcontactfirstname=" . $dbh->quote( $data{'altcontactfirstname'} )
720 . ",altcontactaddress1=" . $dbh->quote( $data{'altcontactaddress1'} )
721 . ",altcontactaddress2=" . $dbh->quote( $data{'altcontactaddress2'} )
722 . ",altcontactaddress3=" . $dbh->quote( $data{'altcontactaddress3'} )
723 . ",altcontactzipcode=" . $dbh->quote( $data{'altcontactzipcode'} )
724 . ",altcontactphone=" . $dbh->quote( $data{'altcontactphone'} ) ;
725 $debug and print STDERR "AddMember SQL: ($query)\n";
726 my $sth = $dbh->prepare($query);
727 # print "Executing SQL: $query\n";
730 $data{'borrowernumber'} = $dbh->{'mysql_insertid'}; # unneeded w/ autoincrement ?
731 # mysql_insertid is probably bad. not necessarily accurate and mysql-specific at best.
733 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
734 if C4::Context->preference("BorrowersLog");
736 # check for enrollment fee & add it if needed
737 $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode=?");
738 $sth->execute($data{'categorycode'});
739 my ($enrolmentfee) = $sth->fetchrow;
741 # insert fee in patron debts
742 manualinvoice($data{'borrowernumber'}, '', '', 'A', $enrolmentfee);
744 return $data{'borrowernumber'};
748 my ($uid,$member) = @_;
749 my $dbh = C4::Context->dbh;
750 # Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
751 # Then we need to tell the user and have them create a new one.
754 "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
755 $sth->execute( $uid, $member );
756 if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
766 my ( $uid, $member, $digest ) = @_;
767 my $dbh = C4::Context->dbh;
769 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
770 #Then we need to tell the user and have them create a new one.
773 "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
774 $sth->execute( $uid, $member );
775 if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
779 #Everything is good so we can update the information.
782 "update borrowers set userid=?, password=? where borrowernumber=?");
783 $sth->execute( $uid, $digest, $member );
787 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
788 if C4::Context->preference("BorrowersLog");
793 =item fixup_cardnumber
795 Warning: The caller is responsible for locking the members table in write
796 mode, to avoid database corruption.
800 use vars qw( @weightings );
801 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
803 sub fixup_cardnumber ($) {
804 my ($cardnumber) = @_;
805 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
806 $autonumber_members = 0 unless defined $autonumber_members;
808 # Find out whether member numbers should be generated
809 # automatically. Should be either "1" or something else.
810 # Defaults to "0", which is interpreted as "no".
812 # if ($cardnumber !~ /\S/ && $autonumber_members) {
813 if ($autonumber_members) {
814 my $dbh = C4::Context->dbh;
815 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
817 # if checkdigit is selected, calculate katipo-style cardnumber.
818 # otherwise, just use the max()
819 # purpose: generate checksum'd member numbers.
820 # We'll assume we just got the max value of digits 2-8 of member #'s
821 # from the database and our job is to increment that by one,
822 # determine the 1st and 9th digits and return the full string.
825 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
829 my $data = $sth->fetchrow_hashref;
830 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
832 if ( !$cardnumber ) { # If DB has no values,
833 $cardnumber = 1000000; # start at 1000000
840 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
842 # read weightings, left to right, 1 char at a time
843 my $temp1 = $weightings[$i];
845 # sequence left to right, 1 char at a time
846 my $temp2 = substr( $cardnumber, $i, 1 );
848 # mult each char 1-7 by its corresponding weighting
849 $sum += $temp1 * $temp2;
852 my $rem = ( $sum % 11 );
853 $rem = 'X' if $rem == 10;
855 $cardnumber = "V$cardnumber$rem";
859 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
860 # better. I'll leave the original in in case it needs to be changed for you
863 "select max(cast(cardnumber as signed)) from borrowers");
865 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
869 my ($result) = $sth->fetchrow;
871 $cardnumber = $result + 1;
879 ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
880 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
881 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
883 C<&GetGuarantees> takes a borrower number (e.g., that of a patron
884 with children) and looks up the borrowers who are guaranteed by that
885 borrower (i.e., the patron's children).
887 C<&GetGuarantees> returns two values: an integer giving the number of
888 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
889 of references to hash, which gives the actual results.
895 my ($borrowernumber) = @_;
896 my $dbh = C4::Context->dbh;
899 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
901 $sth->execute($borrowernumber);
904 my $data = $sth->fetchall_arrayref({});
906 return ( scalar(@$data), $data );
909 =head2 UpdateGuarantees
911 &UpdateGuarantees($parent_borrno);
914 C<&UpdateGuarantees> borrower data for an adulte and updates all the guarantees
915 with the modified information
920 sub UpdateGuarantees {
922 my $dbh = C4::Context->dbh;
923 my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
924 for ( my $i = 0 ; $i < $count ; $i++ ) {
927 # It looks like the $i is only being returned to handle walking through
928 # the array, which is probably better done as a foreach loop.
930 my $guaquery = qq|UPDATE borrowers
931 SET address='$data{'address'}',fax='$data{'fax'}',
932 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
933 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
935 my $sth3 = $dbh->prepare($guaquery);
940 =head2 GetPendingIssues
942 ($count, $issues) = &GetPendingIssues($borrowernumber);
944 Looks up what the patron with the given borrowernumber has borrowed.
946 C<&GetPendingIssues> returns a two-element array. C<$issues> is a
947 reference-to-array, where each element is a reference-to-hash; the
948 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
949 in the Koha database. C<$count> is the number of elements in
955 sub GetPendingIssues {
956 my ($borrowernumber) = @_;
957 my $dbh = C4::Context->dbh;
959 my $sth = $dbh->prepare(
960 "SELECT * FROM issues
961 LEFT JOIN items ON issues.itemnumber=items.itemnumber
962 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
963 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
966 AND returndate IS NULL
967 ORDER BY issues.issuedate"
969 $sth->execute($borrowernumber);
970 my $data = $sth->fetchall_arrayref({});
971 my $today = POSIX::strftime("%Y%m%d", localtime);
973 my $datedue = $_->{'date_due'};
975 if ( $datedue < $today ) {
980 return ( scalar(@$data), $data );
985 ($count, $issues) = &GetAllIssues($borrowernumber, $sortkey, $limit);
987 Looks up what the patron with the given borrowernumber has borrowed,
988 and sorts the results.
990 C<$sortkey> is the name of a field on which to sort the results. This
991 should be the name of a field in the C<issues>, C<biblio>,
992 C<biblioitems>, or C<items> table in the Koha database.
994 C<$limit> is the maximum number of results to return.
996 C<&GetAllIssues> returns a two-element array. C<$issues> is a
997 reference-to-array, where each element is a reference-to-hash; the
998 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
999 C<items> tables of the Koha database. C<$count> is the number of
1000 elements in C<$issues>
1006 my ( $borrowernumber, $order, $limit ) = @_;
1008 #FIXME: sanity-check order and limit
1009 my $dbh = C4::Context->dbh;
1012 "Select *,items.timestamp AS itemstimestamp from
1014 LEFT JOIN items on items.itemnumber=issues.itemnumber
1015 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1016 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1017 where borrowernumber=?
1019 if ( $limit != 0 ) {
1020 $query .= " limit $limit";
1024 my $sth = $dbh->prepare($query);
1025 $sth->execute($borrowernumber);
1028 while ( my $data = $sth->fetchrow_hashref ) {
1029 $result[$i] = $data;
1034 # get all issued items for borrowernumber from oldissues table
1035 # large chunk of older issues data put into table oldissues
1036 # to speed up db calls for issuing items
1037 if ( C4::Context->preference("ReadingHistory") ) {
1038 my $query2 = "SELECT * FROM oldissues
1039 LEFT JOIN items ON items.itemnumber=oldissues.itemnumber
1040 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1041 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1042 WHERE borrowernumber=?
1044 if ( $limit != 0 ) {
1045 $limit = $limit - $count;
1046 $query2 .= " limit $limit";
1049 my $sth2 = $dbh->prepare($query2);
1050 $sth2->execute($borrowernumber);
1052 while ( my $data2 = $sth2->fetchrow_hashref ) {
1053 $result[$i] = $data2;
1060 return ( $i, \@result );
1064 =head2 GetMemberAccountRecords
1066 ($total, $acctlines, $count) = &GetMemberAccountRecords($borrowernumber);
1068 Looks up accounting data for the patron with the given borrowernumber.
1070 C<&GetMemberAccountRecords> returns a three-element array. C<$acctlines> is a
1071 reference-to-array, where each element is a reference-to-hash; the
1072 keys are the fields of the C<accountlines> table in the Koha database.
1073 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1074 total amount outstanding for all of the account lines.
1079 sub GetMemberAccountRecords {
1080 my ($borrowernumber,$date) = @_;
1081 my $dbh = C4::Context->dbh;
1087 WHERE borrowernumber=?);
1088 my @bind = ($borrowernumber);
1089 if ($date && $date ne ''){
1090 $strsth.=" AND date < ? ";
1093 $strsth.=" ORDER BY date desc,timestamp DESC";
1094 my $sth= $dbh->prepare( $strsth );
1095 $sth->execute( @bind );
1097 while ( my $data = $sth->fetchrow_hashref ) {
1098 $acctlines[$numlines] = $data;
1100 $total += $data->{'amountoutstanding'};
1103 return ( $total, \@acctlines,$numlines);
1106 =head2 GetBorNotifyAcctRecord
1108 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1110 Looks up accounting data for the patron with the given borrowernumber per file number.
1112 (FIXME - I'm not at all sure what this is about.)
1114 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1115 reference-to-array, where each element is a reference-to-hash; the
1116 keys are the fields of the C<accountlines> table in the Koha database.
1117 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1118 total amount outstanding for all of the account lines.
1122 sub GetBorNotifyAcctRecord {
1123 my ( $borrowernumber, $notifyid ) = @_;
1124 my $dbh = C4::Context->dbh;
1127 my $sth = $dbh->prepare(
1130 WHERE borrowernumber=?
1132 AND amountoutstanding != '0'
1133 ORDER BY notify_id,accounttype
1135 # 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')
1137 $sth->execute( $borrowernumber, $notifyid );
1139 while ( my $data = $sth->fetchrow_hashref ) {
1140 $acctlines[$numlines] = $data;
1142 $total += $data->{'amountoutstanding'};
1145 return ( $total, \@acctlines, $numlines );
1148 =head2 checkuniquemember (OUEST-PROVENCE)
1150 ($result,$categorycode) = &checkuniquemember($collectivity,$surname,$firstname,$dateofbirth);
1152 Checks that a member exists or not in the database.
1154 C<&result> is nonzero (=exist) or 0 (=does not exist)
1155 C<&categorycode> is from categorycode table
1156 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1157 C<&surname> is the surname
1158 C<&firstname> is the firstname (only if collectivity=0)
1159 C<&dateofbirth> is the date of birth in ISO format (only if collectivity=0)
1163 # FIXME: This function is not legitimate. Multiple patrons might have the same first/last name and birthdate.
1164 # This is especially true since first name is not even a required field.
1166 sub checkuniquemember {
1167 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1168 my $dbh = C4::Context->dbh;
1169 my $request = ($collectivity) ?
1170 "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? " :
1171 "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? and firstname=? and dateofbirth=? ";
1172 my $sth = $dbh->prepare($request);
1173 if ($collectivity) {
1174 $sth->execute( uc($surname) );
1176 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1178 my @data = $sth->fetchrow;
1180 ( $data[0] ) and return $data[0], $data[1];
1184 sub checkcardnumber {
1185 my ($cardnumber,$borrowernumber) = @_;
1186 my $dbh = C4::Context->dbh;
1187 my $query = "SELECT * FROM borrowers WHERE cardnumber=?";
1188 $query .= " AND borrowernumber <> ?" if ($borrowernumber);
1189 my $sth = $dbh->prepare($query);
1190 if ($borrowernumber) {
1191 $sth->execute($cardnumber,$borrowernumber);
1193 $sth->execute($cardnumber);
1195 if (my $data= $sth->fetchrow_hashref()){
1205 =head2 getzipnamecity (OUEST-PROVENCE)
1207 take all info from table city for the fields city and zip
1208 check for the name and the zip code of the city selected
1212 sub getzipnamecity {
1214 my $dbh = C4::Context->dbh;
1217 "select city_name,city_zipcode from cities where cityid=? ");
1218 $sth->execute($cityid);
1219 my @data = $sth->fetchrow;
1220 return $data[0], $data[1];
1224 =head2 getdcity (OUEST-PROVENCE)
1226 recover cityid with city_name condition
1231 my ($city_name) = @_;
1232 my $dbh = C4::Context->dbh;
1233 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1234 $sth->execute($city_name);
1235 my $data = $sth->fetchrow;
1240 =head2 GetExpiryDate
1242 $expirydate = GetExpiryDate($categorycode, $dateenrolled);
1244 Calculate expiry date given a categorycode and starting date. Date argument must be in ISO format.
1245 Return date is also in ISO format.
1250 my ( $categorycode, $dateenrolled ) = @_;
1251 my $enrolmentperiod = 12; # reasonable default
1252 if ($categorycode) {
1253 my $dbh = C4::Context->dbh;
1254 my $sth = $dbh->prepare("select enrolmentperiod from categories where categorycode=?");
1255 $sth->execute($categorycode);
1256 $enrolmentperiod = $sth->fetchrow;
1258 # die "GetExpiryDate: for enrollmentperiod $enrolmentperiod (category '$categorycode') starting $dateenrolled.\n";
1259 my @date = split /-/,$dateenrolled;
1260 return sprintf("%04d-%02d-%02d", Add_Delta_YM(@date,0,$enrolmentperiod));
1263 =head2 checkuserpassword (OUEST-PROVENCE)
1265 check for the password and login are not used
1266 return the number of record
1267 0=> NOT USED 1=> USED
1271 sub checkuserpassword {
1272 my ( $borrowernumber, $userid, $password ) = @_;
1273 $password = md5_base64($password);
1274 my $dbh = C4::Context->dbh;
1277 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1279 $sth->execute( $borrowernumber, $userid, $password );
1280 my $number_rows = $sth->fetchrow;
1281 return $number_rows;
1285 =head2 GetborCatFromCatType
1287 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1289 Looks up the different types of borrowers in the database. Returns two
1290 elements: a reference-to-array, which lists the borrower category
1291 codes, and a reference-to-hash, which maps the borrower category codes
1292 to category descriptions.
1297 sub GetborCatFromCatType {
1298 my ( $category_type, $action ) = @_;
1299 my $dbh = C4::Context->dbh;
1300 my $request = qq| SELECT categorycode,description
1303 ORDER BY categorycode|;
1304 my $sth = $dbh->prepare($request);
1306 $sth->execute($category_type);
1315 while ( my $data = $sth->fetchrow_hashref ) {
1316 push @codes, $data->{'categorycode'};
1317 $labels{ $data->{'categorycode'} } = $data->{'description'};
1320 return ( \@codes, \%labels );
1323 =head2 GetBorrowercategory
1325 $hashref = &GetBorrowercategory($categorycode);
1327 Given the borrower's category code, the function returns the corresponding
1328 data hashref for a comprehensive information display.
1332 sub GetBorrowercategory {
1334 my $dbh = C4::Context->dbh;
1337 "SELECT description,dateofbirthrequired,upperagelimit,category_type
1339 WHERE categorycode = ?"
1341 $sth->execute($catcode);
1343 $sth->fetchrow_hashref;
1346 } # sub getborrowercategory
1348 =head2 ethnicitycategories
1350 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1352 Looks up the different ethnic types in the database. Returns two
1353 elements: a reference-to-array, which lists the ethnicity codes, and a
1354 reference-to-hash, which maps the ethnicity codes to ethnicity
1361 sub ethnicitycategories {
1362 my $dbh = C4::Context->dbh;
1363 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1367 while ( my $data = $sth->fetchrow_hashref ) {
1368 push @codes, $data->{'code'};
1369 $labels{ $data->{'code'} } = $data->{'name'};
1372 return ( \@codes, \%labels );
1377 $ethn_name = &fixEthnicity($ethn_code);
1379 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1380 corresponding descriptive name from the C<ethnicity> table in the
1381 Koha database ("European" or "Pacific Islander").
1388 my $ethnicity = shift;
1389 return unless $ethnicity;
1390 my $dbh = C4::Context->dbh;
1391 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1392 $sth->execute($ethnicity);
1393 my $data = $sth->fetchrow_hashref;
1395 return $data->{'name'};
1396 } # sub fixEthnicity
1400 $dateofbirth,$date = &GetAge($date);
1402 this function return the borrowers age with the value of dateofbirth
1408 my ( $date, $date_ref ) = @_;
1410 if ( not defined $date_ref ) {
1411 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1414 my ( $year1, $month1, $day1 ) = split /-/, $date;
1415 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1417 my $age = $year2 - $year1;
1418 if ( $month1 . $day1 > $month2 . $day2 ) {
1425 =head2 get_institutions
1426 $insitutions = get_institutions();
1428 Just returns a list of all the borrowers of type I, borrownumber and name
1433 sub get_institutions {
1434 my $dbh = C4::Context->dbh();
1437 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1441 while ( my $data = $sth->fetchrow_hashref() ) {
1442 $orgs{ $data->{'borrowernumber'} } = $data;
1447 } # sub get_institutions
1449 =head2 add_member_orgs
1451 add_member_orgs($borrowernumber,$borrowernumbers);
1453 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1458 sub add_member_orgs {
1459 my ( $borrowernumber, $otherborrowers ) = @_;
1460 my $dbh = C4::Context->dbh();
1462 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1463 my $sth = $dbh->prepare($query);
1464 foreach my $otherborrowernumber (@$otherborrowers) {
1465 $sth->execute( $borrowernumber, $otherborrowernumber );
1469 } # sub add_member_orgs
1471 =head2 GetCities (OUEST-PROVENCE)
1473 ($id_cityarrayref, $city_hashref) = &GetCities();
1475 Looks up the different city and zip in the database. Returns two
1476 elements: a reference-to-array, which lists the zip city
1477 codes, and a reference-to-hash, which maps the name of the city.
1478 WHERE =>OUEST PROVENCE OR EXTERIEUR
1484 #my ($type_city) = @_;
1485 my $dbh = C4::Context->dbh;
1486 my $query = qq|SELECT cityid,city_zipcode,city_name
1488 ORDER BY city_name|;
1489 my $sth = $dbh->prepare($query);
1491 #$sth->execute($type_city);
1495 # insert empty value to create a empty choice in cgi popup
1498 while ( my $data = $sth->fetchrow_hashref ) {
1499 push @id, $data->{'city_zipcode'}."|".$data->{'city_name'};
1500 $city{ $data->{'city_zipcode'}."|".$data->{'city_name'} } = $data->{'city_name'};
1503 #test to know if the table contain some records if no the function return nothing
1507 # all we have is the one blank row
1512 return ( \@id, \%city );
1516 =head2 GetSortDetails (OUEST-PROVENCE)
1518 ($lib) = &GetSortDetails($category,$sortvalue);
1520 Returns the authorized value details
1521 C<&$lib>return value of authorized value details
1522 C<&$sortvalue>this is the value of authorized value
1523 C<&$category>this is the value of authorized value category
1527 sub GetSortDetails {
1528 my ( $category, $sortvalue ) = @_;
1529 my $dbh = C4::Context->dbh;
1530 my $query = qq|SELECT lib
1531 FROM authorised_values
1533 AND authorised_value=? |;
1534 my $sth = $dbh->prepare($query);
1535 $sth->execute( $category, $sortvalue );
1536 my $lib = $sth->fetchrow;
1537 return ($lib) if ($lib);
1538 return ($sortvalue) unless ($lib);
1541 =head2 DeleteBorrower
1543 () = &DeleteBorrower($member);
1545 delete all data fo borrowers and add record to deletedborrowers table
1546 C<&$member>this is the borrowernumber
1550 sub MoveMemberToDeleted {
1552 my $dbh = C4::Context->dbh;
1554 $query = qq|SELECT *
1556 WHERE borrowernumber=?|;
1557 my $sth = $dbh->prepare($query);
1558 $sth->execute($member);
1559 my @data = $sth->fetchrow_array;
1562 $dbh->prepare( "INSERT INTO deletedborrowers VALUES ("
1563 . ( "?," x ( scalar(@data) - 1 ) )
1565 $sth->execute(@data);
1571 DelMember($borrowernumber);
1573 This function remove directly a borrower whitout writing it on deleteborrower.
1574 + Deletes reserves for the borrower
1579 my $dbh = C4::Context->dbh;
1580 my $borrowernumber = shift;
1581 #warn "in delmember with $borrowernumber";
1582 return unless $borrowernumber; # borrowernumber is mandatory.
1584 my $query = qq|DELETE
1586 WHERE borrowernumber=?|;
1587 my $sth = $dbh->prepare($query);
1588 $sth->execute($borrowernumber);
1593 WHERE borrowernumber = ?
1595 $sth = $dbh->prepare($query);
1596 $sth->execute($borrowernumber);
1598 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$borrowernumber,"")
1599 if C4::Context->preference("BorrowersLog");
1603 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1605 $date = ExtendMemberSubscriptionTo($borrowerid, $date);
1607 Extending the subscription to a given date or to the expiry date calculated on ISO date.
1612 sub ExtendMemberSubscriptionTo {
1613 my ( $borrowerid,$date) = @_;
1614 my $dbh = C4::Context->dbh;
1615 my $borrower = GetMember($borrowerid,'borrowernumber');
1617 $date=POSIX::strftime("%Y-%m-%d",localtime());
1618 my $borrower = GetMember($borrowerid,'borrowernumber');
1619 $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
1621 my $sth = $dbh->do(<<EOF);
1623 SET dateexpiry='$date'
1624 WHERE borrowernumber='$borrowerid'
1626 # add enrolmentfee if needed
1627 $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode=?");
1628 $sth->execute($borrower->{'categorycode'});
1629 my ($enrolmentfee) = $sth->fetchrow;
1630 if ($enrolmentfee) {
1631 # insert fee in patron debts
1632 manualinvoice($borrower->{'borrowernumber'}, '', '', 'A', $enrolmentfee);
1634 return $date if ($sth);
1638 =head2 GetRoadTypes (OUEST-PROVENCE)
1640 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1642 Looks up the different road type . Returns two
1643 elements: a reference-to-array, which lists the id_roadtype
1644 codes, and a reference-to-hash, which maps the road type of the road .
1649 my $dbh = C4::Context->dbh;
1651 SELECT roadtypeid,road_type
1653 ORDER BY road_type|;
1654 my $sth = $dbh->prepare($query);
1659 # insert empty value to create a empty choice in cgi popup
1661 while ( my $data = $sth->fetchrow_hashref ) {
1663 push @id, $data->{'roadtypeid'};
1664 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1667 #test to know if the table contain some records if no the function return nothing
1675 return ( \@id, \%roadtype );
1681 =head2 GetTitles (OUEST-PROVENCE)
1683 ($borrowertitle)= &GetTitles();
1685 Looks up the different title . Returns array with all borrowers title
1690 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1691 unshift( @borrowerTitle, "" );
1692 my $count=@borrowerTitle;
1697 return ( \@borrowerTitle);
1701 =head2 GetPatronImage
1703 my ($imagedata, $dberror) = GetPatronImage($cardnumber);
1705 Returns the mimetype and binary image data of the image for the patron with the supplied cardnumber.
1709 sub GetPatronImage {
1710 my ($cardnumber) = @_;
1711 warn "Cardnumber passed to GetPatronImage is $cardnumber"; # if $debug;
1712 my $dbh = C4::Context->dbh;
1713 my $query = "SELECT mimetype, imagefile FROM patronimage WHERE cardnumber = ?;";
1714 my $sth = $dbh->prepare($query);
1715 $sth->execute($cardnumber);
1716 my $imagedata = $sth->fetchrow_hashref;
1717 my $dberror = $sth->errstr;
1718 warn "Database error!" if $sth->errstr;
1720 return $imagedata, $dberror;
1723 =head2 PutPatronImage
1725 PutPatronImage($cardnumber, $mimetype, $imgfile);
1727 Stores patron binary image data and mimetype in database.
1731 sub PutPatronImage {
1732 my ($cardnumber, $mimetype, $imgfile) = @_;
1733 warn "Parameters passed in: Cardnumber=$cardnumber, Mimetype=$mimetype, " . ($imgfile ? "Imagefile" : "No Imagefile") if $debug;
1734 my $dbh = C4::Context->dbh;
1735 my $query = "INSERT INTO patronimage (cardnumber, mimetype, imagefile) VALUES (?,?,?) ON DUPLICATE KEY UPDATE cardnumber = ?;";
1736 my $sth = $dbh->prepare($query);
1737 $sth->execute($cardnumber,$mimetype,$imgfile,$cardnumber);
1738 warn "Error returned inserting $cardnumber.$mimetype." if $sth->errstr;
1739 my $dberror = $sth->errstr;
1744 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1746 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1748 Returns the description of roadtype
1749 C<&$roadtype>return description of road type
1750 C<&$roadtypeid>this is the value of roadtype s
1754 sub GetRoadTypeDetails {
1755 my ($roadtypeid) = @_;
1756 my $dbh = C4::Context->dbh;
1760 WHERE roadtypeid=?|;
1761 my $sth = $dbh->prepare($query);
1762 $sth->execute($roadtypeid);
1763 my $roadtype = $sth->fetchrow;
1767 =head2 GetBorrowersWhoHaveNotBorrowedSince
1769 &GetBorrowersWhoHaveNotBorrowedSince($date)
1771 this function get all borrowers who haven't borrowed since the date given on input arg.
1775 sub GetBorrowersWhoHaveNotBorrowedSince {
1777 return unless $date; # date is mandatory.
1778 my $dbh = C4::Context->dbh;
1780 SELECT borrowers.borrowernumber,max(timestamp)
1782 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1783 WHERE issues.borrowernumber IS NOT NULL
1784 GROUP BY borrowers.borrowernumber
1786 my $sth = $dbh->prepare($query);
1790 while ( my $data = $sth->fetchrow_hashref ) {
1791 push @results, $data;
1796 =head2 GetBorrowersWhoHaveNeverBorrowed
1798 $results = &GetBorrowersWhoHaveNeverBorrowed
1800 this function get all borrowers who have never borrowed.
1802 I<$result> is a ref to an array which all elements are a hasref.
1806 sub GetBorrowersWhoHaveNeverBorrowed {
1807 my $dbh = C4::Context->dbh;
1809 SELECT borrowers.borrowernumber,max(timestamp)
1811 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1812 WHERE issues.borrowernumber IS NULL
1814 my $sth = $dbh->prepare($query);
1817 while ( my $data = $sth->fetchrow_hashref ) {
1818 push @results, $data;
1823 =head2 GetBorrowersWithIssuesHistoryOlderThan
1825 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1827 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1829 I<$result> is a ref to an array which all elements are a hashref.
1830 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
1834 sub GetBorrowersWithIssuesHistoryOlderThan {
1835 my $dbh = C4::Context->dbh;
1837 return unless $date; # date is mandatory.
1839 SELECT count(borrowernumber) as n,borrowernumber
1841 WHERE returndate < ?
1842 AND borrowernumber IS NOT NULL
1843 GROUP BY borrowernumber
1845 my $sth = $dbh->prepare($query);
1846 $sth->execute($date);
1849 while ( my $data = $sth->fetchrow_hashref ) {
1850 push @results, $data;
1855 END { } # module clean-up code here (global destructor)