From 363f6a0a68639daf0e548b1ace3dda1962a8a5be 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 --- 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 703a11be4f3..8a12531b643 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::CubsidePickup::NotEnabled->throw unless $policy && $policy->enabled; + my $calendar = Koha::Calendar->new( branchcode => $params->{branchcode} ); + Koha::Exceptions::CubsidePickup::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 a1e10d0c3fb..c6fc93bb901 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::CubsidePickup::LibraryIsClosed' => { + isa => 'Koha::Exceptions::CurbsidePickup', + description => 'Cannot create a pickup on a closed day', + }, 'Koha::Exceptions::CubsidePickup::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 a849f8f938d..593fd2e254a 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::CubsidePickup::TooManyPickups') ) { + if ( $_->isa('Koha::Exceptions::CubsidePickup::NotEnabled') ) { + push @messages, { + type => 'error', + code => 'not_enabled', + }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::LibraryIsClosed') ) { + push @messages, { + type => 'error', + code => 'library_is_closed', + patron => Koha::Patrons->find($borrowernumber) + }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::NoWaitingHolds') ) { + push @messages, { + type => 'error', + code => 'no_waiting_holds', + }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::TooManyPickups') ) { push @messages, { type => 'error', code => 'too_many_pickups', patron => Koha::Patrons->find($borrowernumber) }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::NoMatchingSlots') ) { + push @messages, { + type => 'error', + code => 'no_matching_slots', + }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::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 71549834281..58f0767c33a 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 1462cfd7266..9dae1b16b23 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 7e32a027294..abd430523b5 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::CubsidePickup::TooManyPickups') ) { + if ( $_->isa('Koha::Exceptions::CubsidePickup::NotEnabled') ) { + push @messages, { + type => 'error', + code => 'not_enabled', + }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::LibraryIsClosed') ) { + push @messages, { + type => 'error', + code => 'library_is_closed', + patron => Koha::Patrons->find($borrowernumber) + }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::NoWaitingHolds') ) { + push @messages, { + type => 'error', + code => 'no_waiting_holds', + }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::TooManyPickups') ) { push @messages, { type => 'error', code => 'too_many_pickups', patron => Koha::Patrons->find($borrowernumber) }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::NoMatchingSlots') ) { + push @messages, { + type => 'error', + code => 'no_matching_slots', + }; + } elsif ( $_->isa('Koha::Exceptions::CubsidePickup::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 8d6e9421289..1caea8ac29a 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::CubsidePickup::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::CubsidePickup::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.25.1