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