From b4884e9929dee9bd5a0436289c1e80edb84ff930 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Tue, 7 Dec 2021 04:08:18 +0000 Subject: [PATCH] Bug 24194: (follow-up) Throw an error if expiration date used Signed-off-by: Nicolas Giraud --- C4/ILSDI/Services.pm | 8 ++++++-- C4/Reserves.pm | 4 ++++ Koha/Club/Hold.pm | 6 +++++- Koha/Exceptions/Hold.pm | 4 ++++ Koha/REST/V1/Clubs/Holds.pm | 7 +++++++ Koha/REST/V1/Holds.pm | 9 ++++++++- 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/C4/ILSDI/Services.pm b/C4/ILSDI/Services.pm index c3bc7fa457f..0c1ca02a1af 100644 --- a/C4/ILSDI/Services.pm +++ b/C4/ILSDI/Services.pm @@ -815,8 +815,12 @@ sub HoldTitle { my $resdate = $cgi->param('start_date'); my $expdate; - if ( C4::Context->preference('ReserveExpiration') and $cgi->param('expiry_date') ) { - $expdate = $cgi->param('expiry_date'); + if ( C4::Context->preference('ReserveExpiration') ) { + if ( $cgi->param('expiry_date') ) { + $expdate = $cgi->param('expiry_date'); + } + } else { + return { code => 'expirationDateDisabled' }; } # Add the reserve diff --git a/C4/Reserves.pm b/C4/Reserves.pm index 9ce0bd402f3..51378bce873 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -194,6 +194,10 @@ sub AddReserve { my $item_group_id = $params->{item_group_id}; my $hold_group_id = $params->{hold_group_id}; + if ( !C4::Context->preference('ReserveExpiration') and $params->{expiration_date} ) { + Koha::Exceptions::Hold::ExpirationDateDisabled->throw; + } + $resdate ||= dt_from_string; # if we have an item selectionned, and the pickup branch is the same as the holdingbranch diff --git a/Koha/Club/Hold.pm b/Koha/Club/Hold.pm index 6a2b23269f6..13ff372f95f 100644 --- a/Koha/Club/Hold.pm +++ b/Koha/Club/Hold.pm @@ -70,6 +70,10 @@ sub add { unless scalar @enrollments; my $biblio = Koha::Biblios->find( $params->{biblio_id} ); + + if ( !C4::Context->preference('ReserveExpiration') and $params->{expiration_date} ) { + Koha::Exceptions::Hold::ExpirationDateDisabled->throw; + } my $club_params = { club_id => $params->{club_id}, @@ -131,7 +135,7 @@ sub add { borrowernumber => $patron_id, biblionumber => $params->{biblio_id}, priority => $priority, - expiration_date => C4::Context->preference('ReserveExpiration') ? $params->{expiration_date} : undef, + expiration_date => $params->{expiration_date}, notes => $params->{notes}, title => $biblio->title, itemnumber => $params->{item_id}, diff --git a/Koha/Exceptions/Hold.pm b/Koha/Exceptions/Hold.pm index 0b2ba2a1357..5a1e620f4e3 100644 --- a/Koha/Exceptions/Hold.pm +++ b/Koha/Exceptions/Hold.pm @@ -39,6 +39,10 @@ use Exception::Class ( 'Koha::Exceptions::Hold::MissingPickupLocation' => { isa => 'Koha::Exceptions::Hold', description => 'You must supply a pickup location when placing a hold' + }, + 'Koha::Exceptions::Hold::ExpirationDateDisabled' => { + isa => 'Koha::Exceptions::Hold', + description => 'Expiration date not allowed' } ); diff --git a/Koha/REST/V1/Clubs/Holds.pm b/Koha/REST/V1/Clubs/Holds.pm index 3281df0f3bf..ba3f274e6f6 100644 --- a/Koha/REST/V1/Clubs/Holds.pm +++ b/Koha/REST/V1/Clubs/Holds.pm @@ -88,6 +88,13 @@ sub add { return $c->render_resource_not_found("Bibliographic record") unless $biblio; + if ( !C4::Context->preference('ReserveExpiration') and $expiration_date ) { + return $c->render( + status => 400, + openapi => { error => "Expiration date not allowed" } + ); + } + my $club_hold = Koha::Club::Hold::add( { club_id => $club_id, diff --git a/Koha/REST/V1/Holds.pm b/Koha/REST/V1/Holds.pm index 7694a30cfc7..ffaa2e7210f 100644 --- a/Koha/REST/V1/Holds.pm +++ b/Koha/REST/V1/Holds.pm @@ -82,7 +82,7 @@ sub add { my $item_id = $body->{item_id}; my $patron_id = $body->{patron_id}; my $item_type = $body->{item_type}; - my $expiration_date = C4::Context->preference('ReserveExpiration') ? $body->{expiration_date} : undef; + my $expiration_date = $body->{expiration_date}; my $notes = $body->{notes}; my $hold_date = $body->{hold_date}; my $non_priority = $body->{non_priority}; @@ -99,6 +99,13 @@ sub add { ); } + if ( !C4::Context->preference('ReserveExpiration') and $expiration_date ) { + return $c->render( + status => 400, + openapi => { error => "Expiration date not allowed" } + ); + } + if ( $item_id and $biblio_id ) { $biblio = Koha::Biblios->find($biblio_id); -- 2.39.5