Patch from Galen Charlton, removing $Id$ $Log$ and $Revision$ from files
[koha.git] / admin / finesrules.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::Auth;
24 use C4::Output;
25
26 use C4::Koha;
27 use C4::Branch; # GetBranches
28
29 my $input = new CGI;
30 my $dbh = C4::Context->dbh;
31
32 my $type=$input->param('type');
33 my $branch = $input->param('branch');
34 $branch="*" unless $branch;
35 my $op = $input->param('op');
36
37 # my $flagsrequired;
38 # $flagsrequired->{circulation}=1;
39 my ($template, $loggedinuser, $cookie)
40     = get_template_and_user({template_name => "admin/finesrules.tmpl",
41                             query => $input,
42                             type => "intranet",
43                             authnotrequired => 0,
44                             flagsrequired => {parameters => 1},
45                             debug => 1,
46                              });
47 # save the values entered
48 if ($op eq 'save') {
49   my @names=$input->param();
50   my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
51
52   my $sth_Finsert = $dbh->prepare("INSERT INTO issuingrules (branchcode,categorycode,itemtype,fine,firstremind,chargeperiod) VALUES (?,?,?,?,?,?)");
53   my $sth_Fupdate=$dbh->prepare("UPDATE issuingrules SET fine=?,firstremind=?,chargeperiod=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
54   my $sth_Fdelete=$dbh->prepare("DELETE FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=? AND issuelength=0");
55
56   foreach my $key (@names){
57     # FINES
58     if ($key =~ /F-(.*)-(.*)\.(.*)/) {
59       my $br = $1; # branch
60       my $bor = $2; # borrower category
61       my $cat = $3; # item type
62       my $data=$input->param($key);
63       my ($fine,$firstremind,$chargeperiod)=split(',',$data);
64       $bor="*" unless ($bor);
65       $cat="*" unless ($cat);
66       $sth_search->execute($br,$bor,$cat);
67       my $res = $sth_search->fetchrow_hashref();
68       if ($res->{total} >0) {
69         $sth_Fupdate->execute($fine,$firstremind,$chargeperiod,$br,$bor,$cat);
70       } else {
71         $sth_Finsert->execute($br,$bor,$cat,$fine,$firstremind,$chargeperiod);
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 }
98
99 my %row = (in_title => "*");
100 push @title_loop, \%row;
101 push @trow3,'*';
102
103 $sth->finish;
104 $sth=$dbh->prepare("Select description,itemtype from itemtypes order by description");
105 $sth->execute;
106 # $i=0;
107 my $toggle= 1;
108 my @row_loop;
109 my @itemtypes;
110 while (my $row=$sth->fetchrow_hashref){
111         push @itemtypes,\$row;
112 }
113
114 foreach my $data (@itemtypes) {
115     my @trow2;
116     my @cell_loop;
117     if ( $toggle eq 1 ) {
118         $toggle = 0;
119     } else {
120         $toggle = 1;
121     }
122     for (my $i=0;$i<=$#trow3;$i++){
123         my $sth2=$dbh->prepare("SELECT * FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
124         $sth2->execute($branch,$trow3[$i],$$data->{'itemtype'});
125         my $dat=$sth2->fetchrow_hashref;
126         $sth2->finish;
127         my $fine=$dat->{'fine'};
128         # remove trailing 0s
129         $fine =~ s/\.*0*$//g;
130         my $finesvalue;
131         $finesvalue= "$fine,$dat->{'firstremind'},$dat->{'chargeperiod'}" if $fine ne '';
132         my %row = (finesname=> "F-$branch-$trow3[$i].$$data->{'itemtype'}",
133                     finesvalue => $finesvalue,
134                     toggle => $toggle,
135                     );
136         push @cell_loop,\%row;
137     }
138     my %row = (categorycode => $$data->{description},
139                 cell =>\@cell_loop,
140                 );
141     push @row_loop, \%row;
142 }
143
144 $sth->finish;
145 $template->param(title => \@title_loop,
146                 row => \@row_loop,
147                 branchloop => \@branchloop,
148                 branch => $branch,
149                 );
150 output_html_with_http_headers $input, $cookie, $template->output;