Bug 32482: (follow-up) Add markup comments
[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 qw( output_html_with_http_headers );
24 use C4::Auth qw( get_template_and_user );
25 use C4::HoldsQueue qw( TransportCostMatrix UpdateTransportCostMatrix );
26
27 use Koha::Libraries;
28
29
30 my $input = CGI->new;
31
32 my ($template, $loggedinuser, $cookie)
33     = get_template_and_user({template_name => "admin/transport-cost-matrix.tt",
34                             query => $input,
35                             type => "intranet",
36                             flagsrequired => { parameters => 'manage_transfers' },
37                             });
38 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
39
40 my $update = ( $input->param('op') // '' ) eq 'set-cost-matrix';
41
42 my ($cost_matrix, $have_matrix);
43 unless ($update) {
44     $cost_matrix = TransportCostMatrix();
45     $have_matrix = keys %$cost_matrix if $cost_matrix;
46 }
47
48 my @branchloop = map { code => $_->branchcode, name => $_->branchname }, Koha::Libraries->search({}, { order_by => 'branchname' })->as_list;
49 my (@branchfromloop, @errors);
50 foreach my $branchfrom ( @branchloop ) {
51     my $fromcode = $branchfrom->{code};
52
53     my %from_row = ( code => $fromcode, name => $branchfrom->{name} );
54     foreach my $branchto ( @branchloop ) {
55         my $tocode = $branchto->{code};
56
57         my %from_to_input_def = ( code => $tocode, name => $branchto->{name} );
58         push @{ $from_row{branchtoloop} }, \%from_to_input_def;
59
60         if ($fromcode eq $tocode) {
61             $from_to_input_def{skip} = 1;
62             next;
63         }
64
65         (my $from_to = "${fromcode}_${tocode}") =~ s/\W//go;
66          $from_to_input_def{id} = $from_to;
67         my $input_name   = "cost_$from_to";
68         my $disable_name = "disable_$from_to";
69
70         if ($update) {
71             my $value = $from_to_input_def{value} = $input->param($input_name);
72             if ( $input->param($disable_name) ) {
73                 $from_to_input_def{disabled} = 1;
74             }
75             else {
76                 push @errors, "$from_row{name} -> $from_to_input_def{name}"
77                   unless $value =~ /\d/o && $value >= 0.0;
78             }
79         }
80         else {
81             if ($have_matrix) {
82                 if ( my $cell = $cost_matrix->{$tocode}{$fromcode} ) {
83                     $from_to_input_def{value} = $cell->{cost};
84                     $from_to_input_def{disabled} = 1 if $cell->{disable_transfer};
85                 } else {
86                     # matrix has been previously initialized, but a branch referenced here was created afterward.
87                     $from_to_input_def{disabled} = 1;
88                 }
89             } else {
90                 # First time initializing the matrix
91                 $from_to_input_def{disabled} = 1;
92             }
93         }
94     }
95
96 #              die Dumper(\%from_row);
97     push @branchfromloop, \%from_row;
98 }
99
100 if ($update && !@errors) {
101     my @update_recs = map {
102         my $from = $_->{code};
103         map { frombranch => $from, tobranch => $_->{code}, cost => $_->{value}, disable_transfer => $_->{disabled} || 0 },
104             grep { $_->{code} ne $from }
105             @{ $_->{branchtoloop} };
106     } @branchfromloop;
107
108     UpdateTransportCostMatrix(\@update_recs);
109 }
110
111 $template->param(
112     branchloop => \@branchloop,
113     branchfromloop => \@branchfromloop,
114     WARNING_transport_cost_matrix_off => !$use_transport_cost_matrix,
115     errors => \@errors,
116 );
117 output_html_with_http_headers $input, $cookie, $template->output;
118
119 exit 0;