Bug 30650: Add filter_by_scheduled_today
[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             }
65         )->filter_by_scheduled_today;
66     } else {
67         push @messages, {
68             type => 'error',
69             code => 'no_patron_found',
70             cardnumber => $cardnumber
71         };
72     }
73
74     $tab = 'schedule-pickup';
75     $template->param(
76         patron      => $patron,
77         existing_curbside_pickups => $existing_curbside_pickups,
78     );
79 }
80 elsif ( $op eq 'create-pickup' ) {
81     my $borrowernumber            = $input->param('borrowernumber');
82     my $scheduled_pickup_datetime = $input->param('pickup_time');
83     my $notes                     = $input->param('notes');
84
85     try {
86         my $pickup = Koha::CurbsidePickup->new(
87             {
88                 branchcode                => $branchcode,
89                 borrowernumber            => $borrowernumber,
90                 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
91                 notes                     => $notes,
92             }
93         )->store;
94         $pickup->notify_new_pickup;
95     } catch {
96         if ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
97             push @messages, {
98                 type   => 'error',
99                 code   => 'too_many_pickups',
100                 patron => Koha::Patrons->find($borrowernumber)
101             };
102         } else {
103             warn $_;
104             push @messages, {
105                 type   => 'error',
106                 code   => 'something_wrong_happened',
107             };
108         }
109     }
110 }
111 elsif ( $op eq 'cancel' ) {
112     my $id              = $input->param('id');
113     my $curbside_pickup = Koha::CurbsidePickups->find($id);
114     $curbside_pickup->delete() if $curbside_pickup;
115 }
116 elsif ( $op eq 'mark-as-staged' ) {
117     my $id              = $input->param('id');
118     my $curbside_pickup = Koha::CurbsidePickups->find($id);
119     $curbside_pickup->mark_as_staged if $curbside_pickup;
120 }
121 elsif ( $op eq 'mark-as-unstaged' ) {
122     my $id              = $input->param('id');
123     my $curbside_pickup = Koha::CurbsidePickups->find($id);
124     $curbside_pickup->mark_as_unstaged if $curbside_pickup;
125 }
126 elsif ( $op eq 'mark-patron-has-arrived' ) {
127     my $id              = $input->param('id');
128     my $curbside_pickup = Koha::CurbsidePickups->find($id);
129     $curbside_pickup->mark_patron_has_arrived if $curbside_pickup;
130 }
131 elsif ( $op eq 'mark-as-delivered' ) {
132     my $id = $input->param('id');
133     my $curbside_pickup = Koha::CurbsidePickups->find($id);
134     # FIXME Add a try-catch here
135     $curbside_pickup->mark_as_delivered if $curbside_pickup;
136 }
137
138 $template->param(
139     messages => \@messages,
140     op       => $op,
141     tab      => $tab,
142     policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
143     curbside_pickups => Koha::CurbsidePickups->search(
144         {
145             branchcode => $branchcode,
146         }
147       )->filter_by_scheduled_today,
148 );
149
150
151 output_html_with_http_headers $input, $cookie, $template->output;