Bug 25261: Add system preference
[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
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 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
32 my $cgi = new CGI;
33
34 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user(
35     {
36         template_name   => "circ/renew.tt",
37         query           => $cgi,
38         type            => "intranet",
39         flagsrequired   => { circulate => "circulate_remaining_permissions" },
40     }
41 );
42
43 my $schema = Koha::Database->new()->schema();
44
45 my $barcode        = $cgi->param('barcode');
46 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespae
47 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
48 my $override_limit = $cgi->param('override_limit');
49 my $override_holds = $cgi->param('override_holds');
50 my $hard_due_date  = $cgi->param('hard_due_date');
51
52 my ( $item, $issue, $borrower );
53 my $error = q{};
54 my ( $soonest_renew_date, $latest_auto_renew_date );
55
56 if ($barcode) {
57     $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
58     $item = $schema->resultset("Item")->single( { barcode => $barcode } );
59
60     if ($item) {
61
62         $issue = $item->issue();
63
64         if ($issue) {
65
66             $borrower = $issue->borrower();
67             
68             if ( ( $borrower->debarred() || q{} ) lt dt_from_string()->ymd() ) {
69                 my $can_renew;
70                 ( $can_renew, $error ) =
71                   CanBookBeRenewed( $borrower->borrowernumber(),
72                     $item->itemnumber(), $override_limit );
73
74                 if ( $error && ($error eq 'on_reserve') ) {
75                     if ($override_holds) {
76                         $can_renew = 1;
77                         $error     = undef;
78                     }
79                     else {
80                         $can_renew = 0;
81                     }
82                 }
83
84                 if ( $error && ($error eq 'too_soon' or $error eq 'auto_too_soon') ) {
85                     $soonest_renew_date = C4::Circulation::GetSoonestRenewDate(
86                         $borrower->borrowernumber(),
87                         $item->itemnumber(),
88                     );
89                 }
90                 if ( $error && ( $error eq 'auto_too_late' ) ) {
91                     $latest_auto_renew_date = C4::Circulation::GetLatestAutoRenewDate(
92                         $borrower->borrowernumber(),
93                         $item->itemnumber(),
94                     );
95                 }
96                 if ($can_renew) {
97                     my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
98                     my $date_due;
99                     if ( $cgi->param('renewonholdduedate') ) {
100                         $date_due = dt_from_string( scalar $cgi->param('renewonholdduedate'));
101                     }
102                     if ( C4::Context->preference('SpecifyDueDate') && $hard_due_date ) {
103                         $date_due = dt_from_string( $hard_due_date );
104                     }
105                     $date_due = AddRenewal( undef, $item->itemnumber(), $branchcode, $date_due );
106                     $template->param( date_due => $date_due );
107                 }
108             }
109             else {
110                 $error = "patron_restricted";
111             }
112         }
113         else {
114             $error = "no_checkout";
115         }
116     }
117     else {
118         $error = "no_item";
119     }
120
121     $template->param(
122         item     => $item,
123         issue    => $issue,
124         borrower => $borrower,
125         error    => $error,
126         soonestrenewdate => $soonest_renew_date,
127         latestautorenewdate => $latest_auto_renew_date,
128     );
129 }
130
131 $template->param( hard_due_date => ($hard_due_date ? output_pref({ str => $hard_due_date, dateformat => 'iso' }) : undef) );
132 # Checking if there is a Fast Cataloging Framework
133 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
134
135 output_html_with_http_headers( $cgi, $cookie, $template->output );