Bug 17318: Unit tests
[koha.git] / admin / transport-cost-matrix.pl
blob0aab030729f59c707629cac05c34a8717dbde4a6
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 strict;
21 use warnings;
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Debug;
28 use C4::HoldsQueue qw(TransportCostMatrix UpdateTransportCostMatrix);
30 use Koha::Libraries;
32 use Data::Dumper;
34 my $input = new CGI;
36 my ($template, $loggedinuser, $cookie)
37 = get_template_and_user({template_name => "admin/transport-cost-matrix.tt",
38 query => $input,
39 type => "intranet",
40 authnotrequired => 0,
41 flagsrequired => {parameters => 1},
42 debug => 1,
43 });
44 my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix");
46 my $update = ( $input->param('op') // '' ) eq 'set-cost-matrix';
48 my ($cost_matrix, $have_matrix);
49 unless ($update) {
50 $cost_matrix = TransportCostMatrix();
51 $have_matrix = keys %$cost_matrix if $cost_matrix;
54 my @branchloop = map { code => $_->branchcode, name => $_->branchname }, Koha::Libraries->search({}, { order_by => 'branchname' });
55 my (@branchfromloop, @errors);
56 foreach my $branchfrom ( @branchloop ) {
57 my $fromcode = $branchfrom->{code};
59 my %from_row = ( code => $fromcode, name => $branchfrom->{name} );
60 foreach my $branchto ( @branchloop ) {
61 my $tocode = $branchto->{code};
63 my %from_to_input_def = ( code => $tocode, name => $branchto->{name} );
64 push @{ $from_row{branchtoloop} }, \%from_to_input_def;
66 if ($fromcode eq $tocode) {
67 $from_to_input_def{skip} = 1;
68 next;
71 (my $from_to = "${fromcode}_${tocode}") =~ s/\W//go;
72 $from_to_input_def{id} = $from_to;
73 my $input_name = "cost_$from_to";
74 my $disable_name = "disable_$from_to";
76 if ($update) {
77 my $value = $from_to_input_def{value} = $input->param($input_name);
78 if ( $input->param($disable_name) ) {
79 $from_to_input_def{disabled} = 1;
81 else {
82 push @errors, "$from_row{name} -> $from_to_input_def{name}"
83 unless $value =~ /\d/o && $value >= 0.0;
86 else {
87 if ($have_matrix) {
88 if ( my $cell = $cost_matrix->{$tocode}{$fromcode} ) {
89 $from_to_input_def{value} = $cell->{cost};
90 $from_to_input_def{disabled} = 1 if $cell->{disable_transfer};
91 } else {
92 # matrix has been previously initialized, but a branch referenced here was created afterward.
93 $from_to_input_def{disabled} = 1;
95 } else {
96 # First time initializing the matrix
97 $from_to_input_def{disabled} = 1;
102 # die Dumper(\%from_row);
103 push @branchfromloop, \%from_row;
106 if ($update && !@errors) {
107 my @update_recs = map {
108 my $from = $_->{code};
109 map { frombranch => $from, tobranch => $_->{code}, cost => $_->{value}, disable_transfer => $_->{disabled} || 0 },
110 grep { $_->{code} ne $from }
111 @{ $_->{branchtoloop} };
112 } @branchfromloop;
114 UpdateTransportCostMatrix(\@update_recs);
117 $template->param(
118 branchloop => \@branchloop,
119 branchfromloop => \@branchfromloop,
120 WARNING_transport_cost_matrix_off => !$use_transport_cost_matrix,
121 errors => \@errors,
123 output_html_with_http_headers $input, $cookie, $template->output;
125 exit 0;