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 C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_html_with_http_headers );
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::CurbsidePickups;
27 use Koha::CurbsidePickupPolicies;
32 my $op = $input->param('op') || 'list';
33 my $tab = $input->param('tab'),
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 template_name => "circ/curbside_pickups.tt",
41 flagsrequired => { parameters => 'manage_curbside_pickups' },
45 my $branchcode = C4::Context->userenv()->{'branch'};
46 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
47 if ( $op eq 'find-patron' ) {
48 my $cardnumber = $input->param('cardnumber');
49 my $borrowernumber = $input->param('borrowernumber');
53 ? Koha::Patrons->find( { cardnumber => $cardnumber } )
54 : Koha::Patrons->find($borrowernumber);
56 my $existing_curbside_pickups;
59 $existing_curbside_pickups = Koha::CurbsidePickups->search(
61 branchcode => $branchcode,
62 borrowernumber => $patron->id,
63 delivered_datetime => undef,
65 )->filter_by_scheduled_today;
69 code => 'no_patron_found',
70 cardnumber => $cardnumber
74 $tab = 'schedule-pickup';
77 existing_curbside_pickups => $existing_curbside_pickups,
80 elsif ( $op eq 'create-pickup' ) {
81 my $borrowernumber = $input->param('borrowernumber');
82 my $scheduled_pickup_datetime = $input->param('pickup_time');
83 my $notes = $input->param('notes');
86 my $pickup = Koha::CurbsidePickup->new(
88 branchcode => $branchcode,
89 borrowernumber => $borrowernumber,
90 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
94 $pickup->notify_new_pickup;
96 if ( $_->isa('Koha::Exceptions::CurbsidePickup::NotEnabled') ) {
99 code => 'not_enabled',
101 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::LibraryIsClosed') ) {
104 code => 'library_is_closed',
105 patron => Koha::Patrons->find($borrowernumber)
107 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoWaitingHolds') ) {
110 code => 'no_waiting_holds',
112 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
115 code => 'too_many_pickups',
116 patron => Koha::Patrons->find($borrowernumber)
118 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMatchingSlots') ) {
121 code => 'no_matching_slots',
123 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable') ) {
126 code => 'no_more_pickups_available',
132 code => 'something_wrong_happened',
137 elsif ( $op eq 'cancel' ) {
138 my $id = $input->param('id');
139 my $curbside_pickup = Koha::CurbsidePickups->find($id);
140 $curbside_pickup->delete() if $curbside_pickup;
142 elsif ( $op eq 'mark-as-staged' ) {
143 my $id = $input->param('id');
144 my $curbside_pickup = Koha::CurbsidePickups->find($id);
145 $curbside_pickup->mark_as_staged if $curbside_pickup;
147 elsif ( $op eq 'mark-as-unstaged' ) {
148 my $id = $input->param('id');
149 my $curbside_pickup = Koha::CurbsidePickups->find($id);
150 $curbside_pickup->mark_as_unstaged if $curbside_pickup;
152 elsif ( $op eq 'mark-patron-has-arrived' ) {
153 my $id = $input->param('id');
154 my $curbside_pickup = Koha::CurbsidePickups->find($id);
155 $curbside_pickup->mark_patron_has_arrived if $curbside_pickup;
157 elsif ( $op eq 'mark-as-delivered' ) {
158 my $id = $input->param('id');
159 my $curbside_pickup = Koha::CurbsidePickups->find($id);
160 # FIXME Add a try-catch here
161 $curbside_pickup->mark_as_delivered if $curbside_pickup;
165 messages => \@messages,
168 policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
169 curbside_pickups => Koha::CurbsidePickups->search(
171 branchcode => $branchcode,
173 )->filter_by_scheduled_today,
177 output_html_with_http_headers $input, $cookie, $template->output;