From 18578794d9e5ac3c5e5c0548989224149bccae60 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 11 Dec 2020 16:51:42 -0300 Subject: [PATCH] Bug 27209: Add Koha::Hold->set_pickup_location This patch introduces a method to safely update a hold's pickup location. It will raise exceptionis if the passed parameters are invalid or absent. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Hold.t => SUCCESS; Tests pass 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Josef Moravec Signed-off-by: Jonathan Druart (cherry picked from commit 672413ea294679d4b5b30d30839650f6d30b45ca) Signed-off-by: Fridolin Somers --- Koha/Exceptions/Hold.pm | 4 ++++ Koha/Hold.pm | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Koha/Exceptions/Hold.pm b/Koha/Exceptions/Hold.pm index 2a5b2faec6..65d7b9d9a9 100644 --- a/Koha/Exceptions/Hold.pm +++ b/Koha/Exceptions/Hold.pm @@ -27,6 +27,10 @@ use Exception::Class ( isa => 'Koha::Exceptions::Hold', description => "Found holds cannot be suspended", fields => ['status'] + }, + 'Koha::Exceptions::Hold::InvalidPickupLocation' => { + isa => 'Koha::Exceptions::Hold', + description => 'The supplied pickup location is not valid' } ); diff --git a/Koha/Hold.pm b/Koha/Hold.pm index 7754691238..d848962204 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Carp; use Data::Dumper qw(Dumper); +use List::MoreUtils qw(any); use C4::Context qw(preference); use C4::Letters; @@ -221,6 +222,41 @@ sub set_waiting { return $self; } +=head3 set_pickup_location + + $hold->set_pickup_location({ library_id => $library->id }); + +Updates the hold pickup location. It throws a I if +the passed pickup location is not valid. + +=cut + +sub set_pickup_location { + my ( $self, $params ) = @_; + + Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory') + unless $params->{library_id}; + + my @pickup_locations; + + if ( $self->itemnumber ) { # item-level + @pickup_locations = $self->item->pickup_locations({ patron => $self->patron }); + } + else { # biblio-level + @pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron }); + } + + if ( any { $_->branchcode eq $params->{library_id} } @pickup_locations ) { + # all good, set the new pickup location + $self->branchcode( $params->{library_id} )->store; + } + else { + Koha::Exceptions::Hold::InvalidPickupLocation->throw; + } + + return $self; +} + =head3 set_processing $hold->set_processing; -- 2.39.5