Removing hard-coded $ symbols from the template, addressing Bug 2547
[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 @categories = @{$dbh->selectall_arrayref(
34     'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0',
35     { Slice => {} }
36 )};
37 my @category_codes  = map { $_->{categorycode} } @categories;
38 my @rule_params     = qw(delay letter debarred);
39
40 # blank_row($category_code) - return true if the entire row is blank.
41 sub blank_row {
42     my ($category_code) = @_;
43     for my $rp (@rule_params) {
44         for my $n (1 .. 3) {
45             my $key   = "${rp}${n}-$category_code";
46             
47             if (utf8::is_utf8($key)) {
48               utf8::encode($key);
49             }
50             
51             my $value = $input->param($key);
52             if ($value) {
53                 return 0;
54             }
55         }
56     }
57     return 1;
58 }
59
60 my $type=$input->param('type');
61 my $branch = $input->param('branch');
62 $branch="" unless $branch;
63 my $op = $input->param('op');
64
65 # my $flagsrequired;
66 # $flagsrequired->{circulation}=1;
67 my ($template, $loggedinuser, $cookie)
68     = get_template_and_user({template_name => "tools/overduerules.tmpl",
69                             query => $input,
70                             type => "intranet",
71                             authnotrequired => 0,
72                             flagsrequired => { tools => 'edit_notice_status_triggers'},
73                             debug => 1,
74                             });
75 my $err=0;
76
77 # save the values entered into tables
78 my %temphash;
79 my $input_saved = 0;
80 if ($op eq 'save') {
81     my @names=$input->param();
82     my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
83
84     my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
85     my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
86     my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
87     foreach my $key (@names){
88             # ISSUES
89             if ($key =~ /(.*)([1-3])-(.*)/) {
90                     my $type = $1; # data type
91                     my $num = $2; # From 1 to 3
92                     my $bor = $3; # borrower category
93                     $temphash{$bor}->{"$type$num"}=$input->param("$key") if (($input->param("$key") ne "") or ($input->param("$key")>0));
94             }
95     }
96
97     # figure out which rows need to be deleted
98     my @rows_to_delete = grep { blank_row($_) } @category_codes;
99
100     foreach my $bor (keys %temphash){
101         # get category name if we need it for an error message
102         my $bor_category = GetBorrowercategory($bor);
103         my $bor_category_name = defined($bor_category) ? $bor_category->{description} : $bor;
104
105         # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
106         # Raise error if not true
107         if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
108             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
109             $err=1;
110         } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
111             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
112             $err=1;
113         } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
114             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
115             $err=1;
116         } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
117             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
118             $err=1;
119         } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
120             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
121             $err=1;
122         } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
123             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
124             $err=1;
125         }elsif ($temphash{$bor}->{delay3} and
126                 ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
127                 or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
128                     $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
129                         $err=1;
130         }
131         unless ($err){
132             if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
133                 or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
134                 or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
135                     $sth_search->execute($branch,$bor);
136                     my $res = $sth_search->fetchrow_hashref();
137                     if ($res->{'total'}>0) {
138                         $sth_update->execute(
139                             ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
140                             ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
141                             ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
142                             ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
143                             ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
144                             ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
145                             ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
146                             ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
147                             ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
148                             $branch ,$bor
149                             );
150                     } else {
151                         $sth_insert->execute($branch,$bor,
152                             ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
153                             ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
154                             ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
155                             ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
156                             ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
157                             ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
158                             ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
159                             ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
160                             ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
161                             );
162                     }
163                 }
164         }
165     }
166     unless ($err) {
167         for my $category_code (@rows_to_delete) {
168             $sth_delete->execute($branch, $category_code);
169         }
170         $template->param(datasaved => 1);
171         $input_saved = 1;
172     }
173 }
174 my $branches = GetBranches();
175 my @branchloop;
176 foreach my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
177         my $selected = 1 if $thisbranch eq $branch;
178         my %row =(value => $thisbranch,
179                                 selected => $selected,
180                                 branchname => $branches->{$thisbranch}->{'branchname'},
181                         );
182         push @branchloop, \%row;
183 }
184
185 my $letters = GetLetters("circulation");
186
187 my $countletters = scalar $letters;
188
189 my @line_loop;
190
191 for my $data (@categories) {
192     my %row = (
193         overduename => $data->{'categorycode'},
194         line        => $data->{'description'}
195     );
196     if (%temphash and not $input_saved){
197         # if we managed to save the form submission, don't
198         # reuse %temphash, but take the values from the
199         # database - this makes it easier to identify
200         # bugs where the form submission was not correctly saved
201         for (my $i=1;$i<=3;$i++){
202             $row{"delay$i"}=$temphash{$data->{'categorycode'}}->{"delay$i"};
203             $row{"debarred$i"}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
204             if ($countletters){
205                 my @letterloop;
206                 foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
207                     my $selected = 1 if $thisletter eq $temphash{$data->{'categorycode'}}->{"letter$i"};
208                     my %letterrow =(value => $thisletter,
209                                     selected => $selected,
210                                     lettername => $letters->{$thisletter},
211                                     );
212                     push @letterloop, \%letterrow;
213                 }
214                 $row{"letterloop$i"}=\@letterloop;
215             } else {
216                 $row{"noletter"}=1;
217                 $row{"letter$i"}=$temphash{$data->{'categorycode'}}->{"letter$i"};
218             }
219         }
220     } else {
221     #getting values from table
222         my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
223         $sth2->execute($branch,$data->{'categorycode'});
224         my $dat=$sth2->fetchrow_hashref;
225         for (my $i=1;$i<=3;$i++){
226             if ($countletters){
227                 my @letterloop;
228                 foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
229                     my $selected = 1 if $thisletter eq $dat->{"letter$i"};
230                     my %letterrow =(value => $thisletter,
231                                     selected => $selected,
232                                     lettername => $letters->{$thisletter},
233                                     );
234                     push @letterloop, \%letterrow;
235                 }
236                 $row{"letterloop$i"}=\@letterloop;
237             } else {
238                 $row{"noletter"}=1;
239                 if ($dat->{"letter$i"}){$row{"letter$i"}=$dat->{"letter$i"};}
240             }
241             if ($dat->{"delay$i"}){$row{"delay$i"}=$dat->{"delay$i"};}
242             if ($dat->{"debarred$i"}){$row{"debarred$i"}=$dat->{"debarred$i"};}
243         }
244         $sth2->finish;
245     }
246     push @line_loop,\%row;
247 }
248
249 $template->param(table=> \@line_loop,
250                 branchloop => \@branchloop,
251                 branch => $branch);
252 output_html_with_http_headers $input, $cookie, $template->output;