Added 'Branch Transfer Limits' Feature requested by Geauga County Library System.
[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 CGI;
22 use C4::Auth;
23 use C4::Context;
24 use C4::Output;
25 use C4::Koha;
26 use C4::Branch; 
27
28 my $input = new CGI;
29
30 my ($template, $loggedinuser, $cookie)
31     = get_template_and_user({template_name => "admin/branch_transfer_limits.tmpl",
32                              query => $input,
33                              type => "intranet",
34                              flagsrequired => {borrowers => 1},
35                              debug => 1,
36                              });
37
38 my $dbh = C4::Context->dbh;
39
40 my @itemtypes;
41 my @branchcodes;
42
43 my $sth = $dbh->prepare("SELECT itemtype FROM itemtypes");
44 $sth->execute();
45 while ( my $row = $sth->fetchrow_hashref ) {
46         push( @itemtypes, $row->{'itemtype'} );
47 }
48
49 $sth = $dbh->prepare("SELECT branchcode FROM branches");
50 $sth->execute();
51 while ( my $row = $sth->fetchrow_hashref ) {
52         push( @branchcodes, $row->{'branchcode'} );
53 }
54
55 ## If Form Data Passed, Update the Database
56 if ( $input->param('updateLimits') ) {
57     DeleteBranchTransferLimits();
58
59         foreach my $itemtype ( @itemtypes ) {
60                 foreach my $toBranch ( @branchcodes ) {
61                         foreach my $fromBranch ( @branchcodes ) {
62                                 my $isSet = $input->param( $itemtype . "_" . $toBranch . "_" . $fromBranch );
63                                 if ( $isSet ) {
64                                     CreateBranchTransferLimit( $toBranch, $fromBranch, $itemtype );
65                                 }
66                         }
67                 }
68         }
69 }
70
71 ## Build branchcode loop
72 my @branchcode_loop;
73 foreach my $branchcode ( @branchcodes ) {
74         my %row_data;
75         $row_data{ branchcode } = $branchcode;
76         push ( @branchcode_loop, \%row_data );
77 }
78
79 ## Build the default data
80 my @loop0;
81 foreach my $itemtype ( @itemtypes ) {
82         my @loop1;
83         my %row_data;
84         $row_data{ itemtype } = $itemtype;
85         $row_data{ loop1 } = \@loop1;
86         foreach my $toBranch ( @branchcodes ) {
87                 my @loop2;
88                 my %row_data;
89                 $row_data{ itemtype } = $itemtype;
90                 $row_data{ toBranch } = $toBranch;
91                 $row_data{ loop2 } = \@loop2;
92                 
93                 foreach my $fromBranch ( @branchcodes ) {
94                         my %row_data;
95                         my $isChecked = ! IsBranchTransferAllowed( $toBranch, $fromBranch, $itemtype );
96                         $row_data{ itemtype } = $itemtype;
97                         $row_data{ toBranch } = $toBranch;
98                         $row_data{ fromBranch } = $fromBranch;
99                         $row_data{ isChecked } = $isChecked;
100                         
101                         push( @loop2, \%row_data );
102                 }
103                 
104                 push( @loop1, \%row_data );
105         }
106
107         push( @loop0, \%row_data );     
108 }
109
110
111 $template->param(
112                 loop0 => \@loop0,
113                 branchcode_loop => \@branchcode_loop,
114                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
115                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
116                 IntranetNav => C4::Context->preference("IntranetNav"),
117                 );
118
119 output_html_with_http_headers $input, $cookie, $template->output;
120