b745136dcb2e03611f0c8413d1c399427e9e75c2
[srvgit] / 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::Branch; 
30 use C4::Circulation qw{ IsBranchTransferAllowed DeleteBranchTransferLimits CreateBranchTransferLimit };
31
32 my $input = new CGI;
33
34 my ($template, $loggedinuser, $cookie)
35     = get_template_and_user({template_name => "admin/branch_transfer_limits.tt",
36                              query => $input,
37                              type => "intranet",
38                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
39                              debug => 1,
40                              });
41
42 my $dbh = C4::Context->dbh;
43 my $branchcode;
44 if((!defined($input->param('branchcode'))) & C4::Context::mybranch() ne '')
45 {
46     $branchcode = C4::Context::mybranch();
47 }
48 else
49 {
50         $branchcode = $input->param('branchcode');
51 }
52
53 # Getting the branches for user selection
54 my $branches = GetBranches();
55 my @branch_loop;
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,
60              );
61     push @branch_loop, \%row;
62 }
63
64
65 # Set the template language for the correct limit type using $limitType
66 my $limitType = C4::Context->preference("BranchTransferLimitsType") || "ccode";
67
68 my @codes;
69 my @branchcodes;
70
71 my $sth;
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');
76 }
77 $sth->execute();
78 while ( my $row = $sth->fetchrow_hashref ) {
79         push( @codes, $row->{ $limitType } );
80 }
81
82 $sth = $dbh->prepare("SELECT branchcode FROM branches");
83 $sth->execute();
84 while ( my $row = $sth->fetchrow_hashref ) {
85         push( @branchcodes, $row->{'branchcode'} );
86 }
87
88 ## If Form Data Passed, Update the Database
89 if ( $input->param('updateLimits') ) {
90     DeleteBranchTransferLimits($branchcode);
91
92
93         foreach my $code ( @codes ) {
94                 foreach my $toBranch ( @branchcodes ) {
95                         my $isSet = not $input->param( $code . "_" . $toBranch);
96                         if ( $isSet ) {
97                             CreateBranchTransferLimit( $toBranch, $branchcode, $code );
98                         }
99                 }
100         }
101 }
102
103 ## Build branchcode loop
104 my @branchcode_loop;
105 foreach my $branchcode ( @branchcodes ) {
106         my %row_data;
107         $row_data{ branchcode } = $branchcode;
108         push ( @branchcode_loop, \%row_data );
109 }
110 my $branchcount = scalar(@branchcode_loop);
111
112 ## Build the default data
113 my @codes_loop;
114 foreach my $code ( @codes ) {
115         my @to_branch_loop;
116         my %row_data;
117         $row_data{ code } = $code;
118         $row_data{ to_branch_loop } = \@to_branch_loop;
119         foreach my $toBranch ( @branchcodes ) {
120                 my %row_data;
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 );
126         }
127
128         push( @codes_loop, \%row_data );
129 }
130
131
132 $template->param(
133                 branchcount => $branchcount,
134                 codes_loop => \@codes_loop,
135                 branch_loop => \@branch_loop,
136                 branchcode_loop => \@branchcode_loop,
137                 branchcode => $branchcode,
138         limitType => $limitType,
139                 );
140
141 output_html_with_http_headers $input, $cookie, $template->output;
142