Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / admin / transport-cost-matrix.pl
1 #!/usr/bin/perl
2 # Copyright 2000-2002 Katipo Communications
3 # copyright 2010 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Context;
23 use C4::Output;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Debug;
27 use C4::HoldsQueue qw(TransportCostMatrix UpdateTransportCostMatrix);
28
29 use Koha::Libraries;
30
31 use Data::Dumper;
32
33 my $input = CGI->new;
34
35 my ($template, $loggedinuser, $cookie)
36     = get_template_and_user({template_name => "admin/transport-cost-matrix.tt",
37                             query => $input,
38                             type => "intranet",
39                             flagsrequired => { parameters => 'manage_transfers' },
40                             });
41 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
42
43 my $update = ( $input->param('op') // '' ) eq 'set-cost-matrix';
44
45 my ($cost_matrix, $have_matrix);
46 unless ($update) {
47     $cost_matrix = TransportCostMatrix({ ignore_holds_queue_skip_closed => 1 });
48     $have_matrix = keys %$cost_matrix if $cost_matrix;
49 }
50
51 my @branchloop = map { code => $_->branchcode, name => $_->branchname }, Koha::Libraries->search({}, { order_by => 'branchname' });
52 my (@branchfromloop, @errors);
53 foreach my $branchfrom ( @branchloop ) {
54     my $fromcode = $branchfrom->{code};
55
56     my %from_row = ( code => $fromcode, name => $branchfrom->{name} );
57     foreach my $branchto ( @branchloop ) {
58         my $tocode = $branchto->{code};
59
60         my %from_to_input_def = ( code => $tocode, name => $branchto->{name} );
61         push @{ $from_row{branchtoloop} }, \%from_to_input_def;
62
63         if ($fromcode eq $tocode) {
64             $from_to_input_def{skip} = 1;
65             next;
66         }
67
68         (my $from_to = "${fromcode}_${tocode}") =~ s/\W//go;
69          $from_to_input_def{id} = $from_to;
70         my $input_name   = "cost_$from_to";
71         my $disable_name = "disable_$from_to";
72
73         if ($update) {
74             my $value = $from_to_input_def{value} = $input->param($input_name);
75             if ( $input->param($disable_name) ) {
76                 $from_to_input_def{disabled} = 1;
77             }
78             else {
79                 push @errors, "$from_row{name} -> $from_to_input_def{name}"
80                   unless $value =~ /\d/o && $value >= 0.0;
81             }
82         }
83         else {
84             if ($have_matrix) {
85                 if ( my $cell = $cost_matrix->{$tocode}{$fromcode} ) {
86                     $from_to_input_def{value} = $cell->{cost};
87                     $from_to_input_def{disabled} = 1 if $cell->{disable_transfer};
88                 } else {
89                     # matrix has been previously initialized, but a branch referenced here was created afterward.
90                     $from_to_input_def{disabled} = 1;
91                 }
92             } else {
93                 # First time initializing the matrix
94                 $from_to_input_def{disabled} = 1;
95             }
96         }
97     }
98
99 #              die Dumper(\%from_row);
100     push @branchfromloop, \%from_row;
101 }
102
103 if ($update && !@errors) {
104     my @update_recs = map {
105         my $from = $_->{code};
106         map { frombranch => $from, tobranch => $_->{code}, cost => $_->{value}, disable_transfer => $_->{disabled} || 0 },
107             grep { $_->{code} ne $from }
108             @{ $_->{branchtoloop} };
109     } @branchfromloop;
110
111     UpdateTransportCostMatrix(\@update_recs);
112 }
113
114 $template->param(
115     branchloop => \@branchloop,
116     branchfromloop => \@branchfromloop,
117     WARNING_transport_cost_matrix_off => !$use_transport_cost_matrix,
118     errors => \@errors,
119 );
120 output_html_with_http_headers $input, $cookie, $template->output;
121
122 exit 0;