Lines 1-309
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 <http://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("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)"); |
91 |
my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?"); |
92 |
my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?"); |
93 |
my $sth_insert_mtt = $dbh->prepare(" |
94 |
INSERT INTO overduerules_transport_types( |
95 |
overduerules_id, letternumber, message_transport_type |
96 |
) VALUES ( |
97 |
(SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ? |
98 |
) |
99 |
"); |
100 |
my $sth_delete_mtt = $dbh->prepare(" |
101 |
DELETE FROM overduerules_transport_types |
102 |
WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?) |
103 |
"); |
104 |
|
105 |
foreach my $key (@names){ |
106 |
# ISSUES |
107 |
if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) { |
108 |
my $type = $1; # data type |
109 |
my $num = $2; # From 1 to 3 |
110 |
my $bor = $3; # borrower category |
111 |
my $value = $input->param($key); |
112 |
if ($type eq 'delay') { |
113 |
$temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : ''; |
114 |
} else { |
115 |
# type is letter |
116 |
$temphash{$bor}->{"$type$num"} = $value if $value ne ''; |
117 |
} |
118 |
} |
119 |
} |
120 |
|
121 |
# figure out which rows need to be deleted |
122 |
my @rows_to_delete = grep { blank_row($_) } @category_codes; |
123 |
|
124 |
foreach my $bor (keys %temphash){ |
125 |
# get category name if we need it for an error message |
126 |
my $bor_category = Koha::Patron::Categories->find($bor); |
127 |
my $bor_category_name = $bor_category ? $bor_category->description : $bor; |
128 |
|
129 |
# Do some Checking here : delay1 < delay2 <delay3 all of them being numbers |
130 |
# Raise error if not true |
131 |
if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){ |
132 |
$template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name); |
133 |
$err=1; |
134 |
} elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){ |
135 |
$template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name); |
136 |
$err=1; |
137 |
} elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){ |
138 |
$template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name); |
139 |
$err=1; |
140 |
} elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) { |
141 |
$template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name); |
142 |
$err=1; |
143 |
} elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) { |
144 |
$template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name); |
145 |
$err=1; |
146 |
} elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) { |
147 |
$template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name); |
148 |
$err=1; |
149 |
}elsif ($temphash{$bor}->{delay3} and |
150 |
($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1}) |
151 |
or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){ |
152 |
$template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name); |
153 |
$err=1; |
154 |
} |
155 |
unless ($err){ |
156 |
if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) |
157 |
or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) |
158 |
or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) { |
159 |
$sth_search->execute($branch,$bor); |
160 |
my $res = $sth_search->fetchrow_hashref(); |
161 |
if ($res->{'total'}>0) { |
162 |
$sth_update->execute( |
163 |
($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef), |
164 |
($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""), |
165 |
($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0), |
166 |
($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef), |
167 |
($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""), |
168 |
($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0), |
169 |
($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef), |
170 |
($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""), |
171 |
($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0), |
172 |
$branch ,$bor |
173 |
); |
174 |
} else { |
175 |
$sth_insert->execute($branch,$bor, |
176 |
($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0), |
177 |
($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""), |
178 |
($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0), |
179 |
($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0), |
180 |
($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""), |
181 |
($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0), |
182 |
($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0), |
183 |
($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""), |
184 |
($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0) |
185 |
); |
186 |
} |
187 |
|
188 |
$sth_delete_mtt->execute( $branch, $bor ); |
189 |
for my $letternumber ( 1..3 ) { |
190 |
my @mtt = $input->multi_param( "mtt${letternumber}-$bor" ); |
191 |
next unless @mtt; |
192 |
for my $mtt ( @mtt ) { |
193 |
$sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt); |
194 |
} |
195 |
} |
196 |
} |
197 |
} |
198 |
} |
199 |
unless ($err) { |
200 |
for my $category_code (@rows_to_delete) { |
201 |
$sth_delete->execute($branch, $category_code); |
202 |
} |
203 |
$template->param(datasaved => 1); |
204 |
$input_saved = 1; |
205 |
} |
206 |
} |
207 |
|
208 |
my $letters = C4::Letters::GetLettersAvailableForALibrary( |
209 |
{ |
210 |
branchcode => $branch, |
211 |
module => "circulation", |
212 |
} |
213 |
); |
214 |
|
215 |
my $message_transport_types = C4::Letters::GetMessageTransportTypes(); |
216 |
my ( @first, @second, @third ); |
217 |
for my $patron_category (@patron_categories) { |
218 |
if (%temphash and not $input_saved){ |
219 |
# if we managed to save the form submission, don't |
220 |
# reuse %temphash, but take the values from the |
221 |
# database - this makes it easier to identify |
222 |
# bugs where the form submission was not correctly saved |
223 |
for my $i ( 1..3 ){ |
224 |
my %row = ( |
225 |
overduename => $patron_category->categorycode, |
226 |
line => $patron_category->description, |
227 |
); |
228 |
$row{delay}=$temphash{$patron_category->categorycode}->{"delay$i"}; |
229 |
$row{debarred}=$temphash{$patron_category->categorycode}->{"debarred$i"}; |
230 |
$row{selected_lettercode} = $temphash{ $patron_category->categorycode }->{"letter$i"}; |
231 |
my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) }; |
232 |
my @mtts; |
233 |
for my $mtt ( @$message_transport_types ) { |
234 |
push @mtts, { |
235 |
value => $mtt, |
236 |
selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 , |
237 |
} |
238 |
} |
239 |
$row{message_transport_types} = \@mtts; |
240 |
if ( $i == 1 ) { |
241 |
push @first, \%row; |
242 |
} elsif ( $i == 2 ) { |
243 |
push @second, \%row; |
244 |
} else { |
245 |
push @third, \%row; |
246 |
} |
247 |
} |
248 |
} else { |
249 |
#getting values from table |
250 |
my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?"); |
251 |
$sth2->execute($branch,$patron_category->categorycode); |
252 |
my $dat=$sth2->fetchrow_hashref; |
253 |
for my $i ( 1..3 ){ |
254 |
my %row = ( |
255 |
overduename => $patron_category->categorycode, |
256 |
line => $patron_category->description, |
257 |
); |
258 |
|
259 |
$row{selected_lettercode} = $dat->{"letter$i"}; |
260 |
|
261 |
if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};} |
262 |
if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};} |
263 |
my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) }; |
264 |
my @mtts; |
265 |
for my $mtt ( @$message_transport_types ) { |
266 |
push @mtts, { |
267 |
value => $mtt, |
268 |
selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 , |
269 |
} |
270 |
} |
271 |
$row{message_transport_types} = \@mtts; |
272 |
if ( $i == 1 ) { |
273 |
push @first, \%row; |
274 |
} elsif ( $i == 2 ) { |
275 |
push @second, \%row; |
276 |
} else { |
277 |
push @third, \%row; |
278 |
} |
279 |
|
280 |
} |
281 |
} |
282 |
} |
283 |
|
284 |
my @tabs = ( |
285 |
{ |
286 |
id => 'first', |
287 |
number => 1, |
288 |
values => \@first, |
289 |
}, |
290 |
{ |
291 |
id => 'second', |
292 |
number => 2, |
293 |
values => \@second, |
294 |
}, |
295 |
{ |
296 |
id => 'third', |
297 |
number => 3, |
298 |
values => \@third, |
299 |
}, |
300 |
); |
301 |
|
302 |
$template->param( |
303 |
table => ( @first or @second or @third ? 1 : 0 ), |
304 |
branch => $branch, |
305 |
tabs => \@tabs, |
306 |
message_transport_types => $message_transport_types, |
307 |
letters => $letters, |
308 |
); |
309 |
output_html_with_http_headers $input, $cookie, $template->output; |
310 |
- |