Code cleaning : Some subs renamed.
[koha.git] / tools / overduerules.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::Database;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Interface::CGI::Output;
29 use HTML::Template;
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 => "tools/overduerules.tmpl",
43                              query => $input,
44                              type => "intranet",
45                              authnotrequired => 0,
46                              flagsrequired => {parameters => 1, tools => 1},
47                               debug => 1,
48                              });
49 my $err=0;
50
51 # save the values entered into tables
52 my %temphash;
53 if ($op eq 'save') {
54         my @names=$input->param();
55         my $sth_search = $dbh->prepare("select count(*) as total from overduerules where branchcode=? and categorycode=?");
56
57         my $sth_insert = $dbh->prepare("insert into overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) values (?,?,?,?,?,?,?,?,?,?,?)");
58         my $sth_update=$dbh->prepare("Update overduerules set delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? where branchcode=? and categorycode=?");
59         my $sth_delete=$dbh->prepare("delete from overduerules where branchcode=? and categorycode=?");
60         foreach my $key (@names){
61                 # ISSUES
62                 if ($key =~ /(.*)([1-3])-(.*)/) {
63                         my $type = $1; # data type
64                         my $num = $2; # From 1 to 3
65                         my $bor = $3; # borrower category
66                         $temphash{$bor}->{"$type$num"}=$input->param("$key") if (($input->param("$key") ne "") or ($input->param("$key")>0));
67                 }
68         }
69         foreach my $bor (keys %temphash){
70                 # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
71                 # Raise error if not true
72                 if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
73                         $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor);
74                         $err=1;
75                 } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
76                         $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor);
77                         $err=1;
78                 } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
79                         $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor);
80                         $err=1;
81                 }elsif ($temphash{$bor}->{delay3} and ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2}
82                         or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
83                         or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
84                                 $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor);
85                                 $err=1;
86                 }
87                 unless ($err){
88                         if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
89                                 or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
90                                 or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
91                                 $sth_search->execute($branch,$bor);
92                                 my $res = $sth_search->fetchrow_hashref();
93                                 if ($res->{'total'}>0) {
94                                         $sth_update->execute(
95                                                 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
96                                                 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
97                                                 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
98                                                 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
99                                                 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
100                                                 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
101                                                 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
102                                                 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
103                                                 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
104                                                 $branch ,$bor
105                                                 );
106                                 } else {
107                                         $sth_insert->execute($branch,$bor,
108                                                 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
109                                                 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
110                                                 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
111                                                 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
112                                                 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
113                                                 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
114                                                 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
115                                                 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
116                                                 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
117                                                 );
118                                 }
119                         }
120                 }
121         }
122         unless ($err) {$template->param(datasaved=>1);}
123 }
124 my $branches = GetBranches;
125 my @branchloop;
126 foreach my $thisbranch (keys %$branches) {
127         my $selected = 1 if $thisbranch eq $branch;
128         my %row =(value => $thisbranch,
129                                 selected => $selected,
130                                 branchname => $branches->{$thisbranch}->{'branchname'},
131                         );
132         push @branchloop, \%row;
133 }
134
135 my ($countletters,$letters) = getletters("circulation");
136
137 my $sth=$dbh->prepare("Select description,categorycode from categories where overduenoticerequired>0 order by description");
138 $sth->execute;
139 my @line_loop;
140 my $toggle= 1;
141 # my $i=0;
142 while (my $data=$sth->fetchrow_hashref){
143         if ( $toggle eq 1 ) {
144                 $toggle = 0;
145         } else {
146                 $toggle = 1;
147         }
148         my %row = ( overduename => $data->{'categorycode'},
149                                 toggle => $toggle,
150                                 line => $data->{'description'}
151                                 );
152         if (%temphash){
153                 for (my $i=1;$i<=3;$i++){
154                         $row{"delay$i"}=$temphash{$data->{'categorycode'}}->{"delay$i"};
155                         $row{"debarred$i"}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
156                         if ($countletters){
157                                 my @letterloop;
158                                 foreach my $thisletter (keys %$letters) {
159                                         my $selected = 1 if $thisletter eq $temphash{$data->{'categorycode'}}->{"letter$i"};
160                                         my %letterrow =(value => $thisletter,
161                                                                 selected => $selected,
162                                                                 lettername => $letters->{$thisletter},
163                                                         );
164                                         push @letterloop, \%letterrow;
165                                 }
166                                 $row{"letterloop$i"}=\@letterloop;
167                         } else {
168                                 $row{"noletter"}=1;
169                                 $row{"letter$i"}=$temphash{$data->{'categorycode'}}->{"letter$i"};
170                         }
171                 }
172         } else {
173         #getting values from table
174                 my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? and categorycode=?");
175                 $sth2->execute($branch,$data->{'categorycode'});
176                 my $dat=$sth2->fetchrow_hashref;
177                 for (my $i=1;$i<=3;$i++){
178                         if ($countletters){
179                                 my @letterloop;
180                                 foreach my $thisletter (keys %$letters) {
181                                         my $selected = 1 if $thisletter eq $dat->{"letter$i"};
182                                         my %letterrow =(value => $thisletter,
183                                                                 selected => $selected,
184                                                                 lettername => $letters->{$thisletter},
185                                                         );
186                                         push @letterloop, \%letterrow;
187                                 }
188                                 $row{"letterloop$i"}=\@letterloop;
189                         } else {
190                                 $row{"noletter"}=1;
191                                 if ($dat->{"letter$i"}){$row{"letter$i"}=$dat->{"letter$i"};}
192                         }
193                         if ($dat->{"delay$i"}){$row{"delay$i"}=$dat->{"delay$i"};}
194                         if ($dat->{"debarred$i"}){$row{"debarred$i"}=$dat->{"debarred$i"};}
195                 }
196                 $sth2->finish;
197         }
198         push @line_loop,\%row;
199 }
200 $sth->finish;
201
202 $template->param(table=> \@line_loop,
203                                                 branchloop => \@branchloop,
204                                                 branch => $branch);
205 output_html_with_http_headers $input, $cookie, $template->output;