Bug 20772: Display and edit of price_paid
[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::Illrequests;
28 use Koha::Libraries;
29
30 use Try::Tiny;
31
32 our $cgi = CGI->new;
33 my $illRequests = Koha::Illrequests->new;
34
35 # Grab all passed data
36 # 'our' since Plack changes the scoping
37 # of 'my'
38 our $params = $cgi->Vars();
39
40 # Leave immediately if ILLModule is disabled
41 unless ( C4::Context->preference('ILLModule') ) {
42     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
43     exit;
44 }
45
46 my $op = $params->{method} || 'illlist';
47
48 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
49     template_name => 'ill/ill-requests.tt',
50     query         => $cgi,
51     type          => 'intranet',
52     flagsrequired => { ill => '*' },
53 } );
54
55 # Are we able to actually work?
56 my $backends = Koha::Illrequest::Config->new->available_backends;
57 my $backends_available = ( scalar @{$backends} > 0 );
58 $template->param( backends_available => $backends_available );
59
60 if ( $backends_available ) {
61     if ( $op eq 'illview' ) {
62         # View the details of an ILL
63         my $request = Koha::Illrequests->find($params->{illrequest_id});
64
65         $template->param(
66             request => $request
67         );
68
69     } elsif ( $op eq 'create' ) {
70         # We're in the process of creating a request
71         my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
72         my $backend_result = $request->backend_create($params);
73         $template->param(
74             whole   => $backend_result,
75             request => $request
76         );
77         handle_commit_maybe($backend_result, $request);
78
79     } elsif ( $op eq 'confirm' ) {
80         # Backend 'confirm' method
81         # confirm requires a specific request, so first, find it.
82         my $request = Koha::Illrequests->find($params->{illrequest_id});
83         my $backend_result = $request->backend_confirm($params);
84         $template->param(
85             whole   => $backend_result,
86             request => $request,
87         );
88
89         # handle special commit rules & update type
90         handle_commit_maybe($backend_result, $request);
91
92     } elsif ( $op eq 'cancel' ) {
93         # Backend 'cancel' method
94         # cancel requires a specific request, so first, find it.
95         my $request = Koha::Illrequests->find($params->{illrequest_id});
96         my $backend_result = $request->backend_cancel($params);
97         $template->param(
98             whole   => $backend_result,
99             request => $request,
100         );
101
102         # handle special commit rules & update type
103         handle_commit_maybe($backend_result, $request);
104
105     } elsif ( $op eq 'edit_action' ) {
106         # Handle edits to the Illrequest object.
107         # (not the Illrequestattributes)
108         # We simulate the API for backend requests for uniformity.
109         # So, init:
110         my $request = Koha::Illrequests->find($params->{illrequest_id});
111         if ( !$params->{stage} ) {
112             my $backend_result = {
113                 error   => 0,
114                 status  => '',
115                 message => '',
116                 method  => 'edit_action',
117                 stage   => 'init',
118                 next    => '',
119                 value   => {}
120             };
121             $template->param(
122                 whole   => $backend_result,
123                 request => $request
124             );
125         } else {
126             # Commit:
127             # Save the changes
128             $request->borrowernumber($params->{borrowernumber});
129             $request->biblio_id($params->{biblio_id});
130             $request->branchcode($params->{branchcode});
131             $request->price_paid($params->{price_paid});
132             $request->notesopac($params->{notesopac});
133             $request->notesstaff($params->{notesstaff});
134             $request->store;
135             my $backend_result = {
136                 error   => 0,
137                 status  => '',
138                 message => '',
139                 method  => 'edit_action',
140                 stage   => 'commit',
141                 next    => 'illlist',
142                 value   => {}
143             };
144             handle_commit_maybe($backend_result, $request);
145         }
146
147     } elsif ( $op eq 'moderate_action' ) {
148         # Moderate action is required for an ILL submodule / syspref.
149         # Currently still needs to be implemented.
150         redirect_to_list();
151
152     } elsif ( $op eq 'delete_confirm') {
153         my $request = Koha::Illrequests->find($params->{illrequest_id});
154
155         $template->param(
156             request => $request
157         );
158
159     } elsif ( $op eq 'delete' ) {
160
161         # Check if the request is confirmed, if not, redirect
162         # to the confirmation view
163         if ($params->{confirmed}) {
164             # We simply delete the request...
165             Koha::Illrequests->find( $params->{illrequest_id} )->delete;
166             # ... then return to list view.
167             redirect_to_list();
168         } else {
169             print $cgi->redirect(
170                 "/cgi-bin/koha/ill/ill-requests.pl?" .
171                 "method=delete_confirm&illrequest_id=" .
172                 $params->{illrequest_id});
173             exit;
174         }
175
176     } elsif ( $op eq 'mark_completed' ) {
177         my $request = Koha::Illrequests->find($params->{illrequest_id});
178         my $backend_result = $request->mark_completed($params);
179         $template->param(
180             whole => $backend_result,
181             request => $request,
182         );
183
184         # handle special commit rules & update type
185         handle_commit_maybe($backend_result, $request);
186
187     } elsif ( $op eq 'generic_confirm' ) {
188         my $backend_result;
189         my $request;
190         try {
191             $request = Koha::Illrequests->find($params->{illrequest_id});
192             $params->{current_branchcode} = C4::Context->mybranch;
193             $backend_result = $request->generic_confirm($params);
194             $template->param(
195                 whole => $backend_result,
196                 request => $request,
197             );
198             $template->param( error => $params->{error} )
199                 if $params->{error};
200         }
201         catch {
202             my $error;
203             if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
204                 $error = 'no_target_email';
205             }
206             elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
207                 $error = 'no_library_email';
208             }
209             else {
210                 $error = 'unknown_error';
211             }
212             print $cgi->redirect(
213                 "/cgi-bin/koha/ill/ill-requests.pl?" .
214                 "method=generic_confirm&illrequest_id=" .
215                 $params->{illrequest_id} .
216                 "&error=$error" );
217             exit;
218         };
219
220         # handle special commit rules & update type
221         handle_commit_maybe($backend_result, $request);
222     } elsif ( $op eq 'illlist') {
223
224         # If we receive a pre-filter, make it available to the template
225         my $possible_filters = ['borrowernumber'];
226         my $active_filters = [];
227         foreach my $filter(@{$possible_filters}) {
228             if ($params->{$filter}) {
229                 push @{$active_filters},
230                     { name => $filter, value => $params->{$filter}};
231             }
232         }
233         if (scalar @{$active_filters} > 0) {
234             $template->param(
235                 prefilters => $active_filters
236             );
237         }
238     } else {
239         my $request = Koha::Illrequests->find($params->{illrequest_id});
240         my $backend_result = $request->custom_capability($op, $params);
241         $template->param(
242             whole => $backend_result,
243             request => $request,
244         );
245
246         # handle special commit rules & update type
247         handle_commit_maybe($backend_result, $request);
248     }
249 }
250
251 $template->param(
252     backends   => $backends,
253     media      => [ "Book", "Article", "Journal" ],
254     query_type => $op,
255     branches   => scalar Koha::Libraries->search,
256 );
257
258 output_html_with_http_headers( $cgi, $cookie, $template->output );
259
260 sub handle_commit_maybe {
261     my ( $backend_result, $request ) = @_;
262     # We need to special case 'commit'
263     if ( $backend_result->{stage} eq 'commit' ) {
264         if ( $backend_result->{next} eq 'illview' ) {
265             # Redirect to a view of the newly created request
266             print $cgi->redirect(
267                 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
268                 $request->id
269             );
270             exit;
271         } else {
272             # Redirect to a requests list view
273             redirect_to_list();
274         }
275     }
276 }
277
278 sub redirect_to_list {
279     print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
280     exit;
281 }