1 package Koha::REST::V1::Items;
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 C4::Circulation qw( barcodedecode );
26 use List::MoreUtils qw( any );
27 use Try::Tiny qw( catch try );
31 Koha::REST::V1::Items - Koha REST API for handling items (V1)
41 Controller function that handles listing Koha::Item objects
46 my $c = shift->openapi->valid_input or return;
49 my $items_set = Koha::Items->new;
50 my $items = $c->objects->search( $items_set );
57 $c->unhandled_exception($_);
63 Controller function that handles retrieving a single Koha::Item
68 my $c = shift->openapi->valid_input or return;
71 my $items_rs = Koha::Items->new;
72 my $item = $c->objects->find($items_rs, $c->validation->param('item_id'));
76 openapi => { error => 'Item not found'}
79 return $c->render( status => 200, openapi => $item );
82 $c->unhandled_exception($_);
88 Controller function that handles deleting a single Koha::Item
93 my $c = shift->openapi->valid_input or return;
96 my $item = Koha::Items->find($c->validation->param('item_id'));
100 openapi => { error => 'Item not found'}
104 my $safe_to_delete = $item->safe_to_delete;
106 if ( !$safe_to_delete ) {
108 # Pick the first error, if any
109 my ( $error ) = grep { $_->type eq 'error' } @{ $safe_to_delete->messages };
112 Koha::Exception->throw('Koha::Item->safe_to_delete returned false but carried no error message');
116 book_on_loan => { code => 'checked_out', description => 'The item is checked out' },
117 book_reserved => { code => 'found_hold', description => 'Waiting or in-transit hold for the item' },
118 last_item_for_hold => { code => 'last_item_for_hold', description => 'The item is the last one on a record on which a biblio-level hold is placed' },
119 linked_analytics => { code => 'linked_analytics', description => 'The item has linked analytic records' },
120 not_same_branch => { code => 'not_same_branch', description => 'The item is blocked by independent branches' },
123 if ( any { $error->message eq $_ } keys %{$errors} ) {
125 my $code = $error->message;
130 error => $errors->{ $code }->{description},
131 error_code => $errors->{ $code }->{code},
135 Koha::Exception->throw( 'Koha::Patron->safe_to_delete carried an unexpected message: ' . $error->message );
147 $c->unhandled_exception($_);
151 =head3 pickup_locations
153 Method that returns the possible pickup_locations for a given item
154 used for building the dropdown selector
158 sub pickup_locations {
159 my $c = shift->openapi->valid_input or return;
161 my $item_id = $c->validation->param('item_id');
162 my $item = Koha::Items->find( $item_id );
167 openapi => { error => "Item not found" }
171 my $patron_id = delete $c->validation->output->{patron_id};
172 my $patron = Koha::Patrons->find( $patron_id );
177 openapi => { error => "Patron not found" }
183 my $pl_set = $item->pickup_locations( { patron => $patron } );
186 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
188 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
189 my $libraries = $c->objects->search($libraries_rs);
193 $library->{needs_override} = (
194 any { $_->branchcode eq $library->{library_id} }
195 @{ $pl_set->as_list }
204 my $pickup_locations = $c->objects->search($pl_set);
205 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
210 openapi => \@response
214 $c->unhandled_exception($_);
220 Controller function that handles bundled_items Koha::Item objects
225 my $c = shift->openapi->valid_input or return;
227 my $item_id = $c->validation->param('item_id');
228 my $item = Koha::Items->find( $item_id );
233 openapi => { error => "Item not found" }
238 my $items_set = $item->bundle_items;
239 my $items = $c->objects->search( $items_set );
246 $c->unhandled_exception($_);
252 Controller function that handles adding items to this bundle
257 my $c = shift->openapi->valid_input or return;
259 my $item_id = $c->validation->param('item_id');
260 my $item = Koha::Items->find( $item_id );
265 openapi => { error => "Item not found" }
269 my $bundle_item_id = $c->validation->param('body')->{'external_id'};
270 $bundle_item_id = barcodedecode($bundle_item_id);
271 my $bundle_item = Koha::Items->find( { barcode => $bundle_item_id } );
273 unless ($bundle_item) {
276 openapi => { error => "Bundle item not found" }
281 my $force_checkin = $c->validation->param('body')->{'force_checkin'};
282 my $link = $item->add_to_bundle($bundle_item, { force_checkin => $force_checkin });
285 openapi => $bundle_item
289 if ( ref($_) eq 'Koha::Exceptions::Object::DuplicateID' ) {
293 error => 'Item is already bundled',
294 key => $_->duplicate_id
298 elsif ( ref($_) eq 'Koha::Exceptions::Item::Bundle::IsBundle' ) {
302 error => 'Bundles cannot be nested'
305 } elsif (ref($_) eq 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut') {
309 error => 'Item is checked out',
313 } elsif (ref($_) eq 'Koha::Exceptions::Checkin::FailedCheckin') {
317 error => 'Item cannot be checked in',
318 key => 'failed_checkin'
322 $c->unhandled_exception($_);
327 =head3 remove_from_bundle
329 Controller function that handles removing items from this bundle
333 sub remove_from_bundle {
334 my $c = shift->openapi->valid_input or return;
336 my $item_id = $c->validation->param('item_id');
337 my $item = Koha::Items->find( $item_id );
342 openapi => { error => "Item not found" }
346 my $bundle_item_id = $c->validation->param('bundled_item_id');
347 $bundle_item_id = barcodedecode($bundle_item_id);
348 my $bundle_item = Koha::Items->find( { itemnumber => $bundle_item_id } );
350 unless ($bundle_item) {
353 openapi => { error => "Bundle item not found" }
357 $bundle_item->remove_from_bundle;