Reverting to previous version, kohastructure.sql file didn't work for me, I got an...
[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 use C4::Auth;
25 use C4::Koha;
26 use C4::Branch; # GetBranches
27
28 my $input = new CGI;
29 my $dbh = C4::Context->dbh;
30
31 my $type=$input->param('type');
32 my $branch = $input->param('branch');
33 $branch="" unless $branch;
34 my $op = $input->param('op');
35
36 # my $flagsrequired;
37 # $flagsrequired->{circulation}=1;
38 my ($template, $loggedinuser, $cookie)
39     = get_template_and_user({template_name => "admin/issuingrules.tmpl",
40                              query => $input,
41                              type => "intranet",
42                              authnotrequired => 0,
43                              flagsrequired => {parameters => 1},
44                               debug => 1,
45                              });
46 # save the values entered
47 if ($op eq 'save') {
48         my @names=$input->param();
49         my $sth_search = $dbh->prepare("select count(*) as total from issuingrules where branchcode=? and categorycode=? and itemtype=?");
50
51         my $sth_Iinsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,maxissueqty,issuelength,rentaldiscount) values (?,?,?,?,?,?)");
52         my $sth_Iupdate=$dbh->prepare("Update issuingrules set maxissueqty=?, issuelength=?, rentaldiscount=? where branchcode=? and categorycode=? and itemtype=?");
53         my $sth_Idelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and fine=0");
54         foreach my $key (@names){
55                 # ISSUES
56                 if ($key =~ /I-(.*)-(.*)\.(.*)/) {
57                         my $br = $1; # branch
58                         my $bor = $2; # borrower category
59                         my $cat = $3; # item type
60                         my $data=$input->param($key);
61                         my ($issuelength,$maxissueqty,$rentaldiscount)=split(',',$data);
62 #                       if ($maxissueqty >0) {
63                                 $sth_search->execute($br,$bor,$cat);
64                                 my $res = $sth_search->fetchrow_hashref();
65                                 if ($res->{total}) {
66                                         $sth_Iupdate->execute($maxissueqty,$issuelength,$rentaldiscount,$br,$bor,$cat);
67                                 } else {
68                                         $sth_Iinsert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$rentaldiscount);
69                                 }
70 #                       } else {
71 #                               $sth_Idelete->execute($br,$bor,$cat);
72 #                       }
73                 }
74         }
75
76 }
77 my $branches = GetBranches;
78 my @branchloop;
79 foreach my $thisbranch (keys %$branches) {
80         my $selected = 1 if $thisbranch eq $branch;
81         my %row =(value => $thisbranch,
82                                 selected => $selected,
83                                 branchname => $branches->{$thisbranch}->{'branchname'},
84                         );
85         push @branchloop, \%row;
86 }
87
88 my $sth=$dbh->prepare("Select description,categorycode from categories order by description");
89 $sth->execute;
90 my @trow3;
91 my @title_loop;
92 # my $i=0;
93 while (my $data=$sth->fetchrow_hashref){
94         my %row = (in_title => $data->{'description'});
95         push @title_loop,\%row;
96         push @trow3,$data->{'categorycode'};
97 #       $i++;
98 }
99
100 my %row = (in_title => "*");
101 push @title_loop, \%row;
102 push @trow3,'*';
103
104 $sth->finish;
105 $sth=$dbh->prepare("Select description,itemtype from itemtypes order by description");
106 $sth->execute;
107 # $i=0;
108 my $toggle= 1;
109 my @row_loop;
110 my @itemtypes;
111 while (my $row=$sth->fetchrow_hashref){
112         push @itemtypes,\$row;
113 }
114 my $line;
115 $line->{itemtype} = "*";
116 $line->{description} = "*";
117 push @itemtypes,\$line;
118
119 foreach my $data (@itemtypes) {
120         my @trow2;
121         my @cell_loop;
122         if ( $toggle eq 1 ) {
123                 $toggle = 0;
124         } else {
125                 $toggle = 1;
126         }
127         for (my $i=0;$i<=$#trow3;$i++){
128                 my $sth2=$dbh->prepare("select * from issuingrules where branchcode=? and categorycode=? and itemtype=?");
129                 $sth2->execute($branch,$trow3[$i],$$data->{'itemtype'});
130                 my $dat=$sth2->fetchrow_hashref;
131                 $sth2->finish;
132                 my $fine=$dat->{'fine'}+0;
133                 my $maxissueqty = $dat->{'maxissueqty'}+0;
134                 my $issuelength = $dat->{'issuelength'}+0;
135                 my $rentaldiscount = $dat->{'rentaldiscount'}+0;
136                 my $finesvalue;
137                 $finesvalue= "$fine,$dat->{'firstremind'},$dat->{'chargeperiod'}" if $fine+$dat->{'firstremind'}+$dat->{'chargeperiod'}>0;
138                 my $issuingvalue;
139                 $issuingvalue = "$issuelength,$maxissueqty,$rentaldiscount" if $issuelength+$maxissueqty>0;
140                 my %row = (     issuingname => "I-$branch-$trow3[$i].$$data->{itemtype}",
141                                 issuingvalue => $issuingvalue,
142                                 toggle => $toggle,
143                                 );
144                 push @cell_loop,\%row;
145         }
146         my %row = (categorycode => $$data->{description},
147                                         cell =>\@cell_loop);
148         push @row_loop, \%row;
149 }
150
151 $sth->finish;
152 $template->param(title => \@title_loop,
153                                                 row => \@row_loop,
154                                                 branchloop => \@branchloop,
155                                                 branch => $branch,
156                                                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
157                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
158                 IntranetNav => C4::Context->preference("IntranetNav"),
159                                                 );
160 output_html_with_http_headers $input, $cookie, $template->output;