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 );
29 use Koha::DateUtils qw( dt_from_string );
32 use Koha::CurbsidePickupIssues;
33 use Koha::Exceptions::CurbsidePickup;
37 Koha::CurbsidePickup - Koha Curbside Pickup Object class
50 my ( $self, $params ) = @_;
52 my $existing_curbside_pickups = Koha::CurbsidePickups->search(
54 branchcode => $params->{branchcode},
55 borrowernumber => $params->{borrowernumber},
56 delivered_datetime => undef,
57 scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
60 Koha::Exceptions::CurbsidePickup::TooManyPickups->throw(
61 branchcode => $params->{branchcode},
62 borrowernumber => $params->{borrowernumber}
63 ) if $existing_curbside_pickups->count;
66 Koha::CurbsidePickupPolicies->find( { branchcode => $params->{branchcode} } );
68 $policy->is_valid_pickup_datetime( $params->{scheduled_pickup_datetime} );
70 my $error = @{ $is_valid->messages }[0]->message;
71 Koha::Exceptions::CurbsidePickup::NoMatchingSlots->throw
72 if $error eq 'no_matching_slots';
73 Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable->throw
74 if $error eq 'no_more_available';
75 Koha::Exceptions->throw(
76 "Error message must raise the appropriate exception");
79 return $self->SUPER::new($params);
82 =head3 notify_new_pickup
84 $pickup->notify_new_pickup
86 Will notify the patron that the pickup has been created.
87 Letter 'NEW_CURBSIDE_PICKUP will be used', and depending on 'Hold_Filled' configuration.
91 sub notify_new_pickup {
94 my $patron = $self->patron;
96 my $library = $self->library;
98 $patron->queue_notice({ letter_params => {
100 letter_code => 'NEW_CURBSIDE_PICKUP',
101 borrowernumber => $patron->borrowernumber,
102 branchcode => $self->branchcode,
104 'branches' => $library->unblessed,
105 'borrowers' => $patron->unblessed,
108 curbside_pickup => $self,
110 }, message_name => 'Hold_Filled' });
115 Return the checkouts linked to this pickup
122 my @pi = Koha::CurbsidePickupIssues->search({ curbside_pickup_id => $self->id })->as_list;
124 my @checkouts = map { $_->checkout } @pi;
125 @checkouts = grep { defined $_ } @checkouts;
132 Return the patron linked to this pickup
138 my $rs = $self->_result->borrowernumber;
140 return Koha::Patron->_new_from_dbic( $rs );
143 =head3 staged_by_staff
145 Return the staff member that staged this pickup
149 sub staged_by_staff {
151 my $rs = $self->_result->staged_by;
153 return Koha::Patron->_new_from_dbic( $rs );
158 Return the branch associated with this pickup
164 my $rs = $self->_result->branchcode;
166 return Koha::Library->_new_from_dbic( $rs );
169 =head3 mark_as_staged
171 Mark the pickup as staged
177 my $staged_by = C4::Context->userenv ? C4::Context->userenv->{number} : undef;
180 staged_datetime => dt_from_string(),
181 staged_by => $staged_by,
182 arrival_datetime => undef,
187 =head3 mark_as_unstaged
189 Mark the pickup as unstaged
193 sub mark_as_unstaged {
198 staged_datetime => undef,
200 arrival_datetime => undef,
205 =head3 mark_patron_has_arrived
207 Set the arrival time of the patron
211 sub mark_patron_has_arrived {
215 arrival_datetime => dt_from_string(),
220 =head3 mark_as_delivered
222 Mark the pickup as delivered. The waiting holds will be filled.
226 sub mark_as_delivered {
228 my $patron = $self->patron;
229 my $holds = $patron->holds;
230 my $branchcode = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
231 foreach my $hold ( $holds->as_list ) {
232 if ( $hold->found eq 'W' && $branchcode && $hold->branchcode eq $branchcode ) {
233 my ( $issuingimpossible, $needsconfirmation ) =
234 C4::Circulation::CanBookBeIssued( $patron, $hold->item->barcode );
236 unless ( keys %$issuingimpossible ) {
238 C4::Circulation::AddIssue( $patron->unblessed, $hold->item->barcode );
240 Koha::CurbsidePickupIssue->new(
242 curbside_pickup_id => $self->id,
243 issue_id => $issue->id,
244 reserve_id => $hold->id,
249 Koha::Exceptions->throw(sprintf("Cannot checkout hold %s for patron %s: %s", $patron->id, $hold->id, join(", ", keys %$issuingimpossible)));
255 my $delivered_by = C4::Context->userenv ? C4::Context->userenv->{number} : undef;
256 $self->arrival_datetime(dt_from_string) unless $self->arrival_datetime;
259 delivered_datetime => dt_from_string(),
260 delivered_by => $delivered_by,
267 Return the status of the pickup, can be 'to-be-staged', 'staged-and-ready', 'patron-is-outside' or 'delivered'.
274 !defined $self->staged_datetime ? 'to-be-staged'
275 : !defined $self->arrival_datetime ? 'staged-and-ready'
276 : !defined $self->delivered_datetime ? 'patron-is-outside'
280 =head2 Internal methods
287 return 'CurbsidePickup';