Bug 30650: Notify the patron when a new curbside pickup is created
[koha.git] / circ / 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 use Modern::Perl;
19 use CGI qw ( -utf8 );
20 use Try::Tiny;
21 use C4::Context;
22 use C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_html_with_http_headers );
24
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::CurbsidePickups;
27 use Koha::CurbsidePickupPolicies;
28 use Koha::Libraries;
29 use Koha::Patrons;
30
31 my $input       = CGI->new;
32 my $op          = $input->param('op') || 'list';
33 my $tab         = $input->param('tab'),
34 my @messages;
35
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name => "circ/curbside_pickups.tt",
39         query         => $input,
40         type          => "intranet",
41         flagsrequired => { parameters => 'manage_curbside_pickups' },
42     }
43 );
44
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');
50
51     my $patron =
52       $cardnumber
53       ? Koha::Patrons->find( { cardnumber => $cardnumber } )
54       : Koha::Patrons->find($borrowernumber);
55
56     my $existing_curbside_pickups;
57
58     if ( $patron ){
59         $existing_curbside_pickups = Koha::CurbsidePickups->search(
60             {
61                 branchcode                => $branchcode,
62                 borrowernumber            => $patron->id,
63                 delivered_datetime        => undef,
64                 scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
65             }
66         );
67     } else {
68         push @messages, {
69             type => 'error',
70             code => 'no_patron_found',
71             cardnumber => $cardnumber
72         };
73     }
74
75     $tab = 'schedule-pickup';
76     $template->param(
77         patron      => $patron,
78         existing_curbside_pickups => $existing_curbside_pickups,
79     );
80 }
81 elsif ( $op eq 'create-pickup' ) {
82     my $borrowernumber            = $input->param('borrowernumber');
83     my $scheduled_pickup_datetime = $input->param('pickup_time');
84     my $notes                     = $input->param('notes');
85
86     try {
87         my $pickup = Koha::CurbsidePickup->new(
88             {
89                 branchcode                => $branchcode,
90                 borrowernumber            => $borrowernumber,
91                 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
92                 notes                     => $notes,
93             }
94         )->store;
95         $pickup->notify_new_pickup;
96     } catch {
97         if ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
98             push @messages, {
99                 type   => 'error',
100                 code   => 'too_many_pickups',
101                 patron => Koha::Patrons->find($borrowernumber)
102             };
103         } else {
104             warn $_;
105             push @messages, {
106                 type   => 'error',
107                 code   => 'something_wrong_happened',
108             };
109         }
110     }
111 }
112 elsif ( $op eq 'cancel' ) {
113     my $id              = $input->param('id');
114     my $curbside_pickup = Koha::CurbsidePickups->find($id);
115     $curbside_pickup->delete() if $curbside_pickup;
116 }
117 elsif ( $op eq 'mark-as-staged' ) {
118     my $id              = $input->param('id');
119     my $curbside_pickup = Koha::CurbsidePickups->find($id);
120     $curbside_pickup->mark_as_staged if $curbside_pickup;
121 }
122 elsif ( $op eq 'mark-as-unstaged' ) {
123     my $id              = $input->param('id');
124     my $curbside_pickup = Koha::CurbsidePickups->find($id);
125     $curbside_pickup->mark_as_unstaged if $curbside_pickup;
126 }
127 elsif ( $op eq 'mark-patron-has-arrived' ) {
128     my $id              = $input->param('id');
129     my $curbside_pickup = Koha::CurbsidePickups->find($id);
130     $curbside_pickup->mark_patron_has_arrived if $curbside_pickup;
131 }
132 elsif ( $op eq 'mark-as-delivered' ) {
133     my $id = $input->param('id');
134     my $curbside_pickup = Koha::CurbsidePickups->find($id);
135     # FIXME Add a try-catch here
136     $curbside_pickup->mark_as_delivered if $curbside_pickup;
137 }
138
139 $template->param(
140     messages => \@messages,
141     op       => $op,
142     tab      => $tab,
143     policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
144     curbside_pickups => Koha::CurbsidePickups->search(
145         {
146             branchcode                => $branchcode,
147             scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
148         }
149       ),
150 );
151
152
153 output_html_with_http_headers $input, $cookie, $template->output;