Bug 8435: DBRev 3.13.00.038
[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 Modern::Perl;
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 Koha::DateUtils;
34 use File::Basename;
35 use Koha::List::Patron;
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 $add_to_patron_list       = $input->param('add_to_patron_list');
53 my $add_to_patron_list_which = $input->param('add_to_patron_list_which');
54 my $new_patron_list          = $input->param('new_patron_list');
55 my @borrowernumbers          = $input->param('borrowernumber');
56 $input->delete(
57     'add_to_patron_list', 'add_to_patron_list_which',
58     'new_patron_list',    'borrowernumber',
59 );
60
61 my $patron = $input->Vars;
62 foreach (keys %$patron){
63         delete $$patron{$_} unless($$patron{$_});
64 }
65 my @categories=C4::Category->all;
66
67 my $branches = GetBranches;
68 my @branchloop;
69
70 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
71   my $selected;
72   $selected = 1 if $patron->{branchcode} && $branches->{$_}->{branchcode} eq $patron->{branchcode};
73   my %row = ( value => $_,
74         selected => $selected,
75         branchname => $branches->{$_}->{branchname},
76       );
77   push @branchloop, \%row;
78 }
79
80 my %categories_dislay;
81
82 foreach my $category (@categories){
83         my $hash={
84                         category_description=>$$category{description},
85                         category_type=>$$category{category_type}
86                          };
87         $categories_dislay{$$category{categorycode}} = $hash;
88 }
89 my $AddPatronLists = C4::Context->preference("AddPatronLists") || '';
90 $template->param( 
91         "AddPatronLists_$AddPatronLists" => "1",
92             );
93 if ($AddPatronLists=~/code/){
94     $categories[0]->{'first'}=1;
95 }  
96
97 my $member=$input->param('member') || '';
98 my $orderbyparams=$input->param('orderby') || '';
99 my @orderby;
100 if ($orderbyparams){
101         my @orderbyelt=split(/,/,$orderbyparams);
102         push @orderby, {$orderbyelt[0]=>$orderbyelt[1]||0};
103 }
104 else {
105         @orderby = ({surname=>0},{firstname=>0});
106 }
107
108 $member =~ s/,//g;   #remove any commas from search string
109 $member =~ s/\*/%/g;
110
111 my $from = ( $startfrom - 1 ) * $resultsperpage;
112 my $to   = $from + $resultsperpage;
113
114 my ($count,$results);
115 if ($member || keys %$patron) {
116     my $searchfields = $input->param('searchfields') || '';
117     my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
118
119     if ( $searchfields eq "dateofbirth" ) {
120         $member = output_pref({dt => dt_from_string($member), dateformat => 'iso', dateonly => 1});
121     }
122
123     my $searchtype = $input->param('searchtype');
124     my $search_scope =
125         $quicksearch ? "field_start_with"
126       : $searchtype  ? $searchtype
127       :                "start_with";
128
129     ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
130 }
131
132 if ($add_to_patron_list) {
133     my $patron_list;
134
135     if ( $add_to_patron_list eq 'new' ) {
136         $patron_list = AddPatronList( { name => $new_patron_list } );
137     }
138     else {
139         $patron_list =
140           [ GetPatronLists( { patron_list_id => $add_to_patron_list } ) ]->[0];
141     }
142
143     if ( $add_to_patron_list_which eq 'all' ) {
144         @borrowernumbers = map { $_->{borrowernumber} } @$results;
145     }
146
147     my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } );
148
149     $template->param(
150         patron_list           => $patron_list,
151         patrons_added_to_list => \@patrons_added_to_list,
152       )
153 }
154
155 if ($results) {
156         for my $field ('categorycode','branchcode'){
157                 next unless ($patron->{$field});
158                 @$results = grep { $_->{$field} eq $patron->{$field} } @$results; 
159         }
160     $count = scalar(@$results);
161 } else {
162     $count = 0;
163 }
164
165 if($count == 1){
166     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
167     exit;
168 }
169
170 my @resultsdata;
171 $to=($count>$to?$to:$count);
172 my $index=$from;
173 foreach my $borrower(@$results[$from..$to-1]){
174   #find out stats
175   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
176   $fines ||= 0;
177   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
178
179   my %row = (
180     count => $index++,
181     %$borrower,
182     (defined $categories_dislay{ $borrower->{categorycode} }?   %{ $categories_dislay{ $borrower->{categorycode} } }:()),
183     overdues => $od,
184     issues => $issue,
185     odissue => "$od/$issue",
186     fines =>  sprintf("%.2f",$fines),
187     branchname => $branches->{$borrower->{branchcode}}->{branchname},
188     );
189   push(@resultsdata, \%row);
190 }
191
192 if ($$patron{categorycode}){
193         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
194                 $$category{selected}=1;
195         }
196 }
197 my %parameters=
198         (  %$patron
199                 , 'orderby'                     => $orderbyparams 
200                 , 'resultsperpage'      => $resultsperpage 
201         , 'type'=> 'intranet'); 
202 my $base_url =
203     'member.pl?&'
204   . join(
205     '&',
206     map { "$_=$parameters{$_}" } (keys %parameters)
207   );
208
209 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
210
211 $template->param(
212     %$patron,
213     letters       => \@letters,
214     paginationbar => pagination_bar(
215         $base_url,
216         int( $count / $resultsperpage ) + ( $count % $resultsperpage ? 1 : 0 ),
217         $startfrom,
218         'startfrom'
219     ),
220     startfrom    => $startfrom,
221     from         => ( $startfrom - 1 ) * $resultsperpage + 1,
222     to           => $to,
223     multipage    => ( $count != $to || $startfrom != 1 ),
224     advsearch    => ( $$patron{categorycode} || $$patron{branchcode} ),
225     branchloop   => \@branchloop,
226     categories   => \@categories,
227     searching    => "1",
228     actionname   => basename($0),
229     numresults   => $count,
230     resultsloop  => \@resultsdata,
231     results_per_page => $resultsperpage,
232     member => $member,
233     search_parameters => \%parameters,
234     patron_lists => [ GetPatronLists() ],
235 );
236
237 output_html_with_http_headers $input, $cookie, $template->output;