branch transfer limits
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 my $branchcode;
43 if((!defined($input->param('branchcode'))) & mybranch() ne '')
44 {
45         $branchcode = mybranch();
46 }
47 else
48 {
49         $branchcode = $input->param('branchcode');
50 }
51
52 my $branchname = GetBranchName($branchcode);
53
54 # Getting the branches for user selection
55 my $branches = GetBranches();
56 my @branch_loop;
57 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
58     my %row =(value => $thisbranch,
59               branchname => $branches->{$thisbranch}->{'branchname'},
60              );
61     push @branch_loop, \%row;
62 }
63
64
65 # Set the template language for the correct limit type
66 my $limit_phrase = 'Collection Code';
67 my $limitType = C4::Context->preference("BranchTransferLimitsType");
68 if ( $limitType eq 'itemtype' ) {
69         $limit_phrase = 'Item Type';
70 }
71
72 my @codes;
73 my @branchcodes;
74
75 my $sth;
76 if ( $limitType eq 'ccode' ) {
77         $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"');
78 } elsif ( $limitType eq 'itemtype' ) {
79         $sth = $dbh->prepare('SELECT itemtype FROM itemtypes');
80 }
81 $sth->execute();
82 while ( my $row = $sth->fetchrow_hashref ) {
83         push( @codes, $row->{ $limitType } );
84 }
85
86 $sth = $dbh->prepare("SELECT branchcode FROM branches");
87 $sth->execute();
88 while ( my $row = $sth->fetchrow_hashref ) {
89         push( @branchcodes, $row->{'branchcode'} );
90 }
91
92 ## If Form Data Passed, Update the Database
93 if ( $input->param('updateLimits') ) {
94         DeleteBranchTransferLimits();
95
96         foreach my $code ( @codes ) {
97                 foreach my $toBranch ( @branchcodes ) {
98                         my $isSet = not $input->param( $code . "_" . $toBranch);
99                         if ( $isSet ) {
100                             CreateBranchTransferLimit( $toBranch, $branchcode, $code );
101                         }
102                 }
103         }
104 }
105
106 ## Build branchcode loop
107 my @branchcode_loop;
108 foreach my $branchcode ( @branchcodes ) {
109         my %row_data;
110         $row_data{ branchcode } = $branchcode;
111         push ( @branchcode_loop, \%row_data );
112 }
113 my $branchcount = scalar(@branchcode_loop);
114
115 ## Build the default data
116 my @codes_loop;
117 foreach my $code ( @codes ) {
118         my @to_branch_loop;
119         my %row_data;
120         $row_data{ code } = $code;
121         $row_data{ to_branch_loop } = \@to_branch_loop;
122         foreach my $toBranch ( @branchcodes ) {
123                 my %row_data;
124                 my $isChecked = IsBranchTransferAllowed( $toBranch, $branchcode, $code );
125                 $row_data{ code }         = $code;
126                 $row_data{ toBranch }     = $toBranch;
127                 $row_data{ isChecked }    = $isChecked; 
128                 $row_data{ toBranchname } = GetBranchName($toBranch);   
129                 push( @to_branch_loop, \%row_data );
130         }
131
132         push( @codes_loop, \%row_data );
133 }
134
135
136 $template->param(
137                 branchcount => $branchcount,
138                 codes_loop => \@codes_loop,
139                 branch_loop => \@branch_loop,
140                 branchcode_loop => \@branchcode_loop,
141                 branchcode => $branchcode,
142                 branchname => $branchname,
143                 limit_phrase => $limit_phrase,
144                 );
145
146 output_html_with_http_headers $input, $cookie, $template->output;
147