Bug 23391: Hide finished ILL requests
[koha.git] / 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');
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         ? ( status => { 'not in' => $hidden_statuses } )
68         : (),
69         $args->{borrowernumber}
70         ? ( borrowernumber => $args->{borrowernumber} )
71         : ()
72     })->as_list;
73
74     # Identify patrons & branches that
75     # we're going to need and get them
76     my $to_fetch = {
77         patrons      => {},
78         branches     => {},
79         capabilities => {}
80     };
81     foreach my $req(@requests) {
82         $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
83         $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
84         $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
85     }
86
87     # Fetch the patrons we need
88     my $patron_arr = [];
89     if ($embed{patron}) {
90         my @patron_ids = keys %{$to_fetch->{patrons}};
91         if (scalar @patron_ids > 0) {
92             my $where = {
93                 borrowernumber => { -in => \@patron_ids }
94             };
95             $patron_arr = Koha::Patrons->search($where)->unblessed;
96         }
97     }
98
99     # Fetch the branches we need
100     my $branch_arr = [];
101     if ($embed{library}) {
102         my @branchcodes = keys %{$to_fetch->{branches}};
103         if (scalar @branchcodes > 0) {
104             my $where = {
105                 branchcode => { -in => \@branchcodes }
106             };
107             $branch_arr = Koha::Libraries->search($where)->unblessed;
108         }
109     }
110
111     # Fetch the capabilities we need
112     if ($embed{capabilities}) {
113         my @backends = keys %{$to_fetch->{capabilities}};
114         if (scalar @backends > 0) {
115             foreach my $bc(@backends) {
116                 my $backend = Koha::Illrequest->new->load_backend($bc);
117                 $to_fetch->{$bc} = $backend->capabilities;
118             }
119         }
120     }
121
122     # Now we've got all associated users and branches,
123     # we can augment the request objects
124     my @output = ();
125     foreach my $req(@requests) {
126         my $to_push = $req->unblessed;
127         $to_push->{id_prefix} = $req->id_prefix;
128         # Create new "formatted" columns for each date column
129         # that needs formatting
130         foreach my $field(@format_dates) {
131             if (defined $to_push->{$field}) {
132                 $to_push->{$field . "_formatted"} = format_sqldatetime(
133                     $to_push->{$field},
134                     undef,
135                     undef,
136                     1
137                 );
138             }
139         }
140
141         foreach my $p(@{$patron_arr}) {
142             if ($p->{borrowernumber} == $req->borrowernumber) {
143                 $to_push->{patron} = {
144                     patron_id => $p->{borrowernumber},
145                     firstname      => $p->{firstname},
146                     surname        => $p->{surname},
147                     cardnumber     => $p->{cardnumber}
148                 };
149                 last;
150             }
151         }
152         foreach my $b(@{$branch_arr}) {
153             if ($b->{branchcode} eq $req->branchcode) {
154                 $to_push->{library} = $b;
155                 last;
156             }
157         }
158         if ($embed{metadata}) {
159             my $metadata = Koha::Illrequestattributes->search(
160                 { illrequest_id => $req->illrequest_id },
161                 { columns => [qw/type value/] }
162             )->unblessed;
163             my $meta_hash = {};
164             foreach my $meta(@{$metadata}) {
165                 $meta_hash->{$meta->{type}} = $meta->{value};
166             }
167             $to_push->{metadata} = $meta_hash;
168         }
169         if ($embed{capabilities}) {
170             $to_push->{capabilities} = $to_fetch->{$req->backend};
171         }
172         if ($embed{comments}) {
173             $to_push->{comments} = $req->illcomments->count;
174         }
175         if ($embed{status_alias}) {
176             $to_push->{status_alias} = $req->statusalias;
177         }
178         if ($embed{requested_partners}) {
179             $to_push->{requested_partners} = $req->requested_partners;
180         }
181         push @output, $to_push;
182     }
183
184     return $c->render( status => 200, openapi => \@output );
185 }
186
187 1;