Bug 19230: Preventing warn when deleting course
[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::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.tt",
35                              query => $input,
36                              type => "intranet",
37                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
38                              debug => 1,
39                              });
40
41 my $dbh = C4::Context->dbh;
42 my $branchcode;
43 if((!defined($input->param('branchcode'))) & C4::Context::mybranch() ne '')
44 {
45     $branchcode = C4::Context::mybranch();
46 }
47 else
48 {
49         $branchcode = $input->param('branchcode');
50 }
51
52 # Set the template language for the correct limit type using $limitType
53 my $limitType = C4::Context->preference("BranchTransferLimitsType") || "ccode";
54
55 my @codes;
56 my @branchcodes;
57
58 my $sth;
59 if ( $limitType eq 'ccode' ) {
60         $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"');
61 } elsif ( $limitType eq 'itemtype' ) {
62         $sth = $dbh->prepare('SELECT itemtype FROM itemtypes');
63 }
64 $sth->execute();
65 while ( my $row = $sth->fetchrow_hashref ) {
66         push( @codes, $row->{ $limitType } );
67 }
68
69 $sth = $dbh->prepare("SELECT branchcode FROM branches");
70 $sth->execute();
71 while ( my $row = $sth->fetchrow_hashref ) {
72         push( @branchcodes, $row->{'branchcode'} );
73 }
74
75 ## If Form Data Passed, Update the Database
76 if ( $input->param('updateLimits') ) {
77     DeleteBranchTransferLimits($branchcode);
78
79
80         foreach my $code ( @codes ) {
81                 foreach my $toBranch ( @branchcodes ) {
82                         my $isSet = not $input->param( $code . "_" . $toBranch);
83                         if ( $isSet ) {
84                             CreateBranchTransferLimit( $toBranch, $branchcode, $code );
85                         }
86                 }
87         }
88 }
89
90 ## Build branchcode loop
91 my @branchcode_loop;
92 foreach my $branchcode ( @branchcodes ) {
93         my %row_data;
94         $row_data{ branchcode } = $branchcode;
95         push ( @branchcode_loop, \%row_data );
96 }
97 my $branchcount = scalar(@branchcode_loop);
98
99 ## Build the default data
100 my @codes_loop;
101 foreach my $code ( @codes ) {
102         my @to_branch_loop;
103         my %row_data;
104         $row_data{ code } = $code;
105         $row_data{ to_branch_loop } = \@to_branch_loop;
106         foreach my $toBranch ( @branchcodes ) {
107                 my %row_data;
108                 my $isChecked = IsBranchTransferAllowed( $toBranch, $branchcode, $code );
109                 $row_data{ code }         = $code;
110                 $row_data{ toBranch }     = $toBranch;
111                 $row_data{ isChecked }    = $isChecked; 
112                 push( @to_branch_loop, \%row_data );
113         }
114
115         push( @codes_loop, \%row_data );
116 }
117
118
119 $template->param(
120                 branchcount => $branchcount,
121                 codes_loop => \@codes_loop,
122                 branchcode_loop => \@branchcode_loop,
123                 branchcode => $branchcode,
124         limitType => $limitType,
125                 );
126
127 output_html_with_http_headers $input, $cookie, $template->output;
128