Bug 30650: Notify the patron when a new curbside pickup is created
[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::TooManyPickups') ) {
68             push @messages, {
69                 type   => 'error',
70                 code   => 'too_many_pickups',
71                 patron => Koha::Patrons->find($borrowernumber)
72             };
73         } else {
74             warn $_;
75             push @messages, {
76                 type   => 'error',
77                 code   => 'something_wrong_happened',
78             };
79         }
80     }
81 }
82 elsif ( $op eq 'cancel-pickup' ) {
83     my $id              = $input->param('pickup_id');
84     my $curbside_pickup = Koha::CurbsidePickups->search(
85         { borrowernumber => $borrowernumber } )->find($id);
86     $curbside_pickup->delete
87       if $curbside_pickup
88       && !$curbside_pickup->delivered_datetime;
89 }
90 elsif ( $op eq 'arrival-alert' ) {
91     my $id              = $input->param('pickup_id');
92     my $curbside_pickup = Koha::CurbsidePickups->search(
93         { borrowernumber => $borrowernumber } )->find($id);
94     $curbside_pickup->mark_patron_has_arrived if $curbside_pickup;
95     push @messages, {
96         type => 'message',
97         code => 'library_notified',
98     }
99 }
100
101 $template->param(
102     policies => Koha::CurbsidePickupPolicies->search(
103         {
104             enabled                 => 1,
105             patron_scheduled_pickup => 1,
106         }
107     ),
108     patron_curbside_pickups => Koha::CurbsidePickups->search(
109         {
110             borrowernumber            => $logged_in_patron->borrowernumber,
111             scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
112         },
113         {
114             order_by => { -asc => 'scheduled_pickup_datetime' }
115         }
116     ),
117     curbside_pickups => Koha::CurbsidePickups->search(
118         {
119             scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
120         },
121         {
122             order_by => { -asc => 'scheduled_pickup_datetime' }
123         }
124     ),
125 );
126
127 output_html_with_http_headers $input, $cookie, $template->output, undef,
128   { force_no_caching => 1 };