|
Line 0
Link Here
|
| 0 |
- |
1 |
package Koha::REST::V1::ItemAvailability; |
|
|
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 C4::Auth qw( haspermission ); |
| 23 |
|
| 24 |
use Koha::Exceptions; |
| 25 |
|
| 26 |
use Koha::Availability::Checkout; |
| 27 |
use Koha::Availability::Hold; |
| 28 |
use Koha::Availability::Search; |
| 29 |
|
| 30 |
use Try::Tiny; |
| 31 |
|
| 32 |
sub checkout { |
| 33 |
my ($c, $args, $cb) = @_; |
| 34 |
|
| 35 |
my @availabilities; |
| 36 |
my $user = $c->stash('koha.user'); |
| 37 |
my $borrowernumber = $args->{'borrowernumber'}; |
| 38 |
my $patron; |
| 39 |
my $librarian; |
| 40 |
|
| 41 |
return try { |
| 42 |
($patron, $librarian) = _get_patron($c, $user, $borrowernumber); |
| 43 |
|
| 44 |
my $items = $args->{'itemnumber'}; |
| 45 |
foreach my $itemnumber (@$items) { |
| 46 |
if (my $item = Koha::Items->find($itemnumber)) { |
| 47 |
push @availabilities, Koha::Availability::Checkout->item({ |
| 48 |
patron => $patron, |
| 49 |
item => $item |
| 50 |
})->in_intranet->swaggerize; |
| 51 |
} |
| 52 |
} |
| 53 |
return $c->$cb(\@availabilities, 200); |
| 54 |
} |
| 55 |
catch { |
| 56 |
if ( $_->isa('DBIx::Class::Exception') ) { |
| 57 |
return $c->$cb( { error => $_->{msg} }, 500 ); |
| 58 |
} |
| 59 |
elsif ($_->isa('Koha::Exceptions::AuthenticationRequired')) { |
| 60 |
return $c->$cb( |
| 61 |
{ error => "Authentication required." }, 401 ); |
| 62 |
} |
| 63 |
elsif ($_->isa('Koha::Exceptions::NoPermission')) { |
| 64 |
return $c->$cb({ |
| 65 |
error => "Authorization failure. Missing required permission(s).", |
| 66 |
required_permissions => $_->required_permissions}, 403 ); |
| 67 |
} |
| 68 |
else { |
| 69 |
return $c->$cb( |
| 70 |
{ error => "Something went wrong, check the logs. $_" }, 500 ); |
| 71 |
} |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
sub hold { |
| 76 |
my ($c, $args, $cb) = @_; |
| 77 |
|
| 78 |
my @availabilities; |
| 79 |
my $user = $c->stash('koha.user'); |
| 80 |
my $borrowernumber = $args->{'borrowernumber'}; |
| 81 |
my $to_branch = $args->{'branchcode'}; |
| 82 |
my $patron; |
| 83 |
my $librarian; |
| 84 |
|
| 85 |
return try { |
| 86 |
($patron, $librarian) = _get_patron($c, $user, $borrowernumber); |
| 87 |
|
| 88 |
my $items = $args->{'itemnumber'}; |
| 89 |
my $params = { |
| 90 |
patron => $patron, |
| 91 |
}; |
| 92 |
if ($to_branch) { |
| 93 |
$params->{'to_branch'} = $to_branch; |
| 94 |
} |
| 95 |
foreach my $itemnumber (@$items) { |
| 96 |
if (my $item = Koha::Items->find($itemnumber)) { |
| 97 |
$params->{'item'} = $item; |
| 98 |
my $availability = Koha::Availability::Hold->item($params); |
| 99 |
unless ($librarian) { |
| 100 |
push @availabilities, $availability->in_opac->swaggerize; |
| 101 |
} else { |
| 102 |
push @availabilities, $availability->in_intranet->swaggerize; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return $c->$cb(\@availabilities, 200); |
| 108 |
} |
| 109 |
catch { |
| 110 |
if ( $_->isa('DBIx::Class::Exception') ) { |
| 111 |
return $c->$cb( { error => $_->{msg} }, 500 ); |
| 112 |
} |
| 113 |
elsif ($_->isa('Koha::Exceptions::AuthenticationRequired')) { |
| 114 |
return $c->$cb( |
| 115 |
{ error => "Authentication required." }, 401 ); |
| 116 |
} |
| 117 |
elsif ($_->isa('Koha::Exceptions::NoPermission')) { |
| 118 |
return $c->$cb({ |
| 119 |
error => "Authorization failure. Missing required permission(s).", |
| 120 |
required_permissions => $_->required_permissions}, 403 ); |
| 121 |
} |
| 122 |
else { |
| 123 |
return $c->$cb( |
| 124 |
{ error => "Something went wrong, check the logs. $_" }, 500 ); |
| 125 |
} |
| 126 |
}; |
| 127 |
} |
| 128 |
|
| 129 |
sub search { |
| 130 |
my ($c, $args, $cb) = @_; |
| 131 |
|
| 132 |
my @availabilities; |
| 133 |
|
| 134 |
return try { |
| 135 |
my $items = $args->{'itemnumber'}; |
| 136 |
foreach my $itemnumber (@$items) { |
| 137 |
if (my $item = Koha::Items->find($itemnumber)) { |
| 138 |
push @availabilities, Koha::Availability::Search->item({ |
| 139 |
item => $item |
| 140 |
})->in_opac->swaggerize; |
| 141 |
} |
| 142 |
} |
| 143 |
return $c->$cb(\@availabilities, 200); |
| 144 |
} |
| 145 |
catch { |
| 146 |
if ( $_->isa('DBIx::Class::Exception') ) { |
| 147 |
return $c->$cb( { error => $_->{msg} }, 500 ); |
| 148 |
} |
| 149 |
else { |
| 150 |
return $c->$cb( |
| 151 |
{ error => "Something went wrong, check the logs. $_" }, 500 ); |
| 152 |
} |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
sub _get_patron { |
| 157 |
my ($c, $user, $borrowernumber) = @_; |
| 158 |
|
| 159 |
my $patron; |
| 160 |
my $librarian = 0; |
| 161 |
|
| 162 |
unless ($user) { |
| 163 |
Koha::Exceptions::AuthenticationRequired->throw; |
| 164 |
} |
| 165 |
if (haspermission($user->userid, { borrowers => 1 })) { |
| 166 |
$librarian = 1; |
| 167 |
} |
| 168 |
if ($borrowernumber) { |
| 169 |
if ($borrowernumber == $user->borrowernumber) { |
| 170 |
$patron = $user; |
| 171 |
} else { |
| 172 |
if ($librarian) { |
| 173 |
$patron = Koha::Patrons->find($borrowernumber); |
| 174 |
} else { |
| 175 |
Koha::Exceptions::NoPermission->throw( |
| 176 |
required_permissions => "borrowers" |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
} else { |
| 181 |
$patron = $user; |
| 182 |
} |
| 183 |
|
| 184 |
return ($patron, $librarian); |
| 185 |
} |
| 186 |
|
| 187 |
1; |