Merge branch 'new/bug_6479' into kcmaster
[koha.git] / members / member.pl
blob724c99655b70aa8d5f0ba37466d8b57b9920c7e9
1 #!/usr/bin/perl
4 #script to do a borrower enquiry/bring up borrower details etc
5 #written 20/12/99 by chris@katipo.co.nz
8 # Copyright 2000-2002 Katipo Communications
9 # Copyright 2010 BibLibre
11 # This file is part of Koha.
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.
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.
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.
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;
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;
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 });
49 my $theme = $input->param('theme') || "default";
51 my $patron = $input->Vars;
52 foreach (keys %$patron){
53 delete $$patron{$_} unless($$patron{$_});
55 my @categories=C4::Category->all;
57 my $branches = GetBranches;
58 my @branchloop;
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},
66 push @branchloop, \%row;
69 my %categories_dislay;
71 foreach my $category (@categories){
72 my $hash={
73 category_description=>$$category{description},
74 category_type=>$$category{category_type}
76 $categories_dislay{$$category{categorycode}} = $hash;
78 $template->param(
79 "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
81 if (C4::Context->preference("AddPatronLists")=~/code/){
82 $categories[0]->{'first'}=1;
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};
92 else {
93 @orderby = ({surname=>0},{firstname=>0});
96 $member =~ s/,//g; #remove any commas from search string
97 $member =~ s/\*/%/g;
99 my ($count,$results);
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;
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);
111 if ($results) {
112 for my $field ('categorycode','branchcode'){
113 next unless ($patron->{$field});
114 @$results = grep { $_->{$field} eq $patron->{$field} } @$results;
116 $count = scalar(@$results);
119 if($count == 1){
120 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
121 exit;
124 my @resultsdata;
125 $to=($count>$to?$to:$count);
126 my $index=$from;
127 foreach my $borrower(@$results[$from..$to-1]){
128 #find out stats
129 my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
131 $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
133 my %row = (
134 count => $index++,
135 %$borrower,
136 %{$categories_dislay{$$borrower{categorycode}}},
137 overdues => $od,
138 issues => $issue,
139 odissue => "$od/$issue",
140 fines => sprintf("%.2f",$fines),
142 push(@resultsdata, \%row);
145 if ($$patron{categorycode}){
146 foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
147 $$category{selected}=1;
150 my %parameters=
151 ( %$patron
152 , 'orderby' => $orderbyparams
153 , 'resultsperpage' => $resultsperpage
154 , 'type'=> 'intranet');
155 my $base_url =
156 'member.pl?&'
157 . join(
158 '&',
159 map { "$_=$parameters{$_}" } (keys %parameters)
162 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
164 $template->param(
165 letters => \@letters,
166 paginationbar => pagination_bar(
167 $base_url,
168 int( $count / $resultsperpage ) + ($count % $resultsperpage ? 1 : 0),
169 $startfrom, 'startfrom'
171 startfrom => $startfrom,
172 from => ($startfrom-1)*$resultsperpage+1,
173 to => $to,
174 multipage => ($count != $to || $startfrom!=1),
175 advsearch => ($$patron{categorycode} || $$patron{branchcode}),
176 branchloop=>\@branchloop,
177 categories=>\@categories,
178 searching => "1",
179 actionname =>basename($0),
180 %$patron,
181 numresults => $count,
182 resultsloop => \@resultsdata,
185 output_html_with_http_headers $input, $cookie, $template->output;