3 # This file is part of Koha.
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.
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.
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>.
22 use C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_html_with_http_headers );
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::CurbsidePickups;
27 use Koha::CurbsidePickupPolicies;
32 my $op = $input->param('op') || 'list';
33 my $tab = $input->param('tab'),
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 template_name => "circ/curbside_pickups.tt",
41 flagsrequired => { parameters => 'manage_curbside_pickups' },
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');
53 ? Koha::Patrons->find( { cardnumber => $cardnumber } )
54 : Koha::Patrons->find($borrowernumber);
56 my $existing_curbside_pickups;
59 $existing_curbside_pickups = Koha::CurbsidePickups->search(
61 branchcode => $branchcode,
62 borrowernumber => $patron->id,
63 delivered_datetime => undef,
64 scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
70 code => 'no_patron_found',
71 cardnumber => $cardnumber
75 $tab = 'schedule-pickup';
78 existing_curbside_pickups => $existing_curbside_pickups,
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');
87 my $pickup = Koha::CurbsidePickup->new(
89 branchcode => $branchcode,
90 borrowernumber => $borrowernumber,
91 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
95 $pickup->notify_new_pickup;
97 if ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
100 code => 'too_many_pickups',
101 patron => Koha::Patrons->find($borrowernumber)
107 code => 'something_wrong_happened',
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;
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;
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;
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;
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;
140 messages => \@messages,
143 policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
144 curbside_pickups => Koha::CurbsidePickups->search(
146 branchcode => $branchcode,
147 scheduled_pickup_datetime => { '>' => \'DATE(NOW())' },
153 output_html_with_http_headers $input, $cookie, $template->output;