Bug 15381: Remove GetAuthType and GetAuthTypeCode
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use CGI qw ( -utf8 );
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 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63     {
64         template_name   => "tools/overduerules.tt",
65         query           => $input,
66         type            => "intranet",
67         authnotrequired => 0,
68         flagsrequired   => { tools => 'edit_notice_status_triggers' },
69         debug           => 1,
70     }
71 );
72
73 my $type = $input->param('type');
74
75 my $branch = $input->param('branch');
76 $branch =
77     defined $branch                                                    ? $branch
78   : C4::Context->preference('DefaultToLoggedInLibraryOverdueTriggers') ? C4::Branch::mybranch()
79   : GetBranchesCount() == 1                                            ? undef
80   :                                                                      undef;
81 $branch ||= q{};
82 $branch = q{} if $branch eq 'NO_LIBRARY_SET';
83
84 my $op = $input->param('op');
85 $op ||= q{};
86
87 my $err=0;
88
89 # save the values entered into tables
90 my %temphash;
91 my $input_saved = 0;
92 if ($op eq 'save') {
93     my @names=$input->param();
94     my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
95
96     my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
97     my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
98     my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
99     my $sth_insert_mtt = $dbh->prepare("
100         INSERT INTO overduerules_transport_types(
101             overduerules_id, letternumber, message_transport_type
102         ) VALUES (
103             (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ?
104         )
105     ");
106     my $sth_delete_mtt = $dbh->prepare("
107         DELETE FROM overduerules_transport_types
108         WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?)
109     ");
110
111     foreach my $key (@names){
112             # ISSUES
113             if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
114                     my $type = $1; # data type
115                     my $num = $2; # From 1 to 3
116                     my $bor = $3; # borrower category
117                     my $value = $input->param($key);
118                     if ($type eq 'delay') {
119                         $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : '';
120                     } else {
121                         # type is letter
122                         $temphash{$bor}->{"$type$num"} = $value if $value ne '';
123                     }
124             }
125     }
126
127     # figure out which rows need to be deleted
128     my @rows_to_delete = grep { blank_row($_) } @category_codes;
129
130     foreach my $bor (keys %temphash){
131         # get category name if we need it for an error message
132         my $bor_category = GetBorrowercategory($bor);
133         my $bor_category_name = defined($bor_category) ? $bor_category->{description} : $bor;
134
135         # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
136         # Raise error if not true
137         if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
138             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
139             $err=1;
140         } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
141             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
142             $err=1;
143         } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
144             $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
145             $err=1;
146         } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
147             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
148             $err=1;
149         } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
150             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
151             $err=1;
152         } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
153             $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
154             $err=1;
155         }elsif ($temphash{$bor}->{delay3} and
156                 ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
157                 or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
158                     $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
159                         $err=1;
160         }
161         unless ($err){
162             if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
163                 or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
164                 or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
165                     $sth_search->execute($branch,$bor);
166                     my $res = $sth_search->fetchrow_hashref();
167                     if ($res->{'total'}>0) {
168                         $sth_update->execute(
169                             ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef),
170                             ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
171                             ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
172                             ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef),
173                             ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
174                             ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
175                             ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef),
176                             ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
177                             ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
178                             $branch ,$bor
179                             );
180                     } else {
181                         $sth_insert->execute($branch,$bor,
182                             ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
183                             ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
184                             ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
185                             ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
186                             ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
187                             ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
188                             ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
189                             ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
190                             ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
191                             );
192                     }
193
194                     $sth_delete_mtt->execute( $branch, $bor );
195                     for my $letternumber ( 1..3 ) {
196                         my @mtt = $input->param( "mtt${letternumber}-$bor" );
197                         next unless @mtt;
198                         for my $mtt ( @mtt ) {
199                             $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
200                         }
201                     }
202                 }
203         }
204     }
205     unless ($err) {
206         for my $category_code (@rows_to_delete) {
207             $sth_delete->execute($branch, $category_code);
208         }
209         $template->param(datasaved => 1);
210         $input_saved = 1;
211     }
212 }
213 my $branchloop = GetBranchesLoop($branch);
214
215 my $letters = C4::Letters::GetLettersAvailableForALibrary(
216     {
217         branchcode => $branch,
218         module => "circulation",
219     }
220 );
221
222 my @line_loop;
223
224 my $message_transport_types = C4::Letters::GetMessageTransportTypes();
225 my ( @first, @second, @third );
226 for my $data (@categories) {
227     if (%temphash and not $input_saved){
228         # if we managed to save the form submission, don't
229         # reuse %temphash, but take the values from the
230         # database - this makes it easier to identify
231         # bugs where the form submission was not correctly saved
232         for my $i ( 1..3 ){
233             my %row = (
234                 overduename => $data->{'categorycode'},
235                 line        => $data->{'description'}
236             );
237             $row{delay}=$temphash{$data->{'categorycode'}}->{"delay$i"};
238             $row{debarred}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
239             $row{selected_lettercode} = $temphash{ $data->{categorycode} }->{"letter$i"};
240             my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
241             my @mtts;
242             for my $mtt ( @$message_transport_types ) {
243                 push @mtts, {
244                     value => $mtt,
245                     selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
246                 }
247             }
248             $row{message_transport_types} = \@mtts;
249             if ( $i == 1 ) {
250                 push @first, \%row;
251             } elsif ( $i == 2 ) {
252                 push @second, \%row;
253             } else {
254                 push @third, \%row;
255             }
256         }
257     } else {
258     #getting values from table
259         my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
260         $sth2->execute($branch,$data->{'categorycode'});
261         my $dat=$sth2->fetchrow_hashref;
262         for my $i ( 1..3 ){
263             my %row = (
264                 overduename => $data->{'categorycode'},
265                 line        => $data->{'description'}
266             );
267
268             $row{selected_lettercode} = $dat->{"letter$i"};
269
270             if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};}
271             if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};}
272             my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
273             my @mtts;
274             for my $mtt ( @$message_transport_types ) {
275                 push @mtts, {
276                     value => $mtt,
277                     selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
278                 }
279             }
280             $row{message_transport_types} = \@mtts;
281             if ( $i == 1 ) {
282                 push @first, \%row;
283             } elsif ( $i == 2 ) {
284                 push @second, \%row;
285             } else {
286                 push @third, \%row;
287             }
288
289         }
290     }
291 }
292
293 my @tabs = (
294     {
295         id => 'first',
296         number => 1,
297         values => \@first,
298     },
299     {
300         id => 'second',
301         number => 2,
302         values => \@second,
303     },
304     {
305         id => 'third',
306         number => 3,
307         values => \@third,
308     },
309 );
310
311 $template->param(
312     table => ( @first or @second or @third ? 1 : 0 ),
313     branchloop => $branchloop,
314     branch => $branch,
315     tabs => \@tabs,
316     message_transport_types => $message_transport_types,
317     letters => $letters,
318 );
319 output_html_with_http_headers $input, $cookie, $template->output;