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