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,
65 )->filter_by_scheduled_today;
69 code => 'no_patron_found',
70 cardnumber => $cardnumber
74 $tab = 'schedule-pickup';
77 existing_curbside_pickups => $existing_curbside_pickups,
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');
86 my $pickup = Koha::CurbsidePickup->new(
88 branchcode => $branchcode,
89 borrowernumber => $borrowernumber,
90 scheduled_pickup_datetime => dt_from_string($scheduled_pickup_datetime),
94 $pickup->notify_new_pickup;
96 if ( $_->isa('Koha::Exceptions::CurbsidePickup::TooManyPickups') ) {
99 code => 'too_many_pickups',
100 patron => Koha::Patrons->find($borrowernumber)
106 code => 'something_wrong_happened',
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;
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;
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;
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;
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;
139 messages => \@messages,
142 policy => Koha::CurbsidePickupPolicies->find({ branchcode => $branchcode }),
143 curbside_pickups => Koha::CurbsidePickups->search(
145 branchcode => $branchcode,
147 )->filter_by_scheduled_today,
151 output_html_with_http_headers $input, $cookie, $template->output;