Bug 28641: ILLHiddenRequestStatuses does not consider custom statuses
[srvgit] / Koha / REST / V1 / Illrequests.pm
1 package Koha::REST::V1::Illrequests;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use C4::Context;
23 use Koha::Illrequests;
24 use Koha::Illrequestattributes;
25 use Koha::Libraries;
26 use Koha::Patrons;
27 use Koha::Libraries;
28 use Koha::DateUtils qw( format_sqldatetime );
29
30 =head1 NAME
31
32 Koha::REST::V1::Illrequests
33
34 =head2 Operations
35
36 =head3 list
37
38 Return a list of ILL requests, after applying filters.
39
40 =cut
41
42 sub list {
43     my $c = shift->openapi->valid_input or return;
44
45     my $args = $c->req->params->to_hash // {};
46     my $output = [];
47     my @format_dates = ( 'placed', 'updated', 'completed' );
48
49     # Create a hash where all keys are embedded values
50     # Enables easy checking
51     my %embed;
52     my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
53     if (defined $args->{embed}) {
54         %embed = map { $_ => 1 }  @{$args_arr};
55         delete $args->{embed};
56     }
57
58     # Get the pipe-separated string of hidden ILL statuses
59     my $hidden_statuses_string = C4::Context->preference('ILLHiddenRequestStatuses') // q{};
60     # Turn into arrayref
61     my $hidden_statuses = [ split /\|/, $hidden_statuses_string ];
62
63     # Get all requests
64     # If necessary, only get those from a specified patron
65     my @requests = Koha::Illrequests->search({
66         $hidden_statuses
67         ? (
68             -and => {
69                 status => { 'not in' => $hidden_statuses },
70                 status_alias => [ -or =>
71                     { 'not in' => $hidden_statuses },
72                     { '=' => undef }
73                 ]
74             }
75         )
76         : (),
77         $args->{borrowernumber}
78         ? ( borrowernumber => $args->{borrowernumber} )
79         : ()
80     })->as_list;
81
82     my $fetch_backends = {};
83     foreach my $request (@requests) {
84         $fetch_backends->{ $request->backend } ||=
85           Koha::Illrequest->new->load_backend( $request->backend );
86     }
87
88     # Pre-load the backend object to avoid useless backend lookup/loads
89     @requests = map { $_->_backend( $fetch_backends->{ $_->backend } ); $_ } @requests;
90
91     # Identify patrons & branches that
92     # we're going to need and get them
93     my $to_fetch = {
94         patrons      => {},
95         branches     => {},
96         capabilities => {}
97     };
98     foreach my $req (@requests) {
99         $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
100         $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
101         $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
102     }
103
104     # Fetch the patrons we need
105     my $patron_arr = [];
106     if ($embed{patron}) {
107         my @patron_ids = keys %{$to_fetch->{patrons}};
108         if (scalar @patron_ids > 0) {
109             my $where = {
110                 borrowernumber => { -in => \@patron_ids }
111             };
112             $patron_arr = Koha::Patrons->search($where)->unblessed;
113         }
114     }
115
116     # Fetch the branches we need
117     my $branch_arr = [];
118     if ($embed{library}) {
119         my @branchcodes = keys %{$to_fetch->{branches}};
120         if (scalar @branchcodes > 0) {
121             my $where = {
122                 branchcode => { -in => \@branchcodes }
123             };
124             $branch_arr = Koha::Libraries->search($where)->unblessed;
125         }
126     }
127
128     # Fetch the capabilities we need
129     if ($embed{capabilities}) {
130         my @backends = keys %{$to_fetch->{capabilities}};
131         if (scalar @backends > 0) {
132             foreach my $bc(@backends) {
133                 $to_fetch->{$bc} = $fetch_backends->{$bc}->capabilities;
134             }
135         }
136     }
137
138     # Now we've got all associated users and branches,
139     # we can augment the request objects
140     my @output = ();
141     foreach my $req(@requests) {
142         my $to_push = $req->unblessed;
143         $to_push->{id_prefix} = $req->id_prefix;
144         # Create new "formatted" columns for each date column
145         # that needs formatting
146         foreach my $field(@format_dates) {
147             if (defined $to_push->{$field}) {
148                 $to_push->{$field . "_formatted"} = format_sqldatetime(
149                     $to_push->{$field},
150                     undef,
151                     undef,
152                     1
153                 );
154             }
155         }
156
157         foreach my $p(@{$patron_arr}) {
158             if ($p->{borrowernumber} == $req->borrowernumber) {
159                 $to_push->{patron} = {
160                     patron_id => $p->{borrowernumber},
161                     firstname      => $p->{firstname},
162                     surname        => $p->{surname},
163                     cardnumber     => $p->{cardnumber}
164                 };
165                 last;
166             }
167         }
168         foreach my $b(@{$branch_arr}) {
169             if ($b->{branchcode} eq $req->branchcode) {
170                 $to_push->{library} = $b;
171                 last;
172             }
173         }
174         if ($embed{metadata}) {
175             my $metadata = Koha::Illrequestattributes->search(
176                 { illrequest_id => $req->illrequest_id },
177                 { columns => [qw/type value/] }
178             )->unblessed;
179             my $meta_hash = {};
180             foreach my $meta(@{$metadata}) {
181                 $meta_hash->{$meta->{type}} = $meta->{value};
182             }
183             $to_push->{metadata} = $meta_hash;
184         }
185         if ($embed{capabilities}) {
186             $to_push->{capabilities} = $to_fetch->{$req->backend};
187         }
188         if ($embed{comments}) {
189             $to_push->{comments} = $req->illcomments->count;
190         }
191         if ($embed{status_alias}) {
192             $to_push->{status_alias} = $req->statusalias;
193         }
194         if ($embed{requested_partners}) {
195             $to_push->{requested_partners} = $req->requested_partners;
196         }
197         push @output, $to_push;
198     }
199
200     return $c->render( status => 200, openapi => \@output );
201 }
202
203 1;