From d0835156e4a6341d723993ac9e0095dc3621f6f8 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 10 May 2022 14:01:09 +0200 Subject: [PATCH] Bug 30650: Prevent pickup to be created on holiday We could improve this more and add disable the holiday from the date picker widget, but it's out of the scope here. Sponsored-by: Association KohaLa - https://koha-fr.org/ Signed-off-by: Koha Team University Lyon 3 Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- Koha/CurbsidePickup.pm | 5 ++++ Koha/Exceptions/CurbsidePickup.pm | 4 +++ circ/curbside_pickups.pl | 28 +++++++++++++++++- .../prog/en/modules/circ/curbside_pickups.tt | 10 +++++++ .../en/modules/opac-curbside-pickups.tt | 18 ++++++++++-- opac/opac-curbside-pickups.pl | 29 ++++++++++++++++++- t/db_dependent/Koha/CurbsidePickups.t | 21 ++++++++++++-- 7 files changed, 109 insertions(+), 6 deletions(-) diff --git a/Koha/CurbsidePickup.pm b/Koha/CurbsidePickup.pm index e35b30e212..f9fce8baa9 100644 --- a/Koha/CurbsidePickup.pm +++ b/Koha/CurbsidePickup.pm @@ -26,6 +26,7 @@ use base qw(Koha::Object); use C4::Circulation qw( CanBookBeIssued AddIssue ); use C4::Members::Messaging qw( GetMessagingPreferences ); use C4::Letters qw( GetPreparedLetter EnqueueLetter ); +use Koha::Calendar; use Koha::DateUtils qw( dt_from_string ); use Koha::Patron; use Koha::Library; @@ -55,6 +56,10 @@ sub new { Koha::Exceptions::CurbsidePickup::NotEnabled->throw unless $policy && $policy->enabled; + my $calendar = Koha::Calendar->new( branchcode => $params->{branchcode} ); + Koha::Exceptions::CurbsidePickup::LibraryIsClosed->throw + if $calendar->is_holiday( $params->{scheduled_pickup_datetime} ); + if ( $policy->enable_waiting_holds_only ) { my $patron = Koha::Patrons->find( $params->{borrowernumber} ); my $waiting_holds = $patron->holds->search( diff --git a/Koha/Exceptions/CurbsidePickup.pm b/Koha/Exceptions/CurbsidePickup.pm index 5fa7d01cba..1dd4208d22 100644 --- a/Koha/Exceptions/CurbsidePickup.pm +++ b/Koha/Exceptions/CurbsidePickup.pm @@ -28,6 +28,10 @@ use Exception::Class ( isa => 'Koha::Exceptions::CurbsidePickup', description => 'Curbside pickups are not enable for this library', }, + 'Koha::Exceptions::CurbsidePickup::LibraryIsClosed' => { + isa => 'Koha::Exceptions::CurbsidePickup', + description => 'Cannot create a pickup on a closed day', + }, 'Koha::Exceptions::CurbsidePickup::TooManyPickups' => { isa => 'Koha::Exceptions::CurbsidePickup', description => 'Patron already has a scheduled pickup for this library', diff --git a/circ/curbside_pickups.pl b/circ/curbside_pickups.pl index fd7df5cbe1..e3223e3dc8 100755 --- a/circ/curbside_pickups.pl +++ b/circ/curbside_pickups.pl @@ -93,12 +93,38 @@ elsif ( $op eq 'create-pickup' ) { )->store; $pickup->notify_new_pickup; } catch { - if ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) { + if ( $_->isa('Koha::Exceptions::CurbsidePickup::NotEnabled') ) { + push @messages, { + type => 'error', + code => 'not_enabled', + }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::LibraryIsClosed') ) { + push @messages, { + type => 'error', + code => 'library_is_closed', + patron => Koha::Patrons->find($borrowernumber) + }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoWaitingHolds') ) { + push @messages, { + type => 'error', + code => 'no_waiting_holds', + }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) { push @messages, { type => 'error', code => 'too_many_pickups', patron => Koha::Patrons->find($borrowernumber) }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMatchingSlots') ) { + push @messages, { + type => 'error', + code => 'no_matching_slots', + }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable') ) { + push @messages, { + type => 'error', + code => 'no_more_pickups_available', + }; } else { warn $_; push @messages, { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/curbside_pickups.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/curbside_pickups.tt index 7154983428..58f0767c33 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/curbside_pickups.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/curbside_pickups.tt @@ -69,8 +69,18 @@ [% FOR m IN messages %]
[% SWITCH m.code %] + [% CASE 'not_enabled' %] + The curbside pickup feature is not enabled for this library. + [% CASE 'library_is_closed' %] + Cannot create a curbside pickup for this day, it is an holiday. + [% CASE 'no_waiting_holds' %] + This patron does not have waiting holds. [% CASE 'too_many_pickups' %] This patron already has a scheduled pickup for this library. + [% CASE 'no_matching_slots' %] + Wrong slot selected. + [% CASE 'no_more_pickups_available' %] + There are no more pickups available for this slot. Please choose another one. [% CASE 'cannot_checkout' %] Unable to check the items out to [% INCLUDE 'patron-title.inc' patron=m.patron %] [% CASE 'no_patron_found' %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-curbside-pickups.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-curbside-pickups.tt index 1462cfd726..9dae1b16b2 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-curbside-pickups.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-curbside-pickups.tt @@ -59,12 +59,26 @@

Curbside pickups

[% FOR m IN messages %] -
+ [% IF m.type == "error" %] +
+ [% ELSE %] +
+ [% END %] [% SWITCH m.code %] + [% CASE 'not_enabled' %] + The curbside pickup feature is not enabled for this library. + [% CASE 'library_is_closed' %] + Cannot create a curbside pickup for this day, it is an holiday. + [% CASE 'no_waiting_holds' %] + This patron does not have waiting holds. [% CASE 'too_many_pickups' %] You already have a scheduled pickup for this library. + [% CASE 'no_matching_slots' %] + Wrong slot selected. + [% CASE 'no_more_pickups_available' %] + There are no more pickups available for this slot. Please choose another one. [% CASE 'cannot_checkout' %] - Unable to check the items out. Please contact the library. + Unable to check the items out to [% INCLUDE 'patron-title.inc' patron=m.patron %] [% CASE 'library_notified' %] The library has been notified of your arrival. [% CASE %] diff --git a/opac/opac-curbside-pickups.pl b/opac/opac-curbside-pickups.pl index e48512b3b7..f5b4790ae7 100755 --- a/opac/opac-curbside-pickups.pl +++ b/opac/opac-curbside-pickups.pl @@ -64,12 +64,38 @@ if ( $op eq 'create-pickup' ) { )->store; $pickup->notify_new_pickup; } catch { - if ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) { + if ( $_->isa('Koha::Exceptions::CurbsidePickup::NotEnabled') ) { + push @messages, { + type => 'error', + code => 'not_enabled', + }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::LibraryIsClosed') ) { + push @messages, { + type => 'error', + code => 'library_is_closed', + patron => Koha::Patrons->find($borrowernumber) + }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoWaitingHolds') ) { + push @messages, { + type => 'error', + code => 'no_waiting_holds', + }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) { push @messages, { type => 'error', code => 'too_many_pickups', patron => Koha::Patrons->find($borrowernumber) }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMatchingSlots') ) { + push @messages, { + type => 'error', + code => 'no_matching_slots', + }; + } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable') ) { + push @messages, { + type => 'error', + code => 'no_more_pickups_available', + }; } else { warn $_; push @messages, { @@ -99,6 +125,7 @@ elsif ( $op eq 'arrival-alert' ) { } $template->param( + messages => \@messages, policies => Koha::CurbsidePickupPolicies->search( { enabled => 1, diff --git a/t/db_dependent/Koha/CurbsidePickups.t b/t/db_dependent/Koha/CurbsidePickups.t index 24d57442e5..eb9cf29758 100755 --- a/t/db_dependent/Koha/CurbsidePickups.t +++ b/t/db_dependent/Koha/CurbsidePickups.t @@ -20,9 +20,10 @@ use Modern::Perl; use Test::More tests => 4; use Test::Exception; -use Koha::City; +use C4::Calendar; use Koha::CurbsidePickups; use Koha::CurbsidePickupPolicies; +use Koha::Calendar; use Koha::Database; use Koha::DateUtils qw( dt_from_string ); @@ -77,7 +78,7 @@ $policy->add_opening_slot('1-12:00-18:00'); my $today = dt_from_string; subtest 'Create a pickup' => sub { - plan tests => 8; + plan tests => 9; # Day and datetime are ok my $next_monday = @@ -144,6 +145,22 @@ subtest 'Create a pickup' => sub { 'Koha::Exceptions::CurbsidePickup::NoMatchingSlots', 'Cannot create a pickup on a time that is not matching the start of an interval'; + # Day is a holiday + Koha::Caches->get_instance->flush_all; + C4::Calendar->new( branchcode => $library->branchcode )->insert_week_day_holiday( + weekday => 1, + title => '', + description => 'Mondays', + ); + my $calendar = Koha::Calendar->new( branchcode => $library->branchcode ); + throws_ok { + Koha::CurbsidePickup->new({%$params, scheduled_pickup_datetime => $schedule_dt})->store; + } + 'Koha::Exceptions::CurbsidePickup::LibraryIsClosed', + 'Cannot create a pickup on a holiday'; + + C4::Context->dbh->do(q{DELETE FROM repeatable_holidays}); + Koha::Caches->get_instance->flush_all; }; subtest 'workflow' => sub { -- 2.39.5