3 # Copyright 2000-2002 Katipo Communications
4 # copyright 2010 BibLibre
6 # This file is part of Koha.
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.
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.
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>.
30 use C4::Circulation qw{ IsBranchTransferAllowed DeleteBranchTransferLimits CreateBranchTransferLimit };
34 my ($template, $loggedinuser, $cookie)
35 = get_template_and_user({template_name => "admin/branch_transfer_limits.tt",
38 flagsrequired => {parameters => 'parameters_remaining_permissions'},
42 my $dbh = C4::Context->dbh;
44 if((!defined($input->param('branchcode'))) & C4::Context::mybranch() ne '')
46 $branchcode = C4::Context::mybranch();
50 $branchcode = $input->param('branchcode');
53 # Getting the branches for user selection
54 my $branches = GetBranches();
56 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
57 my %row =(value => $thisbranch,
58 branchname => $branches->{$thisbranch}->{'branchname'},
59 selected => $thisbranch eq $branchcode ? 1 : 0,
61 push @branch_loop, \%row;
65 # Set the template language for the correct limit type using $limitType
66 my $limitType = C4::Context->preference("BranchTransferLimitsType") || "ccode";
72 if ( $limitType eq 'ccode' ) {
73 $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"');
74 } elsif ( $limitType eq 'itemtype' ) {
75 $sth = $dbh->prepare('SELECT itemtype FROM itemtypes');
78 while ( my $row = $sth->fetchrow_hashref ) {
79 push( @codes, $row->{ $limitType } );
82 $sth = $dbh->prepare("SELECT branchcode FROM branches");
84 while ( my $row = $sth->fetchrow_hashref ) {
85 push( @branchcodes, $row->{'branchcode'} );
88 ## If Form Data Passed, Update the Database
89 if ( $input->param('updateLimits') ) {
90 DeleteBranchTransferLimits($branchcode);
93 foreach my $code ( @codes ) {
94 foreach my $toBranch ( @branchcodes ) {
95 my $isSet = not $input->param( $code . "_" . $toBranch);
97 CreateBranchTransferLimit( $toBranch, $branchcode, $code );
103 ## Build branchcode loop
105 foreach my $branchcode ( @branchcodes ) {
107 $row_data{ branchcode } = $branchcode;
108 push ( @branchcode_loop, \%row_data );
110 my $branchcount = scalar(@branchcode_loop);
112 ## Build the default data
114 foreach my $code ( @codes ) {
117 $row_data{ code } = $code;
118 $row_data{ to_branch_loop } = \@to_branch_loop;
119 foreach my $toBranch ( @branchcodes ) {
121 my $isChecked = IsBranchTransferAllowed( $toBranch, $branchcode, $code );
122 $row_data{ code } = $code;
123 $row_data{ toBranch } = $toBranch;
124 $row_data{ isChecked } = $isChecked;
125 push( @to_branch_loop, \%row_data );
128 push( @codes_loop, \%row_data );
133 branchcount => $branchcount,
134 codes_loop => \@codes_loop,
135 branch_loop => \@branch_loop,
136 branchcode_loop => \@branchcode_loop,
137 branchcode => $branchcode,
138 limitType => $limitType,
141 output_html_with_http_headers $input, $cookie, $template->output;