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($_);
64 Controller function that handles retrieving a single Koha::Item
69 my $c = shift->openapi->valid_input or return;
72 my $items_rs = Koha::Items->new;
73 my $item = $c->objects->find($items_rs, $c->validation->param('item_id'));
77 openapi => { error => 'Item not found'}
80 return $c->render( status => 200, openapi => $item );
83 $c->unhandled_exception($_);
87 =head3 pickup_locations
89 Method that returns the possible pickup_locations for a given item
90 used for building the dropdown selector
94 sub pickup_locations {
95 my $c = shift->openapi->valid_input or return;
97 my $item_id = $c->validation->param('item_id');
98 my $item = Koha::Items->find( $item_id );
103 openapi => { error => "Item not found" }
107 my $patron_id = delete $c->validation->output->{patron_id};
108 my $patron = Koha::Patrons->find( $patron_id );
113 openapi => { error => "Patron not found" }
119 my $pl_set = $item->pickup_locations( { patron => $patron } );
122 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
124 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
125 my $libraries = $c->objects->search($libraries_rs);
129 $library->{needs_override} = (
130 any { $_->branchcode eq $library->{library_id} }
131 @{ $pl_set->as_list }
140 my $pickup_locations = $c->objects->search($pl_set);
141 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
146 openapi => \@response
150 $c->unhandled_exception($_);
156 Controller function that handles bundled_items Koha::Item objects
161 my $c = shift->openapi->valid_input or return;
163 my $item_id = $c->validation->param('item_id');
164 my $item = Koha::Items->find( $item_id );
169 openapi => { error => "Item not found" }
174 my $items_set = $item->bundle_items;
175 my $items = $c->objects->search( $items_set );
182 $c->unhandled_exception($_);
188 Controller function that handles adding items to this bundle
193 my $c = shift->openapi->valid_input or return;
195 my $item_id = $c->validation->param('item_id');
196 my $item = Koha::Items->find( $item_id );
201 openapi => { error => "Item not found" }
205 my $bundle_item_id = $c->validation->param('body')->{'external_id'};
206 $bundle_item_id = barcodedecode($bundle_item_id);
207 my $bundle_item = Koha::Items->find( { barcode => $bundle_item_id } );
209 unless ($bundle_item) {
212 openapi => { error => "Bundle item not found" }
217 my $force_checkin = $c->validation->param('body')->{'force_checkin'};
218 my $link = $item->add_to_bundle($bundle_item, { force_checkin => $force_checkin });
221 openapi => $bundle_item
225 if ( ref($_) eq 'Koha::Exceptions::Object::DuplicateID' ) {
229 error => 'Item is already bundled',
230 error_code => 'already_bundled',
231 key => $_->duplicate_id
235 elsif ( ref($_) eq 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut' )
240 error => 'Item is checked out',
241 error_code => 'checked_out'
245 elsif ( ref($_) eq 'Koha::Exceptions::Checkin::FailedCheckin' ) {
249 error => 'Item cannot be checked in',
250 error_code => 'failed_checkin'
254 elsif ( ref($_) eq 'Koha::Exceptions::Item::Bundle::IsBundle' ) {
258 error => 'Bundles cannot be nested',
259 error_code => 'failed_nesting'
264 $c->unhandled_exception($_);
269 =head3 remove_from_bundle
271 Controller function that handles removing items from this bundle
275 sub remove_from_bundle {
276 my $c = shift->openapi->valid_input or return;
278 my $item_id = $c->validation->param('item_id');
279 my $item = Koha::Items->find( $item_id );
284 openapi => { error => "Item not found" }
288 my $bundle_item_id = $c->validation->param('bundled_item_id');
289 $bundle_item_id = barcodedecode($bundle_item_id);
290 my $bundle_item = Koha::Items->find( { itemnumber => $bundle_item_id } );
292 unless ($bundle_item) {
295 openapi => { error => "Bundle item not found" }
299 $bundle_item->remove_from_bundle;