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 => { circulate => '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 $borrowernumber = $input->param('borrowernumber');
50 my $patron = Koha::Patrons->find($borrowernumber);
52 my $existing_curbside_pickups = Koha::CurbsidePickups->search(
54 branchcode => $branchcode,
55 borrowernumber => $patron->id,
56 delivered_datetime => undef,
58 )->filter_by_scheduled_today;
60 $tab = 'schedule-pickup';
63 existing_curbside_pickups => $existing_curbside_pickups,
66 elsif ( $op eq 'create-pickup' ) {
67 my $borrowernumber = $input->param('borrowernumber');
68 my $scheduled_pickup_datetime = $input->param('pickup_time');
69 my $notes = $input->param('notes');
72 my $pickup = Koha::CurbsidePickup->new(
74 branchcode => $branchcode,
75 borrowernumber => $borrowernumber,
76 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
80 $pickup->notify_new_pickup;
82 if ( $_->isa('Koha::Exceptions::CurbsidePickup::NotEnabled') ) {
85 code => 'not_enabled',
87 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::LibraryIsClosed') ) {
90 code => 'library_is_closed',
91 patron => Koha::Patrons->find($borrowernumber)
93 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoWaitingHolds') ) {
96 code => 'no_waiting_holds',
98 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
101 code => 'too_many_pickups',
102 patron => Koha::Patrons->find($borrowernumber)
104 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMatchingSlots') ) {
107 code => 'no_matching_slots',
109 } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable') ) {
112 code => 'no_more_pickups_available',
118 code => 'something_wrong_happened',
123 elsif ( $op eq 'cancel' ) {
124 my $id = $input->param('id');
125 my $curbside_pickup = Koha::CurbsidePickups->find($id);
126 $curbside_pickup->delete() if $curbside_pickup;
128 elsif ( $op eq 'mark-as-staged' ) {
129 my $id = $input->param('id');
130 my $curbside_pickup = Koha::CurbsidePickups->find($id);
131 $curbside_pickup->mark_as_staged if $curbside_pickup;
133 elsif ( $op eq 'mark-as-unstaged' ) {
134 my $id = $input->param('id');
135 my $curbside_pickup = Koha::CurbsidePickups->find($id);
136 $curbside_pickup->mark_as_unstaged if $curbside_pickup;
138 elsif ( $op eq 'mark-patron-has-arrived' ) {
139 my $id = $input->param('id');
140 my $curbside_pickup = Koha::CurbsidePickups->find($id);
141 $curbside_pickup->mark_patron_has_arrived if $curbside_pickup;
143 elsif ( $op eq 'mark-as-delivered' ) {
144 my $id = $input->param('id');
145 my $curbside_pickup = Koha::CurbsidePickups->find($id);
146 # FIXME Add a try-catch here
147 $curbside_pickup->mark_as_delivered if $curbside_pickup;
151 messages => \@messages,
154 policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
155 curbside_pickups => Koha::CurbsidePickups->search(
157 branchcode => $branchcode,
159 )->filter_by_scheduled_today,
163 output_html_with_http_headers $input, $cookie, $template->output;