3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 my $dbh = C4
::Context
->dbh;
35 my @categories = @
{$dbh->selectall_arrayref(
36 'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0',
39 my @category_codes = map { $_->{categorycode
} } @categories;
40 our @rule_params = qw(delay letter debarred);
42 # blank_row($category_code) - return true if the entire row is blank.
44 my ($category_code) = @_;
45 for my $rp (@rule_params) {
47 my $key = "${rp}${n}-$category_code";
49 if (utf8
::is_utf8
($key)) {
53 my $value = $input->param($key);
62 my $type=$input->param('type');
63 my $branch = $input->param('branch');
65 my $op = $input->param('op');
68 my ($template, $loggedinuser, $cookie)
69 = get_template_and_user
({template_name
=> "tools/overduerules.tt",
73 flagsrequired
=> { tools
=> 'edit_notice_status_triggers'},
78 # save the values entered into tables
82 my @names=$input->param();
83 my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
85 my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
86 my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
87 my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
88 my $sth_insert_mtt = $dbh->prepare("
89 INSERT INTO overduerules_transport_types(
90 branchcode, categorycode, letternumber, message_transport_type
95 my $sth_delete_mtt = $dbh->prepare("
96 DELETE FROM overduerules_transport_types
97 WHERE branchcode = ? AND categorycode = ?
100 foreach my $key (@names){
102 if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
103 my $type = $1; # data type
104 my $num = $2; # From 1 to 3
105 my $bor = $3; # borrower category
106 my $value = $input->param($key);
107 if ($type eq 'delay') {
108 $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ?
int($value) : '';
111 $temphash{$bor}->{"$type$num"} = $value if $value ne '';
116 # figure out which rows need to be deleted
117 my @rows_to_delete = grep { blank_row
($_) } @category_codes;
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;
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);
129 } elsif ($temphash{$bor}->{delay2
}=~/[^0-9]/ and $temphash{$bor}->{delay2
} ne ""){
130 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
132 } elsif ($temphash{$bor}->{delay3
}=~/[^0-9]/ and $temphash{$bor}->{delay3
} ne ""){
133 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
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);
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);
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);
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);
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),
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)
183 $sth_delete_mtt->execute( $branch, $bor );
184 for my $letternumber ( 1..3 ) {
185 my @mtt = $input->param( "mtt${letternumber}-$bor" );
187 for my $mtt ( @mtt ) {
188 $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
195 for my $category_code (@rows_to_delete) {
196 $sth_delete->execute($branch, $category_code);
198 $template->param(datasaved
=> 1);
202 my $branchloop = GetBranchesLoop
($branch);
204 my $letters = C4
::Letters
::GetLettersAvailableForALibrary
(
206 branchcode
=> $branch,
207 module
=> "circulation",
213 my $message_transport_types = C4
::Letters
::GetMessageTransportTypes
();
214 my ( @first, @second, @third );
215 for my $data (@categories) {
216 if (%temphash and not $input_saved){
217 # if we managed to save the form submission, don't
218 # reuse %temphash, but take the values from the
219 # database - this makes it easier to identify
220 # bugs where the form submission was not correctly saved
223 overduename
=> $data->{'categorycode'},
224 line
=> $data->{'description'}
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) };
231 for my $mtt ( @
$message_transport_types ) {
234 selected
=> ( grep {/$mtt/} @selected_mtts ) ?
1 : 0 ,
237 $row{message_transport_types
} = \
@mtts;
240 } elsif ( $i == 2 ) {
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;
253 overduename
=> $data->{'categorycode'},
254 line
=> $data->{'description'}
257 $row{selected_lettercode
} = $dat->{"letter$i"};
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) };
263 for my $mtt ( @
$message_transport_types ) {
266 selected
=> ( grep {/$mtt/} @selected_mtts ) ?
1 : 0 ,
269 $row{message_transport_types
} = \
@mtts;
272 } elsif ( $i == 2 ) {
301 table
=> ( @first or @second or @third ?
1 : 0 ),
302 branchloop
=> $branchloop,
305 message_transport_types
=> $message_transport_types,
308 output_html_with_http_headers
$input, $cookie, $template->output;