Bug 23204: (RM follow-up) Use Koha::Script
[koha.git] / admin / transport-cost-matrix.pl
blobbeea9d287755c9818e87e930e17b9b554e32aab1
1 #!/usr/bin/perl
2 # Copyright 2000-2002 Katipo Communications
3 # copyright 2010 BibLibre
5 # This file is part of Koha.
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.
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.
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>.
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);
29 use Koha::Libraries;
31 use Data::Dumper;
33 my $input = new CGI;
35 my ($template, $loggedinuser, $cookie)
36 = get_template_and_user({template_name => "admin/transport-cost-matrix.tt",
37 query => $input,
38 type => "intranet",
39 authnotrequired => 0,
40 flagsrequired => { parameters => 'manage_transfers' },
41 debug => 1,
42 });
43 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
45 my $update = ( $input->param('op') // '' ) eq 'set-cost-matrix';
47 my ($cost_matrix, $have_matrix);
48 unless ($update) {
49 $cost_matrix = TransportCostMatrix();
50 $have_matrix = keys %$cost_matrix if $cost_matrix;
53 my @branchloop = map { code => $_->branchcode, name => $_->branchname }, Koha::Libraries->search({}, { order_by => 'branchname' });
54 my (@branchfromloop, @errors);
55 foreach my $branchfrom ( @branchloop ) {
56 my $fromcode = $branchfrom->{code};
58 my %from_row = ( code => $fromcode, name => $branchfrom->{name} );
59 foreach my $branchto ( @branchloop ) {
60 my $tocode = $branchto->{code};
62 my %from_to_input_def = ( code => $tocode, name => $branchto->{name} );
63 push @{ $from_row{branchtoloop} }, \%from_to_input_def;
65 if ($fromcode eq $tocode) {
66 $from_to_input_def{skip} = 1;
67 next;
70 (my $from_to = "${fromcode}_${tocode}") =~ s/\W//go;
71 $from_to_input_def{id} = $from_to;
72 my $input_name = "cost_$from_to";
73 my $disable_name = "disable_$from_to";
75 if ($update) {
76 my $value = $from_to_input_def{value} = $input->param($input_name);
77 if ( $input->param($disable_name) ) {
78 $from_to_input_def{disabled} = 1;
80 else {
81 push @errors, "$from_row{name} -> $from_to_input_def{name}"
82 unless $value =~ /\d/o && $value >= 0.0;
85 else {
86 if ($have_matrix) {
87 if ( my $cell = $cost_matrix->{$tocode}{$fromcode} ) {
88 $from_to_input_def{value} = $cell->{cost};
89 $from_to_input_def{disabled} = 1 if $cell->{disable_transfer};
90 } else {
91 # matrix has been previously initialized, but a branch referenced here was created afterward.
92 $from_to_input_def{disabled} = 1;
94 } else {
95 # First time initializing the matrix
96 $from_to_input_def{disabled} = 1;
101 # die Dumper(\%from_row);
102 push @branchfromloop, \%from_row;
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;
113 UpdateTransportCostMatrix(\@update_recs);
116 $template->param(
117 branchloop => \@branchloop,
118 branchfromloop => \@branchfromloop,
119 WARNING_transport_cost_matrix_off => !$use_transport_cost_matrix,
120 errors => \@errors,
122 output_html_with_http_headers $input, $cookie, $template->output;
124 exit 0;