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';
23 use Koha::Illrequests;
24 use Koha::Illrequestattributes;
28 use Koha::DateUtils qw( format_sqldatetime );
32 Koha::REST::V1::Illrequests
38 Return a list of ILL requests, after applying filters.
43 my $c = shift->openapi->valid_input or return;
45 my $args = $c->req->params->to_hash // {};
47 my @format_dates = ( 'placed', 'updated', 'completed' );
49 # Create a hash where all keys are embedded values
50 # Enables easy checking
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};
58 # Get the pipe-separated string of hidden ILL statuses
59 my $hidden_statuses_string = C4::Context->preference('ILLHiddenRequestStatuses') // q{};
61 my $hidden_statuses = [ split /\|/, $hidden_statuses_string ];
64 # If necessary, only get those from a specified patron
65 my @requests = Koha::Illrequests->search({
67 ? ( status => { 'not in' => $hidden_statuses } )
69 $args->{borrowernumber}
70 ? ( borrowernumber => $args->{borrowernumber} )
74 # Identify patrons & branches that
75 # we're going to need and get them
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};
87 # Fetch the patrons we need
90 my @patron_ids = keys %{$to_fetch->{patrons}};
91 if (scalar @patron_ids > 0) {
93 borrowernumber => { -in => \@patron_ids }
95 $patron_arr = Koha::Patrons->search($where)->unblessed;
99 # Fetch the branches we need
101 if ($embed{library}) {
102 my @branchcodes = keys %{$to_fetch->{branches}};
103 if (scalar @branchcodes > 0) {
105 branchcode => { -in => \@branchcodes }
107 $branch_arr = Koha::Libraries->search($where)->unblessed;
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;
122 # Now we've got all associated users and branches,
123 # we can augment the request objects
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(
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}
152 foreach my $b(@{$branch_arr}) {
153 if ($b->{branchcode} eq $req->branchcode) {
154 $to_push->{library} = $b;
158 if ($embed{metadata}) {
159 my $metadata = Koha::Illrequestattributes->search(
160 { illrequest_id => $req->illrequest_id },
161 { columns => [qw/type value/] }
164 foreach my $meta(@{$metadata}) {
165 $meta_hash->{$meta->{type}} = $meta->{value};
167 $to_push->{metadata} = $meta_hash;
169 if ($embed{capabilities}) {
170 $to_push->{capabilities} = $to_fetch->{$req->backend};
172 if ($embed{comments}) {
173 $to_push->{comments} = $req->illcomments->count;
175 if ($embed{status_alias}) {
176 $to_push->{status_alias} = $req->statusalias;
178 if ($embed{requested_partners}) {
179 $to_push->{requested_partners} = $req->requested_partners;
181 push @output, $to_push;
184 return $c->render( status => 200, openapi => \@output );