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($searchstring, $type);
84 Looks up patrons (borrowers) by name.
86 BUGFIX 499: C<$type> is now used to determine type of search.
87 if $type is "simple", search is performed on the first letter of the
90 C<$searchstring> is a space-separated list of search terms. Each term
91 must match the beginning a borrower's surname, first name, or other
94 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
95 reference-to-array; each element is a reference-to-hash, whose keys
96 are the fields of the C<borrowers> table in the Koha database.
97 C<$count> is the number of elements in C<$borrowers>.
102 #used by member enquiries from the intranet
105 my ($searchstring, $orderby, $type ) = @_;
106 my $dbh = C4::Context->dbh;
112 if ( $type eq "simple" ) # simple search for one letter only
115 "SELECT * FROM borrowers
116 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
117 WHERE surname LIKE ? ORDER BY $orderby";
118 @bind = ("$searchstring%");
120 else # advanced search looking in surname, firstname and othernames
122 @data = split( ' ', $searchstring );
124 $query = "SELECT * FROM borrowers
125 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
126 WHERE ((surname LIKE ? OR surname LIKE ?
127 OR firstname LIKE ? OR firstname LIKE ?
128 OR othernames LIKE ? OR othernames LIKE ?)
131 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
132 "$data[0]%", "% $data[0]%"
134 for ( my $i = 1 ; $i < $count ; $i++ ) {
135 $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
136 OR firstname LIKE ? OR firstname LIKE ?
137 OR othernames LIKE ? OR othernames LIKE ?)";
139 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
140 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
144 $query = $query . ") OR cardnumber LIKE ?
146 push( @bind, $searchstring );
151 my $sth = $dbh->prepare($query);
153 # warn "Q $orderby : $query";
154 $sth->execute(@bind);
156 my $cnt = $sth->rows;
157 while ( my $data = $sth->fetchrow_hashref ) {
158 push( @results, $data );
163 return ( $cnt, \@results );
166 =head3 GetFlagsAndBranchFromBorrower
170 ($flags, $homebranch) = GetFlagsAndBranchFromBorrower($loggedinuser);
172 this function read on the database to get flags and homebranch for a user
176 it returns the $flags & the homebranch in scalar context.
182 sub GetFlagsAndBranchFromBorrower {
183 my $loggedinuser = @_;
184 my $dbh = C4::Context->dbh;
186 SELECT flags, branchcode
188 WHERE borrowernumber = ?
190 my $sth = $dbh->prepare($query);
191 $sth->execute($loggedinuser);
193 return $sth->fetchrow;
198 $borrower = &GetMember($cardnumber, $borrowernumber);
200 Looks up information about a patron (borrower) by either card number
201 or borrower number. If $borrowernumber is specified, C<&borrdata>
202 searches by borrower number; otherwise, it searches by card number.
204 C<&GetMember> returns a reference-to-hash whose keys are the fields of
205 the C<borrowers> table in the Koha database.
210 my ( $cardnumber, $borrowernumber ) = @_;
211 $cardnumber = uc $cardnumber;
212 my $dbh = C4::Context->dbh;
214 if ( $borrowernumber eq '' ) {
215 $sth = $dbh->prepare("Select * from borrowers where cardnumber=?");
216 $sth->execute($cardnumber);
219 $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
220 $sth->execute($borrowernumber);
222 my $data = $sth->fetchrow_hashref;
227 else { # try with firstname
230 $dbh->prepare("select * from borrowers where firstname=?");
231 $sth->execute($cardnumber);
232 my $data = $sth->fetchrow_hashref;
240 =head2 GetMemberDetails
242 ($borrower, $flags) = &GetMemberDetails($borrowernumber, $cardnumber);
244 Looks up a patron and returns information about him or her. If
245 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
246 up the borrower by number; otherwise, it looks up the borrower by card
249 C<$borrower> is a reference-to-hash whose keys are the fields of the
250 borrowers table in the Koha database. In addition,
251 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
252 about the patron. Its keys act as flags :
254 if $borrower->{flags}->{LOST} {
255 # Patron's card was reported lost
258 Each flag has a C<message> key, giving a human-readable explanation of
259 the flag. If the state of a flag means that the patron should not be
260 allowed to borrow any more books, then it will have a C<noissues> key
263 The possible flags are:
269 =item Shows the patron's credit or debt, if any.
277 =item (Gone, no address.) Set if the patron has left without giving a
286 =item Set if the patron's card has been reported as lost.
294 =item Set if the patron has been debarred.
302 =item Any additional notes about the patron.
310 =item Set if the patron has overdue items. This flag has several keys:
312 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
313 overdue items. Its elements are references-to-hash, each describing an
314 overdue item. The keys are selected fields from the issues, biblio,
315 biblioitems, and items tables of the Koha database.
317 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
318 the overdue items, one per line.
326 =item Set if any items that the patron has reserved are available.
328 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
329 available items. Each element is a reference-to-hash whose keys are
330 fields from the reserves table of the Koha database.
336 sub GetMemberDetails {
337 my ( $borrowernumber, $cardnumber ) = @_;
338 my $dbh = C4::Context->dbh;
341 if ($borrowernumber) {
342 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
343 $sth->execute($borrowernumber);
345 elsif ($cardnumber) {
346 $sth = $dbh->prepare("select * from borrowers where cardnumber=?");
347 $sth->execute($cardnumber);
352 my $borrower = $sth->fetchrow_hashref;
353 my $amount = C4::Accounts::checkaccount( $borrowernumber, $dbh );
354 $borrower->{'amountoutstanding'} = $amount;
355 my $flags = patronflags( $borrower, $dbh );
358 $sth = $dbh->prepare("select bit,flag from userflags");
360 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
361 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
362 $accessflagshash->{$flag} = 1;
366 $borrower->{'flags'} = $flags;
367 $borrower->{'authflags'} = $accessflagshash;
369 # find out how long the membership lasts
372 "select enrolmentperiod from categories where categorycode = ?");
373 $sth->execute( $borrower->{'categorycode'} );
374 my $enrolment = $sth->fetchrow;
375 $borrower->{'enrolmentperiod'} = $enrolment;
376 return ($borrower); #, $flags, $accessflagshash);
383 NOTE!: If you change this function, be sure to update the POD for
386 $flags = &patronflags($patron, $dbh);
389 {message} Message showing patron's credit or debt
390 {noissues} Set if patron owes >$5.00
391 {GNA} Set if patron gone w/o address
392 {message} "Borrower has no valid address"
394 {LOST} Set if patron's card reported lost
395 {message} Message to this effect
397 {DBARRED} Set is patron is debarred
398 {message} Message to this effect
400 {NOTES} Set if patron has notes
401 {message} Notes about patron
402 {ODUES} Set if patron has overdue books
404 {itemlist} ref-to-array: list of overdue books
405 {itemlisttext} Text list of overdue items
406 {WAITING} Set if there are items available that the
408 {message} Message to this effect
409 {itemlist} ref-to-array: list of available items
415 my ( $patroninformation, $dbh ) = @_;
417 C4::Accounts::checkaccount( $patroninformation->{'borrowernumber'}, $dbh );
420 my $noissuescharge = C4::Context->preference("noissuescharge");
421 $flaginfo{'message'} = sprintf "Patron owes \$%.02f", $amount;
422 if ( $amount > $noissuescharge ) {
423 $flaginfo{'noissues'} = 1;
425 $flags{'CHARGES'} = \%flaginfo;
427 elsif ( $amount < 0 ) {
429 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
430 $flags{'CHARGES'} = \%flaginfo;
432 if ( $patroninformation->{'gonenoaddress'}
433 && $patroninformation->{'gonenoaddress'} == 1 )
436 $flaginfo{'message'} = 'Borrower has no valid address.';
437 $flaginfo{'noissues'} = 1;
438 $flags{'GNA'} = \%flaginfo;
440 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
442 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
443 $flaginfo{'noissues'} = 1;
444 $flags{'LOST'} = \%flaginfo;
446 if ( $patroninformation->{'debarred'}
447 && $patroninformation->{'debarred'} == 1 )
450 $flaginfo{'message'} = 'Borrower is Debarred.';
451 $flaginfo{'noissues'} = 1;
452 $flags{'DBARRED'} = \%flaginfo;
454 if ( $patroninformation->{'borrowernotes'}
455 && $patroninformation->{'borrowernotes'} )
458 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
459 $flags{'NOTES'} = \%flaginfo;
461 my ( $odues, $itemsoverdue ) =
462 checkoverdues( $patroninformation->{'borrowernumber'}, $dbh );
465 $flaginfo{'message'} = "Yes";
466 $flaginfo{'itemlist'} = $itemsoverdue;
467 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
470 $flaginfo{'itemlisttext'} .=
471 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
473 $flags{'ODUES'} = \%flaginfo;
476 C4::Reserves2::GetWaitingReserves( $patroninformation->{'borrowernumber'} );
477 my $nowaiting = scalar @$itemswaiting;
478 if ( $nowaiting > 0 ) {
480 $flaginfo{'message'} = "Reserved items available";
481 $flaginfo{'itemlist'} = $itemswaiting;
482 $flags{'WAITING'} = \%flaginfo;
490 $borrower = &borrdata($cardnumber, $borrowernumber);
492 Looks up information about a patron (borrower) by either card number
493 or borrower number. If $borrowernumber is specified, C<&borrdata>
494 searches by borrower number; otherwise, it searches by card number.
496 C<&borrdata> returns a reference-to-hash whose keys are the fields of
497 the C<borrowers> table in the Koha database.
503 my ( $cardnumber, $borrowernumber ) = @_;
504 $cardnumber = uc $cardnumber;
505 my $dbh = C4::Context->dbh;
507 if ( $borrowernumber eq '' ) {
510 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?"
512 $sth->execute($cardnumber);
517 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?"
519 $sth->execute($borrowernumber);
521 my $data = $sth->fetchrow_hashref;
527 elsif ($cardnumber) { # try with firstname
530 "Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname=?"
532 $sth->execute($cardnumber);
533 my $data = $sth->fetchrow_hashref;
544 ($borrowed, $due, $fine) = &borrdata2($borrowernumber);
546 Returns aggregate data about items borrowed by the patron with the
547 given borrowernumber.
549 C<&borrdata2> returns a three-element array. C<$borrowed> is the
550 number of books the patron currently has borrowed. C<$due> is the
551 number of overdue items the patron currently has borrowed. C<$fine> is
552 the total fine currently due by the borrower.
558 my ( $borrowernumber ) = @_;
559 my $dbh = C4::Context->dbh;
561 "Select count(*) from issues where borrowernumber='$borrowernumber' and
565 my $sth = $dbh->prepare($query);
567 my $data = $sth->fetchrow_hashref;
569 $sth = $dbh->prepare(
570 "Select count(*) from issues where
571 borrowernumber='$borrowernumber' and date_due < now() and returndate is NULL"
574 my $data2 = $sth->fetchrow_hashref;
576 $sth = $dbh->prepare(
577 "Select sum(amountoutstanding) from accountlines where
578 borrowernumber='$borrowernumber'"
581 my $data3 = $sth->fetchrow_hashref;
584 return ( $data2->{'count(*)'}, $data->{'count(*)'},
585 $data3->{'sum(amountoutstanding)'} );
590 my $dbh = C4::Context->dbh;
591 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
592 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
593 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
595 # warn "num user".$data{'borrowernumber'};
598 $data{'userid'} = '' if ( $data{'password'} eq '' );
600 # test to know if u must update or not the borrower password
601 if ( $data{'password'} eq '****' ) {
603 $query = "UPDATE borrowers SET
604 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
605 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
606 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
607 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
608 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
609 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
610 ethnotes = ?,sex = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
611 WHERE borrowernumber=$data{'borrowernumber'}";
612 $sth = $dbh->prepare($query);
614 $data{'cardnumber'}, $data{'surname'},
615 $data{'firstname'}, $data{'title'},
616 $data{'othernames'}, $data{'initials'},
617 $data{'streetnumber'}, $data{'streettype'},
618 $data{'address'}, $data{'address2'},
619 $data{'city'}, $data{'zipcode'},
620 $data{'email'}, $data{'phone'},
621 $data{'mobile'}, $data{'fax'},
622 $data{'emailpro'}, $data{'phonepro'},
623 $data{'B_streetnumber'}, $data{'B_streettype'},
624 $data{'B_address'}, $data{'B_city'},
625 $data{'B_zipcode'}, $data{'B_email'},
626 $data{'B_phone'}, $data{'dateofbirth'},
627 $data{'branchcode'}, $data{'categorycode'},
628 $data{'dateenrolled'}, $data{'dateexpiry'},
629 $data{'gonenoaddress'}, $data{'lost'},
630 $data{'debarred'}, $data{'contactname'},
631 $data{'contactfirstname'}, $data{'contacttitle'},
632 $data{'guarantorid'}, $data{'borrowernotes'},
633 $data{'relationship'}, $data{'ethnicity'},
634 $data{'ethnotes'}, $data{'sex'},
635 $data{'userid'}, $data{'opacnote'},
636 $data{'contactnote'}, $data{'sort1'},
642 ( $data{'password'} = md5_base64( $data{'password'} ) )
643 if ( $data{'password'} ne '' );
644 $query = "UPDATE borrowers SET
645 cardnumber = ?,surname = ?,firstname = ?,title = ?,othernames = ?,initials = ?,
646 streetnumber = ?,streettype = ?,address = ?,address2 = ?,city = ?,zipcode = ?,
647 email = ?,phone = ?,mobile = ?,fax = ?,emailpro = ?,phonepro = ?,B_streetnumber = ?,
648 B_streettype = ?,B_address = ?,B_city = ?,B_zipcode = ?,B_email = ?,B_phone = ?,dateofbirth = ?,branchcode = ?,
649 categorycode = ?,dateenrolled = ?,dateexpiry = ?,gonenoaddress = ?,lost = ?,debarred = ?,contactname = ?,
650 contactfirstname = ?,contacttitle = ?,guarantorid = ?,borrowernotes = ?,relationship = ?,ethnicity = ?,
651 ethnotes = ?,sex = ?,password = ?,userid = ?,opacnote = ?,contactnote = ?,sort1 = ?,sort2 = ?
652 WHERE borrowernumber=$data{'borrowernumber'}";
653 $sth = $dbh->prepare($query);
655 $data{'cardnumber'}, $data{'surname'},
656 $data{'firstname'}, $data{'title'},
657 $data{'othernames'}, $data{'initials'},
658 $data{'streetnumber'}, $data{'streettype'},
659 $data{'address'}, $data{'address2'},
660 $data{'city'}, $data{'zipcode'},
661 $data{'email'}, $data{'phone'},
662 $data{'mobile'}, $data{'fax'},
663 $data{'emailpro'}, $data{'phonepro'},
664 $data{'B_streetnumber'}, $data{'B_streettype'},
665 $data{'B_address'}, $data{'B_city'},
666 $data{'B_zipcode'}, $data{'B_email'},
667 $data{'B_phone'}, $data{'dateofbirth'},
668 $data{'branchcode'}, $data{'categorycode'},
669 $data{'dateenrolled'}, $data{'dateexpiry'},
670 $data{'gonenoaddress'}, $data{'lost'},
671 $data{'debarred'}, $data{'contactname'},
672 $data{'contactfirstname'}, $data{'contacttitle'},
673 $data{'guarantorid'}, $data{'borrowernotes'},
674 $data{'relationship'}, $data{'ethnicity'},
675 $data{'ethnotes'}, $data{'sex'},
676 $data{'password'}, $data{'userid'},
677 $data{'opacnote'}, $data{'contactnote'},
678 $data{'sort1'}, $data{'sort2'}
683 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
684 # so when we update information for an adult we should check for guarantees and update the relevant part
685 # of their records, ie addresses and phone numbers
686 my ( $category_type, undef ) = getcategorytype( $data{'category_type'} );
687 if ( $category_type eq 'A' ) {
689 # is adult check guarantees;
690 updateguarantees(%data);
693 &logaction(C4::Context->userenv->{'number'},"MEMBERS","MODIFY",$data{'borrowernumber'},"")
694 if C4::Context->preference("BorrowersLog");
699 my $dbh = C4::Context->dbh;
700 $data{'userid'} = '' unless $data{'password'};
701 $data{'password'} = md5_base64( $data{'password'} ) if $data{'password'};
702 $data{'dateofbirth'} = format_date_in_iso( $data{'dateofbirth'} );
703 $data{'dateenrolled'} = format_date_in_iso( $data{'dateenrolled'} );
704 $data{'dateexpiry'} = format_date_in_iso( $data{'dateexpiry'} );
706 "insert into borrowers set cardnumber="
707 . $dbh->quote( $data{'cardnumber'} )
709 . $dbh->quote( $data{'surname'} )
711 . $dbh->quote( $data{'firstname'} )
713 . $dbh->quote( $data{'title'} )
715 . $dbh->quote( $data{'othernames'} )
717 . $dbh->quote( $data{'initials'} )
719 . $dbh->quote( $data{'streetnumber'} )
721 . $dbh->quote( $data{'streettype'} )
723 . $dbh->quote( $data{'address'} )
725 . $dbh->quote( $data{'address2'} )
727 . $dbh->quote( $data{'zipcode'} )
729 . $dbh->quote( $data{'city'} )
731 . $dbh->quote( $data{'phone'} )
733 . $dbh->quote( $data{'email'} )
735 . $dbh->quote( $data{'mobile'} )
737 . $dbh->quote( $data{'phonepro'} )
739 . $dbh->quote( $data{'opacnote'} )
741 . $dbh->quote( $data{'guarantorid'} )
743 . $dbh->quote( $data{'dateofbirth'} )
745 . $dbh->quote( $data{'branchcode'} )
747 . $dbh->quote( $data{'categorycode'} )
749 . $dbh->quote( $data{'dateenrolled'} )
751 . $dbh->quote( $data{'contactname'} )
753 . $dbh->quote( $data{'borrowernotes'} )
755 . $dbh->quote( $data{'dateexpiry'} )
757 . $dbh->quote( $data{'contactnote'} )
759 . $dbh->quote( $data{'B_address'} )
761 . $dbh->quote( $data{'B_zipcode'} )
763 . $dbh->quote( $data{'B_city'} )
765 . $dbh->quote( $data{'B_phone'} )
767 . $dbh->quote( $data{'B_email'}, )
769 . $dbh->quote( $data{'password'} )
771 . $dbh->quote( $data{'userid'} )
773 . $dbh->quote( $data{'sort1'} )
775 . $dbh->quote( $data{'sort2'} )
777 . $dbh->quote( $data{'contacttitle'} )
779 . $dbh->quote( $data{'emailpro'} )
780 . ",contactfirstname="
781 . $dbh->quote( $data{'contactfirstname'} ) . ",sex="
782 . $dbh->quote( $data{'sex'} ) . ",fax="
783 . $dbh->quote( $data{'fax'} )
785 . $dbh->quote( $data{'relationship'} )
787 . $dbh->quote( $data{'B_streetnumber'} )
789 . $dbh->quote( $data{'B_streettype'} )
791 . $dbh->quote( $data{'gonenoaddress'} )
793 . $dbh->quote( $data{'lost'} )
795 . $dbh->quote( $data{'debarred'} )
797 . $dbh->quote( $data{'ethnicity'} )
799 . $dbh->quote( $data{'ethnotes'} );
801 my $sth = $dbh->prepare($query);
804 $data{'borrowernumber'} = $dbh->{'mysql_insertid'};
806 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CREATE",$data{'borrowernumber'},"")
807 if C4::Context->preference("BorrowersLog");
809 return $data{'borrowernumber'};
813 my ( $uid, $member, $digest ) = @_;
814 my $dbh = C4::Context->dbh;
816 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
817 #Then we need to tell the user and have them create a new one.
820 "select * from borrowers where userid=? and borrowernumber != ?");
821 $sth->execute( $uid, $member );
822 if ( ( $uid ne '' ) && ( $sth->fetchrow ) ) {
827 #Everything is good so we can update the information.
830 "update borrowers set userid=?, password=? where borrowernumber=?");
831 $sth->execute( $uid, $digest, $member );
835 &logaction(C4::Context->userenv->{'number'},"MEMBERS","CHANGE PASS",$member,"")
836 if C4::Context->preference("BorrowersLog");
839 sub getmemberfromuserid {
841 my $dbh = C4::Context->dbh;
842 my $sth = $dbh->prepare("select * from borrowers where userid=?");
843 $sth->execute($userid);
844 return $sth->fetchrow_hashref;
847 sub updateguarantees {
849 my $dbh = C4::Context->dbh;
850 my ( $count, $guarantees ) = findguarantees( $data{'borrowernumber'} );
851 for ( my $i = 0 ; $i < $count ; $i++ ) {
854 # It looks like the $i is only being returned to handle walking through
855 # the array, which is probably better done as a foreach loop.
857 my $guaquery = qq|UPDATE borrowers
858 SET address='$data{'address'}',fax='$data{'fax'}',
859 B_city='$data{'B_city'}',mobile='$data{'mobile'}',city='$data{'city'}',phone='$data{'phone'}'
860 WHERE borrowernumber='$guarantees->[$i]->{'borrowernumber'}'
862 my $sth3 = $dbh->prepare($guaquery);
868 =item fixup_cardnumber
870 Warning: The caller is responsible for locking the members table in write
871 mode, to avoid database corruption.
875 use vars qw( @weightings );
876 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
878 sub fixup_cardnumber ($) {
879 my ($cardnumber) = @_;
880 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
881 $autonumber_members = 0 unless defined $autonumber_members;
883 # Find out whether member numbers should be generated
884 # automatically. Should be either "1" or something else.
885 # Defaults to "0", which is interpreted as "no".
887 # if ($cardnumber !~ /\S/ && $autonumber_members) {
888 if ($autonumber_members) {
889 my $dbh = C4::Context->dbh;
890 if ( C4::Context->preference('checkdigit') eq 'katipo' ) {
892 # if checkdigit is selected, calculate katipo-style cardnumber.
893 # otherwise, just use the max()
894 # purpose: generate checksum'd member numbers.
895 # We'll assume we just got the max value of digits 2-8 of member #'s
896 # from the database and our job is to increment that by one,
897 # determine the 1st and 9th digits and return the full string.
900 "select max(substring(borrowers.cardnumber,2,7)) from borrowers"
904 my $data = $sth->fetchrow_hashref;
905 $cardnumber = $data->{'max(substring(borrowers.cardnumber,2,7))'};
907 if ( !$cardnumber ) { # If DB has no values,
908 $cardnumber = 1000000; # start at 1000000
915 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
917 # read weightings, left to right, 1 char at a time
918 my $temp1 = $weightings[$i];
920 # sequence left to right, 1 char at a time
921 my $temp2 = substr( $cardnumber, $i, 1 );
923 # mult each char 1-7 by its corresponding weighting
924 $sum += $temp1 * $temp2;
927 my $rem = ( $sum % 11 );
928 $rem = 'X' if $rem == 10;
930 $cardnumber = "V$cardnumber$rem";
934 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
935 # better. I'll leave the original in in case it needs to be changed for you
938 "select max(cast(cardnumber as signed)) from borrowers");
940 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
944 my ($result) = $sth->fetchrow;
946 $cardnumber = $result + 1;
952 =head2 findguarantees
954 ($num_children, $children_arrayref) = &findguarantees($parent_borrno);
955 $child0_cardno = $children_arrayref->[0]{"cardnumber"};
956 $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
958 C<&findguarantees> takes a borrower number (e.g., that of a patron
959 with children) and looks up the borrowers who are guaranteed by that
960 borrower (i.e., the patron's children).
962 C<&findguarantees> returns two values: an integer giving the number of
963 borrowers guaranteed by C<$parent_borrno>, and a reference to an array
964 of references to hash, which gives the actual results.
970 my ($borrowernumber) = @_;
971 my $dbh = C4::Context->dbh;
974 "select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
976 $sth->execute($borrowernumber);
979 while ( my $data = $sth->fetchrow_hashref ) {
983 return ( scalar(@dat), \@dat );
988 $guarantor = &findguarantor($borrower_no);
989 $guarantor_cardno = $guarantor->{"cardnumber"};
990 $guarantor_surname = $guarantor->{"surname"};
993 C<&findguarantor> takes a borrower number (presumably that of a child
994 patron), finds the guarantor for C<$borrower_no> (the child's parent),
995 and returns the record for the guarantor.
997 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
998 from the C<borrowers> database table;
1004 my ($borrowernumber) = @_;
1005 my $dbh = C4::Context->dbh;
1007 $dbh->prepare("select guarantorid from borrowers where borrowernumber=?");
1008 $sth->execute($borrowernumber);
1009 my $data = $sth->fetchrow_hashref;
1011 $sth = $dbh->prepare("Select * from borrowers where borrowernumber=?");
1012 $sth->execute( $data->{'guarantorid'} );
1013 $data = $sth->fetchrow_hashref;
1018 =item GuarantornameSearch
1020 ($count, $borrowers) = &GuarantornameSearch($searchstring, $type);
1022 Looks up guarantor by name.
1024 BUGFIX 499: C<$type> is now used to determine type of search.
1025 if $type is "simple", search is performed on the first letter of the
1028 C<$searchstring> is a space-separated list of search terms. Each term
1029 must match the beginning a borrower's surname, first name, or other
1032 C<&GuarantornameSearch> returns a two-element list. C<$borrowers> is a
1033 reference-to-array; each element is a reference-to-hash, whose keys
1034 are the fields of the C<borrowers> table in the Koha database.
1035 C<$count> is the number of elements in C<$borrowers>.
1037 return all info from guarantor =>only category_type A
1042 #used by member enquiries from the intranet
1043 #called by guarantor_search.pl
1044 sub GuarantornameSearch {
1045 my ($searchstring, $orderby, $type ) = @_;
1046 my $dbh = C4::Context->dbh;
1052 if ( $type eq "simple" ) # simple search for one letter only
1055 "Select * from borrowers,categories where borrowers.categorycode=categories.categorycode and category_type='A' and surname like ? order by $orderby";
1056 @bind = ("$searchstring%");
1058 else # advanced search looking in surname, firstname and othernames
1060 @data = split( ' ', $searchstring );
1062 $query = "Select * from borrowers,categories
1063 where ((surname like ? or surname like ?
1064 or firstname like ? or firstname like ?
1065 or othernames like ? or othernames like ?) and borrowers.categorycode=categories.categorycode and category_type='A'
1068 "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
1069 "$data[0]%", "% $data[0]%"
1071 for ( my $i = 1 ; $i < $count ; $i++ ) {
1072 $query = $query . " and (" . " surname like ? or surname like ?
1073 or firstname like ? or firstname like ?
1074 or othernames like ? or othernames like ?)";
1076 "$data[$i]%", "% $data[$i]%", "$data[$i]%",
1077 "% $data[$i]%", "$data[$i]%", "% $data[$i]%" );
1081 $query = $query . ") or cardnumber like ?
1083 push( @bind, $searchstring );
1088 my $sth = $dbh->prepare($query);
1089 $sth->execute(@bind);
1091 my $cnt = $sth->rows;
1092 while ( my $data = $sth->fetchrow_hashref ) {
1093 push( @results, $data );
1098 return ( $cnt, \@results );
1103 ($count, $issues) = &borrissues($borrowernumber);
1105 Looks up what the patron with the given borrowernumber has borrowed.
1107 C<&borrissues> returns a two-element array. C<$issues> is a
1108 reference-to-array, where each element is a reference-to-hash; the
1109 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
1110 in the Koha database. C<$count> is the number of elements in
1117 my ($borrowernumber) = @_;
1118 my $dbh = C4::Context->dbh;
1119 my $sth = $dbh->prepare(
1120 "Select * from issues,biblio,items where borrowernumber=?
1121 and items.itemnumber=issues.itemnumber
1122 and items.biblionumber=biblio.biblionumber
1123 and issues.returndate is NULL order by date_due"
1125 $sth->execute($borrowernumber);
1127 while ( my $data = $sth->fetchrow_hashref ) {
1128 push @result, $data;
1131 return ( scalar(@result), \@result );
1136 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
1138 Looks up what the patron with the given borrowernumber has borrowed,
1139 and sorts the results.
1141 C<$sortkey> is the name of a field on which to sort the results. This
1142 should be the name of a field in the C<issues>, C<biblio>,
1143 C<biblioitems>, or C<items> table in the Koha database.
1145 C<$limit> is the maximum number of results to return.
1147 C<&allissues> returns a two-element array. C<$issues> is a
1148 reference-to-array, where each element is a reference-to-hash; the
1149 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
1150 C<items> tables of the Koha database. C<$count> is the number of
1151 elements in C<$issues>
1157 my ( $borrowernumber, $order, $limit ) = @_;
1159 #FIXME: sanity-check order and limit
1160 my $dbh = C4::Context->dbh;
1163 "Select *,items.timestamp AS itemstimestamp from issues,biblio,items,biblioitems
1164 where borrowernumber=? and
1165 items.biblioitemnumber=biblioitems.biblioitemnumber and
1166 items.itemnumber=issues.itemnumber and
1167 items.biblionumber=biblio.biblionumber order by $order";
1168 if ( $limit != 0 ) {
1169 $query .= " limit $limit";
1173 my $sth = $dbh->prepare($query);
1174 $sth->execute($borrowernumber);
1177 while ( my $data = $sth->fetchrow_hashref ) {
1178 $result[$i] = $data;
1183 # get all issued items for borrowernumber from oldissues table
1184 # large chunk of older issues data put into table oldissues
1185 # to speed up db calls for issuing items
1186 if ( C4::Context->preference("ReadingHistory") ) {
1187 my $query2 = "SELECT * FROM oldissues,biblio,items,biblioitems
1188 WHERE borrowernumber=?
1189 AND items.biblioitemnumber=biblioitems.biblioitemnumber
1190 AND items.itemnumber=oldissues.itemnumber
1191 AND items.biblionumber=biblio.biblionumber
1193 if ( $limit != 0 ) {
1194 $limit = $limit - $count;
1195 $query2 .= " limit $limit";
1198 my $sth2 = $dbh->prepare($query2);
1199 $sth2->execute($borrowernumber);
1201 while ( my $data2 = $sth2->fetchrow_hashref ) {
1202 $result[$i] = $data2;
1209 return ( $i, \@result );
1212 =head2 getboracctrecord
1214 ($count, $acctlines, $total) = &getboracctrecord($borrowernumber);
1216 Looks up accounting data for the patron with the given borrowernumber.
1218 (FIXME - I'm not at all sure what this is about.)
1220 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
1221 reference-to-array, where each element is a reference-to-hash; the
1222 keys are the fields of the C<accountlines> table in the Koha database.
1223 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1224 total amount outstanding for all of the account lines.
1229 sub getboracctrecord {
1231 my $dbh = C4::Context->dbh;
1234 my $sth = $dbh->prepare(
1235 "Select * from accountlines where
1236 borrowernumber=? order by date desc,timestamp desc"
1239 $sth->execute( $params->{'borrowernumber'} );
1241 while ( my $data = $sth->fetchrow_hashref ) {
1243 #FIXME before reinstating: insecure?
1244 # if ($data->{'itemnumber'} ne ''){
1245 # $query="Select * from items,biblio where items.itemnumber=
1246 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1247 # my $sth2=$dbh->prepare($query);
1249 # my $data2=$sth2->fetchrow_hashref;
1253 $acctlines[$numlines] = $data;
1255 $total += $data->{'amountoutstanding'};
1258 return ( $numlines, \@acctlines, $total );
1261 =head2 GetBorNotifyAcctRecord
1263 ($count, $acctlines, $total) = &GetBorNotifyAcctRecord($params,$notifyid);
1265 Looks up accounting data for the patron with the given borrowernumber per file number.
1267 (FIXME - I'm not at all sure what this is about.)
1269 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1270 reference-to-array, where each element is a reference-to-hash; the
1271 keys are the fields of the C<accountlines> table in the Koha database.
1272 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1273 total amount outstanding for all of the account lines.
1277 sub GetBorNotifyAcctRecord {
1278 my ( $params, $notifyid ) = @_;
1279 my $dbh = C4::Context->dbh;
1282 my $query = qq| SELECT *
1284 WHERE borrowernumber=?
1286 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')
1287 AND amountoutstanding != '0'
1288 ORDER BY notify_id,accounttype
1290 my $sth = $dbh->prepare($query);
1292 $sth->execute( $params->{'borrowernumber'}, $notifyid );
1294 while ( my $data = $sth->fetchrow_hashref ) {
1295 $acctlines[$numlines] = $data;
1297 $total += $data->{'amountoutstanding'};
1300 return ( $numlines, \@acctlines, $total );
1303 =head2 checkuniquemember (OUEST-PROVENCE)
1305 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
1307 Checks that a member exists or not in the database.
1309 C<&result> is 1 (=exist) or 0 (=does not exist)
1310 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
1311 C<&surname> is the surname
1312 C<&categorycode> is from categorycode table
1313 C<&firstname> is the firstname (only if collectivity=0)
1314 C<&dateofbirth> is the date of birth (only if collectivity=0)
1318 sub checkuniquemember {
1319 my ( $collectivity, $surname, $firstname, $dateofbirth ) = @_;
1320 my $dbh = C4::Context->dbh;
1322 if ($collectivity) {
1324 # $request="select count(*) from borrowers where surname=? and categorycode=?";
1326 "select borrowernumber,categorycode from borrowers where surname=? ";
1330 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
1332 "select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
1334 my $sth = $dbh->prepare($request);
1335 if ($collectivity) {
1336 $sth->execute( uc($surname) );
1339 $sth->execute( uc($surname), ucfirst($firstname), $dateofbirth );
1341 my @data = $sth->fetchrow;
1344 return $data[0], $data[1];
1354 =head2 getzipnamecity (OUEST-PROVENCE)
1356 take all info from table city for the fields city and zip
1357 check for the name and the zip code of the city selected
1361 sub getzipnamecity {
1363 my $dbh = C4::Context->dbh;
1366 "select city_name,city_zipcode from cities where cityid=? ");
1367 $sth->execute($cityid);
1368 my @data = $sth->fetchrow;
1369 return $data[0], $data[1];
1372 =head2 updatechildguarantor (OUEST-PROVENCE)
1374 check for title,firstname,surname,adress,zip code and city from guarantor to
1381 sub getguarantordata {
1382 my ($borrowerid) = @_;
1383 my $dbh = C4::Context->dbh;
1386 "Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro,fax from borrowers where borrowernumber =? "
1388 $sth->execute($borrowerid);
1389 my $guarantor_data = $sth->fetchrow_hashref;
1391 return $guarantor_data;
1394 =head2 getdcity (OUEST-PROVENCE)
1396 recover cityid with city_name condition
1401 my ($city_name) = @_;
1402 my $dbh = C4::Context->dbh;
1403 my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
1404 $sth->execute($city_name);
1405 my $data = $sth->fetchrow;
1409 =head2 getcategorytype (OUEST-PROVENCE)
1411 check for the category_type with categorycode
1412 and return the category_type
1416 sub getcategorytype {
1417 my ($categorycode) = @_;
1418 my $dbh = C4::Context->dbh;
1421 "Select category_type,description from categories where categorycode=? "
1423 $sth->execute($categorycode);
1424 my ( $category_type, $description ) = $sth->fetchrow;
1425 return $category_type, $description;
1428 sub calcexpirydate {
1429 my ( $categorycode, $dateenrolled ) = @_;
1430 my $dbh = C4::Context->dbh;
1433 "select enrolmentperiod from categories where categorycode=?");
1434 $sth->execute($categorycode);
1435 my ($enrolmentperiod) = $sth->fetchrow;
1436 $enrolmentperiod = 12 unless ($enrolmentperiod);
1437 # warn "Avant format_date_in_iso :".$dateenrolled;
1438 # $dateenrolled=format_date_in_iso($dateenrolled);
1439 # warn "Apres format_date_in_iso :".$dateenrolled;
1440 my @date=split /-/,format_date_in_iso($dateenrolled);
1441 @date=Add_Delta_YM($date[0],$date[1],$date[2],0,$enrolmentperiod);
1442 return sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
1445 =head2 checkuserpassword (OUEST-PROVENCE)
1447 check for the password and login are not used
1448 return the number of record
1449 0=> NOT USED 1=> USED
1453 sub checkuserpassword {
1454 my ( $borrowernumber, $userid, $password ) = @_;
1455 $password = md5_base64($password);
1456 my $dbh = C4::Context->dbh;
1459 "Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? "
1461 $sth->execute( $borrowernumber, $userid, $password );
1462 my $number_rows = $sth->fetchrow;
1463 return $number_rows;
1467 =head2 GetborCatFromCatType
1469 ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
1471 Looks up the different types of borrowers in the database. Returns two
1472 elements: a reference-to-array, which lists the borrower category
1473 codes, and a reference-to-hash, which maps the borrower category codes
1474 to category descriptions.
1479 sub GetborCatFromCatType {
1480 my ( $category_type, $action ) = @_;
1481 my $dbh = C4::Context->dbh;
1482 my $request = qq| SELECT categorycode,description
1485 ORDER BY categorycode|;
1486 my $sth = $dbh->prepare($request);
1488 $sth->execute($category_type);
1497 while ( my $data = $sth->fetchrow_hashref ) {
1498 push @codes, $data->{'categorycode'};
1499 $labels{ $data->{'categorycode'} } = $data->{'description'};
1502 return ( \@codes, \%labels );
1505 =head2 getborrowercategory
1507 $description,$dateofbirthrequired,$upperagelimit,$category_type = &getborrowercategory($categorycode);
1509 Given the borrower's category code, the function returns the corresponding
1510 description , dateofbirthrequired , upperagelimit and category type for a comprehensive information display.
1514 sub getborrowercategory {
1516 my $dbh = C4::Context->dbh;
1519 "SELECT description,dateofbirthrequired,upperagelimit,category_type FROM categories WHERE categorycode = ?"
1521 $sth->execute($catcode);
1522 my ( $description, $dateofbirthrequired, $upperagelimit, $category_type ) =
1525 return ( $description, $dateofbirthrequired, $upperagelimit,
1527 } # sub getborrowercategory
1529 =head2 ethnicitycategories
1531 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
1533 Looks up the different ethnic types in the database. Returns two
1534 elements: a reference-to-array, which lists the ethnicity codes, and a
1535 reference-to-hash, which maps the ethnicity codes to ethnicity
1542 sub ethnicitycategories {
1543 my $dbh = C4::Context->dbh;
1544 my $sth = $dbh->prepare("Select code,name from ethnicity order by name");
1548 while ( my $data = $sth->fetchrow_hashref ) {
1549 push @codes, $data->{'code'};
1550 $labels{ $data->{'code'} } = $data->{'name'};
1553 return ( \@codes, \%labels );
1558 $ethn_name = &fixEthnicity($ethn_code);
1560 Takes an ethnicity code (e.g., "european" or "pi") and returns the
1561 corresponding descriptive name from the C<ethnicity> table in the
1562 Koha database ("European" or "Pacific Islander").
1569 my $ethnicity = shift;
1570 return unless $ethnicity;
1571 my $dbh = C4::Context->dbh;
1572 my $sth = $dbh->prepare("Select name from ethnicity where code = ?");
1573 $sth->execute($ethnicity);
1574 my $data = $sth->fetchrow_hashref;
1576 return $data->{'name'};
1577 } # sub fixEthnicity
1581 $dateofbirth,$date = &get_age($date);
1583 this function return the borrowers age with the value of dateofbirth
1589 my ( $date, $date_ref ) = @_;
1591 if ( not defined $date_ref ) {
1592 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1595 my ( $year1, $month1, $day1 ) = split /-/, $date;
1596 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1598 my $age = $year2 - $year1;
1599 if ( $month1 . $day1 > $month2 . $day2 ) {
1606 =head2 get_institutions
1607 $insitutions = get_institutions();
1609 Just returns a list of all the borrowers of type I, borrownumber and name
1614 sub get_institutions {
1615 my $dbh = C4::Context->dbh();
1618 "SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname"
1622 while ( my $data = $sth->fetchrow_hashref() ) {
1623 $orgs{ $data->{'borrowernumber'} } = $data;
1628 } # sub get_institutions
1630 =head2 add_member_orgs
1632 add_member_orgs($borrowernumber,$borrowernumbers);
1634 Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
1639 sub add_member_orgs {
1640 my ( $borrowernumber, $otherborrowers ) = @_;
1641 my $dbh = C4::Context->dbh();
1643 "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
1644 my $sth = $dbh->prepare($query);
1645 foreach my $otherborrowernumber (@$otherborrowers) {
1646 $sth->execute( $borrowernumber, $otherborrowernumber );
1650 } # sub add_member_orgs
1652 =head2 GetBorrowersFromSurname
1656 \@resutlts = GetBorrowersFromSurname($surname)
1657 this function get the list of borrower names like $surname.
1659 the table of results in @results
1665 sub GetBorrowersFromSurname {
1666 my ($searchstring) = @_;
1667 my $dbh = C4::Context->dbh;
1668 $searchstring =~ s/\'/\\\'/g;
1669 my @data = split( ' ', $searchstring );
1672 SELECT surname,firstname
1674 WHERE (surname like ?)
1677 my $sth = $dbh->prepare($query);
1678 $sth->execute("$data[0]%");
1682 while ( my $data = $sth->fetchrow_hashref ) {
1683 push( @results, $data );
1687 return ( $count, \@results );
1690 =head2 citycaracteristiques (OUEST-PROVENCE)
1692 ($id_cityarrayref, $city_hashref) = &citycaracteristic();
1694 Looks up the different city and zip in the database. Returns two
1695 elements: a reference-to-array, which lists the zip city
1696 codes, and a reference-to-hash, which maps the name of the city.
1697 WHERE =>OUEST PROVENCE OR EXTERIEUR
1703 #my ($type_city) = @_;
1704 my $dbh = C4::Context->dbh;
1705 my $query = qq|SELECT cityid,city_name
1707 ORDER BY city_name|;
1708 my $sth = $dbh->prepare($query);
1710 #$sth->execute($type_city);
1715 # insert empty value to create a empty choice in cgi popup
1717 while ( my $data = $sth->fetchrow_hashref ) {
1719 push @id, $data->{'cityid'};
1720 $city{ $data->{'cityid'} } = $data->{'city_name'};
1723 #test to know if the table contain some records if no the function return nothing
1731 return ( \@id, \%city );
1735 =head2 GetSortDetails (OUEST-PROVENCE)
1737 ($lib) = &GetSortDetails($category,$sortvalue);
1739 Returns the authorized value details
1740 C<&$lib>return value of authorized value details
1741 C<&$sortvalue>this is the value of authorized value
1742 C<&$category>this is the value of authorized value category
1746 sub GetSortDetails {
1747 my ( $category, $sortvalue ) = @_;
1748 my $dbh = C4::Context->dbh;
1749 my $query = qq|SELECT lib
1750 FROM authorised_values
1752 AND authorised_value=? |;
1753 my $sth = $dbh->prepare($query);
1754 $sth->execute( $category, $sortvalue );
1755 my $lib = $sth->fetchrow;
1759 =head2 DeleteBorrower
1761 () = &DeleteBorrower($member);
1763 delete all data fo borrowers and add record to deletedborrowers table
1764 C<&$member>this is the borrowernumber
1768 sub DeleteBorrower {
1770 my $dbh = C4::Context->dbh;
1772 $query = qq|SELECT *
1774 WHERE borrowernumber=?|;
1775 my $sth = $dbh->prepare($query);
1776 $sth->execute($member);
1777 my @data = $sth->fetchrow_array;
1780 $dbh->prepare( "Insert into deletedborrowers values ("
1781 . ( "?," x ( scalar(@data) - 1 ) )
1783 $sth->execute(@data);
1787 WHERE borrowernumber=?|;
1788 $sth = $dbh->prepare($query);
1789 $sth->execute($member);
1793 WHERE borrowernumber=?|;
1794 $sth = $dbh->prepare($query);
1795 $sth->execute($member);
1798 # logging to action_log
1799 &logaction(C4::Context->userenv->{'number'},"MEMBERS","DELETE",$member,"")
1800 if C4::Context->preference("BorrowersLog");
1803 =head2 DelBorrowerCompletly
1805 DelBorrowerCompletly($borrowernumber);
1807 This function remove directly a borrower whitout writing it on deleteborrower.
1811 sub DelBorrowerCompletly {
1812 my $dbh = C4::Context->dbh;
1813 my $borrowernumber = shift;
1814 return unless $borrowernumber; # date is mandatory.
1818 WHERE borrowernumber = ?
1820 my $sth = $dbh->prepare($query);
1821 $sth->execute($borrowernumber);
1825 =head2 member_reregistration (OUEST-PROVENCE)
1827 automatic reregistration in borrowers table
1832 sub GetMembeReregistration {
1833 my ( $categorycode, $borrowerid ) = @_;
1834 my $dbh = C4::Context->dbh;
1835 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
1838 $year = $year + 1900;
1839 if ( $mon < '10' ) {
1842 if ( $mday < '10' ) {
1843 $mday = "0" . $mday;
1845 my $today = sprintf("%04d-%02d-%02d",$year,$mon,$mday);
1846 my $dateexpiry = calcexpirydate( $categorycode, $today );
1847 my $query = qq| UPDATE borrowers
1848 SET dateexpiry='$dateexpiry'
1849 WHERE borrowernumber='$borrowerid'|;
1850 my $sth = $dbh->prepare($query);
1856 =head2 GetRoadTypes (OUEST-PROVENCE)
1858 ($idroadtypearrayref, $roadttype_hashref) = &GetRoadTypes();
1860 Looks up the different road type . Returns two
1861 elements: a reference-to-array, which lists the id_roadtype
1862 codes, and a reference-to-hash, which maps the road type of the road .
1868 my $dbh = C4::Context->dbh;
1869 my $query = qq|SELECT roadtypeid,road_type
1871 ORDER BY road_type|;
1872 my $sth = $dbh->prepare($query);
1877 # insert empty value to create a empty choice in cgi popup
1879 while ( my $data = $sth->fetchrow_hashref ) {
1881 push @id, $data->{'roadtypeid'};
1882 $roadtype{ $data->{'roadtypeid'} } = $data->{'road_type'};
1885 #test to know if the table contain some records if no the function return nothing
1893 return ( \@id, \%roadtype );
1899 =head2 GetBorrowersTitles (OUEST-PROVENCE)
1901 ($borrowertitle)= &GetBorrowersTitles();
1903 Looks up the different title . Returns array with all borrowers title
1907 sub GetBorrowersTitles {
1908 my @borrowerTitle = split /,|\|/,C4::Context->preference('BorrowersTitles');
1909 unshift( @borrowerTitle, "" );
1910 return ( \@borrowerTitle);
1915 =head2 GetRoadTypeDetails (OUEST-PROVENCE)
1917 ($roadtype) = &GetRoadTypeDetails($roadtypeid);
1919 Returns the description of roadtype
1920 C<&$roadtype>return description of road type
1921 C<&$roadtypeid>this is the value of roadtype s
1925 sub GetRoadTypeDetails {
1926 my ($roadtypeid) = @_;
1927 my $dbh = C4::Context->dbh;
1928 my $query = qq|SELECT road_type
1930 WHERE roadtypeid=?|;
1931 my $sth = $dbh->prepare($query);
1932 $sth->execute($roadtypeid);
1933 my $roadtype = $sth->fetchrow;
1937 =head2 GetBorrowersWhoHaveNotBorrowedSince
1939 &GetBorrowersWhoHaveNotBorrowedSince($date)
1941 this function get all borrowers who haven't borrowed since the date given on input arg.
1945 sub GetBorrowersWhoHaveNotBorrowedSince {
1947 return unless $date; # date is mandatory.
1948 my $dbh = C4::Context->dbh;
1950 SELECT borrowers.borrowernumber,max(timestamp)
1952 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1953 WHERE issues.borrowernumber IS NOT NULL
1954 GROUP BY borrowers.borrowernumber
1956 my $sth = $dbh->prepare($query);
1960 while ( my $data = $sth->fetchrow_hashref ) {
1961 push @results, $data;
1966 =head2 GetBorrowersWhoHaveNeverBorrowed
1968 $results = &GetBorrowersWhoHaveNeverBorrowed
1970 this function get all borrowers who have never borrowed.
1972 I<$result> is a ref to an array which all elements are a hasref.
1976 sub GetBorrowersWhoHaveNeverBorrowed {
1977 my $dbh = C4::Context->dbh;
1979 SELECT borrowers.borrowernumber,max(timestamp)
1981 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1982 WHERE issues.borrowernumber IS NULL
1984 my $sth = $dbh->prepare($query);
1987 while ( my $data = $sth->fetchrow_hashref ) {
1988 push @results, $data;
1993 =head2 GetBorrowersWithIssuesHistoryOlderThan
1995 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1997 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1999 I<$result> is a ref to an array which all elements are a hashref.
2000 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
2004 sub GetBorrowersWithIssuesHistoryOlderThan {
2005 my $dbh = C4::Context->dbh;
2007 return unless $date; # date is mandatory.
2009 SELECT count(borrowernumber) as n,borrowernumber
2011 WHERE returndate < ?
2012 AND borrowernumber IS NOT NULL
2013 GROUP BY borrowernumber
2015 my $sth = $dbh->prepare($query);
2016 $sth->execute($date);
2019 while ( my $data = $sth->fetchrow_hashref ) {
2020 push @results, $data;
2025 END { } # module clean-up code here (global destructor)