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 $item = Koha::Items->find($c->validation->param('item_id'));
76 openapi => { error => 'Item not found'}
79 return $c->render( status => 200, openapi => $item->to_api );
82 $c->unhandled_exception($_);
86 =head3 pickup_locations
88 Method that returns the possible pickup_locations for a given item
89 used for building the dropdown selector
93 sub pickup_locations {
94 my $c = shift->openapi->valid_input or return;
96 my $item_id = $c->validation->param('item_id');
97 my $item = Koha::Items->find( $item_id );
102 openapi => { error => "Item not found" }
106 my $patron_id = delete $c->validation->output->{patron_id};
107 my $patron = Koha::Patrons->find( $patron_id );
112 openapi => { error => "Patron not found" }
118 my $pl_set = $item->pickup_locations( { patron => $patron } );
121 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
123 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
124 my $libraries = $c->objects->search($libraries_rs);
128 $library->{needs_override} = (
129 any { $_->branchcode eq $library->{library_id} }
130 @{ $pl_set->as_list }
139 my $pickup_locations = $c->objects->search($pl_set);
140 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
145 openapi => \@response
149 $c->unhandled_exception($_);
155 Controller function that handles bundled_items Koha::Item objects
160 my $c = shift->openapi->valid_input or return;
162 my $item_id = $c->validation->param('item_id');
163 my $item = Koha::Items->find( $item_id );
168 openapi => { error => "Item not found" }
173 my $items_set = $item->bundle_items;
174 my $items = $c->objects->search( $items_set );
181 $c->unhandled_exception($_);
187 Controller function that handles adding items to this bundle
192 my $c = shift->openapi->valid_input or return;
194 my $item_id = $c->validation->param('item_id');
195 my $item = Koha::Items->find( $item_id );
200 openapi => { error => "Item not found" }
204 my $bundle_item_id = $c->validation->param('body')->{'external_id'};
205 $bundle_item_id = barcodedecode($bundle_item_id);
206 my $bundle_item = Koha::Items->find( { barcode => $bundle_item_id } );
208 unless ($bundle_item) {
211 openapi => { error => "Bundle item not found" }
216 my $link = $item->add_to_bundle($bundle_item);
219 openapi => $bundle_item
223 if ( ref($_) eq 'Koha::Exceptions::Object::DuplicateID' ) {
227 error => 'Item is already bundled',
228 key => $_->duplicate_id
233 $c->unhandled_exception($_);
238 =head3 remove_from_bundle
240 Controller function that handles removing items from this bundle
244 sub remove_from_bundle {
245 my $c = shift->openapi->valid_input or return;
247 my $item_id = $c->validation->param('item_id');
248 my $item = Koha::Items->find( $item_id );
253 openapi => { error => "Item not found" }
257 my $bundle_item_id = $c->validation->param('bundled_item_id');
258 $bundle_item_id = barcodedecode($bundle_item_id);
259 my $bundle_item = Koha::Items->find( { itemnumber => $bundle_item_id } );
261 unless ($bundle_item) {
264 openapi => { error => "Bundle item not found" }
268 $bundle_item->remove_from_bundle;