1 package Koha::CurbsidePickupPolicy;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Koha::CurbsidePickupOpeningSlots;
24 use Koha::Result::Boolean;
25 use Koha::Exceptions::CurbsidePickup;
27 use base qw(Koha::Object);
31 Koha::CurbsidePickupPolicy - Koha Curbside Pickup Policy Object class
39 Return the branch associated with this policy
45 my $rs = $self->_result->branchcode;
47 return Koha::Library->_new_from_dbic( $rs );
52 my $rs = $self->_result->curbside_pickup_opening_slots;
54 return Koha::CurbsidePickupOpeningSlots->_new_from_dbic( $rs );
57 sub add_opening_slot {
58 my ( $self, $slot ) = @_;
60 my ( $day, $start, $end ) = split '-', $slot;
61 my ( $start_hour, $start_minute ) = split ':', $start;
62 my ( $end_hour, $end_minute ) = split ':', $end;
64 return Koha::CurbsidePickupOpeningSlot->new(
66 curbside_pickup_policy_id => $self->id,
68 start_hour => $start_hour,
69 start_minute => $start_minute,
70 end_hour => $end_hour,
71 end_minute => $end_minute,
76 =head3 is_valid_pickup_datetime
80 sub is_valid_pickup_datetime {
81 my ( $self, $datetime ) = @_;
84 $self->opening_slots->search( { day => $datetime->dow % 7 } );
86 while ( my $opening_slot = $opening_slots->next ) {
87 my $start = $datetime->clone->set_hour( $opening_slot->start_hour )
88 ->set_minute( $opening_slot->start_minute );
89 my $end = $datetime->clone->set_hour( $opening_slot->end_hour )
90 ->set_minute( $opening_slot->start_minute );
92 my $slot_start = $start->clone;
93 my $slot_end = $slot_start->clone->add(minutes => $self->pickup_interval);
94 while ($slot_end <= $end) {
95 if ( $slot_start == $datetime ) {
96 $matching_slot = $slot_start;
99 $slot_start->add( minutes => $self->pickup_interval);
100 $slot_end->add( minutes => $self->pickup_interval);
104 return Koha::Result::Boolean->new(0)
105 ->add_message( { message => 'no_matching_slots' } )
106 unless $matching_slot;
108 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
109 # Check too many users for this slot
110 my $existing_pickups = Koha::CurbsidePickups->search(
112 branchcode => $self->branchcode,
113 scheduled_pickup_datetime => $dtf->format_datetime($matching_slot),
117 return Koha::Result::Boolean->new(0)
118 ->add_message( { message => 'no_more_available' } )
119 if $existing_pickups->count >= $self->patrons_per_interval;
124 =head2 Internal methods
131 return 'CurbsidePickupPolicy';