Bug 32632: Add 'page-section' to some tools pages
[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 $auto_refresh  = $input->param('auto_refresh');
35 my $refresh_delay = $input->param('refresh_delay');
36 my @messages;
37
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39     {
40         template_name => "circ/curbside_pickups.tt",
41         query         => $input,
42         type          => "intranet",
43         flagsrequired => { circulate => 'manage_curbside_pickups' },
44     }
45 );
46
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');
51
52     my $patron = Koha::Patrons->find($borrowernumber);
53
54     my $existing_curbside_pickups = Koha::CurbsidePickups->search(
55         {
56             branchcode                => $branchcode,
57             borrowernumber            => $patron->id,
58             delivered_datetime        => undef,
59         }
60     )->filter_by_scheduled_today;
61
62     $tab = 'schedule-pickup';
63     $template->param(
64         patron      => $patron,
65         existing_curbside_pickups => $existing_curbside_pickups,
66     );
67 }
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');
72
73     try {
74         my $pickup = Koha::CurbsidePickup->new(
75             {
76                 branchcode                => $branchcode,
77                 borrowernumber            => $borrowernumber,
78                 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
79                 notes                     => $notes,
80             }
81         )->store;
82         $pickup->notify_new_pickup;
83     } catch {
84         if ( $_->isa('Koha::Exceptions::CurbsidePickup::NotEnabled') ) {
85             push @messages, {
86                 type   => 'error',
87                 code   => 'not_enabled',
88             };
89         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::LibraryIsClosed') ) {
90             push @messages, {
91                 type   => 'error',
92                 code   => 'library_is_closed',
93                 patron => Koha::Patrons->find($borrowernumber)
94             };
95         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoWaitingHolds') ) {
96             push @messages, {
97                 type   => 'error',
98                 code   => 'no_waiting_holds',
99             };
100         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
101             push @messages, {
102                 type   => 'error',
103                 code   => 'too_many_pickups',
104                 patron => Koha::Patrons->find($borrowernumber)
105             };
106         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMatchingSlots') ) {
107             push @messages, {
108                 type   => 'error',
109                 code   => 'no_matching_slots',
110             };
111         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable') ) {
112             push @messages, {
113                 type   => 'error',
114                 code   => 'no_more_pickups_available',
115             };
116         } else {
117             warn $_;
118             push @messages, {
119                 type   => 'error',
120                 code   => 'something_wrong_happened',
121             };
122         }
123     }
124 }
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;
129 }
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;
134 }
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;
139 }
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;
144 }
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;
150 }
151
152 $template->param(
153     messages => \@messages,
154     op       => $op,
155     tab      => $tab,
156     auto_refresh  => $auto_refresh,
157     refresh_delay => $refresh_delay,
158     policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
159     curbside_pickups => Koha::CurbsidePickups->search(
160         {
161             branchcode => $branchcode,
162         }
163       )->filter_by_scheduled_today,
164 );
165
166
167 output_html_with_http_headers $input, $cookie, $template->output;