# This file is part of Koha.
#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Mojo::Base 'Mojolicious::Controller';
-use Mojo::JSON;
-use C4::Auth qw( haspermission );
-use C4::Items qw( GetHiddenItemnumbers );
+use C4::Circulation qw( barcodedecode );
use Koha::Items;
-use Try::Tiny;
+use List::MoreUtils qw( any );
+use Try::Tiny qw( catch try );
+
+=head1 NAME
+
+Koha::REST::V1::Items - Koha REST API for handling items (V1)
+
+=head1 API
+
+=head2 Methods
+
+=cut
+
+=head3 list
+
+Controller function that handles listing Koha::Item objects
+
+=cut
+
+sub list {
+ my $c = shift->openapi->valid_input or return;
+
+ return try {
+ my $items_set = Koha::Items->new;
+ my $items = $c->objects->search( $items_set );
+ return $c->render(
+ status => 200,
+ openapi => $items
+ );
+ }
+ catch {
+ $c->unhandled_exception($_);
+ };
+}
+
+=head3 get
+
+Controller function that handles retrieving a single Koha::Item
+
+=cut
sub get {
my $c = shift->openapi->valid_input or return;
- my $item;
try {
- $item = Koha::Items->find($c->validation->param('item_id'));
- return $c->render( status => 200, openapi => _to_api( $item->TO_JSON ) );
+ my $item = Koha::Items->find($c->validation->param('item_id'));
+ unless ( $item ) {
+ return $c->render(
+ status => 404,
+ openapi => { error => 'Item not found'}
+ );
+ }
+ return $c->render( status => 200, openapi => $item->to_api );
}
catch {
- unless ( defined $item ) {
- return $c->render( status => 404,
- openapi => { error => 'Item not found'} );
- }
- if ( $_->isa('DBIx::Class::Exception') ) {
- return $c->render( status => 500,
- openapi => { error => $_->{msg} } );
- }
- else {
- return $c->render( status => 500,
- openapi => { error => "Something went wrong, check the logs."} );
- }
+ $c->unhandled_exception($_);
};
}
-=head3 _to_api
+=head3 delete
-Helper function that maps unblessed Koha::Hold objects into REST api
-attribute names.
+Controller function that handles deleting a single Koha::Item
=cut
-sub _to_api {
- my $item = shift;
-
- # Rename attributes
- foreach my $column ( keys %{ $Koha::REST::V1::Items::to_api_mapping } ) {
- my $mapped_column = $Koha::REST::V1::Items::to_api_mapping->{$column};
- if ( exists $item->{ $column }
- && defined $mapped_column )
- {
- # key != undef
- $item->{ $mapped_column } = delete $item->{ $column };
+sub delete {
+ my $c = shift->openapi->valid_input or return;
+
+ return try {
+ my $item = Koha::Items->find($c->validation->param('item_id'));
+ unless ( $item ) {
+ return $c->render(
+ status => 404,
+ openapi => { error => 'Item not found'}
+ );
}
- elsif ( exists $item->{ $column }
- && !defined $mapped_column )
- {
- # key == undef
- delete $item->{ $column };
+
+ my $safe_to_delete = $item->safe_to_delete;
+
+ if ( !$safe_to_delete ) {
+
+ # Pick the first error, if any
+ my ( $error ) = grep { $_->type eq 'error' } @{ $safe_to_delete->messages };
+
+ unless ( $error ) {
+ Koha::Exception->throw('Koha::Item->safe_to_delete returned false but carried no error message');
+ }
+
+ my $errors = {
+ book_on_loan => { code => 'checked_out', description => 'The item is checked out' },
+ book_reserved => { code => 'found_hold', description => 'Waiting or in-transit hold for the item' },
+ 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' },
+ linked_analytics => { code => 'linked_analytics', description => 'The item has linked analytic records' },
+ not_same_branch => { code => 'not_same_branch', description => 'The item is blocked by independent branches' },
+ };
+
+ if ( any { $error->message eq $_ } keys %{$errors} ) {
+
+ my $code = $error->message;
+
+ return $c->render(
+ status => 409,
+ openapi => {
+ error => $errors->{ $code }->{description},
+ error_code => $errors->{ $code }->{code},
+ }
+ );
+ } else {
+ Koha::Exception->throw( 'Koha::Patron->safe_to_delete carried an unexpected message: ' . $error->message );
+ }
}
- }
- return $item;
+ $item->safe_delete;
+
+ return $c->render(
+ status => 204,
+ openapi => q{}
+ );
+ }
+ catch {
+ $c->unhandled_exception($_);
+ };
}
-=head3 _to_model
+=head3 pickup_locations
-Helper function that maps REST api objects into Koha::Hold
-attribute names.
+Method that returns the possible pickup_locations for a given item
+used for building the dropdown selector
=cut
-sub _to_model {
- my $item = shift;
+sub pickup_locations {
+ my $c = shift->openapi->valid_input or return;
+
+ my $item_id = $c->validation->param('item_id');
+ my $item = Koha::Items->find( $item_id );
+
+ unless ($item) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Item not found" }
+ );
+ }
+
+ my $patron_id = delete $c->validation->output->{patron_id};
+ my $patron = Koha::Patrons->find( $patron_id );
+
+ unless ($patron) {
+ return $c->render(
+ status => 400,
+ openapi => { error => "Patron not found" }
+ );
+ }
+
+ return try {
- foreach my $attribute ( keys %{ $Koha::REST::V1::Items::to_model_mapping } ) {
- my $mapped_attribute = $Koha::REST::V1::Items::to_model_mapping->{$attribute};
- if ( exists $item->{ $attribute }
- && defined $mapped_attribute )
- {
- # key => !undef
- $item->{ $mapped_attribute } = delete $item->{ $attribute };
+ my $pl_set = $item->pickup_locations( { patron => $patron } );
+
+ my @response = ();
+ if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
+
+ my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
+ my $libraries = $c->objects->search($libraries_rs);
+
+ @response = map {
+ my $library = $_;
+ $library->{needs_override} = (
+ any { $_->branchcode eq $library->{library_id} }
+ @{ $pl_set->as_list }
+ )
+ ? Mojo::JSON->false
+ : Mojo::JSON->true;
+ $library;
+ } @{$libraries};
}
- elsif ( exists $item->{ $attribute }
- && !defined $mapped_attribute )
- {
- # key => undef / to be deleted
- delete $item->{ $attribute };
+ else {
+
+ my $pickup_locations = $c->objects->search($pl_set);
+ @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
}
+
+ return $c->render(
+ status => 200,
+ openapi => \@response
+ );
+ }
+ catch {
+ $c->unhandled_exception($_);
+ };
+}
+
+=head3 bundled_items
+
+Controller function that handles bundled_items Koha::Item objects
+
+=cut
+
+sub bundled_items {
+ my $c = shift->openapi->valid_input or return;
+
+ my $item_id = $c->validation->param('item_id');
+ my $item = Koha::Items->find( $item_id );
+
+ unless ($item) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Item not found" }
+ );
}
- return $item;
+ return try {
+ my $items_set = $item->bundle_items;
+ my $items = $c->objects->search( $items_set );
+ return $c->render(
+ status => 200,
+ openapi => $items
+ );
+ }
+ catch {
+ $c->unhandled_exception($_);
+ };
}
-=head2 Global variables
+=head3 add_to_bundle
-=head3 $to_api_mapping
+Controller function that handles adding items to this bundle
=cut
-our $to_api_mapping = {
- itemnumber => 'item_id',
- biblionumber => 'biblio_id',
- biblioitemnumber => undef,
- barcode => 'external_id',
- dateaccessioned => 'acquisition_date',
- booksellerid => 'acquisition_source',
- homebranch => 'home_library_id',
- price => 'purchase_price',
- replacementprice => 'replacement_price',
- replacementpricedate => 'replacement_price_date',
- datelastborrowed => 'last_checkout_date',
- datelastseen => 'last_seen_date',
- stack => undef,
- notforloan => 'not_for_loan_status',
- damaged => 'damaged_status',
- damaged_on => 'damaged_date',
- itemlost => 'lost_status',
- itemlost_on => 'lost_date',
- withdrawn => 'withdrawn',
- withdrawn_on => 'withdrawn_date',
- itemcallnumber => 'callnumber',
- coded_location_qualifier => 'coded_location_qualifier',
- issues => 'checkouts_count',
- renewals => 'renewals_count',
- reserves => 'holds_count',
- restricted => 'restricted_status',
- itemnotes => 'public_notes',
- itemnotes_nonpublic => 'internal_notes',
- holdingbranch => 'holding_library_id',
- paidfor => undef,
- timestamp => 'timestamp',
- location => 'location',
- permanent_location => 'permanent_location',
- onloan => 'checked_out_date',
- cn_source => 'call_number_source',
- cn_sort => 'call_number_sort',
- ccode => 'collection_code',
- materials => 'materials_notes',
- uri => 'uri',
- itype => 'item_type',
- more_subfields_xml => 'extended_subfields',
- enumchron => 'serial_issue_number',
- copynumber => 'copy_number',
- stocknumber => 'inventory_number',
- new_status => 'new_status'
-};
-
-=head3 $to_model_mapping
+sub add_to_bundle {
+ my $c = shift->openapi->valid_input or return;
+
+ my $item_id = $c->validation->param('item_id');
+ my $item = Koha::Items->find( $item_id );
+
+ unless ($item) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Item not found" }
+ );
+ }
+
+ my $bundle_item_id = $c->validation->param('body')->{'external_id'};
+ $bundle_item_id = barcodedecode($bundle_item_id);
+ my $bundle_item = Koha::Items->find( { barcode => $bundle_item_id } );
+
+ unless ($bundle_item) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Bundle item not found" }
+ );
+ }
+
+ return try {
+ my $link = $item->add_to_bundle($bundle_item);
+ return $c->render(
+ status => 201,
+ openapi => $bundle_item
+ );
+ }
+ catch {
+ if ( ref($_) eq 'Koha::Exceptions::Object::DuplicateID' ) {
+ return $c->render(
+ status => 409,
+ openapi => {
+ error => 'Item is already bundled',
+ key => $_->duplicate_id
+ }
+ );
+ }
+ elsif ( ref($_) eq 'Koha::Exceptions::Item::Bundle::IsBundle' ) {
+ return $c->render(
+ status => 400,
+ openapi => {
+ error => 'Bundles cannot be nested'
+ }
+ );
+ }
+ else {
+ $c->unhandled_exception($_);
+ }
+ };
+}
+
+=head3 remove_from_bundle
+
+Controller function that handles removing items from this bundle
=cut
-our $to_model_mapping = {
- item_id => 'itemnumber',
- biblio_id => 'biblionumber',
- external_id => 'barcode',
- acquisition_date => 'dateaccessioned',
- acquisition_source => 'booksellerid',
- home_library_id => 'homebranch',
- purchase_price => 'price',
- replacement_price => 'replacementprice',
- replacement_price_date => 'replacementpricedate',
- last_checkout_date => 'datelastborrowed',
- last_seen_date => 'datelastseen',
- not_for_loan_status => 'notforloan',
- damaged_status => 'damaged',
- damaged_date => 'damaged_on',
- lost_status => 'itemlost',
- lost_date => 'itemlost_on',
- withdrawn => 'withdrawn',
- withdrawn_date => 'withdrawn_on',
- callnumber => 'itemcallnumber',
- coded_location_qualifier => 'coded_location_qualifier',
- checkouts_count => 'issues',
- renewals_count => 'renewals',
- holds_count => 'reserves',
- restricted_status => 'restricted',
- public_notes => 'itemnotes',
- internal_notes => 'itemnotes_nonpublic',
- holding_library_id => 'holdingbranch',
- timestamp => 'timestamp',
- location => 'location',
- permanent_location => 'permanent_location',
- checked_out_date => 'onloan',
- call_number_source => 'cn_source',
- call_number_sort => 'cn_sort',
- collection_code => 'ccode',
- materials_notes => 'materials',
- uri => 'uri',
- item_type => 'itype',
- extended_subfields => 'more_subfields_xml',
- serial_issue_number => 'enumchron',
- copy_number => 'copynumber',
- inventory_number => 'stocknumber',
- new_status => 'new_status'
-};
+sub remove_from_bundle {
+ my $c = shift->openapi->valid_input or return;
+
+ my $item_id = $c->validation->param('item_id');
+ my $item = Koha::Items->find( $item_id );
+
+ unless ($item) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Item not found" }
+ );
+ }
+
+ my $bundle_item_id = $c->validation->param('bundled_item_id');
+ $bundle_item_id = barcodedecode($bundle_item_id);
+ my $bundle_item = Koha::Items->find( { itemnumber => $bundle_item_id } );
+
+ unless ($bundle_item) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Bundle item not found" }
+ );
+ }
+
+ $bundle_item->remove_from_bundle;
+ return $c->render(
+ status => 204,
+ openapi => q{}
+ );
+}
1;