Bug 7143 - Fix name and link to Athens County Public Libraries
[koha.git] / tools / overduerules.pl
blob24019a56a8d0bb78e21a57ce052a8aed88a646b5
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 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
10 # version.
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.
20 use strict;
21 use warnings;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Branch;
28 use C4::Letters;
29 use C4::Members;
30 use C4::Overdues;
32 our $input = new CGI;
33 my $dbh = C4::Context->dbh;
35 my @categories = @{$dbh->selectall_arrayref(
36 'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0',
37 { Slice => {} }
38 )};
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.
43 sub blank_row {
44 my ($category_code) = @_;
45 for my $rp (@rule_params) {
46 for my $n (1 .. 3) {
47 my $key = "${rp}${n}-$category_code";
49 if (utf8::is_utf8($key)) {
50 utf8::encode($key);
53 my $value = $input->param($key);
54 if ($value) {
55 return 0;
59 return 1;
62 my $type=$input->param('type');
63 my $branch = $input->param('branch');
64 $branch ||= q{};
65 my $op = $input->param('op');
66 $op ||= q{};
68 my ($template, $loggedinuser, $cookie)
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;
78 # save the values entered into tables
79 my %temphash;
80 my $input_saved = 0;
81 if ($op eq 'save') {
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
91 ) VALUES (
92 ?, ?, ?, ?
94 ");
95 my $sth_delete_mtt = $dbh->prepare("
96 DELETE FROM overduerules_transport_types
97 WHERE branchcode = ? AND categorycode = ?
98 ");
100 foreach my $key (@names){
101 # ISSUES
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) : '';
109 } else {
110 # type is letter
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);
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;
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
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)
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);
194 unless ($err) {
195 for my $category_code (@rows_to_delete) {
196 $sth_delete->execute($branch, $category_code);
198 $template->param(datasaved => 1);
199 $input_saved = 1;
202 my $branchloop = GetBranchesLoop($branch);
204 my $letters = GetLetters({ module => "circulation" });
206 my @line_loop;
208 my $message_transport_types = C4::Letters::GetMessageTransportTypes();
209 my ( @first, @second, @third );
210 for my $data (@categories) {
211 if (%temphash and not $input_saved){
212 # if we managed to save the form submission, don't
213 # reuse %temphash, but take the values from the
214 # database - this makes it easier to identify
215 # bugs where the form submission was not correctly saved
216 for my $i ( 1..3 ){
217 my %row = (
218 overduename => $data->{'categorycode'},
219 line => $data->{'description'}
221 $row{delay}=$temphash{$data->{'categorycode'}}->{"delay$i"};
222 $row{debarred}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
223 $row{selected_lettercode} = $temphash{ $data->{categorycode} }->{"letter$i"};
224 my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
225 my @mtts;
226 for my $mtt ( @$message_transport_types ) {
227 push @mtts, {
228 value => $mtt,
229 selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
232 $row{message_transport_types} = \@mtts;
233 if ( $i == 1 ) {
234 push @first, \%row;
235 } elsif ( $i == 2 ) {
236 push @second, \%row;
237 } else {
238 push @third, \%row;
241 } else {
242 #getting values from table
243 my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
244 $sth2->execute($branch,$data->{'categorycode'});
245 my $dat=$sth2->fetchrow_hashref;
246 for my $i ( 1..3 ){
247 my %row = (
248 overduename => $data->{'categorycode'},
249 line => $data->{'description'}
252 $row{selected_lettercode} = $dat->{"letter$i"};
254 if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};}
255 if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};}
256 my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
257 my @mtts;
258 for my $mtt ( @$message_transport_types ) {
259 push @mtts, {
260 value => $mtt,
261 selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
264 $row{message_transport_types} = \@mtts;
265 if ( $i == 1 ) {
266 push @first, \%row;
267 } elsif ( $i == 2 ) {
268 push @second, \%row;
269 } else {
270 push @third, \%row;
277 my @tabs = (
279 id => 'first',
280 number => 1,
281 values => \@first,
284 id => 'second',
285 number => 2,
286 values => \@second,
289 id => 'third',
290 number => 3,
291 values => \@third,
295 $template->param(
296 table => ( @first or @second or @third ? 1 : 0 ),
297 branchloop => $branchloop,
298 branch => $branch,
299 tabs => \@tabs,
300 message_transport_types => $message_transport_types,
301 letters => $letters,
303 output_html_with_http_headers $input, $cookie, $template->output;