Bug Fixing Search By initials incorrect
[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 use File::Basename;
33 use YAML;
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 if($quicksearch){
42     ($template, $loggedinuser, $cookie)
43     = get_template_and_user({template_name => "members/member-quicksearch-results.tmpl",
44                  query => $input,
45                  type => "intranet",
46                  authnotrequired => 0,
47                  flagsrequired => {borrowers => 1},
48                  });
49 } else {
50     ($template, $loggedinuser, $cookie)
51     = get_template_and_user({template_name => "members/member.tmpl",
52                  query => $input,
53                  type => "intranet",
54                  authnotrequired => 0,
55                  flagsrequired => {borrowers => 1},
56                  });
57 }
58 my $theme = $input->param('theme') || "default";
59
60 my $patron = $input->Vars;
61 foreach (keys %$patron){
62         delete $$patron{$_} unless($$patron{$_}); 
63 }
64
65 my @categories=C4::Category->all;
66 my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
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=>1},{firstname=>1});
93 }
94 warn Data::Dumper::Dumper(@orderby);
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  #($results)=Search(\@searchpatron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  ) if (@searchpatron);
106  my $search_scope=($quicksearch?"field_start_with":"start_with");
107  ($results)=Search(\@searchpatron,\@orderby,undef,undef,["firstname","surname","email","othernames","cardnumber","userid"],$search_scope  ) if (@searchpatron);
108 if ($results){
109         $count =scalar(@$results);
110 }
111 my @resultsdata;
112 my $to=($count>$to?$to:$count);
113 my $index=$from;
114 foreach my $borrower(@$results[$from..$to-1]){
115   #find out stats
116   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
117
118   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
119
120   my %row = (
121     count => $index++,
122         %$borrower,
123         %{$categories_dislay{$$borrower{categorycode}}},
124     overdues => $od,
125     issues => $issue,
126     odissue => "$od/$issue",
127     fines =>  sprintf("%.2f",$fines),
128     );
129   push(@resultsdata, \%row);
130 }
131
132 if ($$patron{branchcode}){
133         foreach my $branch (grep{$_->{value} eq $$patron{branchcode}}@$branches){
134                 $$branch{selected}=1;
135         }
136 }
137 if ($$patron{categorycode}){
138         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
139                 $$category{selected}=1;
140         }
141 }
142 my %parameters=
143         (  %$patron
144                 , 'orderby'                     => $orderbyparams 
145                 , 'resultsperpage'      => $resultsperpage 
146         , 'type'=> 'intranet'); 
147 my $base_url =
148     'member.pl?&'
149   . join(
150     '&',
151     map { "$_=$parameters{$_}" } (keys %parameters)
152   );
153
154 $template->param(
155     paginationbar => pagination_bar(
156         $base_url,  int( $count / $resultsperpage ) + 1,
157         $startfrom, 'startfrom'
158     ),
159     startfrom => $startfrom,
160     from      => ($startfrom-1)*$resultsperpage+1,  
161     to        => $to,
162     multipage => ($count != $to+1 || $startfrom!=1),
163 );
164 $template->param(
165     branchloop=>$branches,
166         categoryloop=>\@categories,
167 );
168
169
170 $template->param( 
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;