From 2f379249546f4c20e96725fa574ce66760c74aa5 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 18 Dec 2020 16:35:15 +0000 Subject: [PATCH] Bug 25757: Add Koha::Item::Transfer->transit method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test plan 1/ Run the included unit tests Signed-off-by: Kathleen Milne Signed-off-by: Katrin Fischer Signed-off-by: Joonas Kylmälä --- Koha/Exceptions/Item/Transfer.pm | 5 ++ Koha/Item/Transfer.pm | 37 ++++++++++++ t/db_dependent/Koha/Item/Transfer.t | 92 +++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100755 t/db_dependent/Koha/Item/Transfer.t diff --git a/Koha/Exceptions/Item/Transfer.pm b/Koha/Exceptions/Item/Transfer.pm index 746877bd04..40b247c59d 100644 --- a/Koha/Exceptions/Item/Transfer.pm +++ b/Koha/Exceptions/Item/Transfer.pm @@ -15,7 +15,12 @@ use Exception::Class ( 'Koha::Exceptions::Item::Transfer::Limit' => { isa => 'Koha::Exceptions::Item::Transfer', description => "Transfer not allowed" + }, + 'Koha::Exceptions::Item::Transfer::Out' => { + isa => 'Koha::Exceptions::Item::Transfer', + description => "Transfer item is currently checked out" } + ); 1; diff --git a/Koha/Item/Transfer.pm b/Koha/Item/Transfer.pm index 50868aba76..2928480c0a 100644 --- a/Koha/Item/Transfer.pm +++ b/Koha/Item/Transfer.pm @@ -19,7 +19,11 @@ use Modern::Perl; use Carp; +use C4::Items; + use Koha::Database; +use Koha::DateUtils; +use Koha::Exceptions::Item::Transfer; use base qw(Koha::Object); @@ -47,6 +51,39 @@ sub item { return Koha::Item->_new_from_dbic($item_rs); } +=head3 transit + +Set the transfer as in transit by updating the datesent time. + +Also, update date last seen and ensure item holdingbranch is correctly set. + +=cut + +sub transit { + my ($self) = @_; + + # Throw exception if item is still checked out + Koha::Exceptions::Item::Transfer::Out->throw() if ( $self->item->checkout ); + + # Remove the 'shelving cart' location status if it is being used (Bug 3701) + CartToShelf( $self->item->itemnumber ) + if $self->item->location + && $self->item->location eq 'CART' + && (!$self->item->permanent_location + || $self->item->permanent_location ne 'CART' ); + + # Update the transit state + $self->set( + { + frombranch => $self->item->holdingbranch, + datesent => dt_from_string, + } + )->store; + + ModDateLastSeen( $self->item->itemnumber ); + return $self; +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Item/Transfer.t b/t/db_dependent/Koha/Item/Transfer.t new file mode 100755 index 0000000000..37b5fbae75 --- /dev/null +++ b/t/db_dependent/Koha/Item/Transfer.t @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +# Copyright 2020 Koha Development team +# +# 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 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, see . + +use Modern::Perl; + +use Koha::Database; + +use t::lib::TestBuilder; + +use Test::More tests => 1; +use Test::Exception; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'transit tests' => sub { + plan tests => 7; + + $schema->storage->txn_begin; + + my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $item = $builder->build_sample_item( + { + homebranch => $library1->branchcode, + holdingbranch => $library2->branchcode, + datelastseen => undef + } + ); + + my $transfer = $builder->build_object( + { + class => 'Koha::Item::Transfers', + value => { + itemnumber => $item->itemnumber, + frombranch => $library2->branchcode, + tobranch => $library1->branchcode, + reason => 'Manual' + } + } + ); + is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' ); + + # Item checked out should result in failure + my $checkout = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { + itemnumber => $item->itemnumber + } + } + ); + is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' ); + + throws_ok { $transfer->transit() } + 'Koha::Exceptions::Item::Transfer::Out', + 'Exception thrown if item is checked out'; + + $checkout->delete; + + # CartToShelf test + $item->set({ location => 'CART', permanent_location => 'TEST' })->store(); + is ( $item->location, 'CART', 'Item location set to CART'); + $transfer->discard_changes; + $transfer->transit(); + $item->discard_changes; + is ( $item->location, 'TEST', 'Item location correctly restored to match permanent location'); + + # Transit state set + ok( $transfer->datesent, 'Transit set the datesent for the transfer' ); + + # Last seen + ok ( $item->datelastseen, 'Transit set item datelastseen date'); + + $schema->storage->txn_rollback; +}; -- 2.20.1