Bug 11742: Change return type for GetLetters
[koha.git] / members / member.pl
blobdfc73636e32d41fbe5379d5c8b9069d6f1e35e2f
1 #!/usr/bin/perl
4 #script to do a borrower enquiry/bring up borrower details etc
5 #written 20/12/99 by chris@katipo.co.nz
8 # Copyright 2000-2002 Katipo Communications
9 # Copyright 2010 BibLibre
11 # This file is part of Koha.
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License along
23 # with Koha; if not, write to the Free Software Foundation, Inc.,
24 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use Modern::Perl;
27 use C4::Auth;
28 use C4::Output;
29 use CGI;
30 use C4::Members;
31 use C4::Branch;
32 use C4::Category;
33 use Koha::DateUtils;
34 use Koha::List::Patron;
36 my $input = new CGI;
37 my $quicksearch = $input->param('quicksearch') || '';
38 my $startfrom = $input->param('startfrom') || 1;
39 my $resultsperpage = $input->param('resultsperpage') || C4::Context->preference("PatronsPerPage") || 20;
41 my ($template, $loggedinuser, $cookie)
42 = get_template_and_user({template_name => "members/member.tmpl",
43 query => $input,
44 type => "intranet",
45 authnotrequired => 0,
46 flagsrequired => {borrowers => 1},
47 });
49 my $theme = $input->param('theme') || "default";
51 my $add_to_patron_list = $input->param('add_to_patron_list');
52 my $add_to_patron_list_which = $input->param('add_to_patron_list_which');
53 my $new_patron_list = $input->param('new_patron_list');
54 my @borrowernumbers = $input->param('borrowernumber');
55 $input->delete(
56 'add_to_patron_list', 'add_to_patron_list_which',
57 'new_patron_list', 'borrowernumber',
60 my $patron = $input->Vars;
61 foreach (keys %$patron){
62 delete $$patron{$_} unless($$patron{$_});
64 my @categories=C4::Category->all;
66 my $branches = GetBranches;
67 my @branchloop;
69 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
70 my $selected;
71 $selected = 1 if $patron->{branchcode} && $branches->{$_}->{branchcode} eq $patron->{branchcode};
72 my %row = ( value => $_,
73 selected => $selected,
74 branchname => $branches->{$_}->{branchname},
76 push @branchloop, \%row;
79 my %categories_dislay;
81 foreach my $category (@categories){
82 my $hash={
83 category_description=>$$category{description},
84 category_type=>$$category{category_type}
86 $categories_dislay{$$category{categorycode}} = $hash;
88 my $AddPatronLists = C4::Context->preference("AddPatronLists") || '';
89 $template->param(
90 "AddPatronLists_$AddPatronLists" => "1",
92 if ($AddPatronLists=~/code/){
93 $categories[0]->{'first'}=1;
96 my $member=$input->param('member') || '';
97 my $orderbyparams=$input->param('orderby') || '';
98 my @orderby;
99 if ($orderbyparams){
100 my @orderbyelt=split(/,/,$orderbyparams);
101 push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
103 else {
104 @orderby = ({surname=>0},{firstname=>0});
107 $member =~ s/,//g; #remove any commas from search string
108 $member =~ s/\*/%/g;
110 my $from = ( $startfrom - 1 ) * $resultsperpage;
111 my $to = $from + $resultsperpage;
113 my ($count,$results);
114 if ($member || keys %$patron) {
115 my $searchfields = $input->param('searchfields') || '';
116 my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
118 if ( $searchfields eq "dateofbirth" ) {
119 $member = output_pref({dt => dt_from_string($member), dateformat => 'iso', dateonly => 1});
122 my $searchtype = $input->param('searchtype');
123 my $search_scope =
124 $quicksearch ? "field_start_with"
125 : $searchtype ? $searchtype
126 : "start_with";
128 ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
131 if ($add_to_patron_list) {
132 my $patron_list;
134 if ( $add_to_patron_list eq 'new' ) {
135 $patron_list = AddPatronList( { name => $new_patron_list } );
137 else {
138 $patron_list =
139 [ GetPatronLists( { patron_list_id => $add_to_patron_list } ) ]->[0];
142 if ( $add_to_patron_list_which eq 'all' ) {
143 @borrowernumbers = map { $_->{borrowernumber} } @$results;
146 my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } );
148 $template->param(
149 patron_list => $patron_list,
150 patrons_added_to_list => \@patrons_added_to_list,
154 if ($results) {
155 for my $field ('categorycode','branchcode'){
156 next unless ($patron->{$field});
157 @$results = grep { $_->{$field} eq $patron->{$field} } @$results;
159 $count = scalar(@$results);
160 } else {
161 $count = 0;
164 if($count == 1){
165 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
166 exit;
169 my @resultsdata;
170 $to=($count>$to?$to:$count);
171 my $index=$from;
172 foreach my $borrower(@$results[$from..$to-1]){
173 #find out stats
174 my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
175 $fines ||= 0;
176 $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
178 my %row = (
179 count => $index++,
180 %$borrower,
181 (defined $categories_dislay{ $borrower->{categorycode} }? %{ $categories_dislay{ $borrower->{categorycode} } }:()),
182 overdues => $od,
183 issues => $issue,
184 odissue => "$od/$issue",
185 fines => sprintf("%.2f",$fines),
186 branchname => $branches->{$borrower->{branchcode}}->{branchname},
188 push(@resultsdata, \%row);
191 if ($$patron{categorycode}){
192 foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
193 $$category{selected}=1;
196 my %parameters=
197 ( %$patron
198 , 'orderby' => $orderbyparams
199 , 'resultsperpage' => $resultsperpage
200 , 'type'=> 'intranet');
201 my $base_url =
202 'member.pl?&'
203 . join(
204 '&',
205 map { "$_=$parameters{$_}" } (keys %parameters)
208 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
210 $template->param(
211 %$patron,
212 letters => \@letters,
213 paginationbar => pagination_bar(
214 $base_url,
215 int( $count / $resultsperpage ) + ( $count % $resultsperpage ? 1 : 0 ),
216 $startfrom,
217 'startfrom'
219 startfrom => $startfrom,
220 from => ( $startfrom - 1 ) * $resultsperpage + 1,
221 to => $to,
222 multipage => ( $count != $to || $startfrom != 1 ),
223 advsearch => ( $$patron{categorycode} || $$patron{branchcode} ),
224 branchloop => \@branchloop,
225 categories => \@categories,
226 searching => "1",
227 numresults => $count,
228 resultsloop => \@resultsdata,
229 results_per_page => $resultsperpage,
230 member => $member,
231 search_parameters => \%parameters,
232 patron_lists => [ GetPatronLists() ],
235 output_html_with_http_headers $input, $cookie, $template->output;