Bug 19851: Improve responsive layout handling of staff client menu bar
[koha.git] / tools / overduerules.pl
blob5c35a8c7faeacc5b4fe2d1ad6cddeb4b7e1dc3f4
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::Letters;
28 use C4::Members;
29 use C4::Overdues;
30 use Koha::Libraries;
32 use Koha::Patron::Categories;
34 our $input = new CGI;
35 my $dbh = C4::Context->dbh;
37 my @patron_categories = Koha::Patron::Categories->search( { overduenoticerequired => { '>' => 0 } } );
38 my @category_codes = map { $_->categorycode } @patron_categories;
39 our @rule_params = qw(delay letter debarred);
41 # blank_row($category_code) - return true if the entire row is blank.
42 sub blank_row {
43 my ($category_code) = @_;
44 for my $rp (@rule_params) {
45 for my $n (1 .. 3) {
46 my $key = "${rp}${n}-$category_code";
48 if (utf8::is_utf8($key)) {
49 utf8::encode($key);
52 my $value = $input->param($key);
53 if ($value) {
54 return 0;
58 return 1;
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63 template_name => "tools/overduerules.tt",
64 query => $input,
65 type => "intranet",
66 authnotrequired => 0,
67 flagsrequired => { tools => 'edit_notice_status_triggers' },
68 debug => 1,
72 my $type = $input->param('type');
74 my $branch = $input->param('branch');
75 $branch =
76 defined $branch ? $branch
77 : C4::Context->preference('DefaultToLoggedInLibraryOverdueTriggers') ? C4::Context::mybranch()
78 : Koha::Libraries->search->count() == 1 ? undef
79 : undef;
80 $branch ||= q{};
81 $branch = q{} if $branch eq 'NO_LIBRARY_SET';
83 my $op = $input->param('op');
84 $op ||= q{};
86 my $err=0;
88 # save the values entered into tables
89 my %temphash;
90 my $input_saved = 0;
91 if ($op eq 'save') {
92 my @names=$input->multi_param();
93 my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
95 my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
96 my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
97 my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
98 my $sth_insert_mtt = $dbh->prepare("
99 INSERT INTO overduerules_transport_types(
100 overduerules_id, letternumber, message_transport_type
101 ) VALUES (
102 (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ?
105 my $sth_delete_mtt = $dbh->prepare("
106 DELETE FROM overduerules_transport_types
107 WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?)
110 foreach my $key (@names){
111 # ISSUES
112 if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
113 my $type = $1; # data type
114 my $num = $2; # From 1 to 3
115 my $bor = $3; # borrower category
116 my $value = $input->param($key);
117 if ($type eq 'delay') {
118 $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : '';
119 } else {
120 # type is letter
121 $temphash{$bor}->{"$type$num"} = $value if $value ne '';
126 # figure out which rows need to be deleted
127 my @rows_to_delete = grep { blank_row($_) } @category_codes;
129 foreach my $bor (keys %temphash){
130 # get category name if we need it for an error message
131 my $bor_category = Koha::Patron::Categories->find($bor);
132 my $bor_category_name = $bor_category ? $bor_category->description : $bor;
134 # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
135 # Raise error if not true
136 if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
137 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
138 $err=1;
139 } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
140 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
141 $err=1;
142 } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
143 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
144 $err=1;
145 } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
146 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
147 $err=1;
148 } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
149 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
150 $err=1;
151 } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
152 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
153 $err=1;
154 }elsif ($temphash{$bor}->{delay3} and
155 ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
156 or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
157 $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
158 $err=1;
160 unless ($err){
161 if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
162 or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
163 or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
164 $sth_search->execute($branch,$bor);
165 my $res = $sth_search->fetchrow_hashref();
166 if ($res->{'total'}>0) {
167 $sth_update->execute(
168 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef),
169 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
170 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
171 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef),
172 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
173 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
174 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef),
175 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
176 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
177 $branch ,$bor
179 } else {
180 $sth_insert->execute($branch,$bor,
181 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
182 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
183 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
184 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
185 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
186 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
187 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
188 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
189 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
193 $sth_delete_mtt->execute( $branch, $bor );
194 for my $letternumber ( 1..3 ) {
195 my @mtt = $input->multi_param( "mtt${letternumber}-$bor" );
196 next unless @mtt;
197 for my $mtt ( @mtt ) {
198 $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
204 unless ($err) {
205 for my $category_code (@rows_to_delete) {
206 $sth_delete->execute($branch, $category_code);
208 $template->param(datasaved => 1);
209 $input_saved = 1;
213 my $letters = C4::Letters::GetLettersAvailableForALibrary(
215 branchcode => $branch,
216 module => "circulation",
220 my @line_loop;
222 my $message_transport_types = C4::Letters::GetMessageTransportTypes();
223 my ( @first, @second, @third );
224 for my $patron_category (@patron_categories) {
225 if (%temphash and not $input_saved){
226 # if we managed to save the form submission, don't
227 # reuse %temphash, but take the values from the
228 # database - this makes it easier to identify
229 # bugs where the form submission was not correctly saved
230 for my $i ( 1..3 ){
231 my %row = (
232 overduename => $patron_category->categorycode,
233 line => $patron_category->description,
235 $row{delay}=$temphash{$patron_category->categorycode}->{"delay$i"};
236 $row{debarred}=$temphash{$patron_category->categorycode}->{"debarred$i"};
237 $row{selected_lettercode} = $temphash{ $patron_category->categorycode }->{"letter$i"};
238 my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) };
239 my @mtts;
240 for my $mtt ( @$message_transport_types ) {
241 push @mtts, {
242 value => $mtt,
243 selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
246 $row{message_transport_types} = \@mtts;
247 if ( $i == 1 ) {
248 push @first, \%row;
249 } elsif ( $i == 2 ) {
250 push @second, \%row;
251 } else {
252 push @third, \%row;
255 } else {
256 #getting values from table
257 my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
258 $sth2->execute($branch,$patron_category->categorycode);
259 my $dat=$sth2->fetchrow_hashref;
260 for my $i ( 1..3 ){
261 my %row = (
262 overduename => $patron_category->categorycode,
263 line => $patron_category->description,
266 $row{selected_lettercode} = $dat->{"letter$i"};
268 if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};}
269 if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};}
270 my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) };
271 my @mtts;
272 for my $mtt ( @$message_transport_types ) {
273 push @mtts, {
274 value => $mtt,
275 selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
278 $row{message_transport_types} = \@mtts;
279 if ( $i == 1 ) {
280 push @first, \%row;
281 } elsif ( $i == 2 ) {
282 push @second, \%row;
283 } else {
284 push @third, \%row;
291 my @tabs = (
293 id => 'first',
294 number => 1,
295 values => \@first,
298 id => 'second',
299 number => 2,
300 values => \@second,
303 id => 'third',
304 number => 3,
305 values => \@third,
309 $template->param(
310 table => ( @first or @second or @third ? 1 : 0 ),
311 branch => $branch,
312 tabs => \@tabs,
313 message_transport_types => $message_transport_types,
314 letters => $letters,
316 output_html_with_http_headers $input, $cookie, $template->output;