Merge branch 'bug_9102' into 3.12-master
[koha.git] / members / member.pl
1 #!/usr/bin/perl
2
3
4 #script to do a borrower enquiry/bring up borrower details etc
5 #written 20/12/99 by chris@katipo.co.nz
6
7
8 # Copyright 2000-2002 Katipo Communications
9 # Copyright 2010 BibLibre
10 #
11 # This file is part of Koha.
12 #
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.
17 #
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.
21 #
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.
25
26 use strict;
27 #use warnings; FIXME - Bug 2505
28 use C4::Auth;
29 use C4::Output;
30 use CGI;
31 use C4::Members;
32 use C4::Branch;
33 use C4::Category;
34 use File::Basename;
35
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;
40
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                  });
48
49 my $theme = $input->param('theme') || "default";
50
51 my $patron = $input->Vars;
52 foreach (keys %$patron){
53         delete $$patron{$_} unless($$patron{$_});
54 }
55 my @categories=C4::Category->all;
56
57 my $branches = GetBranches;
58 my @branchloop;
59
60 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
61   my $selected;
62   $selected = 1 if $branches->{$_}->{branchcode} eq $$patron{branchcode};
63   my %row = ( value => $_,
64         selected => $selected,
65         branchname => $branches->{$_}->{branchname},
66       );
67   push @branchloop, \%row;
68 }
69
70 my %categories_dislay;
71
72 foreach my $category (@categories){
73         my $hash={
74                         category_description=>$$category{description},
75                         category_type=>$$category{category_type}
76                          };
77         $categories_dislay{$$category{categorycode}} = $hash;
78 }
79 my $AddPatronLists = C4::Context->preference("AddPatronLists") || '';
80 $template->param( 
81         "AddPatronLists_$AddPatronLists" => "1",
82             );
83 if ($AddPatronLists=~/code/){
84     $categories[0]->{'first'}=1;
85 }  
86
87 my $member=$input->param('member');
88 my $orderbyparams=$input->param('orderby');
89 my @orderby;
90 if ($orderbyparams){
91         my @orderbyelt=split(/,/,$orderbyparams);
92         push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
93 }
94 else {
95         @orderby = ({surname=>0},{firstname=>0});
96 }
97
98 my $searchfields = $input->param('searchfields');
99 my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
100
101 $member =~ s/,//g;   #remove any commas from search string
102 $member =~ s/\*/%/g;
103
104 my $searchtype = $input->param('searchtype');
105 my %searchtype_ok = ( 'contain' => 1 );
106 if ( !defined($searchtype_ok{$searchtype}) ) {
107     undef $searchtype;
108 }
109
110 my $from = ( $startfrom - 1 ) * $resultsperpage;
111 my $to   = $from + $resultsperpage;
112
113 my ($count,$results);
114 if ($member || keys %$patron) {
115     #($results)=Search($member || $patron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  );
116     my $search_scope = $searchtype || ( $quicksearch ? "field_start_with" : "start_with" );
117     ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
118 }
119
120 if ($results) {
121         for my $field ('categorycode','branchcode'){
122                 next unless ($patron->{$field});
123                 @$results = grep { $_->{$field} eq $patron->{$field} } @$results; 
124         }
125     $count = scalar(@$results);
126 }
127
128 if($count == 1){
129     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
130     exit;
131 }
132
133 my @resultsdata;
134 $to=($count>$to?$to:$count);
135 my $index=$from;
136 foreach my $borrower(@$results[$from..$to-1]){
137   #find out stats
138   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
139
140   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
141
142   my %row = (
143     count => $index++,
144     %$borrower,
145     (defined $categories_dislay{ $borrower->{categorycode} }?   %{ $categories_dislay{ $borrower->{categorycode} } }:()),
146     overdues => $od,
147     issues => $issue,
148     odissue => "$od/$issue",
149     fines =>  sprintf("%.2f",$fines),
150     branchname => $branches->{$borrower->{branchcode}}->{branchname},
151     );
152   push(@resultsdata, \%row);
153 }
154
155 if ($$patron{categorycode}){
156         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
157                 $$category{selected}=1;
158         }
159 }
160 my %parameters=
161         (  %$patron
162                 , 'orderby'                     => $orderbyparams 
163                 , 'resultsperpage'      => $resultsperpage 
164         , 'type'=> 'intranet'); 
165 my $base_url =
166     'member.pl?&'
167   . join(
168     '&',
169     map { "$_=$parameters{$_}" } (keys %parameters)
170   );
171
172 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
173
174 $template->param(
175     letters => \@letters,
176     paginationbar => pagination_bar(
177         $base_url,
178         int( $count / $resultsperpage ) + ($count % $resultsperpage ? 1 : 0),
179         $startfrom, 'startfrom'
180     ),
181     startfrom => $startfrom,
182     from      => ($startfrom-1)*$resultsperpage+1,  
183     to        => $to,
184     multipage => ($count != $to || $startfrom!=1),
185     advsearch => ($$patron{categorycode} || $$patron{branchcode}),
186     branchloop=>\@branchloop,
187     categories=>\@categories,
188     searching       => "1",
189                 actionname              =>basename($0),
190                 %$patron,
191         numresults      => $count,
192         resultsloop     => \@resultsdata,
193             );
194
195 output_html_with_http_headers $input, $cookie, $template->output;