Bug 29897: Don't display empty li
[koha.git] / opac / opac-curbside-pickups.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18
19 use Modern::Perl;
20 use CGI qw ( -utf8 );
21 use Try::Tiny;
22 use C4::Members;
23 use C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
25
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::CurbsidePickups;
28 use Koha::CurbsidePickupPolicies;
29 use Koha::Libraries;
30 use Koha::Patrons;
31
32 my $input = CGI->new;
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34     {
35         template_name   => "opac-curbside-pickups.tt",
36         query           => $input,
37         type            => "opac",
38     }
39 );
40
41 unless( C4::Context->preference('CurbsidePickup') ) {
42     print $input->redirect("/cgi-bin/koha/errors/404.pl");
43     exit;
44 }
45
46 my $op = $input->param('op') || 'list';
47
48 my $logged_in_patron = Koha::Patrons->find($borrowernumber);
49 my $branchcode = $input->param('pickup_branch');
50 my @messages;
51
52 if ( $op eq 'create-pickup' ) {
53     my $scheduled_pickup_datetime = $input->param('pickup_time');
54     my $notes                     = $input->param('notes');
55
56     try {
57         my $pickup = Koha::CurbsidePickup->new(
58             {
59                 branchcode                => $branchcode,
60                 borrowernumber            => $borrowernumber,
61                 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
62                 notes                     => $notes,
63             }
64         )->store;
65         $pickup->notify_new_pickup;
66     } catch {
67         if ( $_->isa('Koha::Exceptions::CurbsidePickup::NotEnabled') ) {
68             push @messages, {
69                 type   => 'error',
70                 code   => 'not_enabled',
71             };
72         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::LibraryIsClosed') ) {
73             push @messages, {
74                 type   => 'error',
75                 code   => 'library_is_closed',
76                 patron => Koha::Patrons->find($borrowernumber)
77             };
78         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoWaitingHolds') ) {
79             push @messages, {
80                 type   => 'error',
81                 code   => 'no_waiting_holds',
82             };
83         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
84             push @messages, {
85                 type   => 'error',
86                 code   => 'too_many_pickups',
87                 patron => Koha::Patrons->find($borrowernumber)
88             };
89         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMatchingSlots') ) {
90             push @messages, {
91                 type   => 'error',
92                 code   => 'no_matching_slots',
93             };
94         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable') ) {
95             push @messages, {
96                 type   => 'error',
97                 code   => 'no_more_pickups_available',
98             };
99         } else {
100             warn $_;
101             push @messages, {
102                 type   => 'error',
103                 code   => 'something_wrong_happened',
104             };
105         }
106     }
107 }
108 elsif ( $op eq 'cancel-pickup' ) {
109     my $id              = $input->param('pickup_id');
110     my $curbside_pickup = Koha::CurbsidePickups->search(
111         { borrowernumber => $borrowernumber } )->find($id);
112     $curbside_pickup->delete
113       if $curbside_pickup
114       && !$curbside_pickup->delivered_datetime;
115 }
116 elsif ( $op eq 'arrival-alert' ) {
117     my $id              = $input->param('pickup_id');
118     my $curbside_pickup = Koha::CurbsidePickups->search(
119         { borrowernumber => $borrowernumber } )->find($id);
120     $curbside_pickup->mark_patron_has_arrived if $curbside_pickup;
121     push @messages, {
122         type => 'message',
123         code => 'library_notified',
124     }
125 }
126
127 $template->param(
128     messages => \@messages,
129     policies => Koha::CurbsidePickupPolicies->search(
130         {
131             enabled                 => 1,
132             patron_scheduled_pickup => 1,
133         }
134     ),
135     patron_curbside_pickups => Koha::CurbsidePickups->search(
136         {
137             borrowernumber            => $logged_in_patron->borrowernumber,
138         },
139         {
140             order_by => { -asc => 'scheduled_pickup_datetime' }
141         }
142     )->filter_by_scheduled_today,
143     curbside_pickups => Koha::CurbsidePickups->search(
144         {},
145         {
146             order_by => { -asc => 'scheduled_pickup_datetime' }
147         }
148     )->filter_by_scheduled_today,
149 );
150
151 output_html_with_http_headers $input, $cookie, $template->output, undef,
152   { force_no_caching => 1 };