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-308
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; |
25 |
use C4::Koha; |
23 |
use C4::Context; |
26 |
use C4::Context; |
24 |
use C4::Output; |
|
|
25 |
use C4::Auth; |
27 |
use C4::Auth; |
26 |
use C4::Koha; |
28 |
use C4::Output; |
27 |
use C4::Branch; |
|
|
28 |
use C4::Letters; |
29 |
use C4::Letters; |
29 |
use C4::Members; |
30 |
use C4::Branch; |
30 |
use C4::Overdues; |
31 |
use C4::Overdues; |
|
|
32 |
use Data::Dumper; |
33 |
|
34 |
sub StringSearch { |
35 |
my ( $searchstring, $type ) = @_; |
36 |
my $dbh = C4::Context->dbh; |
37 |
$searchstring =~ s/\'/\\\'/g; |
38 |
my @data = split( ' ', $searchstring ); |
39 |
my $sth = $dbh->prepare( |
40 |
"SELECT * FROM overduerules WHERE (branchcode LIKE ?) or (categorycode LIKE ?) or (letter LIKE ?) or (itemtype LIKE ?) ORDER BY branchcode, delay" |
41 |
); |
42 |
$sth->execute("$data[0]%","$data[0]%","$data[0]%","$data[0]%"); |
43 |
return $sth->fetchall_arrayref({}); # return ref-to-array of ref-to-hashes |
44 |
# like [ fetchrow_hashref(), fetchrow_hashref() ... ] |
45 |
} |
31 |
|
46 |
|
32 |
our $input = new CGI; |
47 |
my $input = new CGI; |
33 |
my $dbh = C4::Context->dbh; |
48 |
my $searchfield = $input->param('description'); |
34 |
|
49 |
my $script_name = "/cgi-bin/koha/tools/overduerules.pl"; |
35 |
my @categories = @{$dbh->selectall_arrayref( |
50 |
my $op = $input->param('op'); |
36 |
'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0', |
51 |
my $overduerules_id = $input->param('overduerules_id'); |
37 |
{ Slice => {} } |
52 |
my $branch = $input->param('branchcode'); |
38 |
)}; |
53 |
my $cat = $input->param('categorycode'); |
39 |
my @category_codes = map { $_->{categorycode} } @categories; |
54 |
my $itemtype = $input->param('itemtype'); |
40 |
our @rule_params = qw(delay letter debarred); |
55 |
my $onhold = $input->param('onhold'); |
41 |
|
56 |
my $delay = $input->param('delay') || 1; |
42 |
# blank_row($category_code) - return true if the entire row is blank. |
57 |
my @mttparams; |
43 |
sub blank_row { |
58 |
foreach ($input->param){ |
44 |
my ($category_code) = @_; |
59 |
push (@mttparams, $1) if ($_ =~ /^mtt-(.*)/); |
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 |
} |
60 |
} |
61 |
|
61 |
|
62 |
my $type=$input->param('type'); |
62 |
$searchfield =~ s/\,//g; |
63 |
my $branch = $input->param('branch'); |
63 |
my ($template, $borrowernumber, $cookie ) = get_template_and_user( |
64 |
$branch ||= q{}; |
64 |
{ |
65 |
my $op = $input->param('op'); |
65 |
template_name => "tools/overduerules.tt", |
66 |
$op ||= q{}; |
66 |
query => $input, |
|
|
67 |
type => "intranet", |
68 |
authnotrequired => 0, |
69 |
flagsrequired => { parameters => 'edit_notice_status_triggers' }, |
70 |
debug => 1, |
71 |
} |
72 |
); |
67 |
|
73 |
|
68 |
my ($template, $loggedinuser, $cookie) |
74 |
my $dbh = C4::Context->dbh; |
69 |
= get_template_and_user({template_name => "tools/overduerules.tt", |
|
|
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 |
|
75 |
|
78 |
# save the values entered into tables |
76 |
# collect the parameters as hashrefs |
79 |
my %temphash; |
77 |
my $hrBranches = GetBranches(); |
80 |
my $input_saved = 0; |
78 |
my $hrUsercats = $dbh->selectall_hashref("SELECT description,categorycode FROM categories WHERE overduenoticerequired = 1", "categorycode"); |
81 |
if ($op eq 'save') { |
79 |
my $hrItemtypes = $dbh->selectall_hashref("SELECT description,itemtype FROM itemtypes ", "itemtype"); |
82 |
my @names=$input->param(); |
80 |
my $hrLetters = $dbh->selectall_hashref("SELECT name,code FROM letter WHERE module = 'circulation' order by name", "code"); |
83 |
my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?"); |
81 |
my $arMessage_transport_types = C4::Letters::GetMessageTransportTypes(); |
84 |
|
82 |
|
85 |
my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)"); |
83 |
# get everything on the page |
86 |
my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?"); |
84 |
$template->param( |
87 |
my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?"); |
85 |
script_name => $script_name, |
88 |
my $sth_insert_mtt = $dbh->prepare(" |
86 |
branches => $hrBranches, |
89 |
INSERT INTO overduerules_transport_types( |
87 |
categories => $hrUsercats, |
90 |
overduerules_id, letternumber, message_transport_type |
88 |
itemtypes => $hrItemtypes, |
91 |
) VALUES ( |
89 |
letters => $hrLetters, |
92 |
(SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ? |
90 |
message_transport_types => $arMessage_transport_types, |
93 |
) |
91 |
); |
94 |
"); |
|
|
95 |
my $sth_delete_mtt = $dbh->prepare(" |
96 |
DELETE FROM overduerules_transport_types |
97 |
WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?) |
98 |
"); |
99 |
|
92 |
|
100 |
foreach my $key (@names){ |
93 |
# operations |
101 |
# ISSUES |
94 |
if ( $op =~ m/add/ ) { |
102 |
if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) { |
95 |
$op =~ s/\D//g; |
103 |
my $type = $1; # data type |
96 |
$branch = (!defined $branch ) ? '' : $branch; |
104 |
my $num = $2; # From 1 to 3 |
97 |
$op = ($op eq '') ? 0 : $op; |
105 |
my $bor = $3; # borrower category |
98 |
$cat = (!defined $cat ) ? 0 : $cat; |
106 |
my $value = $input->param($key); |
99 |
$itemtype = (!defined $itemtype ) ? '' :$itemtype; |
107 |
if ($type eq 'delay') { |
100 |
$onhold = (!defined $onhold ) ? 0 : $onhold; |
108 |
$temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : ''; |
101 |
my $debarred = (!defined $input->param('debarred')) ? 0 : $input->param('debarred'); |
109 |
} else { |
102 |
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' "); |
110 |
# type is letter |
103 |
if ( $ruleId ) {# it's a modification. We update the overrules table and delete corresponding entries in overduerules_transport_types |
111 |
$temphash{$bor}->{"$type$num"} = $value if $value ne ''; |
104 |
my $query2 = " |
112 |
} |
105 |
UPDATE overduerules |
113 |
} |
106 |
SET letter = ?, debarred = ? |
|
|
107 |
WHERE branchcode = ? and categorycode = ? and itemtype = ? and onhold = ? and delay = ? "; |
108 |
my $sth = $dbh->prepare($query2); |
109 |
$sth->execute($input->param('letter') || '', $debarred , $branch, $cat, $itemtype, $onhold, $delay); |
110 |
$dbh->do("DELETE FROM overduerules_transport_types where overduerules_id = $ruleId"); |
114 |
} |
111 |
} |
|
|
112 |
elsif ($op != 0) { # case edit pkeys |
113 |
my $query2 = " |
114 |
UPDATE overduerules |
115 |
SET letter = ?, debarred = ?, branchcode = ?, categorycode = ?, itemtype = ?, onhold = ?, delay = ? |
116 |
WHERE overduerules_id = ? "; |
117 |
my $sth = $dbh->prepare($query2); |
118 |
$sth->execute($input->param('letter') || '', $debarred , $branch, $cat, $itemtype, $onhold, $delay, $op); |
119 |
$dbh->do("DELETE FROM overduerules_transport_types where overduerules_id = $op"); |
120 |
} else { # add a new entry in overrules |
121 |
my $query = " |
122 |
INSERT INTO overduerules (branchcode, categorycode, itemtype, onhold, delay, letter, debarred) |
123 |
VALUES (?,?,?,?,?,?,?);"; |
124 |
my $sth = $dbh->prepare($query); |
125 |
$sth->execute($branch, $cat, $itemtype, $onhold, $delay, $input->param('letter') || '', $debarred); |
126 |
$ruleId = $sth->{mysql_insertid}; |
115 |
|
127 |
|
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 |
} |
128 |
} |
194 |
unless ($err) { |
129 |
# following an update or an insert, we have to insert into overduerules_transport_types |
195 |
for my $category_code (@rows_to_delete) { |
130 |
my $sth = $dbh->prepare("INSERT INTO overduerules_transport_types (message_transport_type, overduerules_id) values (?,?)"); |
196 |
$sth_delete->execute($branch, $category_code); |
131 |
foreach my $newMtt (@mttparams){ |
197 |
} |
132 |
$sth->execute($newMtt, $ruleId); |
198 |
$template->param(datasaved => 1); |
|
|
199 |
$input_saved = 1; |
200 |
} |
133 |
} |
|
|
134 |
print $input->redirect('overduerules.pl'); |
135 |
exit; |
201 |
} |
136 |
} |
202 |
my $branchloop = GetBranchesLoop($branch); |
137 |
elsif ( $op eq 'delete' ) { |
203 |
|
138 |
my $sth1 = $dbh->prepare("delete from overduerules_transport_types where overduerules_id = ?"); |
204 |
my $letters = C4::Letters::GetLettersAvailableForALibrary( |
139 |
my $sth2 = $dbh->prepare("delete from overduerules where overduerules_id = ?"); |
205 |
{ |
140 |
$sth1->execute($overduerules_id); |
206 |
branchcode => $branch, |
141 |
$sth2->execute($overduerules_id); |
207 |
module => "circulation", |
142 |
print $input->redirect('overduerules.pl'); |
208 |
} |
143 |
exit; |
209 |
); |
144 |
} |
210 |
|
145 |
else { # default |
211 |
my @line_loop; |
146 |
my ($results) = StringSearch( $searchfield ); |
212 |
|
147 |
my @loop; |
213 |
my $message_transport_types = C4::Letters::GetMessageTransportTypes(); |
148 |
foreach my $rule ( @{$results} ) { |
214 |
my ( @first, @second, @third ); |
149 |
$rule->{branchname} = $hrBranches->{$rule->{branchcode}}->{branchname}; |
215 |
for my $data (@categories) { |
150 |
$rule->{lettername} = $hrLetters->{$rule->{letter}}->{name}; |
216 |
if (%temphash and not $input_saved){ |
151 |
$rule->{categorydescription} = $hrUsercats->{$rule->{categorycode}}->{description}; |
217 |
# if we managed to save the form submission, don't |
152 |
$rule->{itemtypedescription} = $hrItemtypes->{$rule->{itemtype}}->{description}; |
218 |
# reuse %temphash, but take the values from the |
153 |
|
219 |
# database - this makes it easier to identify |
154 |
my @selected_mtts = @{ GetOverdueMessageTransportTypes( $rule->{overduerules_id} ) }; |
220 |
# bugs where the form submission was not correctly saved |
155 |
my @mtts; |
221 |
for my $i ( 1..3 ){ |
156 |
for my $mtt ( @$arMessage_transport_types ) { |
222 |
my %row = ( |
157 |
push @mtts, { |
223 |
overduename => $data->{'categorycode'}, |
158 |
value => $mtt, |
224 |
line => $data->{'description'} |
159 |
selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 , |
225 |
); |
|
|
226 |
$row{delay}=$temphash{$data->{'categorycode'}}->{"delay$i"}; |
227 |
$row{debarred}=$temphash{$data->{'categorycode'}}->{"debarred$i"}; |
228 |
$row{selected_lettercode} = $temphash{ $data->{categorycode} }->{"letter$i"}; |
229 |
my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) }; |
230 |
my @mtts; |
231 |
for my $mtt ( @$message_transport_types ) { |
232 |
push @mtts, { |
233 |
value => $mtt, |
234 |
selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 , |
235 |
} |
236 |
} |
237 |
$row{message_transport_types} = \@mtts; |
238 |
if ( $i == 1 ) { |
239 |
push @first, \%row; |
240 |
} elsif ( $i == 2 ) { |
241 |
push @second, \%row; |
242 |
} else { |
243 |
push @third, \%row; |
244 |
} |
245 |
} |
246 |
} else { |
247 |
#getting values from table |
248 |
my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?"); |
249 |
$sth2->execute($branch,$data->{'categorycode'}); |
250 |
my $dat=$sth2->fetchrow_hashref; |
251 |
for my $i ( 1..3 ){ |
252 |
my %row = ( |
253 |
overduename => $data->{'categorycode'}, |
254 |
line => $data->{'description'} |
255 |
); |
256 |
|
257 |
$row{selected_lettercode} = $dat->{"letter$i"}; |
258 |
|
259 |
if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};} |
260 |
if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};} |
261 |
my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) }; |
262 |
my @mtts; |
263 |
for my $mtt ( @$message_transport_types ) { |
264 |
push @mtts, { |
265 |
value => $mtt, |
266 |
selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 , |
267 |
} |
268 |
} |
269 |
$row{message_transport_types} = \@mtts; |
270 |
if ( $i == 1 ) { |
271 |
push @first, \%row; |
272 |
} elsif ( $i == 2 ) { |
273 |
push @second, \%row; |
274 |
} else { |
275 |
push @third, \%row; |
276 |
} |
160 |
} |
277 |
|
|
|
278 |
} |
161 |
} |
|
|
162 |
$rule->{message_transport_types} = \@mtts; |
163 |
push( @loop, $rule ); |
279 |
} |
164 |
} |
280 |
} |
|
|
281 |
|
165 |
|
282 |
my @tabs = ( |
166 |
$template->param( |
283 |
{ |
167 |
loop => \@loop, |
284 |
id => 'first', |
168 |
); |
285 |
number => 1, |
169 |
} |
286 |
values => \@first, |
|
|
287 |
}, |
288 |
{ |
289 |
id => 'second', |
290 |
number => 2, |
291 |
values => \@second, |
292 |
}, |
293 |
{ |
294 |
id => 'third', |
295 |
number => 3, |
296 |
values => \@third, |
297 |
}, |
298 |
); |
299 |
|
170 |
|
300 |
$template->param( |
|
|
301 |
table => ( @first or @second or @third ? 1 : 0 ), |
302 |
branchloop => $branchloop, |
303 |
branch => $branch, |
304 |
tabs => \@tabs, |
305 |
message_transport_types => $message_transport_types, |
306 |
letters => $letters, |
307 |
); |
308 |
output_html_with_http_headers $input, $cookie, $template->output; |
171 |
output_html_with_http_headers $input, $cookie, $template->output; |
309 |
- |
|
|