Bug 20473: Whitespace
[koha.git] / Koha / CurbsidePickup.pm
1 package Koha::CurbsidePickup;
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 Carp;
21
22 use Koha::Database;
23
24 use base qw(Koha::Object);
25
26 use C4::Circulation qw( CanBookBeIssued AddIssue );
27 use C4::Members::Messaging qw( GetMessagingPreferences );
28 use C4::Letters qw( GetPreparedLetter EnqueueLetter );
29 use Koha::Calendar;
30 use Koha::DateUtils qw( dt_from_string );
31 use Koha::Patron;
32 use Koha::Library;
33 use Koha::CurbsidePickupIssues;
34 use Koha::Exceptions::CurbsidePickup;
35
36 =head1 NAME
37
38 Koha::CurbsidePickup - Koha Curbside Pickup Object class
39
40 =head1 API
41
42 =head2 Class methods
43
44 =cut
45
46 =head3 new
47
48 =cut
49
50 sub new {
51     my ( $self, $params ) = @_;
52
53     my $policy =
54       Koha::CurbsidePickupPolicies->find( { branchcode => $params->{branchcode} } );
55
56     Koha::Exceptions::CurbsidePickup::NotEnabled->throw
57       unless $policy && $policy->enabled;
58
59     my $calendar = Koha::Calendar->new( branchcode => $params->{branchcode} );
60     Koha::Exceptions::CurbsidePickup::LibraryIsClosed->throw
61       if $calendar->is_holiday( $params->{scheduled_pickup_datetime} );
62
63     if ( $policy->enable_waiting_holds_only ) {
64         my $patron        = Koha::Patrons->find( $params->{borrowernumber} );
65         my $waiting_holds = $patron->holds->waiting->search( { branchcode => $params->{branchcode} } );
66
67         Koha::Exceptions::CurbsidePickup::NoWaitingHolds->throw
68           unless $waiting_holds->count;
69     }
70     my $existing_curbside_pickups = Koha::CurbsidePickups->search(
71         {
72             branchcode                => $params->{branchcode},
73             borrowernumber            => $params->{borrowernumber},
74             delivered_datetime        => undef,
75         }
76     )->filter_by_scheduled_today;
77     Koha::Exceptions::CurbsidePickup::TooManyPickups->throw(
78         branchcode     => $params->{branchcode},
79         borrowernumber => $params->{borrowernumber}
80     ) if $existing_curbside_pickups->count;
81
82     my $is_valid =
83       $policy->is_valid_pickup_datetime( $params->{scheduled_pickup_datetime} );
84     unless ($is_valid) {
85         my $error = @{ $is_valid->messages }[0]->message;
86         Koha::Exceptions::CurbsidePickup::NoMatchingSlots->throw
87           if $error eq 'no_matching_slots';
88         Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable->throw
89           if $error eq 'no_more_available';
90         Koha::Exceptions->throw(
91             "Error message must raise the appropriate exception");
92     }
93
94     return $self->SUPER::new($params);
95 }
96
97 =head3 notify_new_pickup
98
99 $pickup->notify_new_pickup
100
101 Will notify the patron that the pickup has been created.
102 Letter 'NEW_CURBSIDE_PICKUP will be used', and depending on 'Hold_Filled' configuration.
103
104 =cut
105
106 sub notify_new_pickup {
107     my ( $self ) = @_;
108
109     my $patron = $self->patron;
110
111     my $library = $self->library;
112
113     $patron->queue_notice({ letter_params => {
114         module     => 'reserves',
115         letter_code => 'NEW_CURBSIDE_PICKUP',
116         borrowernumber => $patron->borrowernumber,
117         branchcode => $self->branchcode,
118         tables     => {
119             'branches'  => $library->unblessed,
120             'borrowers' => $patron->unblessed,
121         },
122         substitute => {
123             curbside_pickup => $self,
124         }
125     }, message_name => 'Hold_Filled' });
126 }
127
128 =head3 checkouts
129
130 Return the checkouts linked to this pickup
131
132 =cut
133
134 sub checkouts {
135     my ( $self ) = @_;
136
137     my @pi = Koha::CurbsidePickupIssues->search({ curbside_pickup_id => $self->id })->as_list;
138
139     my @checkouts = map { $_->checkout } @pi;
140     @checkouts = grep { defined $_ } @checkouts;
141
142     return @checkouts;
143 }
144
145 =head3 patron
146
147 Return the patron linked to this pickup
148
149 =cut
150
151 sub patron {
152     my ( $self ) = @_;
153     my $rs = $self->_result->borrowernumber;
154     return unless $rs;
155     return Koha::Patron->_new_from_dbic( $rs );
156 }
157
158 =head3 staged_by_staff
159
160 Return the staff member that staged this pickup
161
162 =cut
163
164 sub staged_by_staff {
165     my ( $self ) = @_;
166     my $rs = $self->_result->staged_by;
167     return unless $rs;
168     return Koha::Patron->_new_from_dbic( $rs );
169 }
170
171 =head3 library
172
173 Return the branch associated with this pickup
174
175 =cut
176
177 sub library {
178     my ( $self ) = @_;
179     my $rs = $self->_result->branchcode;
180     return unless $rs;
181     return Koha::Library->_new_from_dbic( $rs );
182 }
183
184 =head3 mark_as_staged
185
186 Mark the pickup as staged
187
188 =cut
189
190 sub mark_as_staged {
191     my ( $self ) = @_;
192     my $staged_by = C4::Context->userenv ? C4::Context->userenv->{number} : undef;
193     $self->set(
194         {
195             staged_datetime  => dt_from_string(),
196             staged_by        => $staged_by,
197             arrival_datetime => undef,
198         }
199     )->store;
200 }
201
202 =head3 mark_as_unstaged
203
204 Mark the pickup as unstaged
205
206 =cut
207
208 sub mark_as_unstaged {
209     my ( $self ) = @_;
210
211     $self->set(
212         {
213             staged_datetime  => undef,
214             staged_by        => undef,
215             arrival_datetime => undef,
216         }
217     )->store;
218 }
219
220 =head3 mark_patron_has_arrived
221
222 Set the arrival time of the patron
223
224 =cut
225
226 sub mark_patron_has_arrived {
227     my ( $self ) = @_;
228     $self->set(
229         {
230             arrival_datetime => dt_from_string(),
231         }
232     )->store;
233 }
234
235 =head3 mark_as_delivered
236
237 Mark the pickup as delivered. The waiting holds will be filled.
238
239 =cut
240
241 sub mark_as_delivered {
242     my ( $self ) = @_;
243     my $patron          = $self->patron;
244     my $holds           = $patron->holds;
245     my $branchcode = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
246     foreach my $hold ( $holds->as_list ) {
247         if ( $hold->is_waiting && $branchcode && $hold->branchcode eq $branchcode ) {
248             my ( $issuingimpossible, $needsconfirmation ) =
249               C4::Circulation::CanBookBeIssued( $patron, $hold->item->barcode );
250
251             unless ( keys %$issuingimpossible ) {
252                 my $issue =
253                   C4::Circulation::AddIssue( $patron->unblessed, $hold->item->barcode );
254                 if ($issue) {
255                     Koha::CurbsidePickupIssue->new(
256                         {
257                             curbside_pickup_id => $self->id,
258                             issue_id           => $issue->id,
259                             reserve_id         => $hold->id,
260                         }
261                     )->store();
262                 }
263                 else {
264                     Koha::Exceptions->throw(sprintf("Cannot checkout hold %s for patron %s: %s", $patron->id, $hold->id, join(", ", keys %$issuingimpossible)));
265                 }
266             }
267         }
268     }
269
270     my $delivered_by = C4::Context->userenv ? C4::Context->userenv->{number} : undef;
271     $self->arrival_datetime(dt_from_string) unless $self->arrival_datetime;
272     $self->set(
273         {
274             delivered_datetime => dt_from_string(),
275             delivered_by       => $delivered_by,
276         }
277     )->store;
278 }
279
280 =head3 status
281
282 Return the status of the pickup, can be 'to-be-staged', 'staged-and-ready', 'patron-is-outside' or 'delivered'.
283
284 =cut
285
286 sub status {
287     my ($self) = @_;
288     return
289         !defined $self->staged_datetime    ? 'to-be-staged'
290       : !defined $self->arrival_datetime   ? 'staged-and-ready'
291       : !defined $self->delivered_datetime ? 'patron-is-outside'
292       :                                      'delivered';
293 }
294
295 =head2 Internal methods
296
297 =head3 _type
298
299 =cut
300
301 sub _type {
302     return 'CurbsidePickup';
303 }
304
305 1;