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
65 &GetMemberAccountRecords
66 &GetBorNotifyAcctRecord
71 &GetBorrowersWhoHaveNotBorrowedSince
72 &GetBorrowersWhoHaveNeverBorrowed
73 &GetBorrowersWithIssuesHistoryOlderThan
94 &ExtendMemberSubscriptionTo
111 C4::Members - Perl Module containing convenience functions for member handling
119 This module contains routines for adding, modifying and deleting members/patrons/borrowers
127 ($count, $borrowers) = &SearchMember($searchstring, $type,$category_type,$filter,$showallbranches);
129 Looks up patrons (borrowers) by name.
131 BUGFIX 499: C<$type> is now used to determine type of search.
132 if $type is "simple", search is performed on the first letter of the
135 $category_type is used to get a specified type of user.
136 (mainly adults when creating a child.)
138 C<$searchstring> is a space-separated list of search terms. Each term
139 must match the beginning a borrower's surname, first name, or other
142 C<$filter> is assumed to be a list of elements to filter results on
144 C<$showallbranches> is used in IndependantBranches Context to display all branches results.
146 C<&SearchMember> returns a two-element list. C<$borrowers> is a
147 reference-to-array; each element is a reference-to-hash, whose keys
148 are the fields of the C<borrowers> table in the Koha database.
149 C<$count> is the number of elements in C<$borrowers>.
154 #used by member enquiries from the intranet
155 #called by member.pl and circ/circulation.pl
157 my ($searchstring, $orderby, $type,$category_type,$filter,$showallbranches ) = @_;
158 my $dbh = C4::Context->dbh;
164 # this is used by circulation everytime a new borrowers cardnumber is scanned
165 # so we can check an exact match first, if that works return, otherwise do the rest
166 $query = "SELECT * FROM borrowers
167 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
169 my $sth = $dbh->prepare("$query WHERE cardnumber = ?");
170 $sth->execute($searchstring);
171 my $data = $sth->fetchall_arrayref({});
173 return ( scalar(@$data), $data );
177 if ( $type eq "simple" ) # simple search for one letter only
179 $query .= ($category_type ? " AND category_type = ".$dbh->quote($category_type) : "");
180 $query .= " WHERE (surname LIKE ? OR cardnumber like ?) ";
181 if (C4::Context->preference("IndependantBranches") && !$showallbranches){
182 if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
183 $query.=" AND borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'}) unless (C4::Context->userenv->{'branch'} eq "insecure");
186 $query.=" ORDER BY $orderby";
187 @bind = ("$searchstring%","$searchstring");
189 else # advanced search looking in surname, firstname and othernames
191 @data = split( ' ', $searchstring );
194 if (C4::Context->preference("IndependantBranches") && !$showallbranches){
195 if (C4::Context->userenv && C4::Context->userenv->{flags}!=1 && C4::Context->userenv->{'branch'}){
196 $query.=" borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'})." AND " unless (C4::Context->userenv->{'branch'} eq "insecure");
199 $query.="((surname LIKE ? OR surname LIKE ?
200 OR firstname LIKE ? OR firstname LIKE ?
201 OR othernames LIKE ? OR othernames LIKE ?)
203 ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
205 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
206 "$data[0]%", "% $data[0]%"
208 for ( my $i = 1 ; $i < $count ; $i++ ) {
209 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
210 OR firstname LIKE ? OR firstname LIKE ?
211 OR othernames LIKE ? OR othernames LIKE ?)";
213 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
214 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
218 $query = $query . ") OR cardnumber LIKE ?
220 push( @bind, $searchstring );
225 $sth = $dbh->prepare($query);
227 $debug and print STDERR "Q $orderby : $query\n";
228 $sth->execute(@bind);
230 $data = $sth->fetchall_arrayref({});
233 return ( scalar(@$data), $data );
236 =head2 GetMemberDetails
238 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
240 Looks up a patron and returns information about him or her. If
241 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
242 up the borrower by number; otherwise, it looks up the borrower by card
245 C<$borrower> is a reference-to-hash whose keys are the fields of the
246 borrowers table in the Koha database. In addition,
247 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
248 about the patron. Its keys act as flags :
250 if $borrower->{flags}->{LOST} {
251 # Patron's card was reported lost
254 Each flag has a C<message> key, giving a human-readable explanation of
255 the flag. If the state of a flag means that the patron should not be
256 allowed to borrow any more books, then it will have a C<noissues> key
259 The possible flags are:
265 =item Shows the patron's credit or debt, if any.
273 =item (Gone, no address.) Set if the patron has left without giving a
282 =item Set if the patron's card has been reported as lost.
290 =item Set if the patron has been debarred.
298 =item Any additional notes about the patron.
306 =item Set if the patron has overdue items. This flag has several keys:
308 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
309 overdue items. Its elements are references-to-hash, each describing an
310 overdue item. The keys are selected fields from the issues, biblio,
311 biblioitems, and items tables of the Koha database.
313 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
314 the overdue items, one per line.
322 =item Set if any items that the patron has reserved are available.
324 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
325 available items. Each element is a reference-to-hash whose keys are
326 fields from the reserves table of the Koha database.
332 sub GetMemberDetails {
333 my ( $borrowernumber, $cardnumber ) = @_;
334 my $dbh = C4::Context->dbh;
337 if ($borrowernumber) {
338 $sth = $dbh->prepare("select borrowers.*,category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?");
339 $sth->execute($borrowernumber);
341 elsif ($cardnumber) {
342 $sth = $dbh->prepare("select borrowers.*,category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?");
343 $sth->execute($cardnumber);
348 my $borrower = $sth->fetchrow_hashref;
349 my ($amount) = GetMemberAccountRecords( $borrowernumber);
350 $borrower->{'amountoutstanding'} = $amount;
351 # FIXME - patronflags calls GetMemberAccountRecords... just have patronflags return $amount
352 my $flags = patronflags( $borrower);
355 $sth = $dbh->prepare("select bit,flag from userflags");
357 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
358 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
359 $accessflagshash->{$flag} = 1;
363 $borrower->{'flags'} = $flags;
364 $borrower->{'authflags'} = $accessflagshash;
366 # find out how long the membership lasts
369 "select enrolmentperiod from categories where categorycode = ?");
370 $sth->execute( $borrower->{'categorycode'} );
371 my $enrolment = $sth->fetchrow;
372 $borrower->{'enrolmentperiod'} = $enrolment;
373 return ($borrower); #, $flags, $accessflagshash);
380 NOTE!: If you change this function, be sure to update the POD for
383 $flags = &patronflags($patron);
386 {message} Message showing patron's credit or debt
387 {noissues} Set if patron owes >$5.00
388 {GNA} Set if patron gone w/o address
389 {message} "Borrower has no valid address"
391 {LOST} Set if patron's card reported lost
392 {message} Message to this effect
394 {DBARRED} Set is patron is debarred
395 {message} Message to this effect
397 {NOTES} Set if patron has notes
398 {message} Notes about patron
399 {ODUES} Set if patron has overdue books
401 {itemlist} ref-to-array: list of overdue books
402 {itemlisttext} Text list of overdue items
403 {WAITING} Set if there are items available that the
405 {message} Message to this effect
406 {itemlist} ref-to-array: list of available items
409 # FIXME rename this function.
412 my ( $patroninformation) = @_;
413 my $dbh=C4::Context->dbh;
414 my ($amount) = GetMemberAccountRecords( $patroninformation->{'borrowernumber'});
417 my $noissuescharge = C4::Context->preference("noissuescharge");
418 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
419 $flaginfo{'amount'} = sprintf "%.02f",$amount;
420 if ( $amount > $noissuescharge ) {
421 $flaginfo{'noissues'} = 1;
423 $flags{'CHARGES'} = \%flaginfo;
425 elsif ( $amount < 0 ) {
427 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
428 $flags{'CREDITS'} = \%flaginfo;
430 if ( $patroninformation->{'gonenoaddress'}
431 && $patroninformation->{'gonenoaddress'} == 1 )
434 $flaginfo{'message'} = 'Borrower has no valid address.';
435 $flaginfo{'noissues'} = 1;
436 $flags{'GNA'} = \%flaginfo;
438 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
440 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
441 $flaginfo{'noissues'} = 1;
442 $flags{'LOST'} = \%flaginfo;
444 if ( $patroninformation->{'debarred'}
445 && $patroninformation->{'debarred'} == 1 )
448 $flaginfo{'message'} = 'Borrower is Debarred.';
449 $flaginfo{'noissues'} = 1;
450 $flags{'DBARRED'} = \%flaginfo;
452 if ( $patroninformation->{'borrowernotes'}
453 && $patroninformation->{'borrowernotes'} )
456 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
457 $flags{'NOTES'} = \%flaginfo;
459 my ( $odues, $itemsoverdue ) =
460 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
463 $flaginfo{'message'} = "Yes";
464 $flaginfo{'itemlist'} = $itemsoverdue;
465 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
468 $flaginfo{'itemlisttext'} .=
469 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
471 $flags{'ODUES'} = \%flaginfo;
473 my @itemswaiting = C4::Reserves::GetReservesFromBorrowernumber( $patroninformation->{'borrowernumber'},'W' );
474 my $nowaiting = scalar @itemswaiting;
475 if ( $nowaiting > 0 ) {
477 $flaginfo{'message'} = "Reserved items available";
478 $flaginfo{'itemlist'} = \@itemswaiting;
479 $flags{'WAITING'} = \%flaginfo;
487 $borrower = &GetMember($information, $type);
489 Looks up information about a patron (borrower) by either card number
490 ,firstname, or borrower number, depending on $type value.
491 If C<$type> == 'cardnumber', C<&GetBorrower>
492 searches by cardnumber then by firstname if not found in cardnumber;
493 otherwise, it searches by borrowernumber.
495 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
496 the C<borrowers> table in the Koha database.
502 my ( $information, $type ) = @_;
503 my $dbh = C4::Context->dbh;
506 SELECT borrowers.*, categories.category_type, categories.description
508 LEFT JOIN categories on borrowers.categorycode=categories.categorycode
510 if ($type eq 'cardnumber' || $type eq 'firstname'|| $type eq 'userid'|| $type eq 'borrowernumber'){
511 $information = uc $information;
512 $sth = $dbh->prepare("$select WHERE $type=?");
514 $sth = $dbh->prepare("$select WHERE borrowernumber=?");
516 $sth->execute($information);
517 my $data = $sth->fetchrow_hashref;
519 ($data) and return ($data);
521 if ($type eq 'cardnumber' || $type eq 'firstname') { # otherwise, try with firstname
522 $sth = $dbh->prepare("$select WHERE firstname like ?");
523 $sth->execute($information);
524 $data = $sth->fetchrow_hashref;
526 ($data) and return ($data);
531 =item GetMemberIssuesAndFines
533 ($overdue_count, $issue_count, $total_fines) = &GetMemberIssuesAndFines($borrowernumber);
535 Returns aggregate data about items borrowed by the patron with the
536 given borrowernumber.
538 C<&GetMemberIssuesAndFines> returns a three-element array. C<$overdue_count> is the
539 number of overdue items the patron currently has borrowed. C<$issue_count> is the
540 number of books the patron currently has borrowed. C<$total_fines> is
541 the total fine currently due by the borrower.
546 sub GetMemberIssuesAndFines {
547 my ( $borrowernumber ) = @_;
548 my $dbh = C4::Context->dbh;
549 my $query = "SELECT COUNT(*) FROM issues WHERE borrowernumber = ?";
551 $debug and warn $query."\n";
552 my $sth = $dbh->prepare($query);
553 $sth->execute($borrowernumber);
554 my $issue_count = $sth->fetchrow_arrayref->[0];
557 $sth = $dbh->prepare(
558 "SELECT COUNT(*) FROM issues
559 WHERE borrowernumber = ?
560 AND date_due < now()"
562 $sth->execute($borrowernumber);
563 my $overdue_count = $sth->fetchrow_arrayref->[0];
566 $sth = $dbh->prepare("SELECT SUM(amountoutstanding) FROM accountlines WHERE borrowernumber = ?");
567 $sth->execute($borrowernumber);
568 my $total_fines = $sth->fetchrow_arrayref->[0];
571 return ($overdue_count, $issue_count, $total_fines);
578 &ModMember($borrowernumber);
580 Modify borrower's data. All date fields should ALREADY be in ISO format.
587 my $dbh = C4::Context->dbh;
588 my $iso_re = C4::Dates->new()->regexp('iso');
589 foreach (qw(dateofbirth dateexpiry dateenrolled)) {
590 if (my $tempdate = $data{$_}) { # assignment, not comparison
591 ($tempdate =~ /$iso_re/) and next; # Congatulations, you sent a valid ISO date.
592 warn "ModMember given $_ not in ISO format ($tempdate)";
593 if (my $tempdate2 = format_date_in_iso($tempdate)) { # assignment, not comparison
594 $data{$_} = $tempdate2;
596 warn "ModMember cannot convert '$tempdate' (from syspref)";
600 if (!$data{'dateofbirth'}){
601 undef $data{'dateofbirth'};
603 my $qborrower=$dbh->prepare("SHOW columns from borrowers");
605 my %hashborrowerfields;
606 while (my ($field)=$qborrower->fetchrow){
607 $hashborrowerfields{$field}=1;
609 my $query = "UPDATE borrowers SET \n";
613 # test to know if you must update or not the borrower password
614 if ( $data{'password'} eq '****' ) {
615 delete $data{'password'};
617 $data{'password'} = md5_base64( $data{'password'} ) if ($data{'password'} ne "");
618 delete $data{'password'} if ($data{password} eq "");
621 { push @parameters,"$_ = ".$dbh->quote($data{$_}) if ($_ ne 'borrowernumber' and $_ ne 'flags' and $hashborrowerfields{$_}); }
622 $query .= join (',',@parameters) . "\n WHERE borrowernumber=? \n";
623 $debug and print STDERR "$query (executed w/ arg: $data{'borrowernumber'})";
624 $sth = $dbh->prepare($query);
625 $sth->execute($data{'borrowernumber'});
628 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
629 # so when we update information for an adult we should check for guarantees and update the relevant part
630 # of their records, ie addresses and phone numbers
631 my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
632 if ( $borrowercategory->{'category_type'} eq ('A' || 'S') ) {
633 # is adult check guarantees;
634 UpdateGuarantees(%data);
636 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
637 if C4::Context->preference("BorrowersLog");
645 $borrowernumber = &AddMember(%borrower);
647 insert new borrower into table
648 Returns the borrowernumber
655 my $dbh = C4::Context->dbh;
656 $data{'userid'} = '' unless $data{'password'};
657 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
659 # WE SHOULD NEVER PASS THIS SUBROUTINE ANYTHING OTHER THAN ISO DATES
660 # IF YOU UNCOMMENT THESE LINES YOU BETTER HAVE A DARN COMPELLING REASON
661 # $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
662 # $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'});
663 # $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
664 # This query should be rewritten to use "?" at execute.
665 if (!$data{'dateofbirth'}){
666 undef ($data{'dateofbirth'});
669 "insert into borrowers set cardnumber=" . $dbh->quote( $data{'cardnumber'} )
670 . ",surname=" . $dbh->quote( $data{'surname'} )
671 . ",firstname=" . $dbh->quote( $data{'firstname'} )
672 . ",title=" . $dbh->quote( $data{'title'} )
673 . ",othernames=" . $dbh->quote( $data{'othernames'} )
674 . ",initials=" . $dbh->quote( $data{'initials'} )
675 . ",streetnumber=". $dbh->quote( $data{'streetnumber'} )
676 . ",streettype=" . $dbh->quote( $data{'streettype'} )
677 . ",address=" . $dbh->quote( $data{'address'} )
678 . ",address2=" . $dbh->quote( $data{'address2'} )
679 . ",zipcode=" . $dbh->quote( $data{'zipcode'} )
680 . ",city=" . $dbh->quote( $data{'city'} )
681 . ",phone=" . $dbh->quote( $data{'phone'} )
682 . ",email=" . $dbh->quote( $data{'email'} )
683 . ",mobile=" . $dbh->quote( $data{'mobile'} )
684 . ",phonepro=" . $dbh->quote( $data{'phonepro'} )
685 . ",opacnote=" . $dbh->quote( $data{'opacnote'} )
686 . ",guarantorid=" . $dbh->quote( $data{'guarantorid'} )
687 . ",dateofbirth=" . $dbh->quote( $data{'dateofbirth'} )
688 . ",branchcode=" . $dbh->quote( $data{'branchcode'} )
689 . ",categorycode=" . $dbh->quote( $data{'categorycode'} )
690 . ",dateenrolled=" . $dbh->quote( $data{'dateenrolled'} )
691 . ",contactname=" . $dbh->quote( $data{'contactname'} )
692 . ",borrowernotes=" . $dbh->quote( $data{'borrowernotes'} )
693 . ",dateexpiry=" . $dbh->quote( $data{'dateexpiry'} )
694 . ",contactnote=" . $dbh->quote( $data{'contactnote'} )
695 . ",B_address=" . $dbh->quote( $data{'B_address'} )
696 . ",B_zipcode=" . $dbh->quote( $data{'B_zipcode'} )
697 . ",B_city=" . $dbh->quote( $data{'B_city'} )
698 . ",B_phone=" . $dbh->quote( $data{'B_phone'} )
699 . ",B_email=" . $dbh->quote( $data{'B_email'} )
700 . ",password=" . $dbh->quote( $data{'password'} )
701 . ",userid=" . $dbh->quote( $data{'userid'} )
702 . ",sort1=" . $dbh->quote( $data{'sort1'} )
703 . ",sort2=" . $dbh->quote( $data{'sort2'} )
704 . ",contacttitle=" . $dbh->quote( $data{'contacttitle'} )
705 . ",emailpro=" . $dbh->quote( $data{'emailpro'} )
706 . ",contactfirstname=" . $dbh->quote( $data{'contactfirstname'} )
707 . ",sex=" . $dbh->quote( $data{'sex'} )
708 . ",fax=" . $dbh->quote( $data{'fax'} )
709 . ",relationship=" . $dbh->quote( $data{'relationship'} )
710 . ",B_streetnumber=" . $dbh->quote( $data{'B_streetnumber'} )
711 . ",B_streettype=" . $dbh->quote( $data{'B_streettype'} )
712 . ",gonenoaddress=" . $dbh->quote( $data{'gonenoaddress'} )
713 . ",lost=" . $dbh->quote( $data{'lost'} )
714 . ",debarred=" . $dbh->quote( $data{'debarred'} )
715 . ",ethnicity=" . $dbh->quote( $data{'ethnicity'} )
716 . ",ethnotes=" . $dbh->quote( $data{'ethnotes'} )
717 . ",altcontactsurname=" . $dbh->quote( $data{'altcontactsurname'} )
718 . ",altcontactfirstname=" . $dbh->quote( $data{'altcontactfirstname'} )
719 . ",altcontactaddress1=" . $dbh->quote( $data{'altcontactaddress1'} )
720 . ",altcontactaddress2=" . $dbh->quote( $data{'altcontactaddress2'} )
721 . ",altcontactaddress3=" . $dbh->quote( $data{'altcontactaddress3'} )
722 . ",altcontactzipcode=" . $dbh->quote( $data{'altcontactzipcode'} )
723 . ",altcontactphone=" . $dbh->quote( $data{'altcontactphone'} ) ;
724 $debug and print STDERR "AddMember SQL: ($query)\n";
725 my $sth = $dbh->prepare($query);
726 # print "Executing SQL: $query\n";
729 $data{'borrowernumber'} = $dbh->{'mysql_insertid'}; # unneeded w/ autoincrement ?
730 # mysql_insertid is probably bad. not necessarily accurate and mysql-specific at best.
732 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
733 if C4::Context->preference("BorrowersLog");
735 # check for enrollment fee & add it if needed
736 $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode=?");
737 $sth->execute($data{'categorycode'});
738 my ($enrolmentfee) = $sth->fetchrow;
740 # insert fee in patron debts
741 manualinvoice($data{'borrowernumber'}, '', '', 'A', $enrolmentfee);
743 return $data{'borrowernumber'};
747 my ($uid,$member) = @_;
748 my $dbh = C4::Context->dbh;
749 # Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
750 # Then we need to tell the user and have them create a new one.
753 "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
754 $sth->execute( $uid, $member );
755 if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
765 my ( $uid, $member, $digest ) = @_;
766 my $dbh = C4::Context->dbh;
768 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
769 #Then we need to tell the user and have them create a new one.
772 "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
773 $sth->execute( $uid, $member );
774 if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
778 #Everything is good so we can update the information.
781 "update borrowers set userid=?, password=? where borrowernumber=?");
782 $sth->execute( $uid, $digest, $member );
786 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
787 if C4::Context->preference("BorrowersLog");
792 =item fixup_cardnumber
794 Warning: The caller is responsible for locking the members table in write
795 mode, to avoid database corruption.
799 use vars qw( @weightings );
800 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
802 sub fixup_cardnumber ($) {
803 my ($cardnumber) = @_;
804 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
805 $autonumber_members = 0 unless defined $autonumber_members;
807 # Find out whether member numbers should be generated
808 # automatically. Should be either "1" or something else.
809 # Defaults to "0", which is interpreted as "no".
811 # if ($cardnumber !~ /\S/ && $autonumber_members) {
812 if ($autonumber_members) {
813 my $dbh = C4::Context->dbh;
814 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
816 # if checkdigit is selected, calculate katipo-style cardnumber.
817 # otherwise, just use the max()
818 # purpose: generate checksum'd member numbers.
819 # We'll assume we just got the max value of digits 2-8 of member #'s
820 # from the database and our job is to increment that by one,
821 # determine the 1st and 9th digits and return the full string.
824 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
828 my $data = $sth->fetchrow_hashref;
829 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
831 if ( !$cardnumber ) { # If DB has no values,
832 $cardnumber = 1000000; # start at 1000000
839 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
841 # read weightings, left to right, 1 char at a time
842 my $temp1 = $weightings[$i];
844 # sequence left to right, 1 char at a time
845 my $temp2 = substr( $cardnumber, $i, 1 );
847 # mult each char 1-7 by its corresponding weighting
848 $sum += $temp1 * $temp2;
851 my $rem = ( $sum % 11 );
852 $rem = 'X' if $rem == 10;
854 $cardnumber = "V$cardnumber$rem";
858 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
859 # better. I'll leave the original in in case it needs to be changed for you
862 "select max(cast(cardnumber as signed)) from borrowers");
864 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
868 my ($result) = $sth->fetchrow;
870 $cardnumber = $result + 1;
878 ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
879 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
880 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
882 C<&GetGuarantees> takes a borrower number (e.g., that of a patron
883 with children) and looks up the borrowers who are guaranteed by that
884 borrower (i.e., the patron's children).
886 C<&GetGuarantees> returns two values: an integer giving the number of
887 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
888 of references to hash, which gives the actual results.
894 my ($borrowernumber) = @_;
895 my $dbh = C4::Context->dbh;
898 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
900 $sth->execute($borrowernumber);
903 my $data = $sth->fetchall_arrayref({});
905 return ( scalar(@$data), $data );
908 =head2 UpdateGuarantees
910 &UpdateGuarantees($parent_borrno);
913 C<&UpdateGuarantees> borrower data for an adulte and updates all the guarantees
914 with the modified information
919 sub UpdateGuarantees {
921 my $dbh = C4::Context->dbh;
922 my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
923 for ( my $i = 0 ; $i < $count ; $i++ ) {
926 # It looks like the $i is only being returned to handle walking through
927 # the array, which is probably better done as a foreach loop.
929 my $guaquery = qq|UPDATE borrowers
930 SET address='$data{'address'}',fax='$data{'fax'}',
931 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
932 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
934 my $sth3 = $dbh->prepare($guaquery);
939 =head2 GetPendingIssues
941 ($count, $issues) = &GetPendingIssues($borrowernumber);
943 Looks up what the patron with the given borrowernumber has borrowed.
945 C<&GetPendingIssues> returns a two-element array. C<$issues> is a
946 reference-to-array, where each element is a reference-to-hash; the
947 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
948 in the Koha database. C<$count> is the number of elements in
954 sub GetPendingIssues {
955 my ($borrowernumber) = @_;
956 my $dbh = C4::Context->dbh;
958 my $sth = $dbh->prepare(
959 "SELECT * FROM issues
960 LEFT JOIN items ON issues.itemnumber=items.itemnumber
961 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
962 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
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
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 SELECT *,items.timestamp AS itemstimestamp
1019 LEFT JOIN items on items.itemnumber=old_issues.itemnumber
1020 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1021 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1022 WHERE borrowernumber=?
1024 if ( $limit != 0 ) {
1025 $query .= " limit $limit";
1029 my $sth = $dbh->prepare($query);
1030 $sth->execute($borrowernumber, $borrowernumber);
1033 while ( my $data = $sth->fetchrow_hashref ) {
1034 $result[$i] = $data;
1039 # get all issued items for borrowernumber from oldissues table
1040 # large chunk of older issues data put into table oldissues
1041 # to speed up db calls for issuing items
1042 if ( C4::Context->preference("ReadingHistory") ) {
1043 # FIXME oldissues (not to be confused with old_issues) is
1044 # apparently specific to HLT. Not sure if the ReadingHistory
1045 # syspref is still required, as old_issues by design
1046 # is no longer checked with each loan.
1047 my $query2 = "SELECT * FROM oldissues
1048 LEFT JOIN items ON items.itemnumber=oldissues.itemnumber
1049 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1050 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1051 WHERE borrowernumber=?
1053 if ( $limit != 0 ) {
1054 $limit = $limit - $count;
1055 $query2 .= " limit $limit";
1058 my $sth2 = $dbh->prepare($query2);
1059 $sth2->execute($borrowernumber);
1061 while ( my $data2 = $sth2->fetchrow_hashref ) {
1062 $result[$i] = $data2;
1069 return ( $i, \@result );
1073 =head2 GetMemberAccountRecords
1075 ($total, $acctlines, $count) = &GetMemberAccountRecords($borrowernumber);
1077 Looks up accounting data for the patron with the given borrowernumber.
1079 C<&GetMemberAccountRecords> returns a three-element array. C<$acctlines> is a
1080 reference-to-array, where each element is a reference-to-hash; the
1081 keys are the fields of the C<accountlines> table in the Koha database.
1082 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1083 total amount outstanding for all of the account lines.
1088 sub GetMemberAccountRecords {
1089 my ($borrowernumber,$date) = @_;
1090 my $dbh = C4::Context->dbh;
1096 WHERE borrowernumber=?);
1097 my @bind = ($borrowernumber);
1098 if ($date && $date ne ''){
1099 $strsth.=" AND date < ? ";
1102 $strsth.=" ORDER BY date desc,timestamp DESC";
1103 my $sth= $dbh->prepare( $strsth );
1104 $sth->execute( @bind );
1106 while ( my $data = $sth->fetchrow_hashref ) {
1107 $acctlines[$numlines] = $data;
1109 $total += $data->{'amountoutstanding'};
1112 return ( $total, \@acctlines,$numlines);
1115 =head2 GetBorNotifyAcctRecord
1117 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1119 Looks up accounting data for the patron with the given borrowernumber per file number.
1121 (FIXME - I'm not at all sure what this is about.)
1123 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1124 reference-to-array, where each element is a reference-to-hash; the
1125 keys are the fields of the C<accountlines> table in the Koha database.
1126 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1127 total amount outstanding for all of the account lines.
1131 sub GetBorNotifyAcctRecord {
1132 my ( $borrowernumber, $notifyid ) = @_;
1133 my $dbh = C4::Context->dbh;
1136 my $sth = $dbh->prepare(
1139 WHERE borrowernumber=?
1141 AND amountoutstanding != '0'
1142 ORDER BY notify_id,accounttype
1144 # 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')
1146 $sth->execute( $borrowernumber, $notifyid );
1148 while ( my $data = $sth->fetchrow_hashref ) {
1149 $acctlines[$numlines] = $data;
1151 $total += $data->{'amountoutstanding'};
1154 return ( $total, \@acctlines, $numlines );
1157 =head2 checkuniquemember (OUEST-PROVENCE)
1159 ($result,$categorycode) = &checkuniquemember($collectivity,$surname,$firstname,$dateofbirth);
1161 Checks that a member exists or not in the database.
1163 C<&result> is nonzero (=exist) or 0 (=does not exist)
1164 C<&categorycode> is from categorycode table
1165 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1166 C<&surname> is the surname
1167 C<&firstname> is the firstname (only if collectivity=0)
1168 C<&dateofbirth> is the date of birth in ISO format (only if collectivity=0)
1172 # FIXME: This function is not legitimate. Multiple patrons might have the same first/last name and birthdate.
1173 # This is especially true since first name is not even a required field.
1175 sub checkuniquemember {
1176 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1177 my $dbh = C4::Context->dbh;
1178 my $request = ($collectivity) ?
1179 "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? " :
1180 "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? and firstname=? and dateofbirth=? ";
1181 my $sth = $dbh->prepare($request);
1182 if ($collectivity) {
1183 $sth->execute( uc($surname) );
1185 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1187 my @data = $sth->fetchrow;
1189 ( $data[0] ) and return $data[0], $data[1];
1193 sub checkcardnumber {
1194 my ($cardnumber,$borrowernumber) = @_;
1195 my $dbh = C4::Context->dbh;
1196 my $query = "SELECT * FROM borrowers WHERE cardnumber=?";
1197 $query .= " AND borrowernumber <> ?" if ($borrowernumber);
1198 my $sth = $dbh->prepare($query);
1199 if ($borrowernumber) {
1200 $sth->execute($cardnumber,$borrowernumber);
1202 $sth->execute($cardnumber);
1204 if (my $data= $sth->fetchrow_hashref()){
1214 =head2 getzipnamecity (OUEST-PROVENCE)
1216 take all info from table city for the fields city and zip
1217 check for the name and the zip code of the city selected
1221 sub getzipnamecity {
1223 my $dbh = C4::Context->dbh;
1226 "select city_name,city_zipcode from cities where cityid=? ");
1227 $sth->execute($cityid);
1228 my @data = $sth->fetchrow;
1229 return $data[0], $data[1];
1233 =head2 getdcity (OUEST-PROVENCE)
1235 recover cityid with city_name condition
1240 my ($city_name) = @_;
1241 my $dbh = C4::Context->dbh;
1242 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1243 $sth->execute($city_name);
1244 my $data = $sth->fetchrow;
1249 =head2 GetExpiryDate
1251 $expirydate = GetExpiryDate($categorycode, $dateenrolled);
1253 Calculate expiry date given a categorycode and starting date. Date argument must be in ISO format.
1254 Return date is also in ISO format.
1259 my ( $categorycode, $dateenrolled ) = @_;
1260 my $enrolmentperiod = 12; # reasonable default
1261 if ($categorycode) {
1262 my $dbh = C4::Context->dbh;
1263 my $sth = $dbh->prepare("select enrolmentperiod from categories where categorycode=?");
1264 $sth->execute($categorycode);
1265 $enrolmentperiod = $sth->fetchrow;
1267 # die "GetExpiryDate: for enrollmentperiod $enrolmentperiod (category '$categorycode') starting $dateenrolled.\n";
1268 my @date = split /-/,$dateenrolled;
1269 return sprintf("%04d-%02d-%02d", Add_Delta_YM(@date,0,$enrolmentperiod));
1272 =head2 checkuserpassword (OUEST-PROVENCE)
1274 check for the password and login are not used
1275 return the number of record
1276 0=> NOT USED 1=> USED
1280 sub checkuserpassword {
1281 my ( $borrowernumber, $userid, $password ) = @_;
1282 $password = md5_base64($password);
1283 my $dbh = C4::Context->dbh;
1286 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1288 $sth->execute( $borrowernumber, $userid, $password );
1289 my $number_rows = $sth->fetchrow;
1290 return $number_rows;
1294 =head2 GetborCatFromCatType
1296 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1298 Looks up the different types of borrowers in the database. Returns two
1299 elements: a reference-to-array, which lists the borrower category
1300 codes, and a reference-to-hash, which maps the borrower category codes
1301 to category descriptions.
1306 sub GetborCatFromCatType {
1307 my ( $category_type, $action ) = @_;
1308 my $dbh = C4::Context->dbh;
1309 my $request = qq| SELECT categorycode,description
1312 ORDER BY categorycode|;
1313 my $sth = $dbh->prepare($request);
1315 $sth->execute($category_type);
1324 while ( my $data = $sth->fetchrow_hashref ) {
1325 push @codes, $data->{'categorycode'};
1326 $labels{ $data->{'categorycode'} } = $data->{'description'};
1329 return ( \@codes, \%labels );
1332 =head2 GetBorrowercategory
1334 $hashref = &GetBorrowercategory($categorycode);
1336 Given the borrower's category code, the function returns the corresponding
1337 data hashref for a comprehensive information display.
1341 sub GetBorrowercategory {
1343 my $dbh = C4::Context->dbh;
1346 "SELECT description,dateofbirthrequired,upperagelimit,category_type
1348 WHERE categorycode = ?"
1350 $sth->execute($catcode);
1352 $sth->fetchrow_hashref;
1355 } # sub getborrowercategory
1357 =head2 ethnicitycategories
1359 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1361 Looks up the different ethnic types in the database. Returns two
1362 elements: a reference-to-array, which lists the ethnicity codes, and a
1363 reference-to-hash, which maps the ethnicity codes to ethnicity
1370 sub ethnicitycategories {
1371 my $dbh = C4::Context->dbh;
1372 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1376 while ( my $data = $sth->fetchrow_hashref ) {
1377 push @codes, $data->{'code'};
1378 $labels{ $data->{'code'} } = $data->{'name'};
1381 return ( \@codes, \%labels );
1386 $ethn_name = &fixEthnicity($ethn_code);
1388 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1389 corresponding descriptive name from the C<ethnicity> table in the
1390 Koha database ("European" or "Pacific Islander").
1397 my $ethnicity = shift;
1398 return unless $ethnicity;
1399 my $dbh = C4::Context->dbh;
1400 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1401 $sth->execute($ethnicity);
1402 my $data = $sth->fetchrow_hashref;
1404 return $data->{'name'};
1405 } # sub fixEthnicity
1409 $dateofbirth,$date = &GetAge($date);
1411 this function return the borrowers age with the value of dateofbirth
1417 my ( $date, $date_ref ) = @_;
1419 if ( not defined $date_ref ) {
1420 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1423 my ( $year1, $month1, $day1 ) = split /-/, $date;
1424 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1426 my $age = $year2 - $year1;
1427 if ( $month1 . $day1 > $month2 . $day2 ) {
1434 =head2 get_institutions
1435 $insitutions = get_institutions();
1437 Just returns a list of all the borrowers of type I, borrownumber and name
1442 sub get_institutions {
1443 my $dbh = C4::Context->dbh();
1446 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1450 while ( my $data = $sth->fetchrow_hashref() ) {
1451 $orgs{ $data->{'borrowernumber'} } = $data;
1456 } # sub get_institutions
1458 =head2 add_member_orgs
1460 add_member_orgs($borrowernumber,$borrowernumbers);
1462 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1467 sub add_member_orgs {
1468 my ( $borrowernumber, $otherborrowers ) = @_;
1469 my $dbh = C4::Context->dbh();
1471 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1472 my $sth = $dbh->prepare($query);
1473 foreach my $otherborrowernumber (@$otherborrowers) {
1474 $sth->execute( $borrowernumber, $otherborrowernumber );
1478 } # sub add_member_orgs
1480 =head2 GetCities (OUEST-PROVENCE)
1482 ($id_cityarrayref, $city_hashref) = &GetCities();
1484 Looks up the different city and zip in the database. Returns two
1485 elements: a reference-to-array, which lists the zip city
1486 codes, and a reference-to-hash, which maps the name of the city.
1487 WHERE =>OUEST PROVENCE OR EXTERIEUR
1493 #my ($type_city) = @_;
1494 my $dbh = C4::Context->dbh;
1495 my $query = qq|SELECT cityid,city_zipcode,city_name
1497 ORDER BY city_name|;
1498 my $sth = $dbh->prepare($query);
1500 #$sth->execute($type_city);
1504 # insert empty value to create a empty choice in cgi popup
1507 while ( my $data = $sth->fetchrow_hashref ) {
1508 push @id, $data->{'city_zipcode'}."|".$data->{'city_name'};
1509 $city{ $data->{'city_zipcode'}."|".$data->{'city_name'} } = $data->{'city_name'};
1512 #test to know if the table contain some records if no the function return nothing
1516 # all we have is the one blank row
1521 return ( \@id, \%city );
1525 =head2 GetSortDetails (OUEST-PROVENCE)
1527 ($lib) = &GetSortDetails($category,$sortvalue);
1529 Returns the authorized value details
1530 C<&$lib>return value of authorized value details
1531 C<&$sortvalue>this is the value of authorized value
1532 C<&$category>this is the value of authorized value category
1536 sub GetSortDetails {
1537 my ( $category, $sortvalue ) = @_;
1538 my $dbh = C4::Context->dbh;
1539 my $query = qq|SELECT lib
1540 FROM authorised_values
1542 AND authorised_value=? |;
1543 my $sth = $dbh->prepare($query);
1544 $sth->execute( $category, $sortvalue );
1545 my $lib = $sth->fetchrow;
1546 return ($lib) if ($lib);
1547 return ($sortvalue) unless ($lib);
1550 =head2 DeleteBorrower
1552 () = &DeleteBorrower($member);
1554 delete all data fo borrowers and add record to deletedborrowers table
1555 C<&$member>this is the borrowernumber
1559 sub MoveMemberToDeleted {
1561 my $dbh = C4::Context->dbh;
1563 $query = qq|SELECT *
1565 WHERE borrowernumber=?|;
1566 my $sth = $dbh->prepare($query);
1567 $sth->execute($member);
1568 my @data = $sth->fetchrow_array;
1571 $dbh->prepare( "INSERT INTO deletedborrowers VALUES ("
1572 . ( "?," x ( scalar(@data) - 1 ) )
1574 $sth->execute(@data);
1580 DelMember($borrowernumber);
1582 This function remove directly a borrower whitout writing it on deleteborrower.
1583 + Deletes reserves for the borrower
1588 my $dbh = C4::Context->dbh;
1589 my $borrowernumber = shift;
1590 #warn "in delmember with $borrowernumber";
1591 return unless $borrowernumber; # borrowernumber is mandatory.
1593 my $query = qq|DELETE
1595 WHERE borrowernumber=?|;
1596 my $sth = $dbh->prepare($query);
1597 $sth->execute($borrowernumber);
1602 WHERE borrowernumber = ?
1604 $sth = $dbh->prepare($query);
1605 $sth->execute($borrowernumber);
1607 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$borrowernumber,"")
1608 if C4::Context->preference("BorrowersLog");
1612 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1614 $date = ExtendMemberSubscriptionTo($borrowerid, $date);
1616 Extending the subscription to a given date or to the expiry date calculated on ISO date.
1621 sub ExtendMemberSubscriptionTo {
1622 my ( $borrowerid,$date) = @_;
1623 my $dbh = C4::Context->dbh;
1624 my $borrower = GetMember($borrowerid,'borrowernumber');
1626 $date=POSIX::strftime("%Y-%m-%d",localtime());
1627 my $borrower = GetMember($borrowerid,'borrowernumber');
1628 $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
1630 my $sth = $dbh->do(<<EOF);
1632 SET dateexpiry='$date'
1633 WHERE borrowernumber='$borrowerid'
1635 # add enrolmentfee if needed
1636 $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode=?");
1637 $sth->execute($borrower->{'categorycode'});
1638 my ($enrolmentfee) = $sth->fetchrow;
1639 if ($enrolmentfee) {
1640 # insert fee in patron debts
1641 manualinvoice($borrower->{'borrowernumber'}, '', '', 'A', $enrolmentfee);
1643 return $date if ($sth);
1647 =head2 GetRoadTypes (OUEST-PROVENCE)
1649 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1651 Looks up the different road type . Returns two
1652 elements: a reference-to-array, which lists the id_roadtype
1653 codes, and a reference-to-hash, which maps the road type of the road .
1658 my $dbh = C4::Context->dbh;
1660 SELECT roadtypeid,road_type
1662 ORDER BY road_type|;
1663 my $sth = $dbh->prepare($query);
1668 # insert empty value to create a empty choice in cgi popup
1670 while ( my $data = $sth->fetchrow_hashref ) {
1672 push @id, $data->{'roadtypeid'};
1673 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1676 #test to know if the table contain some records if no the function return nothing
1684 return ( \@id, \%roadtype );
1690 =head2 GetTitles (OUEST-PROVENCE)
1692 ($borrowertitle)= &GetTitles();
1694 Looks up the different title . Returns array with all borrowers title
1699 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1700 unshift( @borrowerTitle, "" );
1701 my $count=@borrowerTitle;
1706 return ( \@borrowerTitle);
1710 =head2 GetPatronImage
1712 my ($imagedata, $dberror) = GetPatronImage($cardnumber);
1714 Returns the mimetype and binary image data of the image for the patron with the supplied cardnumber.
1718 sub GetPatronImage {
1719 my ($cardnumber) = @_;
1720 warn "Cardnumber passed to GetPatronImage is $cardnumber" if $debug;
1721 my $dbh = C4::Context->dbh;
1722 my $query = "SELECT mimetype, imagefile FROM patronimage WHERE cardnumber = ?;";
1723 my $sth = $dbh->prepare($query);
1724 $sth->execute($cardnumber);
1725 my $imagedata = $sth->fetchrow_hashref;
1726 my $dberror = $sth->errstr;
1727 warn "Database error!" if $sth->errstr;
1729 return $imagedata, $dberror;
1732 =head2 PutPatronImage
1734 PutPatronImage($cardnumber, $mimetype, $imgfile);
1736 Stores patron binary image data and mimetype in database.
1740 sub PutPatronImage {
1741 my ($cardnumber, $mimetype, $imgfile) = @_;
1742 warn "Parameters passed in: Cardnumber=$cardnumber, Mimetype=$mimetype, " . ($imgfile ? "Imagefile" : "No Imagefile") if $debug;
1743 my $dbh = C4::Context->dbh;
1744 my $query = "INSERT INTO patronimage (cardnumber, mimetype, imagefile) VALUES (?,?,?) ON DUPLICATE KEY UPDATE imagefile = ?;";
1745 my $sth = $dbh->prepare($query);
1746 $sth->execute($cardnumber,$mimetype,$imgfile,$imgfile);
1747 warn "Error returned inserting $cardnumber.$mimetype." if $sth->errstr;
1748 my $dberror = $sth->errstr;
1753 =head2 RmPatronImage
1755 my ($dberror) = RmPatronImage($cardnumber);
1757 Removes the image for the patron with the supplied cardnumber.
1762 my ($cardnumber) = @_;
1763 warn "Cardnumber passed to GetPatronImage is $cardnumber" if $debug;
1764 my $dbh = C4::Context->dbh;
1765 my $query = "DELETE FROM patronimage WHERE cardnumber = ?;";
1766 my $sth = $dbh->prepare($query);
1767 $sth->execute($cardnumber);
1768 my $dberror = $sth->errstr;
1769 warn "Database error!" if $sth->errstr;
1774 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1776 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1778 Returns the description of roadtype
1779 C<&$roadtype>return description of road type
1780 C<&$roadtypeid>this is the value of roadtype s
1784 sub GetRoadTypeDetails {
1785 my ($roadtypeid) = @_;
1786 my $dbh = C4::Context->dbh;
1790 WHERE roadtypeid=?|;
1791 my $sth = $dbh->prepare($query);
1792 $sth->execute($roadtypeid);
1793 my $roadtype = $sth->fetchrow;
1797 =head2 GetBorrowersWhoHaveNotBorrowedSince
1799 &GetBorrowersWhoHaveNotBorrowedSince($date)
1801 this function get all borrowers who haven't borrowed since the date given on input arg.
1805 sub GetBorrowersWhoHaveNotBorrowedSince {
1807 return unless $date; # date is mandatory.
1808 my $dbh = C4::Context->dbh;
1810 SELECT borrowers.borrowernumber,max(timestamp)
1812 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1813 WHERE issues.borrowernumber IS NOT NULL
1814 GROUP BY borrowers.borrowernumber
1816 my $sth = $dbh->prepare($query);
1820 while ( my $data = $sth->fetchrow_hashref ) {
1821 push @results, $data;
1826 =head2 GetBorrowersWhoHaveNeverBorrowed
1828 $results = &GetBorrowersWhoHaveNeverBorrowed
1830 this function get all borrowers who have never borrowed.
1832 I<$result> is a ref to an array which all elements are a hasref.
1836 sub GetBorrowersWhoHaveNeverBorrowed {
1837 my $dbh = C4::Context->dbh;
1839 SELECT borrowers.borrowernumber,max(timestamp)
1841 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1842 WHERE issues.borrowernumber IS NULL
1844 my $sth = $dbh->prepare($query);
1847 while ( my $data = $sth->fetchrow_hashref ) {
1848 push @results, $data;
1853 =head2 GetBorrowersWithIssuesHistoryOlderThan
1855 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1857 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1859 I<$result> is a ref to an array which all elements are a hashref.
1860 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
1864 sub GetBorrowersWithIssuesHistoryOlderThan {
1865 my $dbh = C4::Context->dbh;
1867 return unless $date; # date is mandatory.
1869 SELECT count(borrowernumber) as n,borrowernumber
1871 WHERE returndate < ?
1872 AND borrowernumber IS NOT NULL
1873 GROUP BY borrowernumber
1875 my $sth = $dbh->prepare($query);
1876 $sth->execute($date);
1879 while ( my $data = $sth->fetchrow_hashref ) {
1880 push @results, $data;
1885 END { } # module clean-up code here (global destructor)