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');
34 my $auto_refresh = $input->param('auto_refresh');
35 my $refresh_delay = $input->param('refresh_delay');
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40 template_name => "circ/curbside_pickups.tt",
43 flagsrequired => { circulate => 'manage_curbside_pickups' },
47 my $branchcode = C4::Context->userenv()->{'branch'};
48 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
49 if ( $op eq 'find-patron' ) {
50 my $borrowernumber = $input->param('borrowernumber');
52 my $patron = Koha::Patrons->find($borrowernumber);
54 my $existing_curbside_pickups = Koha::CurbsidePickups->search(
56 branchcode => $branchcode,
57 borrowernumber => $patron->id,
58 delivered_datetime => undef,
60 )->filter_by_scheduled_today;
62 $tab = 'schedule-pickup';
65 existing_curbside_pickups => $existing_curbside_pickups,
68 elsif ( $op eq 'create-pickup' ) {
69 my $borrowernumber = $input->param('borrowernumber');
70 my $scheduled_pickup_datetime = $input->param('pickup_time');
71 my $notes = $input->param('notes');
74 my $pickup = Koha::CurbsidePickup->new(
76 branchcode => $branchcode,
77 borrowernumber => $borrowernumber,
78 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
82 $pickup->notify_new_pickup;
84 if ( $_->isa('Koha::Exceptions::CurbsidePickup::NotEnabled') ) {
87 code => 'not_enabled',
89 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::LibraryIsClosed') ) {
92 code => 'library_is_closed',
93 patron => Koha::Patrons->find($borrowernumber)
95 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoWaitingHolds') ) {
98 code => 'no_waiting_holds',
100 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
103 code => 'too_many_pickups',
104 patron => Koha::Patrons->find($borrowernumber)
106 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMatchingSlots') ) {
109 code => 'no_matching_slots',
111 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable') ) {
114 code => 'no_more_pickups_available',
120 code => 'something_wrong_happened',
125 elsif ( $op eq 'cancel' ) {
126 my $id = $input->param('id');
127 my $curbside_pickup = Koha::CurbsidePickups->find($id);
128 $curbside_pickup->delete() if $curbside_pickup;
130 elsif ( $op eq 'mark-as-staged' ) {
131 my $id = $input->param('id');
132 my $curbside_pickup = Koha::CurbsidePickups->find($id);
133 $curbside_pickup->mark_as_staged if $curbside_pickup;
135 elsif ( $op eq 'mark-as-unstaged' ) {
136 my $id = $input->param('id');
137 my $curbside_pickup = Koha::CurbsidePickups->find($id);
138 $curbside_pickup->mark_as_unstaged if $curbside_pickup;
140 elsif ( $op eq 'mark-patron-has-arrived' ) {
141 my $id = $input->param('id');
142 my $curbside_pickup = Koha::CurbsidePickups->find($id);
143 $curbside_pickup->mark_patron_has_arrived if $curbside_pickup;
145 elsif ( $op eq 'mark-as-delivered' ) {
146 my $id = $input->param('id');
147 my $curbside_pickup = Koha::CurbsidePickups->find($id);
148 # FIXME Add a try-catch here
149 $curbside_pickup->mark_as_delivered if $curbside_pickup;
153 messages => \@messages,
156 auto_refresh => $auto_refresh,
157 refresh_delay => $refresh_delay,
158 policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
159 curbside_pickups => Koha::CurbsidePickups->search(
161 branchcode => $branchcode,
163 { order_by => [ { -desc => 'delivered_datetime' }, 'scheduled_pickup_datetime' ], }
164 )->filter_by_scheduled_today,
168 output_html_with_http_headers $input, $cookie, $template->output;