Merge remote branch 'kc/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 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 use strict;
26 #use warnings; FIXME - Bug 2505
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 File::Basename;
34
35 my $input = new CGI;
36 my $quicksearch = $input->param('quicksearch');
37 my $startfrom = $input->param('startfrom')||1;
38 my $resultsperpage = $input->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
39
40 my ($template, $loggedinuser, $cookie)
41     = get_template_and_user({template_name => "members/member.tmpl",
42                  query => $input,
43                  type => "intranet",
44                  authnotrequired => 0,
45                  flagsrequired => {borrowers => 1},
46                  });
47
48 my $theme = $input->param('theme') || "default";
49
50 my $patron = $input->Vars;
51 foreach (keys %$patron){
52         delete $$patron{$_} unless($$patron{$_}); 
53 }
54
55 my @categories=C4::Category->all;
56 my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
57
58 my %categories_dislay;
59
60 foreach my $category (@categories){
61         my $hash={
62                         category_description=>$$category{description},
63                         category_type=>$$category{category_type}
64                          };
65         $categories_dislay{$$category{categorycode}} = $hash;
66 }
67 $template->param( 
68         "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
69             );
70 if (C4::Context->preference("AddPatronLists")=~/code/){
71     $categories[0]->{'first'}=1;
72 }  
73
74 my $member=$input->param('member');
75 my $orderbyparams=$input->param('orderby');
76 my @orderby;
77 if ($orderbyparams){
78         my @orderbyelt=split(/,/,$orderbyparams);
79         push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
80 }
81 else {
82         @orderby = ({surname=>0},{firstname=>0});
83 }
84
85 $member =~ s/,//g;   #remove any commas from search string
86 $member =~ s/\*/%/g;
87
88 my ($count,$results);
89
90 my @searchpatron;
91 push @searchpatron, $member if ($member);
92 push @searchpatron, $patron if (keys %$patron);
93 my $from= ($startfrom-1)*$resultsperpage;
94 my $to=$from+$resultsperpage;
95  #($results)=Search(\@searchpatron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  ) if (@searchpatron);
96  my $search_scope=($quicksearch?"field_start_with":"contain");
97  ($results)=Search(\@searchpatron,\@orderby,undef,undef,["firstname","surname","email","othernames","cardnumber","userid"],$search_scope  ) if (@searchpatron);
98 if ($results){
99         $count =scalar(@$results);
100 }
101 my @resultsdata;
102 my $to=($count>$to?$to:$count);
103 my $index=$from;
104 foreach my $borrower(@$results[$from..$to-1]){
105   #find out stats
106   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
107
108   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
109
110   my %row = (
111     count => $index++,
112         %$borrower,
113         %{$categories_dislay{$$borrower{categorycode}}},
114     overdues => $od,
115     issues => $issue,
116     odissue => "$od/$issue",
117     fines =>  sprintf("%.2f",$fines),
118     );
119   push(@resultsdata, \%row);
120 }
121
122 if ($$patron{branchcode}){
123         foreach my $branch (grep{$_->{value} eq $$patron{branchcode}}@$branches){
124                 $$branch{selected}=1;
125         }
126 }
127 if ($$patron{categorycode}){
128         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
129                 $$category{selected}=1;
130         }
131 }
132 my %parameters=
133         (  %$patron
134                 , 'orderby'                     => $orderbyparams 
135                 , 'resultsperpage'      => $resultsperpage 
136         , 'type'=> 'intranet'); 
137 my $base_url =
138     'member.pl?&'
139   . join(
140     '&',
141     map { "$_=$parameters{$_}" } (keys %parameters)
142   );
143
144 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
145 $template->param( letters => \@letters );
146
147 $template->param(
148     paginationbar => pagination_bar(
149         $base_url,
150         int( $count / $resultsperpage ) + ($count % $resultsperpage ? 1 : 0),
151         $startfrom, 'startfrom'
152     ),
153     startfrom => $startfrom,
154     from      => ($startfrom-1)*$resultsperpage+1,  
155     to        => $to,
156     multipage => ($count != $to+1 || $startfrom!=1),
157 );
158 $template->param(
159     branchloop=>$branches,
160     categories=>\@categories,
161 );
162
163
164 $template->param( 
165         searching       => "1",
166                 actionname              =>basename($0),
167                 %$patron,
168         numresults      => $count,
169         resultsloop     => \@resultsdata,
170             );
171
172 output_html_with_http_headers $input, $cookie, $template->output;