Bug 20581: Add display of status_alias to staff vw
[koha.git] / ill / ill-requests.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 PTFS-Europe Ltd and Mark Gavillet
4 # Copyright 2014 PTFS-Europe Ltd
5 #
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.
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
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;
31
32 use Try::Tiny;
33
34 our $cgi = CGI->new;
35 my $illRequests = Koha::Illrequests->new;
36
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
39 # of 'my'
40 our $params = $cgi->Vars();
41
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;
46 }
47
48 my $op = $params->{method} || 'illlist';
49
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 } );
56
57 # Are we able to actually work?
58 my $cfg = Koha::Illrequest::Config->new;
59 my $backends = $cfg->available_backends;
60 my $has_branch = $cfg->has_branch;
61 my $backends_available = ( scalar @{$backends} > 0 );
62 $template->param(
63     backends_available => $backends_available,
64     has_branch         => $has_branch
65 );
66
67 if ( $backends_available ) {
68     if ( $op eq 'illview' ) {
69         # View the details of an ILL
70         my $request = Koha::Illrequests->find($params->{illrequest_id});
71
72         $template->param(
73             request    => $request,
74             csrf_token => Koha::Token->new->generate_csrf({
75                 session_id => scalar $cgi->cookie('CGISESSID'),
76             }),
77             ( $params->{error} ? ( error => $params->{error} ) : () ),
78         );
79
80     } elsif ( $op eq 'create' ) {
81         # We're in the process of creating a request
82         my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
83         my $backend_result = $request->backend_create($params);
84         $template->param(
85             whole   => $backend_result,
86             request => $request
87         );
88         handle_commit_maybe($backend_result, $request);
89
90     } elsif ( $op eq 'migrate' ) {
91         # We're in the process of migrating a request
92         my $request = Koha::Illrequests->find($params->{illrequest_id});
93         my $backend_result;
94         if ( $params->{backend} ) {
95             my $new_request = Koha::Illrequest->new->load_backend( $params->{backend} );
96             $backend_result = $new_request->backend_migrate($params);
97             if ($backend_result) {
98                 $template->param(
99                     whole   => $backend_result,
100                     request => $new_request
101                 );
102                 $request = $new_request;
103             } else {
104                 # Backend failure, redirect back to illview
105                 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
106                       . '?method=illview'
107                       . '&illrequest_id='
108                       . $request->id
109                       . '&error=migrate_target' );
110                 exit;
111             }
112         }
113         else {
114             $backend_result = $request->backend_migrate($params);
115             $template->param(
116                 whole   => $backend_result,
117                 request => $request
118             );
119         }
120         handle_commit_maybe( $backend_result, $request );
121
122     } elsif ( $op eq 'confirm' ) {
123         # Backend 'confirm' method
124         # confirm requires a specific request, so first, find it.
125         my $request = Koha::Illrequests->find($params->{illrequest_id});
126         my $backend_result = $request->backend_confirm($params);
127         $template->param(
128             whole   => $backend_result,
129             request => $request,
130         );
131
132         # handle special commit rules & update type
133         handle_commit_maybe($backend_result, $request);
134
135     } elsif ( $op eq 'cancel' ) {
136         # Backend 'cancel' method
137         # cancel requires a specific request, so first, find it.
138         my $request = Koha::Illrequests->find($params->{illrequest_id});
139         my $backend_result = $request->backend_cancel($params);
140         $template->param(
141             whole   => $backend_result,
142             request => $request,
143         );
144
145         # handle special commit rules & update type
146         handle_commit_maybe($backend_result, $request);
147
148     } elsif ( $op eq 'edit_action' ) {
149         # Handle edits to the Illrequest object.
150         # (not the Illrequestattributes)
151         # We simulate the API for backend requests for uniformity.
152         # So, init:
153         my $request = Koha::Illrequests->find($params->{illrequest_id});
154         if ( !$params->{stage} ) {
155             my $backend_result = {
156                 error   => 0,
157                 status  => '',
158                 message => '',
159                 method  => 'edit_action',
160                 stage   => 'init',
161                 next    => '',
162                 value   => {}
163             };
164             $template->param(
165                 whole          => $backend_result,
166                 request        => $request,
167                 status_aliases => scalar Koha::AuthorisedValues->search(
168                     { category => 'ILLSTATUS' }
169                 )
170             );
171         } else {
172             # Commit:
173             # Save the changes
174             $request->borrowernumber($params->{borrowernumber});
175             $request->biblio_id($params->{biblio_id});
176             $request->branchcode($params->{branchcode});
177             $request->price_paid($params->{price_paid});
178             $request->notesopac($params->{notesopac});
179             $request->notesstaff($params->{notesstaff});
180             my $alias = ($params->{status_alias} =~ /\d/) ?
181                 $params->{status_alias} :
182                 undef;
183             $request->status_alias($alias);
184             $request->store;
185             my $backend_result = {
186                 error   => 0,
187                 status  => '',
188                 message => '',
189                 method  => 'edit_action',
190                 stage   => 'commit',
191                 next    => 'illlist',
192                 value   => {}
193             };
194             handle_commit_maybe($backend_result, $request);
195         }
196
197     } elsif ( $op eq 'moderate_action' ) {
198         # Moderate action is required for an ILL submodule / syspref.
199         # Currently still needs to be implemented.
200         redirect_to_list();
201
202     } elsif ( $op eq 'delete_confirm') {
203         my $request = Koha::Illrequests->find($params->{illrequest_id});
204
205         $template->param(
206             request => $request
207         );
208
209     } elsif ( $op eq 'delete' ) {
210
211         # Check if the request is confirmed, if not, redirect
212         # to the confirmation view
213         if ($params->{confirmed}) {
214             # We simply delete the request...
215             Koha::Illrequests->find( $params->{illrequest_id} )->delete;
216             # ... then return to list view.
217             redirect_to_list();
218         } else {
219             print $cgi->redirect(
220                 "/cgi-bin/koha/ill/ill-requests.pl?" .
221                 "method=delete_confirm&illrequest_id=" .
222                 $params->{illrequest_id});
223             exit;
224         }
225
226     } elsif ( $op eq 'mark_completed' ) {
227         my $request = Koha::Illrequests->find($params->{illrequest_id});
228         my $backend_result = $request->mark_completed($params);
229         $template->param(
230             whole => $backend_result,
231             request => $request,
232         );
233
234         # handle special commit rules & update type
235         handle_commit_maybe($backend_result, $request);
236
237     } elsif ( $op eq 'generic_confirm' ) {
238         my $backend_result;
239         my $request;
240         try {
241             $request = Koha::Illrequests->find($params->{illrequest_id});
242             $params->{current_branchcode} = C4::Context->mybranch;
243             $backend_result = $request->generic_confirm($params);
244             $template->param(
245                 whole => $backend_result,
246                 request => $request,
247             );
248             $template->param( error => $params->{error} )
249                 if $params->{error};
250         }
251         catch {
252             my $error;
253             if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
254                 $error = 'no_target_email';
255             }
256             elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
257                 $error = 'no_library_email';
258             }
259             else {
260                 $error = 'unknown_error';
261             }
262             print $cgi->redirect(
263                 "/cgi-bin/koha/ill/ill-requests.pl?" .
264                 "method=generic_confirm&illrequest_id=" .
265                 $params->{illrequest_id} .
266                 "&error=$error" );
267             exit;
268         };
269
270         # handle special commit rules & update type
271         handle_commit_maybe($backend_result, $request);
272     } elsif ( $op eq 'illlist') {
273
274         # If we receive a pre-filter, make it available to the template
275         my $possible_filters = ['borrowernumber'];
276         my $active_filters = [];
277         foreach my $filter(@{$possible_filters}) {
278             if ($params->{$filter}) {
279                 push @{$active_filters},
280                     { name => $filter, value => $params->{$filter}};
281             }
282         }
283         if (scalar @{$active_filters} > 0) {
284             $template->param(
285                 prefilters => $active_filters
286             );
287         }
288
289     } elsif ( $op eq "save_comment" ) {
290         die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
291            session_id => scalar $cgi->cookie('CGISESSID'),
292            token      => scalar $cgi->param('csrf_token'),
293         });
294         my $comment = Koha::Illcomment->new({
295             illrequest_id  => scalar $params->{illrequest_id},
296             borrowernumber => $patronnumber,
297             comment        => scalar $params->{comment},
298         });
299         $comment->store();
300         # Redirect to view the whole request
301         print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
302             scalar $params->{illrequest_id}
303         );
304         exit;
305
306     } else {
307         my $request = Koha::Illrequests->find($params->{illrequest_id});
308         my $backend_result = $request->custom_capability($op, $params);
309         $template->param(
310             whole => $backend_result,
311             request => $request,
312         );
313
314         # handle special commit rules & update type
315         handle_commit_maybe($backend_result, $request);
316     }
317 }
318
319 $template->param(
320     backends   => $backends,
321     types      => [ "Book", "Article", "Journal" ],
322     query_type => $op,
323     branches   => scalar Koha::Libraries->search,
324 );
325
326 output_html_with_http_headers( $cgi, $cookie, $template->output );
327
328 sub handle_commit_maybe {
329     my ( $backend_result, $request ) = @_;
330
331     # We need to special case 'commit'
332     if ( $backend_result->{stage} eq 'commit' ) {
333         if ( $backend_result->{next} eq 'illview' ) {
334
335             # Redirect to a view of the newly created request
336             print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
337                   . '?method=illview'
338                   . '&illrequest_id='
339                   . $request->id );
340             exit;
341         }
342         elsif ( $backend_result->{next} eq 'emigrate' ) {
343
344             # Redirect to a view of the newly created request
345             print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
346                   . '?method=migrate'
347                   . '&stage=emigrate'
348                   . '&illrequest_id='
349                   . $request->id );
350             exit;
351         }
352         else {
353             # Redirect to a requests list view
354             redirect_to_list();
355         }
356     }
357 }
358
359 sub redirect_to_list {
360     print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
361     exit;
362 }