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