Bug 8796 - patron cards not starting on designated card
[koha.git] / patroncards / members-search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Debug;
29
30 my $cgi = CGI->new;
31
32 my $batch_id = $cgi->param('batch_id') || 0;
33 my $startfrom = $cgi->param('startfrom')||1;
34 my $resultsperpage = $cgi->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
35 my $category = $cgi->param('category') || undef;
36 my $member = $cgi->param('member') || '';
37 my $orderby = $cgi->param('orderby') || undef;
38 my $not_attributes = $cgi->param('not_attributes') || undef;
39
40 my @categories=C4::Category->all;
41 my %categories_display;
42 my $no_categories;
43
44 foreach my $category (@categories) {
45     my $hash={
46         category_description=>$$category{description},
47         category_type=>$$category{category_type}
48     };
49     $categories_display{$$category{categorycode}} = $hash;
50 }
51
52
53 my ($template, $loggedinuser, $cookie) = get_template_and_user({
54                 template_name => "patroncards/members-search.tt",
55                 query => $cgi,
56                 type => "intranet",
57                 authnotrequired => 0,
58                 flagsrequired => {borrowers => 1},
59                 debug => 1,});
60
61 if(scalar(@categories) < 1){ $no_categories = 1; }
62 if($no_categories && C4::Context->preference("AddPatronLists")=~/code/){
63     $template->param(no_categories => 1);
64 } else {
65     $template->param(
66         categories=>\@categories,
67         category => $category
68     );
69 }
70
71 $orderby = "surname,firstname" unless $orderby;
72 $member =~ s/,//g;   #remove any commas from search string
73 $member =~ s/\*/%/g;
74
75 if ($member || $category) {
76     my $results = $category ? Search({''=>$member, categorycode=>$category}, $orderby, undef, undef, undef, undef, $not_attributes )
77                             : Search($member, $orderby, undef, undef, undef, undef, $not_attributes);
78     my $count = $results ? @$results : 0;
79
80     my @resultsdata = ();
81     my $to = ($count>($startfrom * $resultsperpage)?$startfrom * $resultsperpage:$count);
82     for (my $i = ($startfrom-1) * $resultsperpage; $i < $to; $i++){
83         #find out stats
84         my ($od,$issue,$fines) = GetMemberIssuesAndFines($results->[$i]{'borrowernumber'});
85         my %row = (
86             count               => $i + 1,
87                 %{$categories_display{$results->[$i]{categorycode}}},
88             borrowernumber      => $results->[$i]{'borrowernumber'},
89             cardnumber          => $results->[$i]{'cardnumber'},
90             surname             => $results->[$i]{'surname'},
91             firstname           => $results->[$i]{'firstname'},
92             categorycode        => $results->[$i]{'categorycode'},
93             address             => $results->[$i]{'address'},
94             address2            => $results->[$i]{'address2'},
95             city                => $results->[$i]{'city'},
96             zipcode             => $results->[$i]{'zipcode'},
97             country             => $results->[$i]{'country'},
98             branchcode          => $results->[$i]{'branchcode'},
99             overdues            => $od,
100             issues              => $issue,
101             odissue             => "$od/$issue",
102             fines               => ($fines ? sprintf("%.2f",$fines) : ''),
103             borrowernotes       => $results->[$i]{'borrowernotes'},
104             sort1               => $results->[$i]{'sort1'},
105             sort2               => $results->[$i]{'sort2'},
106             dateexpiry          => C4::Dates->new($results->[$i]{'dateexpiry'},'iso')->output('syspref'),
107         );
108         push(@resultsdata, \%row);
109     }
110     my $base_url = '?' . join('&amp;', map { $_->{term} . '=' . $_->{val} } (
111                                             { term => 'member',         val => $member         },
112                                             { term => 'category',       val => $category       },
113                                             { term => 'orderby',        val => $orderby        },
114                                             { term => 'resultsperpage', val => $resultsperpage },
115                                             { term => 'batch_id',       val => $batch_id       },)
116                                         );
117     $template->param(
118         paginationbar   => pagination_bar(
119                                             $base_url,  int( $count / $resultsperpage ) + 1,
120                                             $startfrom, 'startfrom'
121                                          ),
122         startfrom       => $startfrom,
123         from            => ($startfrom-1) * $resultsperpage + 1,
124         to              => $to,
125         multipage       => ($count != $to || $startfrom != 1),
126         searching       => "1",
127         member          => $member,
128         category_type   => $category,
129         numresults      => $count,
130         resultsloop     => \@resultsdata,
131         batch_id        => $batch_id,
132     );
133 }
134 else {
135     $template->param( batch_id => $batch_id);
136 }
137
138 $template->param( 'alphabet' => C4::Context->preference('alphabet') || join ' ', 'A' .. 'Z' );
139
140 output_html_with_http_headers $cgi, $cookie, $template->output;
141
142 __END__
143
144 #script to do a borrower enquiry/bring up borrower details etc
145 #written 20/12/99 by chris@katipo.co.nz
146
147