Merge remote branch 'kc/new/bug_6104' into kcmaster
[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 = 1 if $branches->{$_}->{branchcode} eq $$patron{branchcode};
62   my %row = ( value => $_,
63         selected => $selected,
64         branchname => $branches->{$_}->{branchname},
65       );
66   push @branchloop, \%row;
67 }
68
69 my %categories_dislay;
70
71 foreach my $category (@categories){
72         my $hash={
73                         category_description=>$$category{description},
74                         category_type=>$$category{category_type}
75                          };
76         $categories_dislay{$$category{categorycode}} = $hash;
77 }
78 $template->param( 
79         "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
80             );
81 if (C4::Context->preference("AddPatronLists")=~/code/){
82     $categories[0]->{'first'}=1;
83 }  
84
85 my $member=$input->param('member');
86 my $orderbyparams=$input->param('orderby');
87 my @orderby;
88 if ($orderbyparams){
89         my @orderbyelt=split(/,/,$orderbyparams);
90         push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
91 }
92 else {
93         @orderby = ({surname=>0},{firstname=>0});
94 }
95
96 $member =~ s/,//g;   #remove any commas from search string
97 $member =~ s/\*/%/g;
98
99 my ($count,$results);
100
101 my @searchpatron;
102 push @searchpatron, $member if ($member);
103 push @searchpatron, $patron if ( keys %$patron );
104 my $from = ( $startfrom - 1 ) * $resultsperpage;
105 my $to   = $from + $resultsperpage;
106
107 #($results)=Search(\@searchpatron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  ) if (@searchpatron);
108 my $search_scope = ( $quicksearch ? "field_start_with" : "start_with" );
109 ($results) = Search( \@searchpatron, \@orderby, undef, undef, [ "firstname", "surname", "othernames", "cardnumber", "userid" ], $search_scope ) if (@searchpatron);
110
111 if ($results) {
112         for my $field ('categorycode','branchcode'){
113                 next unless ($patron->{$field});
114                 @$results = grep { $_->{$field} eq $patron->{$field} } @$results; 
115         }
116     $count = scalar(@$results);
117 }
118 my @resultsdata;
119 $to=($count>$to?$to:$count);
120 my $index=$from;
121 foreach my $borrower(@$results[$from..$to-1]){
122   #find out stats
123   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
124
125   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
126
127   my %row = (
128     count => $index++,
129         %$borrower,
130         %{$categories_dislay{$$borrower{categorycode}}},
131     overdues => $od,
132     issues => $issue,
133     odissue => "$od/$issue",
134     fines =>  sprintf("%.2f",$fines),
135     );
136   push(@resultsdata, \%row);
137 }
138
139 if ($$patron{categorycode}){
140         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
141                 $$category{selected}=1;
142         }
143 }
144 my %parameters=
145         (  %$patron
146                 , 'orderby'                     => $orderbyparams 
147                 , 'resultsperpage'      => $resultsperpage 
148         , 'type'=> 'intranet'); 
149 my $base_url =
150     'member.pl?&'
151   . join(
152     '&',
153     map { "$_=$parameters{$_}" } (keys %parameters)
154   );
155
156 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
157
158 $template->param(
159     letters => \@letters,
160     paginationbar => pagination_bar(
161         $base_url,
162         int( $count / $resultsperpage ) + ($count % $resultsperpage ? 1 : 0),
163         $startfrom, 'startfrom'
164     ),
165     startfrom => $startfrom,
166     from      => ($startfrom-1)*$resultsperpage+1,  
167     to        => $to,
168     multipage => ($count != $to || $startfrom!=1),
169     advsearch => ($$patron{categorycode} || $$patron{branchcode}),
170     branchloop=>\@branchloop,
171     categories=>\@categories,
172     searching       => "1",
173                 actionname              =>basename($0),
174                 %$patron,
175         numresults      => $count,
176         resultsloop     => \@resultsdata,
177             );
178
179 output_html_with_http_headers $input, $cookie, $template->output;