Bug 19910: Use the delimiter syspref to name download link
[koha.git] / tools / overduerules.pl
blob616f6070ea9c455b2962ed9a02070440c5e0b672
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Context;
23 use C4::Output;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Letters;
27 use C4::Members;
28 use C4::Overdues;
29 use Koha::Libraries;
31 use Koha::Patron::Categories;
33 our $input = new CGI;
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.
41 sub blank_row {
42 my ($category_code) = @_;
43 for my $rp (@rule_params) {
44 for my $n (1 .. 3) {
45 my $key = "${rp}${n}-$category_code";
47 if (utf8::is_utf8($key)) {
48 utf8::encode($key);
51 my $value = $input->param($key);
52 if ($value) {
53 return 0;
57 return 1;
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62 template_name => "tools/overduerules.tt",
63 query => $input,
64 type => "intranet",
65 authnotrequired => 0,
66 flagsrequired => { tools => 'edit_notice_status_triggers' },
67 debug => 1,
71 my $type = $input->param('type');
73 my $branch = $input->param('branch');
74 $branch =
75 defined $branch ? $branch
76 : C4::Context->preference('DefaultToLoggedInLibraryOverdueTriggers') ? C4::Context::mybranch()
77 : Koha::Libraries->search->count() == 1 ? undef
78 : undef;
79 $branch ||= q{};
80 $branch = q{} if $branch eq 'NO_LIBRARY_SET';
82 my $op = $input->param('op');
83 $op ||= q{};
85 my $err=0;
87 # save the values entered into tables
88 my %temphash;
89 my $input_saved = 0;
90 if ($op eq 'save') {
91 my @names=$input->multi_param();
92 my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
94 my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
95 my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
96 my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
97 my $sth_insert_mtt = $dbh->prepare("
98 INSERT INTO overduerules_transport_types(
99 overduerules_id, letternumber, message_transport_type
100 ) VALUES (
101 (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ?
104 my $sth_delete_mtt = $dbh->prepare("
105 DELETE FROM overduerules_transport_types
106 WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?)
109 foreach my $key (@names){
110 # ISSUES
111 if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
112 my $type = $1; # data type
113 my $num = $2; # From 1 to 3
114 my $bor = $3; # borrower category
115 my $value = $input->param($key);
116 if ($type eq 'delay') {
117 $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : '';
118 } else {
119 # type is letter
120 $temphash{$bor}->{"$type$num"} = $value if $value ne '';
125 # figure out which rows need to be deleted
126 my @rows_to_delete = grep { blank_row($_) } @category_codes;
128 foreach my $bor (keys %temphash){
129 # get category name if we need it for an error message
130 my $bor_category = Koha::Patron::Categories->find($bor);
131 my $bor_category_name = $bor_category ? $bor_category->description : $bor;
133 # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
134 # Raise error if not true
135 if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
136 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
137 $err=1;
138 } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
139 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
140 $err=1;
141 } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
142 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
143 $err=1;
144 } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
145 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
146 $err=1;
147 } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
148 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
149 $err=1;
150 } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
151 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
152 $err=1;
153 }elsif ($temphash{$bor}->{delay3} and
154 ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
155 or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
156 $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
157 $err=1;
159 unless ($err){
160 if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
161 or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
162 or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
163 $sth_search->execute($branch,$bor);
164 my $res = $sth_search->fetchrow_hashref();
165 if ($res->{'total'}>0) {
166 $sth_update->execute(
167 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef),
168 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
169 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
170 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef),
171 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
172 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
173 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef),
174 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
175 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
176 $branch ,$bor
178 } else {
179 $sth_insert->execute($branch,$bor,
180 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
181 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
182 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
183 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
184 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
185 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
186 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
187 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
188 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
192 $sth_delete_mtt->execute( $branch, $bor );
193 for my $letternumber ( 1..3 ) {
194 my @mtt = $input->multi_param( "mtt${letternumber}-$bor" );
195 next unless @mtt;
196 for my $mtt ( @mtt ) {
197 $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
203 unless ($err) {
204 for my $category_code (@rows_to_delete) {
205 $sth_delete->execute($branch, $category_code);
207 $template->param(datasaved => 1);
208 $input_saved = 1;
212 my $letters = C4::Letters::GetLettersAvailableForALibrary(
214 branchcode => $branch,
215 module => "circulation",
219 my @line_loop;
221 my $message_transport_types = C4::Letters::GetMessageTransportTypes();
222 my ( @first, @second, @third );
223 for my $patron_category (@patron_categories) {
224 if (%temphash and not $input_saved){
225 # if we managed to save the form submission, don't
226 # reuse %temphash, but take the values from the
227 # database - this makes it easier to identify
228 # bugs where the form submission was not correctly saved
229 for my $i ( 1..3 ){
230 my %row = (
231 overduename => $patron_category->categorycode,
232 line => $patron_category->description,
234 $row{delay}=$temphash{$patron_category->categorycode}->{"delay$i"};
235 $row{debarred}=$temphash{$patron_category->categorycode}->{"debarred$i"};
236 $row{selected_lettercode} = $temphash{ $patron_category->categorycode }->{"letter$i"};
237 my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) };
238 my @mtts;
239 for my $mtt ( @$message_transport_types ) {
240 push @mtts, {
241 value => $mtt,
242 selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
245 $row{message_transport_types} = \@mtts;
246 if ( $i == 1 ) {
247 push @first, \%row;
248 } elsif ( $i == 2 ) {
249 push @second, \%row;
250 } else {
251 push @third, \%row;
254 } else {
255 #getting values from table
256 my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
257 $sth2->execute($branch,$patron_category->categorycode);
258 my $dat=$sth2->fetchrow_hashref;
259 for my $i ( 1..3 ){
260 my %row = (
261 overduename => $patron_category->categorycode,
262 line => $patron_category->description,
265 $row{selected_lettercode} = $dat->{"letter$i"};
267 if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};}
268 if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};}
269 my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) };
270 my @mtts;
271 for my $mtt ( @$message_transport_types ) {
272 push @mtts, {
273 value => $mtt,
274 selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
277 $row{message_transport_types} = \@mtts;
278 if ( $i == 1 ) {
279 push @first, \%row;
280 } elsif ( $i == 2 ) {
281 push @second, \%row;
282 } else {
283 push @third, \%row;
290 my @tabs = (
292 id => 'first',
293 number => 1,
294 values => \@first,
297 id => 'second',
298 number => 2,
299 values => \@second,
302 id => 'third',
303 number => 3,
304 values => \@third,
308 $template->param(
309 table => ( @first or @second or @third ? 1 : 0 ),
310 branch => $branch,
311 tabs => \@tabs,
312 message_transport_types => $message_transport_types,
313 letters => $letters,
315 output_html_with_http_headers $input, $cookie, $template->output;