Merge branch 'bug2505_patches' of git://git.catalyst.net.nz/koha into to-push
[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 use YAML;
35
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;
40
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                  });
48
49 my $theme = $input->param('theme') || "default";
50
51 my $patron = $input->Vars;
52 foreach (keys %$patron){
53         delete $$patron{$_} unless($$patron{$_}); 
54 }
55
56 my @categories=C4::Category->all;
57 my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
58
59 my %categories_dislay;
60
61 foreach my $category (@categories){
62         my $hash={
63                         category_description=>$$category{description},
64                         category_type=>$$category{category_type}
65                          };
66         $categories_dislay{$$category{categorycode}} = $hash;
67 }
68 $template->param( 
69         "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
70             );
71 if (C4::Context->preference("AddPatronLists")=~/code/){
72     $categories[0]->{'first'}=1;
73 }  
74
75 my $member=$input->param('member');
76 my $orderbyparams=$input->param('orderby');
77 my @orderby;
78 if ($orderbyparams){
79         my @orderbyelt=split(/,/,$orderbyparams);
80         push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
81 }
82 else {
83         @orderby = ({firstname=>1},{surname=>1});
84 }
85
86 $member =~ s/,//g;   #remove any commas from search string
87 $member =~ s/\*/%/g;
88
89 my ($count,$results);
90
91 my @searchpatron;
92 push @searchpatron, $member if ($member);
93 push @searchpatron, $patron if (keys %$patron);
94 my $from= ($startfrom-1)*$resultsperpage;
95 my $to=$from+$resultsperpage;
96  #($results)=Search(\@searchpatron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  ) if (@searchpatron);
97  my $search_scope=($quicksearch?"field_start_with":"start_with");
98  ($results)=Search(\@searchpatron,\@orderby,undef,undef,["firstname","surname","email","othernames","cardnumber","userid"],$search_scope  ) if (@searchpatron);
99 if ($results){
100         $count =scalar(@$results);
101 }
102 my @resultsdata;
103 my $to=($count>$to?$to:$count);
104 my $index=$from;
105 foreach my $borrower(@$results[$from..$to-1]){
106   #find out stats
107   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
108
109   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
110
111   my %row = (
112     count => $index++,
113         %$borrower,
114         %{$categories_dislay{$$borrower{categorycode}}},
115     overdues => $od,
116     issues => $issue,
117     odissue => "$od/$issue",
118     fines =>  sprintf("%.2f",$fines),
119     );
120   push(@resultsdata, \%row);
121 }
122
123 if ($$patron{branchcode}){
124         foreach my $branch (grep{$_->{value} eq $$patron{branchcode}}@$branches){
125                 $$branch{selected}=1;
126         }
127 }
128 if ($$patron{categorycode}){
129         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
130                 $$category{selected}=1;
131         }
132 }
133 my %parameters=
134         (  %$patron
135                 , 'orderby'                     => $orderbyparams 
136                 , 'resultsperpage'      => $resultsperpage 
137         , 'type'=> 'intranet'); 
138 my $base_url =
139     'member.pl?&'
140   . join(
141     '&',
142     map { "$_=$parameters{$_}" } (keys %parameters)
143   );
144
145 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
146 $template->param( letters => \@letters );
147
148 $template->param(
149     paginationbar => pagination_bar(
150         $base_url,  int( $count / $resultsperpage ) + 1,
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         categoryloop=>\@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;