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