Bug 10694: (follow-up) fix various issues
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Branch;
28 use C4::Letters;
29 use C4::Members;
30 use C4::Overdues;
31
32 our $input = new CGI;
33 my $dbh = C4::Context->dbh;
34
35 my @categories = @{$dbh->selectall_arrayref(
36     'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0',
37     { Slice => {} }
38 )};
39 my @category_codes  = map { $_->{categorycode} } @categories;
40 our @rule_params     = qw(delay letter debarred);
41
42 # blank_row($category_code) - return true if the entire row is blank.
43 sub blank_row {
44     my ($category_code) = @_;
45     for my $rp (@rule_params) {
46         for my $n (1 .. 3) {
47             my $key   = "${rp}${n}-$category_code";
48
49             if (utf8::is_utf8($key)) {
50               utf8::encode($key);
51             }
52
53             my $value = $input->param($key);
54             if ($value) {
55                 return 0;
56             }
57         }
58     }
59     return 1;
60 }
61
62 my $type=$input->param('type');
63 my $branch = $input->param('branch');
64 $branch ||= q{};
65 my $op = $input->param('op');
66 $op ||= q{};
67
68 my ($template, $loggedinuser, $cookie)
69     = get_template_and_user({template_name => "tools/overduerules.tmpl",
70                             query => $input,
71                             type => "intranet",
72                             authnotrequired => 0,
73                             flagsrequired => { tools => 'edit_notice_status_triggers'},
74                             debug => 1,
75                             });
76 my $err=0;
77
78 # save the values entered into tables
79 my %temphash;
80 my $input_saved = 0;
81 if ($op eq 'save') {
82     my @names=$input->param();
83     my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
84
85     my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
86     my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
87     my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
88     my $sth_insert_mtt = $dbh->prepare("
89         INSERT INTO overduerules_transport_types(
90             branchcode, categorycode, letternumber, message_transport_type
91         ) VALUES (
92             ?, ?, ?, ?
93         )
94     ");
95     my $sth_delete_mtt = $dbh->prepare("
96         DELETE FROM overduerules_transport_types
97         WHERE branchcode = ? AND categorycode = ?
98     ");
99
100     foreach my $key (@names){
101             # ISSUES
102             if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
103                     my $type = $1; # data type
104                     my $num = $2; # From 1 to 3
105                     my $bor = $3; # borrower category
106                     my $value = $input->param($key);
107                     if ($type eq 'delay') {
108                         $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : '';
109                     } else {
110                         # type is letter
111                         $temphash{$bor}->{"$type$num"} = $value if $value ne '';
112                     }
113             }
114     }
115
116     # figure out which rows need to be deleted
117     my @rows_to_delete = grep { blank_row($_) } @category_codes;
118
119     foreach my $bor (keys %temphash){
120         # get category name if we need it for an error message
121         my $bor_category = GetBorrowercategory($bor);
122         my $bor_category_name = defined($bor_category) ? $bor_category->{description} : $bor;
123
124         # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
125         # Raise error if not true
126         if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
127             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
128             $err=1;
129         } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
130             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
131             $err=1;
132         } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
133             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
134             $err=1;
135         } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
136             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
137             $err=1;
138         } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
139             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
140             $err=1;
141         } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
142             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
143             $err=1;
144         }elsif ($temphash{$bor}->{delay3} and
145                 ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
146                 or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
147                     $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
148                         $err=1;
149         }
150         unless ($err){
151             if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
152                 or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
153                 or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
154                     $sth_search->execute($branch,$bor);
155                     my $res = $sth_search->fetchrow_hashref();
156                     if ($res->{'total'}>0) {
157                         $sth_update->execute(
158                             ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef),
159                             ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
160                             ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
161                             ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef),
162                             ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
163                             ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
164                             ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef),
165                             ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
166                             ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
167                             $branch ,$bor
168                             );
169                     } else {
170                         $sth_insert->execute($branch,$bor,
171                             ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
172                             ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
173                             ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
174                             ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
175                             ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
176                             ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
177                             ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
178                             ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
179                             ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
180                             );
181                     }
182
183                     $sth_delete_mtt->execute( $branch, $bor );
184                     for my $letternumber ( 1..3 ) {
185                         my @mtt = $input->param( "mtt${letternumber}-$bor" );
186                         next unless @mtt;
187                         for my $mtt ( @mtt ) {
188                             $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
189                         }
190                     }
191                 }
192         }
193     }
194     unless ($err) {
195         for my $category_code (@rows_to_delete) {
196             $sth_delete->execute($branch, $category_code);
197         }
198         $template->param(datasaved => 1);
199         $input_saved = 1;
200     }
201 }
202 my $branchloop = GetBranchesLoop($branch);
203
204 my $letters = GetLetters("circulation");
205
206 my $countletters = keys %{$letters};
207
208 my @line_loop;
209
210 my $message_transport_types = C4::Letters::GetMessageTransportTypes();
211 my ( @first, @second, @third );
212 for my $data (@categories) {
213     if (%temphash and not $input_saved){
214         # if we managed to save the form submission, don't
215         # reuse %temphash, but take the values from the
216         # database - this makes it easier to identify
217         # bugs where the form submission was not correctly saved
218         for my $i ( 1..3 ){
219             my %row = (
220                 overduename => $data->{'categorycode'},
221                 line        => $data->{'description'}
222             );
223             $row{delay}=$temphash{$data->{'categorycode'}}->{"delay$i"};
224             $row{debarred}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
225             if ($countletters){
226                 my @letterloop;
227                 foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
228                     my $selected;
229                     if ( $temphash{$data->{categorycode}}->{"letter$i"} &&
230                         $thisletter eq $temphash{$data->{'categorycode'}}->{"letter$i"}) {
231                         $selected = 1;
232                     }
233                     my %letterrow =(value => $thisletter,
234                                     selected => $selected,
235                                     lettername => $letters->{$thisletter},
236                                     );
237                     push @letterloop, \%letterrow;
238                 }
239                 $row{letterloop}=\@letterloop;
240             } else {
241                 $row{noletter}=1;
242                 $row{letter}=$temphash{$data->{'categorycode'}}->{"letter$i"};
243             }
244             my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
245             my @mtts;
246             for my $mtt ( @$message_transport_types ) {
247                 push @mtts, {
248                     value => $mtt,
249                     selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
250                 }
251             }
252             $row{message_transport_types} = \@mtts;
253             if ( $i == 1 ) {
254                 push @first, \%row;
255             } elsif ( $i == 2 ) {
256                 push @second, \%row;
257             } else {
258                 push @third, \%row;
259             }
260         }
261     } else {
262     #getting values from table
263         my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
264         $sth2->execute($branch,$data->{'categorycode'});
265         my $dat=$sth2->fetchrow_hashref;
266         for my $i ( 1..3 ){
267             my %row = (
268                 overduename => $data->{'categorycode'},
269                 line        => $data->{'description'}
270             );
271             if ($countletters){
272                 my @letterloop;
273                 foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
274                     my $selected;
275                     if ($dat->{"letter$i"} && $thisletter eq $dat->{"letter$i"}) {
276                         $selected = 1;
277                     }
278                     my %letterrow =(value => $thisletter,
279                                     selected => $selected,
280                                     lettername => $letters->{$thisletter},
281                                     );
282                     push @letterloop, \%letterrow;
283                 }
284                 $row{letterloop}=\@letterloop;
285             } else {
286                 $row{noletter}=1;
287                 if ($dat->{"letter$i"}){$row{letter}=$dat->{"letter$i"};}
288             }
289             if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};}
290             if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};}
291             my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
292             my @mtts;
293             for my $mtt ( @$message_transport_types ) {
294                 push @mtts, {
295                     value => $mtt,
296                     selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
297                 }
298             }
299             $row{message_transport_types} = \@mtts;
300             if ( $i == 1 ) {
301                 push @first, \%row;
302             } elsif ( $i == 2 ) {
303                 push @second, \%row;
304             } else {
305                 push @third, \%row;
306             }
307
308         }
309     }
310 }
311
312 my @tabs = (
313     {
314         id => 'first',
315         number => 1,
316         values => \@first,
317     },
318     {
319         id => 'second',
320         number => 2,
321         values => \@second,
322     },
323     {
324         id => 'third',
325         number => 3,
326         values => \@third,
327     },
328 );
329
330 $template->param(
331     table => ( @first or @second or @third ? 1 : 0 ),
332     branchloop => $branchloop,
333     branch => $branch,
334     tabs => \@tabs,
335     message_transport_types => $message_transport_types,
336 );
337 output_html_with_http_headers $input, $cookie, $template->output;