Add patron card member searching by category code
[koha.git] / labels / pcard-member-search.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 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use C4::Auth;
27 use C4::Output;
28 use CGI;
29 use C4::Members;
30 use C4::Debug;
31
32
33 my $input = new CGI;
34 my $batch_id    = $input->param('batch_id');
35 my $batch_type  = $input->param('type');
36
37 $debug and warn "[In pcard-member-search] Batch Id: $batch_id, and Type: $batch_type";
38
39 my $quicksearch = $input->param('quicksearch');
40 my $startfrom = $input->param('startfrom')||1;
41 my $resultsperpage = $input->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
42 my $category = $input->param('category');
43
44 my ($template, $loggedinuser, $cookie);
45 if($quicksearch){
46     ($template, $loggedinuser, $cookie)
47     = get_template_and_user({template_name => "members/member-quicksearch-results.tmpl",        # FIXME: template doesn't exist
48                  query => $input,
49                  type => "intranet",
50                  authnotrequired => 0,
51                  flagsrequired => {borrowers => 1},
52                  debug => 1,
53                  });
54 } else {
55     ($template, $loggedinuser, $cookie)
56     = get_template_and_user({template_name => "labels/pcard-members-search.tmpl",
57                  query => $input,
58                  type => "intranet",
59                  authnotrequired => 0,
60                  flagsrequired => {borrowers => 1},
61                  debug => 1,
62                  });
63 }
64 my $theme = $input->param('theme') || "default";
65             # only used if allowthemeoverride is set
66 #my %tmpldata = pathtotemplate ( template => 'member.tmpl', theme => $theme, language => 'fi' );
67     # FIXME - Error-checking
68 #my $template = HTML::Template->new( filename => $tmpldata{'path'},
69 #                   die_on_bad_params => 0,
70 #                   loop_context_vars => 1 );
71
72
73 my $member=$input->param('member');
74 my $orderby=$input->param('orderby');
75 $orderby = "surname,firstname" unless $orderby;
76 $member =~ s/,//g;   #remove any commas from search string
77 $member =~ s/\*/%/g;
78
79 unless ($member||$category) {
80     $template->param( batch_id => $batch_id, type => $batch_type,);
81     output_html_with_http_headers $input, $cookie, $template->output;
82     exit;
83 }
84
85 my ($count,$results);
86
87 if(length($member) == 1)
88 {
89     ($count,$results)=SearchMember($member,$orderby,"simple");
90 }
91 else
92 {
93     ($count,$results)=SearchMember($member,$orderby,"advanced",$category);
94 }
95
96
97 my @resultsdata;
98 my $toggle = 0;
99 my $to=($count>($startfrom*$resultsperpage)?$startfrom*$resultsperpage:$count);
100 for (my $i=($startfrom-1)*$resultsperpage; $i < $to; $i++){
101   #find out stats
102   my ($od,$issue,$fines)=GetMemberIssuesAndFines($results->[$i]{'borrowernumber'});
103
104   my %row = (
105     toggle => $toggle,
106     count => $i+1,
107     borrowernumber => $results->[$i]{'borrowernumber'},
108     cardnumber => $results->[$i]{'cardnumber'},
109     surname => $results->[$i]{'surname'},
110     firstname => $results->[$i]{'firstname'},
111     categorycode => $results->[$i]{'categorycode'},
112     category_type => $results->[$i]{'category_type'},
113     category_description => $results->[$i]{'description'},
114     address => $results->[$i]{'address'},
115         address2 => $results->[$i]{'address2'},
116     city => $results->[$i]{'city'},
117         zipcode => $results->[$i]{'zipcode'},
118     branchcode => $results->[$i]{'branchcode'},
119     overdues => $od,
120     issues => $issue,
121     odissue => "$od/$issue",
122     fines =>  sprintf("%.2f",$fines),
123     borrowernotes => $results->[$i]{'borrowernotes'},
124     sort1 => $results->[$i]{'sort1'},
125     sort2 => $results->[$i]{'sort2'},
126     dateexpiry => C4::Dates->new($results->[$i]{'dateexpiry'},'iso')->output('syspref'),
127     );
128   if ( $toggle ) { $toggle = 0; } else {$toggle = 1; }
129   push(@resultsdata, \%row);
130 }
131 my $base_url =
132     'pcard-member-search.pl?'
133   . join(
134     '&amp;',
135     map { $_->{term} . '=' . $_->{val} } (
136         { term => 'member',         val => $member         },
137         { term => 'category',         val => $category         },
138         { term => 'orderby',        val => $orderby        },
139         { term => 'resultsperpage', val => $resultsperpage },
140         { term => 'type',           val => $batch_type     },
141         { term => 'batch_id',       val => $batch_id       },
142     )
143   );
144
145 $template->param(
146     paginationbar => pagination_bar(
147         $base_url,  int( $count / $resultsperpage ) + 1,
148         $startfrom, 'startfrom'
149     ),
150     startfrom => $startfrom,
151     from      => ($startfrom-1)*$resultsperpage+1,  
152     to        => $to,
153     multipage => ($count != $to || $startfrom!=1),
154 );
155
156 $template->param( 
157         searching       => "1",
158         member          => $member,
159         numresults      => $count,
160         resultsloop     => \@resultsdata,
161         batch_id        => $batch_id,
162         type            => $batch_type,
163             );
164
165 output_html_with_http_headers $input, $cookie, $template->output;