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
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.
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.
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>.
31 use Koha
::Patron
::Categories
;
34 my $dbh = C4
::Context
->dbh;
36 my @patron_categories = Koha
::Patron
::Categories
->search( { overduenoticerequired
=> { '>' => 0 } } );
37 my @category_codes = map { $_->categorycode } @patron_categories;
38 our @rule_params = qw(delay letter debarred);
40 # blank_row($category_code) - return true if the entire row is blank.
42 my ($category_code) = @_;
43 for my $rp (@rule_params) {
45 my $key = "${rp}${n}-$category_code";
47 if (utf8
::is_utf8
($key)) {
51 my $value = $input->param($key);
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
62 template_name
=> "tools/overduerules.tt",
66 flagsrequired
=> { tools
=> 'edit_notice_status_triggers' },
71 my $type = $input->param('type');
73 my $branch = $input->param('branch');
75 defined $branch ?
$branch
76 : C4
::Context
->preference('DefaultToLoggedInLibraryOverdueTriggers') ? C4
::Context
::mybranch
()
77 : Koha
::Libraries
->search->count() == 1 ?
undef
81 my $op = $input->param('op');
86 # save the values entered into tables
90 my @names=$input->multi_param();
91 my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
93 my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
94 my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
95 my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
96 my $sth_insert_mtt = $dbh->prepare("
97 INSERT INTO overduerules_transport_types(
98 overduerules_id, letternumber, message_transport_type
100 (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ?
103 my $sth_delete_mtt = $dbh->prepare("
104 DELETE FROM overduerules_transport_types
105 WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?)
108 foreach my $key (@names){
110 if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
111 my $type = $1; # data type
112 my $num = $2; # From 1 to 3
113 my $bor = $3; # borrower category
114 my $value = $input->param($key);
115 if ($type eq 'delay') {
116 $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ?
int($value) : '';
119 $temphash{$bor}->{"$type$num"} = $value if $value ne '';
124 # figure out which rows need to be deleted
125 my @rows_to_delete = grep { blank_row
($_) } @category_codes;
127 foreach my $bor (keys %temphash){
128 # get category name if we need it for an error message
129 my $bor_category = Koha
::Patron
::Categories
->find($bor);
130 my $bor_category_name = $bor_category ?
$bor_category->description : $bor;
132 # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
133 # Raise error if not true
134 if ($temphash{$bor}->{delay1
}=~/[^0-9]/ and $temphash{$bor}->{delay1
} ne ""){
135 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
137 } elsif ($temphash{$bor}->{delay2
}=~/[^0-9]/ and $temphash{$bor}->{delay2
} ne ""){
138 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
140 } elsif ($temphash{$bor}->{delay3
}=~/[^0-9]/ and $temphash{$bor}->{delay3
} ne ""){
141 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
143 } elsif ($temphash{$bor}->{delay1
} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
144 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
146 } elsif ($temphash{$bor}->{delay2
} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
147 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
149 } elsif ($temphash{$bor}->{delay3
} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
150 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
152 }elsif ($temphash{$bor}->{delay3
} and
153 ($temphash{$bor}->{delay3
}<=$temphash{$bor}->{delay2
} or $temphash{$bor}->{delay3
}<=$temphash{$bor}->{delay1
})
154 or $temphash{$bor}->{delay2
} and ($temphash{$bor}->{delay2
}<=$temphash{$bor}->{delay1
})){
155 $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
159 if (($temphash{$bor}->{delay1
} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
160 or ($temphash{$bor}->{delay2
} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
161 or ($temphash{$bor}->{delay3
} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
162 $sth_search->execute($branch,$bor);
163 my $res = $sth_search->fetchrow_hashref();
164 if ($res->{'total'}>0) {
165 $sth_update->execute(
166 ($temphash{$bor}->{"delay1"}?
$temphash{$bor}->{"delay1"}:undef),
167 ($temphash{$bor}->{"letter1"}?
$temphash{$bor}->{"letter1"}:""),
168 ($temphash{$bor}->{"debarred1"}?
$temphash{$bor}->{"debarred1"}:0),
169 ($temphash{$bor}->{"delay2"}?
$temphash{$bor}->{"delay2"}:undef),
170 ($temphash{$bor}->{"letter2"}?
$temphash{$bor}->{"letter2"}:""),
171 ($temphash{$bor}->{"debarred2"}?
$temphash{$bor}->{"debarred2"}:0),
172 ($temphash{$bor}->{"delay3"}?
$temphash{$bor}->{"delay3"}:undef),
173 ($temphash{$bor}->{"letter3"}?
$temphash{$bor}->{"letter3"}:""),
174 ($temphash{$bor}->{"debarred3"}?
$temphash{$bor}->{"debarred3"}:0),
178 $sth_insert->execute($branch,$bor,
179 ($temphash{$bor}->{"delay1"}?
$temphash{$bor}->{"delay1"}:0),
180 ($temphash{$bor}->{"letter1"}?
$temphash{$bor}->{"letter1"}:""),
181 ($temphash{$bor}->{"debarred1"}?
$temphash{$bor}->{"debarred1"}:0),
182 ($temphash{$bor}->{"delay2"}?
$temphash{$bor}->{"delay2"}:0),
183 ($temphash{$bor}->{"letter2"}?
$temphash{$bor}->{"letter2"}:""),
184 ($temphash{$bor}->{"debarred2"}?
$temphash{$bor}->{"debarred2"}:0),
185 ($temphash{$bor}->{"delay3"}?
$temphash{$bor}->{"delay3"}:0),
186 ($temphash{$bor}->{"letter3"}?
$temphash{$bor}->{"letter3"}:""),
187 ($temphash{$bor}->{"debarred3"}?
$temphash{$bor}->{"debarred3"}:0)
191 $sth_delete_mtt->execute( $branch, $bor );
192 for my $letternumber ( 1..3 ) {
193 my @mtt = $input->multi_param( "mtt${letternumber}-$bor" );
195 for my $mtt ( @mtt ) {
196 $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
203 for my $category_code (@rows_to_delete) {
204 $sth_delete->execute($branch, $category_code);
206 $template->param(datasaved
=> 1);
211 my $letters = C4
::Letters
::GetLettersAvailableForALibrary
(
213 branchcode
=> $branch,
214 module
=> "circulation",
218 my $message_transport_types = C4
::Letters
::GetMessageTransportTypes
();
219 my ( @first, @second, @third );
220 for my $patron_category (@patron_categories) {
221 if (%temphash and not $input_saved){
222 # if we managed to save the form submission, don't
223 # reuse %temphash, but take the values from the
224 # database - this makes it easier to identify
225 # bugs where the form submission was not correctly saved
228 overduename
=> $patron_category->categorycode,
229 line
=> $patron_category->description,
231 $row{delay
}=$temphash{$patron_category->categorycode}->{"delay$i"};
232 $row{debarred
}=$temphash{$patron_category->categorycode}->{"debarred$i"};
233 $row{selected_lettercode
} = $temphash{ $patron_category->categorycode }->{"letter$i"};
234 my @selected_mtts = @
{ GetOverdueMessageTransportTypes
( $branch, $patron_category->categorycode, $i) };
236 for my $mtt ( @
$message_transport_types ) {
239 selected
=> ( grep {/$mtt/} @selected_mtts ) ?
1 : 0 ,
242 $row{message_transport_types
} = \
@mtts;
245 } elsif ( $i == 2 ) {
252 #getting values from table
253 my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
254 $sth2->execute($branch,$patron_category->categorycode);
255 my $dat=$sth2->fetchrow_hashref;
258 overduename
=> $patron_category->categorycode,
259 line
=> $patron_category->description,
262 $row{selected_lettercode
} = $dat->{"letter$i"};
264 if ($dat->{"delay$i"}){$row{delay
}=$dat->{"delay$i"};}
265 if ($dat->{"debarred$i"}){$row{debarred
}=$dat->{"debarred$i"};}
266 my @selected_mtts = @
{ GetOverdueMessageTransportTypes
( $branch, $patron_category->categorycode, $i) };
268 for my $mtt ( @
$message_transport_types ) {
271 selected
=> ( grep {/$mtt/} @selected_mtts ) ?
1 : 0 ,
274 $row{message_transport_types
} = \
@mtts;
277 } elsif ( $i == 2 ) {
306 table
=> ( @first or @second or @third ?
1 : 0 ),
309 message_transport_types
=> $message_transport_types,
312 output_html_with_http_headers
$input, $cookie, $template->output;