Bug 11011: Rephrasing 'in keyword' in OPAC authority search
[koha.git] / tools / overduerules.pl
blob463c73e1a7e8a708050c6a372672d819b06b6eed
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 strict;
21 use warnings;
22 use CGI qw ( -utf8 );
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 = C4::Letters::GetLettersAvailableForALibrary(
206 branchcode => $branch,
207 module => "circulation",
211 my @line_loop;
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
221 for my $i ( 1..3 ){
222 my %row = (
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) };
230 my @mtts;
231 for my $mtt ( @$message_transport_types ) {
232 push @mtts, {
233 value => $mtt,
234 selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
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;
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'}
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) };
262 my @mtts;
263 for my $mtt ( @$message_transport_types ) {
264 push @mtts, {
265 value => $mtt,
266 selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
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;
282 my @tabs = (
284 id => 'first',
285 number => 1,
286 values => \@first,
289 id => 'second',
290 number => 2,
291 values => \@second,
294 id => 'third',
295 number => 3,
296 values => \@third,
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,
308 output_html_with_http_headers $input, $cookie, $template->output;