Bug 15006: [QA Follow-up] Satisfy qa tools with one tab less
[koha.git] / admin / branch_transfer_limits.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25 use C4::Auth;
26 use C4::Context;
27 use C4::Output;
28 use C4::Koha;
29 use C4::Branch; 
30 use C4::Circulation qw{ IsBranchTransferAllowed DeleteBranchTransferLimits CreateBranchTransferLimit };
31
32 my $input = new CGI;
33
34 my ($template, $loggedinuser, $cookie)
35     = get_template_and_user({template_name => "admin/branch_transfer_limits.tt",
36                              query => $input,
37                              type => "intranet",
38                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
39                              debug => 1,
40                              });
41
42 my $dbh = C4::Context->dbh;
43 my $branchcode;
44 if((!defined($input->param('branchcode'))) & mybranch() ne '')
45 {
46         $branchcode = mybranch();
47 }
48 else
49 {
50         $branchcode = $input->param('branchcode');
51 }
52
53 my $branchname = GetBranchName($branchcode);
54
55 # Getting the branches for user selection
56 my $branches = GetBranches();
57 my @branch_loop;
58 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
59     my %row =(value => $thisbranch,
60               branchname => $branches->{$thisbranch}->{'branchname'},
61               selected => $thisbranch eq $branchcode ? 1 : 0,
62              );
63     push @branch_loop, \%row;
64 }
65
66
67 # Set the template language for the correct limit type using $limitType
68 my $limitType = C4::Context->preference("BranchTransferLimitsType") || "ccode";
69
70 my @codes;
71 my @branchcodes;
72
73 my $sth;
74 if ( $limitType eq 'ccode' ) {
75         $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"');
76 } elsif ( $limitType eq 'itemtype' ) {
77         $sth = $dbh->prepare('SELECT itemtype FROM itemtypes');
78 }
79 $sth->execute();
80 while ( my $row = $sth->fetchrow_hashref ) {
81         push( @codes, $row->{ $limitType } );
82 }
83
84 $sth = $dbh->prepare("SELECT branchcode FROM branches");
85 $sth->execute();
86 while ( my $row = $sth->fetchrow_hashref ) {
87         push( @branchcodes, $row->{'branchcode'} );
88 }
89
90 ## If Form Data Passed, Update the Database
91 if ( $input->param('updateLimits') ) {
92     DeleteBranchTransferLimits($branchcode);
93
94
95         foreach my $code ( @codes ) {
96                 foreach my $toBranch ( @branchcodes ) {
97                         my $isSet = not $input->param( $code . "_" . $toBranch);
98                         if ( $isSet ) {
99                             CreateBranchTransferLimit( $toBranch, $branchcode, $code );
100                         }
101                 }
102         }
103 }
104
105 ## Build branchcode loop
106 my @branchcode_loop;
107 foreach my $branchcode ( @branchcodes ) {
108         my %row_data;
109         $row_data{ branchcode } = $branchcode;
110         push ( @branchcode_loop, \%row_data );
111 }
112 my $branchcount = scalar(@branchcode_loop);
113
114 ## Build the default data
115 my @codes_loop;
116 foreach my $code ( @codes ) {
117         my @to_branch_loop;
118         my %row_data;
119         $row_data{ code } = $code;
120         $row_data{ to_branch_loop } = \@to_branch_loop;
121         foreach my $toBranch ( @branchcodes ) {
122                 my %row_data;
123                 my $isChecked = IsBranchTransferAllowed( $toBranch, $branchcode, $code );
124                 $row_data{ code }         = $code;
125                 $row_data{ toBranch }     = $toBranch;
126                 $row_data{ isChecked }    = $isChecked; 
127                 $row_data{ toBranchname } = GetBranchName($toBranch);   
128                 push( @to_branch_loop, \%row_data );
129         }
130
131         push( @codes_loop, \%row_data );
132 }
133
134
135 $template->param(
136                 branchcount => $branchcount,
137                 codes_loop => \@codes_loop,
138                 branch_loop => \@branch_loop,
139                 branchcode_loop => \@branchcode_loop,
140                 branchcode => $branchcode,
141                 branchname => $branchname,
142         limitType => $limitType,
143                 );
144
145 output_html_with_http_headers $input, $cookie, $template->output;
146