Bug 10565: (follow-up) refresh DBIC schema class files
[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;
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
39 my @categories=C4::Category->all;
40 my %categories_display;
41
42 foreach my $category (@categories) {
43     my $hash={
44         category_description=>$$category{description},
45         category_type=>$$category{category_type}
46     };
47     $categories_display{$$category{categorycode}} = $hash;
48 }
49
50 my ($template, $loggedinuser, $cookie) = get_template_and_user({
51                 template_name => "patroncards/members-search.tmpl",
52                 query => $cgi,
53                 type => "intranet",
54                 authnotrequired => 0,
55                 flagsrequired => {borrowers => 1},
56                 debug => 1,});
57
58 $orderby = "surname,firstname" unless $orderby;
59 $member =~ s/,//g;   #remove any commas from search string
60 $member =~ s/\*/%/g;
61
62 if ($member || $category) {
63     my $results = $category ? Search({''=>$member, categorycode=>$category}, $orderby)
64                             : Search($member, $orderby);
65     my $count = $results ? @$results : 0;
66
67     my @resultsdata = ();
68     my $to = ($count>($startfrom * $resultsperpage)?$startfrom * $resultsperpage:$count);
69     for (my $i = ($startfrom-1) * $resultsperpage; $i < $to; $i++){
70         #find out stats
71         my ($od,$issue,$fines) = GetMemberIssuesAndFines($results->[$i]{'borrowernumber'});
72         my %row = (
73             count               => $i + 1,
74                 %{$categories_display{$results->[$i]{categorycode}}},
75             borrowernumber      => $results->[$i]{'borrowernumber'},
76             cardnumber          => $results->[$i]{'cardnumber'},
77             surname             => $results->[$i]{'surname'},
78             firstname           => $results->[$i]{'firstname'},
79             categorycode        => $results->[$i]{'categorycode'},
80             address             => $results->[$i]{'address'},
81             address2            => $results->[$i]{'address2'},
82             city                => $results->[$i]{'city'},
83             zipcode             => $results->[$i]{'zipcode'},
84             country             => $results->[$i]{'country'},
85             branchcode          => $results->[$i]{'branchcode'},
86             overdues            => $od,
87             issues              => $issue,
88             odissue             => "$od/$issue",
89             fines               => ($fines ? sprintf("%.2f",$fines) : ''),
90             borrowernotes       => $results->[$i]{'borrowernotes'},
91             sort1               => $results->[$i]{'sort1'},
92             sort2               => $results->[$i]{'sort2'},
93             dateexpiry          => C4::Dates->new($results->[$i]{'dateexpiry'},'iso')->output('syspref'),
94         );
95         push(@resultsdata, \%row);
96     }
97     my $base_url = '?' . join('&amp;', map { $_->{term} . '=' . $_->{val} } (
98                                             { term => 'member',         val => $member         },
99                                             { term => 'category',       val => $category       },
100                                             { term => 'orderby',        val => $orderby        },
101                                             { term => 'resultsperpage', val => $resultsperpage },
102                                             { term => 'batch_id',       val => $batch_id       },)
103                                         );
104     $template->param(
105         paginationbar   => pagination_bar(
106                                             $base_url,  int( $count / $resultsperpage ) + 1,
107                                             $startfrom, 'startfrom'
108                                          ),
109         startfrom       => $startfrom,
110         from            => ($startfrom-1) * $resultsperpage + 1,
111         to              => $to,
112         multipage       => ($count != $to || $startfrom != 1),
113         searching       => "1",
114         member          => $member,
115         category_type   => $category,
116         numresults      => $count,
117         resultsloop     => \@resultsdata,
118         batch_id        => $batch_id,
119     );
120 }
121 else {
122     $template->param( batch_id => $batch_id);
123 }
124
125 $template->param( 'alphabet' => C4::Context->preference('alphabet') || join ' ', 'A' .. 'Z' );
126
127 output_html_with_http_headers $cgi, $cookie, $template->output;
128
129 __END__
130
131 #script to do a borrower enquiry/bring up borrower details etc
132 #written 20/12/99 by chris@katipo.co.nz
133
134