Merge branch 'bug_8365' into 3.12-master
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Debug;
28 use C4::Branch; # GetBranches
29 use C4::HoldsQueue qw(TransportCostMatrix UpdateTransportCostMatrix);
30
31 use Data::Dumper;
32
33 my $input = new CGI;
34
35 my ($template, $loggedinuser, $cookie)
36     = get_template_and_user({template_name => "admin/transport-cost-matrix.tmpl",
37                             query => $input,
38                             type => "intranet",
39                             authnotrequired => 0,
40                             flagsrequired => {parameters => 1},
41                             debug => 1,
42                             });
43 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
44
45 my $update = $input->param('op') eq 'set-cost-matrix';
46
47 my ($cost_matrix, $have_matrix);
48 unless ($update) {
49     $cost_matrix = TransportCostMatrix();
50     $have_matrix = keys %$cost_matrix if $cost_matrix;
51 }
52
53 my $branches = GetBranches();
54 my @branchloop = map { code => $_,
55                        name => $branches->{$_}->{'branchname'} },
56                  sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} }
57                  keys %$branches;
58 my (@branchfromloop, @cost, @errors);
59 foreach my $branchfrom ( @branchloop ) {
60     my $fromcode = $branchfrom->{code};
61
62     my %from_row = ( code => $fromcode, name => $branchfrom->{name} );
63     foreach my $branchto ( @branchloop ) {
64         my $tocode = $branchto->{code};
65
66         my %from_to_input_def = ( code => $tocode, name => $branchto->{name} );
67         push @{ $from_row{branchtoloop} }, \%from_to_input_def;
68
69         if ($fromcode eq $tocode) {
70             $from_to_input_def{skip} = 1;
71             next;
72         }
73
74         (my $from_to = "${fromcode}_${tocode}") =~ s/\W//go;
75          $from_to_input_def{id} = $from_to;
76         my $input_name   = "cost_$from_to";
77         my $disable_name = "disable_$from_to";
78
79         if ($update) {
80             my $value = $from_to_input_def{value} = $input->param($input_name);
81             if ( $input->param($disable_name) ) {
82                 $from_to_input_def{disabled} = 1;
83             }
84             else {
85                 push @errors, "Invalid value for $from_row{name} -> $from_to_input_def{name}"
86                   unless $value =~ /\d/o && $value >= 0.0;
87             }
88         }
89         else {
90             if ($have_matrix) {
91                 if ( my $cell = $cost_matrix->{$tocode}{$fromcode} ) {
92                     $from_to_input_def{value} = $cell->{cost};
93                     $from_to_input_def{disabled} = 1 if $cell->{disable_transfer};
94                 }
95             } else {
96                 $from_to_input_def{disabled} = 1;
97             }
98         }
99     }
100
101 #              die Dumper(\%from_row);
102     push @branchfromloop, \%from_row;
103 }
104
105 if ($update && !@errors) {
106     my @update_recs = map {
107         my $from = $_->{code};
108         map { frombranch => $from, tobranch => $_->{code}, cost => $_->{value}, disable_transfer => $_->{disabled} || 0 },
109             grep { $_->{code} ne $from }
110             @{ $_->{branchtoloop} };
111     } @branchfromloop;
112
113     UpdateTransportCostMatrix(\@update_recs);
114 }
115
116 $template->param(
117     branchloop => \@branchloop,
118     branchfromloop => \@branchfromloop,
119     WARNING_transport_cost_matrix_off => !$use_transport_cost_matrix,
120     errors => \@errors,
121 );
122 output_html_with_http_headers $input, $cookie, $template->output;
123
124 exit 0;