bug 2799: followup
[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 my @itemtypes;
44 my @branchcodes;
45
46 my $sth = $dbh->prepare("SELECT itemtype FROM itemtypes");
47 $sth->execute();
48 while ( my $row = $sth->fetchrow_hashref ) {
49         push( @itemtypes, $row->{'itemtype'} );
50 }
51
52 $sth = $dbh->prepare("SELECT branchcode FROM branches");
53 $sth->execute();
54 while ( my $row = $sth->fetchrow_hashref ) {
55         push( @branchcodes, $row->{'branchcode'} );
56 }
57
58 ## If Form Data Passed, Update the Database
59 if ( $input->param('updateLimits') ) {
60     DeleteBranchTransferLimits();
61
62         foreach my $itemtype ( @itemtypes ) {
63                 foreach my $toBranch ( @branchcodes ) {
64                         foreach my $fromBranch ( @branchcodes ) {
65                                 my $isSet = $input->param( $itemtype . "_" . $toBranch . "_" . $fromBranch );
66                                 if ( $isSet ) {
67                                     CreateBranchTransferLimit( $toBranch, $fromBranch, $itemtype );
68                                 }
69                         }
70                 }
71         }
72 }
73
74 ## Build branchcode loop
75 my @branchcode_loop;
76 foreach my $branchcode ( @branchcodes ) {
77         my %row_data;
78         $row_data{ branchcode } = $branchcode;
79         push ( @branchcode_loop, \%row_data );
80 }
81
82 ## Build the default data
83 my @itemtypes_loop;
84 foreach my $itemtype ( @itemtypes ) {
85         my @to_branch_loop;
86         my %row_data;
87         $row_data{ itemtype } = $itemtype;
88         $row_data{ to_branch_loop } = \@to_branch_loop;
89         foreach my $toBranch ( @branchcodes ) {
90                 my @from_branch_loop;
91                 my %row_data;
92                 $row_data{ itemtype } = $itemtype;
93                 $row_data{ toBranch } = $toBranch;
94                 $row_data{ from_branch_loop } = \@from_branch_loop;
95                 
96                 foreach my $fromBranch ( @branchcodes ) {
97                         my %row_data;
98                         my $isChecked = ! IsBranchTransferAllowed( $toBranch, $fromBranch, $itemtype );
99                         $row_data{ itemtype } = $itemtype;
100                         $row_data{ toBranch } = $toBranch;
101                         $row_data{ fromBranch } = $fromBranch;
102                         $row_data{ isChecked } = $isChecked;
103                         
104                         push( @from_branch_loop, \%row_data );
105                 }
106                 
107                 push( @to_branch_loop, \%row_data );
108         }
109
110         push( @itemtypes_loop, \%row_data );
111 }
112
113
114 $template->param(
115                 itemtypes_loop => \@itemtypes_loop,
116                 branchcode_loop => \@branchcode_loop,
117                 );
118
119 output_html_with_http_headers $input, $cookie, $template->output;
120