Bug 30650: Be more flexible with opening slots
[koha.git] / admin / curbside_pickup.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 C4::Context;
21 use C4::Auth qw( get_template_and_user );
22 use C4::Output qw( output_html_with_http_headers );
23
24 use Koha::CurbsidePickupPolicies;
25 use Koha::Libraries;
26
27 my $input       = CGI->new;
28 my $op          = $input->param('op') || 'list';
29 my @messages;
30
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32     {
33         template_name => "admin/curbside_pickup.tt",
34         query         => $input,
35         type          => "intranet",
36         flagsrequired => { parameters => 'manage_curbside_pickups' },
37     }
38 );
39
40 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
41 if ( $op eq 'save' ) {
42     foreach my $library ( $libraries->as_list ) {
43         my $branchcode = $library->branchcode;
44
45         my $params = {
46             branchcode      => $branchcode,
47             enabled         => scalar $input->param("enable-$branchcode") || 0,
48             pickup_interval => scalar $input->param("interval-$branchcode"),
49             patrons_per_interval    => scalar $input->param("max-per-interval-$branchcode"),
50             patron_scheduled_pickup => scalar $input->param("patron-scheduled-$branchcode") || 0,
51         };
52
53         my $policy =
54           Koha::CurbsidePickupPolicies->find_or_create( { branchcode => $branchcode } );
55         $policy->update($params);
56
57         $policy->opening_slots->delete;
58         my @pickup_slots = $input->multi_param("pickup-slot-" . $branchcode);
59         for my $pickup_slot ( @pickup_slots ) {
60             $policy->add_opening_slot($pickup_slot);
61         }
62     }
63     $op = 'list';
64 }
65
66 if ( $op eq 'list' ) {
67     $template->param(
68         policies => {
69             map { $_->branchcode => $_ }
70               Koha::CurbsidePickupPolicies->search->as_list
71         },
72         libraries => $libraries,
73     );
74 }
75
76 $template->param(
77     messages => \@messages,
78     op       => $op,
79 );
80
81 output_html_with_http_headers $input, $cookie, $template->output;