1 package Koha
::REST
::V1
::Illrequests
;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo
::Base
'Mojolicious::Controller';
22 use Koha
::Illrequests
;
23 use Koha
::Illrequestattributes
;
27 use Koha
::DateUtils
qw( format_sqldatetime );
31 Koha::REST::V1::Illrequests
37 Return a list of ILL requests, after applying filters.
42 my $c = shift->openapi->valid_input or return;
44 my $args = $c->req->params->to_hash // {};
46 my @format_dates = ( 'placed', 'updated', 'completed' );
48 # Create a hash where all keys are embedded values
49 # Enables easy checking
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
};
58 # If necessary, only get those from a specified patron
59 my @requests = Koha
::Illrequests
->search({
60 $args->{borrowernumber
}
61 ?
( borrowernumber
=> $args->{borrowernumber
} )
65 # Identify patrons & branches that
66 # we're going to need and get them
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
};
78 # Fetch the patrons we need
81 my @patron_ids = keys %{$to_fetch->{patrons
}};
82 if (scalar @patron_ids > 0) {
84 borrowernumber
=> { -in => \
@patron_ids }
86 $patron_arr = Koha
::Patrons
->search($where)->unblessed;
90 # Fetch the branches we need
92 if ($embed{library
}) {
93 my @branchcodes = keys %{$to_fetch->{branches
}};
94 if (scalar @branchcodes > 0) {
96 branchcode
=> { -in => \
@branchcodes }
98 $branch_arr = Koha
::Libraries
->search($where)->unblessed;
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;
113 # Now we've got all associated users and branches,
114 # we can augment the request objects
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
(
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
}
143 foreach my $b(@
{$branch_arr}) {
144 if ($b->{branchcode
} eq $req->branchcode) {
145 $to_push->{library
} = $b;
149 if ($embed{metadata
}) {
150 my $metadata = Koha
::Illrequestattributes
->search(
151 { illrequest_id
=> $req->illrequest_id },
152 { columns
=> [qw
/type value/] }
155 foreach my $meta(@
{$metadata}) {
156 $meta_hash->{$meta->{type
}} = $meta->{value
};
158 $to_push->{metadata
} = $meta_hash;
160 if ($embed{capabilities
}) {
161 $to_push->{capabilities
} = $to_fetch->{$req->backend};
163 if ($embed{comments
}) {
164 $to_push->{comments
} = $req->illcomments->count;
166 if ($embed{status_alias
}) {
167 $to_push->{status_alias
} = $req->statusalias;
169 if ($embed{requested_partners
}) {
170 $to_push->{requested_partners
} = $req->requested_partners;
172 push @output, $to_push;
175 return $c->render( status
=> 200, openapi
=> \
@output );