Finalized XML version for intranet
[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::Interface::CGI::Output;
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, management => 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_Finsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,fine,firstremind,chargeperiod) values (?,?,?,?,?,?)");
52         my $sth_Fupdate=$dbh->prepare("Update issuingrules set fine=?,firstremind=?,chargeperiod=? where branchcode=? and categorycode=? and itemtype=?");
53         my $sth_Fdelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and issuelength=0");
54
55         my $sth_Iinsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,maxissueqty,issuelength) values (?,?,?,?,?)");
56         my $sth_Iupdate=$dbh->prepare("Update issuingrules set maxissueqty=?, issuelength=? where branchcode=? and categorycode=? and itemtype=?");
57         my $sth_Idelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and fine=0");
58         foreach my $key (@names){
59                 # ISSUES
60                 if ($key =~ /I-(.*)-(.*)\.(.*)/) {
61                         my $br = $1; # branch
62                         my $bor = $2; # borrower category
63                         my $cat = $3; # item type
64                         my $data=$input->param($key);
65                         my ($issuelength,$maxissueqty)=split(',',$data);
66 #                       if ($maxissueqty >0) {
67                                 $sth_search->execute($br,$bor,$cat);
68                                 my $res = $sth_search->fetchrow_hashref();
69                                 if ($res->{total}) {
70                                         $sth_Iupdate->execute($maxissueqty,$issuelength,$br,$bor,$cat);
71                                 } else {
72                                         $sth_Iinsert->execute($br,$bor,$cat,$maxissueqty,$issuelength);
73                                 }
74 #                       } else {
75 #                               $sth_Idelete->execute($br,$bor,$cat);
76 #                       }
77                 }
78                 # FINES
79                 if ($key =~ /F-(.*)-(.*)\.(.*)/) {
80                         my $br = $1; # branch
81                         my $bor = $2; # borrower category
82                         my $cat = $3; # item type
83                         my $data=$input->param($key);
84                         my ($fine,$firstremind,$chargeperiod)=split(',',$data);
85 #                       if ($fine >0) {
86                                 $sth_search->execute($br,$bor,$cat);
87                                 my $res = $sth_search->fetchrow_hashref();
88                                 if ($res->{total}) {
89                                         $sth_Fupdate->execute($fine,$firstremind,$chargeperiod,$br,$bor,$cat);
90                                 } else {
91                                         $sth_Finsert->execute($br,$bor,$cat,$fine,$firstremind,$chargeperiod);
92                                 }
93 #                       } else {
94 #                               $sth_Fdelete->execute($br,$bor,$cat);
95 #                       }
96                 }
97         }
98
99 }
100 my $branches = GetBranches;
101 my @branchloop;
102 foreach my $thisbranch (keys %$branches) {
103         my $selected = 1 if $thisbranch eq $branch;
104         my %row =(value => $thisbranch,
105                                 selected => $selected,
106                                 branchname => $branches->{$thisbranch}->{'branchname'},
107                         );
108         push @branchloop, \%row;
109 }
110
111 my $sth=$dbh->prepare("Select description,categorycode from categories order by description");
112 $sth->execute;
113  my @trow3;
114 my @title_loop;
115 # my $i=0;
116 while (my $data=$sth->fetchrow_hashref){
117         my %row = (in_title => $data->{'description'});
118         push @title_loop,\%row;
119         push @trow3,$data->{'categorycode'};
120 #       $i++;
121 }
122
123 my %row = (in_title => "*");
124 push @title_loop, \%row;
125 push @trow3,'*';
126
127 $sth->finish;
128 $sth=$dbh->prepare("Select description,itemtype from itemtypes order by description");
129 $sth->execute;
130 # $i=0;
131 my $toggle= 1;
132 my @row_loop;
133 my @itemtypes;
134 while (my $row=$sth->fetchrow_hashref){
135         push @itemtypes,\$row;
136 }
137 my $line;
138 $line->{itemtype} = "*";
139 $line->{description} = "*";
140 push @itemtypes,\$line;
141
142 foreach my $data (@itemtypes) {
143         my @trow2;
144         my @cell_loop;
145         if ( $toggle eq 1 ) {
146                 $toggle = 0;
147         } else {
148                 $toggle = 1;
149         }
150         for (my $i=0;$i<=$#trow3;$i++){
151                 my $sth2=$dbh->prepare("select * from issuingrules where branchcode=? and categorycode=? and itemtype=?");
152                 $sth2->execute($branch,$trow3[$i],$$data->{'itemtype'});
153                 my $dat=$sth2->fetchrow_hashref;
154                 $sth2->finish;
155                 my $fine=$dat->{'fine'}+0;
156                 my $maxissueqty = $dat->{'maxissueqty'}+0;
157                 my $issuelength = $dat->{'issuelength'}+0;
158                 my $finesvalue;
159                 $finesvalue= "$fine,$dat->{'firstremind'},$dat->{'chargeperiod'}" if $fine+$dat->{'firstremind'}+$dat->{'chargeperiod'}>0;
160                 my $issuingvalue;
161 #               if ($maxissueqty>0) {
162                     $issuingvalue = "$issuelength,$maxissueqty" if $issuelength+$maxissueqty>0;
163 #               }
164 #               else {          
165 #                   $issuingvalue = "$issuelength, 5";
166 #                   $maxissueqty = 5;
167 #               }
168                 my %row = (finesname=> "F-$branch-$trow3[$i].$$data->{'itemtype'}",
169                                         finesvalue => $finesvalue,
170                                         issuingname => "I-$branch-$trow3[$i].$$data->{itemtype}",
171                                         issuingvalue => $issuingvalue,
172                                         toggle => $toggle,
173                                         );
174                 push @cell_loop,\%row;
175         }
176         my %row = (categorycode => $$data->{description},
177                                         cell =>\@cell_loop);
178         push @row_loop, \%row;
179 }
180
181 $sth->finish;
182 $template->param(title => \@title_loop,
183                                                 row => \@row_loop,
184                                                 branchloop => \@branchloop,
185                                                 branch => $branch);
186 output_html_with_http_headers $input, $cookie, $template->output;