Bug 2720 - Overdues which debar automatically should undebar automatically when returned
[koha.git] / serials / member-search.pl
1 #!/usr/bin/perl
2
3 # Parts copyright Catalyst IT 2010
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 =head1 member-search.pl
21
22 Member Search.pl script used to search for members to add to a routing list
23
24 =cut
25
26 use strict;
27 use warnings;
28 use CGI;
29 use C4::Auth;       # get_template_and_user
30 use C4::Output;
31 use C4::Members;    # BornameSearch
32 use C4::Branch;
33 use C4::Category;
34 use File::Basename;
35
36 my $cgi          = new CGI;
37 my $theme = $cgi->param('theme') || "default";
38 my $resultsperpage = $cgi->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
39 my $startfrom = $cgi->param('startfrom')||1;
40
41 my $patron = $cgi->Vars;
42 foreach (keys %$patron){
43     delete $$patron{$_} unless($$patron{$_});
44 }
45
46 my @categories=C4::Category->all;
47 my $branches=(defined $$patron{branchcode}?GetBranchesLoop($$patron{branchcode}):GetBranchesLoop());
48 my $subscriptionid = $cgi->param('subscriptionid');
49 my $searchstring   = $cgi->param('member');
50
51 my %categories_dislay;
52 my ($template, $loggedinuser, $cookie);
53     ($template, $loggedinuser, $cookie)
54     = get_template_and_user({template_name => "serials/member-search.tmpl",
55                  query => $cgi,
56                  type => "intranet",
57                  authnotrequired => 0,
58                  flagsrequired => { serials => 'routing' },
59                  });
60
61 foreach my $category (@categories){
62         my $hash={
63                         category_description=>$$category{description},
64                         category_type=>$$category{category_type}
65                          };
66         $categories_dislay{$$category{categorycode}} = $hash;
67 }
68 $template->param(
69         "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
70             );
71 if (C4::Context->preference("AddPatronLists")=~/code/){
72     $categories[0]->{'first'}=1;
73 }
74
75 my $member=$cgi->param('member');
76 my $orderby=$cgi->param('orderby');
77 $orderby = "surname,firstname" unless $orderby;
78 if (defined $member) {
79     $member =~ s/,//g;   #remove any commas from search string
80     $member =~ s/\*/%/g;
81 }
82
83 my ($count,$results);
84
85 if (C4::Context->preference("IndependentBranches")){
86    if (C4::Context->userenv && C4::Context->userenv->{flags} % 2 !=1 && C4::Context->userenv->{'branch'}){
87         $$patron{branchcode}=C4::Context->userenv->{'branch'};
88    }
89 }
90 $$patron{firstname}.="\%" if ($$patron{firstname});
91 $$patron{surname}.="\%" if ($$patron{surname});
92
93 my @searchpatron;
94 push @searchpatron, $member if ($member);
95 push @searchpatron, $patron if ( keys %$patron );
96 my $from = ( $startfrom - 1 ) * $resultsperpage;
97 my $to   = $from + $resultsperpage;
98 if (@searchpatron) {
99     ($results) = Search(
100         \@searchpatron,
101         [ { surname => 0 }, { firstname => 0 } ],
102         undef,
103         undef,
104         [ "firstname", "surname", "email", "othernames", "cardnumber" ],
105         "start_with"
106     );
107 }
108 if ($results) {
109     $count = scalar(@$results);
110 }
111 my @resultsdata;
112 $to=($count>$to?$to:$count);
113 my $index=$from;
114 foreach my $borrower(@$results[$from..$to-1]){
115     # find out stats
116     $borrower->{'dateexpiry'}= C4::Dates->new($borrower->{'dateexpiry'},'iso')->output('syspref');
117     if ($categories_dislay{$borrower->{'categorycode'}}){
118         my %row = (
119             count => $index++,
120             %$borrower,
121             %{$categories_dislay{$$borrower{categorycode}}},
122         );
123         push(@resultsdata, \%row);
124     }
125     else {
126          warn $borrower->{'cardnumber'} ." has a bad category code of " . $borrower->{'categorycode'} ."\n";
127     }
128 }
129 if ($$patron{branchcode}){
130         foreach my $branch (grep{$_->{value} eq $$patron{branchcode}}@$branches){
131                 $$branch{selected}=1;
132         }
133 }
134 if ($$patron{categorycode}){
135         foreach my $category (grep{$_->{categorycode} eq $$patron{categorycode}}@categories){
136                 $$category{selected}=1;
137         }
138 }
139 my %parameters=
140 (  %{$patron},
141     'orderby' => $orderby,
142     'resultsperpage' => $resultsperpage,
143     'type'=> 'intranet');
144 my $base_url =
145     'member-search.pl?&'
146   . join(
147     '&',
148     map { "$_=$parameters{$_}" } (keys %parameters)
149   );
150
151 $template->param(
152     paginationbar => pagination_bar(
153         $base_url,  int( $count / $resultsperpage ) + 1,
154         $startfrom, 'startfrom'
155     ),
156     startfrom => $startfrom,
157     from      => ($startfrom-1)*$resultsperpage+1,
158     to        => $to,
159     multipage => ($count != $to+1 || $startfrom!=1),
160 );
161 $template->param(
162     branchloop=>$branches,
163         categoryloop=>\@categories,
164 );
165
166
167 $template->param(
168         searching       => "1",
169                 actionname              => basename($0),
170                 %$patron,
171         numresults      => $count,
172         resultsloop     => \@resultsdata,
173             );
174
175 output_html_with_http_headers $cgi, $cookie, $template->output;