Bug 8015: (follow-up) don't require userenv
[koha.git] / circ / renew.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 ByWater Solutions
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 3 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 use Modern::Perl;
21
22 use CGI;
23 use C4::Context;
24 use C4::Auth qw/:DEFAULT get_session/;
25 use C4::Output;
26 use C4::Circulation;
27 use Koha::DateUtils;
28 use Koha::Database;
29
30 my $cgi = new CGI;
31
32 my ( $template, $librarian, $cookie ) = get_template_and_user(
33     {
34         template_name   => "circ/renew.tmpl",
35         query           => $cgi,
36         type            => "intranet",
37         authnotrequired => 0,
38         flagsrequired   => { circulate => "circulate_remaining_permissions" },
39     }
40 );
41
42 my $schema = Koha::Database->new()->schema();
43
44 my $barcode        = $cgi->param('barcode');
45 my $override_limit = $cgi->param('override_limit');
46 my $override_holds = $cgi->param('override_holds');
47
48 my ( $item, $issue, $borrower );
49 my $error = q{};
50
51 if ($barcode) {
52     $item = $schema->resultset("Item")->single( { barcode => $barcode } );
53
54     if ($item) {
55
56         $issue = $item->issues()->single();
57
58         if ($issue) {
59
60             $borrower = $issue->borrower();
61             
62             if ( ( $borrower->debarred() || q{} ) lt dt_from_string()->ymd() ) {
63                 my $can_renew;
64                 ( $can_renew, $error ) =
65                   CanBookBeRenewed( $borrower->borrowernumber(),
66                     $item->itemnumber(), $override_limit );
67
68                 if ( $error && ($error eq 'on_reserve') ) {
69                     if ($override_holds) {
70                         $can_renew = 1;
71                         $error     = undef;
72                     }
73                     else {
74                         $can_renew = 0;
75                     }
76                 }
77
78                 if ($can_renew) {
79                     my $date_due = AddRenewal( undef, $item->itemnumber() );
80                     $template->param( date_due => $date_due );
81                 }
82             }
83             else {
84                 $error = "patron_restricted";
85             }
86         }
87         else {
88             $error = "no_checkout";
89         }
90     }
91     else {
92         $error = "no_item";
93     }
94
95     $template->param(
96         item     => $item,
97         issue    => $issue,
98         borrower => $borrower,
99         error    => $error
100     );
101 }
102
103 output_html_with_http_headers( $cgi, $cookie, $template->output );