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