From 80cbab96acbd2057585af19831658c8ec22b72ae Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 4 Dec 2023 14:04:44 +0000 Subject: [PATCH] Bug 27595: Add place_hold method to Koha::Suggestion This patch adds anew method to allow placing a hold from a purchase suggestion To test: prove -v t/db_dependent/Suggestions.t Signed-off-by: Kelly --- Koha/Suggestion.pm | 25 +++++++++++++++++++ t/db_dependent/Suggestions.t | 48 +++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/Koha/Suggestion.pm b/Koha/Suggestion.pm index 631c1f662a..f406b71f0a 100644 --- a/Koha/Suggestion.pm +++ b/Koha/Suggestion.pm @@ -21,6 +21,7 @@ use Modern::Perl; use C4::Context; use C4::Letters; +use C4::Reserves qw( AddReserve ); use Koha::Database; use Koha::DateUtils qw( dt_from_string ); @@ -207,6 +208,30 @@ sub fund { return Koha::Acquisition::Fund->_new_from_dbic($fund_rs); } +=head3 place_hold + +my $hold_id = $suggestion->place_hold(); + +Places a hold for the suggester if the suggestion is tied to a biblio. + +=cut + +sub place_hold { + my ($self) = @_; + + return unless C4::Context->preference('PlaceHoldsOnOrdersFromSuggestions'); + return unless $self->biblionumber; + + my $hold_id = AddReserve( + { + borrowernumber => $self->suggestedby, + biblionumber => $self->biblionumber, + branchcode => $self->branchcode, + title => $self->title, + } + ); +} + =head3 type =cut diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t index abcbd626c5..88aae138c1 100755 --- a/t/db_dependent/Suggestions.t +++ b/t/db_dependent/Suggestions.t @@ -18,7 +18,7 @@ use Modern::Perl; use DateTime::Duration; -use Test::More tests => 91; +use Test::More tests => 92; use Test::Warn; use t::lib::Mocks; @@ -29,6 +29,7 @@ use C4::Letters qw( GetQueuedMessages GetMessage ); use C4::Budgets qw( AddBudgetPeriod AddBudget GetBudget ); use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Holds; use Koha::Libraries; use Koha::Patrons; use Koha::Suggestions; @@ -594,6 +595,51 @@ subtest 'ModSuggestion should work on suggestions without a suggester' => sub { is( $suggestion->{note}, "Test note", "ModSuggestion works on suggestions without a suggester" ); }; +subtest 'place_hold tests' => sub { + plan tests => 4; + + t::lib::Mocks::mock_preference( "PlaceHoldsOnOrdersFromSuggestions", "0" ); + + my $biblio = $builder->build_sample_biblio(); + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $suggestion = $builder->build_object( + { + class => 'Koha::Suggestions', + value => { + branchcode => $patron->branchcode, + biblionumber => undef, + suggestedby => $patron->id + } + } + ); + + my $hold_id = $suggestion->place_hold(); + is( $hold_id, undef, "No suggestion placed when preference is disabled" ); + + t::lib::Mocks::mock_preference( "PlaceHoldsOnOrdersFromSuggestions", "1" ); + + $hold_id = $suggestion->place_hold(); + is( + $hold_id, undef, + "No suggestion placed when preference is enabled and suggestion does not have a biblionumber" + ); + + $suggestion->biblionumber( $biblio->id )->store(); + $suggestion->discard_changes(); + + $hold_id = $suggestion->place_hold(); + ok( $hold_id, "Suggestion placed when preference is enabled and suggestion does have a biblionumber" ); + + my $hold = Koha::Holds->find($hold_id); + $hold->delete(); + + t::lib::Mocks::mock_preference( "PlaceHoldsOnOrdersFromSuggestions", "0" ); + + $hold_id = $suggestion->place_hold(); + is( $hold_id, undef, "Suggestion not placed when preference is disabled and suggestion does have a biblionumber" ); + +}; + subtest 'Suggestion with ISBN' => sub { my $suggestion_with_isbn = { isbn => '1940997232', -- 2.30.2