removing all useless %env / $env
[koha.git] / admin / issuingrules.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use CGI;
22 use C4::Context;
23 use C4::Output;
24
25 use C4::Auth;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Interface::CGI::Output;
29 use C4::Branch; # GetBranches
30
31 my $input = new CGI;
32 my $dbh = C4::Context->dbh;
33
34 my $type=$input->param('type');
35 my $branch = $input->param('branch');
36 $branch="" unless $branch;
37 my $op = $input->param('op');
38
39 # my $flagsrequired;
40 # $flagsrequired->{circulation}=1;
41 my ($template, $loggedinuser, $cookie)
42     = get_template_and_user({template_name => "admin/issuingrules.tmpl",
43                              query => $input,
44                              type => "intranet",
45                              authnotrequired => 0,
46                              flagsrequired => {parameters => 1},
47                               debug => 1,
48                              });
49 # save the values entered
50 if ($op eq 'save') {
51         my @names=$input->param();
52         my $sth_search = $dbh->prepare("select count(*) as total from issuingrules where branchcode=? and categorycode=? and itemtype=?");
53
54         my $sth_Iinsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,maxissueqty,issuelength,rentaldiscount) values (?,?,?,?,?,?)");
55         my $sth_Iupdate=$dbh->prepare("Update issuingrules set maxissueqty=?, issuelength=?, rentaldiscount=? where branchcode=? and categorycode=? and itemtype=?");
56         my $sth_Idelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and fine=0");
57         foreach my $key (@names){
58                 # ISSUES
59                 if ($key =~ /I-(.*)-(.*)\.(.*)/) {
60                         my $br = $1; # branch
61                         my $bor = $2; # borrower category
62                         my $cat = $3; # item type
63                         my $data=$input->param($key);
64                         my ($issuelength,$maxissueqty,$rentaldiscount)=split(',',$data);
65 #                       if ($maxissueqty >0) {
66                                 $sth_search->execute($br,$bor,$cat);
67                                 my $res = $sth_search->fetchrow_hashref();
68                                 if ($res->{total}) {
69                                         $sth_Iupdate->execute($maxissueqty,$issuelength,$rentaldiscount,$br,$bor,$cat);
70                                 } else {
71                                         $sth_Iinsert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$rentaldiscount);
72                                 }
73 #                       } else {
74 #                               $sth_Idelete->execute($br,$bor,$cat);
75 #                       }
76                 }
77         }
78
79 }
80 my $branches = GetBranches;
81 my @branchloop;
82 foreach my $thisbranch (keys %$branches) {
83         my $selected = 1 if $thisbranch eq $branch;
84         my %row =(value => $thisbranch,
85                                 selected => $selected,
86                                 branchname => $branches->{$thisbranch}->{'branchname'},
87                         );
88         push @branchloop, \%row;
89 }
90
91 my $sth=$dbh->prepare("Select description,categorycode from categories order by description");
92 $sth->execute;
93 my @trow3;
94 my @title_loop;
95 # my $i=0;
96 while (my $data=$sth->fetchrow_hashref){
97         my %row = (in_title => $data->{'description'});
98         push @title_loop,\%row;
99         push @trow3,$data->{'categorycode'};
100 #       $i++;
101 }
102
103 my %row = (in_title => "*");
104 push @title_loop, \%row;
105 push @trow3,'*';
106
107 $sth->finish;
108 $sth=$dbh->prepare("Select description,itemtype from itemtypes order by description");
109 $sth->execute;
110 # $i=0;
111 my $toggle= 1;
112 my @row_loop;
113 my @itemtypes;
114 while (my $row=$sth->fetchrow_hashref){
115         push @itemtypes,\$row;
116 }
117 my $line;
118 $line->{itemtype} = "*";
119 $line->{description} = "*";
120 push @itemtypes,\$line;
121
122 foreach my $data (@itemtypes) {
123         my @trow2;
124         my @cell_loop;
125         if ( $toggle eq 1 ) {
126                 $toggle = 0;
127         } else {
128                 $toggle = 1;
129         }
130         for (my $i=0;$i<=$#trow3;$i++){
131                 my $sth2=$dbh->prepare("select * from issuingrules where branchcode=? and categorycode=? and itemtype=?");
132                 $sth2->execute($branch,$trow3[$i],$$data->{'itemtype'});
133                 my $dat=$sth2->fetchrow_hashref;
134                 $sth2->finish;
135                 my $fine=$dat->{'fine'}+0;
136                 my $maxissueqty = $dat->{'maxissueqty'}+0;
137                 my $issuelength = $dat->{'issuelength'}+0;
138                 my $rentaldiscount = $dat->{'rentaldiscount'}+0;
139                 my $finesvalue;
140                 $finesvalue= "$fine,$dat->{'firstremind'},$dat->{'chargeperiod'}" if $fine+$dat->{'firstremind'}+$dat->{'chargeperiod'}>0;
141                 my $issuingvalue;
142                 $issuingvalue = "$issuelength,$maxissueqty,$rentaldiscount" if $issuelength+$maxissueqty>0;
143                 my %row = (     issuingname => "I-$branch-$trow3[$i].$$data->{itemtype}",
144                                 issuingvalue => $issuingvalue,
145                                 toggle => $toggle,
146                                 );
147                 push @cell_loop,\%row;
148         }
149         my %row = (categorycode => $$data->{description},
150                                         cell =>\@cell_loop);
151         push @row_loop, \%row;
152 }
153
154 $sth->finish;
155 $template->param(title => \@title_loop,
156                                                 row => \@row_loop,
157                                                 branchloop => \@branchloop,
158                                                 branch => $branch,
159                                                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
160                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
161                 IntranetNav => C4::Context->preference("IntranetNav"),
162                                                 );
163 output_html_with_http_headers $input, $cookie, $template->output;