Lines 1-6
Link Here
|
1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
2 |
|
2 |
|
3 |
# Copyright 2000-2002 Katipo Communications |
3 |
# Copyright 2014 Solutions inLibro |
4 |
# |
4 |
# |
5 |
# This file is part of Koha. |
5 |
# This file is part of Koha. |
6 |
# |
6 |
# |
Lines 20-25
Link Here
|
20 |
use strict; |
20 |
use strict; |
21 |
use warnings; |
21 |
use warnings; |
22 |
use CGI qw ( -utf8 ); |
22 |
use CGI qw ( -utf8 ); |
|
|
23 |
use List::Util qw/min/; |
24 |
use File::Spec; |
23 |
use C4::Context; |
25 |
use C4::Context; |
24 |
use C4::Output; |
26 |
use C4::Output; |
25 |
use C4::Auth; |
27 |
use C4::Auth; |
Lines 29-34
use C4::Members;
Link Here
|
29 |
use C4::Overdues; |
31 |
use C4::Overdues; |
30 |
use Koha::Libraries; |
32 |
use Koha::Libraries; |
31 |
|
33 |
|
|
|
34 |
|
32 |
use Koha::Patron::Categories; |
35 |
use Koha::Patron::Categories; |
33 |
|
36 |
|
34 |
our $input = new CGI; |
37 |
our $input = new CGI; |
Lines 38-61
my @patron_categories = Koha::Patron::Categories->search( { overduenoticerequire
Link Here
|
38 |
my @category_codes = map { $_->categorycode } @patron_categories; |
41 |
my @category_codes = map { $_->categorycode } @patron_categories; |
39 |
our @rule_params = qw(delay letter debarred); |
42 |
our @rule_params = qw(delay letter debarred); |
40 |
|
43 |
|
41 |
# blank_row($category_code) - return true if the entire row is blank. |
|
|
42 |
sub blank_row { |
43 |
my ($category_code) = @_; |
44 |
for my $rp (@rule_params) { |
45 |
for my $n (1 .. 3) { |
46 |
my $key = "${rp}${n}-$category_code"; |
47 |
|
48 |
if (utf8::is_utf8($key)) { |
49 |
utf8::encode($key); |
50 |
} |
51 |
|
44 |
|
52 |
my $value = $input->param($key); |
45 |
sub StringSearch { |
53 |
if ($value) { |
46 |
# this needs to be rewritten to use DBIx::Class |
54 |
return 0; |
47 |
my ($searchstring, $type) = @_; |
55 |
} |
48 |
my $dbh = C4::Context->dbh; |
56 |
} |
49 |
my @data = split( ' ', $searchstring ); |
57 |
} |
50 |
my $sth = $dbh->prepare( |
58 |
return 1; |
51 |
"SELECT * FROM overduerules WHERE (branchcode LIKE ?) or (categorycode LIKE ?) or (letter LIKE ?) or (itemtype LIKE ?) ORDER BY branchcode, delay" |
|
|
52 |
); |
53 |
$sth->execute("$data[0]%","$data[0]%","$data[0]%","$data[0]%"); |
54 |
return $sth->fetchall_arrayref({}); |
59 |
} |
55 |
} |
60 |
|
56 |
|
61 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
57 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
Lines 69-77
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
Link Here
|
69 |
} |
65 |
} |
70 |
); |
66 |
); |
71 |
|
67 |
|
72 |
my $type = $input->param('type'); |
68 |
my $searchfield = $input->param('description'); |
|
|
69 |
my $script_name = "/cgi-bin/koha/tools/overduerules.pl"; |
70 |
my $op = $input->param('op'); |
71 |
my $overduerules_id = $input->param('overduerules_id'); |
72 |
my $branch = $input->param('branchcode'); |
73 |
my $cat = $input->param('categorycode'); |
74 |
my $itemtype = $input->param('itemtype'); |
75 |
my $onhold = $input->param('onhold'); |
76 |
my $delay = $input->param('delay') || 1; |
77 |
my @mttparams; |
78 |
foreach ($input->param){ |
79 |
push (@mttparams, $1) if ($_ =~ /^mtt-(.*)/); |
80 |
} |
73 |
|
81 |
|
74 |
my $branch = $input->param('branch'); |
|
|
75 |
$branch = |
82 |
$branch = |
76 |
defined $branch ? $branch |
83 |
defined $branch ? $branch |
77 |
: C4::Context->preference('DefaultToLoggedInLibraryOverdueTriggers') ? C4::Context::mybranch() |
84 |
: C4::Context->preference('DefaultToLoggedInLibraryOverdueTriggers') ? C4::Context::mybranch() |
Lines 80-316
$branch =
Link Here
|
80 |
$branch ||= q{}; |
87 |
$branch ||= q{}; |
81 |
$branch = q{} if $branch eq 'NO_LIBRARY_SET'; |
88 |
$branch = q{} if $branch eq 'NO_LIBRARY_SET'; |
82 |
|
89 |
|
83 |
my $op = $input->param('op'); |
|
|
84 |
$op ||= q{}; |
85 |
|
90 |
|
86 |
my $err=0; |
91 |
$searchfield =~ s/\,//g; |
87 |
|
92 |
|
88 |
# save the values entered into tables |
|
|
89 |
my %temphash; |
90 |
my $input_saved = 0; |
91 |
if ($op eq 'save') { |
92 |
my @names=$input->multi_param(); |
93 |
my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?"); |
94 |
|
93 |
|
95 |
my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)"); |
94 |
# fix all this |
96 |
my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?"); |
95 |
my $hrItemtypes = $dbh->selectall_hashref("SELECT description,itemtype FROM itemtypes ", "itemtype"); |
97 |
my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?"); |
96 |
my $hrLetters = $dbh->selectall_hashref("SELECT name,code FROM letter WHERE module = 'circulation' order by name", "code"); |
98 |
my $sth_insert_mtt = $dbh->prepare(" |
97 |
my $arMessage_transport_types = C4::Letters::GetMessageTransportTypes(); |
99 |
INSERT INTO overduerules_transport_types( |
98 |
my $hrUsercats = $dbh->selectall_hashref("SELECT description,categorycode FROM categories WHERE overduenoticerequired = 1", "categorycode"); |
100 |
overduerules_id, letternumber, message_transport_type |
|
|
101 |
) VALUES ( |
102 |
(SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ? |
103 |
) |
104 |
"); |
105 |
my $sth_delete_mtt = $dbh->prepare(" |
106 |
DELETE FROM overduerules_transport_types |
107 |
WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?) |
108 |
"); |
109 |
|
99 |
|
110 |
foreach my $key (@names){ |
|
|
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 |
# type is letter |
121 |
$temphash{$bor}->{"$type$num"} = $value if $value ne ''; |
122 |
} |
123 |
} |
124 |
} |
125 |
|
100 |
|
126 |
# figure out which rows need to be deleted |
101 |
# get everything on the page |
127 |
my @rows_to_delete = grep { blank_row($_) } @category_codes; |
102 |
$template->param( |
128 |
|
103 |
script_name => $script_name, |
129 |
foreach my $bor (keys %temphash){ |
104 |
categories => \@patron_categories, |
130 |
# get category name if we need it for an error message |
105 |
itemtypes => $hrItemtypes, |
131 |
my $bor_category = Koha::Patron::Categories->find($bor); |
106 |
letters => $hrLetters, |
132 |
my $bor_category_name = $bor_category ? $bor_category->description : $bor; |
107 |
message_transport_types => $arMessage_transport_types, |
133 |
|
108 |
); |
134 |
# Do some Checking here : delay1 < delay2 <delay3 all of them being numbers |
|
|
135 |
# Raise error if not true |
136 |
if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){ |
137 |
$template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name); |
138 |
$err=1; |
139 |
} elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){ |
140 |
$template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name); |
141 |
$err=1; |
142 |
} elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){ |
143 |
$template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name); |
144 |
$err=1; |
145 |
} elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) { |
146 |
$template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name); |
147 |
$err=1; |
148 |
} elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) { |
149 |
$template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name); |
150 |
$err=1; |
151 |
} elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) { |
152 |
$template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name); |
153 |
$err=1; |
154 |
}elsif ($temphash{$bor}->{delay3} and |
155 |
($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1}) |
156 |
or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){ |
157 |
$template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name); |
158 |
$err=1; |
159 |
} |
160 |
unless ($err){ |
161 |
if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) |
162 |
or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) |
163 |
or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) { |
164 |
$sth_search->execute($branch,$bor); |
165 |
my $res = $sth_search->fetchrow_hashref(); |
166 |
if ($res->{'total'}>0) { |
167 |
$sth_update->execute( |
168 |
($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef), |
169 |
($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""), |
170 |
($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0), |
171 |
($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef), |
172 |
($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""), |
173 |
($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0), |
174 |
($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef), |
175 |
($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""), |
176 |
($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0), |
177 |
$branch ,$bor |
178 |
); |
179 |
} else { |
180 |
$sth_insert->execute($branch,$bor, |
181 |
($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0), |
182 |
($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""), |
183 |
($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0), |
184 |
($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0), |
185 |
($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""), |
186 |
($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0), |
187 |
($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0), |
188 |
($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""), |
189 |
($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0) |
190 |
); |
191 |
} |
192 |
|
109 |
|
193 |
$sth_delete_mtt->execute( $branch, $bor ); |
110 |
# operations |
194 |
for my $letternumber ( 1..3 ) { |
111 |
if ( $op =~ m/add/ ) { |
195 |
my @mtt = $input->multi_param( "mtt${letternumber}-$bor" ); |
112 |
$op =~ s/\D//g; |
196 |
next unless @mtt; |
113 |
$op = ($op eq '') ? 0 : $op; |
197 |
for my $mtt ( @mtt ) { |
114 |
$cat = (!defined $cat ) ? 0 : $cat; |
198 |
$sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt); |
115 |
$itemtype = (!defined $itemtype ) ? '' :$itemtype; |
199 |
} |
116 |
$onhold = (!defined $onhold ) ? 0 : $onhold; |
200 |
} |
117 |
my $debarred = (!defined $input->param('debarred')) ? 0 : $input->param('debarred'); |
201 |
} |
118 |
# dangerous, SQL injection fix |
202 |
} |
119 |
my $ruleId = $dbh->selectrow_array("SELECT overduerules_id from overduerules where branchcode = '$branch' and categorycode = '$cat' and itemtype = '$itemtype' and onhold = '$onhold' and delay = '$delay' "); |
|
|
120 |
if ( $ruleId ) {# it's a modification. We update the overrules table and delete corresponding entries in overduerules_transport_types |
121 |
my $query2 = " UPDATE overduerules |
122 |
SET letter = ?, debarred = ? |
123 |
WHERE branchcode = ? and categorycode = ? and itemtype = ? and onhold = ? and delay = ? "; |
124 |
my $sth = $dbh->prepare($query2); |
125 |
$sth->execute($input->param('letter') || '', $debarred , $branch, $cat, $itemtype, $onhold, $delay); |
126 |
$dbh->do("DELETE FROM overduerules_transport_types where overduerules_id = $ruleId"); |
203 |
} |
127 |
} |
204 |
unless ($err) { |
128 |
elsif ($op != 0) { # case edit pkeys |
205 |
for my $category_code (@rows_to_delete) { |
129 |
my $query2 = "UPDATE overduerules |
206 |
$sth_delete->execute($branch, $category_code); |
130 |
SET letter = ?, debarred = ?, branchcode = ?, categorycode = ?, itemtype = ?, onhold = ?, delay = ? |
207 |
} |
131 |
WHERE overduerules_id = ? "; |
208 |
$template->param(datasaved => 1); |
132 |
my $sth = $dbh->prepare($query2); |
209 |
$input_saved = 1; |
133 |
$sth->execute($input->param('letter') || '', $debarred , $branch, $cat, $itemtype, $onhold, $delay, $op); |
|
|
134 |
$dbh->do("DELETE FROM overduerules_transport_types where overduerules_id = $op"); |
210 |
} |
135 |
} |
211 |
} |
136 |
else { # add a new entry in overrules |
212 |
|
137 |
my $query = " |
213 |
my $letters = C4::Letters::GetLettersAvailableForALibrary( |
138 |
INSERT INTO overduerules (branchcode, categorycode, itemtype, onhold, delay, letter, debarred) |
214 |
{ |
139 |
VALUES (?,?,?,?,?,?,?);"; |
215 |
branchcode => $branch, |
140 |
my $sth = $dbh->prepare($query); |
216 |
module => "circulation", |
141 |
$sth->execute($branch, $cat, $itemtype, $onhold, $delay, $input->param('letter') || '', $debarred); |
|
|
142 |
$ruleId = $sth->{mysql_insertid}; # mysqlism need to fix this |
217 |
} |
143 |
} |
218 |
); |
144 |
# following an update or an insert, we have to insert into overduerules_transport_types |
219 |
|
145 |
my $sth = $dbh->prepare("INSERT INTO overduerules_transport_types (message_transport_type, overduerules_id) values (?,?)"); |
220 |
my @line_loop; |
146 |
foreach my $newMtt (@mttparams){ |
221 |
|
147 |
$sth->execute($newMtt, $ruleId); |
222 |
my $message_transport_types = C4::Letters::GetMessageTransportTypes(); |
148 |
} |
223 |
my ( @first, @second, @third ); |
149 |
print $input->redirect('overduerules.pl'); |
224 |
for my $patron_category (@patron_categories) { |
150 |
exit; |
225 |
if (%temphash and not $input_saved){ |
151 |
} |
226 |
# if we managed to save the form submission, don't |
152 |
elsif ( $op eq 'delete' ) { |
227 |
# reuse %temphash, but take the values from the |
153 |
my $sth1 = $dbh->prepare("delete from overduerules_transport_types where overduerules_id = ?"); |
228 |
# database - this makes it easier to identify |
154 |
my $sth2 = $dbh->prepare("delete from overduerules where overduerules_id = ?"); |
229 |
# bugs where the form submission was not correctly saved |
155 |
$sth1->execute($overduerules_id); |
230 |
for my $i ( 1..3 ){ |
156 |
$sth2->execute($overduerules_id); |
231 |
my %row = ( |
157 |
print $input->redirect('overduerules.pl'); |
232 |
overduename => $patron_category->categorycode, |
158 |
exit; |
233 |
line => $patron_category->description, |
159 |
} |
234 |
); |
160 |
else { #default |
235 |
$row{delay}=$temphash{$patron_category->categorycode}->{"delay$i"}; |
161 |
my ($results) = StringSearch( $searchfield ); |
236 |
$row{debarred}=$temphash{$patron_category->categorycode}->{"debarred$i"}; |
162 |
my @loop; |
237 |
$row{selected_lettercode} = $temphash{ $patron_category->categorycode }->{"letter$i"}; |
163 |
my $hrBranches = my $branches = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed; |
238 |
my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) }; |
164 |
foreach my $rule ( @{$results} ) { |
239 |
my @mtts; |
165 |
$rule->{branchname} = $hrBranches->{$rule->{branchcode}}->{branchname}; |
240 |
for my $mtt ( @$message_transport_types ) { |
166 |
$rule->{lettername} = $hrLetters->{$rule->{letter}}->{name}; |
241 |
push @mtts, { |
167 |
$rule->{categorydescription} = $hrUsercats->{$rule->{categorycode}}->{description}; |
242 |
value => $mtt, |
168 |
$rule->{itemtypedescription} = $hrItemtypes->{$rule->{itemtype}}->{description}; |
243 |
selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 , |
169 |
my @selected_mtts = @{ GetOverdueMessageTransportTypes( $rule->{overduerules_id} ) }; |
244 |
} |
170 |
my @mtts; |
245 |
} |
171 |
for my $mtt ( @$arMessage_transport_types ) { |
246 |
$row{message_transport_types} = \@mtts; |
172 |
push @mtts, { |
247 |
if ( $i == 1 ) { |
173 |
value => $mtt, |
248 |
push @first, \%row; |
174 |
selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 , |
249 |
} elsif ( $i == 2 ) { |
|
|
250 |
push @second, \%row; |
251 |
} else { |
252 |
push @third, \%row; |
253 |
} |
254 |
} |
255 |
} else { |
256 |
#getting values from table |
257 |
my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?"); |
258 |
$sth2->execute($branch,$patron_category->categorycode); |
259 |
my $dat=$sth2->fetchrow_hashref; |
260 |
for my $i ( 1..3 ){ |
261 |
my %row = ( |
262 |
overduename => $patron_category->categorycode, |
263 |
line => $patron_category->description, |
264 |
); |
265 |
|
266 |
$row{selected_lettercode} = $dat->{"letter$i"}; |
267 |
|
268 |
if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};} |
269 |
if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};} |
270 |
my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) }; |
271 |
my @mtts; |
272 |
for my $mtt ( @$message_transport_types ) { |
273 |
push @mtts, { |
274 |
value => $mtt, |
275 |
selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 , |
276 |
} |
277 |
} |
278 |
$row{message_transport_types} = \@mtts; |
279 |
if ( $i == 1 ) { |
280 |
push @first, \%row; |
281 |
} elsif ( $i == 2 ) { |
282 |
push @second, \%row; |
283 |
} else { |
284 |
push @third, \%row; |
285 |
} |
175 |
} |
286 |
|
176 |
|
287 |
} |
177 |
} |
|
|
178 |
$rule->{message_transport_types} = \@mtts; |
179 |
push( @loop, $rule ); |
288 |
} |
180 |
} |
289 |
} |
|
|
290 |
|
181 |
|
291 |
my @tabs = ( |
182 |
$template->param( |
292 |
{ |
183 |
loop => \@loop, |
293 |
id => 'first', |
184 |
); |
294 |
number => 1, |
185 |
} |
295 |
values => \@first, |
|
|
296 |
}, |
297 |
{ |
298 |
id => 'second', |
299 |
number => 2, |
300 |
values => \@second, |
301 |
}, |
302 |
{ |
303 |
id => 'third', |
304 |
number => 3, |
305 |
values => \@third, |
306 |
}, |
307 |
); |
308 |
|
186 |
|
309 |
$template->param( |
|
|
310 |
table => ( @first or @second or @third ? 1 : 0 ), |
311 |
branch => $branch, |
312 |
tabs => \@tabs, |
313 |
message_transport_types => $message_transport_types, |
314 |
letters => $letters, |
315 |
); |
316 |
output_html_with_http_headers $input, $cookie, $template->output; |
187 |
output_html_with_http_headers $input, $cookie, $template->output; |
317 |
- |
|
|