Adding Finerules script.
[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::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/finesrules.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_Finsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,fine,firstremind,chargeperiod) values (?,?,?,?,?,?)");
55   my $sth_Fupdate=$dbh->prepare("Update issuingrules set fine=?,firstremind=?,chargeperiod=? where branchcode=? and categorycode=? and itemtype=?");
56   my $sth_Fdelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and issuelength=0");
57
58   foreach my $key (@names){
59     # FINES
60     if ($key =~ /F-(.*)-(.*)\.(.*)/) {
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 ($fine,$firstremind,$chargeperiod)=split(',',$data);
66       warn "$br $bor $cat $fine $firstremind $chargeperiod";
67 #                       if ($fine >0) {
68 #       $br="*" unless ($br);
69       $bor="*" unless ($bor);
70       $cat="*" unless ($cat);
71       $sth_search->execute($br,$bor,$cat);
72       my $res = $sth_search->fetchrow_hashref();
73       if ($res->{total}) {
74               $sth_Fupdate->execute($fine,$firstremind,$chargeperiod,$br,$bor,$cat);
75       } else {
76               $sth_Finsert->execute($br,$bor,$cat,$fine,$firstremind,$chargeperiod);
77       }
78     }
79   }
80
81 }
82 my $branches = GetBranches;
83 my @branchloop;
84 foreach my $thisbranch (keys %$branches) {
85         my $selected = 1 if $thisbranch eq $branch;
86         my %row =(value => $thisbranch,
87                                 selected => $selected,
88                                 branchname => $branches->{$thisbranch}->{'branchname'},
89                         );
90         push @branchloop, \%row;
91 }
92
93 my $sth=$dbh->prepare("Select description,categorycode from categories order by description");
94 $sth->execute;
95 my @trow3;
96 my @title_loop;
97 # my $i=0;
98 while (my $data=$sth->fetchrow_hashref){
99         my %row = (in_title => $data->{'description'});
100         push @title_loop,\%row;
101         push @trow3,$data->{'categorycode'};
102 #       $i++;
103 }
104
105 my %row = (in_title => "*");
106 push @title_loop, \%row;
107 push @trow3,'*';
108
109 $sth->finish;
110 $sth=$dbh->prepare("Select description,itemtype from itemtypes order by description");
111 $sth->execute;
112 # $i=0;
113 my $toggle= 1;
114 my @row_loop;
115 my @itemtypes;
116 while (my $row=$sth->fetchrow_hashref){
117         push @itemtypes,\$row;
118 }
119 my $line;
120 $line->{itemtype} = "*";
121 $line->{description} = "*";
122 push @itemtypes,\$line;
123
124 foreach my $data (@itemtypes) {
125         my @trow2;
126         my @cell_loop;
127         if ( $toggle eq 1 ) {
128                 $toggle = 0;
129         } else {
130                 $toggle = 1;
131         }
132         for (my $i=0;$i<=$#trow3;$i++){
133                 my $sth2=$dbh->prepare("select * from issuingrules where branchcode=? and categorycode=? and itemtype=?");
134                 $sth2->execute($branch,$trow3[$i],$$data->{'itemtype'});
135                 my $dat=$sth2->fetchrow_hashref;
136                 $sth2->finish;
137                 my $fine=$dat->{'fine'}+0;
138                 my $finesvalue;
139                 $finesvalue= "$fine,$dat->{'firstremind'},$dat->{'chargeperiod'}" if $fine+$dat->{'firstremind'}+$dat->{'chargeperiod'}>0;
140                 my %row = (finesname=> "F-$branch-$trow3[$i].$$data->{'itemtype'}",
141                                         finesvalue => $finesvalue,
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;