Bug 17698: (follow-up) Changing to Koha Objects style, adding circ sidebar
[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 qw ( -utf8 );
23 use C4::Context;
24 use C4::Auth qw/:DEFAULT get_session/;
25 use C4::Output;
26 use C4::Circulation;
27 use C4::Koha;
28 use Koha::DateUtils;
29 use Koha::Database;
30 use Koha::BiblioFrameworks;
31 use Koha::Checkouts;
32
33 my $cgi = new CGI;
34
35 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user(
36     {
37         template_name   => "circ/renew.tt",
38         query           => $cgi,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { circulate => "circulate_remaining_permissions" },
42     }
43 );
44
45 my $schema = Koha::Database->new()->schema();
46
47 my $barcode        = $cgi->param('barcode');
48 my $override_limit = $cgi->param('override_limit');
49 my $override_holds = $cgi->param('override_holds');
50
51 my ( $item, $issue, $borrower );
52 my $error = q{};
53 my ( $soonest_renew_date, $latest_auto_renew_date );
54
55 if ($barcode) {
56     $item = $schema->resultset("Item")->single( { barcode => $barcode } );
57
58     if ($item) {
59
60         $issue = $item->issue();
61
62         if ($issue) {
63
64             $borrower = $issue->borrower();
65             
66             if ( ( $borrower->debarred() || q{} ) lt dt_from_string()->ymd() ) {
67                 my $can_renew;
68                 ( $can_renew, $error ) =
69                   CanBookBeRenewed( $borrower->borrowernumber(),
70                     $item->itemnumber(), $override_limit );
71
72                 if ( $error && ($error eq 'on_reserve') ) {
73                     if ($override_holds) {
74                         $can_renew = 1;
75                         $error     = undef;
76                     }
77                     else {
78                         $can_renew = 0;
79                     }
80                 }
81
82                 if ( $error && ($error eq 'too_soon' or $error eq 'auto_too_soon') ) {
83                     $soonest_renew_date = C4::Circulation::GetSoonestRenewDate(
84                         $borrower->borrowernumber(),
85                         $item->itemnumber(),
86                     );
87                 }
88                 if ( $error && ( $error eq 'auto_too_late' ) ) {
89                     $latest_auto_renew_date = C4::Circulation::GetLatestAutoRenewDate(
90                         $borrower->borrowernumber(),
91                         $item->itemnumber(),
92                     );
93                 }
94                 if ($can_renew) {
95                     my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
96                     my $date_due = AddRenewal( undef, $item->itemnumber(), $branchcode );
97                     $template->param( date_due => $date_due );
98                 }
99             }
100             else {
101                 $error = "patron_restricted";
102             }
103         }
104         else {
105             $error = "no_checkout";
106         }
107     }
108     else {
109         $error = "no_item";
110     }
111
112     $template->param(
113         item     => $item,
114         issue    => $issue,
115         borrower => $borrower,
116         error    => $error,
117         soonestrenewdate => $soonest_renew_date,
118         latestautorenewdate => $latest_auto_renew_date,
119     );
120 }
121
122 # Checking if there is a Fast Cataloging Framework
123 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
124
125 my $pending_checkout_notes = Koha::Checkouts->search({ noteseen => 0 })->count;
126 $template->param( pending_checkout_notes => $pending_checkout_notes );
127
128 output_html_with_http_headers( $cgi, $cookie, $template->output );