fix for 2322: Failure to reach amazon.com to retrieve enhanced content causes fatal...
[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::Auth;
25 use C4::Koha;
26 use C4::Branch; # GetBranches
27 use C4::Letters;
28 use C4::Members;
29
30 my $input = new CGI;
31 my $dbh = C4::Context->dbh;
32
33 my $type=$input->param('type');
34 my $branch = $input->param('branch');
35 $branch="" unless $branch;
36 my $op = $input->param('op');
37
38 # my $flagsrequired;
39 # $flagsrequired->{circulation}=1;
40 my ($template, $loggedinuser, $cookie)
41     = get_template_and_user({template_name => "tools/overduerules.tmpl",
42                             query => $input,
43                             type => "intranet",
44                             authnotrequired => 0,
45                             flagsrequired => {parameters => 1, tools => 'edit_notice_status_triggers'},
46                             debug => 1,
47                             });
48 my $err=0;
49
50 # save the values entered into tables
51 my %temphash;
52 my $input_saved = 0;
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         # get category name if we need it for an error message
71         my $bor_category = GetBorrowercategory($bor);
72         my $bor_category_name = defined($bor_category) ? $bor_category->{description} : $bor;
73
74         # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
75         # Raise error if not true
76         if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
77             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
78             $err=1;
79         } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
80             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
81             $err=1;
82         } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
83             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
84             $err=1;
85         } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
86             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
87             $err=1;
88         } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
89             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
90             $err=1;
91         } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
92             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
93             $err=1;
94         }elsif ($temphash{$bor}->{delay3} and
95                 ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
96                 or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
97                     $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
98                         $err=1;
99         }
100         unless ($err){
101             if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
102                 or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
103                 or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
104                     $sth_search->execute($branch,$bor);
105                     my $res = $sth_search->fetchrow_hashref();
106                     if ($res->{'total'}>0) {
107                         $sth_update->execute(
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                             $branch ,$bor
118                             );
119                     } else {
120                         $sth_insert->execute($branch,$bor,
121                             ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
122                             ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
123                             ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
124                             ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
125                             ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
126                             ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
127                             ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
128                             ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
129                             ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
130                             );
131                     }
132                 }
133         }
134     }
135     unless ($err) {
136         $template->param(datasaved=>1);
137         $input_saved = 1;
138     }
139 }
140 my $branches = GetBranches();
141 my @branchloop;
142 foreach my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
143         my $selected = 1 if $thisbranch eq $branch;
144         my %row =(value => $thisbranch,
145                                 selected => $selected,
146                                 branchname => $branches->{$thisbranch}->{'branchname'},
147                         );
148         push @branchloop, \%row;
149 }
150
151 my $letters = GetLetters("circulation");
152
153 my $countletters = scalar $letters;
154
155 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories WHERE overduenoticerequired>0 ORDER BY description");
156 $sth->execute;
157 my @line_loop;
158 my $toggle= 1;
159 # my $i=0;
160 while (my $data=$sth->fetchrow_hashref){
161     if ( $toggle eq 1 ) {
162         $toggle = 0;
163     } else {
164         $toggle = 1;
165     }
166     my %row = ( overduename => $data->{'categorycode'},
167                 toggle => $toggle,
168                 line => $data->{'description'}
169                 );
170     if (%temphash and not $input_saved){
171         # if we managed to save the form submission, don't
172         # reuse %temphash, but take the values from the
173         # database - this makes it easier to identify
174         # bugs where the form submission was not correctly saved
175         for (my $i=1;$i<=3;$i++){
176             $row{"delay$i"}=$temphash{$data->{'categorycode'}}->{"delay$i"};
177             $row{"debarred$i"}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
178             if ($countletters){
179                 my @letterloop;
180                 foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
181                     my $selected = 1 if $thisletter eq $temphash{$data->{'categorycode'}}->{"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                 $row{"letter$i"}=$temphash{$data->{'categorycode'}}->{"letter$i"};
192             }
193         }
194     } else {
195     #getting values from table
196         my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
197         $sth2->execute($branch,$data->{'categorycode'});
198         my $dat=$sth2->fetchrow_hashref;
199         for (my $i=1;$i<=3;$i++){
200             if ($countletters){
201                 my @letterloop;
202                 foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
203                     my $selected = 1 if $thisletter eq $dat->{"letter$i"};
204                     my %letterrow =(value => $thisletter,
205                                     selected => $selected,
206                                     lettername => $letters->{$thisletter},
207                                     );
208                     push @letterloop, \%letterrow;
209                 }
210                 $row{"letterloop$i"}=\@letterloop;
211             } else {
212                 $row{"noletter"}=1;
213                 if ($dat->{"letter$i"}){$row{"letter$i"}=$dat->{"letter$i"};}
214             }
215             if ($dat->{"delay$i"}){$row{"delay$i"}=$dat->{"delay$i"};}
216             if ($dat->{"debarred$i"}){$row{"debarred$i"}=$dat->{"debarred$i"};}
217         }
218         $sth2->finish;
219     }
220     push @line_loop,\%row;
221 }
222 $sth->finish;
223
224 $template->param(table=> \@line_loop,
225                 branchloop => \@branchloop,
226                 branch => $branch);
227 output_html_with_http_headers $input, $cookie, $template->output;