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 <kelly@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Nick Clemens 2023-12-04 14:04:44 +00:00 committed by Katrin Fischer
parent 28e5e49790
commit e4caa84e86
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834
2 changed files with 72 additions and 1 deletions

View file

@ -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

View file

@ -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',