Bug 20473: Whitespace
[koha.git] / Koha / CurbsidePickupPolicy.pm
1 package Koha::CurbsidePickupPolicy;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Koha::Database;
21 use Koha::Library;
22 use Koha::CurbsidePickupOpeningSlots;
23
24 use Koha::Result::Boolean;
25 use Koha::Exceptions::CurbsidePickup;
26
27 use base qw(Koha::Object);
28
29 =head1 NAME
30
31 Koha::CurbsidePickupPolicy - Koha Curbside Pickup Policy Object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 library
38
39 Return the branch associated with this policy
40
41 =cut
42
43 sub library {
44     my ( $self ) = @_;
45     my $rs = $self->_result->branchcode;
46     return unless $rs;
47     return Koha::Library->_new_from_dbic( $rs );
48 }
49
50 =head3 opening_slots
51
52 $policy->opening_slots
53
54 Return the list of opening slots (Koha::CurbsidePickupOpeningSlots object)
55
56 =cut
57
58 sub opening_slots {
59     my ( $self ) = @_;
60     my $rs = $self->_result->curbside_pickup_opening_slots;
61     return unless $rs;
62     return Koha::CurbsidePickupOpeningSlots->_new_from_dbic( $rs );
63 }
64
65 =head3 add_opening_slot
66
67 $policy->add("$d-12:00-15:00");
68
69 Add a new opening slot for this library. It must be formatted "day:start:end" with 'start' and 'end' in 24-hour format.
70
71 =cut
72
73 sub add_opening_slot {
74     my ( $self, $slot ) = @_;
75
76     my ( $day, $start, $end ) = split '-', $slot;
77     my ( $start_hour, $start_minute ) = split ':', $start;
78     my ( $end_hour,   $end_minute )   = split ':', $end;
79
80     return Koha::CurbsidePickupOpeningSlot->new(
81         {
82             curbside_pickup_policy_id => $self->id,
83             day                       => $day,
84             start_hour                => $start_hour,
85             start_minute              => $start_minute,
86             end_hour                  => $end_hour,
87             end_minute                => $end_minute,
88         }
89     )->store;
90 }
91
92 =head3 is_valid_pickup_datetime
93
94 =cut
95
96 sub is_valid_pickup_datetime {
97     my ( $self, $datetime ) = @_;
98
99     my $opening_slots =
100       $self->opening_slots->search( { day => $datetime->dow % 7 } );
101     my $matching_slot;
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 );
107         my $keep_going = 1;
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;
113                 last;
114             }
115             $slot_start->add( minutes => $self->pickup_interval);
116             $slot_end->add( minutes => $self->pickup_interval);
117         }
118     }
119
120     return Koha::Result::Boolean->new(0)
121       ->add_message( { message => 'no_matching_slots' } )
122       unless $matching_slot;
123
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(
127         {
128             branchcode                => $self->branchcode,
129             scheduled_pickup_datetime => $dtf->format_datetime($matching_slot),
130         }
131     );
132
133     return Koha::Result::Boolean->new(0)
134       ->add_message( { message => 'no_more_available' } )
135       if $existing_pickups->count >= $self->patrons_per_interval;
136
137     return 1;
138 }
139
140 =head2 Internal methods
141
142 =head3 _type
143
144 =cut
145
146 sub _type {
147     return 'CurbsidePickupPolicy';
148 }
149
150 1;