DBRev 24.06.00.000: Start of a new release cycle
[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 Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth qw( get_template_and_user );
25 use C4::Context;
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Circulation qw( DeleteBranchTransferLimits CreateBranchTransferLimit IsBranchTransferAllowed );
28
29 my $input = CGI->new;
30 my $op = $input->param('op') || q{};
31
32 my ($template, $loggedinuser, $cookie)
33     = get_template_and_user({template_name => "admin/branch_transfer_limits.tt",
34                              query => $input,
35                              type => "intranet",
36                  flagsrequired => {parameters => 'manage_transfers'},
37                              });
38
39 my $dbh = C4::Context->dbh;
40 my $branchcode;
41 if((!defined($input->param('branchcode'))) & C4::Context::mybranch() ne '')
42 {
43     $branchcode = C4::Context::mybranch();
44 }
45 else
46 {
47         $branchcode = $input->param('branchcode');
48 }
49
50 # Set the template language for the correct limit type using $limitType
51 my $limitType = C4::Context->preference("BranchTransferLimitsType") || "ccode";
52
53 my @codes;
54 my @branchcodes;
55
56 if ( $limitType eq 'ccode' ) {
57     @codes = Koha::AuthorisedValues->search({ category => 'CCODE' })->get_column('authorised_value');
58 } elsif ( $limitType eq 'itemtype' ) {
59     @codes = Koha::ItemTypes->search->get_column('itemtype');
60 }
61
62 @branchcodes = Koha::Libraries->search->get_column('branchcode');
63 ## If Form Data Passed, Update the Database
64 if ( $input->param('updateLimits') && $op eq 'cud-update' ) {
65     DeleteBranchTransferLimits($branchcode);
66
67
68         foreach my $code ( @codes ) {
69                 foreach my $toBranch ( @branchcodes ) {
70                         my $isSet = not $input->param( $code . "_" . $toBranch);
71                         if ( $isSet ) {
72                             CreateBranchTransferLimit( $toBranch, $branchcode, $code );
73                         }
74                 }
75         }
76 }
77
78 ## Build branchcode loop
79 my @branchcode_loop;
80 foreach my $branchcode ( @branchcodes ) {
81         my %row_data;
82         $row_data{ branchcode } = $branchcode;
83         push ( @branchcode_loop, \%row_data );
84 }
85 my $branchcount = scalar(@branchcode_loop);
86
87 ## Build the default data
88 my @codes_loop;
89 foreach my $code ( @codes ) {
90         my @to_branch_loop;
91         my %row_data;
92         $row_data{ code } = $code;
93         $row_data{ to_branch_loop } = \@to_branch_loop;
94         foreach my $toBranch ( @branchcodes ) {
95                 my %row_data;
96                 my $isChecked = IsBranchTransferAllowed( $toBranch, $branchcode, $code );
97                 $row_data{ code }         = $code;
98                 $row_data{ toBranch }     = $toBranch;
99                 $row_data{ isChecked }    = $isChecked; 
100                 push( @to_branch_loop, \%row_data );
101         }
102
103         push( @codes_loop, \%row_data );
104 }
105
106
107 $template->param(
108                 branchcount => $branchcount,
109                 codes_loop => \@codes_loop,
110                 branchcode_loop => \@branchcode_loop,
111                 branchcode => $branchcode,
112         limitType => $limitType,
113                 );
114
115 output_html_with_http_headers $input, $cookie, $template->output;
116