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
24 use C4::Dates qw(format_date_in_iso);
25 use Digest::MD5 qw(md5_base64);
26 use Date::Calc qw/Today Add_Delta_YM/;
27 use C4::Log; # logaction
32 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,$debug);
36 $debug = $ENV{DEBUG} || 0;
41 C4::Members - Perl Module containing convenience functions for member handling
49 This module contains routines for adding, modifying and deleting members/patrons/borrowers
67 &GetMemberIssuesAndFines
82 &GetMemberAccountRecords
83 &GetBorNotifyAcctRecord
89 &GetBorrowersWhoHaveNotBorrowedSince
90 &GetBorrowersWhoHaveNeverBorrowed
91 &GetBorrowersWithIssuesHistoryOlderThan
112 &ExtendMemberSubscriptionTo
128 ($count, $borrowers) = &SearchMember($searchstring, $type,$category_type,$filter,$showallbranches);
130 Looks up patrons (borrowers) by name.
132 BUGFIX 499: C<$type> is now used to determine type of search.
133 if $type is "simple", search is performed on the first letter of the
136 $category_type is used to get a specified type of user.
137 (mainly adults when creating a child.)
139 C<$searchstring> is a space-separated list of search terms. Each term
140 must match the beginning a borrower's surname, first name, or other
143 C<$filter> is assumed to be a list of elements to filter results on
145 C<$showallbranches> is used in IndependantBranches Context to display all branches results.
147 C<&SearchMember> returns a two-element list. C<$borrowers> is a
148 reference-to-array; each element is a reference-to-hash, whose keys
149 are the fields of the C<borrowers> table in the Koha database.
150 C<$count> is the number of elements in C<$borrowers>.
155 #used by member enquiries from the intranet
156 #called by member.pl and circ/circulation.pl
158 my ($searchstring, $orderby, $type,$category_type,$filter,$showallbranches ) = @_;
159 my $dbh = C4::Context->dbh;
165 # this is used by circulation everytime a new borrowers cardnumber is scanned
166 # so we can check an exact match first, if that works return, otherwise do the rest
167 $query = "SELECT * FROM borrowers
168 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
170 my $sth = $dbh->prepare("$query WHERE cardnumber = ?");
171 $sth->execute($searchstring);
172 my $data = $sth->fetchall_arrayref({});
174 return ( scalar(@$data), $data );
178 if ( $type eq "simple" ) # simple search for one letter only
180 $query .= ($category_type ? " AND category_type = ".$dbh->quote($category_type) : "");
181 $query .= " WHERE (surname LIKE ? OR cardnumber like ?) ";
182 if (C4::Context->preference("IndependantBranches") && !$showallbranches){
183 if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
184 $query.=" AND borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'}) unless (C4::Context->userenv->{'branch'} eq "insecure");
187 $query.=" ORDER BY $orderby";
188 @bind = ("$searchstring%","$searchstring");
190 else # advanced search looking in surname, firstname and othernames
192 @data = split( ' ', $searchstring );
195 if (C4::Context->preference("IndependantBranches") && !$showallbranches){
196 if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
197 $query.=" borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'})." AND " unless (C4::Context->userenv->{'branch'} eq "insecure");
200 $query.="((surname LIKE ? OR surname LIKE ?
201 OR firstname LIKE ? OR firstname LIKE ?
202 OR othernames LIKE ? OR othernames LIKE ?)
204 ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
206 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
207 "$data[0]%", "% $data[0]%"
209 for ( my $i = 1 ; $i < $count ; $i++ ) {
210 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
211 OR firstname LIKE ? OR firstname LIKE ?
212 OR othernames LIKE ? OR othernames LIKE ?)";
214 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
215 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
219 $query = $query . ") OR cardnumber LIKE ?
221 push( @bind, $searchstring );
226 $sth = $dbh->prepare($query);
228 $debug and print STDERR "Q $orderby : $query\n";
229 $sth->execute(@bind);
231 $data = $sth->fetchall_arrayref({});
234 return ( scalar(@$data), $data );
237 =head2 GetMemberDetails
239 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
241 Looks up a patron and returns information about him or her. If
242 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
243 up the borrower by number; otherwise, it looks up the borrower by card
246 C<$borrower> is a reference-to-hash whose keys are the fields of the
247 borrowers table in the Koha database. In addition,
248 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
249 about the patron. Its keys act as flags :
251 if $borrower->{flags}->{LOST} {
252 # Patron's card was reported lost
255 Each flag has a C<message> key, giving a human-readable explanation of
256 the flag. If the state of a flag means that the patron should not be
257 allowed to borrow any more books, then it will have a C<noissues> key
260 The possible flags are:
266 =item Shows the patron's credit or debt, if any.
274 =item (Gone, no address.) Set if the patron has left without giving a
283 =item Set if the patron's card has been reported as lost.
291 =item Set if the patron has been debarred.
299 =item Any additional notes about the patron.
307 =item Set if the patron has overdue items. This flag has several keys:
309 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
310 overdue items. Its elements are references-to-hash, each describing an
311 overdue item. The keys are selected fields from the issues, biblio,
312 biblioitems, and items tables of the Koha database.
314 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
315 the overdue items, one per line.
323 =item Set if any items that the patron has reserved are available.
325 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
326 available items. Each element is a reference-to-hash whose keys are
327 fields from the reserves table of the Koha database.
333 sub GetMemberDetails {
334 my ( $borrowernumber, $cardnumber ) = @_;
335 my $dbh = C4::Context->dbh;
338 if ($borrowernumber) {
339 $sth = $dbh->prepare("select borrowers.*,category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?");
340 $sth->execute($borrowernumber);
342 elsif ($cardnumber) {
343 $sth = $dbh->prepare("select borrowers.*,category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?");
344 $sth->execute($cardnumber);
349 my $borrower = $sth->fetchrow_hashref;
350 my ($amount) = GetMemberAccountRecords( $borrowernumber);
351 $borrower->{'amountoutstanding'} = $amount;
352 # FIXME - patronflags calls GetMemberAccountRecords... just have patronflags return $amount
353 my $flags = patronflags( $borrower);
356 $sth = $dbh->prepare("select bit,flag from userflags");
358 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
359 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
360 $accessflagshash->{$flag} = 1;
364 $borrower->{'flags'} = $flags;
365 $borrower->{'authflags'} = $accessflagshash;
367 # find out how long the membership lasts
370 "select enrolmentperiod from categories where categorycode = ?");
371 $sth->execute( $borrower->{'categorycode'} );
372 my $enrolment = $sth->fetchrow;
373 $borrower->{'enrolmentperiod'} = $enrolment;
374 return ($borrower); #, $flags, $accessflagshash);
381 NOTE!: If you change this function, be sure to update the POD for
384 $flags = &patronflags($patron);
387 {message} Message showing patron's credit or debt
388 {noissues} Set if patron owes >$5.00
389 {GNA} Set if patron gone w/o address
390 {message} "Borrower has no valid address"
392 {LOST} Set if patron's card reported lost
393 {message} Message to this effect
395 {DBARRED} Set is patron is debarred
396 {message} Message to this effect
398 {NOTES} Set if patron has notes
399 {message} Notes about patron
400 {ODUES} Set if patron has overdue books
402 {itemlist} ref-to-array: list of overdue books
403 {itemlisttext} Text list of overdue items
404 {WAITING} Set if there are items available that the
406 {message} Message to this effect
407 {itemlist} ref-to-array: list of available items
410 # FIXME rename this function.
413 my ( $patroninformation) = @_;
414 my $dbh=C4::Context->dbh;
415 my ($amount) = GetMemberAccountRecords( $patroninformation->{'borrowernumber'});
418 my $noissuescharge = C4::Context->preference("noissuescharge");
419 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
420 $flaginfo{'amount'} = sprintf "%.02f",$amount;
421 if ( $amount > $noissuescharge ) {
422 $flaginfo{'noissues'} = 1;
424 $flags{'CHARGES'} = \%flaginfo;
426 elsif ( $amount < 0 ) {
428 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
429 $flags{'CREDITS'} = \%flaginfo;
431 if ( $patroninformation->{'gonenoaddress'}
432 && $patroninformation->{'gonenoaddress'} == 1 )
435 $flaginfo{'message'} = 'Borrower has no valid address.';
436 $flaginfo{'noissues'} = 1;
437 $flags{'GNA'} = \%flaginfo;
439 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
441 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
442 $flaginfo{'noissues'} = 1;
443 $flags{'LOST'} = \%flaginfo;
445 if ( $patroninformation->{'debarred'}
446 && $patroninformation->{'debarred'} == 1 )
449 $flaginfo{'message'} = 'Borrower is Debarred.';
450 $flaginfo{'noissues'} = 1;
451 $flags{'DBARRED'} = \%flaginfo;
453 if ( $patroninformation->{'borrowernotes'}
454 && $patroninformation->{'borrowernotes'} )
457 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
458 $flags{'NOTES'} = \%flaginfo;
460 my ( $odues, $itemsoverdue ) =
461 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
464 $flaginfo{'message'} = "Yes";
465 $flaginfo{'itemlist'} = $itemsoverdue;
466 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
469 $flaginfo{'itemlisttext'} .=
470 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
472 $flags{'ODUES'} = \%flaginfo;
474 my @itemswaiting = C4::Reserves::GetReservesFromBorrowernumber( $patroninformation->{'borrowernumber'},'W' );
475 my $nowaiting = scalar @itemswaiting;
476 if ( $nowaiting > 0 ) {
478 $flaginfo{'message'} = "Reserved items available";
479 $flaginfo{'itemlist'} = \@itemswaiting;
480 $flags{'WAITING'} = \%flaginfo;
488 $borrower = &GetMember($information, $type);
490 Looks up information about a patron (borrower) by either card number
491 ,firstname, or borrower number, depending on $type value.
492 If C<$type> == 'cardnumber', C<&GetBorrower>
493 searches by cardnumber then by firstname if not found in cardnumber;
494 otherwise, it searches by borrowernumber.
496 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
497 the C<borrowers> table in the Koha database.
503 my ( $information, $type ) = @_;
504 my $dbh = C4::Context->dbh;
507 SELECT borrowers.*, categories.category_type, categories.description
509 LEFT JOIN categories on borrowers.categorycode=categories.categorycode
511 if ($type eq 'cardnumber' || $type eq 'firstname'|| $type eq 'userid'|| $type eq 'borrowernumber'){
512 $information = uc $information;
513 $sth = $dbh->prepare("$select WHERE $type=?");
515 $sth = $dbh->prepare("$select WHERE borrowernumber=?");
517 $sth->execute($information);
518 my $data = $sth->fetchrow_hashref;
520 ($data) and return ($data);
522 if ($type eq 'cardnumber' || $type eq 'firstname') { # otherwise, try with firstname
523 $sth = $dbh->prepare("$select WHERE firstname like ?");
524 $sth->execute($information);
525 $data = $sth->fetchrow_hashref;
532 =item GetMemberIssuesAndFines
534 ($borrowed, $due, $fine) = &GetMemberIssuesAndFines($borrowernumber);
536 Returns aggregate data about items borrowed by the patron with the
537 given borrowernumber.
539 C<&GetMemberIssuesAndFines> returns a three-element array. C<$borrowed> is the
540 number of books the patron currently has borrowed. C<$due> is the
541 number of overdue items the patron currently has borrowed. C<$fine> is
542 the total fine currently due by the borrower.
547 sub GetMemberIssuesAndFines {
548 my ( $borrowernumber ) = @_;
549 my $dbh = C4::Context->dbh;
551 "Select count(*) from issues where borrowernumber='$borrowernumber' and
554 $debug and print $query, "\n";
555 my $sth = $dbh->prepare($query);
557 my $data = $sth->fetchrow_hashref;
559 $sth = $dbh->prepare(
560 "Select count(*) from issues where
561 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
564 my $data2 = $sth->fetchrow_hashref;
566 $sth = $dbh->prepare(
567 "Select sum(amountoutstanding) from accountlines where
568 borrowernumber='$borrowernumber'"
571 my $data3 = $sth->fetchrow_hashref;
574 return ( $data2->{'count(*)'}, $data->{'count(*)'},
575 $data3->{'sum(amountoutstanding)'} );
582 &ModMember($borrowernumber);
584 Modify borrower's data. All date fields should ALREADY be in ISO format.
591 my $dbh = C4::Context->dbh;
592 my $iso_re = C4::Dates->new()->regexp('iso');
593 foreach (qw(dateofbirth dateexpiry dateenrolled)) {
594 if (my $tempdate = $data{$_}) { # assignment, not comparison
595 ($tempdate =~ /$iso_re/) and next; # Congatulations, you sent a valid ISO date.
596 warn "ModMember given $_ not in ISO format ($tempdate)";
597 if (my $tempdate2 = format_date_in_iso($tempdate)) { # assignment, not comparison
598 $data{$_} = $tempdate2;
600 warn "ModMember cannot convert '$tempdate' (from syspref)";
604 if (!$data{'dateofbirth'}){
605 undef $data{'dateofbirth'};
607 my $qborrower=$dbh->prepare("SHOW columns from borrowers");
609 my %hashborrowerfields;
610 while (my ($field)=$qborrower->fetchrow){
611 $hashborrowerfields{$field}=1;
613 my $query = "UPDATE borrowers SET \n";
617 # test to know if you must update or not the borrower password
618 if ( $data{'password'} eq '****' ) {
619 delete $data{'password'};
621 $data{'password'} = md5_base64( $data{'password'} ) if ($data{'password'} ne "");
622 delete $data{'password'} if ($data{password} eq "");
625 { push @parameters,"$_ = ".$dbh->quote($data{$_}) if ($_ ne 'borrowernumber' and $_ ne 'flags' and $hashborrowerfields{$_}); }
626 $query .= join (',',@parameters) . "\n WHERE borrowernumber=? \n";
627 $debug and print STDERR "$query (executed w/ arg: $data{'borrowernumber'})";
628 $sth = $dbh->prepare($query);
629 $sth->execute($data{'borrowernumber'});
632 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
633 # so when we update information for an adult we should check for guarantees and update the relevant part
634 # of their records, ie addresses and phone numbers
635 my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
636 if ( $borrowercategory->{'category_type'} eq ('A' || 'S') ) {
637 # is adult check guarantees;
638 UpdateGuarantees(%data);
640 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
641 if C4::Context->preference("BorrowersLog");
649 $borrowernumber = &AddMember(%borrower);
651 insert new borrower into table
652 Returns the borrowernumber
659 my $dbh = C4::Context->dbh;
660 $data{'userid'} = '' unless $data{'password'};
661 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
663 # WE SHOULD NEVER PASS THIS SUBROUTINE ANYTHING OTHER THAN ISO DATES
664 # IF YOU UNCOMMENT THESE LINES YOU BETTER HAVE A DARN COMPELLING REASON
665 # $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
666 # $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'});
667 # $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
668 # This query should be rewritten to use "?" at execute.
669 if (!$data{'dateofbirth'}){
670 undef ($data{'dateofbirth'});
673 "insert into borrowers set cardnumber=" . $dbh->quote( $data{'cardnumber'} )
674 . ",surname=" . $dbh->quote( $data{'surname'} )
675 . ",firstname=" . $dbh->quote( $data{'firstname'} )
676 . ",title=" . $dbh->quote( $data{'title'} )
677 . ",othernames=" . $dbh->quote( $data{'othernames'} )
678 . ",initials=" . $dbh->quote( $data{'initials'} )
679 . ",streetnumber=". $dbh->quote( $data{'streetnumber'} )
680 . ",streettype=" . $dbh->quote( $data{'streettype'} )
681 . ",address=" . $dbh->quote( $data{'address'} )
682 . ",address2=" . $dbh->quote( $data{'address2'} )
683 . ",zipcode=" . $dbh->quote( $data{'zipcode'} )
684 . ",city=" . $dbh->quote( $data{'city'} )
685 . ",phone=" . $dbh->quote( $data{'phone'} )
686 . ",email=" . $dbh->quote( $data{'email'} )
687 . ",mobile=" . $dbh->quote( $data{'mobile'} )
688 . ",phonepro=" . $dbh->quote( $data{'phonepro'} )
689 . ",opacnote=" . $dbh->quote( $data{'opacnote'} )
690 . ",guarantorid=" . $dbh->quote( $data{'guarantorid'} )
691 . ",dateofbirth=" . $dbh->quote( $data{'dateofbirth'} )
692 . ",branchcode=" . $dbh->quote( $data{'branchcode'} )
693 . ",categorycode=" . $dbh->quote( $data{'categorycode'} )
694 . ",dateenrolled=" . $dbh->quote( $data{'dateenrolled'} )
695 . ",contactname=" . $dbh->quote( $data{'contactname'} )
696 . ",borrowernotes=" . $dbh->quote( $data{'borrowernotes'} )
697 . ",dateexpiry=" . $dbh->quote( $data{'dateexpiry'} )
698 . ",contactnote=" . $dbh->quote( $data{'contactnote'} )
699 . ",B_address=" . $dbh->quote( $data{'B_address'} )
700 . ",B_zipcode=" . $dbh->quote( $data{'B_zipcode'} )
701 . ",B_city=" . $dbh->quote( $data{'B_city'} )
702 . ",B_phone=" . $dbh->quote( $data{'B_phone'} )
703 . ",B_email=" . $dbh->quote( $data{'B_email'} )
704 . ",password=" . $dbh->quote( $data{'password'} )
705 . ",userid=" . $dbh->quote( $data{'userid'} )
706 . ",sort1=" . $dbh->quote( $data{'sort1'} )
707 . ",sort2=" . $dbh->quote( $data{'sort2'} )
708 . ",contacttitle=" . $dbh->quote( $data{'contacttitle'} )
709 . ",emailpro=" . $dbh->quote( $data{'emailpro'} )
710 . ",contactfirstname=" . $dbh->quote( $data{'contactfirstname'} )
711 . ",sex=" . $dbh->quote( $data{'sex'} )
712 . ",fax=" . $dbh->quote( $data{'fax'} )
713 . ",relationship=" . $dbh->quote( $data{'relationship'} )
714 . ",B_streetnumber=" . $dbh->quote( $data{'B_streetnumber'} )
715 . ",B_streettype=" . $dbh->quote( $data{'B_streettype'} )
716 . ",gonenoaddress=" . $dbh->quote( $data{'gonenoaddress'} )
717 . ",lost=" . $dbh->quote( $data{'lost'} )
718 . ",debarred=" . $dbh->quote( $data{'debarred'} )
719 . ",ethnicity=" . $dbh->quote( $data{'ethnicity'} )
720 . ",ethnotes=" . $dbh->quote( $data{'ethnotes'} )
721 . ",altcontactsurname=" . $dbh->quote( $data{'altcontactsurname'} )
722 . ",altcontactfirstname=" . $dbh->quote( $data{'altcontactfirstname'} )
723 . ",altcontactaddress1=" . $dbh->quote( $data{'altcontactaddress1'} )
724 . ",altcontactaddress2=" . $dbh->quote( $data{'altcontactaddress2'} )
725 . ",altcontactaddress3=" . $dbh->quote( $data{'altcontactaddress3'} )
726 . ",altcontactzipcode=" . $dbh->quote( $data{'altcontactzipcode'} )
727 . ",altcontactphone=" . $dbh->quote( $data{'altcontactphone'} ) ;
728 $debug and print STDERR "AddMember SQL: ($query)\n";
729 my $sth = $dbh->prepare($query);
730 # print "Executing SQL: $query\n";
733 $data{'borrowernumber'} = $dbh->{'mysql_insertid'}; # unneeded w/ autoincrement ?
734 # mysql_insertid is probably bad. not necessarily accurate and mysql-specific at best.
736 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
737 if C4::Context->preference("BorrowersLog");
739 # check for enrollment fee & add it if needed
740 $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode=?");
741 $sth->execute($data{'categorycode'});
742 my ($enrolmentfee) = $sth->fetchrow;
744 # insert fee in patron debts
745 manualinvoice($data{'borrowernumber'}, '', '', 'A', $enrolmentfee);
747 return $data{'borrowernumber'};
751 my ($uid,$member) = @_;
752 my $dbh = C4::Context->dbh;
753 # Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
754 # Then we need to tell the user and have them create a new one.
757 "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
758 $sth->execute( $uid, $member );
759 if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
769 my ( $uid, $member, $digest ) = @_;
770 my $dbh = C4::Context->dbh;
772 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
773 #Then we need to tell the user and have them create a new one.
776 "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
777 $sth->execute( $uid, $member );
778 if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
782 #Everything is good so we can update the information.
785 "update borrowers set userid=?, password=? where borrowernumber=?");
786 $sth->execute( $uid, $digest, $member );
790 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
791 if C4::Context->preference("BorrowersLog");
796 =item fixup_cardnumber
798 Warning: The caller is responsible for locking the members table in write
799 mode, to avoid database corruption.
803 use vars qw( @weightings );
804 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
806 sub fixup_cardnumber ($) {
807 my ($cardnumber) = @_;
808 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
809 $autonumber_members = 0 unless defined $autonumber_members;
811 # Find out whether member numbers should be generated
812 # automatically. Should be either "1" or something else.
813 # Defaults to "0", which is interpreted as "no".
815 # if ($cardnumber !~ /\S/ && $autonumber_members) {
816 if ($autonumber_members) {
817 my $dbh = C4::Context->dbh;
818 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
820 # if checkdigit is selected, calculate katipo-style cardnumber.
821 # otherwise, just use the max()
822 # purpose: generate checksum'd member numbers.
823 # We'll assume we just got the max value of digits 2-8 of member #'s
824 # from the database and our job is to increment that by one,
825 # determine the 1st and 9th digits and return the full string.
828 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
832 my $data = $sth->fetchrow_hashref;
833 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
835 if ( !$cardnumber ) { # If DB has no values,
836 $cardnumber = 1000000; # start at 1000000
843 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
845 # read weightings, left to right, 1 char at a time
846 my $temp1 = $weightings[$i];
848 # sequence left to right, 1 char at a time
849 my $temp2 = substr( $cardnumber, $i, 1 );
851 # mult each char 1-7 by its corresponding weighting
852 $sum += $temp1 * $temp2;
855 my $rem = ( $sum % 11 );
856 $rem = 'X' if $rem == 10;
858 $cardnumber = "V$cardnumber$rem";
862 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
863 # better. I'll leave the original in in case it needs to be changed for you
866 "select max(cast(cardnumber as signed)) from borrowers");
868 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
872 my ($result) = $sth->fetchrow;
874 $cardnumber = $result + 1;
882 ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
883 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
884 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
886 C<&GetGuarantees> takes a borrower number (e.g., that of a patron
887 with children) and looks up the borrowers who are guaranteed by that
888 borrower (i.e., the patron's children).
890 C<&GetGuarantees> returns two values: an integer giving the number of
891 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
892 of references to hash, which gives the actual results.
898 my ($borrowernumber) = @_;
899 my $dbh = C4::Context->dbh;
902 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
904 $sth->execute($borrowernumber);
907 my $data = $sth->fetchall_arrayref({});
909 return ( scalar(@$data), $data );
912 =head2 UpdateGuarantees
914 &UpdateGuarantees($parent_borrno);
917 C<&UpdateGuarantees> borrower data for an adulte and updates all the guarantees
918 with the modified information
923 sub UpdateGuarantees {
925 my $dbh = C4::Context->dbh;
926 my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
927 for ( my $i = 0 ; $i < $count ; $i++ ) {
930 # It looks like the $i is only being returned to handle walking through
931 # the array, which is probably better done as a foreach loop.
933 my $guaquery = qq|UPDATE borrowers
934 SET address='$data{'address'}',fax='$data{'fax'}',
935 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
936 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
938 my $sth3 = $dbh->prepare($guaquery);
943 =head2 GetPendingIssues
945 ($count, $issues) = &GetPendingIssues($borrowernumber);
947 Looks up what the patron with the given borrowernumber has borrowed.
949 C<&GetPendingIssues> returns a two-element array. C<$issues> is a
950 reference-to-array, where each element is a reference-to-hash; the
951 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
952 in the Koha database. C<$count> is the number of elements in
958 sub GetPendingIssues {
959 my ($borrowernumber) = @_;
960 my $dbh = C4::Context->dbh;
962 my $sth = $dbh->prepare(
963 "SELECT * FROM issues
964 LEFT JOIN items ON issues.itemnumber=items.itemnumber
965 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
966 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
969 AND returndate IS NULL
970 ORDER BY issues.issuedate"
972 $sth->execute($borrowernumber);
973 my $data = $sth->fetchall_arrayref({});
974 my $today = POSIX::strftime("%Y%m%d", localtime);
976 my $datedue = $_->{'date_due'};
978 if ( $datedue < $today ) {
983 return ( scalar(@$data), $data );
988 ($count, $issues) = &GetAllIssues($borrowernumber, $sortkey, $limit);
990 Looks up what the patron with the given borrowernumber has borrowed,
991 and sorts the results.
993 C<$sortkey> is the name of a field on which to sort the results. This
994 should be the name of a field in the C<issues>, C<biblio>,
995 C<biblioitems>, or C<items> table in the Koha database.
997 C<$limit> is the maximum number of results to return.
999 C<&GetAllIssues> returns a two-element array. C<$issues> is a
1000 reference-to-array, where each element is a reference-to-hash; the
1001 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1002 C<items> tables of the Koha database. C<$count> is the number of
1003 elements in C<$issues>
1009 my ( $borrowernumber, $order, $limit ) = @_;
1011 #FIXME: sanity-check order and limit
1012 my $dbh = C4::Context->dbh;
1015 "Select *,items.timestamp AS itemstimestamp from
1017 LEFT JOIN items on items.itemnumber=issues.itemnumber
1018 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1019 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1020 where borrowernumber=?
1022 if ( $limit != 0 ) {
1023 $query .= " limit $limit";
1027 my $sth = $dbh->prepare($query);
1028 $sth->execute($borrowernumber);
1031 while ( my $data = $sth->fetchrow_hashref ) {
1032 $result[$i] = $data;
1037 # get all issued items for borrowernumber from oldissues table
1038 # large chunk of older issues data put into table oldissues
1039 # to speed up db calls for issuing items
1040 if ( C4::Context->preference("ReadingHistory") ) {
1041 my $query2 = "SELECT * FROM oldissues
1042 LEFT JOIN items ON items.itemnumber=oldissues.itemnumber
1043 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1044 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1045 WHERE borrowernumber=?
1047 if ( $limit != 0 ) {
1048 $limit = $limit - $count;
1049 $query2 .= " limit $limit";
1052 my $sth2 = $dbh->prepare($query2);
1053 $sth2->execute($borrowernumber);
1055 while ( my $data2 = $sth2->fetchrow_hashref ) {
1056 $result[$i] = $data2;
1063 return ( $i, \@result );
1067 =head2 GetMemberAccountRecords
1069 ($total, $acctlines, $count) = &GetMemberAccountRecords($borrowernumber);
1071 Looks up accounting data for the patron with the given borrowernumber.
1073 C<&GetMemberAccountRecords> returns a three-element array. C<$acctlines> is a
1074 reference-to-array, where each element is a reference-to-hash; the
1075 keys are the fields of the C<accountlines> table in the Koha database.
1076 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1077 total amount outstanding for all of the account lines.
1082 sub GetMemberAccountRecords {
1083 my ($borrowernumber,$date) = @_;
1084 my $dbh = C4::Context->dbh;
1090 WHERE borrowernumber=?);
1091 my @bind = ($borrowernumber);
1092 if ($date && $date ne ''){
1093 $strsth.=" AND date < ? ";
1096 $strsth.=" ORDER BY date desc,timestamp DESC";
1097 my $sth= $dbh->prepare( $strsth );
1098 $sth->execute( @bind );
1100 while ( my $data = $sth->fetchrow_hashref ) {
1101 $acctlines[$numlines] = $data;
1103 $total += $data->{'amountoutstanding'};
1106 return ( $total, \@acctlines,$numlines);
1109 =head2 GetBorNotifyAcctRecord
1111 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1113 Looks up accounting data for the patron with the given borrowernumber per file number.
1115 (FIXME - I'm not at all sure what this is about.)
1117 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1118 reference-to-array, where each element is a reference-to-hash; the
1119 keys are the fields of the C<accountlines> table in the Koha database.
1120 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1121 total amount outstanding for all of the account lines.
1125 sub GetBorNotifyAcctRecord {
1126 my ( $borrowernumber, $notifyid ) = @_;
1127 my $dbh = C4::Context->dbh;
1130 my $sth = $dbh->prepare(
1133 WHERE borrowernumber=?
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')
1136 AND amountoutstanding != '0'
1137 ORDER BY notify_id,accounttype
1139 $sth->execute( $borrowernumber, $notifyid );
1141 while ( my $data = $sth->fetchrow_hashref ) {
1142 $acctlines[$numlines] = $data;
1144 $total += $data->{'amountoutstanding'};
1147 return ( $total, \@acctlines, $numlines );
1150 =head2 checkuniquemember (OUEST-PROVENCE)
1152 ($result,$categorycode) = &checkuniquemember($collectivity,$surname,$firstname,$dateofbirth);
1154 Checks that a member exists or not in the database.
1156 C<&result> is nonzero (=exist) or 0 (=does not exist)
1157 C<&categorycode> is from categorycode table
1158 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1159 C<&surname> is the surname
1160 C<&firstname> is the firstname (only if collectivity=0)
1161 C<&dateofbirth> is the date of birth in ISO format (only if collectivity=0)
1165 # FIXME: This function is not legitimate. Multiple patrons might have the same first/last name and birthdate.
1166 # This is especially true since first name is not even a required field.
1168 sub checkuniquemember {
1169 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1170 my $dbh = C4::Context->dbh;
1171 my $request = ($collectivity) ?
1172 "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? " :
1173 "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? and firstname=? and dateofbirth=? ";
1174 my $sth = $dbh->prepare($request);
1175 if ($collectivity) {
1176 $sth->execute( uc($surname) );
1178 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1180 my @data = $sth->fetchrow;
1182 ( $data[0] ) and return $data[0], $data[1];
1186 sub checkcardnumber {
1187 my ($cardnumber,$borrowernumber) = @_;
1188 my $dbh = C4::Context->dbh;
1189 my $query = "SELECT * FROM borrowers WHERE cardnumber=?";
1190 $query .= " AND borrowernumber <> ?" if ($borrowernumber);
1191 my $sth = $dbh->prepare($query);
1192 if ($borrowernumber) {
1193 $sth->execute($cardnumber,$borrowernumber);
1195 $sth->execute($cardnumber);
1197 if (my $data= $sth->fetchrow_hashref()){
1207 =head2 getzipnamecity (OUEST-PROVENCE)
1209 take all info from table city for the fields city and zip
1210 check for the name and the zip code of the city selected
1214 sub getzipnamecity {
1216 my $dbh = C4::Context->dbh;
1219 "select city_name,city_zipcode from cities where cityid=? ");
1220 $sth->execute($cityid);
1221 my @data = $sth->fetchrow;
1222 return $data[0], $data[1];
1226 =head2 getdcity (OUEST-PROVENCE)
1228 recover cityid with city_name condition
1233 my ($city_name) = @_;
1234 my $dbh = C4::Context->dbh;
1235 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1236 $sth->execute($city_name);
1237 my $data = $sth->fetchrow;
1242 =head2 GetExpiryDate
1244 $expirydate = GetExpiryDate($categorycode, $dateenrolled);
1246 Calculate expiry date given a categorycode and starting date. Date argument must be in ISO format.
1247 Return date is also in ISO format.
1252 my ( $categorycode, $dateenrolled ) = @_;
1253 my $enrolmentperiod = 12; # reasonable default
1254 if ($categorycode) {
1255 my $dbh = C4::Context->dbh;
1256 my $sth = $dbh->prepare("select enrolmentperiod from categories where categorycode=?");
1257 $sth->execute($categorycode);
1258 $enrolmentperiod = $sth->fetchrow;
1260 # die "GetExpiryDate: for enrollmentperiod $enrolmentperiod (category '$categorycode') starting $dateenrolled.\n";
1261 my @date = split /-/,$dateenrolled;
1262 return sprintf("%04d-%02d-%02d", Add_Delta_YM(@date,0,$enrolmentperiod));
1265 =head2 checkuserpassword (OUEST-PROVENCE)
1267 check for the password and login are not used
1268 return the number of record
1269 0=> NOT USED 1=> USED
1273 sub checkuserpassword {
1274 my ( $borrowernumber, $userid, $password ) = @_;
1275 $password = md5_base64($password);
1276 my $dbh = C4::Context->dbh;
1279 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1281 $sth->execute( $borrowernumber, $userid, $password );
1282 my $number_rows = $sth->fetchrow;
1283 return $number_rows;
1287 =head2 GetborCatFromCatType
1289 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1291 Looks up the different types of borrowers in the database. Returns two
1292 elements: a reference-to-array, which lists the borrower category
1293 codes, and a reference-to-hash, which maps the borrower category codes
1294 to category descriptions.
1299 sub GetborCatFromCatType {
1300 my ( $category_type, $action ) = @_;
1301 my $dbh = C4::Context->dbh;
1302 my $request = qq| SELECT categorycode,description
1305 ORDER BY categorycode|;
1306 my $sth = $dbh->prepare($request);
1308 $sth->execute($category_type);
1317 while ( my $data = $sth->fetchrow_hashref ) {
1318 push @codes, $data->{'categorycode'};
1319 $labels{ $data->{'categorycode'} } = $data->{'description'};
1322 return ( \@codes, \%labels );
1325 =head2 GetBorrowercategory
1327 $hashref = &GetBorrowercategory($categorycode);
1329 Given the borrower's category code, the function returns the corresponding
1330 data hashref for a comprehensive information display.
1334 sub GetBorrowercategory {
1336 my $dbh = C4::Context->dbh;
1339 "SELECT description,dateofbirthrequired,upperagelimit,category_type
1341 WHERE categorycode = ?"
1343 $sth->execute($catcode);
1345 $sth->fetchrow_hashref;
1348 } # sub getborrowercategory
1350 =head2 ethnicitycategories
1352 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1354 Looks up the different ethnic types in the database. Returns two
1355 elements: a reference-to-array, which lists the ethnicity codes, and a
1356 reference-to-hash, which maps the ethnicity codes to ethnicity
1363 sub ethnicitycategories {
1364 my $dbh = C4::Context->dbh;
1365 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1369 while ( my $data = $sth->fetchrow_hashref ) {
1370 push @codes, $data->{'code'};
1371 $labels{ $data->{'code'} } = $data->{'name'};
1374 return ( \@codes, \%labels );
1379 $ethn_name = &fixEthnicity($ethn_code);
1381 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1382 corresponding descriptive name from the C<ethnicity> table in the
1383 Koha database ("European" or "Pacific Islander").
1390 my $ethnicity = shift;
1391 return unless $ethnicity;
1392 my $dbh = C4::Context->dbh;
1393 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1394 $sth->execute($ethnicity);
1395 my $data = $sth->fetchrow_hashref;
1397 return $data->{'name'};
1398 } # sub fixEthnicity
1402 $dateofbirth,$date = &GetAge($date);
1404 this function return the borrowers age with the value of dateofbirth
1410 my ( $date, $date_ref ) = @_;
1412 if ( not defined $date_ref ) {
1413 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1416 my ( $year1, $month1, $day1 ) = split /-/, $date;
1417 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1419 my $age = $year2 - $year1;
1420 if ( $month1 . $day1 > $month2 . $day2 ) {
1427 =head2 get_institutions
1428 $insitutions = get_institutions();
1430 Just returns a list of all the borrowers of type I, borrownumber and name
1435 sub get_institutions {
1436 my $dbh = C4::Context->dbh();
1439 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1443 while ( my $data = $sth->fetchrow_hashref() ) {
1444 $orgs{ $data->{'borrowernumber'} } = $data;
1449 } # sub get_institutions
1451 =head2 add_member_orgs
1453 add_member_orgs($borrowernumber,$borrowernumbers);
1455 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1460 sub add_member_orgs {
1461 my ( $borrowernumber, $otherborrowers ) = @_;
1462 my $dbh = C4::Context->dbh();
1464 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1465 my $sth = $dbh->prepare($query);
1466 foreach my $otherborrowernumber (@$otherborrowers) {
1467 $sth->execute( $borrowernumber, $otherborrowernumber );
1471 } # sub add_member_orgs
1473 =head2 GetCities (OUEST-PROVENCE)
1475 ($id_cityarrayref, $city_hashref) = &GetCities();
1477 Looks up the different city and zip in the database. Returns two
1478 elements: a reference-to-array, which lists the zip city
1479 codes, and a reference-to-hash, which maps the name of the city.
1480 WHERE =>OUEST PROVENCE OR EXTERIEUR
1486 #my ($type_city) = @_;
1487 my $dbh = C4::Context->dbh;
1488 my $query = qq|SELECT cityid,city_zipcode,city_name
1490 ORDER BY city_name|;
1491 my $sth = $dbh->prepare($query);
1493 #$sth->execute($type_city);
1497 # insert empty value to create a empty choice in cgi popup
1500 while ( my $data = $sth->fetchrow_hashref ) {
1501 push @id, $data->{'city_zipcode'}."|".$data->{'city_name'};
1502 $city{ $data->{'city_zipcode'}."|".$data->{'city_name'} } = $data->{'city_name'};
1505 #test to know if the table contain some records if no the function return nothing
1509 # all we have is the one blank row
1514 return ( \@id, \%city );
1518 =head2 GetSortDetails (OUEST-PROVENCE)
1520 ($lib) = &GetSortDetails($category,$sortvalue);
1522 Returns the authorized value details
1523 C<&$lib>return value of authorized value details
1524 C<&$sortvalue>this is the value of authorized value
1525 C<&$category>this is the value of authorized value category
1529 sub GetSortDetails {
1530 my ( $category, $sortvalue ) = @_;
1531 my $dbh = C4::Context->dbh;
1532 my $query = qq|SELECT lib
1533 FROM authorised_values
1535 AND authorised_value=? |;
1536 my $sth = $dbh->prepare($query);
1537 $sth->execute( $category, $sortvalue );
1538 my $lib = $sth->fetchrow;
1542 =head2 DeleteBorrower
1544 () = &DeleteBorrower($member);
1546 delete all data fo borrowers and add record to deletedborrowers table
1547 C<&$member>this is the borrowernumber
1551 sub MoveMemberToDeleted {
1553 my $dbh = C4::Context->dbh;
1555 $query = qq|SELECT *
1557 WHERE borrowernumber=?|;
1558 my $sth = $dbh->prepare($query);
1559 $sth->execute($member);
1560 my @data = $sth->fetchrow_array;
1563 $dbh->prepare( "INSERT INTO deletedborrowers VALUES ("
1564 . ( "?," x ( scalar(@data) - 1 ) )
1566 $sth->execute(@data);
1572 DelMember($borrowernumber);
1574 This function remove directly a borrower whitout writing it on deleteborrower.
1575 + Deletes reserves for the borrower
1580 my $dbh = C4::Context->dbh;
1581 my $borrowernumber = shift;
1582 warn "in delmember with $borrowernumber";
1583 return unless $borrowernumber; # borrowernumber is mandatory.
1585 my $query = qq|DELETE
1587 WHERE borrowernumber=?|;
1588 my $sth = $dbh->prepare($query);
1589 $sth->execute($borrowernumber);
1594 WHERE borrowernumber = ?
1596 $sth = $dbh->prepare($query);
1597 $sth->execute($borrowernumber);
1599 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$borrowernumber,"")
1600 if C4::Context->preference("BorrowersLog");
1604 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1606 $date = ExtendMemberSubscriptionTo($borrowerid, $date);
1608 Extending the subscription to a given date or to the expiry date calculated on ISO date.
1613 sub ExtendMemberSubscriptionTo {
1614 my ( $borrowerid,$date) = @_;
1615 my $dbh = C4::Context->dbh;
1616 my $borrower = GetMember($borrowerid,'borrowernumber');
1618 $date=POSIX::strftime("%Y-%m-%d",localtime());
1619 my $borrower = GetMember($borrowerid,'borrowernumber');
1620 $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
1622 my $sth = $dbh->do(<<EOF);
1624 SET dateexpiry='$date'
1625 WHERE borrowernumber='$borrowerid'
1627 # add enrolmentfee if needed
1628 $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode=?");
1629 $sth->execute($borrower->{'categorycode'});
1630 my ($enrolmentfee) = $sth->fetchrow;
1631 if ($enrolmentfee) {
1632 # insert fee in patron debts
1633 manualinvoice($borrower->{'borrowernumber'}, '', '', 'A', $enrolmentfee);
1635 return $date if ($sth);
1639 =head2 GetRoadTypes (OUEST-PROVENCE)
1641 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1643 Looks up the different road type . Returns two
1644 elements: a reference-to-array, which lists the id_roadtype
1645 codes, and a reference-to-hash, which maps the road type of the road .
1650 my $dbh = C4::Context->dbh;
1652 SELECT roadtypeid,road_type
1654 ORDER BY road_type|;
1655 my $sth = $dbh->prepare($query);
1660 # insert empty value to create a empty choice in cgi popup
1662 while ( my $data = $sth->fetchrow_hashref ) {
1664 push @id, $data->{'roadtypeid'};
1665 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1668 #test to know if the table contain some records if no the function return nothing
1676 return ( \@id, \%roadtype );
1682 =head2 GetTitles (OUEST-PROVENCE)
1684 ($borrowertitle)= &GetTitles();
1686 Looks up the different title . Returns array with all borrowers title
1691 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1692 unshift( @borrowerTitle, "" );
1693 my $count=@borrowerTitle;
1698 return ( \@borrowerTitle);
1704 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1706 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1708 Returns the description of roadtype
1709 C<&$roadtype>return description of road type
1710 C<&$roadtypeid>this is the value of roadtype s
1714 sub GetRoadTypeDetails {
1715 my ($roadtypeid) = @_;
1716 my $dbh = C4::Context->dbh;
1720 WHERE roadtypeid=?|;
1721 my $sth = $dbh->prepare($query);
1722 $sth->execute($roadtypeid);
1723 my $roadtype = $sth->fetchrow;
1727 =head2 GetBorrowersWhoHaveNotBorrowedSince
1729 &GetBorrowersWhoHaveNotBorrowedSince($date)
1731 this function get all borrowers who haven't borrowed since the date given on input arg.
1735 sub GetBorrowersWhoHaveNotBorrowedSince {
1737 return unless $date; # date is mandatory.
1738 my $dbh = C4::Context->dbh;
1740 SELECT borrowers.borrowernumber,max(timestamp)
1742 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1743 WHERE issues.borrowernumber IS NOT NULL
1744 GROUP BY borrowers.borrowernumber
1746 my $sth = $dbh->prepare($query);
1750 while ( my $data = $sth->fetchrow_hashref ) {
1751 push @results, $data;
1756 =head2 GetBorrowersWhoHaveNeverBorrowed
1758 $results = &GetBorrowersWhoHaveNeverBorrowed
1760 this function get all borrowers who have never borrowed.
1762 I<$result> is a ref to an array which all elements are a hasref.
1766 sub GetBorrowersWhoHaveNeverBorrowed {
1767 my $dbh = C4::Context->dbh;
1769 SELECT borrowers.borrowernumber,max(timestamp)
1771 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1772 WHERE issues.borrowernumber IS NULL
1774 my $sth = $dbh->prepare($query);
1777 while ( my $data = $sth->fetchrow_hashref ) {
1778 push @results, $data;
1783 =head2 GetBorrowersWithIssuesHistoryOlderThan
1785 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1787 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1789 I<$result> is a ref to an array which all elements are a hashref.
1790 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
1794 sub GetBorrowersWithIssuesHistoryOlderThan {
1795 my $dbh = C4::Context->dbh;
1797 return unless $date; # date is mandatory.
1799 SELECT count(borrowernumber) as n,borrowernumber
1801 WHERE returndate < ?
1802 AND borrowernumber IS NOT NULL
1803 GROUP BY borrowernumber
1805 my $sth = $dbh->prepare($query);
1806 $sth->execute($date);
1809 while ( my $data = $sth->fetchrow_hashref ) {
1810 push @results, $data;
1815 END { } # module clean-up code here (global destructor)