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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 use C4::Circulation qw{ IsBranchTransferAllowed DeleteBranchTransferLimits CreateBranchTransferLimit };
34 my ($template, $loggedinuser, $cookie)
35 = get_template_and_user({template_name => "admin/branch_transfer_limits.tmpl",
38 flagsrequired => {borrowers => 1},
42 my $dbh = C4::Context->dbh;
44 if((!defined($input->param('branchcode'))) & mybranch() ne '')
46 $branchcode = mybranch();
50 $branchcode = $input->param('branchcode');
53 my $branchname = GetBranchName($branchcode);
55 # Getting the branches for user selection
56 my $branches = GetBranches();
58 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
59 my %row =(value => $thisbranch,
60 branchname => $branches->{$thisbranch}->{'branchname'},
62 push @branch_loop, \%row;
66 # Set the template language for the correct limit type
67 my $limit_phrase = 'Collection Code';
68 my $limitType = C4::Context->preference("BranchTransferLimitsType");
69 if ( $limitType eq 'itemtype' ) {
70 $limit_phrase = 'Item Type';
77 if ( $limitType eq 'ccode' ) {
78 $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"');
79 } elsif ( $limitType eq 'itemtype' ) {
80 $sth = $dbh->prepare('SELECT itemtype FROM itemtypes');
83 while ( my $row = $sth->fetchrow_hashref ) {
84 push( @codes, $row->{ $limitType } );
87 $sth = $dbh->prepare("SELECT branchcode FROM branches");
89 while ( my $row = $sth->fetchrow_hashref ) {
90 push( @branchcodes, $row->{'branchcode'} );
93 ## If Form Data Passed, Update the Database
94 if ( $input->param('updateLimits') ) {
95 DeleteBranchTransferLimits();
97 foreach my $code ( @codes ) {
98 foreach my $toBranch ( @branchcodes ) {
99 my $isSet = not $input->param( $code . "_" . $toBranch);
101 CreateBranchTransferLimit( $toBranch, $branchcode, $code );
107 ## Build branchcode loop
109 foreach my $branchcode ( @branchcodes ) {
111 $row_data{ branchcode } = $branchcode;
112 push ( @branchcode_loop, \%row_data );
114 my $branchcount = scalar(@branchcode_loop);
116 ## Build the default data
118 foreach my $code ( @codes ) {
121 $row_data{ code } = $code;
122 $row_data{ to_branch_loop } = \@to_branch_loop;
123 foreach my $toBranch ( @branchcodes ) {
125 my $isChecked = IsBranchTransferAllowed( $toBranch, $branchcode, $code );
126 $row_data{ code } = $code;
127 $row_data{ toBranch } = $toBranch;
128 $row_data{ isChecked } = $isChecked;
129 $row_data{ toBranchname } = GetBranchName($toBranch);
130 push( @to_branch_loop, \%row_data );
133 push( @codes_loop, \%row_data );
138 branchcount => $branchcount,
139 codes_loop => \@codes_loop,
140 branch_loop => \@branch_loop,
141 branchcode_loop => \@branchcode_loop,
142 branchcode => $branchcode,
143 branchname => $branchname,
144 limit_phrase => $limit_phrase,
147 output_html_with_http_headers $input, $cookie, $template->output;