3 # Copyright 2000-2003 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
26 use Digest::MD5 qw(md5_base64);
27 use Date::Calc qw/Today Add_Delta_YM/;
28 use C4::Log; # logaction
33 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
35 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
39 C4::Members - Perl Module containing convenience functions for member handling
47 This module contains routines for adding, modifying and deleting members/patrons/borrowers
58 &BornameSearch &GetMember &GetMemberDetails
60 &fixup_cardnumber &findguarantees &findguarantor &GuarantornameSearch
61 &modmember &newmember &changepassword &borrissues &allissues
62 &checkuniquemember &getzipnamecity &getidcity &getguarantordata &getcategorytype
64 &calcexpirydate &checkuserpassword
66 &GetborCatFromCatType &getborrowercategory
68 ðnicitycategories &get_institutions add_member_orgs
69 &get_age &GetBorrowersFromSurname &GetBranchCodeFromBorrowers
70 &GetFlagsAndBranchFromBorrower
71 &GetCities &GetRoadTypes &GetRoadTypeDetails &GetBorNotifyAcctRecord
72 &GetMembeReregistration
75 &GetBorrowersWhoHaveNotBorrowedSince
76 &GetBorrowersWhoHaveNeverBorrowed
77 &GetBorrowersWithIssuesHistoryOlderThan
82 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
84 Looks up patrons (borrowers) by name.
88 BUGFIX 499: C<$type> is now used to determine type of search.
89 if $type is "simple", search is performed on the first letter of the
92 C<$searchstring> is a space-separated list of search terms. Each term
93 must match the beginning a borrower's surname, first name, or other
96 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
97 reference-to-array; each element is a reference-to-hash, whose keys
98 are the fields of the C<borrowers> table in the Koha database.
99 C<$count> is the number of elements in C<$borrowers>.
104 #used by member enquiries from the intranet
107 my ( $env, $searchstring, $orderby, $type ) = @_;
108 my $dbh = C4::Context->dbh;
114 if ( $type eq "simple" ) # simple search for one letter only
117 "SELECT * FROM borrowers
118 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
119 WHERE surname LIKE ? ORDER BY $orderby";
120 @bind = ("$searchstring%");
122 else # advanced search looking in surname, firstname and othernames
124 @data = split( ' ', $searchstring );
126 $query = "SELECT * FROM borrowers
127 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
128 WHERE ((surname LIKE ? OR surname LIKE ?
129 OR firstname LIKE ? OR firstname LIKE ?
130 OR othernames LIKE ? OR othernames LIKE ?)
133 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
134 "$data[0]%", "% $data[0]%"
136 for ( my $i = 1 ; $i < $count ; $i++ ) {
137 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
138 OR firstname LIKE ? OR firstname LIKE ?
139 OR othernames LIKE ? OR othernames LIKE ?)";
141 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
142 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
146 $query = $query . ") OR cardnumber LIKE ?
148 push( @bind, $searchstring );
153 my $sth = $dbh->prepare($query);
155 # warn "Q $orderby : $query";
156 $sth->execute(@bind);
158 my $cnt = $sth->rows;
159 while ( my $data = $sth->fetchrow_hashref ) {
160 push( @results, $data );
165 return ( $cnt, \@results );
168 =head3 GetFlagsAndBranchFromBorrower
172 ($flags, $homebranch) = GetFlagsAndBranchFromBorrower($loggedinuser);
174 this function read on the database to get flags and homebranch for a user
178 it returns the $flags & the homebranch in scalar context.
184 sub GetFlagsAndBranchFromBorrower {
185 my $loggedinuser = @_;
186 my $dbh = C4::Context->dbh;
188 SELECT flags, branchcode
190 WHERE borrowernumber = ?
192 my $sth = $dbh->prepare($query);
193 $sth->execute($loggedinuser);
195 return $sth->fetchrow;
200 $borrower = &GetMember($cardnumber, $borrowernumber);
202 Looks up information about a patron (borrower) by either card number
203 or borrower number. If $borrowernumber is specified, C<&borrdata>
204 searches by borrower number; otherwise, it searches by card number.
206 C<&GetMember> returns a reference-to-hash whose keys are the fields of
207 the C<borrowers> table in the Koha database.
212 my ( $cardnumber, $borrowernumber ) = @_;
213 $cardnumber = uc $cardnumber;
214 my $dbh = C4::Context->dbh;
216 if ( $borrowernumber eq '' ) {
217 $sth = $dbh->prepare("Select * from borrowers where cardnumber=?");
218 $sth->execute($cardnumber);
221 $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
222 $sth->execute($borrowernumber);
224 my $data = $sth->fetchrow_hashref;
229 else { # try with firstname
232 $dbh->prepare("select * from borrowers where firstname=?");
233 $sth->execute($cardnumber);
234 my $data = $sth->fetchrow_hashref;
242 =head2 GetMemberDetails
244 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
246 Looks up a patron and returns information about him or her. If
247 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
248 up the borrower by number; otherwise, it looks up the borrower by card
251 C<$env> is effectively ignored, but should be a reference-to-hash.
253 C<$borrower> is a reference-to-hash whose keys are the fields of the
254 borrowers table in the Koha database. In addition,
255 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
256 about the patron. Its keys act as flags :
258 if $borrower->{flags}->{LOST} {
259 # Patron's card was reported lost
262 Each flag has a C<message> key, giving a human-readable explanation of
263 the flag. If the state of a flag means that the patron should not be
264 allowed to borrow any more books, then it will have a C<noissues> key
267 The possible flags are:
273 =item Shows the patron's credit or debt, if any.
281 =item (Gone, no address.) Set if the patron has left without giving a
290 =item Set if the patron's card has been reported as lost.
298 =item Set if the patron has been debarred.
306 =item Any additional notes about the patron.
314 =item Set if the patron has overdue items. This flag has several keys:
316 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
317 overdue items. Its elements are references-to-hash, each describing an
318 overdue item. The keys are selected fields from the issues, biblio,
319 biblioitems, and items tables of the Koha database.
321 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
322 the overdue items, one per line.
330 =item Set if any items that the patron has reserved are available.
332 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
333 available items. Each element is a reference-to-hash whose keys are
334 fields from the reserves table of the Koha database.
340 sub GetMemberDetails {
341 my ( $borrowernumber, $cardnumber ) = @_;
342 my $dbh = C4::Context->dbh;
345 if ($borrowernumber) {
346 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
347 $sth->execute($borrowernumber);
349 elsif ($cardnumber) {
350 $sth = $dbh->prepare("select * from borrowers where cardnumber=?");
351 $sth->execute($cardnumber);
356 my $borrower = $sth->fetchrow_hashref;
357 my $amount = C4::Accounts::checkaccount( $borrowernumber, $dbh );
358 $borrower->{'amountoutstanding'} = $amount;
359 my $flags = patronflags( $borrower, $dbh );
362 $sth = $dbh->prepare("select bit,flag from userflags");
364 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
365 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
366 $accessflagshash->{$flag} = 1;
370 $borrower->{'flags'} = $flags;
371 $borrower->{'authflags'} = $accessflagshash;
373 # find out how long the membership lasts
376 "select enrolmentperiod from categories where categorycode = ?");
377 $sth->execute( $borrower->{'categorycode'} );
378 my $enrolment = $sth->fetchrow;
379 $borrower->{'enrolmentperiod'} = $enrolment;
380 return ($borrower); #, $flags, $accessflagshash);
387 NOTE!: If you change this function, be sure to update the POD for
390 $flags = &patronflags($env, $patron, $dbh);
393 {message} Message showing patron's credit or debt
394 {noissues} Set if patron owes >$5.00
395 {GNA} Set if patron gone w/o address
396 {message} "Borrower has no valid address"
398 {LOST} Set if patron's card reported lost
399 {message} Message to this effect
401 {DBARRED} Set is patron is debarred
402 {message} Message to this effect
404 {NOTES} Set if patron has notes
405 {message} Notes about patron
406 {ODUES} Set if patron has overdue books
408 {itemlist} ref-to-array: list of overdue books
409 {itemlisttext} Text list of overdue items
410 {WAITING} Set if there are items available that the
412 {message} Message to this effect
413 {itemlist} ref-to-array: list of available items
419 my ( $patroninformation, $dbh ) = @_;
421 C4::Accounts::checkaccount( $patroninformation->{'borrowernumber'}, $dbh );
424 my $noissuescharge = C4::Context->preference("noissuescharge");
425 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
426 if ( $amount > $noissuescharge ) {
427 $flaginfo{'noissues'} = 1;
429 $flags{'CHARGES'} = \%flaginfo;
431 elsif ( $amount < 0 ) {
433 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
434 $flags{'CHARGES'} = \%flaginfo;
436 if ( $patroninformation->{'gonenoaddress'}
437 && $patroninformation->{'gonenoaddress'} == 1 )
440 $flaginfo{'message'} = 'Borrower has no valid address.';
441 $flaginfo{'noissues'} = 1;
442 $flags{'GNA'} = \%flaginfo;
444 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
446 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
447 $flaginfo{'noissues'} = 1;
448 $flags{'LOST'} = \%flaginfo;
450 if ( $patroninformation->{'debarred'}
451 && $patroninformation->{'debarred'} == 1 )
454 $flaginfo{'message'} = 'Borrower is Debarred.';
455 $flaginfo{'noissues'} = 1;
456 $flags{'DBARRED'} = \%flaginfo;
458 if ( $patroninformation->{'borrowernotes'}
459 && $patroninformation->{'borrowernotes'} )
462 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
463 $flags{'NOTES'} = \%flaginfo;
465 my ( $odues, $itemsoverdue ) =
466 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
469 $flaginfo{'message'} = "Yes";
470 $flaginfo{'itemlist'} = $itemsoverdue;
471 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
474 $flaginfo{'itemlisttext'} .=
475 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
477 $flags{'ODUES'} = \%flaginfo;
480 C4::Reserves2::GetWaitingReserves( $patroninformation->{'borrowernumber'} );
481 my $nowaiting = scalar @$itemswaiting;
482 if ( $nowaiting > 0 ) {
484 $flaginfo{'message'} = "Reserved items available";
485 $flaginfo{'itemlist'} = $itemswaiting;
486 $flags{'WAITING'} = \%flaginfo;
494 $borrower = &borrdata($cardnumber, $borrowernumber);
496 Looks up information about a patron (borrower) by either card number
497 or borrower number. If $borrowernumber is specified, C<&borrdata>
498 searches by borrower number; otherwise, it searches by card number.
500 C<&borrdata> returns a reference-to-hash whose keys are the fields of
501 the C<borrowers> table in the Koha database.
507 my ( $cardnumber, $borrowernumber ) = @_;
508 $cardnumber = uc $cardnumber;
509 my $dbh = C4::Context->dbh;
511 if ( $borrowernumber eq '' ) {
514 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?"
516 $sth->execute($cardnumber);
521 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
523 $sth->execute($borrowernumber);
525 my $data = $sth->fetchrow_hashref;
531 elsif ($cardnumber) { # try with firstname
534 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname=?"
536 $sth->execute($cardnumber);
537 my $data = $sth->fetchrow_hashref;
548 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
550 Returns aggregate data about items borrowed by the patron with the
551 given borrowernumber.
555 C<&borrdata2> returns a three-element array. C<$borrowed> is the
556 number of books the patron currently has borrowed. C<$due> is the
557 number of overdue items the patron currently has borrowed. C<$fine> is
558 the total fine currently due by the borrower.
564 my ( $env, $borrowernumber ) = @_;
565 my $dbh = C4::Context->dbh;
567 "Select count(*) from issues where borrowernumber='$borrowernumber' and
571 my $sth = $dbh->prepare($query);
573 my $data = $sth->fetchrow_hashref;
575 $sth = $dbh->prepare(
576 "Select count(*) from issues where
577 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
580 my $data2 = $sth->fetchrow_hashref;
582 $sth = $dbh->prepare(
583 "Select sum(amountoutstanding) from accountlines where
584 borrowernumber='$borrowernumber'"
587 my $data3 = $sth->fetchrow_hashref;
590 return ( $data2->{'count(*)'}, $data->{'count(*)'},
591 $data3->{'sum(amountoutstanding)'} );
596 my $dbh = C4::Context->dbh;
597 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
598 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
599 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
601 # warn "num user".$data{'borrowernumber'};
604 $data{'userid'} = '' if ( $data{'password'} eq '' );
606 # test to know if u must update or not the borrower password
607 if ( $data{'password'} eq '****' ) {
609 $query = "UPDATE borrowers SET
610 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
611 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
612 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
613 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
614 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
615 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
616 ethnotes = ?,sex = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
617 WHERE borrowernumber=$data{'borrowernumber'}";
618 $sth = $dbh->prepare($query);
620 $data{'cardnumber'}, $data{'surname'},
621 $data{'firstname'}, $data{'title'},
622 $data{'othernames'}, $data{'initials'},
623 $data{'streetnumber'}, $data{'streettype'},
624 $data{'address'}, $data{'address2'},
625 $data{'city'}, $data{'zipcode'},
626 $data{'email'}, $data{'phone'},
627 $data{'mobile'}, $data{'fax'},
628 $data{'emailpro'}, $data{'phonepro'},
629 $data{'B_streetnumber'}, $data{'B_streettype'},
630 $data{'B_address'}, $data{'B_city'},
631 $data{'B_zipcode'}, $data{'B_email'},
632 $data{'B_phone'}, $data{'dateofbirth'},
633 $data{'branchcode'}, $data{'categorycode'},
634 $data{'dateenrolled'}, $data{'dateexpiry'},
635 $data{'gonenoaddress'}, $data{'lost'},
636 $data{'debarred'}, $data{'contactname'},
637 $data{'contactfirstname'}, $data{'contacttitle'},
638 $data{'guarantorid'}, $data{'borrowernotes'},
639 $data{'relationship'}, $data{'ethnicity'},
640 $data{'ethnotes'}, $data{'sex'},
641 $data{'userid'}, $data{'opacnote'},
642 $data{'contactnote'}, $data{'sort1'},
648 ( $data{'password'} = md5_base64( $data{'password'} ) )
649 if ( $data{'password'} ne '' );
650 $query = "UPDATE borrowers SET
651 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
652 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
653 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
654 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
655 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
656 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
657 ethnotes = ?,sex = ?,password = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
658 WHERE borrowernumber=$data{'borrowernumber'}";
659 $sth = $dbh->prepare($query);
661 $data{'cardnumber'}, $data{'surname'},
662 $data{'firstname'}, $data{'title'},
663 $data{'othernames'}, $data{'initials'},
664 $data{'streetnumber'}, $data{'streettype'},
665 $data{'address'}, $data{'address2'},
666 $data{'city'}, $data{'zipcode'},
667 $data{'email'}, $data{'phone'},
668 $data{'mobile'}, $data{'fax'},
669 $data{'emailpro'}, $data{'phonepro'},
670 $data{'B_streetnumber'}, $data{'B_streettype'},
671 $data{'B_address'}, $data{'B_city'},
672 $data{'B_zipcode'}, $data{'B_email'},
673 $data{'B_phone'}, $data{'dateofbirth'},
674 $data{'branchcode'}, $data{'categorycode'},
675 $data{'dateenrolled'}, $data{'dateexpiry'},
676 $data{'gonenoaddress'}, $data{'lost'},
677 $data{'debarred'}, $data{'contactname'},
678 $data{'contactfirstname'}, $data{'contacttitle'},
679 $data{'guarantorid'}, $data{'borrowernotes'},
680 $data{'relationship'}, $data{'ethnicity'},
681 $data{'ethnotes'}, $data{'sex'},
682 $data{'password'}, $data{'userid'},
683 $data{'opacnote'}, $data{'contactnote'},
684 $data{'sort1'}, $data{'sort2'}
689 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
690 # so when we update information for an adult we should check for guarantees and update the relevant part
691 # of their records, ie addresses and phone numbers
692 my ( $category_type, undef ) = getcategorytype( $data{'category_type'} );
693 if ( $category_type eq 'A' ) {
695 # is adult check guarantees;
696 updateguarantees(%data);
699 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
700 if C4::Context->preference("BorrowersLog");
705 my $dbh = C4::Context->dbh;
706 $data{'userid'} = '' unless $data{'password'};
707 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
708 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
709 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
710 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
712 "insert into borrowers set cardnumber="
713 . $dbh->quote( $data{'cardnumber'} )
715 . $dbh->quote( $data{'surname'} )
717 . $dbh->quote( $data{'firstname'} )
719 . $dbh->quote( $data{'title'} )
721 . $dbh->quote( $data{'othernames'} )
723 . $dbh->quote( $data{'initials'} )
725 . $dbh->quote( $data{'streetnumber'} )
727 . $dbh->quote( $data{'streettype'} )
729 . $dbh->quote( $data{'address'} )
731 . $dbh->quote( $data{'address2'} )
733 . $dbh->quote( $data{'zipcode'} )
735 . $dbh->quote( $data{'city'} )
737 . $dbh->quote( $data{'phone'} )
739 . $dbh->quote( $data{'email'} )
741 . $dbh->quote( $data{'mobile'} )
743 . $dbh->quote( $data{'phonepro'} )
745 . $dbh->quote( $data{'opacnote'} )
747 . $dbh->quote( $data{'guarantorid'} )
749 . $dbh->quote( $data{'dateofbirth'} )
751 . $dbh->quote( $data{'branchcode'} )
753 . $dbh->quote( $data{'categorycode'} )
755 . $dbh->quote( $data{'dateenrolled'} )
757 . $dbh->quote( $data{'contactname'} )
759 . $dbh->quote( $data{'borrowernotes'} )
761 . $dbh->quote( $data{'dateexpiry'} )
763 . $dbh->quote( $data{'contactnote'} )
765 . $dbh->quote( $data{'B_address'} )
767 . $dbh->quote( $data{'B_zipcode'} )
769 . $dbh->quote( $data{'B_city'} )
771 . $dbh->quote( $data{'B_phone'} )
773 . $dbh->quote( $data{'B_email'}, )
775 . $dbh->quote( $data{'password'} )
777 . $dbh->quote( $data{'userid'} )
779 . $dbh->quote( $data{'sort1'} )
781 . $dbh->quote( $data{'sort2'} )
783 . $dbh->quote( $data{'contacttitle'} )
785 . $dbh->quote( $data{'emailpro'} )
786 . ",contactfirstname="
787 . $dbh->quote( $data{'contactfirstname'} ) . ",sex="
788 . $dbh->quote( $data{'sex'} ) . ",fax="
789 . $dbh->quote( $data{'fax'} )
791 . $dbh->quote( $data{'relationship'} )
793 . $dbh->quote( $data{'B_streetnumber'} )
795 . $dbh->quote( $data{'B_streettype'} )
797 . $dbh->quote( $data{'gonenoaddress'} )
799 . $dbh->quote( $data{'lost'} )
801 . $dbh->quote( $data{'debarred'} )
803 . $dbh->quote( $data{'ethnicity'} )
805 . $dbh->quote( $data{'ethnotes'} );
807 my $sth = $dbh->prepare($query);
810 $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
812 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
813 if C4::Context->preference("BorrowersLog");
815 return $data{'borrowernumber'};
819 my ( $uid, $member, $digest ) = @_;
820 my $dbh = C4::Context->dbh;
822 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
823 #Then we need to tell the user and have them create a new one.
826 "select * from borrowers where userid=? and borrowernumber != ?");
827 $sth->execute( $uid, $member );
828 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
833 #Everything is good so we can update the information.
836 "update borrowers set userid=?, password=? where borrowernumber=?");
837 $sth->execute( $uid, $digest, $member );
841 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
842 if C4::Context->preference("BorrowersLog");
845 sub getmemberfromuserid {
847 my $dbh = C4::Context->dbh;
848 my $sth = $dbh->prepare("select * from borrowers where userid=?");
849 $sth->execute($userid);
850 return $sth->fetchrow_hashref;
853 sub updateguarantees {
855 my $dbh = C4::Context->dbh;
856 my ( $count, $guarantees ) = findguarantees( $data{'borrowernumber'} );
857 for ( my $i = 0 ; $i < $count ; $i++ ) {
860 # It looks like the $i is only being returned to handle walking through
861 # the array, which is probably better done as a foreach loop.
863 my $guaquery = qq|UPDATE borrowers
864 SET address='$data{'address'}',fax='$data{'fax'}',
865 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
866 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
868 my $sth3 = $dbh->prepare($guaquery);
874 =item fixup_cardnumber
876 Warning: The caller is responsible for locking the members table in write
877 mode, to avoid database corruption.
881 use vars qw( @weightings );
882 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
884 sub fixup_cardnumber ($) {
885 my ($cardnumber) = @_;
886 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
887 $autonumber_members = 0 unless defined $autonumber_members;
889 # Find out whether member numbers should be generated
890 # automatically. Should be either "1" or something else.
891 # Defaults to "0", which is interpreted as "no".
893 # if ($cardnumber !~ /\S/ && $autonumber_members) {
894 if ($autonumber_members) {
895 my $dbh = C4::Context->dbh;
896 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
898 # if checkdigit is selected, calculate katipo-style cardnumber.
899 # otherwise, just use the max()
900 # purpose: generate checksum'd member numbers.
901 # We'll assume we just got the max value of digits 2-8 of member #'s
902 # from the database and our job is to increment that by one,
903 # determine the 1st and 9th digits and return the full string.
906 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
910 my $data = $sth->fetchrow_hashref;
911 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
913 if ( !$cardnumber ) { # If DB has no values,
914 $cardnumber = 1000000; # start at 1000000
921 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
923 # read weightings, left to right, 1 char at a time
924 my $temp1 = $weightings[$i];
926 # sequence left to right, 1 char at a time
927 my $temp2 = substr( $cardnumber, $i, 1 );
929 # mult each char 1-7 by its corresponding weighting
930 $sum += $temp1 * $temp2;
933 my $rem = ( $sum % 11 );
934 $rem = 'X' if $rem == 10;
936 $cardnumber = "V$cardnumber$rem";
940 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
941 # better. I'll leave the original in in case it needs to be changed for you
944 "select max(cast(cardnumber as signed)) from borrowers");
946 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
950 my ($result) = $sth->fetchrow;
952 $cardnumber = $result + 1;
958 =head2 findguarantees
960 ($num_children, $children_arrayref) = &findguarantees($parent_borrno);
961 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
962 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
964 C<&findguarantees> takes a borrower number (e.g., that of a patron
965 with children) and looks up the borrowers who are guaranteed by that
966 borrower (i.e., the patron's children).
968 C<&findguarantees> returns two values: an integer giving the number of
969 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
970 of references to hash, which gives the actual results.
976 my ($borrowernumber) = @_;
977 my $dbh = C4::Context->dbh;
980 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
982 $sth->execute($borrowernumber);
985 while ( my $data = $sth->fetchrow_hashref ) {
989 return ( scalar(@dat), \@dat );
994 $guarantor = &findguarantor($borrower_no);
995 $guarantor_cardno = $guarantor->{"cardnumber"};
996 $guarantor_surname = $guarantor->{"surname"};
999 C<&findguarantor> takes a borrower number (presumably that of a child
1000 patron), finds the guarantor for C<$borrower_no> (the child's parent),
1001 and returns the record for the guarantor.
1003 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
1004 from the C<borrowers> database table;
1010 my ($borrowernumber) = @_;
1011 my $dbh = C4::Context->dbh;
1013 $dbh->prepare("select guarantorid from borrowers where borrowernumber=?");
1014 $sth->execute($borrowernumber);
1015 my $data = $sth->fetchrow_hashref;
1017 $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
1018 $sth->execute( $data->{'guarantorid'} );
1019 $data = $sth->fetchrow_hashref;
1024 =item GuarantornameSearch
1026 ($count, $borrowers) = &GuarantornameSearch($env, $searchstring, $type);
1028 Looks up guarantor by name.
1032 BUGFIX 499: C<$type> is now used to determine type of search.
1033 if $type is "simple", search is performed on the first letter of the
1036 C<$searchstring> is a space-separated list of search terms. Each term
1037 must match the beginning a borrower's surname, first name, or other
1040 C<&GuarantornameSearch> returns a two-element list. C<$borrowers> is a
1041 reference-to-array; each element is a reference-to-hash, whose keys
1042 are the fields of the C<borrowers> table in the Koha database.
1043 C<$count> is the number of elements in C<$borrowers>.
1045 return all info from guarantor =>only category_type A
1050 #used by member enquiries from the intranet
1051 #called by guarantor_search.pl
1052 sub GuarantornameSearch {
1053 my ( $env, $searchstring, $orderby, $type ) = @_;
1054 my $dbh = C4::Context->dbh;
1060 if ( $type eq "simple" ) # simple search for one letter only
1063 "Select * from borrowers,categories where borrowers.categorycode=categories.categorycode and category_type='A' and surname like ? order by $orderby";
1064 @bind = ("$searchstring%");
1066 else # advanced search looking in surname, firstname and othernames
1068 @data = split( ' ', $searchstring );
1070 $query = "Select * from borrowers,categories
1071 where ((surname like ? or surname like ?
1072 or firstname like ? or firstname like ?
1073 or othernames like ? or othernames like ?) and borrowers.categorycode=categories.categorycode and category_type='A'
1076 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
1077 "$data[0]%", "% $data[0]%"
1079 for ( my $i = 1 ; $i < $count ; $i++ ) {
1080 $query = $query . " and (" . " surname like ? or surname like ?
1081 or firstname like ? or firstname like ?
1082 or othernames like ? or othernames like ?)";
1084 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
1085 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
1089 $query = $query . ") or cardnumber like ?
1091 push( @bind, $searchstring );
1096 my $sth = $dbh->prepare($query);
1097 $sth->execute(@bind);
1099 my $cnt = $sth->rows;
1100 while ( my $data = $sth->fetchrow_hashref ) {
1101 push( @results, $data );
1106 return ( $cnt, \@results );
1111 ($count, $issues) = &borrissues($borrowernumber);
1113 Looks up what the patron with the given borrowernumber has borrowed.
1115 C<&borrissues> returns a two-element array. C<$issues> is a
1116 reference-to-array, where each element is a reference-to-hash; the
1117 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1118 in the Koha database. C<$count> is the number of elements in
1125 my ($borrowernumber) = @_;
1126 my $dbh = C4::Context->dbh;
1127 my $sth = $dbh->prepare(
1128 "Select * from issues,biblio,items where borrowernumber=?
1129 and items.itemnumber=issues.itemnumber
1130 and items.biblionumber=biblio.biblionumber
1131 and issues.returndate is NULL order by date_due"
1133 $sth->execute($borrowernumber);
1135 while ( my $data = $sth->fetchrow_hashref ) {
1136 push @result, $data;
1139 return ( scalar(@result), \@result );
1144 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1146 Looks up what the patron with the given borrowernumber has borrowed,
1147 and sorts the results.
1149 C<$sortkey> is the name of a field on which to sort the results. This
1150 should be the name of a field in the C<issues>, C<biblio>,
1151 C<biblioitems>, or C<items> table in the Koha database.
1153 C<$limit> is the maximum number of results to return.
1155 C<&allissues> returns a two-element array. C<$issues> is a
1156 reference-to-array, where each element is a reference-to-hash; the
1157 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1158 C<items> tables of the Koha database. C<$count> is the number of
1159 elements in C<$issues>
1165 my ( $borrowernumber, $order, $limit ) = @_;
1167 #FIXME: sanity-check order and limit
1168 my $dbh = C4::Context->dbh;
1171 "Select *,items.timestamp AS itemstimestamp from issues,biblio,items,biblioitems
1172 where borrowernumber=? and
1173 items.biblioitemnumber=biblioitems.biblioitemnumber and
1174 items.itemnumber=issues.itemnumber and
1175 items.biblionumber=biblio.biblionumber order by $order";
1176 if ( $limit != 0 ) {
1177 $query .= " limit $limit";
1181 my $sth = $dbh->prepare($query);
1182 $sth->execute($borrowernumber);
1185 while ( my $data = $sth->fetchrow_hashref ) {
1186 $result[$i] = $data;
1191 # get all issued items for borrowernumber from oldissues table
1192 # large chunk of older issues data put into table oldissues
1193 # to speed up db calls for issuing items
1194 if ( C4::Context->preference("ReadingHistory") ) {
1195 my $query2 = "SELECT * FROM oldissues,biblio,items,biblioitems
1196 WHERE borrowernumber=?
1197 AND items.biblioitemnumber=biblioitems.biblioitemnumber
1198 AND items.itemnumber=oldissues.itemnumber
1199 AND items.biblionumber=biblio.biblionumber
1201 if ( $limit != 0 ) {
1202 $limit = $limit - $count;
1203 $query2 .= " limit $limit";
1206 my $sth2 = $dbh->prepare($query2);
1207 $sth2->execute($borrowernumber);
1209 while ( my $data2 = $sth2->fetchrow_hashref ) {
1210 $result[$i] = $data2;
1217 return ( $i, \@result );
1220 =head2 getboracctrecord
1222 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
1224 Looks up accounting data for the patron with the given borrowernumber.
1228 (FIXME - I'm not at all sure what this is about.)
1230 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
1231 reference-to-array, where each element is a reference-to-hash; the
1232 keys are the fields of the C<accountlines> table in the Koha database.
1233 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1234 total amount outstanding for all of the account lines.
1239 sub getboracctrecord {
1240 my ( $env, $params ) = @_;
1241 my $dbh = C4::Context->dbh;
1244 my $sth = $dbh->prepare(
1245 "Select * from accountlines where
1246 borrowernumber=? order by date desc,timestamp desc"
1249 $sth->execute( $params->{'borrowernumber'} );
1251 while ( my $data = $sth->fetchrow_hashref ) {
1253 #FIXME before reinstating: insecure?
1254 # if ($data->{'itemnumber'} ne ''){
1255 # $query="Select * from items,biblio where items.itemnumber=
1256 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1257 # my $sth2=$dbh->prepare($query);
1259 # my $data2=$sth2->fetchrow_hashref;
1263 $acctlines[$numlines] = $data;
1265 $total += $data->{'amountoutstanding'};
1268 return ( $numlines, \@acctlines, $total );
1271 =head2 GetBorNotifyAcctRecord
1273 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($env, $params,$notifyid);
1275 Looks up accounting data for the patron with the given borrowernumber per file number.
1279 (FIXME - I'm not at all sure what this is about.)
1281 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1282 reference-to-array, where each element is a reference-to-hash; the
1283 keys are the fields of the C<accountlines> table in the Koha database.
1284 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1285 total amount outstanding for all of the account lines.
1289 sub GetBorNotifyAcctRecord {
1290 my ( $env, $params, $notifyid ) = @_;
1291 my $dbh = C4::Context->dbh;
1294 my $query = qq| SELECT *
1296 WHERE borrowernumber=?
1298 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')
1299 AND amountoutstanding != '0'
1300 ORDER BY notify_id,accounttype
1302 my $sth = $dbh->prepare($query);
1304 $sth->execute( $params->{'borrowernumber'}, $notifyid );
1306 while ( my $data = $sth->fetchrow_hashref ) {
1307 $acctlines[$numlines] = $data;
1309 $total += $data->{'amountoutstanding'};
1312 return ( $numlines, \@acctlines, $total );
1315 =head2 checkuniquemember (OUEST-PROVENCE)
1317 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
1319 Checks that a member exists or not in the database.
1321 C<&result> is 1 (=exist) or 0 (=does not exist)
1322 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1323 C<&surname> is the surname
1324 C<&categorycode> is from categorycode table
1325 C<&firstname> is the firstname (only if collectivity=0)
1326 C<&dateofbirth> is the date of birth (only if collectivity=0)
1330 sub checkuniquemember {
1331 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1332 my $dbh = C4::Context->dbh;
1334 if ($collectivity) {
1336 # $request="select count(*) from borrowers where surname=? and categorycode=?";
1338 "select borrowernumber,categorycode from borrowers where surname=? ";
1342 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1344 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1346 my $sth = $dbh->prepare($request);
1347 if ($collectivity) {
1348 $sth->execute( uc($surname) );
1351 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1353 my @data = $sth->fetchrow;
1356 return $data[0], $data[1];
1366 =head2 getzipnamecity (OUEST-PROVENCE)
1368 take all info from table city for the fields city and zip
1369 check for the name and the zip code of the city selected
1373 sub getzipnamecity {
1375 my $dbh = C4::Context->dbh;
1378 "select city_name,city_zipcode from cities where cityid=? ");
1379 $sth->execute($cityid);
1380 my @data = $sth->fetchrow;
1381 return $data[0], $data[1];
1384 =head2 updatechildguarantor (OUEST-PROVENCE)
1386 check for title,firstname,surname,adress,zip code and city from guarantor to
1393 sub getguarantordata {
1394 my ($borrowerid) = @_;
1395 my $dbh = C4::Context->dbh;
1398 "Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro,fax from borrowers where borrowernumber =? "
1400 $sth->execute($borrowerid);
1401 my $guarantor_data = $sth->fetchrow_hashref;
1403 return $guarantor_data;
1406 =head2 getdcity (OUEST-PROVENCE)
1408 recover cityid with city_name condition
1413 my ($city_name) = @_;
1414 my $dbh = C4::Context->dbh;
1415 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1416 $sth->execute($city_name);
1417 my $data = $sth->fetchrow;
1421 =head2 getcategorytype (OUEST-PROVENCE)
1423 check for the category_type with categorycode
1424 and return the category_type
1428 sub getcategorytype {
1429 my ($categorycode) = @_;
1430 my $dbh = C4::Context->dbh;
1433 "Select category_type,description from categories where categorycode=? "
1435 $sth->execute($categorycode);
1436 my ( $category_type, $description ) = $sth->fetchrow;
1437 return $category_type, $description;
1440 sub calcexpirydate {
1441 my ( $categorycode, $dateenrolled ) = @_;
1442 my $dbh = C4::Context->dbh;
1445 "select enrolmentperiod from categories where categorycode=?");
1446 $sth->execute($categorycode);
1447 my ($enrolmentperiod) = $sth->fetchrow;
1448 $enrolmentperiod = 12 unless ($enrolmentperiod);
1449 # warn "Avant format_date_in_iso :".$dateenrolled;
1450 # $dateenrolled=format_date_in_iso($dateenrolled);
1451 # warn "Apres format_date_in_iso :".$dateenrolled;
1452 my @date=split /-/,format_date_in_iso($dateenrolled);
1453 @date=Add_Delta_YM($date[0],$date[1],$date[2],0,$enrolmentperiod);
1454 return sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
1457 =head2 checkuserpassword (OUEST-PROVENCE)
1459 check for the password and login are not used
1460 return the number of record
1461 0=> NOT USED 1=> USED
1465 sub checkuserpassword {
1466 my ( $borrowernumber, $userid, $password ) = @_;
1467 $password = md5_base64($password);
1468 my $dbh = C4::Context->dbh;
1471 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1473 $sth->execute( $borrowernumber, $userid, $password );
1474 my $number_rows = $sth->fetchrow;
1475 return $number_rows;
1479 =head2 GetborCatFromCatType
1481 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1483 Looks up the different types of borrowers in the database. Returns two
1484 elements: a reference-to-array, which lists the borrower category
1485 codes, and a reference-to-hash, which maps the borrower category codes
1486 to category descriptions.
1491 sub GetborCatFromCatType {
1492 my ( $category_type, $action ) = @_;
1493 my $dbh = C4::Context->dbh;
1494 my $request = qq| SELECT categorycode,description
1497 ORDER BY categorycode|;
1498 my $sth = $dbh->prepare($request);
1500 $sth->execute($category_type);
1509 while ( my $data = $sth->fetchrow_hashref ) {
1510 push @codes, $data->{'categorycode'};
1511 $labels{ $data->{'categorycode'} } = $data->{'description'};
1514 return ( \@codes, \%labels );
1517 =head2 getborrowercategory
1519 $description,$dateofbirthrequired,$upperagelimit,$category_type = &getborrowercategory($categorycode);
1521 Given the borrower's category code, the function returns the corresponding
1522 description , dateofbirthrequired , upperagelimit and category type for a comprehensive information display.
1526 sub getborrowercategory {
1528 my $dbh = C4::Context->dbh;
1531 "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?"
1533 $sth->execute($catcode);
1534 my ( $description, $dateofbirthrequired, $upperagelimit, $category_type ) =
1537 return ( $description, $dateofbirthrequired, $upperagelimit,
1539 } # sub getborrowercategory
1541 =head2 ethnicitycategories
1543 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1545 Looks up the different ethnic types in the database. Returns two
1546 elements: a reference-to-array, which lists the ethnicity codes, and a
1547 reference-to-hash, which maps the ethnicity codes to ethnicity
1554 sub ethnicitycategories {
1555 my $dbh = C4::Context->dbh;
1556 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1560 while ( my $data = $sth->fetchrow_hashref ) {
1561 push @codes, $data->{'code'};
1562 $labels{ $data->{'code'} } = $data->{'name'};
1565 return ( \@codes, \%labels );
1570 $ethn_name = &fixEthnicity($ethn_code);
1572 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1573 corresponding descriptive name from the C<ethnicity> table in the
1574 Koha database ("European" or "Pacific Islander").
1581 my $ethnicity = shift;
1582 return unless $ethnicity;
1583 my $dbh = C4::Context->dbh;
1584 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1585 $sth->execute($ethnicity);
1586 my $data = $sth->fetchrow_hashref;
1588 return $data->{'name'};
1589 } # sub fixEthnicity
1593 $dateofbirth,$date = &get_age($date);
1595 this function return the borrowers age with the value of dateofbirth
1601 my ( $date, $date_ref ) = @_;
1603 if ( not defined $date_ref ) {
1604 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1607 my ( $year1, $month1, $day1 ) = split /-/, $date;
1608 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1610 my $age = $year2 - $year1;
1611 if ( $month1 . $day1 > $month2 . $day2 ) {
1618 =head2 get_institutions
1619 $insitutions = get_institutions();
1621 Just returns a list of all the borrowers of type I, borrownumber and name
1626 sub get_institutions {
1627 my $dbh = C4::Context->dbh();
1630 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1634 while ( my $data = $sth->fetchrow_hashref() ) {
1635 $orgs{ $data->{'borrowernumber'} } = $data;
1640 } # sub get_institutions
1642 =head2 add_member_orgs
1644 add_member_orgs($borrowernumber,$borrowernumbers);
1646 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1651 sub add_member_orgs {
1652 my ( $borrowernumber, $otherborrowers ) = @_;
1653 my $dbh = C4::Context->dbh();
1655 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1656 my $sth = $dbh->prepare($query);
1657 foreach my $otherborrowernumber (@$otherborrowers) {
1658 $sth->execute( $borrowernumber, $otherborrowernumber );
1662 } # sub add_member_orgs
1664 =head2 GetBorrowersFromSurname
1668 \@resutlts = GetBorrowersFromSurname($surname)
1669 this function get the list of borrower names like $surname.
1671 the table of results in @results
1677 sub GetBorrowersFromSurname {
1678 my ($searchstring) = @_;
1679 my $dbh = C4::Context->dbh;
1680 $searchstring =~ s/\'/\\\'/g;
1681 my @data = split( ' ', $searchstring );
1684 SELECT surname,firstname
1686 WHERE (surname like ?)
1689 my $sth = $dbh->prepare($query);
1690 $sth->execute("$data[0]%");
1694 while ( my $data = $sth->fetchrow_hashref ) {
1695 push( @results, $data );
1699 return ( $count, \@results );
1702 =head2 citycaracteristiques (OUEST-PROVENCE)
1704 ($id_cityarrayref, $city_hashref) = &citycaracteristic();
1706 Looks up the different city and zip in the database. Returns two
1707 elements: a reference-to-array, which lists the zip city
1708 codes, and a reference-to-hash, which maps the name of the city.
1709 WHERE =>OUEST PROVENCE OR EXTERIEUR
1715 #my ($type_city) = @_;
1716 my $dbh = C4::Context->dbh;
1717 my $query = qq|SELECT cityid,city_name
1719 ORDER BY city_name|;
1720 my $sth = $dbh->prepare($query);
1722 #$sth->execute($type_city);
1727 # insert empty value to create a empty choice in cgi popup
1729 while ( my $data = $sth->fetchrow_hashref ) {
1731 push @id, $data->{'cityid'};
1732 $city{ $data->{'cityid'} } = $data->{'city_name'};
1735 #test to know if the table contain some records if no the function return nothing
1743 return ( \@id, \%city );
1747 =head2 GetSortDetails (OUEST-PROVENCE)
1749 ($lib) = &GetSortDetails($category,$sortvalue);
1751 Returns the authorized value details
1752 C<&$lib>return value of authorized value details
1753 C<&$sortvalue>this is the value of authorized value
1754 C<&$category>this is the value of authorized value category
1758 sub GetSortDetails {
1759 my ( $category, $sortvalue ) = @_;
1760 my $dbh = C4::Context->dbh;
1761 my $query = qq|SELECT lib
1762 FROM authorised_values
1764 AND authorised_value=? |;
1765 my $sth = $dbh->prepare($query);
1766 $sth->execute( $category, $sortvalue );
1767 my $lib = $sth->fetchrow;
1771 =head2 DeleteBorrower
1773 () = &DeleteBorrower($member);
1775 delete all data fo borrowers and add record to deletedborrowers table
1776 C<&$member>this is the borrowernumber
1780 sub DeleteBorrower {
1782 my $dbh = C4::Context->dbh;
1784 $query = qq|SELECT *
1786 WHERE borrowernumber=?|;
1787 my $sth = $dbh->prepare($query);
1788 $sth->execute($member);
1789 my @data = $sth->fetchrow_array;
1792 $dbh->prepare( "Insert into deletedborrowers values ("
1793 . ( "?," x ( scalar(@data) - 1 ) )
1795 $sth->execute(@data);
1799 WHERE borrowernumber=?|;
1800 $sth = $dbh->prepare($query);
1801 $sth->execute($member);
1805 WHERE borrowernumber=?|;
1806 $sth = $dbh->prepare($query);
1807 $sth->execute($member);
1810 # logging to action_log
1811 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$member,"")
1812 if C4::Context->preference("BorrowersLog");
1815 =head2 DelBorrowerCompletly
1817 DelBorrowerCompletly($borrowernumber);
1819 This function remove directly a borrower whitout writing it on deleteborrower.
1823 sub DelBorrowerCompletly {
1824 my $dbh = C4::Context->dbh;
1825 my $borrowernumber = shift;
1826 return unless $borrowernumber; # date is mandatory.
1830 WHERE borrowernumber = ?
1832 my $sth = $dbh->prepare($query);
1833 $sth->execute($borrowernumber);
1837 =head2 member_reregistration (OUEST-PROVENCE)
1839 automatic reregistration in borrowers table
1844 sub GetMembeReregistration {
1845 my ( $categorycode, $borrowerid ) = @_;
1846 my $dbh = C4::Context->dbh;
1847 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
1850 $year = $year + 1900;
1851 if ( $mon < '10' ) {
1854 if ( $mday < '10' ) {
1855 $mday = "0" . $mday;
1857 my $today = sprintf("%04d-%02d-%02d",$year,$mon,$mday);
1858 my $dateexpiry = calcexpirydate( $categorycode, $today );
1859 my $query = qq| UPDATE borrowers
1860 SET dateexpiry='$dateexpiry'
1861 WHERE borrowernumber='$borrowerid'|;
1862 my $sth = $dbh->prepare($query);
1868 =head2 GetRoadTypes (OUEST-PROVENCE)
1870 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1872 Looks up the different road type . Returns two
1873 elements: a reference-to-array, which lists the id_roadtype
1874 codes, and a reference-to-hash, which maps the road type of the road .
1880 my $dbh = C4::Context->dbh;
1881 my $query = qq|SELECT roadtypeid,road_type
1883 ORDER BY road_type|;
1884 my $sth = $dbh->prepare($query);
1889 # insert empty value to create a empty choice in cgi popup
1891 while ( my $data = $sth->fetchrow_hashref ) {
1893 push @id, $data->{'roadtypeid'};
1894 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1897 #test to know if the table contain some records if no the function return nothing
1905 return ( \@id, \%roadtype );
1911 =head2 GetBorrowersTitles (OUEST-PROVENCE)
1913 ($borrowertitle)= &GetBorrowersTitles();
1915 Looks up the different title . Returns array with all borrowers title
1919 sub GetBorrowersTitles {
1920 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1921 unshift( @borrowerTitle, "" );
1922 return ( \@borrowerTitle);
1927 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1929 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1931 Returns the description of roadtype
1932 C<&$roadtype>return description of road type
1933 C<&$roadtypeid>this is the value of roadtype s
1937 sub GetRoadTypeDetails {
1938 my ($roadtypeid) = @_;
1939 my $dbh = C4::Context->dbh;
1940 my $query = qq|SELECT road_type
1942 WHERE roadtypeid=?|;
1943 my $sth = $dbh->prepare($query);
1944 $sth->execute($roadtypeid);
1945 my $roadtype = $sth->fetchrow;
1949 =head2 GetBorrowersWhoHaveNotBorrowedSince
1951 &GetBorrowersWhoHaveNotBorrowedSince($date)
1953 this function get all borrowers who haven't borrowed since the date given on input arg.
1957 sub GetBorrowersWhoHaveNotBorrowedSince {
1959 return unless $date; # date is mandatory.
1960 my $dbh = C4::Context->dbh;
1962 SELECT borrowers.borrowernumber,max(timestamp)
1964 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1965 WHERE issues.borrowernumber IS NOT NULL
1966 GROUP BY borrowers.borrowernumber
1968 my $sth = $dbh->prepare($query);
1972 while ( my $data = $sth->fetchrow_hashref ) {
1973 push @results, $data;
1978 =head2 GetBorrowersWhoHaveNeverBorrowed
1980 $results = &GetBorrowersWhoHaveNeverBorrowed
1982 this function get all borrowers who have never borrowed.
1984 I<$result> is a ref to an array which all elements are a hasref.
1988 sub GetBorrowersWhoHaveNeverBorrowed {
1989 my $dbh = C4::Context->dbh;
1991 SELECT borrowers.borrowernumber,max(timestamp)
1993 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1994 WHERE issues.borrowernumber IS NULL
1996 my $sth = $dbh->prepare($query);
1999 while ( my $data = $sth->fetchrow_hashref ) {
2000 push @results, $data;
2005 =head2 GetBorrowersWithIssuesHistoryOlderThan
2007 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
2009 this function get all borrowers who has an issue history older than I<$date> given on input arg.
2011 I<$result> is a ref to an array which all elements are a hashref.
2012 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
2016 sub GetBorrowersWithIssuesHistoryOlderThan {
2017 my $dbh = C4::Context->dbh;
2019 return unless $date; # date is mandatory.
2021 SELECT count(borrowernumber) as n,borrowernumber
2023 WHERE returndate < ?
2024 AND borrowernumber IS NOT NULL
2025 GROUP BY borrowernumber
2027 my $sth = $dbh->prepare($query);
2028 $sth->execute($date);
2031 while ( my $data = $sth->fetchrow_hashref ) {
2032 push @results, $data;
2037 END { } # module clean-up code here (global destructor)