Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / serials / checkexpiration.pl
1 #!/usr/bin/perl
2
3 #
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19
20 =head1 NAME
21
22 checkexpiration.pl
23
24 =head1 DESCRIPTION
25
26 This script check what subscription will expire before C<$datenumber $datelimit>
27
28 =head1 PARAMETERS
29
30 =over 4
31
32 =item title
33     To filter subscription on title
34
35 =item issn
36     To filter subscription on issn
37
38 =item date
39 The date to filter on.
40
41 =back
42
43 =cut
44
45 use Modern::Perl;
46 use CGI qw ( -utf8 );
47 use C4::Auth;
48 use C4::Serials; # GetExpirationDate
49 use C4::Output;
50 use C4::Context;
51 use Koha::DateUtils;
52
53 use DateTime;
54
55 my $query = CGI->new;
56
57 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user (
58     {
59         template_name   => "serials/checkexpiration.tt",
60         query           => $query,
61         type            => "intranet",
62         flagsrequired   => { serials => 'check_expiration' },
63     }
64 );
65
66 my $title = $query->param('title');
67 my $issn  = $query->param('issn');
68 my $branch = $query->param('branch');
69 my $date = $query->param('date');
70 $date = eval { dt_from_string( scalar $query->param('date') ) } if $date;
71 my $showhistoricexpired = $query->param('showhistoricexpired');
72
73 if ($date) {
74     my @subscriptions = SearchSubscriptions({ title => $title, issn => $issn, orderby => 'title' });
75     my @subscriptions_loop;
76
77     foreach my $subscription ( @subscriptions ) {
78         my $subscriptionid = $subscription->{'subscriptionid'};
79         my $expirationdate = GetExpirationDate($subscriptionid);
80
81         $subscription->{expirationdate} = $expirationdate;
82
83         next if $expirationdate !~ /\d{4}-\d{2}-\d{2}/; # next if not in ISO format.
84
85         next if $subscription->{closed};
86         if (   !C4::Context->preference("IndependentBranches")
87             or C4::Context->IsSuperLibrarian()
88             or ( ref $flags->{serials} and $flags->{serials}->{superserials} )
89             or ( !ref $flags->{serials} and $flags->{serials} == 1 ) )
90         {
91             $subscription->{cannotedit} = 0;
92         }
93         next if $subscription->{cannotedit};
94
95         my $expirationdate_dt = dt_from_string( $expirationdate, 'iso' );
96         my $today_dt = dt_from_string();
97         if (   DateTime->compare( $date, $expirationdate_dt ) == 1
98             && ( $showhistoricexpired || DateTime->compare( $expirationdate_dt, $today_dt ) == 1 )
99             && ( !$branch || ( $subscription->{'branchcode'} eq $branch ) ) ) {
100             push @subscriptions_loop, $subscription;
101         }
102     }
103
104     $template->param(
105         title               => $title,
106         issn                => $issn,
107         showhistoricexpired => $showhistoricexpired,
108         numsubscription     => scalar @subscriptions_loop,
109         date                => $date,
110         subscriptions_loop  => \@subscriptions_loop,
111         "BiblioDefaultView" . C4::Context->preference("BiblioDefaultView") => 1,
112         searched                                                           => 1,
113     );
114 }
115
116 my $can_change_library;;
117 if (  !C4::Context->preference("IndependentBranches")
118     or C4::Context->IsSuperLibrarian()
119     or ( ref $flags->{serials}  and $flags->{serials}->{superserials} )
120     or ( !ref $flags->{serials} and $flags->{serials} == 1 ) )
121 {
122     $can_change_library = 1;
123 }
124
125 $template->param (
126     (uc(C4::Context->preference("marcflavour"))) => 1,
127     can_change_library => $can_change_library,
128     branch => $branch,
129 );
130
131 output_html_with_http_headers $query, $cookie, $template->output;