Bug 8918: Fix reserve priority in ILS-DI
[koha.git] / members / member.pl
blobff6e41ad21b7f01c30526777804f0bca98a586d7
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 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;
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;
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 });
50 my $theme = $input->param('theme') || "default";
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',
61 my $patron = $input->Vars;
62 foreach (keys %$patron){
63 delete $$patron{$_} unless($$patron{$_});
65 my @categories=C4::Category->all;
67 my $branches = GetBranches;
68 my @branchloop;
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},
77 push @branchloop, \%row;
80 my %categories_dislay;
82 foreach my $category (@categories){
83 my $hash={
84 category_description=>$$category{description},
85 category_type=>$$category{category_type}
87 $categories_dislay{$$category{categorycode}} = $hash;
89 my $AddPatronLists = C4::Context->preference("AddPatronLists") || '';
90 $template->param(
91 "AddPatronLists_$AddPatronLists" => "1",
93 if ($AddPatronLists=~/code/){
94 $categories[0]->{'first'}=1;
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};
104 else {
105 @orderby = ({surname=>0},{firstname=>0});
108 $member =~ s/,//g; #remove any commas from search string
109 $member =~ s/\*/%/g;
111 my $from = ( $startfrom - 1 ) * $resultsperpage;
112 my $to = $from + $resultsperpage;
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" );
119 if ( $searchfields eq "dateofbirth" ) {
120 $member = output_pref({dt => dt_from_string($member), dateformat => 'iso', dateonly => 1});
123 my $searchtype = $input->param('searchtype');
124 my $search_scope =
125 $quicksearch ? "field_start_with"
126 : $searchtype ? $searchtype
127 : "start_with";
129 ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
132 if ($add_to_patron_list) {
133 my $patron_list;
135 if ( $add_to_patron_list eq 'new' ) {
136 $patron_list = AddPatronList( { name => $new_patron_list } );
138 else {
139 $patron_list =
140 [ GetPatronLists( { patron_list_id => $add_to_patron_list } ) ]->[0];
143 if ( $add_to_patron_list_which eq 'all' ) {
144 @borrowernumbers = map { $_->{borrowernumber} } @$results;
147 my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } );
149 $template->param(
150 patron_list => $patron_list,
151 patrons_added_to_list => \@patrons_added_to_list,
155 if ($results) {
156 for my $field ('categorycode','branchcode'){
157 next unless ($patron->{$field});
158 @$results = grep { $_->{$field} eq $patron->{$field} } @$results;
160 $count = scalar(@$results);
161 } else {
162 $count = 0;
165 if($count == 1){
166 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
167 exit;
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');
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},
189 push(@resultsdata, \%row);
192 if ($$patron{categorycode}){
193 foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
194 $$category{selected}=1;
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)
209 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
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'
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() ],
237 output_html_with_http_headers $input, $cookie, $template->output;