Bug 8845: Followup Allow to give a date using the syspref format
[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 # Copyright 2010 BibLibre
10 #
11 # This file is part of Koha.
12 #
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.
17 #
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.
21 #
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.
25
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 Koha::DateUtils;
35 use File::Basename;
36
37 my $input = new CGI;
38 my $quicksearch = $input->param('quicksearch');
39 my $startfrom = $input->param('startfrom')||1;
40 my $resultsperpage = $input->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
41
42 my ($template, $loggedinuser, $cookie)
43     = get_template_and_user({template_name => "members/member.tmpl",
44                  query => $input,
45                  type => "intranet",
46                  authnotrequired => 0,
47                  flagsrequired => {borrowers => 1},
48                  });
49
50 my $theme = $input->param('theme') || "default";
51
52 my $patron = $input->Vars;
53 foreach (keys %$patron){
54         delete $$patron{$_} unless($$patron{$_});
55 }
56 my @categories=C4::Category->all;
57
58 my $branches = GetBranches;
59 my @branchloop;
60
61 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
62   my $selected;
63   $selected = 1 if $branches->{$_}->{branchcode} eq $$patron{branchcode};
64   my %row = ( value => $_,
65         selected => $selected,
66         branchname => $branches->{$_}->{branchname},
67       );
68   push @branchloop, \%row;
69 }
70
71 my %categories_dislay;
72
73 foreach my $category (@categories){
74         my $hash={
75                         category_description=>$$category{description},
76                         category_type=>$$category{category_type}
77                          };
78         $categories_dislay{$$category{categorycode}} = $hash;
79 }
80 my $AddPatronLists = C4::Context->preference("AddPatronLists") || '';
81 $template->param( 
82         "AddPatronLists_$AddPatronLists" => "1",
83             );
84 if ($AddPatronLists=~/code/){
85     $categories[0]->{'first'}=1;
86 }  
87
88 my $member=$input->param('member');
89 my $orderbyparams=$input->param('orderby');
90 my @orderby;
91 if ($orderbyparams){
92         my @orderbyelt=split(/,/,$orderbyparams);
93         push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
94 }
95 else {
96         @orderby = ({surname=>0},{firstname=>0});
97 }
98
99 $member =~ s/,//g;   #remove any commas from search string
100 $member =~ s/\*/%/g;
101
102 my $from = ( $startfrom - 1 ) * $resultsperpage;
103 my $to   = $from + $resultsperpage;
104
105 my ($count,$results);
106 if ($member || keys %$patron) {
107     my $searchfields = $input->param('searchfields');
108     my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
109
110     if ( $searchfields eq "dateofbirth" ) {
111         $member = output_pref(dt_from_string($member), 'iso', 1);
112     }
113
114     my $search_scope = ( $quicksearch ? "field_start_with" : "start_with" );
115     ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
116 }
117
118 if ($results) {
119         for my $field ('categorycode','branchcode'){
120                 next unless ($patron->{$field});
121                 @$results = grep { $_->{$field} eq $patron->{$field} } @$results; 
122         }
123     $count = scalar(@$results);
124 }
125
126 if($count == 1){
127     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
128     exit;
129 }
130
131 my @resultsdata;
132 $to=($count>$to?$to:$count);
133 my $index=$from;
134 foreach my $borrower(@$results[$from..$to-1]){
135   #find out stats
136   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
137
138   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
139
140   my %row = (
141     count => $index++,
142     %$borrower,
143     (defined $categories_dislay{ $borrower->{categorycode} }?   %{ $categories_dislay{ $borrower->{categorycode} } }:()),
144     overdues => $od,
145     issues => $issue,
146     odissue => "$od/$issue",
147     fines =>  sprintf("%.2f",$fines),
148     branchname => $branches->{$borrower->{branchcode}}->{branchname},
149     );
150   push(@resultsdata, \%row);
151 }
152
153 if ($$patron{categorycode}){
154         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
155                 $$category{selected}=1;
156         }
157 }
158 my %parameters=
159         (  %$patron
160                 , 'orderby'                     => $orderbyparams 
161                 , 'resultsperpage'      => $resultsperpage 
162         , 'type'=> 'intranet'); 
163 my $base_url =
164     'member.pl?&'
165   . join(
166     '&',
167     map { "$_=$parameters{$_}" } (keys %parameters)
168   );
169
170 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
171
172 $template->param(
173     letters => \@letters,
174     paginationbar => pagination_bar(
175         $base_url,
176         int( $count / $resultsperpage ) + ($count % $resultsperpage ? 1 : 0),
177         $startfrom, 'startfrom'
178     ),
179     startfrom => $startfrom,
180     from      => ($startfrom-1)*$resultsperpage+1,  
181     to        => $to,
182     multipage => ($count != $to || $startfrom!=1),
183     advsearch => ($$patron{categorycode} || $$patron{branchcode}),
184     branchloop=>\@branchloop,
185     categories=>\@categories,
186     searching       => "1",
187                 actionname              =>basename($0),
188                 %$patron,
189         numresults      => $count,
190         resultsloop     => \@resultsdata,
191             );
192
193 output_html_with_http_headers $input, $cookie, $template->output;