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