From c4f6508be3082b00fe1b78206238b695391114a2 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 25 Jul 2025 13:36:33 +0000 Subject: [PATCH] Bug 40331: Return existing transfer if requesting a new transfer that is identical Currently request_transfer checks for the existence of a current transfer on the item, and a new one can either be enqueued or replace this transfer. This patch adds a check to see if the requested transfer matches an existing transfer on the item. In the case that it does we simply return that transfer rather than create a new transfer. This has the effect of eliminating 'WrongTransfer' generation if an item is checked in twice at the same branch, which feels correct. To test: 1 - place a hold on an item that is currently at branch A, for pickup at branch B 2 - check your item in at Branch A, click to confirm the hold and transfer (print slip if you want to) 3 - sudo koha-mysql kohadev: SELECT * FROM branchtransfers -- See one transfer 4 - check your item in at Branch A again, get a message that the item is supposed to go to Branch B. Don't click on the modal yet 5 - repeat 3 -- See two transfers, one cancelled as WrongTransfer, one pending 6 - Click 'Confirm and transfer' on the modal 7 - repeat 3 -- See three tranfers - the two from before and a new active transfer 8 - DELETE FROM branchtransfers 9 - Apply patch, restart all 10 - repeat 3 - no transfers 11 - Check item in at branch A again and confirm 12 - repeat 3, one transfer 13 - Check item in, don't confirm the modal 14 - repeat 3, one transfer 15 - Confirm the modal 16 - repeat 3, one transfer 17 - Check the item in at branch C 18 - repeat 3, two transfers - one cancelled WrongTransfer, one active 19 - Confirm modal 20 - repeat 3, two transfers 21 - Check item in at branch B 22 - repeat 3, two transfers, one cancelled, one completed Signed-off-by: Christine Lee --- Koha/Item.pm | 12 ++++++++++++ t/db_dependent/Koha/Item.t | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index eb36e62b06f..19a7fc53698 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -804,6 +804,18 @@ sub request_transfer { } } + my $matching_transfers = Koha::Item::Transfers->search( + { + tobranch => $params->{to}->branchcode, + frombranch => $self->holdingbranch, + itemnumber => $self->itemnumber, + reason => $params->{reason}, + datearrived => undef, + datecancelled => undef + } + ); + return $matching_transfers->next if $matching_transfers->count; + Koha::Exceptions::Item::Transfer::Limit->throw() unless ( $params->{ignore_limits} || $self->can_be_transferred( { to => $params->{to} } ) ); diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index c505066d88c..aeb6f8fe68c 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -1119,7 +1119,7 @@ subtest 'pickup_locations() tests' => sub { subtest 'request_transfer() tests' => sub { - plan tests => 18; + plan tests => 20; $schema->storage->txn_begin; @@ -1174,6 +1174,17 @@ subtest 'request_transfer() tests' => sub { my $original_transfer = $transfer->get_from_storage; + # Duplicated request + $transfer = $item->request_transfer( { to => $library1, reason => 'Manual' } ); + is( + ref($transfer), 'Koha::Item::Transfer', + 'Koha::Item->request_transfer should return a Koha::Item::Transfer object' + ); + is( + $transfer->branchtransfer_id, $original_transfer->branchtransfer_id, + "When a matching transfer is requested we update the existing one" + ); + # Transfer already in progress throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) } 'Koha::Exceptions::Item::Transfer::InQueue', -- 2.39.5