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 $item = Koha::Items->find($c->validation->param('item_id'));
75 openapi => { error => 'Item not found'}
78 return $c->render( status => 200, openapi => $item->to_api );
81 $c->unhandled_exception($_);
87 Controller function that handles deleting a single Koha::Item
92 my $c = shift->openapi->valid_input or return;
95 my $item = Koha::Items->find($c->validation->param('item_id'));
99 openapi => { error => 'Item not found'}
103 my $safe_to_delete = $item->safe_to_delete;
105 if ( !$safe_to_delete ) {
107 # Pick the first error, if any
108 my ( $error ) = grep { $_->type eq 'error' } @{ $safe_to_delete->messages };
111 Koha::Exception->throw('Koha::Item->safe_to_delete returned false but carried no error message');
115 book_on_loan => { code => 'checked_out', description => 'The item is checked out' },
116 book_reserved => { code => 'found_hold', description => 'Waiting or in-transit hold for the item' },
117 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' },
118 linked_analytics => { code => 'linked_analytics', description => 'The item has linked analytic records' },
119 not_same_branch => { code => 'not_same_branch', description => 'The item is blocked by independent branches' },
122 if ( any { $error->message eq $_ } keys %{$errors} ) {
124 my $code = $error->message;
129 error => $errors->{ $code }->{description},
130 error_code => $errors->{ $code }->{code},
134 Koha::Exception->throw( 'Koha::Patron->safe_to_delete carried an unexpected message: ' . $error->message );
146 $c->unhandled_exception($_);
150 =head3 pickup_locations
152 Method that returns the possible pickup_locations for a given item
153 used for building the dropdown selector
157 sub pickup_locations {
158 my $c = shift->openapi->valid_input or return;
160 my $item_id = $c->validation->param('item_id');
161 my $item = Koha::Items->find( $item_id );
166 openapi => { error => "Item not found" }
170 my $patron_id = delete $c->validation->output->{patron_id};
171 my $patron = Koha::Patrons->find( $patron_id );
176 openapi => { error => "Patron not found" }
182 my $pl_set = $item->pickup_locations( { patron => $patron } );
185 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
187 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
188 my $libraries = $c->objects->search($libraries_rs);
192 $library->{needs_override} = (
193 any { $_->branchcode eq $library->{library_id} }
194 @{ $pl_set->as_list }
203 my $pickup_locations = $c->objects->search($pl_set);
204 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
209 openapi => \@response
213 $c->unhandled_exception($_);
219 Controller function that handles bundled_items Koha::Item objects
224 my $c = shift->openapi->valid_input or return;
226 my $item_id = $c->validation->param('item_id');
227 my $item = Koha::Items->find( $item_id );
232 openapi => { error => "Item not found" }
237 my $items_set = $item->bundle_items;
238 my $items = $c->objects->search( $items_set );
245 $c->unhandled_exception($_);
251 Controller function that handles adding items to this bundle
256 my $c = shift->openapi->valid_input or return;
258 my $item_id = $c->validation->param('item_id');
259 my $item = Koha::Items->find( $item_id );
264 openapi => { error => "Item not found" }
268 my $bundle_item_id = $c->validation->param('body')->{'external_id'};
269 $bundle_item_id = barcodedecode($bundle_item_id);
270 my $bundle_item = Koha::Items->find( { barcode => $bundle_item_id } );
272 unless ($bundle_item) {
275 openapi => { error => "Bundle item not found" }
280 my $link = $item->add_to_bundle($bundle_item);
283 openapi => $bundle_item
287 if ( ref($_) eq 'Koha::Exceptions::Object::DuplicateID' ) {
291 error => 'Item is already bundled',
292 key => $_->duplicate_id
296 elsif ( ref($_) eq 'Koha::Exceptions::Item::Bundle::IsBundle' ) {
300 error => 'Bundles cannot be nested'
305 $c->unhandled_exception($_);
310 =head3 remove_from_bundle
312 Controller function that handles removing items from this bundle
316 sub remove_from_bundle {
317 my $c = shift->openapi->valid_input or return;
319 my $item_id = $c->validation->param('item_id');
320 my $item = Koha::Items->find( $item_id );
325 openapi => { error => "Item not found" }
329 my $bundle_item_id = $c->validation->param('bundled_item_id');
330 $bundle_item_id = barcodedecode($bundle_item_id);
331 my $bundle_item = Koha::Items->find( { itemnumber => $bundle_item_id } );
333 unless ($bundle_item) {
336 openapi => { error => "Bundle item not found" }
340 $bundle_item->remove_from_bundle;