Bug 8436: Add limit on branch in checkexpiration.pl
[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 strict;
46 use warnings;
47 use CGI qw ( -utf8 );
48 use C4::Auth;
49 use C4::Serials; # GetExpirationDate
50 use C4::Branch;
51 use C4::Output;
52 use C4::Context;
53 use C4::Dates qw/format_date format_date_in_iso/;
54 use Date::Calc qw/Today Date_to_Days/;
55
56 my $query = new CGI;
57
58 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user (
59     {
60         template_name   => "serials/checkexpiration.tt",
61         query           => $query,
62         type            => "intranet",
63         authnotrequired => 0,
64         flagsrequired   => { serials => 'check_expiration' },
65         debug           => 1,
66     }
67 );
68
69 my $title = $query->param('title');
70 my $issn  = $query->param('issn');
71 my $branch = $query->param('branch');
72 my $date  = format_date_in_iso($query->param('date'));
73
74 if ($date) {
75     my @subscriptions = SearchSubscriptions({ title => $title, issn => $issn, orderby => 'title' });
76     my @subscriptions_loop;
77
78     foreach my $subscription ( @subscriptions ) {
79         my $subscriptionid = $subscription->{'subscriptionid'};
80         my $expirationdate = GetExpirationDate($subscriptionid);
81         $subscription->{expirationdate} = $expirationdate;
82         next if $expirationdate !~ /\d{4}-\d{2}-\d{2}/; # next if not in ISO format.
83         next if $subscription->{closed};
84         if (   ( ref $flags->{serials} and $flags->{serials}->{superserials} )
85             or ( !ref $flags->{serials} and $flags->{serials} == 1 ) )
86         {
87             $subscription->{cannotedit} = 0;
88         }
89         next if $subscription->{cannotedit};
90         if ( Date_to_Days(split "-",$expirationdate) < Date_to_Days(split "-",$date) &&
91              Date_to_Days(split "-",$expirationdate) > Date_to_Days(&Today) &&
92              ( !$branch || ($subscription->{'branchcode'} eq $branch) ) ) {
93             $subscription->{expirationdate}=format_date($subscription->{expirationdate});
94             push @subscriptions_loop,$subscription;
95         }
96     }
97
98     $template->param (
99         title           => $title,
100         issn            => $issn,
101         numsubscription => scalar @subscriptions_loop,
102         date => format_date($date),
103         subscriptions_loop => \@subscriptions_loop,
104         "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
105     );
106 }
107
108 my $branchname;
109 my @branches_loop;
110 if (   $flags->{superlibrarian}
111     or ( ref $flags->{serials}  and $flags->{serials}->{superserials} )
112     or ( !ref $flags->{serials} and $flags->{serials} == 1 ) )
113 {
114     my $branches = GetBranches();
115     foreach my $b (sort keys %$branches) {
116         my $selected = 0;
117         if( $branch and $branch eq $b ){
118             $selected = 1;
119             $branchname = $branches->{$b}->{branchname};
120         }
121         push @branches_loop, {
122             branchcode => $b,
123             branchname => $branches->{$b}->{branchname},
124             selected   => $selected,
125         };
126     }
127 }
128
129 $template->param (
130     (uc(C4::Context->preference("marcflavour"))) => 1,
131     branches_loop   => \@branches_loop,
132     branchcode      => $branch,
133     branchname      => $branchname,
134 );
135
136 output_html_with_http_headers $query, $cookie, $template->output;