MT 1716 : Followup : search by initial was broken
[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 with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use C4::Auth;
27 use C4::Output;
28 use CGI;
29 use C4::Members;
30 use C4::Branch;
31 use C4::Category;
32
33 my $input = new CGI;
34 my $quicksearch = $input->param('quicksearch');
35 my $startfrom = $input->param('startfrom')||1;
36 my $resultsperpage = $input->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
37
38 my ($template, $loggedinuser, $cookie);
39 if($quicksearch){
40     ($template, $loggedinuser, $cookie)
41     = get_template_and_user({template_name => "members/member-quicksearch-results.tmpl",
42                  query => $input,
43                  type => "intranet",
44                  authnotrequired => 0,
45                  flagsrequired => {borrowers => 1},
46                  });
47 } else {
48     ($template, $loggedinuser, $cookie)
49     = get_template_and_user({template_name => "members/member.tmpl",
50                  query => $input,
51                  type => "intranet",
52                  authnotrequired => 0,
53                  flagsrequired => {borrowers => 1},
54                  });
55 }
56 my $theme = $input->param('theme') || "default";
57
58 my $patron = $input->Vars;
59 foreach (keys %$patron){
60         delete $$patron{$_} unless($$patron{$_}); 
61 }
62
63 my @categories=C4::Category->all;
64 my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
65
66 my %categories_dislay;
67
68 foreach my $category (@categories){
69         my $hash={
70                         category_description=>$$category{description},
71                         category_type=>$$category{category_type}
72                          };
73         $categories_dislay{$$category{categorycode}} = $hash;
74 }
75 $template->param( 
76         "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
77             );
78 if (C4::Context->preference("AddPatronLists")=~/code/){
79     $categories[0]->{'first'}=1;
80 }  
81
82 my $member=$input->param('member');
83 my $orderby=$input->param('orderby');
84 $orderby = "surname,firstname" unless $orderby;
85 $member =~ s/,//g;   #remove any commas from search string
86 $member =~ s/\*/%/g;
87
88 my ($count,$results);
89
90 if (C4::Context->preference("IndependantBranches")){
91    if (C4::Context->userenv && C4::Context->userenv->{flags} % 2 !=1 && C4::Context->userenv->{'branch'}){
92         $$patron{branchcode}=C4::Context->userenv->{'branch'} unless (C4::Context->userenv->{'branch'} eq "insecure");
93    }
94 }
95 $$patron{firstname}.="\%" if ($$patron{firstname});
96 $$patron{surname}.="\%" if ($$patron{surname});
97
98 my @searchpatron;
99 push @searchpatron, $member if ($member);
100 push @searchpatron, $patron if (keys %$patron);
101 my $from= ($startfrom-1)*$resultsperpage;
102 my $to=$from+$resultsperpage;
103  #($results)=Search(\@searchpatron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  ) if (@searchpatron);
104  ($results)=Search(\@searchpatron,{surname=>1,firstname=>1},undef,undef,["firstname","surname","email","othernames","cardnumber"],"start_with"  ) if (@searchpatron);
105 if ($results){
106         $count =scalar(@$results);
107 }
108 my @resultsdata;
109 my $to=($count>$to?$to:$count);
110 my $index=$from;
111 foreach my $borrower(@$results[$from..$to-1]){
112   #find out stats
113   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
114
115   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
116
117   my %row = (
118     count => $index++,
119         %$borrower,
120         %{$categories_dislay{$$borrower{categorycode}}},
121     overdues => $od,
122     issues => $issue,
123     odissue => "$od/$issue",
124     fines =>  sprintf("%.2f",$fines),
125     );
126   push(@resultsdata, \%row);
127 }
128
129 if ($$patron{branchcode}){
130         foreach my $branch (grep{$_->{value} eq $$patron{branchcode}}@$branches){
131                 $$branch{selected}=1;
132         }
133 }
134 if ($$patron{categorycode}){
135         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
136                 $$category{selected}=1;
137         }
138 }
139 my %parameters=
140         (  %$patron
141                 , 'orderby'                     => $orderby 
142                 , 'resultsperpage'      => $resultsperpage 
143         , 'type'=> 'intranet'); 
144 my $base_url =
145     'member.pl?&'
146   . join(
147     '&',
148     map { "$_=$parameters{$_}" } (keys %parameters)
149   );
150
151 $template->param(
152     paginationbar => pagination_bar(
153         $base_url,  int( $count / $resultsperpage ) + 1,
154         $startfrom, 'startfrom'
155     ),
156     startfrom => $startfrom,
157     from      => ($startfrom-1)*$resultsperpage+1,  
158     to        => $to,
159     multipage => ($count != $to+1 || $startfrom!=1),
160 );
161 $template->param(
162     branchloop=>$branches,
163         categoryloop=>\@categories,
164 );
165
166
167 $template->param( 
168         searching       => "1",
169                 %$patron,
170         numresults      => $count,
171         resultsloop     => \@resultsdata,
172             );
173
174 output_html_with_http_headers $input, $cookie, $template->output;