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
61 &GetMemberAccountRecords
62 &GetBorNotifyAcctRecord
67 &GetBorrowersWhoHaveNotBorrowedSince
68 &GetBorrowersWhoHaveNeverBorrowed
69 &GetBorrowersWithIssuesHistoryOlderThan
90 &ExtendMemberSubscriptionTo
107 C4::Members - Perl Module containing convenience functions for member handling
115 This module contains routines for adding, modifying and deleting members/patrons/borrowers
123 ($count, $borrowers) = &SearchMember($searchstring, $type,$category_type,$filter,$showallbranches);
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<$filter> is assumed to be a list of elements to filter results on
140 C<$showallbranches> is used in IndependantBranches Context to display all branches results.
142 C<&SearchMember> returns a two-element list. C<$borrowers> is a
143 reference-to-array; each element is a reference-to-hash, whose keys
144 are the fields of the C<borrowers> table in the Koha database.
145 C<$count> is the number of elements in C<$borrowers>.
150 #used by member enquiries from the intranet
151 #called by member.pl and circ/circulation.pl
153 my ($searchstring, $orderby, $type,$category_type,$filter,$showallbranches ) = @_;
154 my $dbh = C4::Context->dbh;
160 # this is used by circulation everytime a new borrowers cardnumber is scanned
161 # so we can check an exact match first, if that works return, otherwise do the rest
162 $query = "SELECT * FROM borrowers
163 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
165 my $sth = $dbh->prepare("$query WHERE cardnumber = ?");
166 $sth->execute($searchstring);
167 my $data = $sth->fetchall_arrayref({});
169 return ( scalar(@$data), $data );
173 if ( $type eq "simple" ) # simple search for one letter only
175 $query .= ($category_type ? " AND category_type = ".$dbh->quote($category_type) : "");
176 $query .= " WHERE (surname LIKE ? OR cardnumber like ?) ";
177 if (C4::Context->preference("IndependantBranches") && !$showallbranches){
178 if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
179 $query.=" AND borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'}) unless (C4::Context->userenv->{'branch'} eq "insecure");
182 $query.=" ORDER BY $orderby";
183 @bind = ("$searchstring%","$searchstring");
185 else # advanced search looking in surname, firstname and othernames
187 @data = split( ' ', $searchstring );
190 if (C4::Context->preference("IndependantBranches") && !$showallbranches){
191 if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
192 $query.=" borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'})." AND " unless (C4::Context->userenv->{'branch'} eq "insecure");
195 $query.="((surname LIKE ? OR surname LIKE ?
196 OR firstname LIKE ? OR firstname LIKE ?
197 OR othernames LIKE ? OR othernames LIKE ?)
199 ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
201 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
202 "$data[0]%", "% $data[0]%"
204 for ( my $i = 1 ; $i < $count ; $i++ ) {
205 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
206 OR firstname LIKE ? OR firstname LIKE ?
207 OR othernames LIKE ? OR othernames LIKE ?)";
209 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
210 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
214 $query = $query . ") OR cardnumber LIKE ?
216 push( @bind, $searchstring );
221 $sth = $dbh->prepare($query);
223 $debug and print STDERR "Q $orderby : $query\n";
224 $sth->execute(@bind);
226 $data = $sth->fetchall_arrayref({});
229 return ( scalar(@$data), $data );
232 =head2 GetMemberDetails
234 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
236 Looks up a patron and returns information about him or her. If
237 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
238 up the borrower by number; otherwise, it looks up the borrower by card
241 C<$borrower> is a reference-to-hash whose keys are the fields of the
242 borrowers table in the Koha database. In addition,
243 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
244 about the patron. Its keys act as flags :
246 if $borrower->{flags}->{LOST} {
247 # Patron's card was reported lost
250 Each flag has a C<message> key, giving a human-readable explanation of
251 the flag. If the state of a flag means that the patron should not be
252 allowed to borrow any more books, then it will have a C<noissues> key
255 The possible flags are:
261 =item Shows the patron's credit or debt, if any.
269 =item (Gone, no address.) Set if the patron has left without giving a
278 =item Set if the patron's card has been reported as lost.
286 =item Set if the patron has been debarred.
294 =item Any additional notes about the patron.
302 =item Set if the patron has overdue items. This flag has several keys:
304 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
305 overdue items. Its elements are references-to-hash, each describing an
306 overdue item. The keys are selected fields from the issues, biblio,
307 biblioitems, and items tables of the Koha database.
309 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
310 the overdue items, one per line.
318 =item Set if any items that the patron has reserved are available.
320 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
321 available items. Each element is a reference-to-hash whose keys are
322 fields from the reserves table of the Koha database.
328 sub GetMemberDetails {
329 my ( $borrowernumber, $cardnumber ) = @_;
330 my $dbh = C4::Context->dbh;
333 if ($borrowernumber) {
334 $sth = $dbh->prepare("select borrowers.*,category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?");
335 $sth->execute($borrowernumber);
337 elsif ($cardnumber) {
338 $sth = $dbh->prepare("select borrowers.*,category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?");
339 $sth->execute($cardnumber);
344 my $borrower = $sth->fetchrow_hashref;
345 my ($amount) = GetMemberAccountRecords( $borrowernumber);
346 $borrower->{'amountoutstanding'} = $amount;
347 # FIXME - patronflags calls GetMemberAccountRecords... just have patronflags return $amount
348 my $flags = patronflags( $borrower);
351 $sth = $dbh->prepare("select bit,flag from userflags");
353 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
354 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
355 $accessflagshash->{$flag} = 1;
359 $borrower->{'flags'} = $flags;
360 $borrower->{'authflags'} = $accessflagshash;
362 # find out how long the membership lasts
365 "select enrolmentperiod from categories where categorycode = ?");
366 $sth->execute( $borrower->{'categorycode'} );
367 my $enrolment = $sth->fetchrow;
368 $borrower->{'enrolmentperiod'} = $enrolment;
369 return ($borrower); #, $flags, $accessflagshash);
376 NOTE!: If you change this function, be sure to update the POD for
379 $flags = &patronflags($patron);
382 {message} Message showing patron's credit or debt
383 {noissues} Set if patron owes >$5.00
384 {GNA} Set if patron gone w/o address
385 {message} "Borrower has no valid address"
387 {LOST} Set if patron's card reported lost
388 {message} Message to this effect
390 {DBARRED} Set is patron is debarred
391 {message} Message to this effect
393 {NOTES} Set if patron has notes
394 {message} Notes about patron
395 {ODUES} Set if patron has overdue books
397 {itemlist} ref-to-array: list of overdue books
398 {itemlisttext} Text list of overdue items
399 {WAITING} Set if there are items available that the
401 {message} Message to this effect
402 {itemlist} ref-to-array: list of available items
405 # FIXME rename this function.
408 my ( $patroninformation) = @_;
409 my $dbh=C4::Context->dbh;
410 my ($amount) = GetMemberAccountRecords( $patroninformation->{'borrowernumber'});
413 my $noissuescharge = C4::Context->preference("noissuescharge");
414 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
415 $flaginfo{'amount'} = sprintf "%.02f",$amount;
416 if ( $amount > $noissuescharge ) {
417 $flaginfo{'noissues'} = 1;
419 $flags{'CHARGES'} = \%flaginfo;
421 elsif ( $amount < 0 ) {
423 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
424 $flags{'CREDITS'} = \%flaginfo;
426 if ( $patroninformation->{'gonenoaddress'}
427 && $patroninformation->{'gonenoaddress'} == 1 )
430 $flaginfo{'message'} = 'Borrower has no valid address.';
431 $flaginfo{'noissues'} = 1;
432 $flags{'GNA'} = \%flaginfo;
434 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
436 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
437 $flaginfo{'noissues'} = 1;
438 $flags{'LOST'} = \%flaginfo;
440 if ( $patroninformation->{'debarred'}
441 && $patroninformation->{'debarred'} == 1 )
444 $flaginfo{'message'} = 'Borrower is Debarred.';
445 $flaginfo{'noissues'} = 1;
446 $flags{'DBARRED'} = \%flaginfo;
448 if ( $patroninformation->{'borrowernotes'}
449 && $patroninformation->{'borrowernotes'} )
452 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
453 $flags{'NOTES'} = \%flaginfo;
455 my ( $odues, $itemsoverdue ) =
456 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
459 $flaginfo{'message'} = "Yes";
460 $flaginfo{'itemlist'} = $itemsoverdue;
461 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
464 $flaginfo{'itemlisttext'} .=
465 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
467 $flags{'ODUES'} = \%flaginfo;
469 my @itemswaiting = C4::Reserves::GetReservesFromBorrowernumber( $patroninformation->{'borrowernumber'},'W' );
470 my $nowaiting = scalar @itemswaiting;
471 if ( $nowaiting > 0 ) {
473 $flaginfo{'message'} = "Reserved items available";
474 $flaginfo{'itemlist'} = \@itemswaiting;
475 $flags{'WAITING'} = \%flaginfo;
483 $borrower = &GetMember($information, $type);
485 Looks up information about a patron (borrower) by either card number
486 ,firstname, or borrower number, depending on $type value.
487 If C<$type> == 'cardnumber', C<&GetBorrower>
488 searches by cardnumber then by firstname if not found in cardnumber;
489 otherwise, it searches by borrowernumber.
491 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
492 the C<borrowers> table in the Koha database.
498 my ( $information, $type ) = @_;
499 my $dbh = C4::Context->dbh;
502 SELECT borrowers.*, categories.category_type, categories.description
504 LEFT JOIN categories on borrowers.categorycode=categories.categorycode
506 if ($type eq 'cardnumber' || $type eq 'firstname'|| $type eq 'userid'|| $type eq 'borrowernumber'){
507 $information = uc $information;
508 $sth = $dbh->prepare("$select WHERE $type=?");
510 $sth = $dbh->prepare("$select WHERE borrowernumber=?");
512 $sth->execute($information);
513 my $data = $sth->fetchrow_hashref;
515 ($data) and return ($data);
517 if ($type eq 'cardnumber' || $type eq 'firstname') { # otherwise, try with firstname
518 $sth = $dbh->prepare("$select WHERE firstname like ?");
519 $sth->execute($information);
520 $data = $sth->fetchrow_hashref;
527 =item GetMemberIssuesAndFines
529 ($borrowed, $due, $fine) = &GetMemberIssuesAndFines($borrowernumber);
531 Returns aggregate data about items borrowed by the patron with the
532 given borrowernumber.
534 C<&GetMemberIssuesAndFines> returns a three-element array. C<$borrowed> is the
535 number of books the patron currently has borrowed. C<$due> is the
536 number of overdue items the patron currently has borrowed. C<$fine> is
537 the total fine currently due by the borrower.
542 sub GetMemberIssuesAndFines {
543 my ( $borrowernumber ) = @_;
544 my $dbh = C4::Context->dbh;
546 "Select count(*) from issues where borrowernumber='$borrowernumber' and
549 $debug and print $query, "\n";
550 my $sth = $dbh->prepare($query);
552 my $data = $sth->fetchrow_hashref;
554 $sth = $dbh->prepare(
555 "Select count(*) from issues where
556 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
559 my $data2 = $sth->fetchrow_hashref;
561 $sth = $dbh->prepare(
562 "Select sum(amountoutstanding) from accountlines where
563 borrowernumber='$borrowernumber'"
566 my $data3 = $sth->fetchrow_hashref;
569 return ( $data2->{'count(*)'}, $data->{'count(*)'},
570 $data3->{'sum(amountoutstanding)'} );
577 &ModMember($borrowernumber);
579 Modify borrower's data. All date fields should ALREADY be in ISO format.
586 my $dbh = C4::Context->dbh;
587 my $iso_re = C4::Dates->new()->regexp('iso');
588 foreach (qw(dateofbirth dateexpiry dateenrolled)) {
589 if (my $tempdate = $data{$_}) { # assignment, not comparison
590 ($tempdate =~ /$iso_re/) and next; # Congatulations, you sent a valid ISO date.
591 warn "ModMember given $_ not in ISO format ($tempdate)";
592 if (my $tempdate2 = format_date_in_iso($tempdate)) { # assignment, not comparison
593 $data{$_} = $tempdate2;
595 warn "ModMember cannot convert '$tempdate' (from syspref)";
599 if (!$data{'dateofbirth'}){
600 undef $data{'dateofbirth'};
602 my $qborrower=$dbh->prepare("SHOW columns from borrowers");
604 my %hashborrowerfields;
605 while (my ($field)=$qborrower->fetchrow){
606 $hashborrowerfields{$field}=1;
608 my $query = "UPDATE borrowers SET \n";
612 # test to know if you must update or not the borrower password
613 if ( $data{'password'} eq '****' ) {
614 delete $data{'password'};
616 $data{'password'} = md5_base64( $data{'password'} ) if ($data{'password'} ne "");
617 delete $data{'password'} if ($data{password} eq "");
620 { push @parameters,"$_ = ".$dbh->quote($data{$_}) if ($_ ne 'borrowernumber' and $_ ne 'flags' and $hashborrowerfields{$_}); }
621 $query .= join (',',@parameters) . "\n WHERE borrowernumber=? \n";
622 $debug and print STDERR "$query (executed w/ arg: $data{'borrowernumber'})";
623 $sth = $dbh->prepare($query);
624 $sth->execute($data{'borrowernumber'});
627 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
628 # so when we update information for an adult we should check for guarantees and update the relevant part
629 # of their records, ie addresses and phone numbers
630 my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
631 if ( $borrowercategory->{'category_type'} eq ('A' || 'S') ) {
632 # is adult check guarantees;
633 UpdateGuarantees(%data);
635 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
636 if C4::Context->preference("BorrowersLog");
644 $borrowernumber = &AddMember(%borrower);
646 insert new borrower into table
647 Returns the borrowernumber
654 my $dbh = C4::Context->dbh;
655 $data{'userid'} = '' unless $data{'password'};
656 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
658 # WE SHOULD NEVER PASS THIS SUBROUTINE ANYTHING OTHER THAN ISO DATES
659 # IF YOU UNCOMMENT THESE LINES YOU BETTER HAVE A DARN COMPELLING REASON
660 # $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
661 # $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'});
662 # $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
663 # This query should be rewritten to use "?" at execute.
664 if (!$data{'dateofbirth'}){
665 undef ($data{'dateofbirth'});
668 "insert into borrowers set cardnumber=" . $dbh->quote( $data{'cardnumber'} )
669 . ",surname=" . $dbh->quote( $data{'surname'} )
670 . ",firstname=" . $dbh->quote( $data{'firstname'} )
671 . ",title=" . $dbh->quote( $data{'title'} )
672 . ",othernames=" . $dbh->quote( $data{'othernames'} )
673 . ",initials=" . $dbh->quote( $data{'initials'} )
674 . ",streetnumber=". $dbh->quote( $data{'streetnumber'} )
675 . ",streettype=" . $dbh->quote( $data{'streettype'} )
676 . ",address=" . $dbh->quote( $data{'address'} )
677 . ",address2=" . $dbh->quote( $data{'address2'} )
678 . ",zipcode=" . $dbh->quote( $data{'zipcode'} )
679 . ",city=" . $dbh->quote( $data{'city'} )
680 . ",phone=" . $dbh->quote( $data{'phone'} )
681 . ",email=" . $dbh->quote( $data{'email'} )
682 . ",mobile=" . $dbh->quote( $data{'mobile'} )
683 . ",phonepro=" . $dbh->quote( $data{'phonepro'} )
684 . ",opacnote=" . $dbh->quote( $data{'opacnote'} )
685 . ",guarantorid=" . $dbh->quote( $data{'guarantorid'} )
686 . ",dateofbirth=" . $dbh->quote( $data{'dateofbirth'} )
687 . ",branchcode=" . $dbh->quote( $data{'branchcode'} )
688 . ",categorycode=" . $dbh->quote( $data{'categorycode'} )
689 . ",dateenrolled=" . $dbh->quote( $data{'dateenrolled'} )
690 . ",contactname=" . $dbh->quote( $data{'contactname'} )
691 . ",borrowernotes=" . $dbh->quote( $data{'borrowernotes'} )
692 . ",dateexpiry=" . $dbh->quote( $data{'dateexpiry'} )
693 . ",contactnote=" . $dbh->quote( $data{'contactnote'} )
694 . ",B_address=" . $dbh->quote( $data{'B_address'} )
695 . ",B_zipcode=" . $dbh->quote( $data{'B_zipcode'} )
696 . ",B_city=" . $dbh->quote( $data{'B_city'} )
697 . ",B_phone=" . $dbh->quote( $data{'B_phone'} )
698 . ",B_email=" . $dbh->quote( $data{'B_email'} )
699 . ",password=" . $dbh->quote( $data{'password'} )
700 . ",userid=" . $dbh->quote( $data{'userid'} )
701 . ",sort1=" . $dbh->quote( $data{'sort1'} )
702 . ",sort2=" . $dbh->quote( $data{'sort2'} )
703 . ",contacttitle=" . $dbh->quote( $data{'contacttitle'} )
704 . ",emailpro=" . $dbh->quote( $data{'emailpro'} )
705 . ",contactfirstname=" . $dbh->quote( $data{'contactfirstname'} )
706 . ",sex=" . $dbh->quote( $data{'sex'} )
707 . ",fax=" . $dbh->quote( $data{'fax'} )
708 . ",relationship=" . $dbh->quote( $data{'relationship'} )
709 . ",B_streetnumber=" . $dbh->quote( $data{'B_streetnumber'} )
710 . ",B_streettype=" . $dbh->quote( $data{'B_streettype'} )
711 . ",gonenoaddress=" . $dbh->quote( $data{'gonenoaddress'} )
712 . ",lost=" . $dbh->quote( $data{'lost'} )
713 . ",debarred=" . $dbh->quote( $data{'debarred'} )
714 . ",ethnicity=" . $dbh->quote( $data{'ethnicity'} )
715 . ",ethnotes=" . $dbh->quote( $data{'ethnotes'} )
716 . ",altcontactsurname=" . $dbh->quote( $data{'altcontactsurname'} )
717 . ",altcontactfirstname=" . $dbh->quote( $data{'altcontactfirstname'} )
718 . ",altcontactaddress1=" . $dbh->quote( $data{'altcontactaddress1'} )
719 . ",altcontactaddress2=" . $dbh->quote( $data{'altcontactaddress2'} )
720 . ",altcontactaddress3=" . $dbh->quote( $data{'altcontactaddress3'} )
721 . ",altcontactzipcode=" . $dbh->quote( $data{'altcontactzipcode'} )
722 . ",altcontactphone=" . $dbh->quote( $data{'altcontactphone'} ) ;
723 $debug and print STDERR "AddMember SQL: ($query)\n";
724 my $sth = $dbh->prepare($query);
725 # print "Executing SQL: $query\n";
728 $data{'borrowernumber'} = $dbh->{'mysql_insertid'}; # unneeded w/ autoincrement ?
729 # mysql_insertid is probably bad. not necessarily accurate and mysql-specific at best.
731 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
732 if C4::Context->preference("BorrowersLog");
734 # check for enrollment fee & add it if needed
735 $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode=?");
736 $sth->execute($data{'categorycode'});
737 my ($enrolmentfee) = $sth->fetchrow;
739 # insert fee in patron debts
740 manualinvoice($data{'borrowernumber'}, '', '', 'A', $enrolmentfee);
742 return $data{'borrowernumber'};
746 my ($uid,$member) = @_;
747 my $dbh = C4::Context->dbh;
748 # Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
749 # Then we need to tell the user and have them create a new one.
752 "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
753 $sth->execute( $uid, $member );
754 if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
764 my ( $uid, $member, $digest ) = @_;
765 my $dbh = C4::Context->dbh;
767 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
768 #Then we need to tell the user and have them create a new one.
771 "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
772 $sth->execute( $uid, $member );
773 if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
777 #Everything is good so we can update the information.
780 "update borrowers set userid=?, password=? where borrowernumber=?");
781 $sth->execute( $uid, $digest, $member );
785 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
786 if C4::Context->preference("BorrowersLog");
791 =item fixup_cardnumber
793 Warning: The caller is responsible for locking the members table in write
794 mode, to avoid database corruption.
798 use vars qw( @weightings );
799 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
801 sub fixup_cardnumber ($) {
802 my ($cardnumber) = @_;
803 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
804 $autonumber_members = 0 unless defined $autonumber_members;
806 # Find out whether member numbers should be generated
807 # automatically. Should be either "1" or something else.
808 # Defaults to "0", which is interpreted as "no".
810 # if ($cardnumber !~ /\S/ && $autonumber_members) {
811 if ($autonumber_members) {
812 my $dbh = C4::Context->dbh;
813 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
815 # if checkdigit is selected, calculate katipo-style cardnumber.
816 # otherwise, just use the max()
817 # purpose: generate checksum'd member numbers.
818 # We'll assume we just got the max value of digits 2-8 of member #'s
819 # from the database and our job is to increment that by one,
820 # determine the 1st and 9th digits and return the full string.
823 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
827 my $data = $sth->fetchrow_hashref;
828 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
830 if ( !$cardnumber ) { # If DB has no values,
831 $cardnumber = 1000000; # start at 1000000
838 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
840 # read weightings, left to right, 1 char at a time
841 my $temp1 = $weightings[$i];
843 # sequence left to right, 1 char at a time
844 my $temp2 = substr( $cardnumber, $i, 1 );
846 # mult each char 1-7 by its corresponding weighting
847 $sum += $temp1 * $temp2;
850 my $rem = ( $sum % 11 );
851 $rem = 'X' if $rem == 10;
853 $cardnumber = "V$cardnumber$rem";
857 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
858 # better. I'll leave the original in in case it needs to be changed for you
861 "select max(cast(cardnumber as signed)) from borrowers");
863 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
867 my ($result) = $sth->fetchrow;
869 $cardnumber = $result + 1;
877 ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
878 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
879 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
881 C<&GetGuarantees> takes a borrower number (e.g., that of a patron
882 with children) and looks up the borrowers who are guaranteed by that
883 borrower (i.e., the patron's children).
885 C<&GetGuarantees> returns two values: an integer giving the number of
886 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
887 of references to hash, which gives the actual results.
893 my ($borrowernumber) = @_;
894 my $dbh = C4::Context->dbh;
897 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
899 $sth->execute($borrowernumber);
902 my $data = $sth->fetchall_arrayref({});
904 return ( scalar(@$data), $data );
907 =head2 UpdateGuarantees
909 &UpdateGuarantees($parent_borrno);
912 C<&UpdateGuarantees> borrower data for an adulte and updates all the guarantees
913 with the modified information
918 sub UpdateGuarantees {
920 my $dbh = C4::Context->dbh;
921 my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
922 for ( my $i = 0 ; $i < $count ; $i++ ) {
925 # It looks like the $i is only being returned to handle walking through
926 # the array, which is probably better done as a foreach loop.
928 my $guaquery = qq|UPDATE borrowers
929 SET address='$data{'address'}',fax='$data{'fax'}',
930 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
931 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
933 my $sth3 = $dbh->prepare($guaquery);
938 =head2 GetPendingIssues
940 ($count, $issues) = &GetPendingIssues($borrowernumber);
942 Looks up what the patron with the given borrowernumber has borrowed.
944 C<&GetPendingIssues> returns a two-element array. C<$issues> is a
945 reference-to-array, where each element is a reference-to-hash; the
946 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
947 in the Koha database. C<$count> is the number of elements in
953 sub GetPendingIssues {
954 my ($borrowernumber) = @_;
955 my $dbh = C4::Context->dbh;
957 my $sth = $dbh->prepare(
958 "SELECT * FROM issues
959 LEFT JOIN items ON issues.itemnumber=items.itemnumber
960 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
961 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
964 AND returndate IS NULL
965 ORDER BY issues.issuedate"
967 $sth->execute($borrowernumber);
968 my $data = $sth->fetchall_arrayref({});
969 my $today = POSIX::strftime("%Y%m%d", localtime);
971 my $datedue = $_->{'date_due'};
973 if ( $datedue < $today ) {
978 return ( scalar(@$data), $data );
983 ($count, $issues) = &GetAllIssues($borrowernumber, $sortkey, $limit);
985 Looks up what the patron with the given borrowernumber has borrowed,
986 and sorts the results.
988 C<$sortkey> is the name of a field on which to sort the results. This
989 should be the name of a field in the C<issues>, C<biblio>,
990 C<biblioitems>, or C<items> table in the Koha database.
992 C<$limit> is the maximum number of results to return.
994 C<&GetAllIssues> returns a two-element array. C<$issues> is a
995 reference-to-array, where each element is a reference-to-hash; the
996 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
997 C<items> tables of the Koha database. C<$count> is the number of
998 elements in C<$issues>
1004 my ( $borrowernumber, $order, $limit ) = @_;
1006 #FIXME: sanity-check order and limit
1007 my $dbh = C4::Context->dbh;
1010 "Select *,items.timestamp AS itemstimestamp from
1012 LEFT JOIN items on items.itemnumber=issues.itemnumber
1013 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1014 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1015 where borrowernumber=?
1017 if ( $limit != 0 ) {
1018 $query .= " limit $limit";
1022 my $sth = $dbh->prepare($query);
1023 $sth->execute($borrowernumber);
1026 while ( my $data = $sth->fetchrow_hashref ) {
1027 $result[$i] = $data;
1032 # get all issued items for borrowernumber from oldissues table
1033 # large chunk of older issues data put into table oldissues
1034 # to speed up db calls for issuing items
1035 if ( C4::Context->preference("ReadingHistory") ) {
1036 my $query2 = "SELECT * FROM oldissues
1037 LEFT JOIN items ON items.itemnumber=oldissues.itemnumber
1038 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1039 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1040 WHERE borrowernumber=?
1042 if ( $limit != 0 ) {
1043 $limit = $limit - $count;
1044 $query2 .= " limit $limit";
1047 my $sth2 = $dbh->prepare($query2);
1048 $sth2->execute($borrowernumber);
1050 while ( my $data2 = $sth2->fetchrow_hashref ) {
1051 $result[$i] = $data2;
1058 return ( $i, \@result );
1062 =head2 GetMemberAccountRecords
1064 ($total, $acctlines, $count) = &GetMemberAccountRecords($borrowernumber);
1066 Looks up accounting data for the patron with the given borrowernumber.
1068 C<&GetMemberAccountRecords> returns a three-element array. C<$acctlines> is a
1069 reference-to-array, where each element is a reference-to-hash; the
1070 keys are the fields of the C<accountlines> table in the Koha database.
1071 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1072 total amount outstanding for all of the account lines.
1077 sub GetMemberAccountRecords {
1078 my ($borrowernumber,$date) = @_;
1079 my $dbh = C4::Context->dbh;
1085 WHERE borrowernumber=?);
1086 my @bind = ($borrowernumber);
1087 if ($date && $date ne ''){
1088 $strsth.=" AND date < ? ";
1091 $strsth.=" ORDER BY date desc,timestamp DESC";
1092 my $sth= $dbh->prepare( $strsth );
1093 $sth->execute( @bind );
1095 while ( my $data = $sth->fetchrow_hashref ) {
1096 $acctlines[$numlines] = $data;
1098 $total += $data->{'amountoutstanding'};
1101 return ( $total, \@acctlines,$numlines);
1104 =head2 GetBorNotifyAcctRecord
1106 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1108 Looks up accounting data for the patron with the given borrowernumber per file number.
1110 (FIXME - I'm not at all sure what this is about.)
1112 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1113 reference-to-array, where each element is a reference-to-hash; the
1114 keys are the fields of the C<accountlines> table in the Koha database.
1115 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1116 total amount outstanding for all of the account lines.
1120 sub GetBorNotifyAcctRecord {
1121 my ( $borrowernumber, $notifyid ) = @_;
1122 my $dbh = C4::Context->dbh;
1125 my $sth = $dbh->prepare(
1128 WHERE borrowernumber=?
1130 AND amountoutstanding != '0'
1131 ORDER BY notify_id,accounttype
1133 # 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')
1135 $sth->execute( $borrowernumber, $notifyid );
1137 while ( my $data = $sth->fetchrow_hashref ) {
1138 $acctlines[$numlines] = $data;
1140 $total += $data->{'amountoutstanding'};
1143 return ( $total, \@acctlines, $numlines );
1146 =head2 checkuniquemember (OUEST-PROVENCE)
1148 ($result,$categorycode) = &checkuniquemember($collectivity,$surname,$firstname,$dateofbirth);
1150 Checks that a member exists or not in the database.
1152 C<&result> is nonzero (=exist) or 0 (=does not exist)
1153 C<&categorycode> is from categorycode table
1154 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1155 C<&surname> is the surname
1156 C<&firstname> is the firstname (only if collectivity=0)
1157 C<&dateofbirth> is the date of birth in ISO format (only if collectivity=0)
1161 # FIXME: This function is not legitimate. Multiple patrons might have the same first/last name and birthdate.
1162 # This is especially true since first name is not even a required field.
1164 sub checkuniquemember {
1165 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1166 my $dbh = C4::Context->dbh;
1167 my $request = ($collectivity) ?
1168 "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? " :
1169 "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? and firstname=? and dateofbirth=? ";
1170 my $sth = $dbh->prepare($request);
1171 if ($collectivity) {
1172 $sth->execute( uc($surname) );
1174 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1176 my @data = $sth->fetchrow;
1178 ( $data[0] ) and return $data[0], $data[1];
1182 sub checkcardnumber {
1183 my ($cardnumber,$borrowernumber) = @_;
1184 my $dbh = C4::Context->dbh;
1185 my $query = "SELECT * FROM borrowers WHERE cardnumber=?";
1186 $query .= " AND borrowernumber <> ?" if ($borrowernumber);
1187 my $sth = $dbh->prepare($query);
1188 if ($borrowernumber) {
1189 $sth->execute($cardnumber,$borrowernumber);
1191 $sth->execute($cardnumber);
1193 if (my $data= $sth->fetchrow_hashref()){
1203 =head2 getzipnamecity (OUEST-PROVENCE)
1205 take all info from table city for the fields city and zip
1206 check for the name and the zip code of the city selected
1210 sub getzipnamecity {
1212 my $dbh = C4::Context->dbh;
1215 "select city_name,city_zipcode from cities where cityid=? ");
1216 $sth->execute($cityid);
1217 my @data = $sth->fetchrow;
1218 return $data[0], $data[1];
1222 =head2 getdcity (OUEST-PROVENCE)
1224 recover cityid with city_name condition
1229 my ($city_name) = @_;
1230 my $dbh = C4::Context->dbh;
1231 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1232 $sth->execute($city_name);
1233 my $data = $sth->fetchrow;
1238 =head2 GetExpiryDate
1240 $expirydate = GetExpiryDate($categorycode, $dateenrolled);
1242 Calculate expiry date given a categorycode and starting date. Date argument must be in ISO format.
1243 Return date is also in ISO format.
1248 my ( $categorycode, $dateenrolled ) = @_;
1249 my $enrolmentperiod = 12; # reasonable default
1250 if ($categorycode) {
1251 my $dbh = C4::Context->dbh;
1252 my $sth = $dbh->prepare("select enrolmentperiod from categories where categorycode=?");
1253 $sth->execute($categorycode);
1254 $enrolmentperiod = $sth->fetchrow;
1256 # die "GetExpiryDate: for enrollmentperiod $enrolmentperiod (category '$categorycode') starting $dateenrolled.\n";
1257 my @date = split /-/,$dateenrolled;
1258 return sprintf("%04d-%02d-%02d", Add_Delta_YM(@date,0,$enrolmentperiod));
1261 =head2 checkuserpassword (OUEST-PROVENCE)
1263 check for the password and login are not used
1264 return the number of record
1265 0=> NOT USED 1=> USED
1269 sub checkuserpassword {
1270 my ( $borrowernumber, $userid, $password ) = @_;
1271 $password = md5_base64($password);
1272 my $dbh = C4::Context->dbh;
1275 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1277 $sth->execute( $borrowernumber, $userid, $password );
1278 my $number_rows = $sth->fetchrow;
1279 return $number_rows;
1283 =head2 GetborCatFromCatType
1285 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1287 Looks up the different types of borrowers in the database. Returns two
1288 elements: a reference-to-array, which lists the borrower category
1289 codes, and a reference-to-hash, which maps the borrower category codes
1290 to category descriptions.
1295 sub GetborCatFromCatType {
1296 my ( $category_type, $action ) = @_;
1297 my $dbh = C4::Context->dbh;
1298 my $request = qq| SELECT categorycode,description
1301 ORDER BY categorycode|;
1302 my $sth = $dbh->prepare($request);
1304 $sth->execute($category_type);
1313 while ( my $data = $sth->fetchrow_hashref ) {
1314 push @codes, $data->{'categorycode'};
1315 $labels{ $data->{'categorycode'} } = $data->{'description'};
1318 return ( \@codes, \%labels );
1321 =head2 GetBorrowercategory
1323 $hashref = &GetBorrowercategory($categorycode);
1325 Given the borrower's category code, the function returns the corresponding
1326 data hashref for a comprehensive information display.
1330 sub GetBorrowercategory {
1332 my $dbh = C4::Context->dbh;
1335 "SELECT description,dateofbirthrequired,upperagelimit,category_type
1337 WHERE categorycode = ?"
1339 $sth->execute($catcode);
1341 $sth->fetchrow_hashref;
1344 } # sub getborrowercategory
1346 =head2 ethnicitycategories
1348 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1350 Looks up the different ethnic types in the database. Returns two
1351 elements: a reference-to-array, which lists the ethnicity codes, and a
1352 reference-to-hash, which maps the ethnicity codes to ethnicity
1359 sub ethnicitycategories {
1360 my $dbh = C4::Context->dbh;
1361 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1365 while ( my $data = $sth->fetchrow_hashref ) {
1366 push @codes, $data->{'code'};
1367 $labels{ $data->{'code'} } = $data->{'name'};
1370 return ( \@codes, \%labels );
1375 $ethn_name = &fixEthnicity($ethn_code);
1377 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1378 corresponding descriptive name from the C<ethnicity> table in the
1379 Koha database ("European" or "Pacific Islander").
1386 my $ethnicity = shift;
1387 return unless $ethnicity;
1388 my $dbh = C4::Context->dbh;
1389 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1390 $sth->execute($ethnicity);
1391 my $data = $sth->fetchrow_hashref;
1393 return $data->{'name'};
1394 } # sub fixEthnicity
1398 $dateofbirth,$date = &GetAge($date);
1400 this function return the borrowers age with the value of dateofbirth
1406 my ( $date, $date_ref ) = @_;
1408 if ( not defined $date_ref ) {
1409 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1412 my ( $year1, $month1, $day1 ) = split /-/, $date;
1413 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1415 my $age = $year2 - $year1;
1416 if ( $month1 . $day1 > $month2 . $day2 ) {
1423 =head2 get_institutions
1424 $insitutions = get_institutions();
1426 Just returns a list of all the borrowers of type I, borrownumber and name
1431 sub get_institutions {
1432 my $dbh = C4::Context->dbh();
1435 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1439 while ( my $data = $sth->fetchrow_hashref() ) {
1440 $orgs{ $data->{'borrowernumber'} } = $data;
1445 } # sub get_institutions
1447 =head2 add_member_orgs
1449 add_member_orgs($borrowernumber,$borrowernumbers);
1451 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1456 sub add_member_orgs {
1457 my ( $borrowernumber, $otherborrowers ) = @_;
1458 my $dbh = C4::Context->dbh();
1460 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1461 my $sth = $dbh->prepare($query);
1462 foreach my $otherborrowernumber (@$otherborrowers) {
1463 $sth->execute( $borrowernumber, $otherborrowernumber );
1467 } # sub add_member_orgs
1469 =head2 GetCities (OUEST-PROVENCE)
1471 ($id_cityarrayref, $city_hashref) = &GetCities();
1473 Looks up the different city and zip in the database. Returns two
1474 elements: a reference-to-array, which lists the zip city
1475 codes, and a reference-to-hash, which maps the name of the city.
1476 WHERE =>OUEST PROVENCE OR EXTERIEUR
1482 #my ($type_city) = @_;
1483 my $dbh = C4::Context->dbh;
1484 my $query = qq|SELECT cityid,city_zipcode,city_name
1486 ORDER BY city_name|;
1487 my $sth = $dbh->prepare($query);
1489 #$sth->execute($type_city);
1493 # insert empty value to create a empty choice in cgi popup
1496 while ( my $data = $sth->fetchrow_hashref ) {
1497 push @id, $data->{'city_zipcode'}."|".$data->{'city_name'};
1498 $city{ $data->{'city_zipcode'}."|".$data->{'city_name'} } = $data->{'city_name'};
1501 #test to know if the table contain some records if no the function return nothing
1505 # all we have is the one blank row
1510 return ( \@id, \%city );
1514 =head2 GetSortDetails (OUEST-PROVENCE)
1516 ($lib) = &GetSortDetails($category,$sortvalue);
1518 Returns the authorized value details
1519 C<&$lib>return value of authorized value details
1520 C<&$sortvalue>this is the value of authorized value
1521 C<&$category>this is the value of authorized value category
1525 sub GetSortDetails {
1526 my ( $category, $sortvalue ) = @_;
1527 my $dbh = C4::Context->dbh;
1528 my $query = qq|SELECT lib
1529 FROM authorised_values
1531 AND authorised_value=? |;
1532 my $sth = $dbh->prepare($query);
1533 $sth->execute( $category, $sortvalue );
1534 my $lib = $sth->fetchrow;
1538 =head2 DeleteBorrower
1540 () = &DeleteBorrower($member);
1542 delete all data fo borrowers and add record to deletedborrowers table
1543 C<&$member>this is the borrowernumber
1547 sub MoveMemberToDeleted {
1549 my $dbh = C4::Context->dbh;
1551 $query = qq|SELECT *
1553 WHERE borrowernumber=?|;
1554 my $sth = $dbh->prepare($query);
1555 $sth->execute($member);
1556 my @data = $sth->fetchrow_array;
1559 $dbh->prepare( "INSERT INTO deletedborrowers VALUES ("
1560 . ( "?," x ( scalar(@data) - 1 ) )
1562 $sth->execute(@data);
1568 DelMember($borrowernumber);
1570 This function remove directly a borrower whitout writing it on deleteborrower.
1571 + Deletes reserves for the borrower
1576 my $dbh = C4::Context->dbh;
1577 my $borrowernumber = shift;
1578 #warn "in delmember with $borrowernumber";
1579 return unless $borrowernumber; # borrowernumber is mandatory.
1581 my $query = qq|DELETE
1583 WHERE borrowernumber=?|;
1584 my $sth = $dbh->prepare($query);
1585 $sth->execute($borrowernumber);
1590 WHERE borrowernumber = ?
1592 $sth = $dbh->prepare($query);
1593 $sth->execute($borrowernumber);
1595 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$borrowernumber,"")
1596 if C4::Context->preference("BorrowersLog");
1600 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1602 $date = ExtendMemberSubscriptionTo($borrowerid, $date);
1604 Extending the subscription to a given date or to the expiry date calculated on ISO date.
1609 sub ExtendMemberSubscriptionTo {
1610 my ( $borrowerid,$date) = @_;
1611 my $dbh = C4::Context->dbh;
1612 my $borrower = GetMember($borrowerid,'borrowernumber');
1614 $date=POSIX::strftime("%Y-%m-%d",localtime());
1615 my $borrower = GetMember($borrowerid,'borrowernumber');
1616 $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
1618 my $sth = $dbh->do(<<EOF);
1620 SET dateexpiry='$date'
1621 WHERE borrowernumber='$borrowerid'
1623 # add enrolmentfee if needed
1624 $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode=?");
1625 $sth->execute($borrower->{'categorycode'});
1626 my ($enrolmentfee) = $sth->fetchrow;
1627 if ($enrolmentfee) {
1628 # insert fee in patron debts
1629 manualinvoice($borrower->{'borrowernumber'}, '', '', 'A', $enrolmentfee);
1631 return $date if ($sth);
1635 =head2 GetRoadTypes (OUEST-PROVENCE)
1637 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1639 Looks up the different road type . Returns two
1640 elements: a reference-to-array, which lists the id_roadtype
1641 codes, and a reference-to-hash, which maps the road type of the road .
1646 my $dbh = C4::Context->dbh;
1648 SELECT roadtypeid,road_type
1650 ORDER BY road_type|;
1651 my $sth = $dbh->prepare($query);
1656 # insert empty value to create a empty choice in cgi popup
1658 while ( my $data = $sth->fetchrow_hashref ) {
1660 push @id, $data->{'roadtypeid'};
1661 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1664 #test to know if the table contain some records if no the function return nothing
1672 return ( \@id, \%roadtype );
1678 =head2 GetTitles (OUEST-PROVENCE)
1680 ($borrowertitle)= &GetTitles();
1682 Looks up the different title . Returns array with all borrowers title
1687 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1688 unshift( @borrowerTitle, "" );
1689 my $count=@borrowerTitle;
1694 return ( \@borrowerTitle);
1700 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1702 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1704 Returns the description of roadtype
1705 C<&$roadtype>return description of road type
1706 C<&$roadtypeid>this is the value of roadtype s
1710 sub GetRoadTypeDetails {
1711 my ($roadtypeid) = @_;
1712 my $dbh = C4::Context->dbh;
1716 WHERE roadtypeid=?|;
1717 my $sth = $dbh->prepare($query);
1718 $sth->execute($roadtypeid);
1719 my $roadtype = $sth->fetchrow;
1723 =head2 GetBorrowersWhoHaveNotBorrowedSince
1725 &GetBorrowersWhoHaveNotBorrowedSince($date)
1727 this function get all borrowers who haven't borrowed since the date given on input arg.
1731 sub GetBorrowersWhoHaveNotBorrowedSince {
1733 return unless $date; # date is mandatory.
1734 my $dbh = C4::Context->dbh;
1736 SELECT borrowers.borrowernumber,max(timestamp)
1738 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1739 WHERE issues.borrowernumber IS NOT NULL
1740 GROUP BY borrowers.borrowernumber
1742 my $sth = $dbh->prepare($query);
1746 while ( my $data = $sth->fetchrow_hashref ) {
1747 push @results, $data;
1752 =head2 GetBorrowersWhoHaveNeverBorrowed
1754 $results = &GetBorrowersWhoHaveNeverBorrowed
1756 this function get all borrowers who have never borrowed.
1758 I<$result> is a ref to an array which all elements are a hasref.
1762 sub GetBorrowersWhoHaveNeverBorrowed {
1763 my $dbh = C4::Context->dbh;
1765 SELECT borrowers.borrowernumber,max(timestamp)
1767 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1768 WHERE issues.borrowernumber IS NULL
1770 my $sth = $dbh->prepare($query);
1773 while ( my $data = $sth->fetchrow_hashref ) {
1774 push @results, $data;
1779 =head2 GetBorrowersWithIssuesHistoryOlderThan
1781 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1783 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1785 I<$result> is a ref to an array which all elements are a hashref.
1786 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
1790 sub GetBorrowersWithIssuesHistoryOlderThan {
1791 my $dbh = C4::Context->dbh;
1793 return unless $date; # date is mandatory.
1795 SELECT count(borrowernumber) as n,borrowernumber
1797 WHERE returndate < ?
1798 AND borrowernumber IS NOT NULL
1799 GROUP BY borrowernumber
1801 my $sth = $dbh->prepare($query);
1802 $sth->execute($date);
1805 while ( my $data = $sth->fetchrow_hashref ) {
1806 push @results, $data;
1811 END { } # module clean-up code here (global destructor)