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 $policy->opening_slots
54 Return the list of opening slots (Koha::CurbsidePickupOpeningSlots object)
60 my $rs = $self->_result->curbside_pickup_opening_slots;
62 return Koha::CurbsidePickupOpeningSlots->_new_from_dbic( $rs );
65 =head3 add_opening_slot
67 $policy->add("$d-12:00-15:00");
69 Add a new opening slot for this library. It must be formatted "day:start:end" with 'start' and 'end' in 24-hour format.
73 sub add_opening_slot {
74 my ( $self, $slot ) = @_;
76 my ( $day, $start, $end ) = split '-', $slot;
77 my ( $start_hour, $start_minute ) = split ':', $start;
78 my ( $end_hour, $end_minute ) = split ':', $end;
80 return Koha::CurbsidePickupOpeningSlot->new(
82 curbside_pickup_policy_id => $self->id,
84 start_hour => $start_hour,
85 start_minute => $start_minute,
86 end_hour => $end_hour,
87 end_minute => $end_minute,
92 =head3 is_valid_pickup_datetime
96 sub is_valid_pickup_datetime {
97 my ( $self, $datetime ) = @_;
100 $self->opening_slots->search( { day => $datetime->dow % 7 } );
102 while ( my $opening_slot = $opening_slots->next ) {
103 my $start = $datetime->clone->set_hour( $opening_slot->start_hour )
104 ->set_minute( $opening_slot->start_minute );
105 my $end = $datetime->clone->set_hour( $opening_slot->end_hour )
106 ->set_minute( $opening_slot->start_minute );
108 my $slot_start = $start->clone;
109 my $slot_end = $slot_start->clone->add(minutes => $self->pickup_interval);
110 while ($slot_end <= $end) {
111 if ( $slot_start == $datetime ) {
112 $matching_slot = $slot_start;
115 $slot_start->add( minutes => $self->pickup_interval);
116 $slot_end->add( minutes => $self->pickup_interval);
120 return Koha::Result::Boolean->new(0)
121 ->add_message( { message => 'no_matching_slots' } )
122 unless $matching_slot;
124 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
125 # Check too many users for this slot
126 my $existing_pickups = Koha::CurbsidePickups->search(
128 branchcode => $self->branchcode,
129 scheduled_pickup_datetime => $dtf->format_datetime($matching_slot),
133 return Koha::Result::Boolean->new(0)
134 ->add_message( { message => 'no_more_available' } )
135 if $existing_pickups->count >= $self->patrons_per_interval;
140 =head2 Internal methods
147 return 'CurbsidePickupPolicy';