Bug 22134: (follow-up) Simplify check for expired patron
[koha.git] / serials / serials-search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 Koha Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
21 =head1 NAME
22
23 serials-search.pl
24
25 =head1 DESCRIPTION
26
27 this script is the search page for serials
28
29 =cut
30
31 use Modern::Perl;
32 use CGI qw ( -utf8 );
33 use C4::Auth;
34 use C4::Context;
35 use C4::Koha qw( GetAuthorisedValues );
36 use C4::Output;
37 use C4::Serials;
38 use Koha::AdditionalField;
39
40 use Koha::DateUtils;
41 use Koha::SharedContent;
42
43 my $query         = new CGI;
44 my $title         = $query->param('title_filter') || '';
45 my $ISSN          = $query->param('ISSN_filter') || '';
46 my $EAN           = $query->param('EAN_filter') || '';
47 my $callnumber    = $query->param('callnumber_filter') || '';
48 my $publisher     = $query->param('publisher_filter') || '';
49 my $bookseller    = $query->param('bookseller_filter') || '';
50 my $biblionumber  = $query->param('biblionumber') || '';
51 my $branch        = $query->param('branch_filter') || '';
52 my $location      = $query->param('location_filter') || '';
53 my $expiration_date = $query->param('expiration_date_filter') || '';
54 my $routing       = $query->param('routing') || C4::Context->preference("RoutingSerials");
55 my $searched      = $query->param('searched') || 0;
56 my $mana      = $query->param('mana') || 0;
57 my @subscriptionids = $query->multi_param('subscriptionid');
58 my $op            = $query->param('op');
59
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {
62         template_name   => "serials/serials-search.tt",
63         query           => $query,
64         type            => "intranet",
65         authnotrequired => 0,
66         flagsrequired   => { serials => '*' },
67         debug           => 1,
68     }
69 );
70
71 if ( $op and $op eq "close" ) {
72     for my $subscriptionid ( @subscriptionids ) {
73         C4::Serials::CloseSubscription( $subscriptionid );
74     }
75 } elsif ( $op and $op eq "reopen" ) {
76     for my $subscriptionid ( @subscriptionids ) {
77         C4::Serials::ReopenSubscription( $subscriptionid );
78     }
79 }
80
81
82 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription', searchable => 1 } );
83 my $additional_field_filters;
84 for my $field ( @$additional_fields ) {
85     my $filter_value = $query->param('additional_field_' . $field->{id} . '_filter');
86     if ( defined $filter_value and $filter_value ne q|| ) {
87         $additional_field_filters->{ $field->{name} } = {
88             value => $filter_value,
89             authorised_value_category => $field->{authorised_value_category},
90         };
91     }
92     if ( $field->{authorised_value_category} ) {
93         $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
94     }
95 }
96
97 my $expiration_date_dt = $expiration_date ? dt_from_string( $expiration_date ) : undef;
98 my @subscriptions;
99 my $mana_statuscode;
100 if ($searched){
101     if ($mana) {
102         my $result = Koha::SharedContent::search_entities("subscription",{
103             title        => $title,
104             issn         => $ISSN,
105             ean          => $EAN,
106             publisher    => $publisher
107         });
108         $mana_statuscode = $result->{code};
109         @subscriptions = @{ $result->{data} };
110     }
111     else {
112         @subscriptions = SearchSubscriptions(
113         {
114             biblionumber => $biblionumber,
115             title        => $title,
116             issn         => $ISSN,
117             ean          => $EAN,
118             callnumber   => $callnumber,
119             publisher    => $publisher,
120             bookseller   => $bookseller,
121             branch       => $branch,
122             additional_fields => [ map{ { name => $_, value => $additional_field_filters->{$_}{value}, authorised_value_category => $additional_field_filters->{$_}{authorised_value_category} } } keys %$additional_field_filters ],
123             location     => $location,
124             expiration_date => $expiration_date_dt,
125         });
126     }
127 }
128
129 if ($mana) {
130     $template->param(
131         subscriptions => \@subscriptions,
132         statuscode    => $mana_statuscode,
133         total         => scalar @subscriptions,
134         title_filter  => $title,
135         ISSN_filter   => $ISSN,
136         EAN_filter    => $EAN,
137         callnumber_filter => $callnumber,
138         publisher_filter => $publisher,
139         bookseller_filter  => $bookseller,
140         branch_filter => $branch,
141         location_filter => $location,
142         expiration_date_filter => $expiration_date_dt,
143         done_searched => $searched,
144         routing       => $routing,
145         additional_field_filters => $additional_field_filters,
146         additional_fields_for_subscription => $additional_fields,
147         marcflavour   => (uc(C4::Context->preference("marcflavour"))),
148         mana => $mana,
149         search_only => 1
150     );
151 }
152 else
153 {
154     # to toggle between create or edit routing list options
155     if ($routing) {
156         for my $subscription ( @subscriptions) {
157             $subscription->{routingedit} = check_routing( $subscription->{subscriptionid} );
158         }
159     }
160
161     my (@openedsubscriptions, @closedsubscriptions);
162     for my $sub ( @subscriptions ) {
163         unless ( $sub->{closed} ) {
164             push @openedsubscriptions, $sub
165                 unless $sub->{cannotdisplay};
166         } else {
167             push @closedsubscriptions, $sub
168                 unless $sub->{cannotdisplay};
169         }
170     }
171
172     my @branches = Koha::Libraries->search( {}, { order_by => ['branchcode'] } );
173     my @branches_loop;
174     foreach my $b ( @branches ) {
175         my $selected = 0;
176         $selected = 1 if( defined $branch and $branch eq $b->branchcode );
177         push @branches_loop, {
178             branchcode  => $b->branchcode,
179             branchname  => $b->branchname,
180             selected    => $selected,
181         };
182     }
183
184     $template->param(
185         openedsubscriptions => \@openedsubscriptions,
186         closedsubscriptions => \@closedsubscriptions,
187         total         => @openedsubscriptions + @closedsubscriptions,
188         title_filter  => $title,
189         ISSN_filter   => $ISSN,
190         EAN_filter    => $EAN,
191         callnumber_filter => $callnumber,
192         publisher_filter => $publisher,
193         bookseller_filter  => $bookseller,
194         branch_filter => $branch,
195         location_filter => $location,
196         expiration_date_filter => $expiration_date_dt,
197         branches_loop => \@branches_loop,
198         done_searched => $searched,
199         routing       => $routing,
200         additional_field_filters => $additional_field_filters,
201         additional_fields_for_subscription => $additional_fields,
202         marcflavour   => (uc(C4::Context->preference("marcflavour"))),
203         mana => $mana
204     );
205 }
206 output_html_with_http_headers $query, $cookie, $template->output;