Bug 29145: use overdues restrict delays when removing overdues restriction upon return
[koha.git] / opac / opac-illrequests.pl
1 #!/usr/bin/perl
2
3 # Copyright 2017 PTFS-Europe Ltd
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 JSON qw( encode_json );
23
24 use CGI qw ( -utf8 );
25 use C4::Auth qw( get_template_and_user );
26 use C4::Koha;
27 use C4::Output qw( output_html_with_http_headers );
28 use POSIX qw( strftime );
29
30 use Koha::Illrequest::Config;
31 use Koha::Illrequests;
32 use Koha::Libraries;
33 use Koha::Patrons;
34 use Koha::Illrequest::Workflow::Availability;
35 use Koha::Illrequest::Workflow::TypeDisclaimer;
36
37 my $query = CGI->new;
38
39 # Grab all passed data
40 # 'our' since Plack changes the scoping
41 # of 'my'
42 our $params = $query->Vars();
43
44 # if illrequests is disabled, leave immediately
45 if ( ! C4::Context->preference('ILLModule') ) {
46     print $query->redirect("/cgi-bin/koha/errors/404.pl");
47     exit;
48 }
49
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
51     template_name   => "opac-illrequests.tt",
52     query           => $query,
53     type            => "opac",
54 });
55
56 # Are we able to actually work?
57 my $reduced  = C4::Context->preference('ILLOpacbackends');
58 my $backends = Koha::Illrequest::Config->new->available_backends($reduced);
59 my $backends_available = ( scalar @{$backends} > 0 );
60 $template->param( backends_available => $backends_available );
61
62 my $op = $params->{'method'} || 'list';
63
64 my ( $illrequest_id, $request );
65 if ( $illrequest_id = $params->{illrequest_id} ) {
66     $request = Koha::Illrequests->find($illrequest_id);
67     # Make sure the request belongs to the logged in user
68     if( !$request || $request->borrowernumber != $loggedinuser ) {
69         print $query->redirect("/cgi-bin/koha/errors/404.pl");
70         exit;
71     }
72 }
73
74 if ( $op eq 'list' ) {
75
76     my $requests = Koha::Illrequests->search(
77         { borrowernumber => $loggedinuser }
78     );
79     $template->param(
80         requests => $requests,
81         backends => $backends
82     );
83
84 } elsif ( $op eq 'view') {
85     $template->param(
86         request => $request
87     );
88
89 } elsif ( $op eq 'update') {
90     $request->notesopac($params->{notesopac})->store;
91     # Send a notice to staff alerting them of the update
92     $request->send_staff_notice('ILL_REQUEST_MODIFIED');
93     print $query->redirect(
94             '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id='
95           . $illrequest_id
96           . '&message=1' );
97     exit;
98 } elsif ( $op eq 'cancreq') {
99     $request->status('CANCREQ')->store;
100     print $query->redirect(
101             '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id='
102           . $illrequest_id
103           . '&message=1' );
104     exit;
105 } elsif ( $op eq 'create' ) {
106     if (!$params->{backend}) {
107         my $req = Koha::Illrequest->new;
108         $template->param(
109             backends    => $req->available_backends
110         );
111     } else {
112         my $request = Koha::Illrequest->new
113             ->load_backend($params->{backend});
114
115         # Before request creation operations - Preparation
116         my $availability =
117           Koha::Illrequest::Workflow::Availability->new( $params, 'opac' );
118         my $type_disclaimer =
119           Koha::Illrequest::Workflow::TypeDisclaimer->new( $params, 'opac' );
120
121         # ILLCheckAvailability operation
122         if ($availability->show_availability($request)) {
123             $op = 'availability';
124             $template->param(
125                 $availability->availability_template_params($params)
126             );
127             output_html_with_http_headers $query, $cookie,
128                 $template->output, undef,
129                 { force_no_caching => 1 };
130             exit;
131         # ILLModuleDisclaimerByType operation
132         } elsif ( $type_disclaimer->show_type_disclaimer($request)) {
133             $op = 'typedisclaimer';
134             $template->param(
135                 $type_disclaimer->type_disclaimer_template_params($params)
136             );
137             output_html_with_http_headers $query, $cookie,
138                 $template->output, undef,
139                 { force_no_caching => 1 };
140             exit;
141         }
142
143         $params->{cardnumber} = Koha::Patrons->find({
144             borrowernumber => $loggedinuser
145         })->cardnumber;
146         $params->{opac} = 1;
147         my $backend_result = $request->backend_create($params);
148
149         # After creation actions
150         if ( $params->{type_disclaimer_submitted} ) {
151             $type_disclaimer->after_request_created( $params, $request );
152             print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
153             exit;
154         }
155
156         if ($backend_result->{stage} eq 'copyrightclearance') {
157             $template->param(
158                 stage       => $backend_result->{stage},
159                 whole       => $backend_result
160             );
161         } else {
162             $template->param(
163                 types       => [ "Book", "Article", "Journal" ],
164                 branches    => Koha::Libraries->search->unblessed,
165                 whole       => $backend_result,
166                 request     => $request
167             );
168             if ($backend_result->{stage} eq 'commit') {
169                 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
170                 exit;
171             }
172         }
173
174     }
175 }
176
177 $template->param(
178     message         => $params->{message},
179     illrequestsview => 1,
180     method          => $op
181 );
182
183 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };