Revamp interface of branch transfer limits to give finer conrol of bulk selections.
[koha.git] / admin / branch_transfer_limits.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Branch; 
29 use C4::Circulation qw{ IsBranchTransferAllowed DeleteBranchTransferLimits CreateBranchTransferLimit };
30
31 my $input = new CGI;
32
33 my ($template, $loggedinuser, $cookie)
34     = get_template_and_user({template_name => "admin/branch_transfer_limits.tmpl",
35                              query => $input,
36                              type => "intranet",
37                              flagsrequired => {borrowers => 1},
38                              debug => 1,
39                              });
40
41 my $dbh = C4::Context->dbh;
42
43 # Set the template language for the correct limit type
44 my $limit_phrase = 'Collection Code';
45 my $limitType = C4::Context->preference("BranchTransferLimitsType");
46 if ( $limitType eq 'itemtype' ) {
47         $limit_phrase = 'Item Type';
48 }
49
50 my @codes;
51 my @branchcodes;
52
53 my $sth;
54 if ( $limitType eq 'ccode' ) {
55         $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"');
56 } elsif ( $limitType eq 'itemtype' ) {
57         $sth = $dbh->prepare('SELECT itemtype FROM itemtypes');
58 }
59 $sth->execute();
60 while ( my $row = $sth->fetchrow_hashref ) {
61         push( @codes, $row->{ $limitType } );
62 }
63
64 $sth = $dbh->prepare("SELECT branchcode FROM branches");
65 $sth->execute();
66 while ( my $row = $sth->fetchrow_hashref ) {
67         push( @branchcodes, $row->{'branchcode'} );
68 }
69
70 ## If Form Data Passed, Update the Database
71 if ( $input->param('updateLimits') ) {
72     DeleteBranchTransferLimits();
73
74         foreach my $code ( @codes ) {
75                 foreach my $toBranch ( @branchcodes ) {
76                         foreach my $fromBranch ( @branchcodes ) {
77                                 my $isSet = $input->param( $code . "_" . $toBranch . "_" . $fromBranch );
78                                 if ( $isSet ) {
79                                     CreateBranchTransferLimit( $toBranch, $fromBranch, $code );
80                                 }
81                         }
82                 }
83         }
84 }
85
86 ## Build branchcode loop
87 my @branchcode_loop;
88 foreach my $branchcode ( @branchcodes ) {
89         my %row_data;
90         $row_data{ branchcode } = $branchcode;
91         push ( @branchcode_loop, \%row_data );
92 }
93 my $branchcount = scalar(@branchcode_loop);
94
95 ## Build the default data
96 my @codes_loop;
97 foreach my $code ( @codes ) {
98         my @to_branch_loop;
99         my %row_data;
100         $row_data{ code } = $code;
101         $row_data{ to_branch_loop } = \@to_branch_loop;
102         foreach my $toBranch ( @branchcodes ) {
103                 my @from_branch_loop;
104                 my %row_data;
105                 $row_data{ code } = $code;
106                 $row_data{ toBranch } = $toBranch;
107                 $row_data{ from_branch_loop } = \@from_branch_loop;
108                 
109                 foreach my $fromBranch ( @branchcodes ) {
110                         my %row_data;
111                         my $isChecked = ! IsBranchTransferAllowed( $toBranch, $fromBranch, $code );
112                         $row_data{ code } = $code;
113                         $row_data{ toBranch } = $toBranch;
114                         $row_data{ fromBranch } = $fromBranch;
115                         $row_data{ isChecked } = $isChecked;
116                         
117                         push( @from_branch_loop, \%row_data );
118                 }
119                 
120                 push( @to_branch_loop, \%row_data );
121         }
122
123         push( @codes_loop, \%row_data );
124 }
125
126
127 $template->param(
128                 branchcount => $branchcount,
129                 codes_loop => \@codes_loop,
130                 branchcode_loop => \@branchcode_loop,
131                 limit_phrase => $limit_phrase,
132                 );
133
134 output_html_with_http_headers $input, $cookie, $template->output;
135