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