Bug 13098: Make _FixAccountForLostAndReturned refund the right thing
[koha.git] / ill / ill-requests.pl
bloba2e898a67c65c9cac2e5228f30c4b64483f05f93
1 #!/usr/bin/perl
3 # Copyright 2013 PTFS-Europe Ltd and Mark Gavillet
4 # Copyright 2014 PTFS-Europe Ltd
6 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use Koha::AuthorisedValues;
27 use Koha::Illcomment;
28 use Koha::Illrequests;
29 use Koha::Libraries;
30 use Koha::Token;
32 use Try::Tiny;
34 our $cgi = CGI->new;
35 my $illRequests = Koha::Illrequests->new;
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
39 # of 'my'
40 our $params = $cgi->Vars();
42 # Leave immediately if ILLModule is disabled
43 unless ( C4::Context->preference('ILLModule') ) {
44 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
45 exit;
48 my $op = $params->{method} || 'illlist';
50 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
51 template_name => 'ill/ill-requests.tt',
52 query => $cgi,
53 type => 'intranet',
54 flagsrequired => { ill => '*' },
55 } );
57 # Are we able to actually work?
58 my $backends = Koha::Illrequest::Config->new->available_backends;
59 my $backends_available = ( scalar @{$backends} > 0 );
60 $template->param( backends_available => $backends_available );
62 if ( $backends_available ) {
63 if ( $op eq 'illview' ) {
64 # View the details of an ILL
65 my $request = Koha::Illrequests->find($params->{illrequest_id});
67 $template->param(
68 request => $request,
69 csrf_token => Koha::Token->new->generate_csrf({
70 session_id => scalar $cgi->cookie('CGISESSID'),
71 }),
74 } elsif ( $op eq 'create' ) {
75 # We're in the process of creating a request
76 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
77 my $backend_result = $request->backend_create($params);
78 $template->param(
79 whole => $backend_result,
80 request => $request
82 handle_commit_maybe($backend_result, $request);
84 } elsif ( $op eq 'confirm' ) {
85 # Backend 'confirm' method
86 # confirm requires a specific request, so first, find it.
87 my $request = Koha::Illrequests->find($params->{illrequest_id});
88 my $backend_result = $request->backend_confirm($params);
89 $template->param(
90 whole => $backend_result,
91 request => $request,
94 # handle special commit rules & update type
95 handle_commit_maybe($backend_result, $request);
97 } elsif ( $op eq 'cancel' ) {
98 # Backend 'cancel' method
99 # cancel requires a specific request, so first, find it.
100 my $request = Koha::Illrequests->find($params->{illrequest_id});
101 my $backend_result = $request->backend_cancel($params);
102 $template->param(
103 whole => $backend_result,
104 request => $request,
107 # handle special commit rules & update type
108 handle_commit_maybe($backend_result, $request);
110 } elsif ( $op eq 'edit_action' ) {
111 # Handle edits to the Illrequest object.
112 # (not the Illrequestattributes)
113 # We simulate the API for backend requests for uniformity.
114 # So, init:
115 my $request = Koha::Illrequests->find($params->{illrequest_id});
116 if ( !$params->{stage} ) {
117 my $backend_result = {
118 error => 0,
119 status => '',
120 message => '',
121 method => 'edit_action',
122 stage => 'init',
123 next => '',
124 value => {}
126 $template->param(
127 whole => $backend_result,
128 request => $request
130 } else {
131 # Commit:
132 # Save the changes
133 $request->borrowernumber($params->{borrowernumber});
134 $request->biblio_id($params->{biblio_id});
135 $request->branchcode($params->{branchcode});
136 $request->price_paid($params->{price_paid});
137 $request->notesopac($params->{notesopac});
138 $request->notesstaff($params->{notesstaff});
139 $request->store;
140 my $backend_result = {
141 error => 0,
142 status => '',
143 message => '',
144 method => 'edit_action',
145 stage => 'commit',
146 next => 'illlist',
147 value => {}
149 handle_commit_maybe($backend_result, $request);
152 } elsif ( $op eq 'moderate_action' ) {
153 # Moderate action is required for an ILL submodule / syspref.
154 # Currently still needs to be implemented.
155 redirect_to_list();
157 } elsif ( $op eq 'delete_confirm') {
158 my $request = Koha::Illrequests->find($params->{illrequest_id});
160 $template->param(
161 request => $request
164 } elsif ( $op eq 'delete' ) {
166 # Check if the request is confirmed, if not, redirect
167 # to the confirmation view
168 if ($params->{confirmed}) {
169 # We simply delete the request...
170 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
171 # ... then return to list view.
172 redirect_to_list();
173 } else {
174 print $cgi->redirect(
175 "/cgi-bin/koha/ill/ill-requests.pl?" .
176 "method=delete_confirm&illrequest_id=" .
177 $params->{illrequest_id});
178 exit;
181 } elsif ( $op eq 'mark_completed' ) {
182 my $request = Koha::Illrequests->find($params->{illrequest_id});
183 my $backend_result = $request->mark_completed($params);
184 $template->param(
185 whole => $backend_result,
186 request => $request,
189 # handle special commit rules & update type
190 handle_commit_maybe($backend_result, $request);
192 } elsif ( $op eq 'generic_confirm' ) {
193 my $backend_result;
194 my $request;
195 try {
196 $request = Koha::Illrequests->find($params->{illrequest_id});
197 $params->{current_branchcode} = C4::Context->mybranch;
198 $backend_result = $request->generic_confirm($params);
199 $template->param(
200 whole => $backend_result,
201 request => $request,
203 $template->param( error => $params->{error} )
204 if $params->{error};
206 catch {
207 my $error;
208 if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
209 $error = 'no_target_email';
211 elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
212 $error = 'no_library_email';
214 else {
215 $error = 'unknown_error';
217 print $cgi->redirect(
218 "/cgi-bin/koha/ill/ill-requests.pl?" .
219 "method=generic_confirm&illrequest_id=" .
220 $params->{illrequest_id} .
221 "&error=$error" );
222 exit;
225 # handle special commit rules & update type
226 handle_commit_maybe($backend_result, $request);
227 } elsif ( $op eq 'illlist') {
229 # If we receive a pre-filter, make it available to the template
230 my $possible_filters = ['borrowernumber'];
231 my $active_filters = [];
232 foreach my $filter(@{$possible_filters}) {
233 if ($params->{$filter}) {
234 push @{$active_filters},
235 { name => $filter, value => $params->{$filter}};
238 if (scalar @{$active_filters} > 0) {
239 $template->param(
240 prefilters => $active_filters
244 } elsif ( $op eq "save_comment" ) {
245 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
246 session_id => scalar $cgi->cookie('CGISESSID'),
247 token => scalar $cgi->param('csrf_token'),
249 my $comment = Koha::Illcomment->new({
250 illrequest_id => scalar $params->{illrequest_id},
251 borrowernumber => $patronnumber,
252 comment => scalar $params->{comment},
254 $comment->store();
255 # Redirect to view the whole request
256 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
257 scalar $params->{illrequest_id}
259 exit;
261 } else {
262 my $request = Koha::Illrequests->find($params->{illrequest_id});
263 my $backend_result = $request->custom_capability($op, $params);
264 $template->param(
265 whole => $backend_result,
266 request => $request,
269 # handle special commit rules & update type
270 handle_commit_maybe($backend_result, $request);
274 $template->param(
275 backends => $backends,
276 media => [ "Book", "Article", "Journal" ],
277 query_type => $op,
278 branches => scalar Koha::Libraries->search,
281 output_html_with_http_headers( $cgi, $cookie, $template->output );
283 sub handle_commit_maybe {
284 my ( $backend_result, $request ) = @_;
285 # We need to special case 'commit'
286 if ( $backend_result->{stage} eq 'commit' ) {
287 if ( $backend_result->{next} eq 'illview' ) {
288 # Redirect to a view of the newly created request
289 print $cgi->redirect(
290 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
291 $request->id
293 exit;
294 } else {
295 # Redirect to a requests list view
296 redirect_to_list();
301 sub redirect_to_list {
302 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
303 exit;