Bug 25266: Build the vendor list from the order list
[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;
26 use C4::Koha;
27 use C4::Output;
28
29 use Koha::Illrequest::Config;
30 use Koha::Illrequests;
31 use Koha::Libraries;
32 use Koha::Patrons;
33 use Koha::Illrequest::Availability;
34
35 my $query = new CGI;
36
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
39 # of 'my'
40 our $params = $query->Vars();
41
42 # if illrequests is disabled, leave immediately
43 if ( ! C4::Context->preference('ILLModule') ) {
44     print $query->redirect("/cgi-bin/koha/errors/404.pl");
45     exit;
46 }
47
48 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
49     template_name   => "opac-illrequests.tt",
50     query           => $query,
51     type            => "opac",
52     authnotrequired => 0,
53 });
54
55 # Are we able to actually work?
56 my $reduced  = C4::Context->preference('ILLOpacbackends');
57 my $backends = Koha::Illrequest::Config->new->available_backends($reduced);
58 my $backends_available = ( scalar @{$backends} > 0 );
59 $template->param( backends_available => $backends_available );
60
61 my $op = $params->{'method'} || 'list';
62
63 if ( $op eq 'list' ) {
64
65     my $requests = Koha::Illrequests->search(
66         { borrowernumber => $loggedinuser }
67     );
68     my $req = Koha::Illrequest->new;
69     $template->param(
70         requests => $requests,
71         backends    => $backends
72     );
73
74 } elsif ( $op eq 'view') {
75     my $request = Koha::Illrequests->find({
76         borrowernumber => $loggedinuser,
77         illrequest_id  => $params->{illrequest_id}
78     });
79     $template->param(
80         request => $request
81     );
82
83 } elsif ( $op eq 'update') {
84     my $request = Koha::Illrequests->find({
85         borrowernumber => $loggedinuser,
86         illrequest_id  => $params->{illrequest_id}
87     });
88     $request->notesopac($params->{notesopac})->store;
89     print $query->redirect(
90         '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id=' .
91         $params->{illrequest_id} .
92         '&message=1'
93     );
94     exit;
95 } elsif ( $op eq 'cancreq') {
96     my $request = Koha::Illrequests->find({
97         borrowernumber => $loggedinuser,
98         illrequest_id  => $params->{illrequest_id}
99     });
100     $request->status('CANCREQ')->store;
101     print $query->redirect(
102         '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id=' .
103         $params->{illrequest_id} .
104         '&message=1'
105     );
106     exit;
107 } elsif ( $op eq 'create' ) {
108     if (!$params->{backend}) {
109         my $req = Koha::Illrequest->new;
110         $template->param(
111             backends    => $req->available_backends
112         );
113     } else {
114         my $request = Koha::Illrequest->new
115             ->load_backend($params->{backend});
116
117         # Does this backend enable us to insert an availability stage and should
118         # we? If not, proceed as normal.
119         if (
120             C4::Context->preference("ILLCheckAvailability") &&
121             $request->_backend_capability(
122                 'should_display_availability',
123                 $params
124             ) &&
125             # If the user has elected to continue with the request despite
126             # having viewed availability info, this flag will be set
127             !$params->{checked_availability}
128         ) {
129             # Establish which of the installed availability providers
130             # can service our metadata, if so, jump in
131             my $availability = Koha::Illrequest::Availability->new($params);
132             my $services = $availability->get_services({
133                 ui_context => 'opac'
134             });
135             if (scalar @{$services} > 0) {
136                 # Modify our method so we use the correct part of the
137                 # template
138                 $op = 'availability';
139                 # Prepare the metadata we're sending them
140                 my $metadata = $availability->prep_metadata($params);
141                 $template->param(
142                     metadata        => $metadata,
143                     services_json   => encode_json($services),
144                     services        => $services,
145                     illrequestsview => 1,
146                     message         => $params->{message},
147                     method          => $op,
148                     whole           => $params
149                 );
150                 output_html_with_http_headers $query, $cookie,
151                     $template->output, undef, { force_no_caching => 1 };
152                 exit;
153             }
154         }
155
156         $params->{cardnumber} = Koha::Patrons->find({
157             borrowernumber => $loggedinuser
158         })->cardnumber;
159         $params->{opac} = 1;
160         my $backend_result = $request->backend_create($params);
161         if ($backend_result->{stage} eq 'copyrightclearance') {
162             $template->param(
163                 stage       => $backend_result->{stage},
164                 whole       => $backend_result
165             );
166         } else {
167             $template->param(
168                 types       => [ "Book", "Article", "Journal" ],
169                 branches    => Koha::Libraries->search->unblessed,
170                 whole       => $backend_result,
171                 request     => $request
172             );
173             if ($backend_result->{stage} eq 'commit') {
174                 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
175                 exit;
176             }
177         }
178
179     }
180 }
181
182 $template->param(
183     message         => $params->{message},
184     illrequestsview => 1,
185     method          => $op
186 );
187
188 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };