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