1 package Koha::CurbsidePickup;
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>.
24 use base qw(Koha::Object);
26 use C4::Circulation qw( CanBookBeIssued AddIssue );
27 use C4::Members::Messaging qw( GetMessagingPreferences );
28 use C4::Letters qw( GetPreparedLetter EnqueueLetter );
30 use Koha::DateUtils qw( dt_from_string );
33 use Koha::CurbsidePickupIssues;
34 use Koha::Exceptions::CurbsidePickup;
38 Koha::CurbsidePickup - Koha Curbside Pickup Object class
51 my ( $self, $params ) = @_;
54 Koha::CurbsidePickupPolicies->find( { branchcode => $params->{branchcode} } );
56 Koha::Exceptions::CurbsidePickup::NotEnabled->throw
57 unless $policy && $policy->enabled;
59 my $calendar = Koha::Calendar->new( branchcode => $params->{branchcode} );
60 Koha::Exceptions::CurbsidePickup::LibraryIsClosed->throw
61 if $calendar->is_holiday( $params->{scheduled_pickup_datetime} );
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} } );
67 Koha::Exceptions::CurbsidePickup::NoWaitingHolds->throw
68 unless $waiting_holds->count;
70 my $existing_curbside_pickups = Koha::CurbsidePickups->search(
72 branchcode => $params->{branchcode},
73 borrowernumber => $params->{borrowernumber},
74 delivered_datetime => undef,
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;
83 $policy->is_valid_pickup_datetime( $params->{scheduled_pickup_datetime} );
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");
94 return $self->SUPER::new($params);
97 =head3 notify_new_pickup
99 $pickup->notify_new_pickup
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.
106 sub notify_new_pickup {
109 my $patron = $self->patron;
111 my $library = $self->library;
113 $patron->queue_notice({ letter_params => {
114 module => 'reserves',
115 letter_code => 'NEW_CURBSIDE_PICKUP',
116 borrowernumber => $patron->borrowernumber,
117 branchcode => $self->branchcode,
119 'branches' => $library->unblessed,
120 'borrowers' => $patron->unblessed,
123 curbside_pickup => $self,
125 }, message_name => 'Hold_Filled' });
130 Return the checkouts linked to this pickup
137 my @pi = Koha::CurbsidePickupIssues->search({ curbside_pickup_id => $self->id })->as_list;
139 my @checkouts = map { $_->checkout } @pi;
140 @checkouts = grep { defined $_ } @checkouts;
147 Return the patron linked to this pickup
153 my $rs = $self->_result->borrowernumber;
155 return Koha::Patron->_new_from_dbic( $rs );
158 =head3 staged_by_staff
160 Return the staff member that staged this pickup
164 sub staged_by_staff {
166 my $rs = $self->_result->staged_by;
168 return Koha::Patron->_new_from_dbic( $rs );
173 Return the branch associated with this pickup
179 my $rs = $self->_result->branchcode;
181 return Koha::Library->_new_from_dbic( $rs );
184 =head3 mark_as_staged
186 Mark the pickup as staged
192 my $staged_by = C4::Context->userenv ? C4::Context->userenv->{number} : undef;
195 staged_datetime => dt_from_string(),
196 staged_by => $staged_by,
197 arrival_datetime => undef,
202 =head3 mark_as_unstaged
204 Mark the pickup as unstaged
208 sub mark_as_unstaged {
213 staged_datetime => undef,
215 arrival_datetime => undef,
220 =head3 mark_patron_has_arrived
222 Set the arrival time of the patron
226 sub mark_patron_has_arrived {
230 arrival_datetime => dt_from_string(),
235 =head3 mark_as_delivered
237 Mark the pickup as delivered. The waiting holds will be filled.
241 sub mark_as_delivered {
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 );
251 unless ( keys %$issuingimpossible ) {
253 C4::Circulation::AddIssue( $patron->unblessed, $hold->item->barcode );
255 Koha::CurbsidePickupIssue->new(
257 curbside_pickup_id => $self->id,
258 issue_id => $issue->id,
259 reserve_id => $hold->id,
264 Koha::Exceptions->throw(sprintf("Cannot checkout hold %s for patron %s: %s", $patron->id, $hold->id, join(", ", keys %$issuingimpossible)));
270 my $delivered_by = C4::Context->userenv ? C4::Context->userenv->{number} : undef;
271 $self->arrival_datetime(dt_from_string) unless $self->arrival_datetime;
274 delivered_datetime => dt_from_string(),
275 delivered_by => $delivered_by,
282 Return the status of the pickup, can be 'to-be-staged', 'staged-and-ready', 'patron-is-outside' or 'delivered'.
289 !defined $self->staged_datetime ? 'to-be-staged'
290 : !defined $self->arrival_datetime ? 'staged-and-ready'
291 : !defined $self->delivered_datetime ? 'patron-is-outside'
295 =head2 Internal methods
302 return 'CurbsidePickup';