Bug 32030: Add margin in agreement's filters block
[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 => { circulate => '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 $borrowernumber = $input->param('borrowernumber');
49
50     my $patron = Koha::Patrons->find($borrowernumber);
51
52     my $existing_curbside_pickups = Koha::CurbsidePickups->search(
53         {
54             branchcode                => $branchcode,
55             borrowernumber            => $patron->id,
56             delivered_datetime        => undef,
57         }
58     )->filter_by_scheduled_today;
59
60     $tab = 'schedule-pickup';
61     $template->param(
62         patron      => $patron,
63         existing_curbside_pickups => $existing_curbside_pickups,
64     );
65 }
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');
70
71     try {
72         my $pickup = Koha::CurbsidePickup->new(
73             {
74                 branchcode                => $branchcode,
75                 borrowernumber            => $borrowernumber,
76                 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
77                 notes                     => $notes,
78             }
79         )->store;
80         $pickup->notify_new_pickup;
81     } catch {
82         if ( $_->isa('Koha::Exceptions::CurbsidePickup::NotEnabled') ) {
83             push @messages, {
84                 type   => 'error',
85                 code   => 'not_enabled',
86             };
87         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::LibraryIsClosed') ) {
88             push @messages, {
89                 type   => 'error',
90                 code   => 'library_is_closed',
91                 patron => Koha::Patrons->find($borrowernumber)
92             };
93         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoWaitingHolds') ) {
94             push @messages, {
95                 type   => 'error',
96                 code   => 'no_waiting_holds',
97             };
98         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
99             push @messages, {
100                 type   => 'error',
101                 code   => 'too_many_pickups',
102                 patron => Koha::Patrons->find($borrowernumber)
103             };
104         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMatchingSlots') ) {
105             push @messages, {
106                 type   => 'error',
107                 code   => 'no_matching_slots',
108             };
109         } elsif ( $_->isa('Koha::Exceptions::CurbsidePickup::NoMorePickupsAvailable') ) {
110             push @messages, {
111                 type   => 'error',
112                 code   => 'no_more_pickups_available',
113             };
114         } else {
115             warn $_;
116             push @messages, {
117                 type   => 'error',
118                 code   => 'something_wrong_happened',
119             };
120         }
121     }
122 }
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;
127 }
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;
132 }
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;
137 }
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;
142 }
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;
148 }
149
150 $template->param(
151     messages => \@messages,
152     op       => $op,
153     tab      => $tab,
154     policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
155     curbside_pickups => Koha::CurbsidePickups->search(
156         {
157             branchcode => $branchcode,
158         }
159       )->filter_by_scheduled_today,
160 );
161
162
163 output_html_with_http_headers $input, $cookie, $template->output;