a5ec780d2e145cf3893b7a9a7789fb4fffa69cc0
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Illrequests;
23 use Koha::Illrequestattributes;
24 use Koha::Libraries;
25 use Koha::Patrons;
26 use Koha::Libraries;
27 use Koha::DateUtils qw( format_sqldatetime );
28
29 =head1 NAME
30
31 Koha::REST::V1::Illrequests
32
33 =head2 Operations
34
35 =head3 list
36
37 Return a list of ILL requests, after applying filters.
38
39 =cut
40
41 sub list {
42     my $c = shift->openapi->valid_input or return;
43
44     my $args = $c->req->params->to_hash // {};
45     my $output = [];
46     my @format_dates = ( 'placed', 'updated', 'completed' );
47
48     # Create a hash where all keys are embedded values
49     # Enables easy checking
50     my %embed;
51     my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
52     if (defined $args->{embed}) {
53         %embed = map { $_ => 1 }  @{$args_arr};
54         delete $args->{embed};
55     }
56
57     # Get all requests
58     my @requests = Koha::Illrequests->as_list;
59
60     # Identify patrons & branches that
61     # we're going to need and get them
62     my $to_fetch = {
63         patrons      => {},
64         branches     => {},
65         capabilities => {}
66     };
67     foreach my $req(@requests) {
68         $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
69         $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
70         $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
71     }
72
73     # Fetch the patrons we need
74     my $patron_arr = [];
75     if ($embed{patron}) {
76         my @patron_ids = keys %{$to_fetch->{patrons}};
77         if (scalar @patron_ids > 0) {
78             my $where = {
79                 borrowernumber => { -in => \@patron_ids }
80             };
81             $patron_arr = Koha::Patrons->search($where)->unblessed;
82         }
83     }
84
85     # Fetch the branches we need
86     my $branch_arr = [];
87     if ($embed{library}) {
88         my @branchcodes = keys %{$to_fetch->{branches}};
89         if (scalar @branchcodes > 0) {
90             my $where = {
91                 branchcode => { -in => \@branchcodes }
92             };
93             $branch_arr = Koha::Libraries->search($where)->unblessed;
94         }
95     }
96
97     # Fetch the capabilities we need
98     if ($embed{capabilities}) {
99         my @backends = keys %{$to_fetch->{capabilities}};
100         if (scalar @backends > 0) {
101             foreach my $bc(@backends) {
102                 my $backend = Koha::Illrequest->new->load_backend($bc);
103                 $to_fetch->{$bc} = $backend->capabilities;
104             }
105         }
106     }
107
108     # Now we've got all associated users and branches,
109     # we can augment the request objects
110     my @output = ();
111     foreach my $req(@requests) {
112         my $to_push = $req->unblessed;
113         $to_push->{id_prefix} = $req->id_prefix;
114         # Create new "formatted" columns for each date column
115         # that needs formatting
116         foreach my $field(@format_dates) {
117             if (defined $to_push->{$field}) {
118                 $to_push->{$field . "_formatted"} = format_sqldatetime(
119                     $to_push->{$field},
120                     undef,
121                     undef,
122                     1
123                 );
124             }
125         }
126
127         foreach my $p(@{$patron_arr}) {
128             if ($p->{borrowernumber} == $req->borrowernumber) {
129                 $to_push->{patron} = {
130                     patron_id => $p->{borrowernumber},
131                     firstname      => $p->{firstname},
132                     surname        => $p->{surname},
133                     cardnumber     => $p->{cardnumber}
134                 };
135                 last;
136             }
137         }
138         foreach my $b(@{$branch_arr}) {
139             if ($b->{branchcode} eq $req->branchcode) {
140                 $to_push->{library} = $b;
141                 last;
142             }
143         }
144         if ($embed{metadata}) {
145             my $metadata = Koha::Illrequestattributes->search(
146                 { illrequest_id => $req->illrequest_id },
147                 { columns => [qw/type value/] }
148             )->unblessed;
149             my $meta_hash = {};
150             foreach my $meta(@{$metadata}) {
151                 $meta_hash->{$meta->{type}} = $meta->{value};
152             }
153             $to_push->{metadata} = $meta_hash;
154         }
155         if ($embed{capabilities}) {
156             $to_push->{capabilities} = $to_fetch->{$req->backend};
157         }
158         if ($embed{comments}) {
159             $to_push->{comments} = $req->illcomments->count;
160         }
161         if ($embed{status_alias}) {
162             $to_push->{status_alias} = $req->statusalias;
163         }
164         if ($embed{requested_partners}) {
165             $to_push->{requested_partners} = $req->requested_partners;
166         }
167         push @output, $to_push;
168     }
169
170     return $c->render( status => 200, openapi => \@output );
171 }
172
173 1;